Fix todos and finish stage 1

This commit is contained in:
morrigan 2019-11-06 15:45:28 +01:00
parent 1666769690
commit 87d7ed9c41
2 changed files with 18 additions and 8 deletions

View File

@ -74,7 +74,7 @@ export class XXHandshake {
}
private dh(privateKey: bytes32, publicKey: bytes32) : bytes32 {
const derived = x25519.derive(privateKey, publicKey);
const derived = x25519.derive(publicKey, privateKey);
const result = Buffer.alloc(32);
derived.copy(result);
return result;
@ -268,14 +268,18 @@ export class XXHandshake {
}
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);
return await this.decryptAndHash(hs.ss, message.ciphertext);
}
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);
if (!hs.e) {
@ -283,16 +287,19 @@ export class XXHandshake {
}
this.mixKey(hs.ss, this.dh(hs.e.privateKey, hs.re));
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;
}
this.mixKey(hs.ss, this.dh(hs.e.privateKey, hs.rs));
return await this.decryptAndHash(hs.ss, message.ciphertext);
}
private async readMessageC(hs: HandshakeState, message: MessageBuffer) {
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;
}
if (!hs.e) {
throw new Error("Handshake state `e` param is missing.");
}

View File

@ -85,11 +85,14 @@ describe("Index", () => {
const payloadRespEnc = NoiseHandshakePayload.encode(payloadResp).finish();
const message1 = Buffer.concat([message, payloadRespEnc]);
console.log("nsResp: ", nsResp)
const messageBuffer2 = await xx.sendMessage(nsResp, message1);
expect(messageBuffer2.ne.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 () => {