simple-handshake
Simple Noise handshake state machine
Usage
var simpleHandshake = require('simple-handshake')
var clientKeys = simpleHandshake.keygen()
var serverKeys = simpleHandshake.keygen()
var client = simpleHandshake(true, {
pattern: 'XX',
staticKeyPair: clientKeys
})
var server = simpleHandshake(false, {
pattern: 'XX',
staticKeyPair: serverKeys
})
rtt(client, server, function (err) {
if (err) throw err
console.log(client.split, server.split)
})
function rtt (from, to, cb) {
if (from.waiting !== false) return cb(new Error('Not ready to send data'))
from.send(null, function (err, buf) {
if (err) return cb(err)
to.recv(buf, function (err) {
if (err) return cb(err)
if (from.finished === true) return cb()
return rtt(to, from, cb)
})
})
}
API
var hs = simpleHandshake(isInitiator, [opts])
Options include:
{
pattern: 'XX',
prologue: Buffer.alloc(0),
staticKeyPair: {publicKey, secretKey},
remoteStaticKey: Buffer,
onstatickey(remoteStaticKey, cb),
ephemeralKeyPair: {publicKey, secretKey},
remoteEphemeralKey: Buffer
}
hs.waiting
Flag indicating whether this instance is waiting for remote data, ie. hs.recv
should be called next. If false
hs.send
should be called next.
hs.finished
Flag indicating whether the handshake is finished. If an error occurs this flag
will also be set true
, as the instance is no longer usable.
hs.split
A Noise split containing a {rx, tx}
object of Buffers which are
32 byte shared secret | 8 byte nonce
(a Noise CipherState
). rx
at the
initiator matches tx
at the responder.
hs.handshakeHash
Channel binding handshake hash, available after the handshake has completed and
a split has occurred. See the Noise Specification for details
hs.send(payload, cb(err, message))
Encode a message with a payload
(which if null
defaults to an empty buffer),
for sending to the other party. Message is written in a preallocated Buffer,
meaning that the backing Buffer is reused at the next call to .send
.
hs.recv(message, cb(err, payload))
Decode a message
with a payload
(which may be an empty buffer). payload
is
written in a preallocated Buffer, meaning that the backing Buffer for is reused
at the next call to .recv
, so you must copy the payload if you need it for
longer. If a static key is received and onstatickey
is set, this function is
called between parsing and cb
.
hs.destroy()
Destroy internal state in case you need to terminate the handshake before it has
completed.
SimpleHandshake.keygen()
Generate a key pair for use with the staticKeyPair
option
Install
npm install simple-handshake
License
ISC