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

fix!: remove node-forge dependency from @libp2p/crypto #2355

Merged
merged 7 commits into from
Jan 12, 2024

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Jan 11, 2024

TLDR: the bundle size has been reduced by about 1/3rd

  • parsing/creating PEM/pkix/pkcs1 files is now done by asn1.js
  • Streaming AES-CTR ciphers are now in @libp2p/aes-ctr
  • RSA encryption/decryption and PEM import/export are now in @libp2p/rsa

AES-CTR

WebCrypto doesn't support streaming ciphers.

We have a node-forge-backed shim that allows using streaming AES-CTR in browsers but we don't use it anywhere, so this has been split out into it's own module as @libp2p/aes-ctr.

RSA encrypt/decrypt

This was added to @libp2p/crypto to support webrtc-stardust but that effort didn't go anywhere and we don't use these methods anywhere else in the stack.

For reasons lost to the mists of time, we chose to use a padding algorithm that WebCrypto doesn't support so node-forge (or some other userland implemenation) will always be necessary in browsers, so these ops have been pulled out into @libp2p/rsa which people can use if they need it.

This is now done by manipulating the asn1 structures directly.

PEM/pkix/pkcs1

The previous PEM import/export is also ported to @libp2p/crypto-rsa because it seems to handle more weird edge cases introduced by OpenSSL.

These could be handled in @libp2p/crypto eventually but for now it at least supports round-tripping it's own PEM files.

Fixes #2086

BREAKING CHANGE: Legacy RSA operations are now in @libp2p/rsa, streaming AES-CTR ciphers are in @libp2p/aes-ctr

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

@achingbrain achingbrain requested a review from a team as a code owner January 11, 2024 15:00
TLDR: the bundle size has been reduced by ~50KB

- parsing/creating PEM/pkix/pkcs1 files is now done by asn1.js
- Streaming AES-CTR ciphers are now in @libp2p/crypto-aes-ctr
- RSA encryption/decryption and PEM import/export are now in @libp2p/crypto-rsa

WebCrypto [doesn't support streaming ciphers](w3c/webcrypto#73).

We have a node-forge-backed shim that allows using streaming AES-CTR in browsers but we don't use it anywhere, so this has been split out into it's own module as `@libp2p/aes-ctr`.

This was added to `@libp2p/crypto` to [support webrtc-stardust](libp2p/js-libp2p-crypto#125 (comment)) but that effort didn't go anywhere and we don't use these methods anywhere else in the stack.

For reasons lost to the mists of time, we chose to require a padding algorithm that WebCrypto doesn't support so node-forge (or some other userland implemenation) will always be necessary in browsers, so these ops have been pull out into @libp2p/crypto-rsa which people can use if they need it.

This is now done by manipulating the asn1 structures directly.

The previous PEM import/export is also ported to `@libp2p/crypto-rsa` because it seems to handle more weird edge cases introduced by OpenSSL.

These could be handled in `@libp2p/crypto` eventually but for now it at least supports round-tripping it's own PEM files.

BREAKING CHANGE: Legacy RSA operations are now in @libp2p/crypto-rsa, streaming AES-CTR ciphers are in @libp2p/crypto-aes-ctr
@achingbrain achingbrain force-pushed the fix/remove-node-forge-from-crypto branch from c2f5274 to 41e2551 Compare January 11, 2024 15:01
Copy link
Member

@wemeetagain wemeetagain left a comment

Choose a reason for hiding this comment

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

lgtm

@achingbrain achingbrain merged commit 856ccd7 into main Jan 12, 2024
27 checks passed
@achingbrain achingbrain deleted the fix/remove-node-forge-from-crypto branch January 12, 2024 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: reduce scope of crypto module
2 participants