@protontech/openpgp 4.10.5 → 5.3.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 (173) hide show
  1. package/README.md +311 -239
  2. package/dist/lightweight/bn.interface.min.mjs +3 -0
  3. package/dist/lightweight/bn.interface.min.mjs.map +1 -0
  4. package/dist/lightweight/bn.interface.mjs +340 -0
  5. package/dist/lightweight/bn.min.mjs +3 -0
  6. package/dist/lightweight/bn.min.mjs.map +1 -0
  7. package/dist/lightweight/bn.mjs +3434 -0
  8. package/dist/lightweight/elliptic.min.mjs +3 -0
  9. package/dist/lightweight/elliptic.min.mjs.map +1 -0
  10. package/dist/lightweight/elliptic.mjs +4313 -0
  11. package/dist/lightweight/openpgp.min.mjs +3 -0
  12. package/dist/lightweight/openpgp.min.mjs.map +1 -0
  13. package/dist/lightweight/openpgp.mjs +31375 -0
  14. package/dist/lightweight/ponyfill.es6.min.mjs +3 -0
  15. package/dist/lightweight/ponyfill.es6.min.mjs.map +1 -0
  16. package/dist/lightweight/ponyfill.es6.mjs +3831 -0
  17. package/dist/lightweight/web-streams-adapter.min.mjs +17 -0
  18. package/dist/lightweight/web-streams-adapter.min.mjs.map +1 -0
  19. package/dist/lightweight/web-streams-adapter.mjs +561 -0
  20. package/dist/node/openpgp.js +43943 -0
  21. package/dist/node/openpgp.min.js +17 -0
  22. package/dist/node/openpgp.min.js.map +1 -0
  23. package/dist/node/openpgp.min.mjs +17 -0
  24. package/dist/node/openpgp.min.mjs.map +1 -0
  25. package/dist/node/openpgp.mjs +43880 -0
  26. package/dist/openpgp.js +41080 -41565
  27. package/dist/openpgp.min.js +17 -2
  28. package/dist/openpgp.min.js.map +1 -0
  29. package/dist/openpgp.min.mjs +17 -0
  30. package/dist/openpgp.min.mjs.map +1 -0
  31. package/dist/openpgp.mjs +43868 -0
  32. package/lightweight/package.json +5 -0
  33. package/openpgp.d.ts +889 -0
  34. package/package.json +63 -57
  35. package/dist/compat/openpgp.js +0 -61067
  36. package/dist/compat/openpgp.min.js +0 -2
  37. package/dist/compat/openpgp.worker.js +0 -173
  38. package/dist/compat/openpgp.worker.min.js +0 -2
  39. package/dist/lightweight/elliptic.min.js +0 -5
  40. package/dist/lightweight/openpgp.js +0 -40024
  41. package/dist/lightweight/openpgp.min.js +0 -2
  42. package/dist/lightweight/openpgp.worker.js +0 -173
  43. package/dist/lightweight/openpgp.worker.min.js +0 -2
  44. package/dist/openpgp.worker.js +0 -173
  45. package/dist/openpgp.worker.min.js +0 -2
  46. package/src/cleartext.js +0 -220
  47. package/src/config/config.js +0 -224
  48. package/src/config/index.js +0 -7
  49. package/src/config/localStorage.js +0 -35
  50. package/src/crypto/aes_kw.js +0 -153
  51. package/src/crypto/cfb.js +0 -169
  52. package/src/crypto/cipher/aes.js +0 -27
  53. package/src/crypto/cipher/blowfish.js +0 -398
  54. package/src/crypto/cipher/cast5.js +0 -610
  55. package/src/crypto/cipher/des.js +0 -476
  56. package/src/crypto/cipher/index.js +0 -91
  57. package/src/crypto/cipher/twofish.js +0 -346
  58. package/src/crypto/cmac.js +0 -98
  59. package/src/crypto/crypto.js +0 -394
  60. package/src/crypto/eax.js +0 -172
  61. package/src/crypto/gcm.js +0 -141
  62. package/src/crypto/hash/index.js +0 -163
  63. package/src/crypto/hash/md5.js +0 -205
  64. package/src/crypto/index.js +0 -57
  65. package/src/crypto/ocb.js +0 -274
  66. package/src/crypto/pkcs1.js +0 -170
  67. package/src/crypto/pkcs5.js +0 -55
  68. package/src/crypto/public_key/dsa.js +0 -188
  69. package/src/crypto/public_key/elgamal.js +0 -137
  70. package/src/crypto/public_key/elliptic/curves.js +0 -385
  71. package/src/crypto/public_key/elliptic/ecdh.js +0 -414
  72. package/src/crypto/public_key/elliptic/ecdsa.js +0 -348
  73. package/src/crypto/public_key/elliptic/eddsa.js +0 -119
  74. package/src/crypto/public_key/elliptic/index.js +0 -34
  75. package/src/crypto/public_key/elliptic/indutnyKey.js +0 -85
  76. package/src/crypto/public_key/index.js +0 -28
  77. package/src/crypto/public_key/prime.js +0 -275
  78. package/src/crypto/public_key/rsa.js +0 -597
  79. package/src/crypto/random.js +0 -145
  80. package/src/crypto/signature.js +0 -137
  81. package/src/encoding/armor.js +0 -433
  82. package/src/encoding/base64.js +0 -96
  83. package/src/enums.js +0 -493
  84. package/src/hkp.js +0 -89
  85. package/src/index.js +0 -161
  86. package/src/key/factory.js +0 -326
  87. package/src/key/helper.js +0 -363
  88. package/src/key/index.js +0 -32
  89. package/src/key/key.js +0 -890
  90. package/src/key/subkey.js +0 -187
  91. package/src/key/user.js +0 -230
  92. package/src/keyring/index.js +0 -12
  93. package/src/keyring/keyring.js +0 -229
  94. package/src/keyring/localstore.js +0 -119
  95. package/src/lightweight_helper.js +0 -26
  96. package/src/message.js +0 -825
  97. package/src/openpgp.js +0 -717
  98. package/src/packet/all_packets.js +0 -116
  99. package/src/packet/clone.js +0 -189
  100. package/src/packet/compressed.js +0 -194
  101. package/src/packet/index.js +0 -20
  102. package/src/packet/literal.js +0 -168
  103. package/src/packet/marker.js +0 -62
  104. package/src/packet/one_pass_signature.js +0 -156
  105. package/src/packet/packet.js +0 -300
  106. package/src/packet/packetlist.js +0 -232
  107. package/src/packet/public_key.js +0 -280
  108. package/src/packet/public_key_encrypted_session_key.js +0 -156
  109. package/src/packet/public_subkey.js +0 -44
  110. package/src/packet/secret_key.js +0 -448
  111. package/src/packet/secret_subkey.js +0 -41
  112. package/src/packet/signature.js +0 -782
  113. package/src/packet/sym_encrypted_aead_protected.js +0 -189
  114. package/src/packet/sym_encrypted_integrity_protected.js +0 -139
  115. package/src/packet/sym_encrypted_session_key.js +0 -204
  116. package/src/packet/symmetrically_encrypted.js +0 -118
  117. package/src/packet/trust.js +0 -35
  118. package/src/packet/user_attribute.js +0 -94
  119. package/src/packet/userid.js +0 -87
  120. package/src/polyfills.js +0 -64
  121. package/src/signature.js +0 -73
  122. package/src/type/ecdh_symkey.js +0 -69
  123. package/src/type/kdf_params.js +0 -114
  124. package/src/type/keyid.js +0 -110
  125. package/src/type/mpi.js +0 -138
  126. package/src/type/oid.js +0 -110
  127. package/src/type/s2k.js +0 -203
  128. package/src/util.js +0 -836
  129. package/src/wkd.js +0 -88
  130. package/src/worker/async_proxy.js +0 -190
  131. package/src/worker/worker.js +0 -167
  132. package/test/crypto/aes_kw.js +0 -57
  133. package/test/crypto/cipher/aes.js +0 -86
  134. package/test/crypto/cipher/blowfish.js +0 -58
  135. package/test/crypto/cipher/cast5.js +0 -25
  136. package/test/crypto/cipher/des.js +0 -143
  137. package/test/crypto/cipher/index.js +0 -7
  138. package/test/crypto/cipher/twofish.js +0 -71
  139. package/test/crypto/crypto.js +0 -383
  140. package/test/crypto/eax.js +0 -150
  141. package/test/crypto/ecdh.js +0 -359
  142. package/test/crypto/elliptic.js +0 -251
  143. package/test/crypto/elliptic_data.js +0 -102
  144. package/test/crypto/hash/index.js +0 -5
  145. package/test/crypto/hash/md5.js +0 -16
  146. package/test/crypto/hash/ripemd.js +0 -14
  147. package/test/crypto/hash/sha.js +0 -20
  148. package/test/crypto/index.js +0 -14
  149. package/test/crypto/ocb.js +0 -183
  150. package/test/crypto/pkcs5.js +0 -39
  151. package/test/crypto/random.js +0 -79
  152. package/test/crypto/rsa.js +0 -180
  153. package/test/crypto/validate.js +0 -387
  154. package/test/general/armor.js +0 -408
  155. package/test/general/brainpool.js +0 -360
  156. package/test/general/decompression.js +0 -60
  157. package/test/general/ecc_nist.js +0 -115
  158. package/test/general/ecc_secp256k1.js +0 -242
  159. package/test/general/forwarding.js +0 -43
  160. package/test/general/hkp.js +0 -165
  161. package/test/general/index.js +0 -20
  162. package/test/general/key.js +0 -3402
  163. package/test/general/keyring.js +0 -336
  164. package/test/general/oid.js +0 -39
  165. package/test/general/openpgp.js +0 -2542
  166. package/test/general/packet.js +0 -937
  167. package/test/general/signature.js +0 -1665
  168. package/test/general/streaming.js +0 -944
  169. package/test/general/testInputs.js +0 -18
  170. package/test/general/util.js +0 -183
  171. package/test/general/wkd.js +0 -48
  172. package/test/general/x25519.js +0 -556
  173. package/test/unittests.js +0 -64
@@ -1,137 +0,0 @@
1
- // GPG4Browsers - An OpenPGP implementation in javascript
2
- // Copyright (C) 2011 Recurity Labs GmbH
3
- //
4
- // This library is free software; you can redistribute it and/or
5
- // modify it under the terms of the GNU Lesser General Public
6
- // License as published by the Free Software Foundation; either
7
- // version 3.0 of the License, or (at your option) any later version.
8
- //
9
- // This library is distributed in the hope that it will be useful,
10
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
- // Lesser General Public License for more details.
13
- //
14
- // You should have received a copy of the GNU Lesser General Public
15
- // License along with this library; if not, write to the Free Software
16
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
-
18
- /**
19
- * @fileoverview ElGamal implementation
20
- * @requires bn.js
21
- * @requires crypto/random
22
- * @module crypto/public_key/elgamal
23
- */
24
-
25
- import BN from 'bn.js';
26
- import random from '../random';
27
-
28
- const zero = new BN(0);
29
-
30
- export default {
31
- /**
32
- * ElGamal Encryption function
33
- * @param {BN} m
34
- * @param {BN} p
35
- * @param {BN} g
36
- * @param {BN} y
37
- * @returns {{ c1: BN, c2: BN }}
38
- * @async
39
- */
40
- encrypt: async function(m, p, g, y) {
41
- const redp = new BN.red(p);
42
- const mred = m.toRed(redp);
43
- const gred = g.toRed(redp);
44
- const yred = y.toRed(redp);
45
- // See Section 11.5 here: https://crypto.stanford.edu/~dabo/cryptobook/BonehShoup_0_4.pdf
46
- const k = await random.getRandomBN(zero, p); // returns in [0, p-1]
47
- return {
48
- c1: gred.redPow(k).fromRed(),
49
- c2: yred.redPow(k).redMul(mred).fromRed()
50
- };
51
- },
52
-
53
- /**
54
- * ElGamal Encryption function
55
- * @param {BN} c1
56
- * @param {BN} c2
57
- * @param {BN} p
58
- * @param {BN} x
59
- * @returns BN
60
- * @async
61
- */
62
- decrypt: async function(c1, c2, p, x) {
63
- const redp = new BN.red(p);
64
- const c1red = c1.toRed(redp);
65
- const c2red = c2.toRed(redp);
66
- return c1red.redPow(x).redInvm().redMul(c2red).fromRed();
67
- },
68
-
69
- /**
70
- * Validate ElGamal parameters
71
- * @param {Uint8Array} p ElGamal prime
72
- * @param {Uint8Array} g ElGamal group generator
73
- * @param {Uint8Array} y ElGamal public key
74
- * @param {Uint8Array} x ElGamal private exponent
75
- * @returns {Promise<Boolean>} whether params are valid
76
- * @async
77
- */
78
- validateParams: async function (p, g, y, x) {
79
- p = new BN(p);
80
- g = new BN(g);
81
- y = new BN(y);
82
-
83
- const one = new BN(1);
84
- // Check that 1 < g < p
85
- if (g.lte(one) || g.gte(p)) {
86
- return false;
87
- }
88
-
89
- // Expect p-1 to be large
90
- const pSize = p.subn(1).bitLength();
91
- if (pSize < 1023) {
92
- return false;
93
- }
94
-
95
- const pred = new BN.red(p);
96
- const gModP = g.toRed(pred);
97
- /**
98
- * g should have order p-1
99
- * Check that g ** (p-1) = 1 mod p
100
- */
101
- if (!gModP.redPow(p.subn(1)).eq(one)) {
102
- return false;
103
- }
104
-
105
- /**
106
- * Since p-1 is not prime, g might have a smaller order that divides p-1
107
- * We want to make sure that the order is large enough to hinder a small subgroup attack
108
- *
109
- * We just check g**i != 1 for all i up to a threshold
110
- */
111
- let res = g;
112
- const i = new BN(1);
113
- const threshold = new BN(2).shln(17); // we want order > threshold
114
- while (i.lt(threshold)) {
115
- res = res.mul(g).mod(p);
116
- if (res.eqn(1)) {
117
- return false;
118
- }
119
- i.iaddn(1);
120
- }
121
-
122
- /**
123
- * Re-derive public key y' = g ** x mod p
124
- * Expect y == y'
125
- *
126
- * Blinded exponentiation computes g**{r(p-1) + x} to compare to y
127
- */
128
- x = new BN(x);
129
- const r = await random.getRandomBN(new BN(2).shln(pSize - 1), new BN(2).shln(pSize)); // draw r of same size as p-1
130
- const rqx = p.subn(1).mul(r).add(x);
131
- if (!y.eq(gModP.redPow(rqx))) {
132
- return false;
133
- }
134
-
135
- return true;
136
- }
137
- };
@@ -1,385 +0,0 @@
1
- // OpenPGP.js - An OpenPGP implementation in javascript
2
- // Copyright (C) 2015-2016 Decentral
3
- //
4
- // This library is free software; you can redistribute it and/or
5
- // modify it under the terms of the GNU Lesser General Public
6
- // License as published by the Free Software Foundation; either
7
- // version 3.0 of the License, or (at your option) any later version.
8
- //
9
- // This library is distributed in the hope that it will be useful,
10
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
- // Lesser General Public License for more details.
13
- //
14
- // You should have received a copy of the GNU Lesser General Public
15
- // License along with this library; if not, write to the Free Software
16
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
-
18
- /**
19
- * @fileoverview Wrapper of an instance of an Elliptic Curve
20
- * @requires bn.js
21
- * @requires tweetnacl
22
- * @requires crypto/public_key/elliptic/key
23
- * @requires crypto/random
24
- * @requires enums
25
- * @requires util
26
- * @requires type/oid
27
- * @requires config
28
- * @module crypto/public_key/elliptic/curve
29
- */
30
-
31
- import BN from 'bn.js';
32
- import nacl from 'tweetnacl/nacl-fast-light.js';
33
- import random from '../../random';
34
- import enums from '../../../enums';
35
- import util from '../../../util';
36
- import OID from '../../../type/oid';
37
- import { keyFromPublic, keyFromPrivate, getIndutnyCurve } from './indutnyKey';
38
-
39
- const webCrypto = util.getWebCrypto();
40
- const nodeCrypto = util.getNodeCrypto();
41
-
42
- const webCurves = {
43
- 'p256': 'P-256',
44
- 'p384': 'P-384',
45
- 'p521': 'P-521'
46
- };
47
- const knownCurves = nodeCrypto ? nodeCrypto.getCurves() : [];
48
- const nodeCurves = nodeCrypto ? {
49
- secp256k1: knownCurves.includes('secp256k1') ? 'secp256k1' : undefined,
50
- p256: knownCurves.includes('prime256v1') ? 'prime256v1' : undefined,
51
- p384: knownCurves.includes('secp384r1') ? 'secp384r1' : undefined,
52
- p521: knownCurves.includes('secp521r1') ? 'secp521r1' : undefined,
53
- ed25519: knownCurves.includes('ED25519') ? 'ED25519' : undefined,
54
- curve25519: knownCurves.includes('X25519') ? 'X25519' : undefined,
55
- brainpoolP256r1: knownCurves.includes('brainpoolP256r1') ? 'brainpoolP256r1' : undefined,
56
- brainpoolP384r1: knownCurves.includes('brainpoolP384r1') ? 'brainpoolP384r1' : undefined,
57
- brainpoolP512r1: knownCurves.includes('brainpoolP512r1') ? 'brainpoolP512r1' : undefined
58
- } : {};
59
-
60
- const curves = {
61
- p256: {
62
- oid: [0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07],
63
- keyType: enums.publicKey.ecdsa,
64
- hash: enums.hash.sha256,
65
- cipher: enums.symmetric.aes128,
66
- node: nodeCurves.p256,
67
- web: webCurves.p256,
68
- payloadSize: 32,
69
- sharedSize: 256
70
- },
71
- p384: {
72
- oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22],
73
- keyType: enums.publicKey.ecdsa,
74
- hash: enums.hash.sha384,
75
- cipher: enums.symmetric.aes192,
76
- node: nodeCurves.p384,
77
- web: webCurves.p384,
78
- payloadSize: 48,
79
- sharedSize: 384
80
- },
81
- p521: {
82
- oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x23],
83
- keyType: enums.publicKey.ecdsa,
84
- hash: enums.hash.sha512,
85
- cipher: enums.symmetric.aes256,
86
- node: nodeCurves.p521,
87
- web: webCurves.p521,
88
- payloadSize: 66,
89
- sharedSize: 528
90
- },
91
- secp256k1: {
92
- oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x0A],
93
- keyType: enums.publicKey.ecdsa,
94
- hash: enums.hash.sha256,
95
- cipher: enums.symmetric.aes128,
96
- node: nodeCurves.secp256k1,
97
- payloadSize: 32
98
- },
99
- ed25519: {
100
- oid: [0x06, 0x09, 0x2B, 0x06, 0x01, 0x04, 0x01, 0xDA, 0x47, 0x0F, 0x01],
101
- keyType: enums.publicKey.eddsa,
102
- hash: enums.hash.sha512,
103
- node: false, // nodeCurves.ed25519 TODO
104
- payloadSize: 32
105
- },
106
- curve25519: {
107
- oid: [0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x97, 0x55, 0x01, 0x05, 0x01],
108
- keyType: enums.publicKey.ecdh,
109
- hash: enums.hash.sha256,
110
- cipher: enums.symmetric.aes128,
111
- node: false, // nodeCurves.curve25519 TODO
112
- payloadSize: 32
113
- },
114
- brainpoolP256r1: {
115
- oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x07],
116
- keyType: enums.publicKey.ecdsa,
117
- hash: enums.hash.sha256,
118
- cipher: enums.symmetric.aes128,
119
- node: nodeCurves.brainpoolP256r1,
120
- payloadSize: 32
121
- },
122
- brainpoolP384r1: {
123
- oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0B],
124
- keyType: enums.publicKey.ecdsa,
125
- hash: enums.hash.sha384,
126
- cipher: enums.symmetric.aes192,
127
- node: nodeCurves.brainpoolP384r1,
128
- payloadSize: 48
129
- },
130
- brainpoolP512r1: {
131
- oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0D],
132
- keyType: enums.publicKey.ecdsa,
133
- hash: enums.hash.sha512,
134
- cipher: enums.symmetric.aes256,
135
- node: nodeCurves.brainpoolP512r1,
136
- payloadSize: 64
137
- }
138
- };
139
-
140
- /**
141
- * @constructor
142
- */
143
- function Curve(oid_or_name, params) {
144
- try {
145
- if (util.isArray(oid_or_name) ||
146
- util.isUint8Array(oid_or_name)) {
147
- // by oid byte array
148
- oid_or_name = new OID(oid_or_name);
149
- }
150
- if (oid_or_name instanceof OID) {
151
- // by curve OID
152
- oid_or_name = oid_or_name.getName();
153
- }
154
- // by curve name or oid string
155
- this.name = enums.write(enums.curve, oid_or_name);
156
- } catch (err) {
157
- throw new Error('Not valid curve');
158
- }
159
- params = params || curves[this.name];
160
-
161
- this.keyType = params.keyType;
162
-
163
- this.oid = params.oid;
164
- this.hash = params.hash;
165
- this.cipher = params.cipher;
166
- this.node = params.node && curves[this.name];
167
- this.web = params.web && curves[this.name];
168
- this.payloadSize = params.payloadSize;
169
- if (this.web && util.getWebCrypto()) {
170
- this.type = 'web';
171
- } else if (this.node && util.getNodeCrypto()) {
172
- this.type = 'node';
173
- } else if (this.name === 'curve25519') {
174
- this.type = 'curve25519';
175
- } else if (this.name === 'ed25519') {
176
- this.type = 'ed25519';
177
- }
178
- }
179
-
180
- Curve.prototype.genKeyPair = async function () {
181
- let keyPair;
182
- switch (this.type) {
183
- case 'web':
184
- try {
185
- return await webGenKeyPair(this.name);
186
- } catch (err) {
187
- util.print_debug_error("Browser did not support generating ec key " + err.message);
188
- break;
189
- }
190
- case 'node':
191
- return nodeGenKeyPair(this.name);
192
- case 'curve25519': {
193
- const privateKey = await random.getRandomBytes(32);
194
- privateKey[0] = (privateKey[0] & 127) | 64;
195
- privateKey[31] &= 248;
196
- const secretKey = privateKey.slice().reverse();
197
- keyPair = nacl.box.keyPair.fromSecretKey(secretKey);
198
- const publicKey = util.concatUint8Array([new Uint8Array([0x40]), keyPair.publicKey]);
199
- return { publicKey, privateKey };
200
- }
201
- case 'ed25519': {
202
- const privateKey = await random.getRandomBytes(32);
203
- const keyPair = nacl.sign.keyPair.fromSeed(privateKey);
204
- const publicKey = util.concatUint8Array([new Uint8Array([0x40]), keyPair.publicKey]);
205
- return { publicKey, privateKey };
206
- }
207
- }
208
- const indutnyCurve = await getIndutnyCurve(this.name);
209
- keyPair = await indutnyCurve.genKeyPair({
210
- entropy: util.Uint8Array_to_str(await random.getRandomBytes(32))
211
- });
212
- return { publicKey: new Uint8Array(keyPair.getPublic('array', false)), privateKey: keyPair.getPrivate().toArrayLike(Uint8Array) };
213
- };
214
-
215
- async function generate(curve) {
216
- curve = new Curve(curve);
217
- const keyPair = await curve.genKeyPair();
218
- return {
219
- oid: curve.oid,
220
- Q: new BN(keyPair.publicKey),
221
- d: new BN(keyPair.privateKey),
222
- hash: curve.hash,
223
- cipher: curve.cipher
224
- };
225
- }
226
-
227
- function getPreferredHashAlgo(oid) {
228
- return curves[enums.write(enums.curve, oid.toHex())].hash;
229
- }
230
-
231
- /**
232
- * Validate ECDH and EcDSA parameters
233
- * Not suitable for EdDSA (different secret key format)
234
- * @param {module:enums.publicKey} algo EC algorithm, to filter supported curves
235
- * @param {module:type/oid} oid EC object identifier
236
- * @param {Uint8Array} Q EC public point
237
- * @param {Uint8Array} d EC secret scalar
238
- * @returns {Promise<Boolean>} whether params are valid
239
- * @async
240
- */
241
- async function validateStandardParams(algo, oid, Q, d) {
242
- const supportedCurves = {
243
- p256: true,
244
- p384: true,
245
- p521: true,
246
- secp256k1: true,
247
- curve25519: algo === enums.publicKey.ecdh,
248
- brainpoolP256r1: true,
249
- brainpoolP384r1: true,
250
- brainpoolP512r1: true
251
- };
252
-
253
- // Check whether the given curve is supported
254
- const curveName = oid.getName();
255
- if (!supportedCurves[curveName]) {
256
- return false;
257
- }
258
-
259
- if (curveName === 'curve25519') {
260
- d = d.slice().reverse();
261
- // Re-derive public point Q'
262
- const { publicKey } = nacl.box.keyPair.fromSecretKey(d);
263
-
264
- Q = new Uint8Array(Q);
265
- const dG = new Uint8Array([0x40, ...publicKey]); // Add public key prefix
266
- if (!util.equalsUint8Array(dG, Q)) {
267
- return false;
268
- }
269
-
270
- return true;
271
- }
272
-
273
- const curve = await getIndutnyCurve(curveName);
274
- try {
275
- // Parse Q and check that it is on the curve but not at infinity
276
- Q = keyFromPublic(curve, Q).getPublic();
277
- } catch (validationErrors) {
278
- return false;
279
- }
280
-
281
- /**
282
- * Re-derive public point Q' = dG from private key
283
- * Expect Q == Q'
284
- */
285
- d = new BN(d);
286
- const dG = keyFromPrivate(curve, d).getPublic();
287
- if (!dG.eq(Q)) {
288
- return false;
289
- }
290
-
291
- return true;
292
- }
293
-
294
- export default Curve;
295
-
296
- export {
297
- curves, webCurves, nodeCurves, generate, getPreferredHashAlgo, jwkToRawPublic, rawPublicToJwk, privateToJwk, validateStandardParams
298
- };
299
-
300
- //////////////////////////
301
- // //
302
- // Helper functions //
303
- // //
304
- //////////////////////////
305
-
306
-
307
- async function webGenKeyPair(name) {
308
- // Note: keys generated with ECDSA and ECDH are structurally equivalent
309
- const webCryptoKey = await webCrypto.generateKey({ name: "ECDSA", namedCurve: webCurves[name] }, true, ["sign", "verify"]);
310
-
311
- const privateKey = await webCrypto.exportKey("jwk", webCryptoKey.privateKey);
312
- const publicKey = await webCrypto.exportKey("jwk", webCryptoKey.publicKey);
313
-
314
- return {
315
- publicKey: jwkToRawPublic(publicKey),
316
- privateKey: util.b64_to_Uint8Array(privateKey.d, true)
317
- };
318
- }
319
-
320
- async function nodeGenKeyPair(name) {
321
- // Note: ECDSA and ECDH key generation is structurally equivalent
322
- const ecdh = nodeCrypto.createECDH(nodeCurves[name]);
323
- await ecdh.generateKeys();
324
- return {
325
- publicKey: new Uint8Array(ecdh.getPublicKey()),
326
- privateKey: new Uint8Array(ecdh.getPrivateKey())
327
- };
328
- }
329
-
330
- //////////////////////////
331
- // //
332
- // Helper functions //
333
- // //
334
- //////////////////////////
335
-
336
- /**
337
- * @param {JsonWebKey} jwk key for conversion
338
- *
339
- * @returns {Uint8Array} raw public key
340
- */
341
- function jwkToRawPublic(jwk) {
342
- const bufX = util.b64_to_Uint8Array(jwk.x);
343
- const bufY = util.b64_to_Uint8Array(jwk.y);
344
- const publicKey = new Uint8Array(bufX.length + bufY.length + 1);
345
- publicKey[0] = 0x04;
346
- publicKey.set(bufX, 1);
347
- publicKey.set(bufY, bufX.length + 1);
348
- return publicKey;
349
- }
350
-
351
- /**
352
- * @param {Integer} payloadSize ec payload size
353
- * @param {String} name curve name
354
- * @param {Uint8Array} publicKey public key
355
- *
356
- * @returns {JsonWebKey} public key in jwk format
357
- */
358
- function rawPublicToJwk(payloadSize, name, publicKey) {
359
- const len = payloadSize;
360
- const bufX = publicKey.slice(1, len + 1);
361
- const bufY = publicKey.slice(len + 1, len * 2 + 1);
362
- // https://www.rfc-editor.org/rfc/rfc7518.txt
363
- const jwk = {
364
- kty: "EC",
365
- crv: name,
366
- x: util.Uint8Array_to_b64(bufX, true),
367
- y: util.Uint8Array_to_b64(bufY, true),
368
- ext: true
369
- };
370
- return jwk;
371
- }
372
-
373
- /**
374
- * @param {Integer} payloadSize ec payload size
375
- * @param {String} name curve name
376
- * @param {Uint8Array} publicKey public key
377
- * @param {Uint8Array} privateKey private key
378
- *
379
- * @returns {JsonWebKey} private key in jwk format
380
- */
381
- function privateToJwk(payloadSize, name, publicKey, privateKey) {
382
- const jwk = rawPublicToJwk(payloadSize, name, publicKey);
383
- jwk.d = util.Uint8Array_to_b64(privateKey, true);
384
- return jwk;
385
- }