Skip to content

Commit

Permalink
Merge pull request #40 from yusefnapora/fix-xx-message-c
Browse files Browse the repository at this point in the history
remove empty ephemeral key from XX message C
  • Loading branch information
morrigan authored Mar 4, 2020
2 parents 049c2e6 + 409bfd2 commit 04b3a80
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
18 changes: 17 additions & 1 deletion src/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export function encode1(message: MessageBuffer): bytes {
return Buffer.concat([message.ne, message.ns, message.ciphertext]);
}

export function encode2(message: MessageBuffer): bytes {
return Buffer.concat([message.ns, message.ciphertext]);
}

export function decode0(input: bytes): MessageBuffer {
if (input.length < 32) {
throw new Error("Cannot decode stage 0 MessageBuffer: length less than 32 bytes.");
Expand All @@ -39,7 +43,7 @@ export function decode0(input: bytes): MessageBuffer {

export function decode1(input: bytes): MessageBuffer {
if (input.length < 80) {
throw new Error("Cannot decode stage 0 MessageBuffer: length less than 96 bytes.");
throw new Error("Cannot decode stage 1 MessageBuffer: length less than 80 bytes.");
}

return {
Expand All @@ -48,3 +52,15 @@ export function decode1(input: bytes): MessageBuffer {
ciphertext: input.slice(80, input.length),
}
}

export function decode2(input: bytes): MessageBuffer {
if (input.length < 48) {
throw new Error("Cannot decode stage 2 MessageBuffer: length less than 48 bytes.");
}

return {
ne: Buffer.alloc(0),
ns: input.slice(0, 48),
ciphertext: input.slice(48, input.length),
}
}
6 changes: 3 additions & 3 deletions src/handshake-xx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
verifySignedPayload,
} from "./utils";
import { logger } from "./logger";
import { decode0, decode1, encode0, encode1 } from "./encoder";
import {decode0, decode1, decode2, encode0, encode1, encode2} from "./encoder";
import { WrappedConnection } from "./noise";
import PeerId from "peer-id";

Expand Down Expand Up @@ -99,11 +99,11 @@ export class XXHandshake implements IHandshake {
if (this.isInitiator) {
logger('Stage 2 - Initiator sending third handshake message.');
const messageBuffer = this.xx.sendMessage(this.session, this.payload);
this.connection.writeLP(encode1(messageBuffer));
this.connection.writeLP(encode2(messageBuffer));
logger('Stage 2 - Initiator sent message with signed payload.');
} else {
logger('Stage 2 - Responder waiting for third handshake message...');
const receivedMessageBuffer = decode1((await this.connection.readLP()).slice());
const receivedMessageBuffer = decode2((await this.connection.readLP()).slice());
const {plaintext, valid} = this.xx.recvMessage(this.session, receivedMessageBuffer);
if(!valid) {
throw new Error("xx handshake stage 2 validation fail");
Expand Down
17 changes: 6 additions & 11 deletions test/noise.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { expect, assert } from "chai";
import {assert, expect} from "chai";
import DuplexPair from 'it-pair/duplex';
import { Noise } from "../src";
import {Noise} from "../src";
import {createPeerIdsFromFixtures} from "./fixtures/peer";
import Wrap from "it-pb-rpc";
import { random } from "bcrypto";
import {random} from "bcrypto";
import sinon from "sinon";
import {XXHandshake} from "../src/handshake-xx";
import {
createHandshakePayload,
generateKeypair,
getHandshakePayload, getPayload,
signPayload
} from "../src/utils";
import {decode0, decode1, encode1, uint16BEDecode, uint16BEEncode} from "../src/encoder";
import {createHandshakePayload, generateKeypair, getHandshakePayload, getPayload, signPayload} from "../src/utils";
import {decode0, decode2, encode1, uint16BEDecode, uint16BEEncode} from "../src/encoder";
import {XX} from "../src/handshakes/xx";
import {Buffer} from "buffer";
import {getKeyPairFromPeerId} from "./utils";
Expand Down Expand Up @@ -89,7 +84,7 @@ describe("Noise", () => {
wrapped.writeLP(encode1(messageBuffer));

// Stage 2 - finish handshake
receivedMessageBuffer = decode1((await wrapped.readLP()).slice());
receivedMessageBuffer = decode2((await wrapped.readLP()).slice());
xx.recvMessage(handshake.session, receivedMessageBuffer);
return {wrapped, handshake};
})(),
Expand Down

0 comments on commit 04b3a80

Please sign in to comment.