@libp2p/crypto 3.0.4-ee7ffe9b9 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/dist/index.min.js +72 -14
  2. package/dist/src/index.d.ts +0 -2
  3. package/dist/src/index.d.ts.map +1 -1
  4. package/dist/src/index.js +0 -2
  5. package/dist/src/index.js.map +1 -1
  6. package/dist/src/keys/ed25519-browser.d.ts +1 -1
  7. package/dist/src/keys/ed25519-browser.d.ts.map +1 -1
  8. package/dist/src/keys/index.d.ts +4 -2
  9. package/dist/src/keys/index.d.ts.map +1 -1
  10. package/dist/src/keys/index.js +7 -12
  11. package/dist/src/keys/index.js.map +1 -1
  12. package/dist/src/keys/rsa-browser.d.ts +0 -2
  13. package/dist/src/keys/rsa-browser.d.ts.map +1 -1
  14. package/dist/src/keys/rsa-browser.js +0 -25
  15. package/dist/src/keys/rsa-browser.js.map +1 -1
  16. package/dist/src/keys/rsa-class.d.ts +6 -5
  17. package/dist/src/keys/rsa-class.d.ts.map +1 -1
  18. package/dist/src/keys/rsa-class.js +11 -25
  19. package/dist/src/keys/rsa-class.js.map +1 -1
  20. package/dist/src/keys/rsa-utils.d.ts +15 -2
  21. package/dist/src/keys/rsa-utils.d.ts.map +1 -1
  22. package/dist/src/keys/rsa-utils.js +304 -39
  23. package/dist/src/keys/rsa-utils.js.map +1 -1
  24. package/dist/src/keys/rsa.d.ts +0 -2
  25. package/dist/src/keys/rsa.d.ts.map +1 -1
  26. package/dist/src/keys/rsa.js +2 -22
  27. package/dist/src/keys/rsa.js.map +1 -1
  28. package/dist/src/pbkdf2.d.ts +1 -1
  29. package/dist/src/pbkdf2.d.ts.map +1 -1
  30. package/dist/src/pbkdf2.js +14 -10
  31. package/dist/src/pbkdf2.js.map +1 -1
  32. package/dist/src/util.d.ts +0 -7
  33. package/dist/src/util.d.ts.map +1 -1
  34. package/dist/src/util.js +0 -25
  35. package/dist/src/util.js.map +1 -1
  36. package/dist/src/webcrypto-browser.d.ts +5 -0
  37. package/dist/src/webcrypto-browser.d.ts.map +1 -0
  38. package/dist/src/webcrypto-browser.js +17 -0
  39. package/dist/src/webcrypto-browser.js.map +1 -0
  40. package/dist/src/webcrypto.d.ts +3 -1
  41. package/dist/src/webcrypto.d.ts.map +1 -1
  42. package/dist/src/webcrypto.js +4 -11
  43. package/dist/src/webcrypto.js.map +1 -1
  44. package/dist/typedoc-urls.json +42 -0
  45. package/package.json +8 -15
  46. package/src/index.ts +0 -2
  47. package/src/keys/ed25519-browser.ts +1 -1
  48. package/src/keys/index.ts +10 -12
  49. package/src/keys/rsa-browser.ts +0 -29
  50. package/src/keys/rsa-class.ts +11 -28
  51. package/src/keys/rsa-utils.ts +373 -39
  52. package/src/keys/rsa.ts +2 -23
  53. package/src/pbkdf2.ts +17 -15
  54. package/src/util.ts +0 -29
  55. package/src/webcrypto-browser.ts +24 -0
  56. package/src/webcrypto.ts +5 -18
  57. package/dist/src/aes/cipher-mode.d.ts +0 -2
  58. package/dist/src/aes/cipher-mode.d.ts.map +0 -1
  59. package/dist/src/aes/cipher-mode.js +0 -13
  60. package/dist/src/aes/cipher-mode.js.map +0 -1
  61. package/dist/src/aes/ciphers-browser.d.ts +0 -7
  62. package/dist/src/aes/ciphers-browser.d.ts.map +0 -1
  63. package/dist/src/aes/ciphers-browser.js +0 -26
  64. package/dist/src/aes/ciphers-browser.js.map +0 -1
  65. package/dist/src/aes/ciphers.d.ts +0 -5
  66. package/dist/src/aes/ciphers.d.ts.map +0 -1
  67. package/dist/src/aes/ciphers.js +0 -4
  68. package/dist/src/aes/ciphers.js.map +0 -1
  69. package/dist/src/aes/index.d.ts +0 -50
  70. package/dist/src/aes/index.d.ts.map +0 -1
  71. package/dist/src/aes/index.js +0 -61
  72. package/dist/src/aes/index.js.map +0 -1
  73. package/dist/src/keys/jwk2pem.d.ts +0 -8
  74. package/dist/src/keys/jwk2pem.d.ts.map +0 -1
  75. package/dist/src/keys/jwk2pem.js +0 -14
  76. package/dist/src/keys/jwk2pem.js.map +0 -1
  77. package/src/aes/cipher-mode.ts +0 -15
  78. package/src/aes/ciphers-browser.ts +0 -31
  79. package/src/aes/ciphers.ts +0 -4
  80. package/src/aes/index.ts +0 -70
  81. package/src/keys/jwk2pem.ts +0 -21
@@ -1,5 +1,7 @@
1
+ /// <reference types="node" />
2
+ import { webcrypto } from 'crypto';
1
3
  declare const _default: {
2
- get(win?: typeof globalThis): Crypto;
4
+ get(win?: typeof globalThis): webcrypto.Crypto;
3
5
  };
4
6
  export default _default;
5
7
  //# sourceMappingURL=webcrypto.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"webcrypto.d.ts","sourceRoot":"","sources":["../../src/webcrypto.ts"],"names":[],"mappings":";;;AAIA,wBAmBC"}
1
+ {"version":3,"file":"webcrypto.d.ts","sourceRoot":"","sources":["../../src/webcrypto.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;;;;AAIlC,wBAIC"}
@@ -1,17 +1,10 @@
1
1
  /* eslint-env browser */
2
- // Check native crypto exists and is enabled (In insecure context `self.crypto`
3
- // exists but `self.crypto.subtle` does not).
2
+ import { webcrypto } from 'crypto';
3
+ // globalThis `SubtleCrypto` shipped in node.js 19.x, Electron currently uses
4
+ // v18.x so this override file is necessary until Electron updates
4
5
  export default {
5
6
  get(win = globalThis) {
6
- const nativeCrypto = win.crypto;
7
- if (nativeCrypto == null || nativeCrypto.subtle == null) {
8
- throw Object.assign(new Error('Missing Web Crypto API. ' +
9
- 'The most likely cause of this error is that this page is being accessed ' +
10
- 'from an insecure context (i.e. not HTTPS). For more information and ' +
11
- 'possible resolutions see ' +
12
- 'https://github.com/libp2p/js-libp2p/blob/main/packages/crypto/README.md#web-crypto-api'), { code: 'ERR_MISSING_WEB_CRYPTO' });
13
- }
14
- return nativeCrypto;
7
+ return webcrypto;
15
8
  }
16
9
  };
17
10
  //# sourceMappingURL=webcrypto.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"webcrypto.js","sourceRoot":"","sources":["../../src/webcrypto.ts"],"names":[],"mappings":"AAAA,wBAAwB;AAExB,+EAA+E;AAC/E,6CAA6C;AAC7C,eAAe;IACb,GAAG,CAAE,GAAG,GAAG,UAAU;QACnB,MAAM,YAAY,GAAG,GAAG,CAAC,MAAM,CAAA;QAE/B,IAAI,YAAY,IAAI,IAAI,IAAI,YAAY,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC;YACxD,MAAM,MAAM,CAAC,MAAM,CACjB,IAAI,KAAK,CACP,0BAA0B;gBAC1B,0EAA0E;gBAC1E,sEAAsE;gBACtE,2BAA2B;gBAC3B,wFAAwF,CACzF,EACD,EAAE,IAAI,EAAE,wBAAwB,EAAE,CACnC,CAAA;QACH,CAAC;QAED,OAAO,YAAY,CAAA;IACrB,CAAC;CACF,CAAA"}
1
+ {"version":3,"file":"webcrypto.js","sourceRoot":"","sources":["../../src/webcrypto.ts"],"names":[],"mappings":"AAAA,wBAAwB;AAExB,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAElC,6EAA6E;AAC7E,kEAAkE;AAClE,eAAe;IACb,GAAG,CAAE,GAAG,GAAG,UAAU;QACnB,OAAO,SAAS,CAAA;IAClB,CAAC;CACF,CAAA"}
@@ -0,0 +1,42 @@
1
+ {
2
+ "HMAC": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_crypto.hmac.HMAC.html",
3
+ "./hmac:HMAC": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_crypto.hmac.HMAC.html",
4
+ "create": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.hmac.create.html",
5
+ "./hmac:create": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.hmac.create.html",
6
+ "pbkdf2": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.index.pbkdf2.html",
7
+ "randomBytes": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.index.randomBytes.html",
8
+ "codec": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.keysPBM.KeyType.codec.html",
9
+ "decode": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.keysPBM.PrivateKey.decode.html",
10
+ "encode": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.keysPBM.PrivateKey.encode.html",
11
+ "KeyType": "https://libp2p.github.io/js-libp2p/enums/_libp2p_crypto.keys.keysPBM.KeyType-1.html",
12
+ "PrivateKey": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_crypto.keys.keysPBM.PrivateKey-1.html",
13
+ "PublicKey": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_crypto.keys.keysPBM.PublicKey-1.html",
14
+ "Ed25519PrivateKey": "https://libp2p.github.io/js-libp2p/classes/_libp2p_crypto.keys.Ed25519PrivateKey.html",
15
+ "Ed25519PublicKey": "https://libp2p.github.io/js-libp2p/classes/_libp2p_crypto.keys.Ed25519PublicKey.html",
16
+ "RsaPrivateKey": "https://libp2p.github.io/js-libp2p/classes/_libp2p_crypto.keys.RsaPrivateKey.html",
17
+ "RsaPublicKey": "https://libp2p.github.io/js-libp2p/classes/_libp2p_crypto.keys.RsaPublicKey.html",
18
+ "Secp256k1PrivateKey": "https://libp2p.github.io/js-libp2p/classes/_libp2p_crypto.keys.Secp256k1PrivateKey.html",
19
+ "Secp256k1PublicKey": "https://libp2p.github.io/js-libp2p/classes/_libp2p_crypto.keys.Secp256k1PublicKey.html",
20
+ "JWKKeyPair": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_crypto.keys.JWKKeyPair.html",
21
+ "KeyTypes": "https://libp2p.github.io/js-libp2p/types/_libp2p_crypto.keys.KeyTypes.html",
22
+ "./keys:KeyTypes": "https://libp2p.github.io/js-libp2p/types/_libp2p_crypto.keys.KeyTypes.html",
23
+ "MAX_RSA_KEY_SIZE": "https://libp2p.github.io/js-libp2p/variables/_libp2p_crypto.keys.MAX_RSA_KEY_SIZE.html",
24
+ "supportedKeys": "https://libp2p.github.io/js-libp2p/variables/_libp2p_crypto.keys.supportedKeys.html",
25
+ "./keys:supportedKeys": "https://libp2p.github.io/js-libp2p/variables/_libp2p_crypto.keys.supportedKeys.html",
26
+ "generateEphemeralKeyPair": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.generateEphemeralKeyPair.html",
27
+ "generateKeyPair": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.generateKeyPair.html",
28
+ "./keys:generateKeyPair": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.generateKeyPair.html",
29
+ "generateKeyPairFromSeed": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.generateKeyPairFromSeed.html",
30
+ "./keys:generateKeyPairFromSeed": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.generateKeyPairFromSeed.html",
31
+ "importKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.importKey.html",
32
+ "./keys:importKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.importKey.html",
33
+ "keyStretcher": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.keyStretcher.html",
34
+ "marshalPrivateKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.marshalPrivateKey.html",
35
+ "./keys:marshalPrivateKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.marshalPrivateKey.html",
36
+ "marshalPublicKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.marshalPublicKey.html",
37
+ "./keys:marshalPublicKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.marshalPublicKey.html",
38
+ "unmarshalPrivateKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.unmarshalPrivateKey.html",
39
+ "./keys:unmarshalPrivateKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.unmarshalPrivateKey.html",
40
+ "unmarshalPublicKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.unmarshalPublicKey.html",
41
+ "./keys:unmarshalPublicKey": "https://libp2p.github.io/js-libp2p/functions/_libp2p_crypto.keys.unmarshalPublicKey.html"
42
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/crypto",
3
- "version": "3.0.4-ee7ffe9b9",
3
+ "version": "4.0.0",
4
4
  "description": "Crypto primitives for libp2p",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/crypto#readme",
@@ -51,10 +51,6 @@
51
51
  "types": "./src/index.d.ts",
52
52
  "import": "./dist/src/index.js"
53
53
  },
54
- "./aes": {
55
- "types": "./dist/src/aes/index.d.ts",
56
- "import": "./dist/src/aes/index.js"
57
- },
58
54
  "./hmac": {
59
55
  "types": "./dist/src/hmac/index.d.ts",
60
56
  "import": "./dist/src/hmac/index.js"
@@ -69,10 +65,7 @@
69
65
  "parserOptions": {
70
66
  "project": true,
71
67
  "sourceType": "module"
72
- },
73
- "ignorePatterns": [
74
- "src/*.d.ts"
75
- ]
68
+ }
76
69
  },
77
70
  "scripts": {
78
71
  "clean": "aegir clean",
@@ -90,13 +83,13 @@
90
83
  "generate": "protons ./src/keys/keys.proto"
91
84
  },
92
85
  "dependencies": {
93
- "@libp2p/interface": "1.1.1-ee7ffe9b9",
86
+ "@libp2p/interface": "^1.1.1",
94
87
  "@noble/curves": "^1.1.0",
95
- "@noble/hashes": "^1.3.1",
88
+ "@noble/hashes": "^1.3.3",
89
+ "asn1js": "^3.0.5",
96
90
  "multiformats": "^13.0.0",
97
- "node-forge": "^1.1.0",
98
91
  "protons-runtime": "^5.0.0",
99
- "uint8arraylist": "^2.4.3",
92
+ "uint8arraylist": "^2.4.7",
100
93
  "uint8arrays": "^5.0.0"
101
94
  },
102
95
  "devDependencies": {
@@ -106,12 +99,12 @@
106
99
  "protons": "^7.3.0"
107
100
  },
108
101
  "browser": {
109
- "./dist/src/aes/ciphers.js": "./dist/src/aes/ciphers-browser.js",
110
102
  "./dist/src/ciphers/aes-gcm.js": "./dist/src/ciphers/aes-gcm.browser.js",
111
103
  "./dist/src/hmac/index.js": "./dist/src/hmac/index-browser.js",
112
104
  "./dist/src/keys/ecdh.js": "./dist/src/keys/ecdh-browser.js",
113
105
  "./dist/src/keys/ed25519.js": "./dist/src/keys/ed25519-browser.js",
114
106
  "./dist/src/keys/rsa.js": "./dist/src/keys/rsa-browser.js",
115
- "./dist/src/keys/secp256k1.js": "./dist/src/keys/secp256k1-browser.js"
107
+ "./dist/src/keys/secp256k1.js": "./dist/src/keys/secp256k1-browser.js",
108
+ "./dist/src/webcrypto.js": "./dist/src/webcrypto-browser.js"
116
109
  }
117
110
  }
package/src/index.ts CHANGED
@@ -8,13 +8,11 @@
8
8
  * To enable the Web Crypto API and allow `@libp2p/crypto` to work fully, please serve your page over HTTPS.
9
9
  */
10
10
 
11
- import * as aes from './aes/index.js'
12
11
  import * as hmac from './hmac/index.js'
13
12
  import * as keys from './keys/index.js'
14
13
  import pbkdf2 from './pbkdf2.js'
15
14
  import randomBytes from './random-bytes.js'
16
15
 
17
- export { aes }
18
16
  export { hmac }
19
17
  export { keys }
20
18
  export { randomBytes }
@@ -1,5 +1,5 @@
1
1
  import { ed25519 as ed } from '@noble/curves/ed25519'
2
- import type { Uint8ArrayKeyPair } from './interface'
2
+ import type { Uint8ArrayKeyPair } from './interface.js'
3
3
  import type { Uint8ArrayList } from 'uint8arraylist'
4
4
 
5
5
  const PUBLIC_KEY_BYTE_LENGTH = 32
package/src/keys/index.ts CHANGED
@@ -10,18 +10,14 @@
10
10
  * For encryption / decryption support, RSA keys should be used.
11
11
  */
12
12
 
13
- import 'node-forge/lib/asn1.js'
14
- import 'node-forge/lib/pbe.js'
15
13
  import { CodeError } from '@libp2p/interface'
16
- // @ts-expect-error types are missing
17
- import forge from 'node-forge/lib/forge.js'
18
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
19
14
  import * as Ed25519 from './ed25519-class.js'
20
15
  import generateEphemeralKeyPair from './ephemeral-keys.js'
21
16
  import { importer } from './importer.js'
22
17
  import { keyStretcher } from './key-stretcher.js'
23
18
  import * as keysPBM from './keys.js'
24
19
  import * as RSA from './rsa-class.js'
20
+ import { importFromPem } from './rsa-utils.js'
25
21
  import * as Secp256k1 from './secp256k1-class.js'
26
22
  import type { PrivateKey, PublicKey } from '@libp2p/interface'
27
23
 
@@ -31,6 +27,11 @@ export { keysPBM }
31
27
 
32
28
  export type KeyTypes = 'RSA' | 'Ed25519' | 'secp256k1'
33
29
 
30
+ export { RsaPrivateKey, RsaPublicKey, MAX_RSA_KEY_SIZE } from './rsa-class.js'
31
+ export { Ed25519PrivateKey, Ed25519PublicKey } from './ed25519-class.js'
32
+ export { Secp256k1PrivateKey, Secp256k1PublicKey } from './secp256k1-class.js'
33
+ export type { JWKKeyPair } from './interface.js'
34
+
34
35
  export const supportedKeys = {
35
36
  rsa: RSA,
36
37
  ed25519: Ed25519,
@@ -144,12 +145,9 @@ export async function importKey (encryptedKey: string, password: string): Promis
144
145
  // Ignore and try the old pem decrypt
145
146
  }
146
147
 
147
- // Only rsa supports pem right now
148
- const key = forge.pki.decryptRsaPrivateKey(encryptedKey, password)
149
- if (key === null) {
150
- throw new CodeError('Cannot read the key, most likely the password is wrong or not a RSA key', 'ERR_CANNOT_DECRYPT_PEM')
148
+ if (!encryptedKey.includes('BEGIN')) {
149
+ throw new CodeError('Encrypted key was not a libp2p-key or a PEM file', 'ERR_INVALID_IMPORT_FORMAT')
151
150
  }
152
- let der = forge.asn1.toDer(forge.pki.privateKeyToAsn1(key))
153
- der = uint8ArrayFromString(der.getBytes(), 'ascii')
154
- return supportedKeys.rsa.unmarshalRsaPrivateKey(der)
151
+
152
+ return importFromPem(encryptedKey, password)
155
153
  }
@@ -1,9 +1,7 @@
1
1
  import { CodeError } from '@libp2p/interface'
2
2
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
3
- import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
4
3
  import randomBytes from '../random-bytes.js'
5
4
  import webcrypto from '../webcrypto.js'
6
- import { jwk2pub, jwk2priv } from './jwk2pem.js'
7
5
  import * as utils from './rsa-utils.js'
8
6
  import type { JWKKeyPair } from './interface.js'
9
7
  import type { Uint8ArrayList } from 'uint8arraylist'
@@ -130,33 +128,6 @@ async function derivePublicFromPrivate (jwKey: JsonWebKey): Promise<CryptoKey> {
130
128
  )
131
129
  }
132
130
 
133
- /*
134
-
135
- RSA encryption/decryption for the browser with webcrypto workaround
136
- "bloody dark magic. webcrypto's why."
137
-
138
- Explanation:
139
- - Convert JWK to nodeForge
140
- - Convert msg Uint8Array to nodeForge buffer: ByteBuffer is a "binary-string backed buffer", so let's make our Uint8Array a binary string
141
- - Convert resulting nodeForge buffer to Uint8Array: it returns a binary string, turn that into a Uint8Array
142
-
143
- */
144
-
145
- function convertKey (key: JsonWebKey, pub: boolean, msg: Uint8Array | Uint8ArrayList, handle: (msg: string, key: { encrypt(msg: string): string, decrypt(msg: string): string }) => string): Uint8Array {
146
- const fkey = pub ? jwk2pub(key) : jwk2priv(key)
147
- const fmsg = uint8ArrayToString(msg instanceof Uint8Array ? msg : msg.subarray(), 'ascii')
148
- const fomsg = handle(fmsg, fkey)
149
- return uint8ArrayFromString(fomsg, 'ascii')
150
- }
151
-
152
- export function encrypt (key: JsonWebKey, msg: Uint8Array | Uint8ArrayList): Uint8Array {
153
- return convertKey(key, true, msg, (msg, key) => key.encrypt(msg))
154
- }
155
-
156
- export function decrypt (key: JsonWebKey, msg: Uint8Array | Uint8ArrayList): Uint8Array {
157
- return convertKey(key, false, msg, (msg, key) => key.decrypt(msg))
158
- }
159
-
160
131
  export function keySize (jwk: JsonWebKey): number {
161
132
  if (jwk.kty !== 'RSA') {
162
133
  throw new CodeError('invalid key type', 'ERR_INVALID_KEY_TYPE')
@@ -1,9 +1,6 @@
1
1
  import { CodeError } from '@libp2p/interface'
2
2
  import { sha256 } from 'multiformats/hashes/sha2'
3
- // @ts-expect-error types are missing
4
- import forge from 'node-forge/lib/forge.js'
5
3
  import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
6
- import 'node-forge/lib/sha512.js'
7
4
  import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
8
5
  import { isPromise } from '../util.js'
9
6
  import { exporter } from './exporter.js'
@@ -12,7 +9,7 @@ import * as crypto from './rsa.js'
12
9
  import type { Multibase } from 'multiformats'
13
10
  import type { Uint8ArrayList } from 'uint8arraylist'
14
11
 
15
- export const MAX_KEY_SIZE = 8192
12
+ export const MAX_RSA_KEY_SIZE = 8192
16
13
 
17
14
  export class RsaPublicKey {
18
15
  private readonly _key: JsonWebKey
@@ -36,10 +33,6 @@ export class RsaPublicKey {
36
33
  }).subarray()
37
34
  }
38
35
 
39
- encrypt (bytes: Uint8Array | Uint8ArrayList): Uint8Array {
40
- return crypto.encrypt(this._key, bytes)
41
- }
42
-
43
36
  equals (key: any): boolean | boolean {
44
37
  return uint8ArrayEquals(this.bytes, key.bytes)
45
38
  }
@@ -80,10 +73,6 @@ export class RsaPrivateKey {
80
73
  return new RsaPublicKey(this._publicKey)
81
74
  }
82
75
 
83
- decrypt (bytes: Uint8Array | Uint8ArrayList): Uint8Array {
84
- return crypto.decrypt(this._key, bytes)
85
- }
86
-
87
76
  marshal (): Uint8Array {
88
77
  return crypto.utils.jwkToPkcs1(this._key)
89
78
  }
@@ -122,21 +111,15 @@ export class RsaPrivateKey {
122
111
  }
123
112
 
124
113
  /**
125
- * Exports the key into a password protected PEM format
114
+ * Exports the key as libp2p-key - a aes-gcm encrypted value with the key
115
+ * derived from the password.
116
+ *
117
+ * To export it as a password protected PEM file, please use the `exportPEM`
118
+ * function from `@libp2p/rsa`.
126
119
  */
127
120
  async export (password: string, format = 'pkcs-8'): Promise<Multibase<'m'>> {
128
121
  if (format === 'pkcs-8') {
129
- const buffer = new forge.util.ByteBuffer(this.marshal())
130
- const asn1 = forge.asn1.fromDer(buffer)
131
- const privateKey = forge.pki.privateKeyFromAsn1(asn1)
132
-
133
- const options = {
134
- algorithm: 'aes256',
135
- count: 10000,
136
- saltSize: 128 / 8,
137
- prfAlgorithm: 'sha512'
138
- }
139
- return forge.pki.encryptRsaPrivateKey(privateKey, password, options)
122
+ return crypto.utils.exportToPem(this, password)
140
123
  } else if (format === 'libp2p-key') {
141
124
  return exporter(this.bytes, password)
142
125
  } else {
@@ -148,7 +131,7 @@ export class RsaPrivateKey {
148
131
  export async function unmarshalRsaPrivateKey (bytes: Uint8Array): Promise<RsaPrivateKey> {
149
132
  const jwk = crypto.utils.pkcs1ToJwk(bytes)
150
133
 
151
- if (crypto.keySize(jwk) > MAX_KEY_SIZE) {
134
+ if (crypto.keySize(jwk) > MAX_RSA_KEY_SIZE) {
152
135
  throw new CodeError('key size is too large', 'ERR_KEY_SIZE_TOO_LARGE')
153
136
  }
154
137
 
@@ -160,7 +143,7 @@ export async function unmarshalRsaPrivateKey (bytes: Uint8Array): Promise<RsaPri
160
143
  export function unmarshalRsaPublicKey (bytes: Uint8Array): RsaPublicKey {
161
144
  const jwk = crypto.utils.pkixToJwk(bytes)
162
145
 
163
- if (crypto.keySize(jwk) > MAX_KEY_SIZE) {
146
+ if (crypto.keySize(jwk) > MAX_RSA_KEY_SIZE) {
164
147
  throw new CodeError('key size is too large', 'ERR_KEY_SIZE_TOO_LARGE')
165
148
  }
166
149
 
@@ -168,7 +151,7 @@ export function unmarshalRsaPublicKey (bytes: Uint8Array): RsaPublicKey {
168
151
  }
169
152
 
170
153
  export async function fromJwk (jwk: JsonWebKey): Promise<RsaPrivateKey> {
171
- if (crypto.keySize(jwk) > MAX_KEY_SIZE) {
154
+ if (crypto.keySize(jwk) > MAX_RSA_KEY_SIZE) {
172
155
  throw new CodeError('key size is too large', 'ERR_KEY_SIZE_TOO_LARGE')
173
156
  }
174
157
 
@@ -178,7 +161,7 @@ export async function fromJwk (jwk: JsonWebKey): Promise<RsaPrivateKey> {
178
161
  }
179
162
 
180
163
  export async function generateKeyPair (bits: number): Promise<RsaPrivateKey> {
181
- if (bits > MAX_KEY_SIZE) {
164
+ if (bits > MAX_RSA_KEY_SIZE) {
182
165
  throw new CodeError('key size is too large', 'ERR_KEY_SIZE_TOO_LARGE')
183
166
  }
184
167