Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add prefix in encryptStream #236

Merged
merged 2 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/crypto/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Uint8Array> {
Expand All @@ -14,6 +15,8 @@ export function encryptStream (handshake: IHandshake): Transform<Uint8Array> {
}

const data = handshake.encrypt(chunk.subarray(i, end), handshake.session)

yield uint16BEEncode(data.byteLength)
yield data
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the type changed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because LengthEncoderFunction is typed are returning either Uint8Array or Uint8Array list, requiring extra code downstream to handle both cases.

const target = allocUnsafe(2)
new DataView(target.buffer, target.byteOffset, target.byteLength).setUint16(0, value, false)
return target
Expand Down
5 changes: 2 additions & 3 deletions src/noise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down