Make the arguments to ComposeInitatorHandshakeMessage comprehensible

This commit is contained in:
eyedeekay
2024-12-12 14:55:40 -05:00
parent 280c877d39
commit db91315582
5 changed files with 96 additions and 85 deletions

View File

@ -2,6 +2,7 @@ package lease
import (
"testing"
"github.com/stretchr/testify/assert"
. "github.com/go-i2p/go-i2p/lib/common/data"
@ -14,7 +15,7 @@ func TestTunnelGateway(t *testing.T) {
var lease_bytes []byte
lease_bytes = append(lease_bytes, expectedTunnelGatewayBytes...)
lease_bytes = append(lease_bytes, make([]byte, LEASE_SIZE - LEASE_TUNNEL_GW_SIZE)...)
lease_bytes = append(lease_bytes, make([]byte, LEASE_SIZE-LEASE_TUNNEL_GW_SIZE)...)
lease := Lease(lease_bytes)
tunnelGateway := lease.TunnelGateway()
@ -29,7 +30,7 @@ func TestTunnelID(t *testing.T) {
var lease_bytes []byte
lease_bytes = append(lease_bytes, make([]byte, LEASE_TUNNEL_GW_SIZE)...)
lease_bytes = append(lease_bytes, expectedTunnelIDBytes...)
lease_bytes = append(lease_bytes, make([]byte, LEASE_SIZE - LEASE_TUNNEL_ID_SIZE - LEASE_TUNNEL_GW_SIZE)...)
lease_bytes = append(lease_bytes, make([]byte, LEASE_SIZE-LEASE_TUNNEL_ID_SIZE-LEASE_TUNNEL_GW_SIZE)...)
lease := Lease(lease_bytes)
tunnelID := lease.TunnelID()
@ -42,7 +43,7 @@ func TestDate(t *testing.T) {
expectedDateBytes := []byte{0x21, 0x37, 0x31, 0x33, 0x16, 0x93, 0x13, 0x28}
var lease_bytes []byte
lease_bytes = append(lease_bytes, make([]byte, LEASE_TUNNEL_GW_SIZE + LEASE_TUNNEL_ID_SIZE)...)
lease_bytes = append(lease_bytes, make([]byte, LEASE_TUNNEL_GW_SIZE+LEASE_TUNNEL_ID_SIZE)...)
lease_bytes = append(lease_bytes, expectedDateBytes...)
lease := Lease(lease_bytes)

View File

@ -45,40 +45,50 @@ func (c *NoiseSession) RunOutgoingHandshake() error {
return nil
}
func (c *NoiseSession) ComposeInitiatorHandshakeMessage(s noise.DHKey, rs []byte, payload []byte, ePrivate []byte) (negData, msg []byte, state *noise.HandshakeState, err error) {
func (c *NoiseSession) ComposeInitiatorHandshakeMessage(
localStatic noise.DHKey,
remoteStatic []byte,
payload []byte,
ephemeralPrivate []byte,
) (
negotiationData,
handshakeMessage []byte,
handshakeState *noise.HandshakeState,
err error,
) {
log.Debug("Starting ComposeInitiatorHandshakeMessage")
if len(rs) != 0 && len(rs) != noise.DH25519.DHLen() {
if len(remoteStatic) != 0 && len(remoteStatic) != noise.DH25519.DHLen() {
return nil, nil, nil, errors.New("only 32 byte curve25519 public keys are supported")
}
negData = make([]byte, 6)
copy(negData, initNegotiationData(nil))
negotiationData = make([]byte, 6)
copy(negotiationData, initNegotiationData(nil))
pattern := noise.HandshakeXK
negData[5] = NOISE_PATTERN_XK
negotiationData[5] = NOISE_PATTERN_XK
var random io.Reader
if len(ePrivate) == 0 {
if len(ephemeralPrivate) == 0 {
random = rand.Reader
} else {
random = bytes.NewBuffer(ePrivate)
random = bytes.NewBuffer(ephemeralPrivate)
}
config := noise.Config{
CipherSuite: noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256),
Pattern: pattern,
Initiator: true,
StaticKeypair: s,
StaticKeypair: localStatic,
Random: random,
}
state, err = noise.NewHandshakeState(config)
handshakeState, err = noise.NewHandshakeState(config)
if err != nil {
return nil, nil, nil, err
}
// Write message, expecting no CipherStates yet since this is message 1
msg, cs0, cs1, err := state.WriteMessage(nil, payload)
handshakeMessage, cs0, cs1, err := handshakeState.WriteMessage(nil, payload)
if err != nil {
return nil, nil, nil, err
}
@ -88,5 +98,5 @@ func (c *NoiseSession) ComposeInitiatorHandshakeMessage(s noise.DHKey, rs []byte
return nil, nil, nil, errors.New("unexpected cipher states in message 1")
}
return negData, msg, state, nil
return negotiationData, handshakeMessage, handshakeState, nil
}

View File

@ -6,8 +6,8 @@ import (
"github.com/go-i2p/go-i2p/lib/config"
"github.com/go-i2p/go-i2p/lib/router"
"github.com/go-i2p/logger"
"github.com/go-i2p/go-i2p/lib/util/signals"
"github.com/go-i2p/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"