Skip to content

Commit

Permalink
make tests work
Browse files Browse the repository at this point in the history
  • Loading branch information
Varunram committed Dec 5, 2018
1 parent 89e3025 commit c618cee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
17 changes: 9 additions & 8 deletions lndc/noise.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ func newXKHandshakeState(initiator bool, prologue []byte,
// EphemeralGenerator is a functional option that allows callers to substitute
// a custom function for use when generating ephemeral keys for ActOne or
// ActTwo. The function closure return by this function can be passed into
// NewNoiseMachine as a function option parameter.
// NewNoiseMachine(XK or XX) as a function option parameter.
func EphemeralGenerator(gen func() (*koblitz.PrivateKey, error)) func(*Machine) {
return func(m *Machine) {
m.ephemeralGen = gen
m.EphemeralGen = gen
}
}

Expand Down Expand Up @@ -353,7 +353,8 @@ type Machine struct {
sendCipher cipherState
recvCipher cipherState

ephemeralGen func() (*koblitz.PrivateKey, error)
EphemeralGen func() (*koblitz.PrivateKey, error)
// this is exported in order for noise_test to define a static ephemeral pubkey

handshakeState

Expand All @@ -372,7 +373,7 @@ type Machine struct {
nextCipherText [math.MaxUint16 + macSize]byte
}

// NewNoiseMachine creates a new instance of the lndc state-machine. If
// NewNoiseXKMachine creates a new instance of the lndc state-machine. If
// the responder (listener) is creating the object, then the remotePub should
// be nil. The handshake state within lndc is initialized using the ascii
// string "lightning" as the prologue. The last parameter is a set of variadic
Expand All @@ -387,7 +388,7 @@ func NewNoiseXKMachine(initiator bool, localStatic *koblitz.PrivateKey,
m := &Machine{handshakeState: handshake}
// With the initial base machine created, we'll assign our default
// version of the ephemeral key generator.
m.ephemeralGen = func() (*koblitz.PrivateKey, error) {
m.EphemeralGen = func() (*koblitz.PrivateKey, error) {
return koblitz.NewPrivateKey(koblitz.S256())
}

Expand All @@ -402,7 +403,7 @@ func NewNoiseXXMachine(initiator bool, localStatic *koblitz.PrivateKey) *Machine

// With the initial base machine created, we'll assign our default
// version of the ephemeral key generator.
m.ephemeralGen = func() (*koblitz.PrivateKey, error) {
m.EphemeralGen = func() (*koblitz.PrivateKey, error) {
return koblitz.NewPrivateKey(koblitz.S256())
}

Expand Down Expand Up @@ -475,7 +476,7 @@ func (b *Machine) GenActOne(remotePK [33]byte) ([]byte, error) {
var err error
actOne := make([]byte, ActOneSize)
// Generate e
b.localEphemeral, err = b.ephemeralGen()
b.localEphemeral, err = b.EphemeralGen()
if err != nil {
return actOne, err
}
Expand Down Expand Up @@ -544,7 +545,7 @@ func (b *Machine) GenActTwo(HandshakeVersion byte) ([]byte, error) {
var err error
actTwo := make([]byte, ActTwoSize)
// e
b.localEphemeral, err = b.ephemeralGen()
b.localEphemeral, err = b.EphemeralGen()
if err != nil {
return actTwo, err
}
Expand Down
18 changes: 9 additions & 9 deletions lndc/noise_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func TestBolt0008TestVectors(t *testing.T) {
// EphemeralGenerator function for the state machine to ensure that the
// initiator and responder both generate the ephemeral public key
// defined within the test vectors.
initiatorEphemeral := EphemeralGenerator(func() (*koblitz.PrivateKey, error) {
initiatorEphemeral := func() (*koblitz.PrivateKey, error) {
e := "121212121212121212121212121212121212121212121212121212" +
"1212121212"
eBytes, err := hex.DecodeString(e)
Expand All @@ -320,8 +320,8 @@ func TestBolt0008TestVectors(t *testing.T) {

priv, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), eBytes)
return priv, nil
})
responderEphemeral := EphemeralGenerator(func() (*koblitz.PrivateKey, error) {
}
responderEphemeral := func() (*koblitz.PrivateKey, error) {
e := "222222222222222222222222222222222222222222222222222" +
"2222222222222"
eBytes, err := hex.DecodeString(e)
Expand All @@ -331,12 +331,14 @@ func TestBolt0008TestVectors(t *testing.T) {

priv, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), eBytes)
return priv, nil
})
}

// Finally, we'll create both brontide state machines, so we can begin
// our test.
initiator := NewNoiseMachine(true, initiatorPriv, initiatorEphemeral)
responder := NewNoiseMachine(false, responderPriv, responderEphemeral)
initiator := NewNoiseXXMachine(true, initiatorPriv)
initiator.EphemeralGen = initiatorEphemeral // needed for locking the ephemeral key
responder := NewNoiseXXMachine(false, responderPriv)
responder.EphemeralGen = responderEphemeral // needed for locking the ephemeral key

// We'll start with the initiator generating the initial payload for
// act one. This should consist of exactly 50 bytes. We'll assert that
Expand All @@ -347,9 +349,7 @@ func TestBolt0008TestVectors(t *testing.T) {
if err != nil {
t.Fatalf("unable to generate act one: %v", err)
}
expectedActOne, err := hex.DecodeString("01036360e856310ce5d294e" +
"8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f71432d5611e91" +
"ffea67c17e8d5ae0cbb3")
expectedActOne, err := hex.DecodeString("01036360e856310ce5d294e8be33fc807077dc56ac80d95d9cd4ddbd21325eff73f71432d5611e91ffea67c17e8d5ae0cbb3")
if err != nil {
t.Fatalf("unable to parse expected act one: %v", err)
}
Expand Down

0 comments on commit c618cee

Please sign in to comment.