Skip to content

Commit d9305fc

Browse files
authored
feat: add support for XK handshake pattern (#18)
1 parent 5c868df commit d9305fc

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

noise.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ const HANDSHAKES = Object.freeze({
4040
PRESHARE_RS,
4141
[TOK_E, TOK_ES, TOK_S, TOK_SS],
4242
[TOK_E, TOK_EE, TOK_SE]
43+
],
44+
XK: [
45+
PRESHARE_RS,
46+
[TOK_E, TOK_ES],
47+
[TOK_E, TOK_EE],
48+
[TOK_S, TOK_SE]
4349
]
4450
})
4551

test/handshake.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,72 @@ test('XXpsk0: good', t => {
185185
t.alike(initiator.tx, responder.rx)
186186
t.end()
187187
})
188+
189+
test('XK', t => {
190+
const initiator = new NoiseState('XK', true, null)
191+
const responder = new NoiseState('XK', false, null)
192+
193+
initiator.initialise(Buffer.alloc(0), responder.s.publicKey)
194+
responder.initialise(Buffer.alloc(0))
195+
196+
const message = initiator.send()
197+
responder.recv(message)
198+
199+
const reply = responder.send()
200+
initiator.recv(reply)
201+
202+
const initiatorReply = initiator.send()
203+
responder.recv(initiatorReply)
204+
205+
t.alike(initiator.complete, true)
206+
t.alike(responder.complete, true)
207+
208+
t.unlike(initiator.rx, null)
209+
t.unlike(initiator.tx, null)
210+
211+
t.alike(initiator.rs, responder.s.publicKey)
212+
t.alike(responder.rs, initiator.s.publicKey)
213+
t.alike(initiator.rx, responder.tx)
214+
t.alike(initiator.tx, responder.rx)
215+
t.end()
216+
})
217+
218+
test('XK: bad preshared key', t => {
219+
t.plan(1)
220+
221+
const initiator = new NoiseState('XK', true, null)
222+
const responder = new NoiseState('XK', false, null)
223+
const wrongResponder = new NoiseState('XK', false, null)
224+
225+
// Initiator has wrong responder's static key
226+
initiator.initialise(Buffer.alloc(0), wrongResponder.s.publicKey)
227+
responder.initialise(Buffer.alloc(0))
228+
229+
const message = initiator.send()
230+
t.exception(() => responder.recv(message), 'could not verify data')
231+
})
232+
233+
test('XK: missing preshared key', t => {
234+
t.plan(1)
235+
236+
const initiator = new NoiseState('XK', true, null)
237+
// Initiator must provide responder's static key for XK
238+
t.exception(() => initiator.initialise(Buffer.alloc(0)), 'Remote pubkey required')
239+
})
240+
241+
test('XK: tampered message', t => {
242+
t.plan(1)
243+
244+
const initiator = new NoiseState('XK', true, null)
245+
const responder = new NoiseState('XK', false, null)
246+
247+
initiator.initialise(Buffer.alloc(0), responder.s.publicKey)
248+
responder.initialise(Buffer.alloc(0))
249+
250+
const message = initiator.send()
251+
252+
// Flip a bit in the message
253+
message[message.length - 1] ^= 1
254+
255+
t.exception(() => responder.recv(message), 'could not verify data')
256+
})

0 commit comments

Comments
 (0)