@substrate-system/keys 0.3.3 → 0.5.1
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/README.md +35 -3
- package/dist/_base.min.js +2 -2
- package/dist/_base.min.js.map +4 -4
- package/dist/aes/index.cjs +14 -7
- package/dist/aes/index.cjs.map +2 -2
- package/dist/aes/index.d.ts +11 -3
- package/dist/aes/index.d.ts.map +1 -1
- package/dist/aes/index.js +14 -7
- package/dist/aes/index.js.map +2 -2
- package/dist/aes/index.min.cjs +1 -1
- package/dist/aes/index.min.cjs.map +3 -3
- package/dist/aes/index.min.js +2 -2
- package/dist/aes/index.min.js.map +3 -3
- package/dist/browser/index.js +341 -116
- package/dist/browser/index.min.js +2 -2
- package/dist/constants.cjs +4 -0
- package/dist/constants.cjs.map +2 -2
- package/dist/constants.d.ts +6 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +4 -0
- package/dist/constants.js.map +2 -2
- package/dist/constants.min.cjs +1 -1
- package/dist/constants.min.cjs.map +3 -3
- package/dist/constants.min.js +1 -1
- package/dist/constants.min.js.map +3 -3
- package/dist/crypto.cjs +9 -7
- package/dist/crypto.cjs.map +2 -2
- package/dist/crypto.d.ts +6 -0
- package/dist/crypto.d.ts.map +1 -1
- package/dist/crypto.js +9 -8
- package/dist/crypto.js.map +2 -2
- package/dist/crypto.min.cjs +2 -2
- package/dist/crypto.min.cjs.map +2 -2
- package/dist/crypto.min.js +2 -2
- package/dist/crypto.min.js.map +4 -4
- package/dist/ecc/index.cjs +176 -60
- package/dist/ecc/index.cjs.map +2 -2
- package/dist/ecc/index.d.ts +47 -13
- package/dist/ecc/index.d.ts.map +1 -1
- package/dist/ecc/index.js +179 -61
- package/dist/ecc/index.js.map +2 -2
- package/dist/ecc/index.min.cjs +1 -1
- package/dist/ecc/index.min.cjs.map +3 -3
- package/dist/ecc/index.min.js +2 -2
- package/dist/ecc/index.min.js.map +4 -4
- package/dist/errors.min.js +1 -1
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +4 -4
- package/dist/meta.json +33 -26
- package/dist/rsa/index.cjs +15 -4
- package/dist/rsa/index.cjs.map +2 -2
- package/dist/rsa/index.d.ts +6 -1
- package/dist/rsa/index.d.ts.map +1 -1
- package/dist/rsa/index.js +15 -5
- package/dist/rsa/index.js.map +2 -2
- package/dist/rsa/index.min.cjs +1 -1
- package/dist/rsa/index.min.cjs.map +3 -3
- package/dist/rsa/index.min.js +2 -2
- package/dist/rsa/index.min.js.map +4 -4
- package/dist/types.cjs.map +2 -2
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +2 -2
- package/dist/types.min.cjs +1 -1
- package/dist/types.min.cjs.map +2 -2
- package/dist/types.min.js +1 -1
- package/dist/types.min.js.map +2 -2
- package/dist/util.cjs +6 -4
- package/dist/util.cjs.map +2 -2
- package/dist/util.d.ts +5 -1
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +6 -4
- package/dist/util.js.map +2 -2
- package/dist/util.min.cjs +1 -1
- package/dist/util.min.cjs.map +3 -3
- package/dist/util.min.js +1 -1
- package/dist/util.min.js.map +3 -3
- package/package.json +3 -3
package/dist/ecc/index.d.ts
CHANGED
|
@@ -70,28 +70,48 @@ export declare class EccKeys extends AbstractKeys {
|
|
|
70
70
|
* 4. AES-GCM encrypt the content
|
|
71
71
|
* 5. Return the ephemeral public key (encapsulation) + wrapped key
|
|
72
72
|
*
|
|
73
|
+
* The result is signed with our Ed25519 write key, so the recipient
|
|
74
|
+
* can verify who created the wrap. `unwrap` requires our DID and
|
|
75
|
+
* rejects the wrap unless the signature matches it.
|
|
76
|
+
*
|
|
73
77
|
* @param {SymmKey|Uint8Array|string} contentKey The existing AES key used
|
|
74
78
|
* to encrypt the content. Can be a base64 encoded string.
|
|
75
79
|
* @param {CryptoKey} newPublicKey The new device's public X25519 key.
|
|
76
80
|
* @param {string} [info] Optional info parameter for HKDF.
|
|
77
81
|
* Defaults to 'key-wrap'.
|
|
78
|
-
* @returns {Promise<WrappedKey>} The ephemeral public key (base64)
|
|
79
|
-
* the wrapped AES key (base64)
|
|
82
|
+
* @returns {Promise<WrappedKey>} The ephemeral public key (base64),
|
|
83
|
+
* the wrapped AES key (base64), and a signature (base64) by
|
|
84
|
+
* our write key.
|
|
80
85
|
*/
|
|
81
86
|
wrap(contentKey: SymmKey | Uint8Array | string, newPublicKey: CryptoKey, // the new device's public key
|
|
82
87
|
info?: string): Promise<WrappedKey>;
|
|
83
88
|
/**
|
|
84
|
-
* Unwrap a content key that
|
|
89
|
+
* Unwrap a content key that another device wrapped for this device
|
|
90
|
+
* with `wrap`.
|
|
85
91
|
*
|
|
86
92
|
* The recipient device uses its private key + the ephemeral public key
|
|
87
93
|
* to rederive the KEK and decrypt the wrapped content key.
|
|
88
94
|
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
95
|
+
* The wrap's signature is always checked against `signer` before
|
|
96
|
+
* anything is decrypted, and this throws if the signature is missing
|
|
97
|
+
* or invalid. `signer` must be the DID of the device you expect the
|
|
98
|
+
* key from, obtained over a channel an attacker cannot tamper with.
|
|
99
|
+
* A DID that travels alongside the wrapped key proves nothing, since
|
|
100
|
+
* an attacker who can swap the wrapped key can swap the DID too.
|
|
101
|
+
*
|
|
102
|
+
* @param wrapped The `{ enc, wrappedKey, signature }` object returned
|
|
103
|
+
* by `wrap`.
|
|
104
|
+
* @param signer The DID of the expected sender.
|
|
105
|
+
* @param opts.info The HKDF info string. Must match what was passed to
|
|
106
|
+
* `wrap`. Defaults to 'key-wrap'.
|
|
107
|
+
* @param opts.extractable Can the returned key be exported with
|
|
108
|
+
* `crypto.subtle.exportKey`? Default `true`.
|
|
92
109
|
* @returns {Promise<CryptoKey>} The unwrapped AES content key.
|
|
93
110
|
*/
|
|
94
|
-
unwrap(
|
|
111
|
+
unwrap(wrapped: WrappedKey, signer: DID, opts?: {
|
|
112
|
+
info?: string;
|
|
113
|
+
extractable?: boolean;
|
|
114
|
+
}): Promise<CryptoKey>;
|
|
95
115
|
/**
|
|
96
116
|
* Encrypt the given content to the given public key, or encrypt to
|
|
97
117
|
* our own public key if a key is not passed in.
|
|
@@ -105,8 +125,9 @@ export declare class EccKeys extends AbstractKeys {
|
|
|
105
125
|
* will use our own public key if not passed in. Can be a CryptoKey
|
|
106
126
|
* or a base64 encoded string.
|
|
107
127
|
* @param {string} [info] info tag for HKDF. Default is the class property.
|
|
108
|
-
* @param {SymmKey|Uint8Array|string} [aesKey]
|
|
109
|
-
*
|
|
128
|
+
* @param {SymmKey|Uint8Array|string} [aesKey] Use this exact AES key
|
|
129
|
+
* instead of deriving one from ECDH. Not relevant for most use
|
|
130
|
+
* cases. Pass the same key to `decrypt` to read it back.
|
|
110
131
|
* @param {SymmKeyLength} [keysize] Default is 256
|
|
111
132
|
* @returns {Promise<ArrayBuffer>} Buffer of ephemeral-public-key + salt +
|
|
112
133
|
* iv + cipher text
|
|
@@ -128,9 +149,12 @@ export declare class EccKeys extends AbstractKeys {
|
|
|
128
149
|
* @param msg The encrypted content
|
|
129
150
|
* @param {string} aesAlgorithm The algorithm. Default is AES-GCM.
|
|
130
151
|
* @param {string} info Custom "info" parameter
|
|
152
|
+
* @param {SymmKey|Uint8Array|string} [aesKey] Use this exact AES key
|
|
153
|
+
* instead of deriving one from ECDH. Must match the `aesKey`
|
|
154
|
+
* passed to the corresponding `encrypt` call.
|
|
131
155
|
* @returns {Promise<ArrayBuffer>} The decrypted content.
|
|
132
156
|
*/
|
|
133
|
-
decrypt(msg: string | Uint8Array | ArrayBuffer, aesAlgorithm?: string | null, info?: string | null): Promise<ArrayBuffer | Uint8Array>;
|
|
157
|
+
decrypt(msg: string | Uint8Array | ArrayBuffer, aesAlgorithm?: string | null, info?: string | null, aesKey?: SymmKey | Uint8Array | string | null): Promise<ArrayBuffer | Uint8Array>;
|
|
134
158
|
/**
|
|
135
159
|
* Decrypt and return as string using ECIES. The encrypted message should be
|
|
136
160
|
* ephemeral public key + salt + iv + cipher text.
|
|
@@ -143,17 +167,27 @@ export declare class EccKeys extends AbstractKeys {
|
|
|
143
167
|
* @param {string} info Custom "info" parameter
|
|
144
168
|
* @returns {Promise<string>} The decrypted content as a string.
|
|
145
169
|
*/
|
|
146
|
-
decryptAsString(msg: string | Uint8Array | ArrayBuffer, aesAlgorithm?: string | null, info?: string | null): Promise<string>;
|
|
170
|
+
decryptAsString(msg: string | Uint8Array | ArrayBuffer, aesAlgorithm?: string | null, info?: string | null, aesKey?: SymmKey | Uint8Array | string | null): Promise<string>;
|
|
147
171
|
/**
|
|
148
172
|
* Do DHKE, create a new AES-GCM key.
|
|
149
173
|
*
|
|
174
|
+
* The HKDF salt is either supplied by the caller (so both sides can
|
|
175
|
+
* re-derive the exact same key later) or generated at random, in
|
|
176
|
+
* which case it is returned as a `.salt` (base64) property on the
|
|
177
|
+
* key so the caller can persist it for later re-derivation.
|
|
178
|
+
*
|
|
150
179
|
* @param {CryptoKey|string|null} [publicKey] Public key to use in DHKE.
|
|
151
180
|
* Will use our public key if it is not passed in.
|
|
152
181
|
* @param {string} info The info parameter for DHKE. Will use the class
|
|
153
182
|
* property `INFO` if it is not passed in.
|
|
154
|
-
* @
|
|
183
|
+
* @param {Uint8Array|ArrayBuffer|string|null} [salt] Reuse this salt
|
|
184
|
+
* instead of generating a random one -- pass the same salt on
|
|
185
|
+
* both sides to re-derive the same key.
|
|
186
|
+
* @returns {CryptoKey} New AES key, with a `.salt` (base64) property
|
|
155
187
|
*/
|
|
156
|
-
getAesKey(publicKey?: CryptoKey | string | null, info?: string | null): Promise<CryptoKey
|
|
188
|
+
getAesKey(publicKey?: CryptoKey | string | null, info?: string | null, salt?: Uint8Array | ArrayBuffer | string | null): Promise<CryptoKey & {
|
|
189
|
+
salt: string;
|
|
190
|
+
}>;
|
|
157
191
|
/**
|
|
158
192
|
* Sign the given content using our private write key.
|
|
159
193
|
*/
|
package/dist/ecc/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ecc/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,kBAAkB,EAAwB,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ecc/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,kBAAkB,EAAwB,MAAM,aAAa,CAAA;AAc3E,OAAO,EACH,QAAQ,EACR,MAAM,EACN,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,OAAO,EACZ,KAAK,GAAG,EACR,KAAK,QAAQ,EACb,KAAK,GAAG,EACR,KAAK,UAAU,EAClB,MAAM,aAAa,CAAA;AAQpB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAU1C;;GAEG;AACH,qBAAa,OAAQ,SAAQ,YAAY;IACrC,MAAM,CAAC,IAAI,EAAC,KAAK,GAAC,KAAK,CAAiB;IACxC,MAAM,CAAC,iBAAiB,EAAC,MAAM,CAAuB;IACtD,MAAM,CAAC,cAAc,EAAC,MAAM,CAAoB;IAChD,MAAM,CAAC,IAAI,SAAS;IAEpB;;;;;;;;;OASG;WACU,MAAM,CAAC,CAAC,SAAS,YAAY,GAAG,OAAO,EAChD,OAAO,CAAC,EAAC,OAAO,EAChB,WAAW,CAAC,EAAC,OAAO,EACpB,IAAI,CAAC,EAAC;QACF,KAAK,CAAC,EAAC,MAAM,CAAC;QACd,YAAY,CAAC,EAAC,aAAa,GAAC,IAAI,CAAC;QACjC,SAAS,CAAC,EAAC,aAAa,GAAC,IAAI,CAAC;KACjC,GACH,OAAO,CAAC,CAAC,CAAC;IAIZ,IAAI,iBAAiB;4BAGa,kBAAkB,KAAG,OAAO,CAAC,MAAM,CAAC;MAMrE;IAED,IAAI,cAAc;4BAGgB,kBAAkB,KAAG,OAAO,CAAC,MAAM,CAAC;MAMrE;WAEY,mBAAmB,CAC5B,WAAW,GAAC,O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AI,EAAC,MAAM,CAAA;KAAE,CAAC;IAmCtC;;OAEG;IACG,IAAI,CAAE,GAAG,EAAC,GAAG,EAAE,SAAS,CAAC,EAAC,QAAQ,GAAE,OAAO,CAAC,UAAU,CAAC;IAqB7D;;OAEG;IACG,YAAY,CAAE,GAAG,EAAC,MAAM,EAAE,SAAS,CAAC,EAAC,QAAQ,GAAE,OAAO,CAAC,MAAM,CAAC;CAIvE;AAcD;;;;;GAKG;AACH,wBAAsB,eAAe,CACjC,OAAO,EAAC,aAAa,EACrB,MAAM,EAAC,KAAK,GACd,OAAO,CAAC,UAAU,CAAC,CAAA;AAErB,wBAAsB,eAAe,CACjC,OAAO,EAAC,aAAa,EACrB,MAAM,CAAC,EAAC,KAAK,GAAC,MAAM,GAAC,OAAO,GAC9B,OAAO,CAAC,WAAW,CAAC,CAAA;AAStB,wBAAsB,eAAe,CACjC,SAAS,EAAC,MAAM,EAChB,KAAK,EAAC,QAAQ,EACd,GAAG,EAAC,MAAM,GACZ,OAAO,CAAC,SAAS,CAAC,CAkBnB;;;;;AAED,wBAGC;AAoCD;;;GAGG;AACH,wBAAsB,MAAM,CACxB,GAAG,EAAC,MAAM,GAAC,UAAU,EACrB,GAAG,EAAC,MAAM,GAAC,UAAU,EACrB,UAAU,EAAC,GAAG,GAChB,OAAO,CAAC,OAAO,CAAC,CAsDjB"}
|
package/dist/ecc/index.js
CHANGED
|
@@ -12,7 +12,9 @@ import {
|
|
|
12
12
|
DEFAULT_SYMM_LENGTH,
|
|
13
13
|
ECC_WRITE_ALGORITHM,
|
|
14
14
|
ECC_EXCHANGE_ALGORITHM,
|
|
15
|
-
X25519_PUBLIC_KEY_LENGTH
|
|
15
|
+
X25519_PUBLIC_KEY_LENGTH,
|
|
16
|
+
WRAP_FORMAT_VERSION,
|
|
17
|
+
AES_GCM_TAG_LENGTH
|
|
16
18
|
} from "../constants.js";
|
|
17
19
|
import {
|
|
18
20
|
EccCurve,
|
|
@@ -26,6 +28,7 @@ import {
|
|
|
26
28
|
} from "../util.js";
|
|
27
29
|
import { checkValidKeyUse } from "../errors.js";
|
|
28
30
|
import { AbstractKeys } from "../_base.js";
|
|
31
|
+
import { AES } from "../aes/index.js";
|
|
29
32
|
const webcrypto = globalThis.crypto;
|
|
30
33
|
function toArrayBuffer(data) {
|
|
31
34
|
return new Uint8Array(data).buffer;
|
|
@@ -124,89 +127,167 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
|
|
|
124
127
|
* 4. AES-GCM encrypt the content
|
|
125
128
|
* 5. Return the ephemeral public key (encapsulation) + wrapped key
|
|
126
129
|
*
|
|
130
|
+
* The result is signed with our Ed25519 write key, so the recipient
|
|
131
|
+
* can verify who created the wrap. `unwrap` requires our DID and
|
|
132
|
+
* rejects the wrap unless the signature matches it.
|
|
133
|
+
*
|
|
127
134
|
* @param {SymmKey|Uint8Array|string} contentKey The existing AES key used
|
|
128
135
|
* to encrypt the content. Can be a base64 encoded string.
|
|
129
136
|
* @param {CryptoKey} newPublicKey The new device's public X25519 key.
|
|
130
137
|
* @param {string} [info] Optional info parameter for HKDF.
|
|
131
138
|
* Defaults to 'key-wrap'.
|
|
132
|
-
* @returns {Promise<WrappedKey>} The ephemeral public key (base64)
|
|
133
|
-
* the wrapped AES key (base64)
|
|
139
|
+
* @returns {Promise<WrappedKey>} The ephemeral public key (base64),
|
|
140
|
+
* the wrapped AES key (base64), and a signature (base64) by
|
|
141
|
+
* our write key.
|
|
134
142
|
*/
|
|
135
143
|
async wrap(contentKey, newPublicKey, info) {
|
|
136
|
-
const ephemeralKeypair = await _EccKeys._createExchangeKeys(
|
|
137
|
-
const
|
|
144
|
+
const ephemeralKeypair = await _EccKeys._createExchangeKeys(false);
|
|
145
|
+
const ephemeralPubKeyBuffer = await exportPublicKey(ephemeralKeypair);
|
|
146
|
+
const ephemeralPubKeyBytes = new Uint8Array(ephemeralPubKeyBuffer);
|
|
147
|
+
const recipientPubKey = new Uint8Array(
|
|
148
|
+
await webcrypto.subtle.exportKey("raw", newPublicKey)
|
|
149
|
+
);
|
|
150
|
+
const baseInfo = info || "key-wrap";
|
|
151
|
+
const salt = webcrypto.getRandomValues(new Uint8Array(SALT_LENGTH));
|
|
138
152
|
const kek = await deriveKey(
|
|
139
153
|
ephemeralKeypair.privateKey,
|
|
140
154
|
newPublicKey,
|
|
141
155
|
salt,
|
|
142
|
-
|
|
156
|
+
wrapKekInfo(ephemeralPubKeyBytes, recipientPubKey, baseInfo)
|
|
143
157
|
);
|
|
144
158
|
let contentKeyBytes;
|
|
159
|
+
let ownsContentKeyBytes = false;
|
|
145
160
|
if (contentKey instanceof CryptoKey) {
|
|
146
|
-
const exported = await
|
|
161
|
+
const exported = await webcrypto.subtle.exportKey("raw", contentKey);
|
|
147
162
|
contentKeyBytes = new Uint8Array(exported);
|
|
163
|
+
ownsContentKeyBytes = true;
|
|
148
164
|
} else if (typeof contentKey === "string") {
|
|
149
165
|
contentKeyBytes = fromString(contentKey, "base64pad");
|
|
166
|
+
ownsContentKeyBytes = true;
|
|
150
167
|
} else {
|
|
151
168
|
contentKeyBytes = contentKey;
|
|
152
169
|
}
|
|
153
|
-
const iv =
|
|
154
|
-
const wrappedKeyBuffer = await
|
|
170
|
+
const iv = webcrypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
171
|
+
const wrappedKeyBuffer = await webcrypto.subtle.encrypt(
|
|
155
172
|
{ name: DEFAULT_SYMM_ALGORITHM, iv },
|
|
156
173
|
kek,
|
|
157
174
|
toArrayBuffer(contentKeyBytes)
|
|
158
175
|
);
|
|
159
|
-
|
|
160
|
-
const saltBuf = new Uint8Array(salt);
|
|
176
|
+
if (ownsContentKeyBytes) contentKeyBytes.fill(0);
|
|
161
177
|
const ivBuf = new Uint8Array(iv);
|
|
162
178
|
const wrappedBuf = new Uint8Array(wrappedKeyBuffer);
|
|
163
|
-
const combinedSize =
|
|
179
|
+
const combinedSize = 1 + SALT_LENGTH + ivBuf.length + wrappedBuf.length;
|
|
164
180
|
const combined = new Uint8Array(combinedSize);
|
|
165
|
-
combined
|
|
166
|
-
combined.set(
|
|
167
|
-
combined.set(
|
|
181
|
+
combined[0] = WRAP_FORMAT_VERSION;
|
|
182
|
+
combined.set(salt, 1);
|
|
183
|
+
combined.set(ivBuf, 1 + SALT_LENGTH);
|
|
184
|
+
combined.set(wrappedBuf, 1 + SALT_LENGTH + ivBuf.length);
|
|
185
|
+
const signData = wrapSignData(
|
|
186
|
+
ephemeralPubKeyBytes,
|
|
187
|
+
recipientPubKey,
|
|
188
|
+
combined,
|
|
189
|
+
baseInfo
|
|
190
|
+
);
|
|
191
|
+
const signature = await this.sign(signData);
|
|
168
192
|
return {
|
|
169
|
-
enc: toBase64(
|
|
170
|
-
wrappedKey: toBase64(combined)
|
|
193
|
+
enc: toBase64(ephemeralPubKeyBytes),
|
|
194
|
+
wrappedKey: toBase64(combined),
|
|
195
|
+
signature: toString(signature, "base64pad")
|
|
171
196
|
};
|
|
172
197
|
}
|
|
173
198
|
/**
|
|
174
|
-
* Unwrap a content key that
|
|
199
|
+
* Unwrap a content key that another device wrapped for this device
|
|
200
|
+
* with `wrap`.
|
|
175
201
|
*
|
|
176
202
|
* The recipient device uses its private key + the ephemeral public key
|
|
177
203
|
* to rederive the KEK and decrypt the wrapped content key.
|
|
178
204
|
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
205
|
+
* The wrap's signature is always checked against `signer` before
|
|
206
|
+
* anything is decrypted, and this throws if the signature is missing
|
|
207
|
+
* or invalid. `signer` must be the DID of the device you expect the
|
|
208
|
+
* key from, obtained over a channel an attacker cannot tamper with.
|
|
209
|
+
* A DID that travels alongside the wrapped key proves nothing, since
|
|
210
|
+
* an attacker who can swap the wrapped key can swap the DID too.
|
|
211
|
+
*
|
|
212
|
+
* @param wrapped The `{ enc, wrappedKey, signature }` object returned
|
|
213
|
+
* by `wrap`.
|
|
214
|
+
* @param signer The DID of the expected sender.
|
|
215
|
+
* @param opts.info The HKDF info string. Must match what was passed to
|
|
216
|
+
* `wrap`. Defaults to 'key-wrap'.
|
|
217
|
+
* @param opts.extractable Can the returned key be exported with
|
|
218
|
+
* `crypto.subtle.exportKey`? Default `true`.
|
|
182
219
|
* @returns {Promise<CryptoKey>} The unwrapped AES content key.
|
|
183
220
|
*/
|
|
184
|
-
async unwrap(
|
|
221
|
+
async unwrap(wrapped, signer, opts = {}) {
|
|
222
|
+
const { enc, wrappedKey, signature } = wrapped;
|
|
223
|
+
const info = opts.info;
|
|
224
|
+
const extractable = opts.extractable ?? true;
|
|
225
|
+
if (!signer) {
|
|
226
|
+
throw new Error("`unwrap` requires the DID of the sender");
|
|
227
|
+
}
|
|
228
|
+
if (!signature) {
|
|
229
|
+
throw new Error("The wrapped key is not signed");
|
|
230
|
+
}
|
|
231
|
+
const encBytes = fromString(enc, "base64pad");
|
|
232
|
+
if (encBytes.length !== X25519_PUBLIC_KEY_LENGTH) {
|
|
233
|
+
throw new Error(
|
|
234
|
+
`\`enc\` must be a ${X25519_PUBLIC_KEY_LENGTH}-byte X25519 public key, got ${encBytes.length} bytes`
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
const wrappedBuf = new Uint8Array(base64ToArrBuf(wrappedKey));
|
|
238
|
+
const baseInfo = info || "key-wrap";
|
|
239
|
+
const minWrappedLength = 1 + SALT_LENGTH + IV_LENGTH + AES_GCM_TAG_LENGTH;
|
|
240
|
+
if (wrappedBuf.length < minWrappedLength) {
|
|
241
|
+
throw new Error(
|
|
242
|
+
`Wrapped key must be at least ${minWrappedLength} bytes, got ${wrappedBuf.length} bytes`
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
const version = wrappedBuf[0];
|
|
246
|
+
if (version !== WRAP_FORMAT_VERSION) {
|
|
247
|
+
throw new Error(
|
|
248
|
+
`Unsupported wrapped key format version: ${version}`
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
const ourPubKeyBytes = new Uint8Array(
|
|
252
|
+
await exportPublicKey(this.exchangeKey)
|
|
253
|
+
);
|
|
254
|
+
const signData = wrapSignData(
|
|
255
|
+
encBytes,
|
|
256
|
+
ourPubKeyBytes,
|
|
257
|
+
wrappedBuf,
|
|
258
|
+
baseInfo
|
|
259
|
+
);
|
|
260
|
+
const isValid = await verify(signData, signature, signer);
|
|
261
|
+
if (!isValid) {
|
|
262
|
+
throw new Error("Invalid signature for the wrapped key");
|
|
263
|
+
}
|
|
185
264
|
const ephemeralPubKey = await importPublicKey(
|
|
186
265
|
enc,
|
|
187
266
|
EccCurve.X25519,
|
|
188
267
|
KeyUse.Exchange
|
|
189
268
|
);
|
|
190
|
-
const
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
|
|
269
|
+
const salt = wrappedBuf.slice(1, 1 + SALT_LENGTH);
|
|
270
|
+
const iv = wrappedBuf.slice(
|
|
271
|
+
1 + SALT_LENGTH,
|
|
272
|
+
1 + SALT_LENGTH + IV_LENGTH
|
|
273
|
+
);
|
|
274
|
+
const ciphertext = wrappedBuf.slice(1 + SALT_LENGTH + IV_LENGTH);
|
|
194
275
|
const kek = await deriveKey(
|
|
195
276
|
this.exchangeKey.privateKey,
|
|
196
277
|
ephemeralPubKey,
|
|
197
278
|
salt,
|
|
198
|
-
|
|
279
|
+
wrapKekInfo(encBytes, ourPubKeyBytes, baseInfo)
|
|
199
280
|
);
|
|
200
|
-
const contentKeyBytes = await
|
|
281
|
+
const contentKeyBytes = await webcrypto.subtle.decrypt(
|
|
201
282
|
{ name: DEFAULT_SYMM_ALGORITHM, iv },
|
|
202
283
|
kek,
|
|
203
284
|
toArrayBuffer(ciphertext)
|
|
204
285
|
);
|
|
205
|
-
const contentKey = await
|
|
286
|
+
const contentKey = await webcrypto.subtle.importKey(
|
|
206
287
|
"raw",
|
|
207
288
|
contentKeyBytes,
|
|
208
289
|
{ name: DEFAULT_SYMM_ALGORITHM },
|
|
209
|
-
|
|
290
|
+
extractable,
|
|
210
291
|
["encrypt", "decrypt"]
|
|
211
292
|
);
|
|
212
293
|
return contentKey;
|
|
@@ -224,14 +305,15 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
|
|
|
224
305
|
* will use our own public key if not passed in. Can be a CryptoKey
|
|
225
306
|
* or a base64 encoded string.
|
|
226
307
|
* @param {string} [info] info tag for HKDF. Default is the class property.
|
|
227
|
-
* @param {SymmKey|Uint8Array|string} [aesKey]
|
|
228
|
-
*
|
|
308
|
+
* @param {SymmKey|Uint8Array|string} [aesKey] Use this exact AES key
|
|
309
|
+
* instead of deriving one from ECDH. Not relevant for most use
|
|
310
|
+
* cases. Pass the same key to `decrypt` to read it back.
|
|
229
311
|
* @param {SymmKeyLength} [keysize] Default is 256
|
|
230
312
|
* @returns {Promise<ArrayBuffer>} Buffer of ephemeral-public-key + salt +
|
|
231
313
|
* iv + cipher text
|
|
232
314
|
*/
|
|
233
315
|
async encrypt(content, recipient, info, aesKey, keysize) {
|
|
234
|
-
const salt =
|
|
316
|
+
const salt = webcrypto.getRandomValues(new Uint8Array(SALT_LENGTH));
|
|
235
317
|
const _publicKey = recipient || this.exchangeKey.publicKey;
|
|
236
318
|
let publicKey;
|
|
237
319
|
if (typeof _publicKey === "string") {
|
|
@@ -243,8 +325,8 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
|
|
|
243
325
|
} else {
|
|
244
326
|
publicKey = _publicKey;
|
|
245
327
|
}
|
|
246
|
-
const ephemeralKeypair = await _EccKeys._createExchangeKeys(
|
|
247
|
-
|
|
328
|
+
const ephemeralKeypair = await _EccKeys._createExchangeKeys(false);
|
|
329
|
+
const key = aesKey ? aesKey instanceof CryptoKey ? aesKey : await AES.import(aesKey) : await deriveKey(
|
|
248
330
|
ephemeralKeypair.privateKey,
|
|
249
331
|
// use ephemeral private key
|
|
250
332
|
publicKey,
|
|
@@ -252,18 +334,9 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
|
|
|
252
334
|
info || _EccKeys.INFO,
|
|
253
335
|
keysize
|
|
254
336
|
);
|
|
255
|
-
|
|
256
|
-
key = await deriveKey(
|
|
257
|
-
ephemeralKeypair.privateKey,
|
|
258
|
-
// use ephemeral private key
|
|
259
|
-
publicKey,
|
|
260
|
-
salt,
|
|
261
|
-
info || _EccKeys.INFO
|
|
262
|
-
);
|
|
263
|
-
}
|
|
264
|
-
const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
337
|
+
const iv = webcrypto.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
265
338
|
const plaintext = typeof content === "string" ? new TextEncoder().encode(content) : content;
|
|
266
|
-
const ciphertext = await
|
|
339
|
+
const ciphertext = await webcrypto.subtle.encrypt(
|
|
267
340
|
{ name: DEFAULT_SYMM_ALGORITHM, iv },
|
|
268
341
|
key,
|
|
269
342
|
toArrayBuffer(plaintext)
|
|
@@ -305,9 +378,12 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
|
|
|
305
378
|
* @param msg The encrypted content
|
|
306
379
|
* @param {string} aesAlgorithm The algorithm. Default is AES-GCM.
|
|
307
380
|
* @param {string} info Custom "info" parameter
|
|
381
|
+
* @param {SymmKey|Uint8Array|string} [aesKey] Use this exact AES key
|
|
382
|
+
* instead of deriving one from ECDH. Must match the `aesKey`
|
|
383
|
+
* passed to the corresponding `encrypt` call.
|
|
308
384
|
* @returns {Promise<ArrayBuffer>} The decrypted content.
|
|
309
385
|
*/
|
|
310
|
-
async decrypt(msg, aesAlgorithm, info) {
|
|
386
|
+
async decrypt(msg, aesAlgorithm, info, aesKey) {
|
|
311
387
|
const encrypted = normalizeToBuf(msg, base64ToArrBuf);
|
|
312
388
|
const ephemeralPubKeyBuf = encrypted.slice(0, X25519_PUBLIC_KEY_LENGTH);
|
|
313
389
|
const ephemeralPublicKey = await webcrypto.subtle.importKey(
|
|
@@ -329,13 +405,13 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
|
|
|
329
405
|
X25519_PUBLIC_KEY_LENGTH + SALT_LENGTH + IV_LENGTH
|
|
330
406
|
);
|
|
331
407
|
const { privateKey } = this.exchangeKey;
|
|
332
|
-
const key = await deriveKey(
|
|
408
|
+
const key = aesKey ? aesKey instanceof CryptoKey ? aesKey : await AES.import(aesKey) : await deriveKey(
|
|
333
409
|
privateKey,
|
|
334
410
|
ephemeralPublicKey,
|
|
335
411
|
salt,
|
|
336
412
|
info || _EccKeys.INFO
|
|
337
413
|
);
|
|
338
|
-
const decrypted = await
|
|
414
|
+
const decrypted = await webcrypto.subtle.decrypt(
|
|
339
415
|
{
|
|
340
416
|
name: aesAlgorithm || DEFAULT_SYMM_ALGORITHM,
|
|
341
417
|
iv
|
|
@@ -357,20 +433,28 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
|
|
|
357
433
|
* @param {string} info Custom "info" parameter
|
|
358
434
|
* @returns {Promise<string>} The decrypted content as a string.
|
|
359
435
|
*/
|
|
360
|
-
async decryptAsString(msg, aesAlgorithm, info) {
|
|
361
|
-
const dec = await this.decrypt(msg, aesAlgorithm, info);
|
|
436
|
+
async decryptAsString(msg, aesAlgorithm, info, aesKey) {
|
|
437
|
+
const dec = await this.decrypt(msg, aesAlgorithm, info, aesKey);
|
|
362
438
|
return toString(new Uint8Array(dec));
|
|
363
439
|
}
|
|
364
440
|
/**
|
|
365
441
|
* Do DHKE, create a new AES-GCM key.
|
|
366
442
|
*
|
|
443
|
+
* The HKDF salt is either supplied by the caller (so both sides can
|
|
444
|
+
* re-derive the exact same key later) or generated at random, in
|
|
445
|
+
* which case it is returned as a `.salt` (base64) property on the
|
|
446
|
+
* key so the caller can persist it for later re-derivation.
|
|
447
|
+
*
|
|
367
448
|
* @param {CryptoKey|string|null} [publicKey] Public key to use in DHKE.
|
|
368
449
|
* Will use our public key if it is not passed in.
|
|
369
450
|
* @param {string} info The info parameter for DHKE. Will use the class
|
|
370
451
|
* property `INFO` if it is not passed in.
|
|
371
|
-
* @
|
|
452
|
+
* @param {Uint8Array|ArrayBuffer|string|null} [salt] Reuse this salt
|
|
453
|
+
* instead of generating a random one -- pass the same salt on
|
|
454
|
+
* both sides to re-derive the same key.
|
|
455
|
+
* @returns {CryptoKey} New AES key, with a `.salt` (base64) property
|
|
372
456
|
*/
|
|
373
|
-
async getAesKey(publicKey, info) {
|
|
457
|
+
async getAesKey(publicKey, info, salt) {
|
|
374
458
|
if (!publicKey) {
|
|
375
459
|
publicKey = this.publicExchangeKey;
|
|
376
460
|
}
|
|
@@ -384,14 +468,16 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
|
|
|
384
468
|
} else {
|
|
385
469
|
pub = publicKey;
|
|
386
470
|
}
|
|
387
|
-
const
|
|
471
|
+
const saltBytes = salt ? new Uint8Array(
|
|
472
|
+
typeof salt === "string" ? base64ToArrBuf(salt) : salt
|
|
473
|
+
) : webcrypto.getRandomValues(new Uint8Array(SALT_LENGTH));
|
|
388
474
|
const aesKey = await deriveKey(
|
|
389
475
|
this.exchangeKey.privateKey,
|
|
390
476
|
pub,
|
|
391
|
-
|
|
477
|
+
saltBytes,
|
|
392
478
|
info || _EccKeys.INFO
|
|
393
479
|
);
|
|
394
|
-
return aesKey;
|
|
480
|
+
return Object.assign(aesKey, { salt: toBase64(saltBytes) });
|
|
395
481
|
}
|
|
396
482
|
/**
|
|
397
483
|
* Sign the given content using our private write key.
|
|
@@ -434,7 +520,7 @@ Object.assign(EccKeys.prototype.sign, {
|
|
|
434
520
|
}, "asString")
|
|
435
521
|
});
|
|
436
522
|
async function exportPublicKey(keypair, format = "raw") {
|
|
437
|
-
return await
|
|
523
|
+
return await webcrypto.subtle.exportKey(format, keypair.publicKey);
|
|
438
524
|
}
|
|
439
525
|
__name(exportPublicKey, "exportPublicKey");
|
|
440
526
|
async function importPublicKey(base64Key, curve, use) {
|
|
@@ -512,26 +598,58 @@ async function verify(msg, sig, signingDid) {
|
|
|
512
598
|
}
|
|
513
599
|
}
|
|
514
600
|
__name(verify, "verify");
|
|
601
|
+
function wrapKekInfo(ephemeralPubKey, recipientPubKey, info) {
|
|
602
|
+
const infoBytes = new TextEncoder().encode(info);
|
|
603
|
+
const data = new Uint8Array(
|
|
604
|
+
ephemeralPubKey.length + recipientPubKey.length + infoBytes.length
|
|
605
|
+
);
|
|
606
|
+
data.set(ephemeralPubKey, 0);
|
|
607
|
+
data.set(recipientPubKey, ephemeralPubKey.length);
|
|
608
|
+
data.set(infoBytes, ephemeralPubKey.length + recipientPubKey.length);
|
|
609
|
+
return data;
|
|
610
|
+
}
|
|
611
|
+
__name(wrapKekInfo, "wrapKekInfo");
|
|
612
|
+
function wrapSignData(ephemeralPubKey, recipientPubKey, wrappedBlob, info) {
|
|
613
|
+
const infoBytes = new TextEncoder().encode(info);
|
|
614
|
+
const blobLength = new Uint8Array(4);
|
|
615
|
+
new DataView(blobLength.buffer).setUint32(0, wrappedBlob.length);
|
|
616
|
+
const data = new Uint8Array(
|
|
617
|
+
ephemeralPubKey.length + recipientPubKey.length + blobLength.length + wrappedBlob.length + infoBytes.length
|
|
618
|
+
);
|
|
619
|
+
let offset = 0;
|
|
620
|
+
data.set(ephemeralPubKey, offset);
|
|
621
|
+
offset += ephemeralPubKey.length;
|
|
622
|
+
data.set(recipientPubKey, offset);
|
|
623
|
+
offset += recipientPubKey.length;
|
|
624
|
+
data.set(blobLength, offset);
|
|
625
|
+
offset += blobLength.length;
|
|
626
|
+
data.set(wrappedBlob, offset);
|
|
627
|
+
offset += wrappedBlob.length;
|
|
628
|
+
data.set(infoBytes, offset);
|
|
629
|
+
return data;
|
|
630
|
+
}
|
|
631
|
+
__name(wrapSignData, "wrapSignData");
|
|
515
632
|
async function deriveKey(privateKey, publicKey, salt, info, keysize) {
|
|
516
633
|
const encoder = new TextEncoder();
|
|
517
|
-
const
|
|
634
|
+
const infoBytes = typeof info === "string" ? encoder.encode(info) : info;
|
|
635
|
+
const sharedSecret = await webcrypto.subtle.deriveBits(
|
|
518
636
|
{ name: "X25519", public: publicKey },
|
|
519
637
|
privateKey,
|
|
520
638
|
256
|
|
521
639
|
);
|
|
522
|
-
const hkdfBaseKey = await
|
|
640
|
+
const hkdfBaseKey = await webcrypto.subtle.importKey(
|
|
523
641
|
"raw",
|
|
524
642
|
sharedSecret,
|
|
525
643
|
"HKDF",
|
|
526
644
|
false,
|
|
527
645
|
["deriveKey"]
|
|
528
646
|
);
|
|
529
|
-
return
|
|
647
|
+
return webcrypto.subtle.deriveKey(
|
|
530
648
|
{
|
|
531
649
|
name: "HKDF",
|
|
532
650
|
hash: "SHA-256",
|
|
533
651
|
salt: salt instanceof Uint8Array ? toArrayBuffer(salt) : salt,
|
|
534
|
-
info:
|
|
652
|
+
info: toArrayBuffer(infoBytes)
|
|
535
653
|
},
|
|
536
654
|
hkdfBaseKey,
|
|
537
655
|
{ name: "AES-GCM", length: keysize || DEFAULT_SYMM_LENGTH },
|