Skip to content

Commit

Permalink
fix: Allow the caller to set the prologue (#181) (#182)
Browse files Browse the repository at this point in the history
* fix: Allow the caller to set the prologue (#181)

* The easier PR comments.

* Add a test.

* refix formatting
  • Loading branch information
John-LittleBearLabs authored Aug 10, 2022
1 parent a43cba9 commit 15f7a6e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/noise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ 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

/**
* @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)
this.crypto = crypto

Expand All @@ -45,6 +45,7 @@ export class Noise implements INoiseConnection {
} else {
this.staticKeys = this.crypto.generateX25519KeyPair()
}
this.prologue = prologueBytes ?? new Uint8Array(0)
}

/**
Expand Down
26 changes: 24 additions & 2 deletions test/noise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,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) {
Expand Down Expand Up @@ -183,4 +183,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<Uint8Array>()
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)
}
})
})

0 comments on commit 15f7a6e

Please sign in to comment.