@substrate-system/keys 0.2.36 → 0.2.37

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 (50) hide show
  1. package/README.md +63 -8
  2. package/dist/_base.cjs.map +1 -1
  3. package/dist/_base.d.ts +4 -4
  4. package/dist/_base.d.ts.map +1 -1
  5. package/dist/_base.js.map +1 -1
  6. package/dist/_base.min.cjs +1 -1
  7. package/dist/_base.min.cjs.map +1 -1
  8. package/dist/_base.min.js +2 -2
  9. package/dist/_base.min.js.map +3 -3
  10. package/dist/aes/index.cjs +10 -7
  11. package/dist/aes/index.cjs.map +2 -2
  12. package/dist/aes/index.d.ts.map +1 -1
  13. package/dist/aes/index.js +10 -7
  14. package/dist/aes/index.js.map +2 -2
  15. package/dist/aes/index.min.cjs +1 -1
  16. package/dist/aes/index.min.cjs.map +2 -2
  17. package/dist/aes/index.min.js +2 -2
  18. package/dist/aes/index.min.js.map +2 -2
  19. package/dist/browser/index.js +79 -45
  20. package/dist/browser/index.min.js +2 -2
  21. package/dist/constants.cjs +3 -1
  22. package/dist/constants.cjs.map +2 -2
  23. package/dist/constants.d.ts +1 -0
  24. package/dist/constants.d.ts.map +1 -1
  25. package/dist/constants.js +3 -1
  26. package/dist/constants.js.map +2 -2
  27. package/dist/constants.min.cjs +1 -1
  28. package/dist/constants.min.cjs.map +3 -3
  29. package/dist/constants.min.js +1 -1
  30. package/dist/constants.min.js.map +3 -3
  31. package/dist/crypto.min.js +1 -1
  32. package/dist/crypto.min.js.map +2 -2
  33. package/dist/ecc/index.cjs +67 -38
  34. package/dist/ecc/index.cjs.map +2 -2
  35. package/dist/ecc/index.d.ts +33 -24
  36. package/dist/ecc/index.d.ts.map +1 -1
  37. package/dist/ecc/index.js +70 -40
  38. package/dist/ecc/index.js.map +2 -2
  39. package/dist/ecc/index.min.cjs +1 -1
  40. package/dist/ecc/index.min.cjs.map +3 -3
  41. package/dist/ecc/index.min.js +2 -2
  42. package/dist/ecc/index.min.js.map +3 -3
  43. package/dist/index.min.js +2 -2
  44. package/dist/index.min.js.map +3 -3
  45. package/dist/meta.json +16 -15
  46. package/dist/rsa/index.min.js +2 -2
  47. package/dist/rsa/index.min.js.map +3 -3
  48. package/dist/util.min.js +1 -1
  49. package/dist/util.min.js.map +2 -2
  50. 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);
@@ -4915,14 +4920,16 @@
4915
4920
  * 1. Generate ephemeral X25519 keypair
4916
4921
  * 2. Do ECDH with recipient's public key -> shared secret
4917
4922
  * 3. Use HKDF to derive a KEK (key encryption key)
4918
- * 4. AES-GCM encrypt the content key under the KEK
4923
+ * 4. AES-GCM encrypt the content
4919
4924
  * 5. Return the ephemeral public key (encapsulation) + wrapped key
4920
4925
  *
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'.
4926
+ * @param {SymmKey|Uint8Array|string} contentKey The existing AES key used
4927
+ * to encrypt the content. Can be a base64 encoded string.
4928
+ * @param {CryptoKey} newPublicKey The new device's public X25519 key.
4929
+ * @param {string} [info] Optional info parameter for HKDF.
4930
+ * Defaults to 'key-wrap'.
4924
4931
  * @returns {Promise<WrappedKey>} The ephemeral public key (base64) and
4925
- * the wrapped content key (base64).
4932
+ * the wrapped AES key (base64).
4926
4933
  */
4927
4934
  async wrap(contentKey, newPublicKey, info) {
4928
4935
  const ephemeralKeypair = await _EccKeys._createExchangeKeys(true);
@@ -4938,7 +4945,7 @@
4938
4945
  const exported = await crypto.subtle.exportKey("raw", contentKey);
4939
4946
  contentKeyBytes = new Uint8Array(exported);
4940
4947
  } else if (typeof contentKey === "string") {
4941
- contentKeyBytes = new Uint8Array(base64ToArrBuf(contentKey));
4948
+ contentKeyBytes = fromString2(contentKey, "base64pad");
4942
4949
  } else {
4943
4950
  contentKeyBytes = contentKey;
4944
4951
  }
@@ -5007,17 +5014,22 @@
5007
5014
  * Encrypt the given content to the given public key, or encrypt to
5008
5015
  * our own public key if a key is not passed in.
5009
5016
  *
5017
+ * This does ECIES style encryption -- a new ephemeral keypair is generated
5018
+ * and used to encrypt the message with ephemeral private + recipient public.
5019
+ * The public key from the ephemeral keypair is prefixed to the cipher text.
5020
+ *
5010
5021
  * @param {string|Uint8Array} content Content to encrypt
5011
5022
  * @param {CryptoKey|string} [recipient] Their public key. Optional b/c we
5012
- * will use our own public key if not passed in.
5023
+ * will use our own public key if not passed in. Can be a CryptoKey
5024
+ * or a base64 encoded string.
5013
5025
  * @param {string} [info] info tag for HKDF. Default is the class property.
5014
- * @param {SymmKey|Uint8Array|string} aesKey This is not relevant for most
5026
+ * @param {SymmKey|Uint8Array|string} [aesKey] This is not relevant for most
5015
5027
  * use cases.
5016
5028
  * @param {SymmKeyLength} [keysize] Default is 256
5017
- * @returns {Promise<ArrayBuffer>} Buffer of salt + iv + cipher text
5029
+ * @returns {Promise<ArrayBuffer>} Buffer of ephemeral-public-key + salt +
5030
+ * iv + cipher text
5018
5031
  */
5019
5032
  async encrypt(content, recipient, info, aesKey, keysize) {
5020
- const encoder = new TextEncoder();
5021
5033
  const salt = crypto.getRandomValues(new Uint8Array(SALT_LENGTH));
5022
5034
  const _publicKey = recipient || this.exchangeKey.publicKey;
5023
5035
  let publicKey;
@@ -5030,8 +5042,10 @@
5030
5042
  } else {
5031
5043
  publicKey = _publicKey;
5032
5044
  }
5045
+ const ephemeralKeypair = await _EccKeys._createExchangeKeys(true);
5033
5046
  let key = aesKey || await deriveKey(
5034
- this.exchangeKey.privateKey,
5047
+ ephemeralKeypair.privateKey,
5048
+ // use ephemeral private key
5035
5049
  publicKey,
5036
5050
  salt,
5037
5051
  info || _EccKeys.INFO,
@@ -5039,27 +5053,32 @@
5039
5053
  );
5040
5054
  if (!(key instanceof CryptoKey)) {
5041
5055
  key = await deriveKey(
5042
- this.exchangeKey.privateKey,
5056
+ ephemeralKeypair.privateKey,
5057
+ // use ephemeral private key
5043
5058
  publicKey,
5044
5059
  salt,
5045
5060
  info || _EccKeys.INFO
5046
5061
  );
5047
5062
  }
5048
5063
  const iv = crypto.getRandomValues(new Uint8Array(IV_LENGTH));
5049
- const plaintext = typeof content === "string" ? encoder.encode(content) : content;
5064
+ const plaintext = typeof content === "string" ? new TextEncoder().encode(content) : content;
5050
5065
  const ciphertext = await crypto.subtle.encrypt(
5051
5066
  { name: DEFAULT_SYMM_ALGORITHM, iv },
5052
5067
  key,
5053
5068
  toArrayBuffer3(plaintext)
5054
5069
  );
5070
+ const ephemeralPubKeyBuf = new Uint8Array(
5071
+ await exportPublicKey(ephemeralKeypair)
5072
+ );
5055
5073
  const saltBuffer = new Uint8Array(salt);
5056
5074
  const ivBuffer = new Uint8Array(iv);
5057
5075
  const cipherBuf = new Uint8Array(ciphertext);
5058
- const size = saltBuffer.length + ivBuffer.length + cipherBuf.length;
5076
+ const size = ephemeralPubKeyBuf.length + saltBuffer.length + ivBuffer.length + cipherBuf.length;
5059
5077
  const result = new Uint8Array(size);
5060
- result.set(saltBuffer, 0);
5061
- result.set(ivBuffer, saltBuffer.length);
5062
- result.set(cipherBuf, saltBuffer.length + ivBuffer.length);
5078
+ result.set(ephemeralPubKeyBuf, 0);
5079
+ result.set(saltBuffer, ephemeralPubKeyBuf.length);
5080
+ result.set(ivBuffer, ephemeralPubKeyBuf.length + saltBuffer.length);
5081
+ result.set(cipherBuf, ephemeralPubKeyBuf.length + saltBuffer.length + ivBuffer.length);
5063
5082
  return result;
5064
5083
  }
5065
5084
  /**
@@ -5077,26 +5096,41 @@
5077
5096
  }
5078
5097
  /**
5079
5098
  * Decrypt the given message. The encrypted message should be
5080
- * salt + iv + cipher text.
5099
+ * ephemeral public key + salt + iv + cipher text.
5100
+ *
5101
+ * The ephemeral public key is extracted from the beginning of the message
5102
+ * and used with our private key to derive the AES decryption key.
5103
+ *
5081
5104
  * @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
5105
  * @param {string} aesAlgorithm The algorithm. Default is AES-GCM.
5086
5106
  * @param {string} info Custom "info" parameter
5087
5107
  * @returns {Promise<ArrayBuffer>} The decrypted content.
5088
5108
  */
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;
5109
+ async decrypt(msg, aesAlgorithm, info) {
5092
5110
  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);
5111
+ const ephemeralPubKeyBuf = encrypted.slice(0, X25519_PUBLIC_KEY_LENGTH);
5112
+ const ephemeralPublicKey = await webcrypto.subtle.importKey(
5113
+ "raw",
5114
+ ephemeralPubKeyBuf,
5115
+ { name: ECC_EXCHANGE_ALGORITHM },
5116
+ true,
5117
+ []
5118
+ );
5119
+ const salt = encrypted.slice(
5120
+ X25519_PUBLIC_KEY_LENGTH,
5121
+ X25519_PUBLIC_KEY_LENGTH + SALT_LENGTH
5122
+ );
5123
+ const iv = encrypted.slice(
5124
+ X25519_PUBLIC_KEY_LENGTH + SALT_LENGTH,
5125
+ X25519_PUBLIC_KEY_LENGTH + SALT_LENGTH + IV_LENGTH
5126
+ );
5127
+ const ciphertext = encrypted.slice(
5128
+ X25519_PUBLIC_KEY_LENGTH + SALT_LENGTH + IV_LENGTH
5129
+ );
5096
5130
  const { privateKey } = this.exchangeKey;
5097
5131
  const key = await deriveKey(
5098
5132
  privateKey,
5099
- pub,
5133
+ ephemeralPublicKey,
5100
5134
  salt,
5101
5135
  info || _EccKeys.INFO
5102
5136
  );
@@ -5111,19 +5145,19 @@
5111
5145
  return decrypted;
5112
5146
  }
5113
5147
  /**
5114
- * Decrypt and return as string.
5115
- * The encrypted message should be salt + iv + cipher text.
5148
+ * Decrypt and return as string using ECIES. The encrypted message should be
5149
+ * ephemeral public key + salt + iv + cipher text.
5150
+ *
5151
+ * The ephemeral public key is extracted from the beginning of the message
5152
+ * and used with our private key to derive the AES decryption key.
5116
5153
  *
5117
5154
  * @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
5155
  * @param {string} aesAlgorithm The algorithm. Default is AES-GCM.
5122
5156
  * @param {string} info Custom "info" parameter
5123
- * @returns {Promise<ArrayBuffer>} The decrypted content.
5157
+ * @returns {Promise<string>} The decrypted content as a string.
5124
5158
  */
5125
- async decryptAsString(msg, publicKey, aesAlgorithm, info) {
5126
- const dec = await this.decrypt(msg, publicKey, aesAlgorithm, info);
5159
+ async decryptAsString(msg, aesAlgorithm, info) {
5160
+ const dec = await this.decrypt(msg, aesAlgorithm, info);
5127
5161
  return toString2(new Uint8Array(dec));
5128
5162
  }
5129
5163
  /**
@@ -5221,8 +5255,8 @@
5221
5255
  }, "asString")
5222
5256
  });
5223
5257
  Object.assign(EccKeys.prototype.decrypt, {
5224
- asString: /* @__PURE__ */ __name(async function(msg, publicKey, aesAlgorithm) {
5225
- return this.decryptAsString(msg, publicKey, aesAlgorithm);
5258
+ asString: /* @__PURE__ */ __name(async function(msg, aesAlgorithm) {
5259
+ return this.decryptAsString(msg, aesAlgorithm);
5226
5260
  }, "asString")
5227
5261
  });
5228
5262
  Object.assign(EccKeys.prototype.sign, {