@protontech/openpgp 6.0.0-beta.0 → 6.0.0-beta.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.
Files changed (41) hide show
  1. package/dist/lightweight/argon2id.min.mjs +2 -2
  2. package/dist/lightweight/argon2id.min.mjs.map +1 -1
  3. package/dist/lightweight/argon2id.mjs +5 -28
  4. package/dist/lightweight/legacy_ciphers.min.mjs +2 -2
  5. package/dist/lightweight/legacy_ciphers.min.mjs.map +1 -1
  6. package/dist/lightweight/legacy_ciphers.mjs +6 -51
  7. package/dist/lightweight/noble_curves.min.mjs +11 -11
  8. package/dist/lightweight/noble_curves.min.mjs.map +1 -1
  9. package/dist/lightweight/noble_curves.mjs +477 -465
  10. package/dist/lightweight/noble_hashes.min.mjs +2 -2
  11. package/dist/lightweight/noble_hashes.min.mjs.map +1 -1
  12. package/dist/lightweight/noble_hashes.mjs +19 -31
  13. package/dist/lightweight/openpgp.min.mjs +2 -2
  14. package/dist/lightweight/openpgp.min.mjs.map +1 -1
  15. package/dist/lightweight/openpgp.mjs +600 -458
  16. package/dist/lightweight/sha3.min.mjs +3 -3
  17. package/dist/lightweight/sha3.min.mjs.map +1 -1
  18. package/dist/lightweight/sha3.mjs +80 -80
  19. package/dist/node/openpgp.cjs +1254 -5443
  20. package/dist/node/openpgp.min.cjs +11 -12
  21. package/dist/node/openpgp.min.cjs.map +1 -1
  22. package/dist/node/openpgp.min.mjs +11 -12
  23. package/dist/node/openpgp.min.mjs.map +1 -1
  24. package/dist/node/openpgp.mjs +1254 -5443
  25. package/dist/openpgp.js +1247 -5436
  26. package/dist/openpgp.min.js +11 -12
  27. package/dist/openpgp.min.js.map +1 -1
  28. package/dist/openpgp.min.mjs +11 -12
  29. package/dist/openpgp.min.mjs.map +1 -1
  30. package/dist/openpgp.mjs +1247 -5436
  31. package/openpgp.d.ts +62 -60
  32. package/package.json +16 -10
  33. package/dist/lightweight/bn.interface.min.mjs +0 -3
  34. package/dist/lightweight/bn.interface.min.mjs.map +0 -1
  35. package/dist/lightweight/bn.interface.mjs +0 -3807
  36. package/dist/lightweight/interface.min.mjs +0 -3
  37. package/dist/lightweight/interface.min.mjs.map +0 -1
  38. package/dist/lightweight/interface.mjs +0 -16
  39. package/dist/lightweight/native.interface.min.mjs +0 -3
  40. package/dist/lightweight/native.interface.min.mjs.map +0 -1
  41. package/dist/lightweight/native.interface.mjs +0 -456
@@ -1,4 +1,4 @@
1
- /*! OpenPGP.js v6.0.0-beta.0 - 2024-04-18 - this is LGPL licensed code, see LICENSE/our website https://openpgpjs.org/ for more information. */
1
+ /*! OpenPGP.js v6.0.0-beta.1 - 2024-05-17 - this is LGPL licensed code, see LICENSE/our website https://openpgpjs.org/ for more information. */
2
2
  const globalThis = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
3
3
 
4
4
  const doneWritingPromise = Symbol('doneWritingPromise');
@@ -1465,6 +1465,14 @@ var config = {
1465
1465
  * @property {Boolean} aeadProtect
1466
1466
  */
1467
1467
  aeadProtect: false,
1468
+ /**
1469
+ * Whether to disable encrypton using SEIPDv2 even if the encryption keys include the SEIPDv2 feature flag.
1470
+ * If true, SEIPDv1 (i.e. no AEAD) packets are always used instead.
1471
+ * SEIPDv2 is a more secure and faster choice, but it is not necessarily compatible with other libs and our mobile apps.
1472
+ * @memberof module:config
1473
+ * @property {Boolean} ignoreSEIPDv2FeatureFlag
1474
+ */
1475
+ ignoreSEIPDv2FeatureFlag: false,
1468
1476
  /**
1469
1477
  * When reading OpenPGP v4 private keys (e.g. those generated in OpenPGP.js when not setting `config.v5Keys = true`)
1470
1478
  * which were encrypted by OpenPGP.js v5 (or older) using `config.aeadProtect = true`,
@@ -1652,7 +1660,7 @@ var config = {
1652
1660
  * @memberof module:config
1653
1661
  * @property {String} versionString A version string to be included in armored messages
1654
1662
  */
1655
- versionString: 'OpenPGP.js 6.0.0-beta.0',
1663
+ versionString: 'OpenPGP.js 6.0.0-beta.1',
1656
1664
  /**
1657
1665
  * @memberof module:config
1658
1666
  * @property {String} commentString A comment string to be included in armored messages
@@ -1713,27 +1721,6 @@ var config = {
1713
1721
  rejectCurves: new Set([enums.curve.secp256k1])
1714
1722
  };
1715
1723
 
1716
- /**
1717
- * We don't use the BigIntegerInterface wrapper from noble-hashes because:
1718
- * - importing the instance results in it being shared with noble-hashes, which separately calls `setImplementation()`
1719
- * on load, causing it to throw due to duplicate initialization.
1720
- * - even duplicating the interface code here to keep a separate instance requires handing a race-conditions the first time
1721
- * `getBigInteger` is called, when the code needs to check if the implementation is set, and initialize it if not.
1722
- * Ultimately, the interface provides no advantages and it's only needed because of TS.
1723
- */
1724
- const detectBigInt = () => typeof BigInt !== 'undefined';
1725
- async function getBigInteger() {
1726
- if (detectBigInt()) {
1727
- // NativeBigInteger is small, so it's imported in isolation (it could also be imported at the top level)
1728
- const { default: NativeBigInteger } = await import('./native.interface.mjs');
1729
- return NativeBigInteger;
1730
- } else {
1731
- // FallbackBigInteger relies on large BN.js lib, which is also used by noble-hashes and noble-curves
1732
- const { default: FallbackBigInteger } = await import('./bn.interface.mjs');
1733
- return FallbackBigInteger;
1734
- }
1735
- }
1736
-
1737
1724
  // GPG4Browsers - An OpenPGP implementation in javascript
1738
1725
  // Copyright (C) 2011 Recurity Labs GmbH
1739
1726
  //
@@ -1775,8 +1762,6 @@ const util = {
1775
1762
 
1776
1763
  isStream: isStream,
1777
1764
 
1778
- getBigInteger,
1779
-
1780
1765
  /**
1781
1766
  * Load noble-curves lib on demand and return the requested curve function
1782
1767
  * @param {enums.publicKey} publicKeyAlgo
@@ -2498,7 +2483,7 @@ function b64ToUint8Array(base64) {
2498
2483
  */
2499
2484
  function uint8ArrayToB64(bytes, url) {
2500
2485
  let encoded = encode$1(bytes).replace(/[\r\n]/g, '');
2501
- if (url) {
2486
+ {
2502
2487
  encoded = encoded.replace(/[+]/g, '-').replace(/[/]/g, '_').replace(/[=]/g, '');
2503
2488
  }
2504
2489
  return encoded;
@@ -4285,7 +4270,7 @@ function is_bytes(a) {
4285
4270
  return a instanceof Uint8Array;
4286
4271
  }
4287
4272
  function _heap_init(heap, heapSize) {
4288
- const size = heap ? heap.byteLength : heapSize || 65536;
4273
+ const size = heap ? heap.byteLength : 65536;
4289
4274
  if (size & 0xfff || size <= 0)
4290
4275
  throw new Error('heap size must be a positive integer and a multiple of 4096');
4291
4276
  heap = heap || new Uint8Array(new ArrayBuffer(size));
@@ -5842,6 +5827,202 @@ var mode = {
5842
5827
  ocb: OCB
5843
5828
  };
5844
5829
 
5830
+ // Operations are not constant time, but we try and limit timing leakage where we can
5831
+ const _0n$1 = BigInt(0);
5832
+ const _1n$4 = BigInt(1);
5833
+ function uint8ArrayToBigInt(bytes) {
5834
+ const hexAlphabet = '0123456789ABCDEF';
5835
+ let s = '';
5836
+ bytes.forEach(v => {
5837
+ s += hexAlphabet[v >> 4] + hexAlphabet[v & 15];
5838
+ });
5839
+ return BigInt('0x0' + s);
5840
+ }
5841
+ function mod$1(a, m) {
5842
+ const reduced = a % m;
5843
+ return reduced < _0n$1 ? reduced + m : reduced;
5844
+ }
5845
+ /**
5846
+ * Compute modular exponentiation using square and multiply
5847
+ * @param {BigInt} a - Base
5848
+ * @param {BigInt} e - Exponent
5849
+ * @param {BigInt} n - Modulo
5850
+ * @returns {BigInt} b ** e mod n.
5851
+ */
5852
+ function modExp(b, e, n) {
5853
+ if (n === _0n$1)
5854
+ throw Error('Modulo cannot be zero');
5855
+ if (n === _1n$4)
5856
+ return BigInt(0);
5857
+ if (e < _0n$1)
5858
+ throw Error('Unsopported negative exponent');
5859
+ let exp = e;
5860
+ let x = b;
5861
+ x %= n;
5862
+ let r = BigInt(1);
5863
+ while (exp > _0n$1) {
5864
+ const lsb = exp & _1n$4;
5865
+ exp >>= _1n$4; // e / 2
5866
+ // Always compute multiplication step, to reduce timing leakage
5867
+ const rx = (r * x) % n;
5868
+ // Update r only if lsb is 1 (odd exponent)
5869
+ r = lsb ? rx : r;
5870
+ x = (x * x) % n; // Square
5871
+ }
5872
+ return r;
5873
+ }
5874
+ function abs(x) {
5875
+ return x >= _0n$1 ? x : -x;
5876
+ }
5877
+ /**
5878
+ * Extended Eucleadian algorithm (http://anh.cs.luc.edu/331/notes/xgcd.pdf)
5879
+ * Given a and b, compute (x, y) such that ax + by = gdc(a, b).
5880
+ * Negative numbers are also supported.
5881
+ * @param {BigInt} a - First operand
5882
+ * @param {BigInt} b - Second operand
5883
+ * @returns {{ gcd, x, y: bigint }}
5884
+ */
5885
+ function _egcd(aInput, bInput) {
5886
+ let x = BigInt(0);
5887
+ let y = BigInt(1);
5888
+ let xPrev = BigInt(1);
5889
+ let yPrev = BigInt(0);
5890
+ // Deal with negative numbers: run algo over absolute values,
5891
+ // and "move" the sign to the returned x and/or y.
5892
+ // See https://math.stackexchange.com/questions/37806/extended-euclidean-algorithm-with-negative-numbers
5893
+ let a = abs(aInput);
5894
+ let b = abs(bInput);
5895
+ const aNegated = aInput < _0n$1;
5896
+ const bNegated = bInput < _0n$1;
5897
+ while (b !== _0n$1) {
5898
+ const q = a / b;
5899
+ let tmp = x;
5900
+ x = xPrev - q * x;
5901
+ xPrev = tmp;
5902
+ tmp = y;
5903
+ y = yPrev - q * y;
5904
+ yPrev = tmp;
5905
+ tmp = b;
5906
+ b = a % b;
5907
+ a = tmp;
5908
+ }
5909
+ return {
5910
+ x: aNegated ? -xPrev : xPrev,
5911
+ y: bNegated ? -yPrev : yPrev,
5912
+ gcd: a
5913
+ };
5914
+ }
5915
+ /**
5916
+ * Compute the inverse of `a` modulo `n`
5917
+ * Note: `a` and and `n` must be relatively prime
5918
+ * @param {BigInt} a
5919
+ * @param {BigInt} n - Modulo
5920
+ * @returns {BigInt} x such that a*x = 1 mod n
5921
+ * @throws {Error} if the inverse does not exist
5922
+ */
5923
+ function modInv(a, n) {
5924
+ const { gcd, x } = _egcd(a, n);
5925
+ if (gcd !== _1n$4) {
5926
+ throw new Error('Inverse does not exist');
5927
+ }
5928
+ return mod$1(x + n, n);
5929
+ }
5930
+ /**
5931
+ * Compute greatest common divisor between this and n
5932
+ * @param {BigInt} aInput - Operand
5933
+ * @param {BigInt} bInput - Operand
5934
+ * @returns {BigInt} gcd
5935
+ */
5936
+ function gcd(aInput, bInput) {
5937
+ let a = aInput;
5938
+ let b = bInput;
5939
+ while (b !== _0n$1) {
5940
+ const tmp = b;
5941
+ b = a % b;
5942
+ a = tmp;
5943
+ }
5944
+ return a;
5945
+ }
5946
+ /**
5947
+ * Get this value as an exact Number (max 53 bits)
5948
+ * Fails if this value is too large
5949
+ * @returns {Number}
5950
+ */
5951
+ function bigIntToNumber(x) {
5952
+ const number = Number(x);
5953
+ if (number > Number.MAX_SAFE_INTEGER) {
5954
+ // We throw and error to conform with the bn.js implementation
5955
+ throw new Error('Number can only safely store up to 53 bits');
5956
+ }
5957
+ return number;
5958
+ }
5959
+ /**
5960
+ * Get value of i-th bit
5961
+ * @param {BigInt} x
5962
+ * @param {Number} i - Bit index
5963
+ * @returns {Number} Bit value.
5964
+ */
5965
+ function getBit(x, i) {
5966
+ const bit = (x >> BigInt(i)) & _1n$4;
5967
+ return bit === _0n$1 ? 0 : 1;
5968
+ }
5969
+ /**
5970
+ * Compute bit length
5971
+ */
5972
+ function bitLength(x) {
5973
+ // -1n >> -1n is -1n
5974
+ // 1n >> 1n is 0n
5975
+ const target = x < _0n$1 ? BigInt(-1) : _0n$1;
5976
+ let bitlen = 1;
5977
+ let tmp = x;
5978
+ // eslint-disable-next-line no-cond-assign
5979
+ while ((tmp >>= _1n$4) !== target) {
5980
+ bitlen++;
5981
+ }
5982
+ return bitlen;
5983
+ }
5984
+ /**
5985
+ * Compute byte length
5986
+ */
5987
+ function byteLength(x) {
5988
+ const target = x < _0n$1 ? BigInt(-1) : _0n$1;
5989
+ const _8n = BigInt(8);
5990
+ let len = 1;
5991
+ let tmp = x;
5992
+ // eslint-disable-next-line no-cond-assign
5993
+ while ((tmp >>= _8n) !== target) {
5994
+ len++;
5995
+ }
5996
+ return len;
5997
+ }
5998
+ /**
5999
+ * Get Uint8Array representation of this number
6000
+ * @param {String} endian - Endianess of output array (defaults to 'be')
6001
+ * @param {Number} length - Of output array
6002
+ * @returns {Uint8Array}
6003
+ */
6004
+ function bigIntToUint8Array(x, endian = 'be', length) {
6005
+ // we get and parse the hex string (https://coolaj86.com/articles/convert-js-bigints-to-typedarrays/)
6006
+ // this is faster than shift+mod iterations
6007
+ let hex = x.toString(16);
6008
+ if (hex.length % 2 === 1) {
6009
+ hex = '0' + hex;
6010
+ }
6011
+ const rawLength = hex.length / 2;
6012
+ const bytes = new Uint8Array(length || rawLength);
6013
+ // parse hex
6014
+ const offset = length ? length - rawLength : 0;
6015
+ let i = 0;
6016
+ while (i < rawLength) {
6017
+ bytes[i + offset] = parseInt(hex.slice(2 * i, 2 * i + 2), 16);
6018
+ i++;
6019
+ }
6020
+ if (endian !== 'be') {
6021
+ bytes.reverse();
6022
+ }
6023
+ return bytes;
6024
+ }
6025
+
5845
6026
  // GPG4Browsers - An OpenPGP implementation in javascript
5846
6027
  // Copyright (C) 2011 Recurity Labs GmbH
5847
6028
  //
@@ -5881,27 +6062,25 @@ function getRandomBytes(length) {
5881
6062
  }
5882
6063
 
5883
6064
  /**
5884
- * Create a secure random BigInteger that is greater than or equal to min and less than max.
5885
- * @param {module:BigInteger} min - Lower bound, included
5886
- * @param {module:BigInteger} max - Upper bound, excluded
5887
- * @returns {Promise<module:BigInteger>} Random BigInteger.
6065
+ * Create a secure random BigInt that is greater than or equal to min and less than max.
6066
+ * @param {bigint} min - Lower bound, included
6067
+ * @param {bigint} max - Upper bound, excluded
6068
+ * @returns {bigint} Random BigInt.
5888
6069
  * @async
5889
6070
  */
5890
- async function getRandomBigInteger(min, max) {
5891
- const BigInteger = await util.getBigInteger();
5892
-
5893
- if (max.lt(min)) {
6071
+ function getRandomBigInteger(min, max) {
6072
+ if (max < min) {
5894
6073
  throw new Error('Illegal parameter value: max <= min');
5895
6074
  }
5896
6075
 
5897
- const modulus = max.sub(min);
5898
- const bytes = modulus.byteLength();
6076
+ const modulus = max - min;
6077
+ const bytes = byteLength(modulus);
5899
6078
 
5900
6079
  // Using a while loop is necessary to avoid bias introduced by the mod operation.
5901
6080
  // However, we request 64 extra random bits so that the bias is negligible.
5902
6081
  // Section B.1.1 here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
5903
- const r = new BigInteger(getRandomBytes(bytes + 8));
5904
- return r.mod(modulus).add(min);
6082
+ const r = uint8ArrayToBigInt(getRandomBytes(bytes + 8));
6083
+ return mod$1(r, modulus) + min;
5905
6084
  }
5906
6085
 
5907
6086
  var random = /*#__PURE__*/Object.freeze({
@@ -5926,177 +6105,159 @@ var random = /*#__PURE__*/Object.freeze({
5926
6105
  // You should have received a copy of the GNU Lesser General Public
5927
6106
  // License along with this library; if not, write to the Free Software
5928
6107
  // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5929
-
5930
-
6108
+ /**
6109
+ * @fileoverview Algorithms for probabilistic random prime generation
6110
+ * @module crypto/public_key/prime
6111
+ */
6112
+ const _1n$3 = BigInt(1);
5931
6113
  /**
5932
6114
  * Generate a probably prime random number
5933
- * @param {Integer} bits - Bit length of the prime
5934
- * @param {BigInteger} e - Optional RSA exponent to check against the prime
5935
- * @param {Integer} k - Optional number of iterations of Miller-Rabin test
5936
- * @returns BigInteger
5937
- * @async
6115
+ * @param bits - Bit length of the prime
6116
+ * @param e - Optional RSA exponent to check against the prime
6117
+ * @param k - Optional number of iterations of Miller-Rabin test
5938
6118
  */
5939
- async function randomProbablePrime(bits, e, k) {
5940
- const BigInteger = await util.getBigInteger();
5941
-
5942
- const one = new BigInteger(1);
5943
- const min = one.leftShift(new BigInteger(bits - 1));
5944
- const thirty = new BigInteger(30);
5945
- /*
5946
- * We can avoid any multiples of 3 and 5 by looking at n mod 30
5947
- * n mod 30 = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
5948
- * the next possible prime is mod 30:
5949
- * 1 7 7 7 7 7 7 11 11 11 11 13 13 17 17 17 17 19 19 23 23 23 23 29 29 29 29 29 29 1
5950
- */
5951
- const adds = [1, 6, 5, 4, 3, 2, 1, 4, 3, 2, 1, 2, 1, 4, 3, 2, 1, 2, 1, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 2];
5952
-
5953
- const n = await getRandomBigInteger(min, min.leftShift(one));
5954
- let i = n.mod(thirty).toNumber();
5955
-
5956
- do {
5957
- n.iadd(new BigInteger(adds[i]));
5958
- i = (i + adds[i]) % adds.length;
5959
- // If reached the maximum, go back to the minimum.
5960
- if (n.bitLength() > bits) {
5961
- n.imod(min.leftShift(one)).iadd(min);
5962
- i = n.mod(thirty).toNumber();
5963
- }
5964
- } while (!await isProbablePrime(n, e, k));
5965
- return n;
6119
+ function randomProbablePrime(bits, e, k) {
6120
+ const _30n = BigInt(30);
6121
+ const min = _1n$3 << BigInt(bits - 1);
6122
+ /*
6123
+ * We can avoid any multiples of 3 and 5 by looking at n mod 30
6124
+ * n mod 30 = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
6125
+ * the next possible prime is mod 30:
6126
+ * 1 7 7 7 7 7 7 11 11 11 11 13 13 17 17 17 17 19 19 23 23 23 23 29 29 29 29 29 29 1
6127
+ */
6128
+ const adds = [1, 6, 5, 4, 3, 2, 1, 4, 3, 2, 1, 2, 1, 4, 3, 2, 1, 2, 1, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 2];
6129
+ let n = getRandomBigInteger(min, min << _1n$3);
6130
+ let i = bigIntToNumber(mod$1(n, _30n));
6131
+ do {
6132
+ n += BigInt(adds[i]);
6133
+ i = (i + adds[i]) % adds.length;
6134
+ // If reached the maximum, go back to the minimum.
6135
+ if (bitLength(n) > bits) {
6136
+ n = mod$1(n, min << _1n$3);
6137
+ n += min;
6138
+ i = bigIntToNumber(mod$1(n, _30n));
6139
+ }
6140
+ } while (!isProbablePrime(n, e, k));
6141
+ return n;
5966
6142
  }
5967
-
5968
6143
  /**
5969
6144
  * Probabilistic primality testing
5970
- * @param {BigInteger} n - Number to test
5971
- * @param {BigInteger} e - Optional RSA exponent to check against the prime
5972
- * @param {Integer} k - Optional number of iterations of Miller-Rabin test
5973
- * @returns {boolean}
5974
- * @async
6145
+ * @param n - Number to test
6146
+ * @param e - Optional RSA exponent to check against the prime
6147
+ * @param k - Optional number of iterations of Miller-Rabin test
5975
6148
  */
5976
- async function isProbablePrime(n, e, k) {
5977
- if (e && !n.dec().gcd(e).isOne()) {
5978
- return false;
5979
- }
5980
- if (!await divisionTest(n)) {
5981
- return false;
5982
- }
5983
- if (!await fermat(n)) {
5984
- return false;
5985
- }
5986
- if (!await millerRabin(n, k)) {
5987
- return false;
5988
- }
5989
- // TODO implement the Lucas test
5990
- // See Section C.3.3 here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
5991
- return true;
6149
+ function isProbablePrime(n, e, k) {
6150
+ if (e && gcd(n - _1n$3, e) !== _1n$3) {
6151
+ return false;
6152
+ }
6153
+ if (!divisionTest(n)) {
6154
+ return false;
6155
+ }
6156
+ if (!fermat(n)) {
6157
+ return false;
6158
+ }
6159
+ if (!millerRabin(n, k)) {
6160
+ return false;
6161
+ }
6162
+ // TODO implement the Lucas test
6163
+ // See Section C.3.3 here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
6164
+ return true;
5992
6165
  }
5993
-
5994
6166
  /**
5995
6167
  * Tests whether n is probably prime or not using Fermat's test with b = 2.
5996
6168
  * Fails if b^(n-1) mod n != 1.
5997
- * @param {BigInteger} n - Number to test
5998
- * @param {BigInteger} b - Optional Fermat test base
5999
- * @returns {boolean}
6169
+ * @param n - Number to test
6170
+ * @param b - Optional Fermat test base
6000
6171
  */
6001
- async function fermat(n, b) {
6002
- const BigInteger = await util.getBigInteger();
6003
-
6004
- b = b || new BigInteger(2);
6005
- return b.modExp(n.dec(), n).isOne();
6172
+ function fermat(n, b = BigInt(2)) {
6173
+ return modExp(b, n - _1n$3, n) === _1n$3;
6006
6174
  }
6007
-
6008
- async function divisionTest(n) {
6009
- const BigInteger = await util.getBigInteger();
6010
-
6011
- return smallPrimes.every(m => {
6012
- return n.mod(new BigInteger(m)) !== 0;
6013
- });
6175
+ function divisionTest(n) {
6176
+ const _0n = BigInt(0);
6177
+ return smallPrimes.every(m => mod$1(n, m) !== _0n);
6014
6178
  }
6015
-
6016
6179
  // https://github.com/gpg/libgcrypt/blob/master/cipher/primegen.c
6017
6180
  const smallPrimes = [
6018
- 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
6019
- 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
6020
- 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
6021
- 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
6022
- 211, 223, 227, 229, 233, 239, 241, 251, 257, 263,
6023
- 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
6024
- 331, 337, 347, 349, 353, 359, 367, 373, 379, 383,
6025
- 389, 397, 401, 409, 419, 421, 431, 433, 439, 443,
6026
- 449, 457, 461, 463, 467, 479, 487, 491, 499, 503,
6027
- 509, 521, 523, 541, 547, 557, 563, 569, 571, 577,
6028
- 587, 593, 599, 601, 607, 613, 617, 619, 631, 641,
6029
- 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
6030
- 709, 719, 727, 733, 739, 743, 751, 757, 761, 769,
6031
- 773, 787, 797, 809, 811, 821, 823, 827, 829, 839,
6032
- 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
6033
- 919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
6034
- 991, 997, 1009, 1013, 1019, 1021, 1031, 1033,
6035
- 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091,
6036
- 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151,
6037
- 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213,
6038
- 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277,
6039
- 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307,
6040
- 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399,
6041
- 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451,
6042
- 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493,
6043
- 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559,
6044
- 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609,
6045
- 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667,
6046
- 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733,
6047
- 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789,
6048
- 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871,
6049
- 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931,
6050
- 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997,
6051
- 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053,
6052
- 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111,
6053
- 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161,
6054
- 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243,
6055
- 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297,
6056
- 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357,
6057
- 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411,
6058
- 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473,
6059
- 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551,
6060
- 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633,
6061
- 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687,
6062
- 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729,
6063
- 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791,
6064
- 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851,
6065
- 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917,
6066
- 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999,
6067
- 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061,
6068
- 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137,
6069
- 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209,
6070
- 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271,
6071
- 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331,
6072
- 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391,
6073
- 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467,
6074
- 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533,
6075
- 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583,
6076
- 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643,
6077
- 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709,
6078
- 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779,
6079
- 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851,
6080
- 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917,
6081
- 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989,
6082
- 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049,
6083
- 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111,
6084
- 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177,
6085
- 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243,
6086
- 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297,
6087
- 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391,
6088
- 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457,
6089
- 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519,
6090
- 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597,
6091
- 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657,
6092
- 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729,
6093
- 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799,
6094
- 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889,
6095
- 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951,
6096
- 4957, 4967, 4969, 4973, 4987, 4993, 4999
6097
- ];
6098
-
6099
-
6181
+ 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
6182
+ 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
6183
+ 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
6184
+ 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
6185
+ 211, 223, 227, 229, 233, 239, 241, 251, 257, 263,
6186
+ 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
6187
+ 331, 337, 347, 349, 353, 359, 367, 373, 379, 383,
6188
+ 389, 397, 401, 409, 419, 421, 431, 433, 439, 443,
6189
+ 449, 457, 461, 463, 467, 479, 487, 491, 499, 503,
6190
+ 509, 521, 523, 541, 547, 557, 563, 569, 571, 577,
6191
+ 587, 593, 599, 601, 607, 613, 617, 619, 631, 641,
6192
+ 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
6193
+ 709, 719, 727, 733, 739, 743, 751, 757, 761, 769,
6194
+ 773, 787, 797, 809, 811, 821, 823, 827, 829, 839,
6195
+ 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
6196
+ 919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
6197
+ 991, 997, 1009, 1013, 1019, 1021, 1031, 1033,
6198
+ 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091,
6199
+ 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151,
6200
+ 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213,
6201
+ 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277,
6202
+ 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307,
6203
+ 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399,
6204
+ 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451,
6205
+ 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493,
6206
+ 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559,
6207
+ 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609,
6208
+ 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667,
6209
+ 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733,
6210
+ 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789,
6211
+ 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871,
6212
+ 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931,
6213
+ 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997,
6214
+ 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053,
6215
+ 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111,
6216
+ 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161,
6217
+ 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243,
6218
+ 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297,
6219
+ 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357,
6220
+ 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411,
6221
+ 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473,
6222
+ 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551,
6223
+ 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633,
6224
+ 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687,
6225
+ 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729,
6226
+ 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791,
6227
+ 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851,
6228
+ 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917,
6229
+ 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999,
6230
+ 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061,
6231
+ 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137,
6232
+ 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209,
6233
+ 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271,
6234
+ 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331,
6235
+ 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391,
6236
+ 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467,
6237
+ 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533,
6238
+ 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583,
6239
+ 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643,
6240
+ 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709,
6241
+ 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779,
6242
+ 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851,
6243
+ 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917,
6244
+ 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989,
6245
+ 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049,
6246
+ 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111,
6247
+ 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177,
6248
+ 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243,
6249
+ 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297,
6250
+ 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391,
6251
+ 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457,
6252
+ 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519,
6253
+ 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597,
6254
+ 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657,
6255
+ 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729,
6256
+ 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799,
6257
+ 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889,
6258
+ 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951,
6259
+ 4957, 4967, 4969, 4973, 4987, 4993, 4999
6260
+ ].map(n => BigInt(n));
6100
6261
  // Miller-Rabin - Miller Rabin algorithm for primality test
6101
6262
  // Copyright Fedor Indutny, 2014.
6102
6263
  //
@@ -6120,63 +6281,51 @@ const smallPrimes = [
6120
6281
  // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
6121
6282
  // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
6122
6283
  // USE OR OTHER DEALINGS IN THE SOFTWARE.
6123
-
6124
6284
  // Adapted on Jan 2018 from version 4.0.1 at https://github.com/indutny/miller-rabin
6125
-
6126
6285
  // Sample syntax for Fixed-Base Miller-Rabin:
6127
6286
  // millerRabin(n, k, () => new BN(small_primes[Math.random() * small_primes.length | 0]))
6128
-
6129
6287
  /**
6130
6288
  * Tests whether n is probably prime or not using the Miller-Rabin test.
6131
6289
  * See HAC Remark 4.28.
6132
- * @param {BigInteger} n - Number to test
6133
- * @param {Integer} k - Optional number of iterations of Miller-Rabin test
6134
- * @param {Function} rand - Optional function to generate potential witnesses
6290
+ * @param n - Number to test
6291
+ * @param k - Optional number of iterations of Miller-Rabin test
6292
+ * @param rand - Optional function to generate potential witnesses
6135
6293
  * @returns {boolean}
6136
6294
  * @async
6137
6295
  */
6138
- async function millerRabin(n, k, rand) {
6139
- const BigInteger = await util.getBigInteger();
6140
-
6141
- const len = n.bitLength();
6142
-
6143
- if (!k) {
6144
- k = Math.max(1, (len / 48) | 0);
6145
- }
6146
-
6147
- const n1 = n.dec(); // n - 1
6148
-
6149
- // Find d and s, (n - 1) = (2 ^ s) * d;
6150
- let s = 0;
6151
- while (!n1.getBit(s)) { s++; }
6152
- const d = n.rightShift(new BigInteger(s));
6153
-
6154
- for (; k > 0; k--) {
6155
- const a = rand ? rand() : await getRandomBigInteger(new BigInteger(2), n1);
6156
-
6157
- let x = a.modExp(d, n);
6158
- if (x.isOne() || x.equal(n1)) {
6159
- continue;
6296
+ function millerRabin(n, k, rand) {
6297
+ const len = bitLength(n);
6298
+ if (!k) {
6299
+ k = Math.max(1, (len / 48) | 0);
6160
6300
  }
6161
-
6162
- let i;
6163
- for (i = 1; i < s; i++) {
6164
- x = x.mul(x).mod(n);
6165
-
6166
- if (x.isOne()) {
6167
- return false;
6168
- }
6169
- if (x.equal(n1)) {
6170
- break;
6171
- }
6172
- }
6173
-
6174
- if (i === s) {
6175
- return false;
6301
+ const n1 = n - _1n$3; // n - 1
6302
+ // Find d and s, (n - 1) = (2 ^ s) * d;
6303
+ let s = 0;
6304
+ while (!getBit(n1, s)) {
6305
+ s++;
6306
+ }
6307
+ const d = n >> BigInt(s);
6308
+ for (; k > 0; k--) {
6309
+ const a = getRandomBigInteger(BigInt(2), n1);
6310
+ let x = modExp(a, d, n);
6311
+ if (x === _1n$3 || x === n1) {
6312
+ continue;
6313
+ }
6314
+ let i;
6315
+ for (i = 1; i < s; i++) {
6316
+ x = mod$1(x * x, n);
6317
+ if (x === _1n$3) {
6318
+ return false;
6319
+ }
6320
+ if (x === n1) {
6321
+ break;
6322
+ }
6323
+ }
6324
+ if (i === s) {
6325
+ return false;
6326
+ }
6176
6327
  }
6177
- }
6178
-
6179
- return true;
6328
+ return true;
6180
6329
  }
6181
6330
 
6182
6331
  // GPG4Browsers - An OpenPGP implementation in javascript
@@ -6302,7 +6451,7 @@ function emeDecode(encoded, randomPayload) {
6302
6451
  * @param {Integer} emLen - Intended length in octets of the encoded message
6303
6452
  * @returns {Uint8Array} Encoded message.
6304
6453
  */
6305
- async function emsaEncode(algo, hashed, emLen) {
6454
+ function emsaEncode(algo, hashed, emLen) {
6306
6455
  let i;
6307
6456
  if (hashed.length !== hash.getHashByteLength(algo)) {
6308
6457
  throw new Error('Invalid hash length');
@@ -6359,6 +6508,7 @@ var pkcs1 = /*#__PURE__*/Object.freeze({
6359
6508
 
6360
6509
  const webCrypto$6 = util.getWebCrypto();
6361
6510
  const nodeCrypto$4 = util.getNodeCrypto();
6511
+ const _1n$2 = BigInt(1);
6362
6512
 
6363
6513
  /** Create signature
6364
6514
  * @param {module:enums.hash} hashAlgo - Hash algorithm
@@ -6472,16 +6622,14 @@ async function decrypt$4(data, n, e, d, p, q, u, randomPayload) {
6472
6622
  * @async
6473
6623
  */
6474
6624
  async function generate$5(bits, e) {
6475
- const BigInteger = await util.getBigInteger();
6476
-
6477
- e = new BigInteger(e);
6625
+ e = BigInt(e);
6478
6626
 
6479
6627
  // Native RSA keygen using Web Crypto
6480
6628
  if (util.getWebCrypto()) {
6481
6629
  const keyGenOpt = {
6482
6630
  name: 'RSASSA-PKCS1-v1_5',
6483
6631
  modulusLength: bits, // the specified keysize in bits
6484
- publicExponent: e.toUint8Array(), // take three bytes (max 65537) for exponent
6632
+ publicExponent: bigIntToUint8Array(e), // take three bytes (max 65537) for exponent
6485
6633
  hash: {
6486
6634
  name: 'SHA-1' // not required for actual RSA keys, but for crypto api 'sign' and 'verify'
6487
6635
  }
@@ -6496,7 +6644,7 @@ async function generate$5(bits, e) {
6496
6644
  } else if (util.getNodeCrypto()) {
6497
6645
  const opts = {
6498
6646
  modulusLength: bits,
6499
- publicExponent: e.toNumber(),
6647
+ publicExponent: bigIntToNumber(e),
6500
6648
  publicKeyEncoding: { type: 'pkcs1', format: 'jwk' },
6501
6649
  privateKeyEncoding: { type: 'pkcs1', format: 'jwk' }
6502
6650
  };
@@ -6519,26 +6667,26 @@ async function generate$5(bits, e) {
6519
6667
  let q;
6520
6668
  let n;
6521
6669
  do {
6522
- q = await randomProbablePrime(bits - (bits >> 1), e, 40);
6523
- p = await randomProbablePrime(bits >> 1, e, 40);
6524
- n = p.mul(q);
6525
- } while (n.bitLength() !== bits);
6670
+ q = randomProbablePrime(bits - (bits >> 1), e, 40);
6671
+ p = randomProbablePrime(bits >> 1, e, 40);
6672
+ n = p * q;
6673
+ } while (bitLength(n) !== bits);
6526
6674
 
6527
- const phi = p.dec().imul(q.dec());
6675
+ const phi = (p - _1n$2) * (q - _1n$2);
6528
6676
 
6529
- if (q.lt(p)) {
6677
+ if (q < p) {
6530
6678
  [p, q] = [q, p];
6531
6679
  }
6532
6680
 
6533
6681
  return {
6534
- n: n.toUint8Array(),
6535
- e: e.toUint8Array(),
6536
- d: e.modInv(phi).toUint8Array(),
6537
- p: p.toUint8Array(),
6538
- q: q.toUint8Array(),
6682
+ n: bigIntToUint8Array(n),
6683
+ e: bigIntToUint8Array(e),
6684
+ d: bigIntToUint8Array(modInv(e, phi)),
6685
+ p: bigIntToUint8Array(p),
6686
+ q: bigIntToUint8Array(q),
6539
6687
  // dp: d.mod(p.subn(1)),
6540
6688
  // dq: d.mod(q.subn(1)),
6541
- u: p.modInv(q).toUint8Array()
6689
+ u: bigIntToUint8Array(modInv(p, q))
6542
6690
  };
6543
6691
  }
6544
6692
 
@@ -6554,26 +6702,24 @@ async function generate$5(bits, e) {
6554
6702
  * @async
6555
6703
  */
6556
6704
  async function validateParams$8(n, e, d, p, q, u) {
6557
- const BigInteger = await util.getBigInteger();
6558
-
6559
- n = new BigInteger(n);
6560
- p = new BigInteger(p);
6561
- q = new BigInteger(q);
6705
+ n = uint8ArrayToBigInt(n);
6706
+ p = uint8ArrayToBigInt(p);
6707
+ q = uint8ArrayToBigInt(q);
6562
6708
 
6563
6709
  // expect pq = n
6564
- if (!p.mul(q).equal(n)) {
6710
+ if ((p * q) !== n) {
6565
6711
  return false;
6566
6712
  }
6567
6713
 
6568
- const two = new BigInteger(2);
6714
+ const _2n = BigInt(2);
6569
6715
  // expect p*u = 1 mod q
6570
- u = new BigInteger(u);
6571
- if (!p.mul(u).mod(q).isOne()) {
6716
+ u = uint8ArrayToBigInt(u);
6717
+ if (mod$1(p * u, q) !== BigInt(1)) {
6572
6718
  return false;
6573
6719
  }
6574
6720
 
6575
- e = new BigInteger(e);
6576
- d = new BigInteger(d);
6721
+ e = uint8ArrayToBigInt(e);
6722
+ d = uint8ArrayToBigInt(d);
6577
6723
  /**
6578
6724
  * In RSA pkcs#1 the exponents (d, e) are inverses modulo lcm(p-1, q-1)
6579
6725
  * We check that [de = 1 mod (p-1)] and [de = 1 mod (q-1)]
@@ -6581,11 +6727,11 @@ async function validateParams$8(n, e, d, p, q, u) {
6581
6727
  *
6582
6728
  * We blind the multiplication with r, and check that rde = r mod lcm(p-1, q-1)
6583
6729
  */
6584
- const nSizeOver3 = new BigInteger(Math.floor(n.bitLength() / 3));
6585
- const r = await getRandomBigInteger(two, two.leftShift(nSizeOver3)); // r in [ 2, 2^{|n|/3} ) < p and q
6586
- const rde = r.mul(d).mul(e);
6730
+ const nSizeOver3 = BigInt(Math.floor(bitLength(n) / 3));
6731
+ const r = getRandomBigInteger(_2n, _2n << nSizeOver3); // r in [ 2, 2^{|n|/3} ) < p and q
6732
+ const rde = r * d * e;
6587
6733
 
6588
- const areInverses = rde.mod(p.dec()).equal(r) && rde.mod(q.dec()).equal(r);
6734
+ const areInverses = mod$1(rde, p - _1n$2) === r && mod$1(rde, q - _1n$2) === r;
6589
6735
  if (!areInverses) {
6590
6736
  return false;
6591
6737
  }
@@ -6594,15 +6740,13 @@ async function validateParams$8(n, e, d, p, q, u) {
6594
6740
  }
6595
6741
 
6596
6742
  async function bnSign(hashAlgo, n, d, hashed) {
6597
- const BigInteger = await util.getBigInteger();
6598
-
6599
- n = new BigInteger(n);
6600
- const m = new BigInteger(await emsaEncode(hashAlgo, hashed, n.byteLength()));
6601
- d = new BigInteger(d);
6602
- if (m.gte(n)) {
6743
+ n = uint8ArrayToBigInt(n);
6744
+ const m = uint8ArrayToBigInt(emsaEncode(hashAlgo, hashed, byteLength(n)));
6745
+ d = uint8ArrayToBigInt(d);
6746
+ if (m >= n) {
6603
6747
  throw new Error('Message size cannot exceed modulus size');
6604
6748
  }
6605
- return m.modExp(d, n).toUint8Array('be', n.byteLength());
6749
+ return bigIntToUint8Array(modExp(m, d, n), 'be', byteLength(n));
6606
6750
  }
6607
6751
 
6608
6752
  async function webSign$1(hashName, data, n, e, d, p, q, u) {
@@ -6632,16 +6776,14 @@ async function nodeSign$1(hashAlgo, data, n, e, d, p, q, u) {
6632
6776
  }
6633
6777
 
6634
6778
  async function bnVerify(hashAlgo, s, n, e, hashed) {
6635
- const BigInteger = await util.getBigInteger();
6636
-
6637
- n = new BigInteger(n);
6638
- s = new BigInteger(s);
6639
- e = new BigInteger(e);
6640
- if (s.gte(n)) {
6779
+ n = uint8ArrayToBigInt(n);
6780
+ s = uint8ArrayToBigInt(s);
6781
+ e = uint8ArrayToBigInt(e);
6782
+ if (s >= n) {
6641
6783
  throw new Error('Signature size cannot exceed modulus size');
6642
6784
  }
6643
- const EM1 = s.modExp(e, n).toUint8Array('be', n.byteLength());
6644
- const EM2 = await emsaEncode(hashAlgo, hashed, n.byteLength());
6785
+ const EM1 = bigIntToUint8Array(modExp(s, e, n), 'be', byteLength(n));
6786
+ const EM2 = emsaEncode(hashAlgo, hashed, byteLength(n));
6645
6787
  return util.equalsUint8Array(EM1, EM2);
6646
6788
  }
6647
6789
 
@@ -6663,7 +6805,7 @@ async function nodeVerify$1(hashAlgo, data, s, n, e) {
6663
6805
  verify.end();
6664
6806
 
6665
6807
  try {
6666
- return await verify.verify(key, s);
6808
+ return verify.verify(key, s);
6667
6809
  } catch (err) {
6668
6810
  return false;
6669
6811
  }
@@ -6677,15 +6819,13 @@ async function nodeEncrypt(data, n, e) {
6677
6819
  }
6678
6820
 
6679
6821
  async function bnEncrypt(data, n, e) {
6680
- const BigInteger = await util.getBigInteger();
6681
-
6682
- n = new BigInteger(n);
6683
- data = new BigInteger(emeEncode(data, n.byteLength()));
6684
- e = new BigInteger(e);
6685
- if (data.gte(n)) {
6822
+ n = uint8ArrayToBigInt(n);
6823
+ data = uint8ArrayToBigInt(emeEncode(data, byteLength(n)));
6824
+ e = uint8ArrayToBigInt(e);
6825
+ if (data >= n) {
6686
6826
  throw new Error('Message size cannot exceed modulus size');
6687
6827
  }
6688
- return data.modExp(e, n).toUint8Array('be', n.byteLength());
6828
+ return bigIntToUint8Array(modExp(data, e, n), 'be', byteLength(n));
6689
6829
  }
6690
6830
 
6691
6831
  async function nodeDecrypt(data, n, e, d, p, q, u) {
@@ -6700,36 +6840,32 @@ async function nodeDecrypt(data, n, e, d, p, q, u) {
6700
6840
  }
6701
6841
 
6702
6842
  async function bnDecrypt(data, n, e, d, p, q, u, randomPayload) {
6703
- const BigInteger = await util.getBigInteger();
6704
-
6705
- data = new BigInteger(data);
6706
- n = new BigInteger(n);
6707
- e = new BigInteger(e);
6708
- d = new BigInteger(d);
6709
- p = new BigInteger(p);
6710
- q = new BigInteger(q);
6711
- u = new BigInteger(u);
6712
- if (data.gte(n)) {
6843
+ data = uint8ArrayToBigInt(data);
6844
+ n = uint8ArrayToBigInt(n);
6845
+ e = uint8ArrayToBigInt(e);
6846
+ d = uint8ArrayToBigInt(d);
6847
+ p = uint8ArrayToBigInt(p);
6848
+ q = uint8ArrayToBigInt(q);
6849
+ u = uint8ArrayToBigInt(u);
6850
+ if (data >= n) {
6713
6851
  throw new Error('Data too large.');
6714
6852
  }
6715
- const dq = d.mod(q.dec()); // d mod (q-1)
6716
- const dp = d.mod(p.dec()); // d mod (p-1)
6717
-
6718
- const unblinder = (await getRandomBigInteger(new BigInteger(2), n)).mod(n);
6719
- const blinder = unblinder.modInv(n).modExp(e, n);
6720
- data = data.mul(blinder).mod(n);
6853
+ const dq = mod$1(d, q - _1n$2); // d mod (q-1)
6854
+ const dp = mod$1(d, p - _1n$2); // d mod (p-1)
6721
6855
 
6856
+ const unblinder = getRandomBigInteger(BigInt(2), n);
6857
+ const blinder = modExp(modInv(unblinder, n), e, n);
6858
+ data = mod$1(data * blinder, n);
6722
6859
 
6723
- const mp = data.modExp(dp, p); // data**{d mod (q-1)} mod p
6724
- const mq = data.modExp(dq, q); // data**{d mod (p-1)} mod q
6725
- const h = u.mul(mq.sub(mp)).mod(q); // u * (mq-mp) mod q (operands already < q)
6860
+ const mp = modExp(data, dp, p); // data**{d mod (q-1)} mod p
6861
+ const mq = modExp(data, dq, q); // data**{d mod (p-1)} mod q
6862
+ const h = mod$1(u * (mq - mp), q); // u * (mq-mp) mod q (operands already < q)
6726
6863
 
6727
- let result = h.mul(p).add(mp); // result < n due to relations above
6864
+ let result = h * p + mp; // result < n due to relations above
6728
6865
 
6729
- result = result.mul(unblinder).mod(n);
6866
+ result = mod$1(result * unblinder, n);
6730
6867
 
6731
-
6732
- return emeDecode(result.toUint8Array('be', n.byteLength()), randomPayload);
6868
+ return emeDecode(bigIntToUint8Array(result, 'be', byteLength(n)), randomPayload);
6733
6869
  }
6734
6870
 
6735
6871
  /** Convert Openpgp private key params to jwk key according to
@@ -6743,28 +6879,26 @@ async function bnDecrypt(data, n, e, d, p, q, u, randomPayload) {
6743
6879
  * @param {Uint8Array} u
6744
6880
  */
6745
6881
  async function privateToJWK$1(n, e, d, p, q, u) {
6746
- const BigInteger = await util.getBigInteger();
6747
-
6748
- const pNum = new BigInteger(p);
6749
- const qNum = new BigInteger(q);
6750
- const dNum = new BigInteger(d);
6751
-
6752
- let dq = dNum.mod(qNum.dec()); // d mod (q-1)
6753
- let dp = dNum.mod(pNum.dec()); // d mod (p-1)
6754
- dp = dp.toUint8Array();
6755
- dq = dq.toUint8Array();
6882
+ const pNum = uint8ArrayToBigInt(p);
6883
+ const qNum = uint8ArrayToBigInt(q);
6884
+ const dNum = uint8ArrayToBigInt(d);
6885
+
6886
+ let dq = mod$1(dNum, qNum - _1n$2); // d mod (q-1)
6887
+ let dp = mod$1(dNum, pNum - _1n$2); // d mod (p-1)
6888
+ dp = bigIntToUint8Array(dp);
6889
+ dq = bigIntToUint8Array(dq);
6756
6890
  return {
6757
6891
  kty: 'RSA',
6758
- n: uint8ArrayToB64(n, true),
6759
- e: uint8ArrayToB64(e, true),
6760
- d: uint8ArrayToB64(d, true),
6892
+ n: uint8ArrayToB64(n),
6893
+ e: uint8ArrayToB64(e),
6894
+ d: uint8ArrayToB64(d),
6761
6895
  // switch p and q
6762
- p: uint8ArrayToB64(q, true),
6763
- q: uint8ArrayToB64(p, true),
6896
+ p: uint8ArrayToB64(q),
6897
+ q: uint8ArrayToB64(p),
6764
6898
  // switch dp and dq
6765
- dp: uint8ArrayToB64(dq, true),
6766
- dq: uint8ArrayToB64(dp, true),
6767
- qi: uint8ArrayToB64(u, true),
6899
+ dp: uint8ArrayToB64(dq),
6900
+ dq: uint8ArrayToB64(dp),
6901
+ qi: uint8ArrayToB64(u),
6768
6902
  ext: true
6769
6903
  };
6770
6904
  }
@@ -6778,8 +6912,8 @@ async function privateToJWK$1(n, e, d, p, q, u) {
6778
6912
  function publicToJWK(n, e) {
6779
6913
  return {
6780
6914
  kty: 'RSA',
6781
- n: uint8ArrayToB64(n, true),
6782
- e: uint8ArrayToB64(e, true),
6915
+ n: uint8ArrayToB64(n),
6916
+ e: uint8ArrayToB64(e),
6783
6917
  ext: true
6784
6918
  };
6785
6919
  }
@@ -6788,7 +6922,7 @@ function publicToJWK(n, e) {
6788
6922
  function jwkToPrivate(jwk, e) {
6789
6923
  return {
6790
6924
  n: b64ToUint8Array(jwk.n),
6791
- e: e.toUint8Array(),
6925
+ e: bigIntToUint8Array(e),
6792
6926
  d: b64ToUint8Array(jwk.d),
6793
6927
  // switch p and q
6794
6928
  p: b64ToUint8Array(jwk.q),
@@ -6826,6 +6960,8 @@ var rsa = /*#__PURE__*/Object.freeze({
6826
6960
  // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6827
6961
 
6828
6962
 
6963
+ const _1n$1 = BigInt(1);
6964
+
6829
6965
  /**
6830
6966
  * ElGamal Encryption function
6831
6967
  * Note that in OpenPGP, the message needs to be padded with PKCS#1 (same as RSA)
@@ -6837,21 +6973,19 @@ var rsa = /*#__PURE__*/Object.freeze({
6837
6973
  * @async
6838
6974
  */
6839
6975
  async function encrypt$3(data, p, g, y) {
6840
- const BigInteger = await util.getBigInteger();
6841
-
6842
- p = new BigInteger(p);
6843
- g = new BigInteger(g);
6844
- y = new BigInteger(y);
6976
+ p = uint8ArrayToBigInt(p);
6977
+ g = uint8ArrayToBigInt(g);
6978
+ y = uint8ArrayToBigInt(y);
6845
6979
 
6846
- const padded = emeEncode(data, p.byteLength());
6847
- const m = new BigInteger(padded);
6980
+ const padded = emeEncode(data, byteLength(p));
6981
+ const m = uint8ArrayToBigInt(padded);
6848
6982
 
6849
6983
  // OpenPGP uses a "special" version of ElGamal where g is generator of the full group Z/pZ*
6850
6984
  // hence g has order p-1, and to avoid that k = 0 mod p-1, we need to pick k in [1, p-2]
6851
- const k = await getRandomBigInteger(new BigInteger(1), p.dec());
6985
+ const k = getRandomBigInteger(_1n$1, p - _1n$1);
6852
6986
  return {
6853
- c1: g.modExp(k, p).toUint8Array(),
6854
- c2: y.modExp(k, p).imul(m).imod(p).toUint8Array()
6987
+ c1: bigIntToUint8Array(modExp(g, k, p)),
6988
+ c2: bigIntToUint8Array(mod$1(modExp(y, k, p) * m, p))
6855
6989
  };
6856
6990
  }
6857
6991
 
@@ -6868,15 +7002,13 @@ async function encrypt$3(data, p, g, y) {
6868
7002
  * @async
6869
7003
  */
6870
7004
  async function decrypt$3(c1, c2, p, x, randomPayload) {
6871
- const BigInteger = await util.getBigInteger();
6872
-
6873
- c1 = new BigInteger(c1);
6874
- c2 = new BigInteger(c2);
6875
- p = new BigInteger(p);
6876
- x = new BigInteger(x);
7005
+ c1 = uint8ArrayToBigInt(c1);
7006
+ c2 = uint8ArrayToBigInt(c2);
7007
+ p = uint8ArrayToBigInt(p);
7008
+ x = uint8ArrayToBigInt(x);
6877
7009
 
6878
- const padded = c1.modExp(x, p).modInv(p).imul(c2).imod(p);
6879
- return emeDecode(padded.toUint8Array('be', p.byteLength()), randomPayload);
7010
+ const padded = mod$1(modInv(modExp(c1, x, p), p) * c2, p);
7011
+ return emeDecode(bigIntToUint8Array(padded, 'be', byteLength(p)), randomPayload);
6880
7012
  }
6881
7013
 
6882
7014
  /**
@@ -6889,22 +7021,19 @@ async function decrypt$3(c1, c2, p, x, randomPayload) {
6889
7021
  * @async
6890
7022
  */
6891
7023
  async function validateParams$7(p, g, y, x) {
6892
- const BigInteger = await util.getBigInteger();
7024
+ p = uint8ArrayToBigInt(p);
7025
+ g = uint8ArrayToBigInt(g);
7026
+ y = uint8ArrayToBigInt(y);
6893
7027
 
6894
- p = new BigInteger(p);
6895
- g = new BigInteger(g);
6896
- y = new BigInteger(y);
6897
-
6898
- const one = new BigInteger(1);
6899
7028
  // Check that 1 < g < p
6900
- if (g.lte(one) || g.gte(p)) {
7029
+ if (g <= _1n$1 || g >= p) {
6901
7030
  return false;
6902
7031
  }
6903
7032
 
6904
7033
  // Expect p-1 to be large
6905
- const pSize = new BigInteger(p.bitLength());
6906
- const n1023 = new BigInteger(1023);
6907
- if (pSize.lt(n1023)) {
7034
+ const pSize = BigInt(bitLength(p));
7035
+ const _1023n = BigInt(1023);
7036
+ if (pSize < _1023n) {
6908
7037
  return false;
6909
7038
  }
6910
7039
 
@@ -6912,7 +7041,7 @@ async function validateParams$7(p, g, y, x) {
6912
7041
  * g should have order p-1
6913
7042
  * Check that g ** (p-1) = 1 mod p
6914
7043
  */
6915
- if (!g.modExp(p.dec(), p).isOne()) {
7044
+ if (modExp(g, p - _1n$1, p) !== _1n$1) {
6916
7045
  return false;
6917
7046
  }
6918
7047
 
@@ -6923,14 +7052,15 @@ async function validateParams$7(p, g, y, x) {
6923
7052
  * We just check g**i != 1 for all i up to a threshold
6924
7053
  */
6925
7054
  let res = g;
6926
- const i = new BigInteger(1);
6927
- const threshold = new BigInteger(2).leftShift(new BigInteger(17)); // we want order > threshold
6928
- while (i.lt(threshold)) {
6929
- res = res.mul(g).imod(p);
6930
- if (res.isOne()) {
7055
+ let i = BigInt(1);
7056
+ const _2n = BigInt(2);
7057
+ const threshold = _2n << BigInt(17); // we want order > threshold
7058
+ while (i < threshold) {
7059
+ res = mod$1(res * g, p);
7060
+ if (res === _1n$1) {
6931
7061
  return false;
6932
7062
  }
6933
- i.iinc();
7063
+ i++;
6934
7064
  }
6935
7065
 
6936
7066
  /**
@@ -6939,11 +7069,10 @@ async function validateParams$7(p, g, y, x) {
6939
7069
  *
6940
7070
  * Blinded exponentiation computes g**{r(p-1) + x} to compare to y
6941
7071
  */
6942
- x = new BigInteger(x);
6943
- const two = new BigInteger(2);
6944
- const r = await getRandomBigInteger(two.leftShift(pSize.dec()), two.leftShift(pSize)); // draw r of same size as p-1
6945
- const rqx = p.dec().imul(r).iadd(x);
6946
- if (!y.equal(g.modExp(rqx, p))) {
7072
+ x = uint8ArrayToBigInt(x);
7073
+ const r = getRandomBigInteger(_2n << (pSize - _1n$1), _2n << pSize); // draw r of same size as p-1
7074
+ const rqx = (p - _1n$1) * r + x;
7075
+ if (y !== modExp(g, rqx, p)) {
6947
7076
  return false;
6948
7077
  }
6949
7078
 
@@ -9087,8 +9216,8 @@ function rawPublicToJWK(payloadSize, name, publicKey) {
9087
9216
  const jwk = {
9088
9217
  kty: 'EC',
9089
9218
  crv: name,
9090
- x: uint8ArrayToB64(bufX, true),
9091
- y: uint8ArrayToB64(bufY, true),
9219
+ x: uint8ArrayToB64(bufX),
9220
+ y: uint8ArrayToB64(bufY),
9092
9221
  ext: true
9093
9222
  };
9094
9223
  return jwk;
@@ -9104,7 +9233,7 @@ function rawPublicToJWK(payloadSize, name, publicKey) {
9104
9233
  */
9105
9234
  function privateToJWK(payloadSize, name, publicKey, privateKey) {
9106
9235
  const jwk = rawPublicToJWK(payloadSize, name, publicKey);
9107
- jwk.d = uint8ArrayToB64(privateKey, true);
9236
+ jwk.d = uint8ArrayToB64(privateKey);
9108
9237
  return jwk;
9109
9238
  }
9110
9239
 
@@ -9172,8 +9301,8 @@ async function sign$6(oid, hashAlgo, message, publicKey, privateKey, hashed) {
9172
9301
  // lowS: non-canonical sig: https://stackoverflow.com/questions/74338846/ecdsa-signature-verification-mismatch
9173
9302
  const signature = nobleCurve.sign(hashed, privateKey, { lowS: false });
9174
9303
  return {
9175
- r: signature.r.toUint8Array('be', curve.payloadSize),
9176
- s: signature.s.toUint8Array('be', curve.payloadSize)
9304
+ r: bigIntToUint8Array(signature.r, 'be', curve.payloadSize),
9305
+ s: bigIntToUint8Array(signature.s, 'be', curve.payloadSize)
9177
9306
  };
9178
9307
  }
9179
9308
 
@@ -9256,6 +9385,7 @@ async function validateParams$6(oid, Q, d) {
9256
9385
  const hashed = await hash.digest(hashAlgo, message);
9257
9386
  try {
9258
9387
  const signature = await sign$6(oid, hashAlgo, message, Q, d, hashed);
9388
+ // eslint-disable-next-line @typescript-eslint/return-await
9259
9389
  return await verify$6(oid, hashAlgo, signature, message, Q, hashed);
9260
9390
  } catch (err) {
9261
9391
  return false;
@@ -10514,6 +10644,9 @@ var elliptic = /*#__PURE__*/Object.freeze({
10514
10644
  https://tools.ietf.org/html/rfc4880#section-14
10515
10645
  */
10516
10646
 
10647
+ const _0n = BigInt(0);
10648
+ const _1n = BigInt(1);
10649
+
10517
10650
  /**
10518
10651
  * DSA Sign function
10519
10652
  * @param {Integer} hashAlgo
@@ -10526,26 +10659,24 @@ var elliptic = /*#__PURE__*/Object.freeze({
10526
10659
  * @async
10527
10660
  */
10528
10661
  async function sign$3(hashAlgo, hashed, g, p, q, x) {
10529
- const BigInteger = await util.getBigInteger();
10530
-
10531
- const one = new BigInteger(1);
10532
- p = new BigInteger(p);
10533
- q = new BigInteger(q);
10534
- g = new BigInteger(g);
10535
- x = new BigInteger(x);
10662
+ const _0n = BigInt(0);
10663
+ p = uint8ArrayToBigInt(p);
10664
+ q = uint8ArrayToBigInt(q);
10665
+ g = uint8ArrayToBigInt(g);
10666
+ x = uint8ArrayToBigInt(x);
10536
10667
 
10537
10668
  let k;
10538
10669
  let r;
10539
10670
  let s;
10540
10671
  let t;
10541
- g = g.mod(p);
10542
- x = x.mod(q);
10672
+ g = mod$1(g, p);
10673
+ x = mod$1(x, q);
10543
10674
  // If the output size of the chosen hash is larger than the number of
10544
10675
  // bits of q, the hash result is truncated to fit by taking the number
10545
10676
  // of leftmost bits equal to the number of bits of q. This (possibly
10546
10677
  // truncated) hash function result is treated as a number and used
10547
10678
  // directly in the DSA signature algorithm.
10548
- const h = new BigInteger(hashed.subarray(0, q.byteLength())).mod(q);
10679
+ const h = mod$1(uint8ArrayToBigInt(hashed.subarray(0, byteLength(q))), q);
10549
10680
  // FIPS-186-4, section 4.6:
10550
10681
  // The values of r and s shall be checked to determine if r = 0 or s = 0.
10551
10682
  // If either r = 0 or s = 0, a new value of k shall be generated, and the
@@ -10553,22 +10684,22 @@ async function sign$3(hashAlgo, hashed, g, p, q, x) {
10553
10684
  // or s = 0 if signatures are generated properly.
10554
10685
  while (true) {
10555
10686
  // See Appendix B here: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
10556
- k = await getRandomBigInteger(one, q); // returns in [1, q-1]
10557
- r = g.modExp(k, p).imod(q); // (g**k mod p) mod q
10558
- if (r.isZero()) {
10687
+ k = getRandomBigInteger(_1n, q); // returns in [1, q-1]
10688
+ r = mod$1(modExp(g, k, p), q); // (g**k mod p) mod q
10689
+ if (r === _0n) {
10559
10690
  continue;
10560
10691
  }
10561
- const xr = x.mul(r).imod(q);
10562
- t = h.add(xr).imod(q); // H(m) + x*r mod q
10563
- s = k.modInv(q).imul(t).imod(q); // k**-1 * (H(m) + x*r) mod q
10564
- if (s.isZero()) {
10692
+ const xr = mod$1(x * r, q);
10693
+ t = mod$1(h + xr, q); // H(m) + x*r mod q
10694
+ s = mod$1(modInv(k, q) * t, q); // k**-1 * (H(m) + x*r) mod q
10695
+ if (s === _0n) {
10565
10696
  continue;
10566
10697
  }
10567
10698
  break;
10568
10699
  }
10569
10700
  return {
10570
- r: r.toUint8Array('be', q.byteLength()),
10571
- s: s.toUint8Array('be', q.byteLength())
10701
+ r: bigIntToUint8Array(r, 'be', byteLength(p)),
10702
+ s: bigIntToUint8Array(s, 'be', byteLength(p))
10572
10703
  };
10573
10704
  }
10574
10705
 
@@ -10586,37 +10717,34 @@ async function sign$3(hashAlgo, hashed, g, p, q, x) {
10586
10717
  * @async
10587
10718
  */
10588
10719
  async function verify$3(hashAlgo, r, s, hashed, g, p, q, y) {
10589
- const BigInteger = await util.getBigInteger();
10590
-
10591
- const zero = new BigInteger(0);
10592
- r = new BigInteger(r);
10593
- s = new BigInteger(s);
10720
+ r = uint8ArrayToBigInt(r);
10721
+ s = uint8ArrayToBigInt(s);
10594
10722
 
10595
- p = new BigInteger(p);
10596
- q = new BigInteger(q);
10597
- g = new BigInteger(g);
10598
- y = new BigInteger(y);
10723
+ p = uint8ArrayToBigInt(p);
10724
+ q = uint8ArrayToBigInt(q);
10725
+ g = uint8ArrayToBigInt(g);
10726
+ y = uint8ArrayToBigInt(y);
10599
10727
 
10600
- if (r.lte(zero) || r.gte(q) ||
10601
- s.lte(zero) || s.gte(q)) {
10728
+ if (r <= _0n || r >= q ||
10729
+ s <= _0n || s >= q) {
10602
10730
  util.printDebug('invalid DSA Signature');
10603
10731
  return false;
10604
10732
  }
10605
- const h = new BigInteger(hashed.subarray(0, q.byteLength())).imod(q);
10606
- const w = s.modInv(q); // s**-1 mod q
10607
- if (w.isZero()) {
10733
+ const h = mod$1(uint8ArrayToBigInt(hashed.subarray(0, byteLength(q))), q);
10734
+ const w = modInv(s, q); // s**-1 mod q
10735
+ if (w === _0n) {
10608
10736
  util.printDebug('invalid DSA Signature');
10609
10737
  return false;
10610
10738
  }
10611
10739
 
10612
- g = g.mod(p);
10613
- y = y.mod(p);
10614
- const u1 = h.mul(w).imod(q); // H(m) * w mod q
10615
- const u2 = r.mul(w).imod(q); // r * w mod q
10616
- const t1 = g.modExp(u1, p); // g**u1 mod p
10617
- const t2 = y.modExp(u2, p); // y**u2 mod p
10618
- const v = t1.mul(t2).imod(p).imod(q); // (g**u1 * y**u2 mod p) mod q
10619
- return v.equal(r);
10740
+ g = mod$1(g, p);
10741
+ y = mod$1(y, p);
10742
+ const u1 = mod$1(h * w, q); // H(m) * w mod q
10743
+ const u2 = mod$1(r * w, q); // r * w mod q
10744
+ const t1 = modExp(g, u1, p); // g**u1 mod p
10745
+ const t2 = modExp(y, u2, p); // y**u2 mod p
10746
+ const v = mod$1(mod$1(t1 * t2, p), q); // (g**u1 * y**u2 mod p) mod q
10747
+ return v === r;
10620
10748
  }
10621
10749
 
10622
10750
  /**
@@ -10630,22 +10758,19 @@ async function verify$3(hashAlgo, r, s, hashed, g, p, q, y) {
10630
10758
  * @async
10631
10759
  */
10632
10760
  async function validateParams$1(p, q, g, y, x) {
10633
- const BigInteger = await util.getBigInteger();
10634
-
10635
- p = new BigInteger(p);
10636
- q = new BigInteger(q);
10637
- g = new BigInteger(g);
10638
- y = new BigInteger(y);
10639
- const one = new BigInteger(1);
10761
+ p = uint8ArrayToBigInt(p);
10762
+ q = uint8ArrayToBigInt(q);
10763
+ g = uint8ArrayToBigInt(g);
10764
+ y = uint8ArrayToBigInt(y);
10640
10765
  // Check that 1 < g < p
10641
- if (g.lte(one) || g.gte(p)) {
10766
+ if (g <= _1n || g >= p) {
10642
10767
  return false;
10643
10768
  }
10644
10769
 
10645
10770
  /**
10646
10771
  * Check that subgroup order q divides p-1
10647
10772
  */
10648
- if (!p.dec().mod(q).isZero()) {
10773
+ if (mod$1(p - _1n, q) !== _0n) {
10649
10774
  return false;
10650
10775
  }
10651
10776
 
@@ -10653,16 +10778,16 @@ async function validateParams$1(p, q, g, y, x) {
10653
10778
  * g has order q
10654
10779
  * Check that g ** q = 1 mod p
10655
10780
  */
10656
- if (!g.modExp(q, p).isOne()) {
10781
+ if (modExp(g, q, p) !== _1n) {
10657
10782
  return false;
10658
10783
  }
10659
10784
 
10660
10785
  /**
10661
10786
  * Check q is large and probably prime (we mainly want to avoid small factors)
10662
10787
  */
10663
- const qSize = new BigInteger(q.bitLength());
10664
- const n150 = new BigInteger(150);
10665
- if (qSize.lt(n150) || !(await isProbablePrime(q, null, 32))) {
10788
+ const qSize = BigInt(bitLength(q));
10789
+ const _150n = BigInt(150);
10790
+ if (qSize < _150n || !isProbablePrime(q, null, 32)) {
10666
10791
  return false;
10667
10792
  }
10668
10793
 
@@ -10672,11 +10797,11 @@ async function validateParams$1(p, q, g, y, x) {
10672
10797
  *
10673
10798
  * Blinded exponentiation computes g**{rq + x} to compare to y
10674
10799
  */
10675
- x = new BigInteger(x);
10676
- const two = new BigInteger(2);
10677
- const r = await getRandomBigInteger(two.leftShift(qSize.dec()), two.leftShift(qSize)); // draw r of same size as q
10678
- const rqx = q.mul(r).add(x);
10679
- if (!y.equal(g.modExp(rqx, p))) {
10800
+ x = uint8ArrayToBigInt(x);
10801
+ const _2n = BigInt(2);
10802
+ const r = getRandomBigInteger(_2n << (qSize - _1n), _2n << qSize); // draw r of same size as q
10803
+ const rqx = q * r + x;
10804
+ if (y !== modExp(g, rqx, p)) {
10680
10805
  return false;
10681
10806
  }
10682
10807
 
@@ -16049,7 +16174,7 @@ async function runAEAD(packet, fn, key, data) {
16049
16174
  done = true;
16050
16175
  }
16051
16176
  cryptedBytes += chunk.length - tagLengthIfDecrypting;
16052
- // eslint-disable-next-line no-loop-func
16177
+ // eslint-disable-next-line @typescript-eslint/no-loop-func
16053
16178
  latestPromise = latestPromise.then(() => cryptedPromise).then(async crypted => {
16054
16179
  await writer.ready;
16055
16180
  await writer.write(crypted);
@@ -17185,7 +17310,7 @@ class PublicSubkeyPacket extends PublicKeyPacket {
17185
17310
  * @param {Date} [date] - Creation date
17186
17311
  * @param {Object} [config] - Full configuration, defaults to openpgp.config
17187
17312
  */
17188
- // eslint-disable-next-line no-useless-constructor
17313
+ // eslint-disable-next-line @typescript-eslint/no-useless-constructor
17189
17314
  constructor(date, config) {
17190
17315
  super(date, config);
17191
17316
  }
@@ -18090,7 +18215,7 @@ class PaddingPacket {
18090
18215
  * Read a padding packet
18091
18216
  * @param {Uint8Array | ReadableStream<Uint8Array>} bytes
18092
18217
  */
18093
- read(bytes) { // eslint-disable-line no-unused-vars
18218
+ read(bytes) { // eslint-disable-line @typescript-eslint/no-unused-vars
18094
18219
  // Padding packets are ignored, so this function is never called.
18095
18220
  }
18096
18221
 
@@ -18374,7 +18499,7 @@ async function getPreferredCompressionAlgo(keys = [], date = new Date(), userIDs
18374
18499
  async function getPreferredCipherSuite(keys = [], date = new Date(), userIDs = [], config$1 = config) {
18375
18500
  const selfSigs = await Promise.all(keys.map((key, i) => key.getPrimarySelfSignature(date, userIDs[i], config$1)));
18376
18501
  const withAEAD = keys.length ?
18377
- selfSigs.every(selfSig => selfSig.features && (selfSig.features[0] & enums.features.seipdv2)) :
18502
+ !config$1.ignoreSEIPDv2FeatureFlag && selfSigs.every(selfSig => selfSig.features && (selfSig.features[0] & enums.features.seipdv2)) :
18378
18503
  config$1.aeadProtect;
18379
18504
 
18380
18505
  if (withAEAD) {
@@ -19644,6 +19769,7 @@ class Key {
19644
19769
  }
19645
19770
  }
19646
19771
  if (!users.length) {
19772
+ // eslint-disable-next-line @typescript-eslint/no-throw-literal
19647
19773
  throw exception || new Error('Could not find primary user');
19648
19774
  }
19649
19775
  await Promise.all(users.map(async function (a) {
@@ -20554,7 +20680,12 @@ async function readKey({ armoredKey, binaryKey, config: config$1, ...rest }) {
20554
20680
  input = binaryKey;
20555
20681
  }
20556
20682
  const packetlist = await PacketList.fromBinary(input, allowedKeyPackets, config$1);
20557
- return createKey(packetlist);
20683
+ const keyIndex = packetlist.indexOfTag(enums.packet.publicKey, enums.packet.secretKey);
20684
+ if (keyIndex.length === 0) {
20685
+ throw new Error('No key packet found');
20686
+ }
20687
+ const firstKeyPacketList = packetlist.slice(keyIndex[0], keyIndex[1]);
20688
+ return createKey(firstKeyPacketList);
20558
20689
  }
20559
20690
 
20560
20691
  /**
@@ -20591,7 +20722,15 @@ async function readPrivateKey({ armoredKey, binaryKey, config: config$1, ...rest
20591
20722
  input = binaryKey;
20592
20723
  }
20593
20724
  const packetlist = await PacketList.fromBinary(input, allowedKeyPackets, config$1);
20594
- return new PrivateKey(packetlist);
20725
+ const keyIndex = packetlist.indexOfTag(enums.packet.publicKey, enums.packet.secretKey);
20726
+ for (let i = 0; i < keyIndex.length; i++) {
20727
+ if (packetlist[keyIndex[i]].constructor.tag === enums.packet.publicKey) {
20728
+ continue;
20729
+ }
20730
+ const firstPrivateKeyList = packetlist.slice(keyIndex[i], keyIndex[i + 1]);
20731
+ return new PrivateKey(firstPrivateKeyList);
20732
+ }
20733
+ throw new Error('No secret key packet found');
20595
20734
  }
20596
20735
 
20597
20736
  /**
@@ -20670,15 +20809,18 @@ async function readPrivateKeys({ armoredKeys, binaryKeys, config: config$1 }) {
20670
20809
  }
20671
20810
  const keys = [];
20672
20811
  const packetlist = await PacketList.fromBinary(input, allowedKeyPackets, config$1);
20673
- const keyIndex = packetlist.indexOfTag(enums.packet.secretKey);
20674
- if (keyIndex.length === 0) {
20675
- throw new Error('No secret key packet found');
20676
- }
20812
+ const keyIndex = packetlist.indexOfTag(enums.packet.publicKey, enums.packet.secretKey);
20677
20813
  for (let i = 0; i < keyIndex.length; i++) {
20814
+ if (packetlist[keyIndex[i]].constructor.tag === enums.packet.publicKey) {
20815
+ continue;
20816
+ }
20678
20817
  const oneKeyList = packetlist.slice(keyIndex[i], keyIndex[i + 1]);
20679
20818
  const newKey = new PrivateKey(oneKeyList);
20680
20819
  keys.push(newKey);
20681
20820
  }
20821
+ if (keys.length === 0) {
20822
+ throw new Error('No secret key packet found');
20823
+ }
20682
20824
  return keys;
20683
20825
  }
20684
20826
 
@@ -22092,7 +22234,7 @@ async function encrypt({ message, encryptionKeys, signingKeys, passwords, sessio
22092
22234
  // serialize data
22093
22235
  const armor = format === 'armored';
22094
22236
  const data = armor ? message.armor(config$1) : message.write();
22095
- return convertStream(data);
22237
+ return await convertStream(data);
22096
22238
  } catch (err) {
22097
22239
  throw util.wrapError('Error encrypting message', err);
22098
22240
  }
@@ -22229,7 +22371,7 @@ async function sign({ message, signingKeys, format = 'armored', detached = false
22229
22371
  ]);
22230
22372
  });
22231
22373
  }
22232
- return convertStream(signature);
22374
+ return await convertStream(signature);
22233
22375
  } catch (err) {
22234
22376
  throw util.wrapError('Error signing message', err);
22235
22377
  }
@@ -22412,12 +22554,12 @@ async function decryptSessionKeys({ message, decryptionKeys, passwords, date = n
22412
22554
  */
22413
22555
  function checkString(data, name) {
22414
22556
  if (!util.isString(data)) {
22415
- throw new Error('Parameter [' + (name || 'data') + '] must be of type String');
22557
+ throw new Error('Parameter [' + (name ) + '] must be of type String');
22416
22558
  }
22417
22559
  }
22418
22560
  function checkBinary(data, name) {
22419
22561
  if (!util.isUint8Array(data)) {
22420
- throw new Error('Parameter [' + (name || 'data') + '] must be of type Uint8Array');
22562
+ throw new Error('Parameter [' + ('data') + '] must be of type Uint8Array');
22421
22563
  }
22422
22564
  }
22423
22565
  function checkMessage(message) {