1
0
mirror of https://github.com/fluencelabs/js-libp2p-noise synced 2025-05-09 11:17:30 +00:00

110 lines
4.2 KiB
TypeScript
Raw Normal View History

2019-11-27 14:19:35 +01:00
import { expect, assert } from "chai";
2019-11-12 14:07:25 +01:00
import DuplexPair from 'it-pair/duplex';
2019-11-11 15:39:09 +01:00
import { Noise } from "../src";
2019-11-26 10:52:30 +01:00
import { generateEd25519Keys } from "./utils";
import {createPeerIds, createPeerIdsFromFixtures} from "./fixtures/peer";
import Wrap from "it-pb-rpc";
import {Handshake} from "../src/handshake";
import {
createHandshakePayload,
decodeMessageBuffer,
encodeMessageBuffer,
generateKeypair,
getHandshakePayload,
signPayload
} from "../src/utils";
import {XXHandshake} from "../src/xx";
import {Buffer} from "buffer";
2019-11-11 15:39:09 +01:00
describe("Noise", () => {
2019-11-26 10:52:30 +01:00
let remotePeer, localPeer;
before(async () => {
2019-11-26 14:14:10 +01:00
// [remotePeer, localPeer] = await createPeerIds(2);
// TODO: Handle Peer ID received ed25519 keys
const pair1 = generateKeypair();
remotePeer = {
id: "id-1",
pubKey: { bytes: pair1.publicKey },
privKey: { bytes: pair1.privateKey },
}
const pair2 = generateKeypair();
localPeer = {
id: "id-2",
pubKey: { bytes: pair2.publicKey },
privKey: { bytes: pair2.privateKey },
}
2019-11-26 10:52:30 +01:00
});
2019-11-26 15:24:10 +01:00
it("should communicate through encrypted streams", async() => {
2019-11-26 10:52:30 +01:00
const libp2pKeys = await generateEd25519Keys();
2019-11-26 15:24:10 +01:00
const libp2pKeys2 = await generateEd25519Keys();
2019-11-26 10:52:30 +01:00
2019-11-26 15:24:10 +01:00
const noiseInit = new Noise(libp2pKeys._key, localPeer.privKey.bytes);
const noiseResp = new Noise(libp2pKeys2._key, remotePeer.privKey.bytes);
2019-11-26 10:52:30 +01:00
const [inboundConnection, outboundConnection] = DuplexPair();
2019-11-26 14:14:10 +01:00
const [outbound, inbound] = await Promise.all([
2019-11-26 15:24:10 +01:00
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer),
2019-11-26 10:52:30 +01:00
]);
2019-11-26 14:14:10 +01:00
const wrappedInbound = Wrap(inbound.conn);
const wrappedOutbound = Wrap(outbound.conn);
wrappedOutbound.writeLP(Buffer.from("test"));
2019-11-26 15:24:10 +01:00
const response = await wrappedInbound.readLP();
expect(response.toString()).equal("test");
2019-11-27 08:39:06 +01:00
});
2019-11-26 15:24:10 +01:00
2019-11-27 08:39:06 +01:00
it("should test that secureOutbound is spec compliant", async() => {
2019-11-26 15:24:10 +01:00
const libp2pKeys = await generateEd25519Keys();
const noiseInit = new Noise(libp2pKeys._key, localPeer.privKey.bytes);
const [inboundConnection, outboundConnection] = DuplexPair();
2019-11-27 14:19:35 +01:00
const [outbound, { wrapped, ns, handshake }] = await Promise.all([
2019-11-27 08:39:06 +01:00
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
2019-11-26 15:24:10 +01:00
(async () => {
const wrapped = Wrap(inboundConnection);
const prologue = Buffer.from('/noise');
const staticKeys = {
privateKey: remotePeer.privKey.bytes,
publicKey: remotePeer.pubKey.bytes,
};
2019-11-27 14:19:35 +01:00
const xx = new XXHandshake();
const handshake = new Handshake('XX', false, localPeer.pubKey.bytes, prologue, staticKeys, wrapped, xx);
const ns = await xx.initSession(false, prologue, staticKeys, localPeer.pubKey.bytes);
2019-11-26 15:24:10 +01:00
2019-11-27 14:19:35 +01:00
let receivedMessageBuffer = decodeMessageBuffer((await wrapped.readLP()).slice());
// The first handshake message contains the initiator's ephemeral public key
expect(receivedMessageBuffer.ne.length).equal(32);
await xx.recvMessage(ns, receivedMessageBuffer);
2019-11-26 15:24:10 +01:00
2019-11-27 14:19:35 +01:00
// Stage 1
const signedPayload = signPayload(staticKeys.privateKey, getHandshakePayload(staticKeys.publicKey));
const handshakePayload = await createHandshakePayload(localPeer.pubKey.bytes, signedPayload);
const messageBuffer = await xx.sendMessage(ns, handshakePayload);
wrapped.writeLP(encodeMessageBuffer(messageBuffer));
// Stage 2 - finish handshake
receivedMessageBuffer = decodeMessageBuffer((await wrapped.readLP()).slice());
await xx.recvMessage(ns, receivedMessageBuffer);
return { wrapped, ns, handshake };
2019-11-26 15:24:10 +01:00
})(),
]);
2019-11-27 14:19:35 +01:00
const wrappedOutbound = Wrap(outbound.conn);
wrappedOutbound.write(Buffer.from("test"));
// Check that noise message is prefixed with 16-bit big-endian unsigned integer
const receivedEncryptedPayload = (await wrapped.read()).slice();
const dataLength = receivedEncryptedPayload.readInt16BE(0);
const data = receivedEncryptedPayload.slice(2, dataLength + 2);
const decrypted = handshake.decrypt(data, ns);
// Decrypted data should match
assert(decrypted.equals(Buffer.from("test")));
2019-11-11 15:39:09 +01:00
})
});