Skip to content

Commit 41e2551

Browse files
committed
fix!: remove node-forge dependency from @libp2p/crypto
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
1 parent 4691f41 commit 41e2551

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1319
-538
lines changed

packages/crypto-aes-ctr/LICENSE

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This project is dual licensed under MIT and Apache-2.0.
2+
3+
MIT: https://www.opensource.org/licenses/mit
4+
Apache-2.0: https://www.apache.org/licenses/license-2.0
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

packages/crypto-aes-ctr/LICENSE-MIT

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

packages/crypto-aes-ctr/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
2+
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
3+
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)
4+
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=main\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amain)
5+
6+
> Streaming AES-CTR for node and browsers
7+
8+
# About
9+
10+
WebCrypto does not support streaming encryption - <https://github.com/w3c/webcrypto/issues/73>
11+
12+
In browsers this module uses `node-forge` to expose a streaming interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.
13+
14+
In node.js it uses the regular streaming API exported by the `crypto` module.
15+
16+
This uses `CTR` mode.
17+
18+
## Example
19+
20+
```js
21+
import { create } from '@libp2p/crypto-aes-ctr'
22+
23+
// Setting up Key and IV
24+
25+
// A 16 bytes array, 128 Bits, AES-128 is chosen
26+
const key128 = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
27+
28+
// A 16 bytes array, 128 Bits,
29+
const IV = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
30+
31+
const decryptedMessage = 'Hello, world!'
32+
33+
// Encrypting
34+
const cipher = await crypto.aes.create(key128, IV)
35+
const encryptedBuffer = await encrypt(Uint8Array.from(decryptedMessage))
36+
console.log(encryptedBuffer)
37+
// prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
38+
39+
// Decrypting
40+
const decipher = await crypto.aes.create(key128, IV)
41+
const decryptedBuffer = await decrypt(encryptedBuffer)
42+
43+
console.log(decryptedBuffer)
44+
// prints: <Uint8Array 42 f1 67 d9 2e 42 d0 32 9e b1 f8 3c>
45+
46+
console.log(decryptedBuffer.toString('utf-8'))
47+
// prints: Hello, world!
48+
```
49+
50+
# Install
51+
52+
```console
53+
$ npm i @libp2p/crypto-aes-ctr
54+
```
55+
56+
## Browser `<script>` tag
57+
58+
Loading this module through a script tag will make it's exports available as `Libp2pCryptoAesCtr` in the global namespace.
59+
60+
```html
61+
<script src="https://unpkg.com/@libp2p/crypto-aes-ctr/dist/index.min.js"></script>
62+
```
63+
64+
# API Docs
65+
66+
- <https://libp2p.github.io/js-libp2p/modules/_libp2p_crypto_aes_ctr.html>
67+
68+
# License
69+
70+
Licensed under either of
71+
72+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
73+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
74+
75+
# Contribution
76+
77+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

packages/crypto-aes-ctr/package.json

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"name": "@libp2p/crypto-aes-ctr",
3+
"version": "3.0.4",
4+
"description": "Streaming AES-CTR for node and browsers",
5+
"license": "Apache-2.0 OR MIT",
6+
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/crypto-aes-ctr#readme",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/libp2p/js-libp2p.git"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/libp2p/js-libp2p/issues"
13+
},
14+
"publishConfig": {
15+
"access": "public",
16+
"provenance": true
17+
},
18+
"keywords": [
19+
"IPFS",
20+
"aes",
21+
"aes-ctr",
22+
"crypto",
23+
"libp2p"
24+
],
25+
"type": "module",
26+
"types": "./dist/src/index.d.ts",
27+
"files": [
28+
"src",
29+
"dist",
30+
"!dist/test",
31+
"!**/*.tsbuildinfo"
32+
],
33+
"exports": {
34+
".": {
35+
"types": "./src/index.d.ts",
36+
"import": "./dist/src/index.js"
37+
}
38+
},
39+
"eslintConfig": {
40+
"extends": "ipfs",
41+
"parserOptions": {
42+
"project": true,
43+
"sourceType": "module"
44+
},
45+
"ignorePatterns": [
46+
"src/*.d.ts"
47+
]
48+
},
49+
"scripts": {
50+
"clean": "aegir clean",
51+
"lint": "aegir lint",
52+
"dep-check": "aegir dep-check",
53+
"build": "aegir build",
54+
"test": "aegir test",
55+
"test:chrome": "aegir test -t browser",
56+
"test:chrome-webworker": "aegir test -t webworker",
57+
"test:firefox": "aegir test -t browser -- --browser firefox",
58+
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
59+
"test:webkit": "aegir test -t browser -- --browser webkit",
60+
"test:node": "aegir test -t node --cov",
61+
"test:electron-main": "aegir test -t electron-main"
62+
},
63+
"dependencies": {
64+
"@libp2p/interface": "^1.1.1",
65+
"node-forge": "^1.3.1",
66+
"uint8arrays": "^5.0.0"
67+
},
68+
"devDependencies": {
69+
"@types/mocha": "^10.0.0",
70+
"aegir": "^42.0.0"
71+
},
72+
"browser": {
73+
"./dist/src/ciphers.js": "./dist/src/ciphers-browser.js"
74+
}
75+
}
File renamed without changes.

packages/crypto/src/aes/index.ts packages/crypto-aes-ctr/src/index.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
/**
22
* @packageDocumentation
33
*
4-
* Exposes an interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.
4+
* WebCrypto does not support streaming encryption - https://github.com/w3c/webcrypto/issues/73
5+
*
6+
* In browsers this module uses `node-forge` to expose a streaming interface to AES encryption (formerly Rijndael), as defined in U.S. Federal Information Processing Standards Publication 197.
7+
*
8+
* In node.js it uses the regular streaming API exported by the `crypto` module.
59
*
610
* This uses `CTR` mode.
711
*
8-
* /**
912
* @example
1013
*
1114
* ```js
12-
* import { create } from '@libp2p/crypto/aes'
15+
* import { create } from '@libp2p/crypto-aes-ctr'
1316
*
1417
* // Setting up Key and IV
1518
*

packages/crypto/test/aes/aes.spec.ts packages/crypto-aes-ctr/test/index.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
/* eslint-disable valid-jsdoc */
33
/* eslint-env mocha */
44
import { expect } from 'aegir/chai'
5-
import * as crypto from '../../src/index.js'
6-
import fixtures from './../fixtures/aes.js'
7-
import goFixtures from './../fixtures/go-aes.js'
8-
import type { AESCipher } from '../../src/aes/index.js'
5+
import * as aes from '../src/index.js'
6+
import fixtures from './fixtures/aes.js'
7+
import goFixtures from './fixtures/go-aes.js'
8+
import type { AESCipher } from '../src/index.js'
99

1010
const bytes = [{
1111
length: 16,
@@ -24,7 +24,7 @@ describe('AES-CTR', () => {
2424
const iv = new Uint8Array(16)
2525
iv.fill(1)
2626

27-
const cipher = crypto.aes.create(key, iv)
27+
const cipher = aes.create(key, iv)
2828

2929
await encryptAndDecrypt(cipher)
3030
await encryptAndDecrypt(cipher)
@@ -42,7 +42,7 @@ describe('AES-CTR', () => {
4242
const iv = new Uint8Array(16)
4343
iv.fill(1)
4444

45-
const cipher = crypto.aes.create(key, iv)
45+
const cipher = aes.create(key, iv)
4646
// @ts-expect-error cannot index fixtures like this
4747
const fixture = fixtures[length]
4848

@@ -71,7 +71,7 @@ describe('AES-CTR', () => {
7171
const iv = new Uint8Array(16)
7272
iv.fill(1)
7373

74-
const cipher = crypto.aes.create(key, iv)
74+
const cipher = aes.create(key, iv)
7575
// @ts-expect-error cannot index fixtures like this
7676
const fixture = goFixtures[length]
7777

@@ -90,7 +90,7 @@ describe('AES-CTR', () => {
9090
it('checks key length', () => {
9191
const key = new Uint8Array(5)
9292
const iv = new Uint8Array(16)
93-
return expect(() => crypto.aes.create(key, iv)).to.throw().with.property('code', 'ERR_INVALID_KEY_LENGTH')
93+
return expect(() => aes.create(key, iv)).to.throw().with.property('code', 'ERR_INVALID_KEY_LENGTH')
9494
})
9595
})
9696

packages/crypto-aes-ctr/tsconfig.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "aegir/src/config/tsconfig.aegir.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": [
7+
"src",
8+
"test"
9+
],
10+
"references": [
11+
{
12+
"path": "../interface"
13+
}
14+
]
15+
}

packages/crypto-aes-ctr/typedoc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"entryPoints": [
3+
"./src/index.ts"
4+
]
5+
}

packages/crypto-rsa/LICENSE

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This project is dual licensed under MIT and Apache-2.0.
2+
3+
MIT: https://www.opensource.org/licenses/mit
4+
Apache-2.0: https://www.apache.org/licenses/license-2.0

packages/crypto-rsa/LICENSE-APACHE

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

packages/crypto-rsa/LICENSE-MIT

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

packages/crypto-rsa/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
2+
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
3+
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)
4+
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=main\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amain)
5+
6+
> Utility functions for dealing with legacy RSA keys
7+
8+
# About
9+
10+
Provides utility functions for dealing with legacy RSA keys.
11+
12+
# Install
13+
14+
```console
15+
$ npm i @libp2p/crypto-rsa
16+
```
17+
18+
## Browser `<script>` tag
19+
20+
Loading this module through a script tag will make it's exports available as `Libp2pCryptoRsa` in the global namespace.
21+
22+
```html
23+
<script src="https://unpkg.com/@libp2p/crypto-rsa/dist/index.min.js"></script>
24+
```
25+
26+
# API Docs
27+
28+
- <https://libp2p.github.io/js-libp2p/modules/_libp2p_crypto_rsa.html>
29+
30+
# License
31+
32+
Licensed under either of
33+
34+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
35+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
36+
37+
# Contribution
38+
39+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

0 commit comments

Comments
 (0)