@substrate-system/keys 0.3.3 → 0.5.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 (78) hide show
  1. package/README.md +35 -3
  2. package/dist/_base.min.js +2 -2
  3. package/dist/_base.min.js.map +3 -3
  4. package/dist/aes/index.cjs +14 -7
  5. package/dist/aes/index.cjs.map +2 -2
  6. package/dist/aes/index.d.ts +11 -3
  7. package/dist/aes/index.d.ts.map +1 -1
  8. package/dist/aes/index.js +14 -7
  9. package/dist/aes/index.js.map +2 -2
  10. package/dist/aes/index.min.cjs +1 -1
  11. package/dist/aes/index.min.cjs.map +3 -3
  12. package/dist/aes/index.min.js +2 -2
  13. package/dist/aes/index.min.js.map +3 -3
  14. package/dist/browser/index.js +223 -82
  15. package/dist/browser/index.min.js +2 -2
  16. package/dist/constants.cjs +4 -0
  17. package/dist/constants.cjs.map +2 -2
  18. package/dist/constants.d.ts +6 -0
  19. package/dist/constants.d.ts.map +1 -1
  20. package/dist/constants.js +4 -0
  21. package/dist/constants.js.map +2 -2
  22. package/dist/constants.min.cjs +1 -1
  23. package/dist/constants.min.cjs.map +3 -3
  24. package/dist/constants.min.js +1 -1
  25. package/dist/constants.min.js.map +3 -3
  26. package/dist/crypto.cjs +9 -7
  27. package/dist/crypto.cjs.map +2 -2
  28. package/dist/crypto.d.ts +6 -0
  29. package/dist/crypto.d.ts.map +1 -1
  30. package/dist/crypto.js +9 -8
  31. package/dist/crypto.js.map +2 -2
  32. package/dist/crypto.min.cjs +2 -2
  33. package/dist/crypto.min.cjs.map +2 -2
  34. package/dist/crypto.min.js +2 -2
  35. package/dist/crypto.min.js.map +3 -3
  36. package/dist/ecc/index.cjs +176 -60
  37. package/dist/ecc/index.cjs.map +2 -2
  38. package/dist/ecc/index.d.ts +47 -13
  39. package/dist/ecc/index.d.ts.map +1 -1
  40. package/dist/ecc/index.js +179 -61
  41. package/dist/ecc/index.js.map +2 -2
  42. package/dist/ecc/index.min.cjs +1 -1
  43. package/dist/ecc/index.min.cjs.map +3 -3
  44. package/dist/ecc/index.min.js +2 -2
  45. package/dist/ecc/index.min.js.map +3 -3
  46. package/dist/errors.min.js +1 -1
  47. package/dist/index.min.js +2 -2
  48. package/dist/index.min.js.map +3 -3
  49. package/dist/meta.json +33 -26
  50. package/dist/rsa/index.cjs +15 -4
  51. package/dist/rsa/index.cjs.map +2 -2
  52. package/dist/rsa/index.d.ts +6 -1
  53. package/dist/rsa/index.d.ts.map +1 -1
  54. package/dist/rsa/index.js +15 -5
  55. package/dist/rsa/index.js.map +2 -2
  56. package/dist/rsa/index.min.cjs +1 -1
  57. package/dist/rsa/index.min.cjs.map +3 -3
  58. package/dist/rsa/index.min.js +2 -2
  59. package/dist/rsa/index.min.js.map +3 -3
  60. package/dist/types.cjs.map +2 -2
  61. package/dist/types.d.ts +11 -0
  62. package/dist/types.d.ts.map +1 -1
  63. package/dist/types.js.map +2 -2
  64. package/dist/types.min.cjs +1 -1
  65. package/dist/types.min.cjs.map +2 -2
  66. package/dist/types.min.js +1 -1
  67. package/dist/types.min.js.map +2 -2
  68. package/dist/util.cjs +6 -4
  69. package/dist/util.cjs.map +2 -2
  70. package/dist/util.d.ts +4 -0
  71. package/dist/util.d.ts.map +1 -1
  72. package/dist/util.js +6 -4
  73. package/dist/util.js.map +2 -2
  74. package/dist/util.min.cjs +1 -1
  75. package/dist/util.min.cjs.map +3 -3
  76. package/dist/util.min.js +1 -1
  77. package/dist/util.min.js.map +3 -3
  78. package/package.json +2 -2
@@ -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) and
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 was wrapped for this device using the `add` method.
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
- * @param enc The ephemeral public key (base64) from the sender.
90
- * @param wrappedKey The wrapped content key (base64) - contains salt + iv + ciphertext.
91
- * @param info Optional info parameter for HKDF. Must match what was used in `add`. Defaults to 'key-wrap'.
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(enc: string, wrappedKey: string, info?: string): Promise<CryptoKey>;
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] This is not relevant for most
109
- * use cases.
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
- * @returns {CryptoKey} New AES key
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
  */
@@ -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;AAY3E,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;AAS1C;;GAEG;AACH,qBAAa,OAAQ,SAAQ,YAAY;IACrC,MAAM,CAAC,I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,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) and
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(true);
137
- const salt = crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
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
- info || "key-wrap"
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 crypto.subtle.exportKey("raw", contentKey);
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 = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
154
- const wrappedKeyBuffer = await crypto.subtle.encrypt(
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
- const ephemeralPubKeyBuffer = await exportPublicKey(ephemeralKeypair);
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 = saltBuf.length + ivBuf.length + wrappedBuf.length;
179
+ const combinedSize = 1 + SALT_LENGTH + ivBuf.length + wrappedBuf.length;
164
180
  const combined = new Uint8Array(combinedSize);
165
- combined.set(saltBuf, 0);
166
- combined.set(ivBuf, saltBuf.length);
167
- combined.set(wrappedBuf, saltBuf.length + ivBuf.length);
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(new Uint8Array(ephemeralPubKeyBuffer)),
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 was wrapped for this device using the `add` method.
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
- * @param enc The ephemeral public key (base64) from the sender.
180
- * @param wrappedKey The wrapped content key (base64) - contains salt + iv + ciphertext.
181
- * @param info Optional info parameter for HKDF. Must match what was used in `add`. Defaults to 'key-wrap'.
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(enc, wrappedKey, info) {
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 wrappedBuf = new Uint8Array(base64ToArrBuf(wrappedKey));
191
- const salt = wrappedBuf.slice(0, SALT_LENGTH);
192
- const iv = wrappedBuf.slice(SALT_LENGTH, SALT_LENGTH + IV_LENGTH);
193
- const ciphertext = wrappedBuf.slice(SALT_LENGTH + IV_LENGTH);
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
- info || "key-wrap"
279
+ wrapKekInfo(encBytes, ourPubKeyBytes, baseInfo)
199
280
  );
200
- const contentKeyBytes = await crypto.subtle.decrypt(
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 crypto.subtle.importKey(
286
+ const contentKey = await webcrypto.subtle.importKey(
206
287
  "raw",
207
288
  contentKeyBytes,
208
289
  { name: DEFAULT_SYMM_ALGORITHM },
209
- true,
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] This is not relevant for most
228
- * use cases.
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 = crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
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(true);
247
- let key = aesKey || await deriveKey(
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
- if (!(key instanceof CryptoKey)) {
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 crypto.subtle.encrypt(
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 crypto.subtle.decrypt(
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
- * @returns {CryptoKey} New AES key
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 salt = crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
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
- salt,
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 crypto.subtle.exportKey(format, keypair.publicKey);
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 sharedSecret = await crypto.subtle.deriveBits(
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 crypto.subtle.importKey(
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 crypto.subtle.deriveKey(
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: encoder.encode(info)
652
+ info: toArrayBuffer(infoBytes)
535
653
  },
536
654
  hkdfBaseKey,
537
655
  { name: "AES-GCM", length: keysize || DEFAULT_SYMM_LENGTH },