Import packages/crypto/src/noise.ts

This commit is contained in:
QQ 2026-11-17 08:42:54 +08:00
parent b988e99b54
commit 80fc63aa39
1 changed files with 13 additions and 1 deletions

View File

@ -17,6 +17,8 @@ export interface NoiseIkSessionOptions {
remotePublicKey: string
prologue: Uint8Array
random?: (length: number) => Uint8Array
/** Optional byte-identical ChaChaPoly accelerator. Nonces remain owned by Noise. */
encryptChaChaPoly?: (key: Uint8Array, nonce: bigint, ad: Uint8Array, plaintext: Uint8Array) => Uint8Array | undefined
}
/**
@ -41,7 +43,17 @@ export class NoiseIkSession {
initiator,
s: { secretKey: localPrivate, publicKey: localPublic },
...(initiator ? { rs: Uint8Array.from(this.expectedRemoteStatic) } : {}),
cipher: chachaPoly,
cipher: options.encryptChaChaPoly ? {
...chachaPoly,
encrypt(key, nonce, ad, plaintext, out) {
const encrypted = options.encryptChaChaPoly!(key, nonce, ad, plaintext)
if (encrypted === undefined) return chachaPoly.encrypt(key, nonce, ad, plaintext, out)
if (encrypted.byteLength !== plaintext.byteLength + chachaPoly.tagLen) {
throw new NoiseSessionError('INVALID_ENCRYPTION_OUTPUT', 'ChaChaPoly accelerator returned an invalid length.')
}
out.set(encrypted)
},
} : chachaPoly,
hash: sha256H,
rng: options.random ?? randomBytes,
})