oven-sh / bun

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

Rewrite `node:crypto` #13681

Open Jarred-Sumner opened 1 week ago

Jarred-Sumner commented 1 week ago

There are several open issues about node:crypto

Our current implementation of node:crypto is an incrementally modified fork of the browserify crypto polyfill with a lot of BoringSSL bindings added in key places.

The browserify crypto polyfill was a great starting point, but we've outgrown it.

When you skim through the code, you'll note that very little of it makes sense in the context of Bun

https://github.com/oven-sh/bun/blob/1bec6c3810ad49272e0f7ad385c1dd3efbfd7d19/src/js/node/crypto.ts#L2238-L2258

It has a JavaScript implementation of MD5, SHA1, crypto.randomBytes, DES, AES, diffieHelman, etc. We have BoringSSL. We even expose BoringSSL's MD5, SHA1, etc. Let's use it.

Let's also get rid of the commonJS wrappers in it. The only JavaScript code in node:crypto should be related to streams, or wrapping a native implementation in a stream. Everything else should be in native code.

The other issue here is in the particular stream implementations themselves. There are likely many small subtly incompatible bugs in the various classes exposed by node:crypto. We need to be running node's tests against our crypto implementation. Ideally, we'd be running other test suites too for this.

wpaulino commented 1 week ago

I plan to start with the switch to BoringSSL for publicEncrypt/privateDecrypt.