oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
72.87k stars 2.64k forks source link

Support ChaCha20-Poly1305 in `node:crypto` #8072

Open cemelo opened 7 months ago

cemelo commented 7 months ago

What is the problem this feature would solve?

I'm trying to build a HomeKit bridge using hap-nodejs. It depends on the support for ChaCha20-Poly1305 to be able to run.

What is the feature you are proposing to solve the problem?

Adding support for that cipher is enough, afaik, to enable hap-nodejs to run on top of Bun.

What alternatives have you considered?

N/A

javalsai commented 5 months ago

I'm using a JS implementation from npm for now, but it's somewhat slow (it's pure JS) and checking the github is old code which I can barely understand. Just in case is looking for a functional alternative for now.

I also tried making a .d.ts file for it, not really a TS expert, but it includes definitions for most of the basic functions (if somebody can make it better, please do):

import { Transform } from "node:stream"

declare class CipherBase extends Transform {
    update(data: Buffer, inputEnc?: BufferEncoding, outputEnv?: BufferEncoding)
    final(): Buffer
}

declare class PolyStream extends CipherBase {
    _update(data: Buffer)
    _final(): Buffer
}

declare class Cipher {
    constructor(key: Buffer, nonce: Buffer, decrypt?: bool)
    setAAD(aad: Buffer): void
    _update(chunk: Buffer): Buffer
    _final(): void
    getAuthTag(): Buffer
    setAuthTag(tag: Buffer): void
}

declare class CipherLegacy {
    constructor(key: Buffer, nonce: Buffer, decrypt?: bool)
    setAAD(aad: Buffer): void
    _flushlentag(): void
    _update(chunk: Buffer): Buffer
    _final(): void
    getAuthTag(): Buffer
    setAuthTag(tag: Buffer): void
}

declare class ChaChaStream extends CipherBase {
    constructor(key: Buffer, iv: Buffer)
}

declare module chacha {
    let aead: Cipher
    let createHmac: typeof PolyStream
    let chacha20: typeof ChaChaStream
    let ChaCha20: typeof ChaChaStream
    let aeadLegacy: typeof CipherLegacy
    let AeadLegacy: typeof CipherLegacy
    function createCipher(key: Buffer, iv: Buffer, decrypt?: bool): Cipher;
    function createDecipher(key: Buffer, iv: Buffer, decrypt?: bool): Cipher;
}

export let Transform
export default chacha