@libp2p/crypto 1.0.10 → 1.0.12
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.
- package/dist/index.min.js +15 -7
- package/dist/src/aes/cipher-mode.js +2 -2
- package/dist/src/aes/cipher-mode.js.map +1 -1
- package/dist/src/keys/ecdh-browser.js +6 -6
- package/dist/src/keys/ecdh-browser.js.map +1 -1
- package/dist/src/keys/ecdh.js +2 -2
- package/dist/src/keys/ecdh.js.map +1 -1
- package/dist/src/keys/ed25519-class.js +3 -3
- package/dist/src/keys/ed25519-class.js.map +1 -1
- package/dist/src/keys/ed25519.d.ts +1 -1
- package/dist/src/keys/ed25519.d.ts.map +1 -1
- package/dist/src/keys/ed25519.js +16 -3
- package/dist/src/keys/ed25519.js.map +1 -1
- package/dist/src/keys/index.js +4 -4
- package/dist/src/keys/index.js.map +1 -1
- package/dist/src/keys/key-stretcher.js +3 -3
- package/dist/src/keys/key-stretcher.js.map +1 -1
- package/dist/src/keys/rsa-browser.js +2 -2
- package/dist/src/keys/rsa-browser.js.map +1 -1
- package/dist/src/keys/rsa-class.js +3 -3
- package/dist/src/keys/rsa-class.js.map +1 -1
- package/dist/src/keys/rsa-utils.js +3 -3
- package/dist/src/keys/rsa-utils.js.map +1 -1
- package/dist/src/keys/rsa.js +2 -2
- package/dist/src/keys/rsa.js.map +1 -1
- package/dist/src/keys/secp256k1-class.js +2 -2
- package/dist/src/keys/secp256k1-class.js.map +1 -1
- package/dist/src/keys/secp256k1.js +6 -6
- package/dist/src/keys/secp256k1.js.map +1 -1
- package/dist/src/pbkdf2.js +2 -2
- package/dist/src/pbkdf2.js.map +1 -1
- package/dist/src/random-bytes.js +2 -2
- package/dist/src/random-bytes.js.map +1 -1
- package/dist/typedoc-urls.json +22 -6
- package/package.json +3 -3
- package/src/aes/cipher-mode.ts +2 -2
- package/src/keys/ecdh-browser.ts +6 -6
- package/src/keys/ecdh.ts +2 -2
- package/src/keys/ed25519-class.ts +3 -3
- package/src/keys/ed25519.ts +19 -4
- package/src/keys/index.ts +4 -4
- package/src/keys/key-stretcher.ts +3 -3
- package/src/keys/rsa-browser.ts +2 -2
- package/src/keys/rsa-class.ts +3 -3
- package/src/keys/rsa-utils.ts +3 -3
- package/src/keys/rsa.ts +2 -2
- package/src/keys/secp256k1-class.ts +2 -2
- package/src/keys/secp256k1.ts +6 -6
- package/src/pbkdf2.ts +2 -2
- package/src/random-bytes.ts +2 -2
package/dist/src/random-bytes.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { utils } from '@noble/secp256k1';
|
|
2
|
-
import
|
|
2
|
+
import { CodeError } from '@libp2p/interfaces/errors';
|
|
3
3
|
export default function randomBytes(length) {
|
|
4
4
|
if (isNaN(length) || length <= 0) {
|
|
5
|
-
throw
|
|
5
|
+
throw new CodeError('random bytes length must be a Number bigger than 0', 'ERR_INVALID_LENGTH');
|
|
6
6
|
}
|
|
7
7
|
return utils.randomBytes(length);
|
|
8
8
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random-bytes.js","sourceRoot":"","sources":["../../src/random-bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACxC,OAAO,
|
|
1
|
+
{"version":3,"file":"random-bytes.js","sourceRoot":"","sources":["../../src/random-bytes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AAErD,MAAM,CAAC,OAAO,UAAU,WAAW,CAAE,MAAc;IACjD,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE;QAChC,MAAM,IAAI,SAAS,CAAC,oDAAoD,EAAE,oBAAoB,CAAC,CAAA;KAChG;IACD,OAAO,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAA;AAClC,CAAC"}
|
package/dist/typedoc-urls.json
CHANGED
|
@@ -1,18 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"AESCipher": "https://libp2p.github.io/js-libp2p-crypto/interfaces/aes.AESCipher.html",
|
|
3
|
-
"create": "https://libp2p.github.io/js-libp2p-crypto/functions/
|
|
3
|
+
"create": "https://libp2p.github.io/js-libp2p-crypto/functions/aes.create.html",
|
|
4
4
|
"pbkdf2": "https://libp2p.github.io/js-libp2p-crypto/functions/index.pbkdf2.html",
|
|
5
5
|
"randomBytes": "https://libp2p.github.io/js-libp2p-crypto/functions/index.randomBytes.html",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
6
|
+
"Ed25519PrivateKey": "https://libp2p.github.io/js-libp2p-crypto/classes/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_ed25519_class_.Ed25519PrivateKey.html",
|
|
7
|
+
"Ed25519PublicKey": "https://libp2p.github.io/js-libp2p-crypto/classes/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_ed25519_class_.Ed25519PublicKey.html",
|
|
8
|
+
"generateKeyPair": "https://libp2p.github.io/js-libp2p-crypto/functions/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_ed25519_class_.generateKeyPair.html",
|
|
9
|
+
"generateKeyPairFromSeed": "https://libp2p.github.io/js-libp2p-crypto/functions/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_ed25519_class_.generateKeyPairFromSeed.html",
|
|
10
|
+
"unmarshalEd25519PrivateKey": "https://libp2p.github.io/js-libp2p-crypto/functions/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_ed25519_class_.unmarshalEd25519PrivateKey.html",
|
|
11
|
+
"unmarshalEd25519PublicKey": "https://libp2p.github.io/js-libp2p-crypto/functions/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_ed25519_class_.unmarshalEd25519PublicKey.html",
|
|
12
|
+
"RsaPrivateKey": "https://libp2p.github.io/js-libp2p-crypto/classes/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_rsa_class_.RsaPrivateKey.html",
|
|
13
|
+
"RsaPublicKey": "https://libp2p.github.io/js-libp2p-crypto/classes/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_rsa_class_.RsaPublicKey.html",
|
|
14
|
+
"fromJwk": "https://libp2p.github.io/js-libp2p-crypto/functions/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_rsa_class_.fromJwk.html",
|
|
15
|
+
"unmarshalRsaPrivateKey": "https://libp2p.github.io/js-libp2p-crypto/functions/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_rsa_class_.unmarshalRsaPrivateKey.html",
|
|
16
|
+
"unmarshalRsaPublicKey": "https://libp2p.github.io/js-libp2p-crypto/functions/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_rsa_class_.unmarshalRsaPublicKey.html",
|
|
17
|
+
"Secp256k1PrivateKey": "https://libp2p.github.io/js-libp2p-crypto/classes/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_secp256k1_class_.Secp256k1PrivateKey.html",
|
|
18
|
+
"Secp256k1PublicKey": "https://libp2p.github.io/js-libp2p-crypto/classes/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_secp256k1_class_.Secp256k1PublicKey.html",
|
|
19
|
+
"unmarshalSecp256k1PrivateKey": "https://libp2p.github.io/js-libp2p-crypto/functions/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_secp256k1_class_.unmarshalSecp256k1PrivateKey.html",
|
|
20
|
+
"unmarshalSecp256k1PublicKey": "https://libp2p.github.io/js-libp2p-crypto/functions/keys._internal_.__home_runner_work_js_libp2p_crypto_js_libp2p_crypto_src_keys_secp256k1_class_.unmarshalSecp256k1PublicKey.html",
|
|
21
|
+
"ECDHKey": "https://libp2p.github.io/js-libp2p-crypto/interfaces/keys._internal_.ECDHKey.html",
|
|
22
|
+
"ECDHKeyPair": "https://libp2p.github.io/js-libp2p-crypto/interfaces/keys._internal_.ECDHKeyPair.html",
|
|
23
|
+
"codec": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.keysPBM.KeyType.codec.html",
|
|
24
|
+
"decode": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.keysPBM.PrivateKey.decode.html",
|
|
25
|
+
"encode": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.keysPBM.PrivateKey.encode.html",
|
|
26
|
+
"KeyType": "https://libp2p.github.io/js-libp2p-crypto/enums/keys.keysPBM.KeyType-1.html",
|
|
9
27
|
"PrivateKey": "https://libp2p.github.io/js-libp2p-crypto/interfaces/keys.keysPBM.PrivateKey-1.html",
|
|
10
28
|
"PublicKey": "https://libp2p.github.io/js-libp2p-crypto/interfaces/keys.keysPBM.PublicKey-1.html",
|
|
11
29
|
"KeyTypes": "https://libp2p.github.io/js-libp2p-crypto/types/keys.KeyTypes.html",
|
|
12
30
|
"supportedKeys": "https://libp2p.github.io/js-libp2p-crypto/variables/keys.supportedKeys.html",
|
|
13
31
|
"generateEphemeralKeyPair": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.generateEphemeralKeyPair.html",
|
|
14
|
-
"generateKeyPair": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.generateKeyPair.html",
|
|
15
|
-
"generateKeyPairFromSeed": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.generateKeyPairFromSeed.html",
|
|
16
32
|
"importKey": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.importKey.html",
|
|
17
33
|
"keyStretcher": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.keyStretcher.html",
|
|
18
34
|
"marshalPrivateKey": "https://libp2p.github.io/js-libp2p-crypto/functions/keys.marshalPrivateKey.html",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/crypto",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "Crypto primitives for libp2p",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p-crypto#readme",
|
|
@@ -180,10 +180,10 @@
|
|
|
180
180
|
},
|
|
181
181
|
"dependencies": {
|
|
182
182
|
"@libp2p/interface-keys": "^1.0.2",
|
|
183
|
+
"@libp2p/interfaces": "^3.2.0",
|
|
183
184
|
"@noble/ed25519": "^1.6.0",
|
|
184
185
|
"@noble/secp256k1": "^1.5.4",
|
|
185
|
-
"
|
|
186
|
-
"multiformats": "^10.0.0",
|
|
186
|
+
"multiformats": "^11.0.0",
|
|
187
187
|
"node-forge": "^1.1.0",
|
|
188
188
|
"protons-runtime": "^4.0.1",
|
|
189
189
|
"uint8arrays": "^4.0.2"
|
package/src/aes/cipher-mode.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
2
2
|
|
|
3
3
|
const CIPHER_MODES = {
|
|
4
4
|
16: 'aes-128-ctr',
|
|
@@ -11,5 +11,5 @@ export function cipherMode (key: Uint8Array) {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
const modes = Object.entries(CIPHER_MODES).map(([k, v]) => `${k} (${v})`).join(' / ')
|
|
14
|
-
throw
|
|
14
|
+
throw new CodeError(`Invalid key length ${key.length} bytes. Must be ${modes}`, 'ERR_INVALID_KEY_LENGTH')
|
|
15
15
|
}
|
package/src/keys/ecdh-browser.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
2
2
|
import webcrypto from '../webcrypto.js'
|
|
3
3
|
import { base64urlToBuffer } from '../util.js'
|
|
4
4
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
@@ -17,7 +17,7 @@ const names = curveTypes.join(' / ')
|
|
|
17
17
|
|
|
18
18
|
export async function generateEphmeralKeyPair (curve: string) {
|
|
19
19
|
if (curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521') {
|
|
20
|
-
throw
|
|
20
|
+
throw new CodeError(`Unknown curve: ${curve}. Must be ${names}`, 'ERR_INVALID_CURVE')
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
const pair = await webcrypto.get().subtle.generateKey(
|
|
@@ -94,11 +94,11 @@ const curveLengths = {
|
|
|
94
94
|
// go-ipfs uses)
|
|
95
95
|
function marshalPublicKey (jwk: JsonWebKey) {
|
|
96
96
|
if (jwk.crv == null || jwk.x == null || jwk.y == null) {
|
|
97
|
-
throw
|
|
97
|
+
throw new CodeError('JWK was missing components', 'ERR_INVALID_PARAMETERS')
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
if (jwk.crv !== 'P-256' && jwk.crv !== 'P-384' && jwk.crv !== 'P-521') {
|
|
101
|
-
throw
|
|
101
|
+
throw new CodeError(`Unknown curve: ${jwk.crv}. Must be ${names}`, 'ERR_INVALID_CURVE')
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
const byteLen = curveLengths[jwk.crv]
|
|
@@ -113,13 +113,13 @@ function marshalPublicKey (jwk: JsonWebKey) {
|
|
|
113
113
|
// Unmarshal converts a point, serialized by Marshal, into an jwk encoded key
|
|
114
114
|
function unmarshalPublicKey (curve: string, key: Uint8Array) {
|
|
115
115
|
if (curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521') {
|
|
116
|
-
throw
|
|
116
|
+
throw new CodeError(`Unknown curve: ${curve}. Must be ${names}`, 'ERR_INVALID_CURVE')
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
const byteLen = curveLengths[curve]
|
|
120
120
|
|
|
121
121
|
if (!uint8ArrayEquals(key.subarray(0, 1), Uint8Array.from([4]))) {
|
|
122
|
-
throw
|
|
122
|
+
throw new CodeError('Cannot unmarshal public key - invalid key format', 'ERR_INVALID_KEY_FORMAT')
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
return {
|
package/src/keys/ecdh.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import crypto from 'crypto'
|
|
2
|
-
import
|
|
2
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
3
3
|
import type { ECDHKey, ECDHKeyPair } from './interface.js'
|
|
4
4
|
|
|
5
5
|
const curves = {
|
|
@@ -13,7 +13,7 @@ const names = curveTypes.join(' / ')
|
|
|
13
13
|
|
|
14
14
|
export async function generateEphmeralKeyPair (curve: string): Promise<ECDHKey> { // eslint-disable-line require-await
|
|
15
15
|
if (curve !== 'P-256' && curve !== 'P-384' && curve !== 'P-521') {
|
|
16
|
-
throw
|
|
16
|
+
throw new CodeError(`Unknown curve: ${curve}. Must be ${names}`, 'ERR_INVALID_CURVE')
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
const ecdh = crypto.createECDH(curves[curve])
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
2
2
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
3
3
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
4
4
|
import { base58btc } from 'multiformats/bases/base58'
|
|
@@ -101,7 +101,7 @@ export class Ed25519PrivateKey {
|
|
|
101
101
|
if (format === 'libp2p-key') {
|
|
102
102
|
return await exporter(this.bytes, password)
|
|
103
103
|
} else {
|
|
104
|
-
throw
|
|
104
|
+
throw new CodeError(`export format '${format}' is not supported`, 'ERR_INVALID_EXPORT_FORMAT')
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
}
|
|
@@ -139,7 +139,7 @@ export async function generateKeyPairFromSeed (seed: Uint8Array) {
|
|
|
139
139
|
function ensureKey (key: Uint8Array, length: number) {
|
|
140
140
|
key = Uint8Array.from(key ?? [])
|
|
141
141
|
if (key.length !== length) {
|
|
142
|
-
throw
|
|
142
|
+
throw new CodeError(`Key must be a Uint8Array of length ${length}, got ${key.length}`, 'ERR_INVALID_KEY_TYPE')
|
|
143
143
|
}
|
|
144
144
|
return key
|
|
145
145
|
}
|
package/src/keys/ed25519.ts
CHANGED
|
@@ -13,10 +13,25 @@ const SIGNATURE_BYTE_LENGTH = 64
|
|
|
13
13
|
export { PUBLIC_KEY_BYTE_LENGTH as publicKeyLength }
|
|
14
14
|
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }
|
|
15
15
|
|
|
16
|
-
function derivePublicKey (privateKey: Uint8Array) {
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
function derivePublicKey (privateKey: Uint8Array): Uint8Array {
|
|
17
|
+
const keyObject = crypto.createPrivateKey({
|
|
18
|
+
format: 'jwk',
|
|
19
|
+
key: {
|
|
20
|
+
crv: 'Ed25519',
|
|
21
|
+
x: '',
|
|
22
|
+
d: uint8arrayToString(privateKey, 'base64url'),
|
|
23
|
+
kty: 'OKP'
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
const jwk = keyObject.export({
|
|
27
|
+
format: 'jwk'
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
if (jwk.x == null || jwk.x === '') {
|
|
31
|
+
throw new Error('Could not export JWK public key')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return uint8arrayFromString(jwk.x, 'base64url')
|
|
20
35
|
}
|
|
21
36
|
|
|
22
37
|
export async function generateKey () {
|
package/src/keys/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import 'node-forge/lib/asn1.js'
|
|
|
3
3
|
import 'node-forge/lib/pbe.js'
|
|
4
4
|
// @ts-expect-error types are missing
|
|
5
5
|
import forge from 'node-forge/lib/forge.js'
|
|
6
|
-
import
|
|
6
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
7
7
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
8
8
|
import { keyStretcher } from './key-stretcher.js'
|
|
9
9
|
import generateEphemeralKeyPair from './ephemeral-keys.js'
|
|
@@ -27,7 +27,7 @@ export const supportedKeys = {
|
|
|
27
27
|
|
|
28
28
|
function unsupportedKey (type: string) {
|
|
29
29
|
const supported = Object.keys(supportedKeys).join(' / ')
|
|
30
|
-
return
|
|
30
|
+
return new CodeError(`invalid or unsupported key type ${type}. Must be ${supported}`, 'ERR_UNSUPPORTED_KEY_TYPE')
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
function typeToKey (type: string) {
|
|
@@ -49,7 +49,7 @@ export async function generateKeyPair (type: KeyTypes, bits?: number): Promise<P
|
|
|
49
49
|
// seed is a 32 byte uint8array
|
|
50
50
|
export async function generateKeyPairFromSeed (type: KeyTypes, seed: Uint8Array, bits?: number): Promise<PrivateKey> { // eslint-disable-line require-await
|
|
51
51
|
if (type.toLowerCase() !== 'ed25519') {
|
|
52
|
-
throw
|
|
52
|
+
throw new CodeError('Seed key derivation is unimplemented for RSA or secp256k1', 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE')
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
return await Ed25519.generateKeyPairFromSeed(seed)
|
|
@@ -121,7 +121,7 @@ export async function importKey (encryptedKey: string, password: string): Promis
|
|
|
121
121
|
// Only rsa supports pem right now
|
|
122
122
|
const key = forge.pki.decryptRsaPrivateKey(encryptedKey, password)
|
|
123
123
|
if (key === null) {
|
|
124
|
-
throw
|
|
124
|
+
throw new CodeError('Cannot read the key, most likely the password is wrong or not a RSA key', 'ERR_CANNOT_DECRYPT_PEM')
|
|
125
125
|
}
|
|
126
126
|
let der = forge.asn1.toDer(forge.pki.privateKeyToAsn1(key))
|
|
127
127
|
der = uint8ArrayFromString(der.getBytes(), 'ascii')
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
2
2
|
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
|
|
3
3
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
4
4
|
import * as hmac from '../hmac/index.js'
|
|
@@ -27,11 +27,11 @@ export async function keyStretcher (cipherType: 'AES-128' | 'AES-256' | 'Blowfis
|
|
|
27
27
|
|
|
28
28
|
if (cipher == null) {
|
|
29
29
|
const allowed = Object.keys(cipherMap).join(' / ')
|
|
30
|
-
throw
|
|
30
|
+
throw new CodeError(`unknown cipher type '${cipherType}'. Must be ${allowed}`, 'ERR_INVALID_CIPHER_TYPE')
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
if (hash == null) {
|
|
34
|
-
throw
|
|
34
|
+
throw new CodeError('missing hash type', 'ERR_MISSING_HASH_TYPE')
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
const cipherKeySize = cipher.keySize
|
package/src/keys/rsa-browser.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
|
4
4
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
5
5
|
import * as utils from './rsa-utils.js'
|
|
6
6
|
import { jwk2pub, jwk2priv } from './jwk2pem.js'
|
|
7
|
-
import
|
|
7
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
8
8
|
|
|
9
9
|
export { utils }
|
|
10
10
|
|
|
@@ -102,7 +102,7 @@ export async function hashAndVerify (key: JsonWebKey, sig: Uint8Array, msg: Uint
|
|
|
102
102
|
|
|
103
103
|
async function exportKey (pair: CryptoKeyPair) {
|
|
104
104
|
if (pair.privateKey == null || pair.publicKey == null) {
|
|
105
|
-
throw
|
|
105
|
+
throw new CodeError('Private and public key are required', 'ERR_INVALID_PARAMETERS')
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
return await Promise.all([
|
package/src/keys/rsa-class.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
3
|
-
import
|
|
3
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
4
4
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
5
5
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
6
6
|
import 'node-forge/lib/sha512.js'
|
|
@@ -66,7 +66,7 @@ export class RsaPrivateKey {
|
|
|
66
66
|
|
|
67
67
|
get public () {
|
|
68
68
|
if (this._publicKey == null) {
|
|
69
|
-
throw
|
|
69
|
+
throw new CodeError('public key not provided', 'ERR_PUBKEY_NOT_PROVIDED')
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
return new RsaPublicKey(this._publicKey)
|
|
@@ -128,7 +128,7 @@ export class RsaPrivateKey {
|
|
|
128
128
|
} else if (format === 'libp2p-key') {
|
|
129
129
|
return await exporter(this.bytes, password)
|
|
130
130
|
} else {
|
|
131
|
-
throw
|
|
131
|
+
throw new CodeError(`export format '${format}' is not supported`, 'ERR_INVALID_EXPORT_FORMAT')
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
}
|
package/src/keys/rsa-utils.ts
CHANGED
|
@@ -5,7 +5,7 @@ import forge from 'node-forge/lib/forge.js'
|
|
|
5
5
|
import { bigIntegerToUintBase64url, base64urlToBigInteger } from './../util.js'
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
7
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
8
|
-
import
|
|
8
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
9
9
|
|
|
10
10
|
// Convert a PKCS#1 in ASN1 DER format to a JWK key
|
|
11
11
|
export function pkcs1ToJwk (bytes: Uint8Array): JsonWebKey {
|
|
@@ -30,7 +30,7 @@ export function pkcs1ToJwk (bytes: Uint8Array): JsonWebKey {
|
|
|
30
30
|
// Convert a JWK key into PKCS#1 in ASN1 DER format
|
|
31
31
|
export function jwkToPkcs1 (jwk: JsonWebKey) {
|
|
32
32
|
if (jwk.n == null || jwk.e == null || jwk.d == null || jwk.p == null || jwk.q == null || jwk.dp == null || jwk.dq == null || jwk.qi == null) {
|
|
33
|
-
throw
|
|
33
|
+
throw new CodeError('JWK was missing components', 'ERR_INVALID_PARAMETERS')
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
const asn1 = forge.pki.privateKeyToAsn1({
|
|
@@ -62,7 +62,7 @@ export function pkixToJwk (bytes: Uint8Array): JsonWebKey {
|
|
|
62
62
|
// Convert a JWK key to PKCIX in ASN1 DER format
|
|
63
63
|
export function jwkToPkix (jwk: JsonWebKey) {
|
|
64
64
|
if (jwk.n == null || jwk.e == null) {
|
|
65
|
-
throw
|
|
65
|
+
throw new CodeError('JWK was missing components', 'ERR_INVALID_PARAMETERS')
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
const asn1 = forge.pki.publicKeyToAsn1({
|
package/src/keys/rsa.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import crypto from 'crypto'
|
|
2
2
|
import { promisify } from 'util'
|
|
3
|
-
import
|
|
3
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
4
4
|
import randomBytes from '../random-bytes.js'
|
|
5
5
|
import * as utils from './rsa-utils.js'
|
|
6
6
|
import type { JWKKeyPair } from './interface.js'
|
|
@@ -28,7 +28,7 @@ export async function generateKey (bits: number): Promise<JWKKeyPair> { // eslin
|
|
|
28
28
|
// Takes a jwk key
|
|
29
29
|
export async function unmarshalPrivateKey (key: JsonWebKey): Promise<JWKKeyPair> { // eslint-disable-line require-await
|
|
30
30
|
if (key == null) {
|
|
31
|
-
throw
|
|
31
|
+
throw new CodeError('Missing key parameter', 'ERR_MISSING_KEY')
|
|
32
32
|
}
|
|
33
33
|
return {
|
|
34
34
|
privateKey: key,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
2
|
-
import
|
|
2
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
3
3
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
4
4
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
5
5
|
import * as crypto from './secp256k1.js'
|
|
@@ -99,7 +99,7 @@ export class Secp256k1PrivateKey {
|
|
|
99
99
|
if (format === 'libp2p-key') {
|
|
100
100
|
return await exporter(this.bytes, password)
|
|
101
101
|
} else {
|
|
102
|
-
throw
|
|
102
|
+
throw new CodeError(`export format '${format}' is not supported`, 'ERR_INVALID_EXPORT_FORMAT')
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
}
|
package/src/keys/secp256k1.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
2
2
|
import * as secp from '@noble/secp256k1'
|
|
3
3
|
import { sha256 } from 'multiformats/hashes/sha2'
|
|
4
4
|
|
|
@@ -18,7 +18,7 @@ export async function hashAndSign (key: Uint8Array, msg: Uint8Array) {
|
|
|
18
18
|
try {
|
|
19
19
|
return await secp.sign(digest, key)
|
|
20
20
|
} catch (err) {
|
|
21
|
-
throw
|
|
21
|
+
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -30,7 +30,7 @@ export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint
|
|
|
30
30
|
const { digest } = await sha256.digest(msg)
|
|
31
31
|
return secp.verify(sig, digest, key)
|
|
32
32
|
} catch (err) {
|
|
33
|
-
throw
|
|
33
|
+
throw new CodeError(String(err), 'ERR_INVALID_INPUT')
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
|
@@ -48,7 +48,7 @@ export function validatePrivateKey (key: Uint8Array) {
|
|
|
48
48
|
try {
|
|
49
49
|
secp.getPublicKey(key, true)
|
|
50
50
|
} catch (err) {
|
|
51
|
-
throw
|
|
51
|
+
throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY')
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -56,7 +56,7 @@ export function validatePublicKey (key: Uint8Array) {
|
|
|
56
56
|
try {
|
|
57
57
|
secp.Point.fromHex(key)
|
|
58
58
|
} catch (err) {
|
|
59
|
-
throw
|
|
59
|
+
throw new CodeError(String(err), 'ERR_INVALID_PUBLIC_KEY')
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
|
|
@@ -64,6 +64,6 @@ export function computePublicKey (privateKey: Uint8Array) {
|
|
|
64
64
|
try {
|
|
65
65
|
return secp.getPublicKey(privateKey, true)
|
|
66
66
|
} catch (err) {
|
|
67
|
-
throw
|
|
67
|
+
throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY')
|
|
68
68
|
}
|
|
69
69
|
}
|
package/src/pbkdf2.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import forgePbkdf2 from 'node-forge/lib/pbkdf2.js'
|
|
3
3
|
// @ts-expect-error types are missing
|
|
4
4
|
import forgeUtil from 'node-forge/lib/util.js'
|
|
5
|
-
import
|
|
5
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Maps an IPFS hash name to its node-forge equivalent.
|
|
@@ -23,7 +23,7 @@ const hashName = {
|
|
|
23
23
|
export default function pbkdf2 (password: string, salt: string, iterations: number, keySize: number, hash: string): string {
|
|
24
24
|
if (hash !== 'sha1' && hash !== 'sha2-256' && hash !== 'sha2-512') {
|
|
25
25
|
const types = Object.keys(hashName).join(' / ')
|
|
26
|
-
throw
|
|
26
|
+
throw new CodeError(`Hash '${hash}' is unknown or not supported. Must be ${types}`, 'ERR_UNSUPPORTED_HASH_TYPE')
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
const hasher = hashName[hash]
|
package/src/random-bytes.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { utils } from '@noble/secp256k1'
|
|
2
|
-
import
|
|
2
|
+
import { CodeError } from '@libp2p/interfaces/errors'
|
|
3
3
|
|
|
4
4
|
export default function randomBytes (length: number): Uint8Array {
|
|
5
5
|
if (isNaN(length) || length <= 0) {
|
|
6
|
-
throw
|
|
6
|
+
throw new CodeError('random bytes length must be a Number bigger than 0', 'ERR_INVALID_LENGTH')
|
|
7
7
|
}
|
|
8
8
|
return utils.randomBytes(length)
|
|
9
9
|
}
|