@libp2p/crypto 1.0.13 → 1.0.15
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 +6 -6
- package/dist/src/ciphers/aes-gcm.browser.d.ts +8 -0
- package/dist/src/ciphers/aes-gcm.browser.d.ts.map +1 -1
- package/dist/src/ciphers/aes-gcm.browser.js +34 -8
- package/dist/src/ciphers/aes-gcm.browser.js.map +1 -1
- package/dist/src/keys/keys.d.ts +3 -3
- package/dist/src/keys/keys.d.ts.map +1 -1
- package/dist/src/keys/keys.js +1 -0
- package/dist/src/keys/keys.js.map +1 -1
- package/package.json +4 -2
- package/src/ciphers/aes-gcm.browser.ts +33 -7
- package/src/keys/keys.ts +4 -3
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
import type { CreateOptions, AESCipher } from './interface.js';
|
|
2
|
+
export declare function isWebkitLinux(): boolean;
|
|
3
|
+
export declare const derivedEmptyPasswordKey: {
|
|
4
|
+
alg: string;
|
|
5
|
+
ext: boolean;
|
|
6
|
+
k: string;
|
|
7
|
+
key_ops: string[];
|
|
8
|
+
kty: string;
|
|
9
|
+
};
|
|
2
10
|
export declare function create(opts?: CreateOptions): AESCipher;
|
|
3
11
|
//# sourceMappingURL=aes-gcm.browser.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aes-gcm.browser.d.ts","sourceRoot":"","sources":["../../../src/ciphers/aes-gcm.browser.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"aes-gcm.browser.d.ts","sourceRoot":"","sources":["../../../src/ciphers/aes-gcm.browser.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAE9D,wBAAgB,aAAa,IAAK,OAAO,CAExC;AAYD,eAAO,MAAM,uBAAuB;;;;;;CAA0G,CAAA;AAI9I,wBAAgB,MAAM,CAAE,IAAI,CAAC,EAAE,aAAa,GAAG,SAAS,CA4EvD"}
|
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import { concat } from 'uint8arrays/concat';
|
|
2
2
|
import { fromString } from 'uint8arrays/from-string';
|
|
3
3
|
import webcrypto from '../webcrypto.js';
|
|
4
|
+
export function isWebkitLinux() {
|
|
5
|
+
return typeof navigator !== 'undefined' && navigator.userAgent.includes('Safari') && navigator.userAgent.includes('Linux') && !navigator.userAgent.includes('Chrome');
|
|
6
|
+
}
|
|
7
|
+
// WebKit on Linux does not support deriving a key from an empty PBKDF2 key.
|
|
8
|
+
// So, as a workaround, we provide the generated key as a constant. We test that
|
|
9
|
+
// this generated key is accurate in test/workaround.spec.ts
|
|
10
|
+
// Generated via:
|
|
11
|
+
// await crypto.subtle.exportKey('jwk',
|
|
12
|
+
// await crypto.subtle.deriveKey(
|
|
13
|
+
// { name: 'PBKDF2', salt: new Uint8Array(16), iterations: 32767, hash: { name: 'SHA-256' } },
|
|
14
|
+
// await crypto.subtle.importKey('raw', new Uint8Array(0), { name: 'PBKDF2' }, false, ['deriveKey']),
|
|
15
|
+
// { name: 'AES-GCM', length: 128 }, true, ['encrypt', 'decrypt'])
|
|
16
|
+
// )
|
|
17
|
+
export const derivedEmptyPasswordKey = { alg: 'A128GCM', ext: true, k: 'scm9jmO_4BJAgdwWGVulLg', key_ops: ['encrypt', 'decrypt'], kty: 'oct' };
|
|
4
18
|
// Based off of code from https://github.com/luke-park/SecureCompatibleEncryptionExamples
|
|
5
19
|
export function create(opts) {
|
|
6
20
|
const algorithm = opts?.algorithm ?? 'AES-GCM';
|
|
@@ -22,10 +36,16 @@ export function create(opts) {
|
|
|
22
36
|
if (typeof password === 'string') {
|
|
23
37
|
password = fromString(password);
|
|
24
38
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
39
|
+
let cryptoKey;
|
|
40
|
+
if (password.length === 0 && isWebkitLinux()) {
|
|
41
|
+
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['encrypt']);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
// Derive a key using PBKDF2.
|
|
45
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } };
|
|
46
|
+
const rawKey = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey']);
|
|
47
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, rawKey, { name: algorithm, length: keyLength }, true, ['encrypt']);
|
|
48
|
+
}
|
|
29
49
|
// Encrypt the string.
|
|
30
50
|
const ciphertext = await crypto.subtle.encrypt(aesGcm, cryptoKey, data);
|
|
31
51
|
return concat([salt, aesGcm.iv, new Uint8Array(ciphertext)]);
|
|
@@ -44,10 +64,16 @@ export function create(opts) {
|
|
|
44
64
|
if (typeof password === 'string') {
|
|
45
65
|
password = fromString(password);
|
|
46
66
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
67
|
+
let cryptoKey;
|
|
68
|
+
if (password.length === 0 && isWebkitLinux()) {
|
|
69
|
+
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['decrypt']);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
// Derive the key using PBKDF2.
|
|
73
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } };
|
|
74
|
+
const rawKey = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey']);
|
|
75
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, rawKey, { name: algorithm, length: keyLength }, true, ['decrypt']);
|
|
76
|
+
}
|
|
51
77
|
// Decrypt the string.
|
|
52
78
|
const plaintext = await crypto.subtle.decrypt(aesGcm, cryptoKey, ciphertext);
|
|
53
79
|
return new Uint8Array(pl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AAE,IAAI,EAAE,MAAM,EAAE,EAAE,CAAA;YACjF,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,CAAC,WAAW,CAAC,CAAC,CAAA;YACvG,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,YAAY,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,IAAI,EAAE,CAAC,SAAS,CAAC,CAAC,CAAA;SAC3H;QAED,sBAAsB;QACtB,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,SAAS,EAAE,UAAU,CAAC,CAAA;QAC5E,OAAO,IAAI,UAAU,CAAC,SAAS,CAAC,CAAA;IAClC,CAAC;IAED,MAAM,MAAM,GAAc;QACxB,OAAO;QACP,OAAO;KACR,CAAA;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/dist/src/keys/keys.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
2
1
|
import type { Codec } from 'protons-runtime';
|
|
2
|
+
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
3
3
|
export declare enum KeyType {
|
|
4
4
|
RSA = "RSA",
|
|
5
5
|
Ed25519 = "Ed25519",
|
|
@@ -14,7 +14,7 @@ export interface PublicKey {
|
|
|
14
14
|
}
|
|
15
15
|
export declare namespace PublicKey {
|
|
16
16
|
const codec: () => Codec<PublicKey>;
|
|
17
|
-
const encode: (obj: PublicKey) => Uint8Array;
|
|
17
|
+
const encode: (obj: Partial<PublicKey>) => Uint8Array;
|
|
18
18
|
const decode: (buf: Uint8Array | Uint8ArrayList) => PublicKey;
|
|
19
19
|
}
|
|
20
20
|
export interface PrivateKey {
|
|
@@ -23,7 +23,7 @@ export interface PrivateKey {
|
|
|
23
23
|
}
|
|
24
24
|
export declare namespace PrivateKey {
|
|
25
25
|
const codec: () => Codec<PrivateKey>;
|
|
26
|
-
const encode: (obj: PrivateKey) => Uint8Array;
|
|
26
|
+
const encode: (obj: Partial<PrivateKey>) => Uint8Array;
|
|
27
27
|
const decode: (buf: Uint8Array | Uint8ArrayList) => PrivateKey;
|
|
28
28
|
}
|
|
29
29
|
//# sourceMappingURL=keys.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../../../src/keys/keys.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../../../src/keys/keys.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AAC5C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,oBAAY,OAAO;IACjB,GAAG,QAAQ;IACX,OAAO,YAAY;IACnB,SAAS,cAAc;CACxB;AAQD,yBAAiB,OAAO,CAAC;IAChB,MAAM,KAAK,QAAO,MAAM,OAAO,CAErC,CAAA;CACF;AACD,MAAM,WAAW,SAAS;IACxB,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,IAAI,CAAC,EAAE,UAAU,CAAA;CAClB;AAED,yBAAiB,SAAS,CAAC;IAGlB,MAAM,KAAK,QAAO,MAAM,SAAS,CA8CvC,CAAA;IAEM,MAAM,MAAM,QAAS,QAAQ,SAAS,CAAC,KAAG,UAEhD,CAAA;IAEM,MAAM,MAAM,QAAS,UAAU,GAAG,cAAc,KAAG,SAEzD,CAAA;CACF;AAED,MAAM,WAAW,UAAU;IACzB,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,IAAI,CAAC,EAAE,UAAU,CAAA;CAClB;AAED,yBAAiB,UAAU,CAAC;IAGnB,MAAM,KAAK,QAAO,MAAM,UAAU,CA8CxC,CAAA;IAEM,MAAM,MAAM,QAAS,QAAQ,UAAU,CAAC,KAAG,UAEjD,CAAA;IAEM,MAAM,MAAM,QAAS,UAAU,GAAG,cAAc,KAAG,UAEzD,CAAA;CACF"}
|
package/dist/src/keys/keys.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
/* eslint-disable complexity */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
5
6
|
import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime';
|
|
6
7
|
export var KeyType;
|
|
7
8
|
(function (KeyType) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../../../src/keys/keys.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,+BAA+B;AAC/B,oDAAoD;AACpD,8EAA8E;
|
|
1
|
+
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../../../src/keys/keys.ts"],"names":[],"mappings":"AAAA,kCAAkC;AAClC,+BAA+B;AAC/B,oDAAoD;AACpD,8EAA8E;AAC9E,0DAA0D;AAE1D,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAIpF,MAAM,CAAN,IAAY,OAIX;AAJD,WAAY,OAAO;IACjB,sBAAW,CAAA;IACX,8BAAmB,CAAA;IACnB,kCAAuB,CAAA;AACzB,CAAC,EAJW,OAAO,KAAP,OAAO,QAIlB;AAED,IAAK,eAIJ;AAJD,WAAK,eAAe;IAClB,mDAAO,CAAA;IACP,2DAAW,CAAA;IACX,+DAAa,CAAA;AACf,CAAC,EAJI,eAAe,KAAf,eAAe,QAInB;AAED,WAAiB,OAAO;IACT,aAAK,GAAG,GAAmB,EAAE;QACxC,OAAO,WAAW,CAAU,eAAe,CAAC,CAAA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CAAC,GAAG,EAAE,UAAU,CAAC,KAAK,EAAE,CAAC,CAAA;IAC/C,CAAC,CAAA;AACH,CAAC,EA1DgB,UAAU,KAAV,UAAU,QA0D1B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/crypto",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
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",
|
|
@@ -172,6 +172,7 @@
|
|
|
172
172
|
"test:chrome-webworker": "aegir test -t webworker",
|
|
173
173
|
"test:firefox": "aegir test -t browser -- --browser firefox",
|
|
174
174
|
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
|
|
175
|
+
"test:webkit": "bash -c '[ \"${CI}\" == \"true\" ] && playwright install-deps'; aegir test -t browser -- --browser webkit",
|
|
175
176
|
"test:node": "aegir test -t node --cov",
|
|
176
177
|
"test:electron-main": "aegir test -t electron-main",
|
|
177
178
|
"release": "aegir release",
|
|
@@ -185,7 +186,7 @@
|
|
|
185
186
|
"@noble/secp256k1": "^1.5.4",
|
|
186
187
|
"multiformats": "^11.0.0",
|
|
187
188
|
"node-forge": "^1.1.0",
|
|
188
|
-
"protons-runtime": "^
|
|
189
|
+
"protons-runtime": "^5.0.0",
|
|
189
190
|
"uint8arraylist": "^2.4.3",
|
|
190
191
|
"uint8arrays": "^4.0.2"
|
|
191
192
|
},
|
|
@@ -193,6 +194,7 @@
|
|
|
193
194
|
"@types/mocha": "^10.0.0",
|
|
194
195
|
"aegir": "^38.1.2",
|
|
195
196
|
"benchmark": "^2.1.4",
|
|
197
|
+
"protons": "^7.0.2",
|
|
196
198
|
"util": "^0.12.5"
|
|
197
199
|
},
|
|
198
200
|
"browser": {
|
|
@@ -3,6 +3,22 @@ import { fromString } from 'uint8arrays/from-string'
|
|
|
3
3
|
import webcrypto from '../webcrypto.js'
|
|
4
4
|
import type { CreateOptions, AESCipher } from './interface.js'
|
|
5
5
|
|
|
6
|
+
export function isWebkitLinux (): boolean {
|
|
7
|
+
return typeof navigator !== 'undefined' && navigator.userAgent.includes('Safari') && navigator.userAgent.includes('Linux') && !navigator.userAgent.includes('Chrome')
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// WebKit on Linux does not support deriving a key from an empty PBKDF2 key.
|
|
11
|
+
// So, as a workaround, we provide the generated key as a constant. We test that
|
|
12
|
+
// this generated key is accurate in test/workaround.spec.ts
|
|
13
|
+
// Generated via:
|
|
14
|
+
// await crypto.subtle.exportKey('jwk',
|
|
15
|
+
// await crypto.subtle.deriveKey(
|
|
16
|
+
// { name: 'PBKDF2', salt: new Uint8Array(16), iterations: 32767, hash: { name: 'SHA-256' } },
|
|
17
|
+
// await crypto.subtle.importKey('raw', new Uint8Array(0), { name: 'PBKDF2' }, false, ['deriveKey']),
|
|
18
|
+
// { name: 'AES-GCM', length: 128 }, true, ['encrypt', 'decrypt'])
|
|
19
|
+
// )
|
|
20
|
+
export const derivedEmptyPasswordKey = { alg: 'A128GCM', ext: true, k: 'scm9jmO_4BJAgdwWGVulLg', key_ops: ['encrypt', 'decrypt'], kty: 'oct' }
|
|
21
|
+
|
|
6
22
|
// Based off of code from https://github.com/luke-park/SecureCompatibleEncryptionExamples
|
|
7
23
|
|
|
8
24
|
export function create (opts?: CreateOptions): AESCipher {
|
|
@@ -29,10 +45,15 @@ export function create (opts?: CreateOptions): AESCipher {
|
|
|
29
45
|
password = fromString(password)
|
|
30
46
|
}
|
|
31
47
|
|
|
48
|
+
let cryptoKey: CryptoKey
|
|
49
|
+
if (password.length === 0 && isWebkitLinux()) {
|
|
50
|
+
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['encrypt'])
|
|
51
|
+
} else {
|
|
32
52
|
// Derive a key using PBKDF2.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
53
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } }
|
|
54
|
+
const rawKey = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey'])
|
|
55
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, rawKey, { name: algorithm, length: keyLength }, true, ['encrypt'])
|
|
56
|
+
}
|
|
36
57
|
|
|
37
58
|
// Encrypt the string.
|
|
38
59
|
const ciphertext = await crypto.subtle.encrypt(aesGcm, cryptoKey, data)
|
|
@@ -55,10 +76,15 @@ export function create (opts?: CreateOptions): AESCipher {
|
|
|
55
76
|
password = fromString(password)
|
|
56
77
|
}
|
|
57
78
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
79
|
+
let cryptoKey: CryptoKey
|
|
80
|
+
if (password.length === 0 && isWebkitLinux()) {
|
|
81
|
+
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['decrypt'])
|
|
82
|
+
} else {
|
|
83
|
+
// Derive the key using PBKDF2.
|
|
84
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } }
|
|
85
|
+
const rawKey = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey'])
|
|
86
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, rawKey, { name: algorithm, length: keyLength }, true, ['decrypt'])
|
|
87
|
+
}
|
|
62
88
|
|
|
63
89
|
// Decrypt the string.
|
|
64
90
|
const plaintext = await crypto.subtle.decrypt(aesGcm, cryptoKey, ciphertext)
|
package/src/keys/keys.ts
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
/* eslint-disable complexity */
|
|
3
3
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-empty-interface */
|
|
5
6
|
|
|
6
7
|
import { enumeration, encodeMessage, decodeMessage, message } from 'protons-runtime'
|
|
7
|
-
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
8
8
|
import type { Codec } from 'protons-runtime'
|
|
9
|
+
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
9
10
|
|
|
10
11
|
export enum KeyType {
|
|
11
12
|
RSA = 'RSA',
|
|
@@ -80,7 +81,7 @@ export namespace PublicKey {
|
|
|
80
81
|
return _codec
|
|
81
82
|
}
|
|
82
83
|
|
|
83
|
-
export const encode = (obj: PublicKey): Uint8Array => {
|
|
84
|
+
export const encode = (obj: Partial<PublicKey>): Uint8Array => {
|
|
84
85
|
return encodeMessage(obj, PublicKey.codec())
|
|
85
86
|
}
|
|
86
87
|
|
|
@@ -145,7 +146,7 @@ export namespace PrivateKey {
|
|
|
145
146
|
return _codec
|
|
146
147
|
}
|
|
147
148
|
|
|
148
|
-
export const encode = (obj: PrivateKey): Uint8Array => {
|
|
149
|
+
export const encode = (obj: Partial<PrivateKey>): Uint8Array => {
|
|
149
150
|
return encodeMessage(obj, PrivateKey.codec())
|
|
150
151
|
}
|
|
151
152
|
|