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 ( import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
. "github.com/go-i2p/go-i2p/lib/common/data" . "github.com/go-i2p/go-i2p/lib/common/data"

View File

@ -45,40 +45,50 @@ func (c *NoiseSession) RunOutgoingHandshake() error {
return nil 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") 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") return nil, nil, nil, errors.New("only 32 byte curve25519 public keys are supported")
} }
negData = make([]byte, 6) negotiationData = make([]byte, 6)
copy(negData, initNegotiationData(nil)) copy(negotiationData, initNegotiationData(nil))
pattern := noise.HandshakeXK pattern := noise.HandshakeXK
negData[5] = NOISE_PATTERN_XK negotiationData[5] = NOISE_PATTERN_XK
var random io.Reader var random io.Reader
if len(ePrivate) == 0 { if len(ephemeralPrivate) == 0 {
random = rand.Reader random = rand.Reader
} else { } else {
random = bytes.NewBuffer(ePrivate) random = bytes.NewBuffer(ephemeralPrivate)
} }
config := noise.Config{ config := noise.Config{
CipherSuite: noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256), CipherSuite: noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256),
Pattern: pattern, Pattern: pattern,
Initiator: true, Initiator: true,
StaticKeypair: s, StaticKeypair: localStatic,
Random: random, Random: random,
} }
state, err = noise.NewHandshakeState(config) handshakeState, err = noise.NewHandshakeState(config)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
// Write message, expecting no CipherStates yet since this is message 1 // 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 { if err != nil {
return nil, nil, nil, err 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 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/config"
"github.com/go-i2p/go-i2p/lib/router" "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/go-i2p/lib/util/signals"
"github.com/go-i2p/logger"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"