brontide: add EphemeralGenerator as a functional arg to NewBrontideMachine
This commit modifies the NewBrontideMachine constructor to allow a caller to specify exactly _how_ new ephemeral private keys for the crypto handshake are generated. This allows callers a bit more flexibility when using brontide, and also allow test cases to insert specific public keys for use within the hand shake.
This commit is contained in:
parent
399d9c974f
commit
e375a308b9
@ -303,6 +303,16 @@ func newHandshakeState(initiator bool, prologue []byte,
|
|||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// NewBrontideMachine as a function option parameter.
|
||||||
|
func EphemeralGenerator(gen func() (*btcec.PrivateKey, error)) func(*Machine) {
|
||||||
|
return func(m *Machine) {
|
||||||
|
m.ephemeralGen = gen
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Machine is a state-machine which implements Brontide: an
|
// Machine is a state-machine which implements Brontide: an
|
||||||
// Authenticated-key Exchange in Three Acts. Brontide is derived from the Noise
|
// Authenticated-key Exchange in Three Acts. Brontide is derived from the Noise
|
||||||
// framework, specifically implementing the Noise_XK handshake. Once the
|
// framework, specifically implementing the Noise_XK handshake. Once the
|
||||||
@ -332,20 +342,38 @@ type Machine struct {
|
|||||||
sendCipher cipherState
|
sendCipher cipherState
|
||||||
recvCipher cipherState
|
recvCipher cipherState
|
||||||
|
|
||||||
|
ephemeralGen func() (*btcec.PrivateKey, error)
|
||||||
|
|
||||||
handshakeState
|
handshakeState
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBrontideMachine creates a new instance of the brontide state-machine. If
|
// NewBrontideMachine creates a new instance of the brontide state-machine. If
|
||||||
// the responder (listener) is creating the object, then the remotePub should
|
// the responder (listener) is creating the object, then the remotePub should
|
||||||
// be nil. The handshake state within brontide is initialized using the ascii
|
// be nil. The handshake state within brontide is initialized using the ascii
|
||||||
// string "bitcoin" as the prologue.
|
// string "bitcoin" as the prologue. The last parameter is a set of variadic
|
||||||
|
// arguments for adding additional options to the brontide Machine
|
||||||
|
// initialization.
|
||||||
func NewBrontideMachine(initiator bool, localPub *btcec.PrivateKey,
|
func NewBrontideMachine(initiator bool, localPub *btcec.PrivateKey,
|
||||||
remotePub *btcec.PublicKey) *Machine {
|
remotePub *btcec.PublicKey, options ...func(*Machine)) *Machine {
|
||||||
|
|
||||||
handshake := newHandshakeState(initiator, []byte("lightning"), localPub,
|
handshake := newHandshakeState(initiator, []byte("lightning"), localPub,
|
||||||
remotePub)
|
remotePub)
|
||||||
|
|
||||||
return &Machine{handshakeState: handshake}
|
m := &Machine{handshakeState: handshake}
|
||||||
|
|
||||||
|
// With the initial base machine created, we'll assign our default
|
||||||
|
// version of the ephemeral key generator.
|
||||||
|
m.ephemeralGen = func() (*btcec.PrivateKey, error) {
|
||||||
|
return btcec.NewPrivateKey(btcec.S256())
|
||||||
|
}
|
||||||
|
|
||||||
|
// With the default options established, we'll now process all the
|
||||||
|
// options passed in as parameters.
|
||||||
|
for _, option := range options {
|
||||||
|
option(m)
|
||||||
|
}
|
||||||
|
|
||||||
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -392,7 +420,7 @@ func (b *Machine) GenActOne() ([ActOneSize]byte, error) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// e
|
// e
|
||||||
b.localEphemeral, err = btcec.NewPrivateKey(btcec.S256())
|
b.localEphemeral, err = b.ephemeralGen()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return actOne, err
|
return actOne, err
|
||||||
}
|
}
|
||||||
@ -465,7 +493,7 @@ func (b *Machine) GenActTwo() ([ActTwoSize]byte, error) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
// e
|
// e
|
||||||
b.localEphemeral, err = btcec.NewPrivateKey(btcec.S256())
|
b.localEphemeral, err = b.ephemeralGen()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return actTwo, err
|
return actTwo, err
|
||||||
}
|
}
|
||||||
@ -570,9 +598,6 @@ func (b *Machine) RecvActThree(actThree [ActThreeSize]byte) error {
|
|||||||
actThree[:])
|
actThree[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(roasbeef): print out entire version each time, also print out
|
|
||||||
// which act the error occurred at
|
|
||||||
|
|
||||||
copy(s[:], actThree[1:33+16+1])
|
copy(s[:], actThree[1:33+16+1])
|
||||||
copy(p[:], actThree[33+16+1:])
|
copy(p[:], actThree[33+16+1:])
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user