@libp2p/crypto 1.0.14 → 1.0.16
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 +7 -7
- package/dist/src/ciphers/aes-gcm.browser.d.ts +7 -0
- package/dist/src/ciphers/aes-gcm.browser.d.ts.map +1 -1
- package/dist/src/ciphers/aes-gcm.browser.js +46 -8
- package/dist/src/ciphers/aes-gcm.browser.js.map +1 -1
- package/package.json +2 -1
- package/src/ciphers/aes-gcm.browser.ts +42 -7
|
@@ -1,3 +1,10 @@
|
|
|
1
1
|
import type { CreateOptions, AESCipher } from './interface.js';
|
|
2
|
+
export declare const derivedEmptyPasswordKey: {
|
|
3
|
+
alg: string;
|
|
4
|
+
ext: boolean;
|
|
5
|
+
k: string;
|
|
6
|
+
key_ops: string[];
|
|
7
|
+
kty: string;
|
|
8
|
+
};
|
|
2
9
|
export declare function create(opts?: CreateOptions): AESCipher;
|
|
3
10
|
//# 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;AAY9D,eAAO,MAAM,uBAAuB;;;;;;CAA0G,CAAA;AAI9I,wBAAgB,MAAM,CAAE,IAAI,CAAC,EAAE,aAAa,GAAG,SAAS,CAyFvD"}
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { concat } from 'uint8arrays/concat';
|
|
2
2
|
import { fromString } from 'uint8arrays/from-string';
|
|
3
3
|
import webcrypto from '../webcrypto.js';
|
|
4
|
+
// WebKit on Linux does not support deriving a key from an empty PBKDF2 key.
|
|
5
|
+
// So, as a workaround, we provide the generated key as a constant. We test that
|
|
6
|
+
// this generated key is accurate in test/workaround.spec.ts
|
|
7
|
+
// Generated via:
|
|
8
|
+
// await crypto.subtle.exportKey('jwk',
|
|
9
|
+
// await crypto.subtle.deriveKey(
|
|
10
|
+
// { name: 'PBKDF2', salt: new Uint8Array(16), iterations: 32767, hash: { name: 'SHA-256' } },
|
|
11
|
+
// await crypto.subtle.importKey('raw', new Uint8Array(0), { name: 'PBKDF2' }, false, ['deriveKey']),
|
|
12
|
+
// { name: 'AES-GCM', length: 128 }, true, ['encrypt', 'decrypt'])
|
|
13
|
+
// )
|
|
14
|
+
export const derivedEmptyPasswordKey = { alg: 'A128GCM', ext: true, k: 'scm9jmO_4BJAgdwWGVulLg', key_ops: ['encrypt', 'decrypt'], kty: 'oct' };
|
|
4
15
|
// Based off of code from https://github.com/luke-park/SecureCompatibleEncryptionExamples
|
|
5
16
|
export function create(opts) {
|
|
6
17
|
const algorithm = opts?.algorithm ?? 'AES-GCM';
|
|
@@ -22,10 +33,24 @@ export function create(opts) {
|
|
|
22
33
|
if (typeof password === 'string') {
|
|
23
34
|
password = fromString(password);
|
|
24
35
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
let cryptoKey;
|
|
37
|
+
if (password.length === 0) {
|
|
38
|
+
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['encrypt']);
|
|
39
|
+
try {
|
|
40
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } };
|
|
41
|
+
const runtimeDerivedEmptyPassword = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey']);
|
|
42
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, runtimeDerivedEmptyPassword, { name: algorithm, length: keyLength }, true, ['encrypt']);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['encrypt']);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// Derive a key using PBKDF2.
|
|
50
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } };
|
|
51
|
+
const rawKey = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey']);
|
|
52
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, rawKey, { name: algorithm, length: keyLength }, true, ['encrypt']);
|
|
53
|
+
}
|
|
29
54
|
// Encrypt the string.
|
|
30
55
|
const ciphertext = await crypto.subtle.encrypt(aesGcm, cryptoKey, data);
|
|
31
56
|
return concat([salt, aesGcm.iv, new Uint8Array(ciphertext)]);
|
|
@@ -44,10 +69,23 @@ export function create(opts) {
|
|
|
44
69
|
if (typeof password === 'string') {
|
|
45
70
|
password = fromString(password);
|
|
46
71
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
72
|
+
let cryptoKey;
|
|
73
|
+
if (password.length === 0) {
|
|
74
|
+
try {
|
|
75
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } };
|
|
76
|
+
const runtimeDerivedEmptyPassword = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey']);
|
|
77
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, runtimeDerivedEmptyPassword, { name: algorithm, length: keyLength }, true, ['decrypt']);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['decrypt']);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
// Derive the key using PBKDF2.
|
|
85
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } };
|
|
86
|
+
const rawKey = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey']);
|
|
87
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, rawKey, { name: algorithm, length: keyLength }, true, ['decrypt']);
|
|
88
|
+
}
|
|
51
89
|
// Decrypt the string.
|
|
52
90
|
const plaintext = await crypto.subtle.decrypt(aesGcm, cryptoKey, ciphertext);
|
|
53
91
|
return new Uint8Array(plaintext);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aes-gcm.browser.js","sourceRoot":"","sources":["../../../src/ciphers/aes-gcm.browser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,SAAS,MAAM,iBAAiB,CAAA;AAGvC,yFAAyF;AAEzF,MAAM,UAAU,MAAM,CAAE,IAAoB;IAC1C,MAAM,SAAS,GAAG,IAAI,EAAE,SAAS,IAAI,SAAS,CAAA;IAC9C,IAAI,SAAS,GAAG,IAAI,EAAE,SAAS,IAAI,EAAE,CAAA;IACrC,MAAM,WAAW,GAAG,IAAI,EAAE,WAAW,IAAI,EAAE,CAAA;IAC3C,MAAM,MAAM,GAAG,IAAI,EAAE,MAAM,IAAI,SAAS,CAAA;IACxC,MAAM,UAAU,GAAG,IAAI,EAAE,UAAU,IAAI,EAAE,CAAA;IACzC,MAAM,UAAU,GAAG,IAAI,EAAE,UAAU,IAAI,KAAK,CAAA;IAE5C,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,EAAE,CAAA;IAC9B,SAAS,IAAI,CAAC,CAAA,CAAC,4CAA4C;IAE3D;;;OAGG;IACH,KAAK,UAAU,OAAO,CAAE,IAAgB,EAAE,QAA6B;QACrE,MAAM,IAAI,GAAG,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,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,EAAE,IAAI,EAAE,EAAE,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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/crypto",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
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",
|
|
@@ -3,6 +3,18 @@ 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
|
+
// WebKit on Linux does not support deriving a key from an empty PBKDF2 key.
|
|
7
|
+
// So, as a workaround, we provide the generated key as a constant. We test that
|
|
8
|
+
// this generated key is accurate in test/workaround.spec.ts
|
|
9
|
+
// Generated via:
|
|
10
|
+
// await crypto.subtle.exportKey('jwk',
|
|
11
|
+
// await crypto.subtle.deriveKey(
|
|
12
|
+
// { name: 'PBKDF2', salt: new Uint8Array(16), iterations: 32767, hash: { name: 'SHA-256' } },
|
|
13
|
+
// await crypto.subtle.importKey('raw', new Uint8Array(0), { name: 'PBKDF2' }, false, ['deriveKey']),
|
|
14
|
+
// { name: 'AES-GCM', length: 128 }, true, ['encrypt', 'decrypt'])
|
|
15
|
+
// )
|
|
16
|
+
export const derivedEmptyPasswordKey = { alg: 'A128GCM', ext: true, k: 'scm9jmO_4BJAgdwWGVulLg', key_ops: ['encrypt', 'decrypt'], kty: 'oct' }
|
|
17
|
+
|
|
6
18
|
// Based off of code from https://github.com/luke-park/SecureCompatibleEncryptionExamples
|
|
7
19
|
|
|
8
20
|
export function create (opts?: CreateOptions): AESCipher {
|
|
@@ -29,10 +41,22 @@ export function create (opts?: CreateOptions): AESCipher {
|
|
|
29
41
|
password = fromString(password)
|
|
30
42
|
}
|
|
31
43
|
|
|
44
|
+
let cryptoKey: CryptoKey
|
|
45
|
+
if (password.length === 0) {
|
|
46
|
+
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['encrypt'])
|
|
47
|
+
try {
|
|
48
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } }
|
|
49
|
+
const runtimeDerivedEmptyPassword = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey'])
|
|
50
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, runtimeDerivedEmptyPassword, { name: algorithm, length: keyLength }, true, ['encrypt'])
|
|
51
|
+
} catch {
|
|
52
|
+
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['encrypt'])
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
32
55
|
// Derive a key using PBKDF2.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
56
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } }
|
|
57
|
+
const rawKey = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey'])
|
|
58
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, rawKey, { name: algorithm, length: keyLength }, true, ['encrypt'])
|
|
59
|
+
}
|
|
36
60
|
|
|
37
61
|
// Encrypt the string.
|
|
38
62
|
const ciphertext = await crypto.subtle.encrypt(aesGcm, cryptoKey, data)
|
|
@@ -55,10 +79,21 @@ export function create (opts?: CreateOptions): AESCipher {
|
|
|
55
79
|
password = fromString(password)
|
|
56
80
|
}
|
|
57
81
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
82
|
+
let cryptoKey: CryptoKey
|
|
83
|
+
if (password.length === 0) {
|
|
84
|
+
try {
|
|
85
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } }
|
|
86
|
+
const runtimeDerivedEmptyPassword = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey'])
|
|
87
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, runtimeDerivedEmptyPassword, { name: algorithm, length: keyLength }, true, ['decrypt'])
|
|
88
|
+
} catch {
|
|
89
|
+
cryptoKey = await crypto.subtle.importKey('jwk', derivedEmptyPasswordKey, { name: 'AES-GCM' }, true, ['decrypt'])
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
// Derive the key using PBKDF2.
|
|
93
|
+
const deriveParams = { name: 'PBKDF2', salt, iterations, hash: { name: digest } }
|
|
94
|
+
const rawKey = await crypto.subtle.importKey('raw', password, { name: 'PBKDF2' }, false, ['deriveKey'])
|
|
95
|
+
cryptoKey = await crypto.subtle.deriveKey(deriveParams, rawKey, { name: algorithm, length: keyLength }, true, ['decrypt'])
|
|
96
|
+
}
|
|
62
97
|
|
|
63
98
|
// Decrypt the string.
|
|
64
99
|
const plaintext = await crypto.subtle.decrypt(aesGcm, cryptoKey, ciphertext)
|