@substrate-system/keys 0.2.36 → 0.2.38

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 (52) hide show
  1. package/README.md +63 -8
  2. package/dist/_base.cjs +6 -0
  3. package/dist/_base.cjs.map +2 -2
  4. package/dist/_base.d.ts +8 -4
  5. package/dist/_base.d.ts.map +1 -1
  6. package/dist/_base.js +6 -0
  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 +10 -7
  13. package/dist/aes/index.cjs.map +2 -2
  14. package/dist/aes/index.d.ts.map +1 -1
  15. package/dist/aes/index.js +10 -7
  16. package/dist/aes/index.js.map +2 -2
  17. package/dist/aes/index.min.cjs +1 -1
  18. package/dist/aes/index.min.cjs.map +2 -2
  19. package/dist/aes/index.min.js +2 -2
  20. package/dist/aes/index.min.js.map +2 -2
  21. package/dist/browser/index.js +85 -45
  22. package/dist/browser/index.min.js +2 -2
  23. package/dist/constants.cjs +3 -1
  24. package/dist/constants.cjs.map +2 -2
  25. package/dist/constants.d.ts +1 -0
  26. package/dist/constants.d.ts.map +1 -1
  27. package/dist/constants.js +3 -1
  28. package/dist/constants.js.map +2 -2
  29. package/dist/constants.min.cjs +1 -1
  30. package/dist/constants.min.cjs.map +3 -3
  31. package/dist/constants.min.js +1 -1
  32. package/dist/constants.min.js.map +3 -3
  33. package/dist/crypto.min.js +1 -1
  34. package/dist/crypto.min.js.map +2 -2
  35. package/dist/ecc/index.cjs +67 -38
  36. package/dist/ecc/index.cjs.map +2 -2
  37. package/dist/ecc/index.d.ts +33 -24
  38. package/dist/ecc/index.d.ts.map +1 -1
  39. package/dist/ecc/index.js +70 -40
  40. package/dist/ecc/index.js.map +2 -2
  41. package/dist/ecc/index.min.cjs +1 -1
  42. package/dist/ecc/index.min.cjs.map +3 -3
  43. package/dist/ecc/index.min.js +2 -2
  44. package/dist/ecc/index.min.js.map +3 -3
  45. package/dist/index.min.js +2 -2
  46. package/dist/index.min.js.map +3 -3
  47. package/dist/meta.json +18 -17
  48. package/dist/rsa/index.min.js +2 -2
  49. package/dist/rsa/index.min.js.map +3 -3
  50. package/dist/util.min.js +1 -1
  51. package/dist/util.min.js.map +2 -2
  52. package/package.json +1 -1
@@ -2391,6 +2391,7 @@
2391
2391
  RSA_SIGN_ALGORITHM: () => RSA_SIGN_ALGORITHM,
2392
2392
  RsaKeys: () => RsaKeys,
2393
2393
  SALT_LENGTH: () => SALT_LENGTH,
2394
+ X25519_PUBLIC_KEY_LENGTH: () => X25519_PUBLIC_KEY_LENGTH,
2394
2395
  encrypt: () => encrypt2,
2395
2396
  encryptKeyTo: () => encryptKeyTo2,
2396
2397
  encryptTo: () => encryptTo2,
@@ -3676,6 +3677,7 @@
3676
3677
  var ECC_WRITE_ALGORITHM = "Ed25519";
3677
3678
  var ECC_EXCHANGE_ALGORITHM = "X25519";
3678
3679
  var DEFAULT_ECC_EXCHANGE_CURVE = "X25519" /* X25519 */;
3680
+ var X25519_PUBLIC_KEY_LENGTH = 32;
3679
3681
  var KEY_USE = {
3680
3682
  sign: ["sign", "verify"],
3681
3683
  encrypt: ["deriveKey", "deriveBits"]
@@ -4223,13 +4225,16 @@
4223
4225
  __name(importAesKey, "importAesKey");
4224
4226
  async function encrypt(data, cryptoKey, format2, iv) {
4225
4227
  const key = isCryptoKey(cryptoKey) ? cryptoKey : await importAesKey(cryptoKey);
4226
- const encrypted = iv ? await webcrypto.subtle.encrypt(
4227
- {
4228
- name: AES_GCM,
4229
- iv: toArrayBuffer(iv)
4230
- },
4231
- key,
4232
- toArrayBuffer(data)
4228
+ const encrypted = iv ? (
4229
+ // was an `iv` passed in? then encrypt here
4230
+ await webcrypto.subtle.encrypt(
4231
+ {
4232
+ name: AES_GCM,
4233
+ iv: toArrayBuffer(iv)
4234
+ },
4235
+ key,
4236
+ data instanceof ArrayBuffer ? data : toArrayBuffer(typeof data === "string" ? fromString2(data) : data)
4237
+ )
4233
4238
  ) : await encryptBytes(data, key);
4234
4239
  if (format2 && format2 === "arraybuffer") return encrypted;
4235
4240
  return new Uint8Array(encrypted);
@@ -4438,6 +4443,12 @@
4438
4443
  this._instance = keysInstance;
4439
4444
  return keysInstance;
4440
4445
  }
4446
+ static async exist(opts = {}) {
4447
+ const keys = await get(opts.writeKeyName || this.WRITE_KEY_NAME);
4448
+ const excahngeKeys = await get(opts.encryptionKeyName || this.EXCHANGE_KEY_NAME);
4449
+ if (!keys || !excahngeKeys) return false;
4450
+ return true;
4451
+ }
4441
4452
  /**
4442
4453
  * Restore some keys from indexedDB, or create a new keypair if it doesn't
4443
4454
  * exist yet.
@@ -4915,14 +4926,16 @@
4915
4926
  * 1. Generate ephemeral X25519 keypair
4916
4927
  * 2. Do ECDH with recipient's public key -> shared secret
4917
4928
  * 3. Use HKDF to derive a KEK (key encryption key)
4918
- * 4. AES-GCM encrypt the content key under the KEK
4929
+ * 4. AES-GCM encrypt the content
4919
4930
  * 5. Return the ephemeral public key (encapsulation) + wrapped key
4920
4931
  *
4921
- * @param contentKey The existing AES key used to encrypt the content.
4922
- * @param newPublicKey The new device's public X25519 key.
4923
- * @param info Optional info parameter for HKDF. Defaults to 'key-wrap'.
4932
+ * @param {SymmKey|Uint8Array|string} contentKey The existing AES key used
4933
+ * to encrypt the content. Can be a base64 encoded string.
4934
+ * @param {CryptoKey} newPublicKey The new device's public X25519 key.
4935
+ * @param {string} [info] Optional info parameter for HKDF.
4936
+ * Defaults to 'key-wrap'.
4924
4937
  * @returns {Promise<WrappedKey>} The ephemeral public key (base64) and
4925
- * the wrapped content key (base64).
4938
+ * the wrapped AES key (base64).
4926
4939
  */
4927
4940
  async wrap(contentKey, newPublicKey, info) {
4928
4941
  const ephemeralKeypair = await _EccKeys._createExchangeKeys(true);
@@ -4938,7 +4951,7 @@
4938
4951
  const exported = await crypto.subtle.exportKey("raw", contentKey);
4939
4952
  contentKeyBytes = new Uint8Array(exported);
4940
4953
  } else if (typeof contentKey === "string") {
4941
- contentKeyBytes = new Uint8Array(base64ToArrBuf(contentKey));
4954
+ contentKeyBytes = fromString2(contentKey, "base64pad");
4942
4955
  } else {
4943
4956
  contentKeyBytes = contentKey;
4944
4957
  }
@@ -5007,17 +5020,22 @@
5007
5020
  * Encrypt the given content to the given public key, or encrypt to
5008
5021
  * our own public key if a key is not passed in.
5009
5022
  *
5023
+ * This does ECIES style encryption -- a new ephemeral keypair is generated
5024
+ * and used to encrypt the message with ephemeral private + recipient public.
5025
+ * The public key from the ephemeral keypair is prefixed to the cipher text.
5026
+ *
5010
5027
  * @param {string|Uint8Array} content Content to encrypt
5011
5028
  * @param {CryptoKey|string} [recipient] Their public key. Optional b/c we
5012
- * will use our own public key if not passed in.
5029
+ * will use our own public key if not passed in. Can be a CryptoKey
5030
+ * or a base64 encoded string.
5013
5031
  * @param {string} [info] info tag for HKDF. Default is the class property.
5014
- * @param {SymmKey|Uint8Array|string} aesKey This is not relevant for most
5032
+ * @param {SymmKey|Uint8Array|string} [aesKey] This is not relevant for most
5015
5033
  * use cases.
5016
5034
  * @param {SymmKeyLength} [keysize] Default is 256
5017
- * @returns {Promise<ArrayBuffer>} Buffer of salt + iv + cipher text
5035
+ * @returns {Promise<ArrayBuffer>} Buffer of ephemeral-public-key + salt +
5036
+ * iv + cipher text
5018
5037
  */
5019
5038
  async encrypt(content, recipient, info, aesKey, keysize) {
5020
- const encoder = new TextEncoder();
5021
5039
  const salt = crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
5022
5040
  const _publicKey = recipient || this.exchangeKey.publicKey;
5023
5041
  let publicKey;
@@ -5030,8 +5048,10 @@
5030
5048
  } else {
5031
5049
  publicKey = _publicKey;
5032
5050
  }
5051
+ const ephemeralKeypair = await _EccKeys._createExchangeKeys(true);
5033
5052
  let key = aesKey || await deriveKey(
5034
- this.exchangeKey.privateKey,
5053
+ ephemeralKeypair.privateKey,
5054
+ // use ephemeral private key
5035
5055
  publicKey,
5036
5056
  salt,
5037
5057
  info || _EccKeys.INFO,
@@ -5039,27 +5059,32 @@
5039
5059
  );
5040
5060
  if (!(key instanceof CryptoKey)) {
5041
5061
  key = await deriveKey(
5042
- this.exchangeKey.privateKey,
5062
+ ephemeralKeypair.privateKey,
5063
+ // use ephemeral private key
5043
5064
  publicKey,
5044
5065
  salt,
5045
5066
  info || _EccKeys.INFO
5046
5067
  );
5047
5068
  }
5048
5069
  const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
5049
- const plaintext = typeof content === "string" ? encoder.encode(content) : content;
5070
+ const plaintext = typeof content === "string" ? new TextEncoder().encode(content) : content;
5050
5071
  const ciphertext = await crypto.subtle.encrypt(
5051
5072
  { name: DEFAULT_SYMM_ALGORITHM, iv },
5052
5073
  key,
5053
5074
  toArrayBuffer3(plaintext)
5054
5075
  );
5076
+ const ephemeralPubKeyBuf = new Uint8Array(
5077
+ await exportPublicKey(ephemeralKeypair)
5078
+ );
5055
5079
  const saltBuffer = new Uint8Array(salt);
5056
5080
  const ivBuffer = new Uint8Array(iv);
5057
5081
  const cipherBuf = new Uint8Array(ciphertext);
5058
- const size = saltBuffer.length + ivBuffer.length + cipherBuf.length;
5082
+ const size = ephemeralPubKeyBuf.length + saltBuffer.length + ivBuffer.length + cipherBuf.length;
5059
5083
  const result = new Uint8Array(size);
5060
- result.set(saltBuffer, 0);
5061
- result.set(ivBuffer, saltBuffer.length);
5062
- result.set(cipherBuf, saltBuffer.length + ivBuffer.length);
5084
+ result.set(ephemeralPubKeyBuf, 0);
5085
+ result.set(saltBuffer, ephemeralPubKeyBuf.length);
5086
+ result.set(ivBuffer, ephemeralPubKeyBuf.length + saltBuffer.length);
5087
+ result.set(cipherBuf, ephemeralPubKeyBuf.length + saltBuffer.length + ivBuffer.length);
5063
5088
  return result;
5064
5089
  }
5065
5090
  /**
@@ -5077,26 +5102,41 @@
5077
5102
  }
5078
5103
  /**
5079
5104
  * Decrypt the given message. The encrypted message should be
5080
- * salt + iv + cipher text.
5105
+ * ephemeral public key + salt + iv + cipher text.
5106
+ *
5107
+ * The ephemeral public key is extracted from the beginning of the message
5108
+ * and used with our private key to derive the AES decryption key.
5109
+ *
5081
5110
  * @param msg The encrypted content
5082
- * @param {CryptoKey|string} [publicKey] The public key used to generate
5083
- * the AES key used on this message. If omitted, decrypt with our own
5084
- * public key.
5085
5111
  * @param {string} aesAlgorithm The algorithm. Default is AES-GCM.
5086
5112
  * @param {string} info Custom "info" parameter
5087
5113
  * @returns {Promise<ArrayBuffer>} The decrypted content.
5088
5114
  */
5089
- async decrypt(msg, publicKey, aesAlgorithm, info) {
5090
- let pub = typeof publicKey === "string" ? await importPublicKey(publicKey, "X25519" /* X25519 */, "write" /* Write */) : publicKey;
5091
- if (!pub) pub = this.publicExchangeKey;
5115
+ async decrypt(msg, aesAlgorithm, info) {
5092
5116
  const encrypted = normalizeToBuf(msg, base64ToArrBuf);
5093
- const salt = encrypted.slice(0, SALT_LENGTH);
5094
- const iv = encrypted.slice(SALT_LENGTH, SALT_LENGTH + IV_LENGTH);
5095
- const ciphertext = encrypted.slice(SALT_LENGTH + IV_LENGTH);
5117
+ const ephemeralPubKeyBuf = encrypted.slice(0, X25519_PUBLIC_KEY_LENGTH);
5118
+ const ephemeralPublicKey = await webcrypto.subtle.importKey(
5119
+ "raw",
5120
+ ephemeralPubKeyBuf,
5121
+ { name: ECC_EXCHANGE_ALGORITHM },
5122
+ true,
5123
+ []
5124
+ );
5125
+ const salt = encrypted.slice(
5126
+ X25519_PUBLIC_KEY_LENGTH,
5127
+ X25519_PUBLIC_KEY_LENGTH + SALT_LENGTH
5128
+ );
5129
+ const iv = encrypted.slice(
5130
+ X25519_PUBLIC_KEY_LENGTH + SALT_LENGTH,
5131
+ X25519_PUBLIC_KEY_LENGTH + SALT_LENGTH + IV_LENGTH
5132
+ );
5133
+ const ciphertext = encrypted.slice(
5134
+ X25519_PUBLIC_KEY_LENGTH + SALT_LENGTH + IV_LENGTH
5135
+ );
5096
5136
  const { privateKey } = this.exchangeKey;
5097
5137
  const key = await deriveKey(
5098
5138
  privateKey,
5099
- pub,
5139
+ ephemeralPublicKey,
5100
5140
  salt,
5101
5141
  info || _EccKeys.INFO
5102
5142
  );
@@ -5111,19 +5151,19 @@
5111
5151
  return decrypted;
5112
5152
  }
5113
5153
  /**
5114
- * Decrypt and return as string.
5115
- * The encrypted message should be salt + iv + cipher text.
5154
+ * Decrypt and return as string using ECIES. The encrypted message should be
5155
+ * ephemeral public key + salt + iv + cipher text.
5156
+ *
5157
+ * The ephemeral public key is extracted from the beginning of the message
5158
+ * and used with our private key to derive the AES decryption key.
5116
5159
  *
5117
5160
  * @param msg The encrypted content
5118
- * @param {CryptoKey|string} [publicKey] The public key used to generate
5119
- * the AES key used on this message. If omitted, decrypt with our own
5120
- * public key.
5121
5161
  * @param {string} aesAlgorithm The algorithm. Default is AES-GCM.
5122
5162
  * @param {string} info Custom "info" parameter
5123
- * @returns {Promise<ArrayBuffer>} The decrypted content.
5163
+ * @returns {Promise<string>} The decrypted content as a string.
5124
5164
  */
5125
- async decryptAsString(msg, publicKey, aesAlgorithm, info) {
5126
- const dec = await this.decrypt(msg, publicKey, aesAlgorithm, info);
5165
+ async decryptAsString(msg, aesAlgorithm, info) {
5166
+ const dec = await this.decrypt(msg, aesAlgorithm, info);
5127
5167
  return toString2(new Uint8Array(dec));
5128
5168
  }
5129
5169
  /**
@@ -5221,8 +5261,8 @@
5221
5261
  }, "asString")
5222
5262
  });
5223
5263
  Object.assign(EccKeys.prototype.decrypt, {
5224
- asString: /* @__PURE__ */ __name(async function(msg, publicKey, aesAlgorithm) {
5225
- return this.decryptAsString(msg, publicKey, aesAlgorithm);
5264
+ asString: /* @__PURE__ */ __name(async function(msg, aesAlgorithm) {
5265
+ return this.decryptAsString(msg, aesAlgorithm);
5226
5266
  }, "asString")
5227
5267
  });
5228
5268
  Object.assign(EccKeys.prototype.sign, {