From b1dba4e658272bd6feae55f56519cf3646c4f124 Mon Sep 17 00:00:00 2001 From: John Turpish Date: Fri, 5 Aug 2022 01:26:58 -0400 Subject: [PATCH 1/4] fix: Allow the caller to set the prologue (#181) --- package.json | 3 ++- src/noise.ts | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a845d559..2ed8847d 100644 --- a/package.json +++ b/package.json @@ -93,7 +93,8 @@ "benchmark": "^2.1.4", "mkdirp": "^1.0.4", "protons": "^4.0.0", - "sinon": "^14.0.0" + "sinon": "^14.0.0", + "typescript": "^4.7.4" }, "browser": { "./dist/src/alloc-unsafe.js": "./dist/src/alloc-unsafe-browser.js", diff --git a/src/noise.ts b/src/noise.ts index 820d98e0..5121e347 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -32,7 +32,7 @@ export class Noise implements INoiseConnection { public protocol = '/noise' public crypto: ICryptoInterface - private readonly prologue = new Uint8Array(0) + private readonly prologue: Uint8Array; private readonly staticKeys: KeyPair private readonly earlyData?: bytes private readonly useNoisePipes: boolean @@ -41,7 +41,7 @@ export class Noise implements INoiseConnection { * @param {bytes} staticNoiseKey - x25519 private key, reuse for faster handshakes * @param {bytes} earlyData */ - constructor (staticNoiseKey?: bytes, earlyData?: bytes, crypto: ICryptoInterface = stablelib) { + constructor (staticNoiseKey?: bytes, earlyData?: bytes, crypto: ICryptoInterface = stablelib, prologueBytes?: Uint8Array) { this.earlyData = earlyData ?? new Uint8Array(0) // disabled until properly specked this.useNoisePipes = false @@ -53,6 +53,11 @@ export class Noise implements INoiseConnection { } else { this.staticKeys = this.crypto.generateX25519KeyPair() } + if (prologueBytes) { + this.prologue = prologueBytes; + } else { + this.prologue = new Uint8Array(0); + } } /** From d1655d5e630211bffcf97e5b23a100c9aeee9d88 Mon Sep 17 00:00:00 2001 From: John Turpish Date: Fri, 5 Aug 2022 12:17:23 -0400 Subject: [PATCH 2/4] The easier PR comments. --- package.json | 3 +-- src/noise.ts | 8 ++------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 2ed8847d..a845d559 100644 --- a/package.json +++ b/package.json @@ -93,8 +93,7 @@ "benchmark": "^2.1.4", "mkdirp": "^1.0.4", "protons": "^4.0.0", - "sinon": "^14.0.0", - "typescript": "^4.7.4" + "sinon": "^14.0.0" }, "browser": { "./dist/src/alloc-unsafe.js": "./dist/src/alloc-unsafe-browser.js", diff --git a/src/noise.ts b/src/noise.ts index 5121e347..28be732a 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -32,7 +32,7 @@ export class Noise implements INoiseConnection { public protocol = '/noise' public crypto: ICryptoInterface - private readonly prologue: Uint8Array; + private readonly prologue: Uint8Array private readonly staticKeys: KeyPair private readonly earlyData?: bytes private readonly useNoisePipes: boolean @@ -53,11 +53,7 @@ export class Noise implements INoiseConnection { } else { this.staticKeys = this.crypto.generateX25519KeyPair() } - if (prologueBytes) { - this.prologue = prologueBytes; - } else { - this.prologue = new Uint8Array(0); - } + this.prologue = prologueBytes ?? new Uint8Array(0); } /** From 878ae04021ab1be9dcfa86fdf7aad54ab5e6d934 Mon Sep 17 00:00:00 2001 From: John Turpish Date: Mon, 8 Aug 2022 14:05:02 -0400 Subject: [PATCH 3/4] Add a test. --- test/noise.spec.ts | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/test/noise.spec.ts b/test/noise.spec.ts index 1d7fde3a..43fa36de 100644 --- a/test/noise.spec.ts +++ b/test/noise.spec.ts @@ -120,9 +120,9 @@ describe('Noise', () => { const wrappedInbound = pbStream(inbound.conn) const wrappedOutbound = pbStream(outbound.conn) - const largePlaintext = randomBytes(100000) + const largePlaintext = randomBytes(60000) wrappedOutbound.writeLP(Buffer.from(largePlaintext)) - const response = await wrappedInbound.read(100000) + const response = await wrappedInbound.read(60000) expect(response.length).equals(largePlaintext.length) } catch (e) { @@ -374,4 +374,26 @@ describe('Noise', () => { assert(false, err.message) } }) + it('should accept a prologue', async () => { + try { + const noiseInit = new Noise(undefined, undefined, stablelib, Buffer.from('Some prologue')) + const noiseResp = new Noise(undefined, undefined, stablelib, Buffer.from('Some prologue')) + + const [inboundConnection, outboundConnection] = duplexPair() + const [outbound, inbound] = await Promise.all([ + noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer), + noiseResp.secureInbound(remotePeer, inboundConnection, localPeer) + ]) + const wrappedInbound = pbStream(inbound.conn) + const wrappedOutbound = pbStream(outbound.conn) + + wrappedOutbound.writeLP(Buffer.from('test')) + const response = await wrappedInbound.readLP() + expect(uint8ArrayToString(response.slice())).equal('test') + } catch (e) { + const err = e as Error + assert(false, err.message) + } + }) + }) From 525e9a0e9d12aaf781446ec21d8bafe42d5b7231 Mon Sep 17 00:00:00 2001 From: John Turpish Date: Mon, 8 Aug 2022 15:29:26 -0400 Subject: [PATCH 4/4] refix formatting --- src/noise.ts | 2 +- test/noise.spec.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/noise.ts b/src/noise.ts index 28be732a..62a5bd9b 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -53,7 +53,7 @@ export class Noise implements INoiseConnection { } else { this.staticKeys = this.crypto.generateX25519KeyPair() } - this.prologue = prologueBytes ?? new Uint8Array(0); + this.prologue = prologueBytes ?? new Uint8Array(0) } /** diff --git a/test/noise.spec.ts b/test/noise.spec.ts index 43fa36de..93b82fb0 100644 --- a/test/noise.spec.ts +++ b/test/noise.spec.ts @@ -374,6 +374,7 @@ describe('Noise', () => { assert(false, err.message) } }) + it('should accept a prologue', async () => { try { const noiseInit = new Noise(undefined, undefined, stablelib, Buffer.from('Some prologue')) @@ -395,5 +396,4 @@ describe('Noise', () => { assert(false, err.message) } }) - })