mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 14:12:30 +00:00
Fix todos and finish stage 1
This commit is contained in:
parent
1666769690
commit
87d7ed9c41
21
src/xx.ts
21
src/xx.ts
@ -74,7 +74,7 @@ export class XXHandshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private dh(privateKey: bytes32, publicKey: bytes32) : bytes32 {
|
private dh(privateKey: bytes32, publicKey: bytes32) : bytes32 {
|
||||||
const derived = x25519.derive(privateKey, publicKey);
|
const derived = x25519.derive(publicKey, privateKey);
|
||||||
const result = Buffer.alloc(32);
|
const result = Buffer.alloc(32);
|
||||||
derived.copy(result);
|
derived.copy(result);
|
||||||
return result;
|
return result;
|
||||||
@ -268,14 +268,18 @@ export class XXHandshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async readMessageA(hs: HandshakeState, message: MessageBuffer) : Promise<bytes> {
|
private async readMessageA(hs: HandshakeState, message: MessageBuffer) : Promise<bytes> {
|
||||||
// TODO: validate public key here
|
if (x25519.publicKeyVerify(message.ne)) {
|
||||||
|
hs.re = message.ne;
|
||||||
|
}
|
||||||
|
|
||||||
this.mixHash(hs.ss, hs.re);
|
this.mixHash(hs.ss, hs.re);
|
||||||
return await this.decryptAndHash(hs.ss, message.ciphertext);
|
return await this.decryptAndHash(hs.ss, message.ciphertext);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async readMessageB(hs: HandshakeState, message: MessageBuffer) : Promise<bytes> {
|
private async readMessageB(hs: HandshakeState, message: MessageBuffer) : Promise<bytes> {
|
||||||
// TODO: validate public key here
|
if (x25519.publicKeyVerify(message.ne)) {
|
||||||
|
hs.re = message.ne;
|
||||||
|
}
|
||||||
|
|
||||||
this.mixHash(hs.ss, hs.re);
|
this.mixHash(hs.ss, hs.re);
|
||||||
if (!hs.e) {
|
if (!hs.e) {
|
||||||
@ -283,16 +287,19 @@ export class XXHandshake {
|
|||||||
}
|
}
|
||||||
this.mixKey(hs.ss, this.dh(hs.e.privateKey, hs.re));
|
this.mixKey(hs.ss, this.dh(hs.e.privateKey, hs.re));
|
||||||
const ns = await this.decryptAndHash(hs.ss, message.ns);
|
const ns = await this.decryptAndHash(hs.ss, message.ns);
|
||||||
// TODO: validate ns here as public key
|
if (ns.length === 32 && x25519.publicKeyVerify(message.ns)) {
|
||||||
hs.rs = ns;
|
hs.rs = ns;
|
||||||
|
}
|
||||||
this.mixKey(hs.ss, this.dh(hs.e.privateKey, hs.rs));
|
this.mixKey(hs.ss, this.dh(hs.e.privateKey, hs.rs));
|
||||||
return await this.decryptAndHash(hs.ss, message.ciphertext);
|
return await this.decryptAndHash(hs.ss, message.ciphertext);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async readMessageC(hs: HandshakeState, message: MessageBuffer) {
|
private async readMessageC(hs: HandshakeState, message: MessageBuffer) {
|
||||||
const ns = await this.decryptAndHash(hs.ss, message.ns);
|
const ns = await this.decryptAndHash(hs.ss, message.ns);
|
||||||
// TODO: validate ns here as public key
|
if (ns.length === 32 && x25519.publicKeyVerify(message.ns)) {
|
||||||
hs.rs = ns;
|
hs.rs = ns;
|
||||||
|
}
|
||||||
|
|
||||||
if (!hs.e) {
|
if (!hs.e) {
|
||||||
throw new Error("Handshake state `e` param is missing.");
|
throw new Error("Handshake state `e` param is missing.");
|
||||||
}
|
}
|
||||||
|
@ -85,11 +85,14 @@ describe("Index", () => {
|
|||||||
const payloadRespEnc = NoiseHandshakePayload.encode(payloadResp).finish();
|
const payloadRespEnc = NoiseHandshakePayload.encode(payloadResp).finish();
|
||||||
|
|
||||||
const message1 = Buffer.concat([message, payloadRespEnc]);
|
const message1 = Buffer.concat([message, payloadRespEnc]);
|
||||||
console.log("nsResp: ", nsResp)
|
|
||||||
const messageBuffer2 = await xx.sendMessage(nsResp, message1);
|
const messageBuffer2 = await xx.sendMessage(nsResp, message1);
|
||||||
|
|
||||||
expect(messageBuffer2.ne.length).not.equal(0);
|
expect(messageBuffer2.ne.length).not.equal(0);
|
||||||
expect(messageBuffer2.ns.length).not.equal(0);
|
expect(messageBuffer2.ns.length).not.equal(0);
|
||||||
|
|
||||||
|
// initiator receive payload
|
||||||
|
const plaintext2 = await xx.RecvMessage(nsInit, messageBuffer2);
|
||||||
|
console.log(plaintext2);
|
||||||
}
|
}
|
||||||
|
|
||||||
it("Test handshake", async () => {
|
it("Test handshake", async () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user