@substrate-system/keys 0.3.2 → 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 (86) hide show
  1. package/README.md +35 -3
  2. package/dist/_base.cjs +20 -3
  3. package/dist/_base.cjs.map +2 -2
  4. package/dist/_base.d.ts +9 -1
  5. package/dist/_base.d.ts.map +1 -1
  6. package/dist/_base.js +20 -3
  7. package/dist/_base.js.map +2 -2
  8. package/dist/_base.min.cjs +1 -1
  9. package/dist/_base.min.cjs.map +3 -3
  10. package/dist/_base.min.js +2 -2
  11. package/dist/_base.min.js.map +3 -3
  12. package/dist/aes/index.cjs +14 -7
  13. package/dist/aes/index.cjs.map +2 -2
  14. package/dist/aes/index.d.ts +11 -3
  15. package/dist/aes/index.d.ts.map +1 -1
  16. package/dist/aes/index.js +14 -7
  17. package/dist/aes/index.js.map +2 -2
  18. package/dist/aes/index.min.cjs +1 -1
  19. package/dist/aes/index.min.cjs.map +3 -3
  20. package/dist/aes/index.min.js +2 -2
  21. package/dist/aes/index.min.js.map +3 -3
  22. package/dist/browser/index.js +244 -110
  23. package/dist/browser/index.min.js +2 -2
  24. package/dist/constants.cjs +4 -0
  25. package/dist/constants.cjs.map +2 -2
  26. package/dist/constants.d.ts +6 -0
  27. package/dist/constants.d.ts.map +1 -1
  28. package/dist/constants.js +4 -0
  29. package/dist/constants.js.map +2 -2
  30. package/dist/constants.min.cjs +1 -1
  31. package/dist/constants.min.cjs.map +3 -3
  32. package/dist/constants.min.js +1 -1
  33. package/dist/constants.min.js.map +3 -3
  34. package/dist/crypto.cjs +9 -7
  35. package/dist/crypto.cjs.map +2 -2
  36. package/dist/crypto.d.ts +6 -0
  37. package/dist/crypto.d.ts.map +1 -1
  38. package/dist/crypto.js +9 -8
  39. package/dist/crypto.js.map +2 -2
  40. package/dist/crypto.min.cjs +2 -2
  41. package/dist/crypto.min.cjs.map +2 -2
  42. package/dist/crypto.min.js +2 -2
  43. package/dist/crypto.min.js.map +3 -3
  44. package/dist/ecc/index.cjs +176 -72
  45. package/dist/ecc/index.cjs.map +2 -2
  46. package/dist/ecc/index.d.ts +47 -13
  47. package/dist/ecc/index.d.ts.map +1 -1
  48. package/dist/ecc/index.js +179 -73
  49. package/dist/ecc/index.js.map +2 -2
  50. package/dist/ecc/index.min.cjs +1 -1
  51. package/dist/ecc/index.min.cjs.map +3 -3
  52. package/dist/ecc/index.min.js +2 -2
  53. package/dist/ecc/index.min.js.map +3 -3
  54. package/dist/errors.min.js +1 -1
  55. package/dist/index.min.js +2 -2
  56. package/dist/index.min.js.map +3 -3
  57. package/dist/meta.json +37 -30
  58. package/dist/rsa/index.cjs +15 -16
  59. package/dist/rsa/index.cjs.map +2 -2
  60. package/dist/rsa/index.d.ts +6 -1
  61. package/dist/rsa/index.d.ts.map +1 -1
  62. package/dist/rsa/index.js +15 -17
  63. package/dist/rsa/index.js.map +2 -2
  64. package/dist/rsa/index.min.cjs +1 -1
  65. package/dist/rsa/index.min.cjs.map +3 -3
  66. package/dist/rsa/index.min.js +2 -2
  67. package/dist/rsa/index.min.js.map +3 -3
  68. package/dist/types.cjs.map +2 -2
  69. package/dist/types.d.ts +11 -0
  70. package/dist/types.d.ts.map +1 -1
  71. package/dist/types.js.map +2 -2
  72. package/dist/types.min.cjs +1 -1
  73. package/dist/types.min.cjs.map +2 -2
  74. package/dist/types.min.js +1 -1
  75. package/dist/types.min.js.map +2 -2
  76. package/dist/util.cjs +6 -4
  77. package/dist/util.cjs.map +2 -2
  78. package/dist/util.d.ts +4 -0
  79. package/dist/util.d.ts.map +1 -1
  80. package/dist/util.js +6 -4
  81. package/dist/util.js.map +2 -2
  82. package/dist/util.min.cjs +1 -1
  83. package/dist/util.min.cjs.map +3 -3
  84. package/dist/util.min.js +1 -1
  85. package/dist/util.min.js.map +3 -3
  86. 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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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;
@@ -43,12 +46,6 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
43
46
  * @returns {Promise<EccKeys>} A new class instance.
44
47
  */
45
48
  static async create(session, extractable, keys) {
46
- if (keys?.dbKey) {
47
- const exchangeKeyName = `${keys.dbKey}.${DEFAULT_ECC_EXCHANGE}`;
48
- const writeKeyName = `${keys.dbKey}.${DEFAULT_ECC_WRITE}`;
49
- _EccKeys.EXCHANGE_KEY_NAME = exchangeKeyName;
50
- _EccKeys.WRITE_KEY_NAME = writeKeyName;
51
- }
52
49
  return await super.create(session, extractable, keys);
53
50
  }
54
51
  get publicExchangeKey() {
@@ -102,12 +99,6 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
102
99
  static async load(opts = {
103
100
  session: false
104
101
  }) {
105
- if (opts.dbKey) {
106
- const exchangeKeyName = `${opts.dbKey}.${DEFAULT_ECC_EXCHANGE}`;
107
- const writeKeyName = `${opts.dbKey}.${DEFAULT_ECC_WRITE}`;
108
- _EccKeys.EXCHANGE_KEY_NAME = exchangeKeyName;
109
- _EccKeys.WRITE_KEY_NAME = writeKeyName;
110
- }
111
102
  return super.load(opts);
112
103
  }
113
104
  /**
@@ -136,89 +127,167 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
136
127
  * 4. AES-GCM encrypt the content
137
128
  * 5. Return the ephemeral public key (encapsulation) + wrapped key
138
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
+ *
139
134
  * @param {SymmKey|Uint8Array|string} contentKey The existing AES key used
140
135
  * to encrypt the content. Can be a base64 encoded string.
141
136
  * @param {CryptoKey} newPublicKey The new device's public X25519 key.
142
137
  * @param {string} [info] Optional info parameter for HKDF.
143
138
  * Defaults to 'key-wrap'.
144
- * @returns {Promise<WrappedKey>} The ephemeral public key (base64) and
145
- * 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.
146
142
  */
147
143
  async wrap(contentKey, newPublicKey, info) {
148
- const ephemeralKeypair = await _EccKeys._createExchangeKeys(true);
149
- 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));
150
152
  const kek = await deriveKey(
151
153
  ephemeralKeypair.privateKey,
152
154
  newPublicKey,
153
155
  salt,
154
- info || "key-wrap"
156
+ wrapKekInfo(ephemeralPubKeyBytes, recipientPubKey, baseInfo)
155
157
  );
156
158
  let contentKeyBytes;
159
+ let ownsContentKeyBytes = false;
157
160
  if (contentKey instanceof CryptoKey) {
158
- const exported = await crypto.subtle.exportKey("raw", contentKey);
161
+ const exported = await webcrypto.subtle.exportKey("raw", contentKey);
159
162
  contentKeyBytes = new Uint8Array(exported);
163
+ ownsContentKeyBytes = true;
160
164
  } else if (typeof contentKey === "string") {
161
165
  contentKeyBytes = fromString(contentKey, "base64pad");
166
+ ownsContentKeyBytes = true;
162
167
  } else {
163
168
  contentKeyBytes = contentKey;
164
169
  }
165
- const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
166
- const wrappedKeyBuffer = await crypto.subtle.encrypt(
170
+ const iv = webcrypto.getRandomValues(new Uint8Array(IV_LENGTH));
171
+ const wrappedKeyBuffer = await webcrypto.subtle.encrypt(
167
172
  { name: DEFAULT_SYMM_ALGORITHM, iv },
168
173
  kek,
169
174
  toArrayBuffer(contentKeyBytes)
170
175
  );
171
- const ephemeralPubKeyBuffer = await exportPublicKey(ephemeralKeypair);
172
- const saltBuf = new Uint8Array(salt);
176
+ if (ownsContentKeyBytes) contentKeyBytes.fill(0);
173
177
  const ivBuf = new Uint8Array(iv);
174
178
  const wrappedBuf = new Uint8Array(wrappedKeyBuffer);
175
- const combinedSize = saltBuf.length + ivBuf.length + wrappedBuf.length;
179
+ const combinedSize = 1 + SALT_LENGTH + ivBuf.length + wrappedBuf.length;
176
180
  const combined = new Uint8Array(combinedSize);
177
- combined.set(saltBuf, 0);
178
- combined.set(ivBuf, saltBuf.length);
179
- 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);
180
192
  return {
181
- enc: toBase64(new Uint8Array(ephemeralPubKeyBuffer)),
182
- wrappedKey: toBase64(combined)
193
+ enc: toBase64(ephemeralPubKeyBytes),
194
+ wrappedKey: toBase64(combined),
195
+ signature: toString(signature, "base64pad")
183
196
  };
184
197
  }
185
198
  /**
186
- * 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`.
187
201
  *
188
202
  * The recipient device uses its private key + the ephemeral public key
189
203
  * to rederive the KEK and decrypt the wrapped content key.
190
204
  *
191
- * @param enc The ephemeral public key (base64) from the sender.
192
- * @param wrappedKey The wrapped content key (base64) - contains salt + iv + ciphertext.
193
- * @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`.
194
219
  * @returns {Promise<CryptoKey>} The unwrapped AES content key.
195
220
  */
196
- 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
+ }
197
264
  const ephemeralPubKey = await importPublicKey(
198
265
  enc,
199
266
  EccCurve.X25519,
200
267
  KeyUse.Exchange
201
268
  );
202
- const wrappedBuf = new Uint8Array(base64ToArrBuf(wrappedKey));
203
- const salt = wrappedBuf.slice(0, SALT_LENGTH);
204
- const iv = wrappedBuf.slice(SALT_LENGTH, SALT_LENGTH + IV_LENGTH);
205
- 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);
206
275
  const kek = await deriveKey(
207
276
  this.exchangeKey.privateKey,
208
277
  ephemeralPubKey,
209
278
  salt,
210
- info || "key-wrap"
279
+ wrapKekInfo(encBytes, ourPubKeyBytes, baseInfo)
211
280
  );
212
- const contentKeyBytes = await crypto.subtle.decrypt(
281
+ const contentKeyBytes = await webcrypto.subtle.decrypt(
213
282
  { name: DEFAULT_SYMM_ALGORITHM, iv },
214
283
  kek,
215
284
  toArrayBuffer(ciphertext)
216
285
  );
217
- const contentKey = await crypto.subtle.importKey(
286
+ const contentKey = await webcrypto.subtle.importKey(
218
287
  "raw",
219
288
  contentKeyBytes,
220
289
  { name: DEFAULT_SYMM_ALGORITHM },
221
- true,
290
+ extractable,
222
291
  ["encrypt", "decrypt"]
223
292
  );
224
293
  return contentKey;
@@ -236,14 +305,15 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
236
305
  * will use our own public key if not passed in. Can be a CryptoKey
237
306
  * or a base64 encoded string.
238
307
  * @param {string} [info] info tag for HKDF. Default is the class property.
239
- * @param {SymmKey|Uint8Array|string} [aesKey] This is not relevant for most
240
- * 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.
241
311
  * @param {SymmKeyLength} [keysize] Default is 256
242
312
  * @returns {Promise<ArrayBuffer>} Buffer of ephemeral-public-key + salt +
243
313
  * iv + cipher text
244
314
  */
245
315
  async encrypt(content, recipient, info, aesKey, keysize) {
246
- const salt = crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
316
+ const salt = webcrypto.getRandomValues(new Uint8Array(SALT_LENGTH));
247
317
  const _publicKey = recipient || this.exchangeKey.publicKey;
248
318
  let publicKey;
249
319
  if (typeof _publicKey === "string") {
@@ -255,8 +325,8 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
255
325
  } else {
256
326
  publicKey = _publicKey;
257
327
  }
258
- const ephemeralKeypair = await _EccKeys._createExchangeKeys(true);
259
- 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(
260
330
  ephemeralKeypair.privateKey,
261
331
  // use ephemeral private key
262
332
  publicKey,
@@ -264,18 +334,9 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
264
334
  info || _EccKeys.INFO,
265
335
  keysize
266
336
  );
267
- if (!(key instanceof CryptoKey)) {
268
- key = await deriveKey(
269
- ephemeralKeypair.privateKey,
270
- // use ephemeral private key
271
- publicKey,
272
- salt,
273
- info || _EccKeys.INFO
274
- );
275
- }
276
- const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
337
+ const iv = webcrypto.getRandomValues(new Uint8Array(IV_LENGTH));
277
338
  const plaintext = typeof content === "string" ? new TextEncoder().encode(content) : content;
278
- const ciphertext = await crypto.subtle.encrypt(
339
+ const ciphertext = await webcrypto.subtle.encrypt(
279
340
  { name: DEFAULT_SYMM_ALGORITHM, iv },
280
341
  key,
281
342
  toArrayBuffer(plaintext)
@@ -317,9 +378,12 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
317
378
  * @param msg The encrypted content
318
379
  * @param {string} aesAlgorithm The algorithm. Default is AES-GCM.
319
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.
320
384
  * @returns {Promise<ArrayBuffer>} The decrypted content.
321
385
  */
322
- async decrypt(msg, aesAlgorithm, info) {
386
+ async decrypt(msg, aesAlgorithm, info, aesKey) {
323
387
  const encrypted = normalizeToBuf(msg, base64ToArrBuf);
324
388
  const ephemeralPubKeyBuf = encrypted.slice(0, X25519_PUBLIC_KEY_LENGTH);
325
389
  const ephemeralPublicKey = await webcrypto.subtle.importKey(
@@ -341,13 +405,13 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
341
405
  X25519_PUBLIC_KEY_LENGTH + SALT_LENGTH + IV_LENGTH
342
406
  );
343
407
  const { privateKey } = this.exchangeKey;
344
- const key = await deriveKey(
408
+ const key = aesKey ? aesKey instanceof CryptoKey ? aesKey : await AES.import(aesKey) : await deriveKey(
345
409
  privateKey,
346
410
  ephemeralPublicKey,
347
411
  salt,
348
412
  info || _EccKeys.INFO
349
413
  );
350
- const decrypted = await crypto.subtle.decrypt(
414
+ const decrypted = await webcrypto.subtle.decrypt(
351
415
  {
352
416
  name: aesAlgorithm || DEFAULT_SYMM_ALGORITHM,
353
417
  iv
@@ -369,20 +433,28 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
369
433
  * @param {string} info Custom "info" parameter
370
434
  * @returns {Promise<string>} The decrypted content as a string.
371
435
  */
372
- async decryptAsString(msg, aesAlgorithm, info) {
373
- 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);
374
438
  return toString(new Uint8Array(dec));
375
439
  }
376
440
  /**
377
441
  * Do DHKE, create a new AES-GCM key.
378
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
+ *
379
448
  * @param {CryptoKey|string|null} [publicKey] Public key to use in DHKE.
380
449
  * Will use our public key if it is not passed in.
381
450
  * @param {string} info The info parameter for DHKE. Will use the class
382
451
  * property `INFO` if it is not passed in.
383
- * @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
384
456
  */
385
- async getAesKey(publicKey, info) {
457
+ async getAesKey(publicKey, info, salt) {
386
458
  if (!publicKey) {
387
459
  publicKey = this.publicExchangeKey;
388
460
  }
@@ -396,14 +468,16 @@ const _EccKeys = class _EccKeys extends AbstractKeys {
396
468
  } else {
397
469
  pub = publicKey;
398
470
  }
399
- 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));
400
474
  const aesKey = await deriveKey(
401
475
  this.exchangeKey.privateKey,
402
476
  pub,
403
- salt,
477
+ saltBytes,
404
478
  info || _EccKeys.INFO
405
479
  );
406
- return aesKey;
480
+ return Object.assign(aesKey, { salt: toBase64(saltBytes) });
407
481
  }
408
482
  /**
409
483
  * Sign the given content using our private write key.
@@ -446,7 +520,7 @@ Object.assign(EccKeys.prototype.sign, {
446
520
  }, "asString")
447
521
  });
448
522
  async function exportPublicKey(keypair, format = "raw") {
449
- return await crypto.subtle.exportKey(format, keypair.publicKey);
523
+ return await webcrypto.subtle.exportKey(format, keypair.publicKey);
450
524
  }
451
525
  __name(exportPublicKey, "exportPublicKey");
452
526
  async function importPublicKey(base64Key, curve, use) {
@@ -524,26 +598,58 @@ async function verify(msg, sig, signingDid) {
524
598
  }
525
599
  }
526
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");
527
632
  async function deriveKey(privateKey, publicKey, salt, info, keysize) {
528
633
  const encoder = new TextEncoder();
529
- const sharedSecret = await crypto.subtle.deriveBits(
634
+ const infoBytes = typeof info === "string" ? encoder.encode(info) : info;
635
+ const sharedSecret = await webcrypto.subtle.deriveBits(
530
636
  { name: "X25519", public: publicKey },
531
637
  privateKey,
532
638
  256
533
639
  );
534
- const hkdfBaseKey = await crypto.subtle.importKey(
640
+ const hkdfBaseKey = await webcrypto.subtle.importKey(
535
641
  "raw",
536
642
  sharedSecret,
537
643
  "HKDF",
538
644
  false,
539
645
  ["deriveKey"]
540
646
  );
541
- return crypto.subtle.deriveKey(
647
+ return webcrypto.subtle.deriveKey(
542
648
  {
543
649
  name: "HKDF",
544
650
  hash: "SHA-256",
545
651
  salt: salt instanceof Uint8Array ? toArrayBuffer(salt) : salt,
546
- info: encoder.encode(info)
652
+ info: toArrayBuffer(infoBytes)
547
653
  },
548
654
  hkdfBaseKey,
549
655
  { name: "AES-GCM", length: keysize || DEFAULT_SYMM_LENGTH },