js-libp2p-noise/test/ik-handshake.spec.ts

80 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-06-19 12:49:40 +02:00
import Wrap from 'it-pb-rpc'
import Duplex from 'it-pair/duplex'
import { Buffer } from 'buffer'
import { assert, expect } from 'chai'
2020-01-15 11:32:40 +01:00
2020-06-19 12:49:40 +02:00
import { createPeerIdsFromFixtures } from './fixtures/peer'
import { generateKeypair, getPayload } from '../src/utils'
import { IKHandshake } from '../src/handshake-ik'
2020-01-15 11:32:40 +01:00
2020-06-19 12:49:40 +02:00
describe('IK Handshake', () => {
2020-06-19 13:06:31 +02:00
let peerA, peerB
2020-01-15 11:32:40 +01:00
before(async () => {
2020-06-19 13:06:31 +02:00
[peerA, peerB] = await createPeerIdsFromFixtures(3)
2020-06-19 12:49:40 +02:00
})
2020-01-15 11:32:40 +01:00
2020-06-19 12:49:40 +02:00
it('should finish both stages as initiator and responder', async () => {
2020-01-15 11:32:40 +01:00
try {
2020-06-19 12:49:40 +02:00
const duplex = Duplex()
const connectionFrom = Wrap(duplex[0])
const connectionTo = Wrap(duplex[1])
2020-01-15 11:32:40 +01:00
2020-06-19 12:49:40 +02:00
const prologue = Buffer.alloc(0)
const staticKeysInitiator = generateKeypair()
const staticKeysResponder = generateKeypair()
2020-01-15 11:32:40 +01:00
2020-06-19 12:49:40 +02:00
const initPayload = await getPayload(peerA, staticKeysInitiator.publicKey)
const handshakeInit = new IKHandshake(true, initPayload, prologue, staticKeysInitiator, connectionFrom, staticKeysResponder.publicKey, peerB)
2020-01-15 11:32:40 +01:00
2020-06-19 12:49:40 +02:00
const respPayload = await getPayload(peerB, staticKeysResponder.publicKey)
const handshakeResp = new IKHandshake(false, respPayload, prologue, staticKeysResponder, connectionTo, staticKeysInitiator.publicKey)
2020-01-15 11:32:40 +01:00
2020-06-19 12:49:40 +02:00
await handshakeInit.stage0()
await handshakeResp.stage0()
2020-01-15 11:32:40 +01:00
2020-06-19 12:49:40 +02:00
await handshakeResp.stage1()
await handshakeInit.stage1()
2020-01-15 11:32:40 +01:00
// Test shared key
if (handshakeInit.session.cs1 && handshakeResp.session.cs1 && handshakeInit.session.cs2 && handshakeResp.session.cs2) {
2020-06-19 12:49:40 +02:00
assert(handshakeInit.session.cs1.k.equals(handshakeResp.session.cs1.k))
assert(handshakeInit.session.cs2.k.equals(handshakeResp.session.cs2.k))
2020-01-15 11:32:40 +01:00
} else {
2020-06-19 12:49:40 +02:00
assert(false)
2020-01-15 11:32:40 +01:00
}
// Test encryption and decryption
2020-06-19 12:49:40 +02:00
const encrypted = handshakeInit.encrypt(Buffer.from('encryptthis'), handshakeInit.session)
const { plaintext: decrypted } = handshakeResp.decrypt(encrypted, handshakeResp.session)
assert(decrypted.equals(Buffer.from('encryptthis')))
2020-01-15 11:32:40 +01:00
} catch (e) {
2020-06-19 12:49:40 +02:00
assert(false, e.message)
2020-01-15 11:32:40 +01:00
}
2020-06-19 12:49:40 +02:00
})
2020-01-15 11:40:32 +01:00
2020-06-19 12:49:40 +02:00
it("should throw error if responder's static key changed", async () => {
2020-01-15 11:40:32 +01:00
try {
2020-06-19 12:49:40 +02:00
const duplex = Duplex()
const connectionFrom = Wrap(duplex[0])
const connectionTo = Wrap(duplex[1])
2020-01-15 11:40:32 +01:00
2020-06-19 12:49:40 +02:00
const prologue = Buffer.alloc(0)
const staticKeysInitiator = generateKeypair()
const staticKeysResponder = generateKeypair()
const oldScammyKeys = generateKeypair()
2020-01-15 11:40:32 +01:00
2020-06-19 12:49:40 +02:00
const initPayload = await getPayload(peerA, staticKeysInitiator.publicKey)
const handshakeInit = new IKHandshake(true, initPayload, prologue, staticKeysInitiator, connectionFrom, oldScammyKeys.publicKey, peerB)
2020-01-15 11:40:32 +01:00
2020-06-19 12:49:40 +02:00
const respPayload = await getPayload(peerB, staticKeysResponder.publicKey)
const handshakeResp = new IKHandshake(false, respPayload, prologue, staticKeysResponder, connectionTo, staticKeysInitiator.publicKey)
2020-01-15 11:40:32 +01:00
2020-06-19 12:49:40 +02:00
await handshakeInit.stage0()
await handshakeResp.stage0()
2020-01-15 11:40:32 +01:00
} catch (e) {
2020-06-19 12:49:40 +02:00
expect(e.message).to.include("Error occurred while verifying initiator's signed payload")
2020-01-15 11:40:32 +01:00
}
2020-06-19 12:49:40 +02:00
})
})