Skip to content

crypto: refactor crypto subsystem and introduce WebCrypto API

Rodrigo Muino Tomonari requested to merge github/fork/jasnell/subtlecrypto into master

@nodejs/tsc @nodejs/crypto ... This is a big one. It is still a work in progress. I'm opening the draft PR so that people can follow along with the work.

What does this do:

Three main things... lots of other little things

  1. It refactors the Node.js src crypto internals so that they are more maintainable and organized. The existing node_crypto.cc has grown into a massive disorganized and unmaintainable mess that very few brave touch. This breaks that functionality up across multiple files in src/crypto that are organized by purpose/algorithm.

  2. It makes a number of important fixes and improvements to the existing crypto internals. For example, previously, CryptoJob had no mechanism for tracking the memory associated with it. CryptoJob has been refactored into an AsyncWrap derived object, allowing and improving memory tracking.

  3. It introduces an experimental Web Crypto API implementation as require('crypto').webcrypto. The initial intent of this API is to be standards compliant.

  4. It introduces HKDF support for the legacy API. HKDF is required by the Web Crypto API implementation, so I decided to go ahead and add a variation for the legacy API also.

5. It introduces crypto.timingSafeEqual.bigint() for performing constant-time comparisons of bigint values. Pulled this due to some outstanding technical concerns that will need to be looked at later.

  1. It introduces the ability to use ArrayBuffer for the existing legacy crypto APIs. Previously, the legacy API was restricted to Buffer, TypedArray, and DataView objects. Because the Web Crypto API makes use of ArrayBuffer also, I decided to go ahead and extend that capability to the legacy API. (Note: this is still a work in progress that will be completed before this PR moves out of draft status)

  2. It introduces one extension to the Web Crypto API exportKey() and importKey() methods to allow converting back and forth between a Node.js KeyObject and a Web Crypto CryptoKey object. The CryptoKey is currently implemented as a wrapper around KeyObject.

My next task on this PR is to begin filling out the tests. This PR will remain in draft status until I have those ready. However, please feel free to begin reviewing the changes made so far.

This is a big PR that will take some time to review. I'm happy to jump on a zoom call with anyone to walk through it.

Outside of changes to error messages and codes (which have been made more consistent), there should be no backwards compatibility breaking changes to the existing legacy API. Any backwards breaking changes should be considered bugs.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

Merge request reports

Loading