mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 14:12:30 +00:00
Create test
This commit is contained in:
parent
b5941c750c
commit
d03f4974ba
2
src/@types/it-pb-rpc/index.d.ts
vendored
2
src/@types/it-pb-rpc/index.d.ts
vendored
@ -2,7 +2,7 @@ declare module "it-pb-rpc" {
|
|||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
import { Duplex } from "it-pair";
|
import { Duplex } from "it-pair";
|
||||||
type WrappedDuplex = {
|
type WrappedDuplex = {
|
||||||
read(bytes: number): Promise<Buffer>,
|
read(bytes?: number): Promise<Buffer>,
|
||||||
readLP(): Promise<Buffer>,
|
readLP(): Promise<Buffer>,
|
||||||
write(input: Buffer): void,
|
write(input: Buffer): void,
|
||||||
writeLP(input: Buffer): void,
|
writeLP(input: Buffer): void,
|
||||||
|
@ -32,6 +32,7 @@ export class Handshake {
|
|||||||
prologue: bytes32,
|
prologue: bytes32,
|
||||||
staticKeys: KeyPair,
|
staticKeys: KeyPair,
|
||||||
connection: WrappedConnection,
|
connection: WrappedConnection,
|
||||||
|
handshake?: XXHandshake,
|
||||||
) {
|
) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.isInitiator = isInitiator;
|
this.isInitiator = isInitiator;
|
||||||
@ -40,7 +41,7 @@ export class Handshake {
|
|||||||
this.staticKeys = staticKeys;
|
this.staticKeys = staticKeys;
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
|
|
||||||
this.xx = new XXHandshake();
|
this.xx = handshake || new XXHandshake();
|
||||||
}
|
}
|
||||||
|
|
||||||
// stage 0
|
// stage 0
|
||||||
@ -55,8 +56,7 @@ export class Handshake {
|
|||||||
earlyData,
|
earlyData,
|
||||||
this.staticKeys.privateKey
|
this.staticKeys.privateKey
|
||||||
);
|
);
|
||||||
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
|
const messageBuffer = await this.xx.sendMessage(ns, handshakePayload);
|
||||||
const messageBuffer = await this.xx.sendMessage(ns, message);
|
|
||||||
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
||||||
|
|
||||||
logger("Stage 0 - Initiator finished proposing");
|
logger("Stage 0 - Initiator finished proposing");
|
||||||
@ -80,8 +80,7 @@ export class Handshake {
|
|||||||
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
|
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey));
|
||||||
const handshakePayload = await createHandshakePayload(this.remotePublicKey, signedPayload);
|
const handshakePayload = await createHandshakePayload(this.remotePublicKey, signedPayload);
|
||||||
|
|
||||||
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
|
const messageBuffer = await this.xx.sendMessage(session, handshakePayload);
|
||||||
const messageBuffer = await this.xx.sendMessage(session, message);
|
|
||||||
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
this.connection.writeLP(encodeMessageBuffer(messageBuffer));
|
||||||
logger('Stage 1 - Responder sent the message.')
|
logger('Stage 1 - Responder sent the message.')
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,9 @@ import DuplexPair from 'it-pair/duplex';
|
|||||||
import ensureBuffer from 'it-buffer';
|
import ensureBuffer from 'it-buffer';
|
||||||
import pipe from 'it-pipe';
|
import pipe from 'it-pipe';
|
||||||
import lp from 'it-length-prefixed';
|
import lp from 'it-length-prefixed';
|
||||||
const { int16BEEncode, int16BEDecode } = lp;
|
|
||||||
|
|
||||||
import { Handshake } from "./handshake";
|
import { Handshake } from "./handshake";
|
||||||
import { generateKeypair } from "./utils";
|
import { generateKeypair, int16BEDecode, int16BEEncode } from "./utils";
|
||||||
import { decryptStream, encryptStream } from "./crypto";
|
import { decryptStream, encryptStream } from "./crypto";
|
||||||
import { bytes } from "./@types/basic";
|
import { bytes } from "./@types/basic";
|
||||||
import { NoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p";
|
import { NoiseConnection, PeerId, KeyPair, SecureOutbound } from "./@types/libp2p";
|
||||||
|
10
src/utils.ts
10
src/utils.ts
@ -74,3 +74,13 @@ export function decodeMessageBuffer(message: bytes) : MessageBuffer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const int16BEEncode = (value, target, offset) => {
|
||||||
|
target = target || Buffer.allocUnsafe(2);
|
||||||
|
return target.writeInt16BE(value, offset);
|
||||||
|
};
|
||||||
|
int16BEEncode.bytes = 2;
|
||||||
|
|
||||||
|
export const int16BEDecode = data => {
|
||||||
|
if (data.length < 2) throw RangeError('Could not decode int16BE');
|
||||||
|
return data.readInt16BE(0);}
|
||||||
|
int16BEDecode.bytes = 2;
|
||||||
|
@ -308,6 +308,7 @@ export class XXHandshake {
|
|||||||
throw new Error("Handshake state `e` param is missing.");
|
throw new Error("Handshake state `e` param is missing.");
|
||||||
}
|
}
|
||||||
this.mixKey(hs.ss, this.dh(hs.e.privateKey, hs.rs));
|
this.mixKey(hs.ss, this.dh(hs.e.privateKey, hs.rs));
|
||||||
|
|
||||||
const plaintext = await this.decryptAndHash(hs.ss, message.ciphertext);
|
const plaintext = await this.decryptAndHash(hs.ss, message.ciphertext);
|
||||||
const { cs1, cs2 } = this.split(hs.ss);
|
const { cs1, cs2 } = this.split(hs.ss);
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { expect } from "chai";
|
import { expect, assert } from "chai";
|
||||||
import DuplexPair from 'it-pair/duplex';
|
import DuplexPair from 'it-pair/duplex';
|
||||||
|
|
||||||
import { Noise } from "../src";
|
import { Noise } from "../src";
|
||||||
@ -63,7 +63,7 @@ describe("Noise", () => {
|
|||||||
const noiseInit = new Noise(libp2pKeys._key, localPeer.privKey.bytes);
|
const noiseInit = new Noise(libp2pKeys._key, localPeer.privKey.bytes);
|
||||||
const [inboundConnection, outboundConnection] = DuplexPair();
|
const [inboundConnection, outboundConnection] = DuplexPair();
|
||||||
|
|
||||||
const [outbound] = await Promise.all([
|
const [outbound, { wrapped, ns, handshake }] = await Promise.all([
|
||||||
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
|
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
|
||||||
(async () => {
|
(async () => {
|
||||||
const wrapped = Wrap(inboundConnection);
|
const wrapped = Wrap(inboundConnection);
|
||||||
@ -72,16 +72,38 @@ describe("Noise", () => {
|
|||||||
privateKey: remotePeer.privKey.bytes,
|
privateKey: remotePeer.privKey.bytes,
|
||||||
publicKey: remotePeer.pubKey.bytes,
|
publicKey: remotePeer.pubKey.bytes,
|
||||||
};
|
};
|
||||||
const handshake = new Handshake('XX', false, localPeer.pubKey.bytes, prologue, staticKeys, wrapped);
|
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);
|
||||||
|
|
||||||
// Finish handshake
|
let receivedMessageBuffer = decodeMessageBuffer((await wrapped.readLP()).slice());
|
||||||
const sessionResponder = await handshake.propose(Buffer.alloc(0));
|
// The first handshake message contains the initiator's ephemeral public key
|
||||||
await handshake.exchange(sessionResponder);
|
expect(receivedMessageBuffer.ne.length).equal(32);
|
||||||
await handshake.finish(sessionResponder);
|
await xx.recvMessage(ns, receivedMessageBuffer);
|
||||||
|
|
||||||
// Create the encrypted streams
|
// Stage 1
|
||||||
console.log(sessionResponder);
|
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 };
|
||||||
})(),
|
})(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
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")));
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user