From b9568fd04764a80a3ff7bc0098f45c6cc8342b29 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Fri, 14 Oct 2022 10:01:21 -0500 Subject: [PATCH 1/2] Add prefix in encryptStream --- src/crypto/streaming.ts | 3 +++ src/encoder.ts | 4 ++-- src/noise.ts | 5 ++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/crypto/streaming.ts b/src/crypto/streaming.ts index 42a9293..212052f 100644 --- a/src/crypto/streaming.ts +++ b/src/crypto/streaming.ts @@ -2,6 +2,7 @@ import type { Transform } from 'it-stream-types' import type { Uint8ArrayList } from 'uint8arraylist' import type { IHandshake } from '../@types/handshake-interface.js' import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from '../constants.js' +import { uint16BEEncode } from '../encoder.js' // Returns generator that encrypts payload from the user export function encryptStream (handshake: IHandshake): Transform { @@ -14,6 +15,8 @@ export function encryptStream (handshake: IHandshake): Transform { } const data = handshake.encrypt(chunk.subarray(i, end), handshake.session) + + yield uint16BEEncode(data.byteLength) yield data } } diff --git a/src/encoder.ts b/src/encoder.ts index 1345d2e..30c87d7 100644 --- a/src/encoder.ts +++ b/src/encoder.ts @@ -2,7 +2,7 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat' import type { Uint8ArrayList } from 'uint8arraylist' import type { bytes } from './@types/basic.js' import type { MessageBuffer } from './@types/handshake.js' -import type { LengthDecoderFunction, LengthEncoderFunction } from 'it-length-prefixed' +import type { LengthDecoderFunction } from 'it-length-prefixed' const allocUnsafe = (len: number): Uint8Array => { if (globalThis.Buffer) { @@ -12,7 +12,7 @@ const allocUnsafe = (len: number): Uint8Array => { return new Uint8Array(len) } -export const uint16BEEncode: LengthEncoderFunction = (value: number) => { +export const uint16BEEncode = (value: number): Uint8Array => { const target = allocUnsafe(2) new DataView(target.buffer, target.byteOffset, target.byteLength).setUint16(0, value, false) return target diff --git a/src/noise.ts b/src/noise.ts index e22bb39..b5466c6 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -3,7 +3,7 @@ import type { SecuredConnection } from '@libp2p/interface-connection-encrypter' import { pbStream, ProtobufStream } from 'it-pb-stream' import { duplexPair } from 'it-pair/duplex' import { pipe } from 'it-pipe' -import { encode, decode } from 'it-length-prefixed' +import { decode } from 'it-length-prefixed' import type { Duplex } from 'it-stream-types' import type { bytes } from './@types/basic.js' import type { IHandshake } from './@types/handshake-interface.js' @@ -165,8 +165,7 @@ export class Noise implements INoiseConnection { await pipe( secure, // write to wrapper - encryptStream(handshake), // data is encrypted - encode({ lengthEncoder: uint16BEEncode }), // prefix with message length + encryptStream(handshake), // encrypt data + prefix with message length network, // send to the remote peer decode({ lengthDecoder: uint16BEDecode }), // read message length prefix decryptStream(handshake), // decrypt the incoming data From cf366473e970b01c4f94e22f572b2d9eaceab51a Mon Sep 17 00:00:00 2001 From: Cayman Date: Sun, 30 Oct 2022 21:56:12 -0500 Subject: [PATCH 2/2] chore: fix lint error --- src/crypto/streaming.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/streaming.ts b/src/crypto/streaming.ts index 212052f..b967b77 100644 --- a/src/crypto/streaming.ts +++ b/src/crypto/streaming.ts @@ -15,7 +15,7 @@ export function encryptStream (handshake: IHandshake): Transform { } const data = handshake.encrypt(chunk.subarray(i, end), handshake.session) - + yield uint16BEEncode(data.byteLength) yield data }