2019-11-22 13:06:51 +01:00
|
|
|
import {assert} from "chai";
|
2019-11-22 12:52:59 +01:00
|
|
|
import Duplex from 'it-pair/duplex';
|
|
|
|
import {Buffer} from "buffer";
|
|
|
|
import Wrap from "it-pb-rpc";
|
2019-11-21 14:43:12 +01:00
|
|
|
|
2019-11-22 12:52:59 +01:00
|
|
|
import {Handshake} from "../src/handshake";
|
|
|
|
import {generateKeypair} from "../src/utils";
|
2019-11-28 17:32:46 +01:00
|
|
|
import {createPeerIds, createPeerIdsFromFixtures} from "./fixtures/peer";
|
|
|
|
|
2019-11-21 14:43:12 +01:00
|
|
|
|
|
|
|
describe("Handshake", () => {
|
|
|
|
it("should propose, exchange and finish handshake", async() => {
|
2019-11-22 12:52:59 +01:00
|
|
|
const duplex = Duplex();
|
|
|
|
const connectionFrom = Wrap(duplex[0]);
|
|
|
|
const connectionTo = Wrap(duplex[1]);
|
2019-11-21 14:43:12 +01:00
|
|
|
|
2019-11-22 12:52:59 +01:00
|
|
|
const prologue = Buffer.from('/noise');
|
|
|
|
const staticKeysInitiator = generateKeypair();
|
|
|
|
const staticKeysResponder = generateKeypair();
|
2019-11-28 17:32:46 +01:00
|
|
|
const [peerA, peerB] = await createPeerIds(2);
|
|
|
|
|
|
|
|
const initiatorPrivKey = peerA.privKey.marshal().slice(0, 32);
|
|
|
|
const initiatorPubKey = peerA.pubKey.marshal();
|
|
|
|
const handshakeInitator = new Handshake(true, initiatorPrivKey, initiatorPubKey, prologue, staticKeysInitiator, connectionFrom);
|
2019-11-22 12:52:59 +01:00
|
|
|
|
2019-11-28 17:32:46 +01:00
|
|
|
const responderPrivKey = peerB.privKey.marshal().slice(0, 32);
|
|
|
|
const responderPubKey = peerB.pubKey.marshal();
|
|
|
|
const handshakeResponder = new Handshake(false, responderPrivKey, responderPubKey, prologue, staticKeysResponder, connectionTo);
|
2019-11-22 12:52:59 +01:00
|
|
|
|
2019-11-28 17:32:46 +01:00
|
|
|
await handshakeInitator.propose();
|
|
|
|
await handshakeResponder.propose();
|
2019-11-22 12:52:59 +01:00
|
|
|
|
2019-11-28 17:32:46 +01:00
|
|
|
await handshakeResponder.exchange();
|
|
|
|
await handshakeInitator.exchange();
|
2019-11-22 12:52:59 +01:00
|
|
|
|
2019-11-28 17:32:46 +01:00
|
|
|
await handshakeInitator.finish();
|
|
|
|
await handshakeResponder.finish();
|
2019-11-22 12:52:59 +01:00
|
|
|
|
2019-11-28 17:32:46 +01:00
|
|
|
const sessionInitator = handshakeInitator.session;
|
|
|
|
const sessionResponder = handshakeResponder.session;
|
|
|
|
|
|
|
|
// Test shared key
|
2019-11-22 13:06:51 +01:00
|
|
|
if (sessionInitator.cs1 && sessionResponder.cs1 && sessionInitator.cs2 && sessionResponder.cs2) {
|
|
|
|
assert(sessionInitator.cs1.k.equals(sessionResponder.cs1.k));
|
|
|
|
assert(sessionInitator.cs2.k.equals(sessionResponder.cs2.k));
|
|
|
|
} else {
|
|
|
|
assert(false);
|
|
|
|
}
|
2019-11-28 17:32:46 +01:00
|
|
|
|
|
|
|
// Test encryption and decryption
|
|
|
|
const encrypted = handshakeInitator.encrypt(Buffer.from("encryptthis"), handshakeInitator.session);
|
|
|
|
const decrypted = handshakeResponder.decrypt(encrypted, handshakeResponder.session);
|
|
|
|
assert(decrypted.equals(Buffer.from("encryptthis")));
|
2019-11-22 13:06:51 +01:00
|
|
|
});
|
2019-11-21 14:43:12 +01:00
|
|
|
});
|