@protontech/openpgp 4.10.6 → 5.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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 +31379 -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 +43947 -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 +43884 -0
  26. package/dist/openpgp.js +41082 -41563
  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 +43872 -0
  32. package/lightweight/package.json +5 -0
  33. package/openpgp.d.ts +890 -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,394 +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
- // The GPG4Browsers crypto interface
19
-
20
- /**
21
- * @fileoverview Provides functions for asymmetric encryption and decryption as
22
- * well as key generation and parameter handling for all public-key cryptosystems.
23
- * @requires crypto/public_key
24
- * @requires crypto/cipher
25
- * @requires crypto/random
26
- * @requires type/ecdh_symkey
27
- * @requires type/kdf_params
28
- * @requires type/mpi
29
- * @requires type/oid
30
- * @requires enums
31
- * @requires util
32
- * @module crypto/crypto
33
- */
34
-
35
- import publicKey from './public_key';
36
- import cipher from './cipher';
37
- import random from './random';
38
- import type_ecdh_symkey from '../type/ecdh_symkey';
39
- import type_kdf_params from '../type/kdf_params';
40
- import type_mpi from '../type/mpi';
41
- import type_oid from '../type/oid';
42
- import enums from '../enums';
43
- import util from '../util';
44
- import pkcs1 from './pkcs1';
45
- import pkcs5 from './pkcs5';
46
-
47
- function constructParams(types, data) {
48
- return types.map(function(type, i) {
49
- if (data && data[i]) {
50
- return new type(data[i]);
51
- }
52
- return new type();
53
- });
54
- }
55
-
56
- export default {
57
- /**
58
- * Encrypts data using specified algorithm and public key parameters.
59
- * See {@link https://tools.ietf.org/html/rfc4880#section-9.1|RFC 4880 9.1} for public key algorithms.
60
- * @param {module:enums.publicKey} algo Public key algorithm
61
- * @param {Array<module:type/mpi|
62
- module:type/oid|
63
- module:type/kdf_params>} pub_params Algorithm-specific public key parameters
64
- * @param {String} data Data to be encrypted
65
- * @param {String} fingerprint Recipient fingerprint
66
- * @returns {Array<module:type/mpi|
67
- * module:type/ecdh_symkey>} encrypted session key parameters
68
- * @async
69
- */
70
- publicKeyEncrypt: async function(algo, pub_params, data, fingerprint) {
71
- const types = this.getEncSessionKeyParamTypes(algo);
72
- switch (algo) {
73
- case enums.publicKey.rsa_encrypt:
74
- case enums.publicKey.rsa_encrypt_sign: {
75
- data = util.str_to_Uint8Array(data);
76
- const n = pub_params[0].toUint8Array();
77
- const e = pub_params[1].toUint8Array();
78
- const res = await publicKey.rsa.encrypt(data, n, e);
79
- return constructParams(types, [res]);
80
- }
81
- case enums.publicKey.elgamal: {
82
- data = new type_mpi(await pkcs1.eme.encode(data, pub_params[0].byteLength()));
83
- const m = data.toBN();
84
- const p = pub_params[0].toBN();
85
- const g = pub_params[1].toBN();
86
- const y = pub_params[2].toBN();
87
- const res = await publicKey.elgamal.encrypt(m, p, g, y);
88
- return constructParams(types, [res.c1, res.c2]);
89
- }
90
- case enums.publicKey.ecdh: {
91
- data = new type_mpi(pkcs5.encode(data));
92
- const oid = pub_params[0];
93
- const Q = pub_params[1].toUint8Array();
94
- const kdfParams = pub_params[2];
95
- const { publicKey: V, wrappedKey: C } = await publicKey.elliptic.ecdh.encrypt(
96
- oid, kdfParams, data, Q, fingerprint);
97
- return constructParams(types, [V, C]);
98
- }
99
- default:
100
- return [];
101
- }
102
- },
103
-
104
- /**
105
- * Decrypts data using specified algorithm and private key parameters.
106
- * See {@link https://tools.ietf.org/html/rfc4880#section-5.5.3|RFC 4880 5.5.3}
107
- * @param {module:enums.publicKey} algo Public key algorithm
108
- * @param {Array<module:type/mpi|
109
- module:type/oid|
110
- module:type/kdf_params>} key_params Algorithm-specific public, private key parameters
111
- * @param {Array<module:type/mpi|
112
- module:type/ecdh_symkey>}
113
- data_params encrypted session key parameters
114
- * @param {String} fingerprint Recipient fingerprint
115
- * @returns {String} String containing the decrypted data
116
- * @async
117
- */
118
- publicKeyDecrypt: async function(algo, key_params, data_params, fingerprint) {
119
- switch (algo) {
120
- case enums.publicKey.rsa_encrypt_sign:
121
- case enums.publicKey.rsa_encrypt: {
122
- const c = data_params[0].toUint8Array();
123
- const n = key_params[0].toUint8Array(); // n = pq
124
- const e = key_params[1].toUint8Array();
125
- const d = key_params[2].toUint8Array(); // de = 1 mod (p-1)(q-1)
126
- const p = key_params[3].toUint8Array();
127
- const q = key_params[4].toUint8Array();
128
- const u = key_params[5].toUint8Array(); // p^-1 mod q
129
- return publicKey.rsa.decrypt(c, n, e, d, p, q, u);
130
- }
131
- case enums.publicKey.elgamal: {
132
- const c1 = data_params[0].toBN();
133
- const c2 = data_params[1].toBN();
134
- const p = key_params[0].toBN();
135
- const x = key_params[3].toBN();
136
- const result = new type_mpi(await publicKey.elgamal.decrypt(c1, c2, p, x));
137
- return pkcs1.eme.decode(result.toString());
138
- }
139
- case enums.publicKey.ecdh: {
140
- const oid = key_params[0];
141
- const kdfParams = key_params[2];
142
- const V = data_params[0].toUint8Array();
143
- const C = data_params[1].data;
144
- const Q = key_params[1].toUint8Array();
145
- const d = key_params[3].toUint8Array();
146
- const result = new type_mpi(await publicKey.elliptic.ecdh.decrypt(
147
- oid, kdfParams, V, C, Q, d, fingerprint));
148
- return pkcs5.decode(result.toString());
149
- }
150
- default:
151
- throw new Error('Invalid public key encryption algorithm.');
152
- }
153
- },
154
-
155
- /** Returns the types comprising the private key of an algorithm
156
- * @param {module:enums.publicKey} algo The public key algorithm
157
- * @returns {Array<String>} The array of types
158
- */
159
- getPrivKeyParamTypes: function(algo) {
160
- switch (algo) {
161
- // Algorithm-Specific Fields for RSA secret keys:
162
- // - multiprecision integer (MPI) of RSA secret exponent d.
163
- // - MPI of RSA secret prime value p.
164
- // - MPI of RSA secret prime value q (p < q).
165
- // - MPI of u, the multiplicative inverse of p, mod q.
166
- case enums.publicKey.rsa_encrypt:
167
- case enums.publicKey.rsa_encrypt_sign:
168
- case enums.publicKey.rsa_sign:
169
- return [type_mpi, type_mpi, type_mpi, type_mpi];
170
- // Algorithm-Specific Fields for Elgamal secret keys:
171
- // - MPI of Elgamal secret exponent x.
172
- case enums.publicKey.elgamal:
173
- return [type_mpi];
174
- // Algorithm-Specific Fields for DSA secret keys:
175
- // - MPI of DSA secret exponent x.
176
- case enums.publicKey.dsa:
177
- return [type_mpi];
178
- // Algorithm-Specific Fields for ECDSA or ECDH secret keys:
179
- // - MPI of an integer representing the secret key.
180
- case enums.publicKey.ecdh:
181
- case enums.publicKey.ecdsa:
182
- case enums.publicKey.eddsa:
183
- return [type_mpi];
184
- default:
185
- throw new Error('Invalid public key encryption algorithm.');
186
- }
187
- },
188
-
189
- /** Returns the types comprising the public key of an algorithm
190
- * @param {module:enums.publicKey} algo The public key algorithm
191
- * @returns {Array<String>} The array of types
192
- */
193
- getPubKeyParamTypes: function(algo) {
194
- switch (algo) {
195
- // Algorithm-Specific Fields for RSA public keys:
196
- // - a multiprecision integer (MPI) of RSA public modulus n;
197
- // - an MPI of RSA public encryption exponent e.
198
- case enums.publicKey.rsa_encrypt:
199
- case enums.publicKey.rsa_encrypt_sign:
200
- case enums.publicKey.rsa_sign:
201
- return [type_mpi, type_mpi];
202
- // Algorithm-Specific Fields for Elgamal public keys:
203
- // - MPI of Elgamal prime p;
204
- // - MPI of Elgamal group generator g;
205
- // - MPI of Elgamal public key value y (= g**x mod p where x is secret).
206
- case enums.publicKey.elgamal:
207
- return [type_mpi, type_mpi, type_mpi];
208
- // Algorithm-Specific Fields for DSA public keys:
209
- // - MPI of DSA prime p;
210
- // - MPI of DSA group order q (q is a prime divisor of p-1);
211
- // - MPI of DSA group generator g;
212
- // - MPI of DSA public-key value y (= g**x mod p where x is secret).
213
- case enums.publicKey.dsa:
214
- return [type_mpi, type_mpi, type_mpi, type_mpi];
215
- // Algorithm-Specific Fields for ECDSA/EdDSA public keys:
216
- // - OID of curve;
217
- // - MPI of EC point representing public key.
218
- case enums.publicKey.ecdsa:
219
- case enums.publicKey.eddsa:
220
- return [type_oid, type_mpi];
221
- // Algorithm-Specific Fields for ECDH public keys:
222
- // - OID of curve;
223
- // - MPI of EC point representing public key.
224
- // - KDF: variable-length field containing KDF parameters.
225
- case enums.publicKey.ecdh:
226
- return [type_oid, type_mpi, type_kdf_params];
227
- default:
228
- throw new Error('Invalid public key encryption algorithm.');
229
- }
230
- },
231
-
232
- /** Returns the types comprising the encrypted session key of an algorithm
233
- * @param {module:enums.publicKey} algo The public key algorithm
234
- * @returns {Array<String>} The array of types
235
- */
236
- getEncSessionKeyParamTypes: function(algo) {
237
- switch (algo) {
238
- // Algorithm-Specific Fields for RSA encrypted session keys:
239
- // - MPI of RSA encrypted value m**e mod n.
240
- case enums.publicKey.rsa_encrypt:
241
- case enums.publicKey.rsa_encrypt_sign:
242
- return [type_mpi];
243
-
244
- // Algorithm-Specific Fields for Elgamal encrypted session keys:
245
- // - MPI of Elgamal value g**k mod p
246
- // - MPI of Elgamal value m * y**k mod p
247
- case enums.publicKey.elgamal:
248
- return [type_mpi, type_mpi];
249
- // Algorithm-Specific Fields for ECDH encrypted session keys:
250
- // - MPI containing the ephemeral key used to establish the shared secret
251
- // - ECDH Symmetric Key
252
- case enums.publicKey.ecdh:
253
- return [type_mpi, type_ecdh_symkey];
254
- default:
255
- throw new Error('Invalid public key encryption algorithm.');
256
- }
257
- },
258
-
259
- /** Generate algorithm-specific key parameters
260
- * @param {module:enums.publicKey} algo The public key algorithm
261
- * @param {Integer} bits Bit length for RSA keys
262
- * @param {module:type/oid} oid Object identifier for ECC keys
263
- * @returns {Array} The array of parameters
264
- * @async
265
- */
266
- generateParams: function(algo, bits, oid) {
267
- const types = [].concat(this.getPubKeyParamTypes(algo), this.getPrivKeyParamTypes(algo));
268
- switch (algo) {
269
- case enums.publicKey.rsa_encrypt:
270
- case enums.publicKey.rsa_encrypt_sign:
271
- case enums.publicKey.rsa_sign: {
272
- return publicKey.rsa.generate(bits, "10001").then(function(keyObject) {
273
- return constructParams(
274
- types, [keyObject.n, keyObject.e, keyObject.d, keyObject.p, keyObject.q, keyObject.u]
275
- );
276
- });
277
- }
278
- case enums.publicKey.dsa:
279
- case enums.publicKey.elgamal:
280
- throw new Error('Unsupported algorithm for key generation.');
281
- case enums.publicKey.ecdsa:
282
- case enums.publicKey.eddsa:
283
- return publicKey.elliptic.generate(oid).then(function (keyObject) {
284
- return constructParams(types, [keyObject.oid, keyObject.Q, keyObject.d]);
285
- });
286
- case enums.publicKey.ecdh:
287
- return publicKey.elliptic.generate(oid).then(function (keyObject) {
288
- return constructParams(types, [
289
- keyObject.oid,
290
- keyObject.Q,
291
- { hash: keyObject.hash, cipher: keyObject.cipher },
292
- keyObject.d
293
- ]);
294
- });
295
- default:
296
- throw new Error('Invalid public key algorithm.');
297
- }
298
- },
299
-
300
- /**
301
- * Validate algorithm-specific key parameters
302
- * @param {module:enums.publicKey} algo The public key algorithm
303
- * @param {Array} params The array of parameters
304
- * @returns {Promise<Boolean> whether the parameters are valid
305
- * @async
306
- */
307
- validateParams: async function(algo, params) {
308
- switch (algo) {
309
- case enums.publicKey.rsa_encrypt:
310
- case enums.publicKey.rsa_encrypt_sign:
311
- case enums.publicKey.rsa_sign: {
312
- if (params.length < 6) {
313
- throw new Error('Missing key parameters');
314
- }
315
- const n = params[0].toUint8Array();
316
- const e = params[1].toUint8Array();
317
- const d = params[2].toUint8Array();
318
- const p = params[3].toUint8Array();
319
- const q = params[4].toUint8Array();
320
- const u = params[5].toUint8Array();
321
- return publicKey.rsa.validateParams(n, e, d, p, q, u);
322
- }
323
- case enums.publicKey.dsa: {
324
- if (params.length < 5) {
325
- throw new Error('Missing key parameters');
326
- }
327
- const p = params[0].toUint8Array();
328
- const q = params[1].toUint8Array();
329
- const g = params[2].toUint8Array();
330
- const y = params[3].toUint8Array();
331
- const x = params[4].toUint8Array();
332
- return publicKey.dsa.validateParams(p, q, g, y, x);
333
- }
334
- case enums.publicKey.elgamal: {
335
- if (params.length < 4) {
336
- throw new Error('Missing key parameters');
337
- }
338
- const p = params[0].toUint8Array();
339
- const g = params[1].toUint8Array();
340
- const y = params[2].toUint8Array();
341
- const x = params[3].toUint8Array();
342
- return publicKey.elgamal.validateParams(p, g, y, x);
343
- }
344
- case enums.publicKey.ecdsa:
345
- case enums.publicKey.ecdh: {
346
- const expectedLen = algo === enums.publicKey.ecdh ? 3 : 2;
347
- if (params.length < expectedLen) {
348
- throw new Error('Missing key parameters');
349
- }
350
-
351
- const algoModule = publicKey.elliptic[enums.read(enums.publicKey, algo)];
352
- const { oid, Q, d } = algoModule.parseParams(params);
353
- return algoModule.validateParams(oid, Q, d);
354
- }
355
- case enums.publicKey.eddsa: {
356
- const expectedLen = 3;
357
- if (params.length < expectedLen) {
358
- throw new Error('Missing key parameters');
359
- }
360
-
361
- const { oid, Q, seed } = publicKey.elliptic.eddsa.parseParams(params);
362
- return publicKey.elliptic.eddsa.validateParams(oid, Q, seed);
363
- }
364
- default:
365
- throw new Error('Invalid public key algorithm.');
366
- }
367
- },
368
-
369
- /**
370
- * Generates a random byte prefix for the specified algorithm
371
- * See {@link https://tools.ietf.org/html/rfc4880#section-9.2|RFC 4880 9.2} for algorithms.
372
- * @param {module:enums.symmetric} algo Symmetric encryption algorithm
373
- * @returns {Uint8Array} Random bytes with length equal to the block size of the cipher, plus the last two bytes repeated.
374
- * @async
375
- */
376
- getPrefixRandom: async function(algo) {
377
- const prefixrandom = await random.getRandomBytes(cipher[algo].blockSize);
378
- const repeat = new Uint8Array([prefixrandom[prefixrandom.length - 2], prefixrandom[prefixrandom.length - 1]]);
379
- return util.concat([prefixrandom, repeat]);
380
- },
381
-
382
- /**
383
- * Generating a session key for the specified symmetric algorithm
384
- * See {@link https://tools.ietf.org/html/rfc4880#section-9.2|RFC 4880 9.2} for algorithms.
385
- * @param {module:enums.symmetric} algo Symmetric encryption algorithm
386
- * @returns {Uint8Array} Random bytes as a string to be used as a key
387
- * @async
388
- */
389
- generateSessionKey: function(algo) {
390
- return random.getRandomBytes(cipher[algo].keySize);
391
- },
392
-
393
- constructParams: constructParams
394
- };
package/src/crypto/eax.js DELETED
@@ -1,172 +0,0 @@
1
- // OpenPGP.js - An OpenPGP implementation in javascript
2
- // Copyright (C) 2018 ProtonTech AG
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 This module implements AES-EAX en/decryption on top of
20
- * native AES-CTR using either the WebCrypto API or Node.js' crypto API.
21
- * @requires asmcrypto.js
22
- * @requires crypto/cmac
23
- * @requires util
24
- * @module crypto/eax
25
- */
26
-
27
- import { AES_CTR } from 'asmcrypto.js/dist_es5/aes/ctr';
28
- import CMAC from './cmac';
29
- import util from '../util';
30
-
31
- const webCrypto = util.getWebCrypto();
32
- const nodeCrypto = util.getNodeCrypto();
33
- const Buffer = util.getNodeBuffer();
34
-
35
-
36
- const blockLength = 16;
37
- const ivLength = blockLength;
38
- const tagLength = blockLength;
39
-
40
- const zero = new Uint8Array(blockLength);
41
- const one = new Uint8Array(blockLength); one[blockLength - 1] = 1;
42
- const two = new Uint8Array(blockLength); two[blockLength - 1] = 2;
43
-
44
- async function OMAC(key) {
45
- const cmac = await CMAC(key);
46
- return function(t, message) {
47
- return cmac(util.concatUint8Array([t, message]));
48
- };
49
- }
50
-
51
- async function CTR(key) {
52
- if (
53
- util.getWebCrypto() &&
54
- key.length !== 24 && // WebCrypto (no 192 bit support) see: https://www.chromium.org/blink/webcrypto#TOC-AES-support
55
- navigator.userAgent.indexOf('Edge') === -1
56
- ) {
57
- key = await webCrypto.importKey('raw', key, { name: 'AES-CTR', length: key.length * 8 }, false, ['encrypt']);
58
- return async function(pt, iv) {
59
- const ct = await webCrypto.encrypt({ name: 'AES-CTR', counter: iv, length: blockLength * 8 }, key, pt);
60
- return new Uint8Array(ct);
61
- };
62
- }
63
- if (util.getNodeCrypto()) { // Node crypto library
64
- key = Buffer.from(key);
65
- return async function(pt, iv) {
66
- pt = Buffer.from(pt);
67
- iv = Buffer.from(iv);
68
- const en = new nodeCrypto.createCipheriv('aes-' + (key.length * 8) + '-ctr', key, iv);
69
- const ct = Buffer.concat([en.update(pt), en.final()]);
70
- return new Uint8Array(ct);
71
- };
72
- }
73
- // asm.js fallback
74
- return async function(pt, iv) {
75
- return AES_CTR.encrypt(pt, key, iv);
76
- };
77
- }
78
-
79
-
80
- /**
81
- * Class to en/decrypt using EAX mode.
82
- * @param {String} cipher The symmetric cipher algorithm to use e.g. 'aes128'
83
- * @param {Uint8Array} key The encryption key
84
- */
85
- async function EAX(cipher, key) {
86
- if (cipher.substr(0, 3) !== 'aes') {
87
- throw new Error('EAX mode supports only AES cipher');
88
- }
89
-
90
- const [
91
- omac,
92
- ctr
93
- ] = await Promise.all([
94
- OMAC(key),
95
- CTR(key)
96
- ]);
97
-
98
- return {
99
- /**
100
- * Encrypt plaintext input.
101
- * @param {Uint8Array} plaintext The cleartext input to be encrypted
102
- * @param {Uint8Array} nonce The nonce (16 bytes)
103
- * @param {Uint8Array} adata Associated data to sign
104
- * @returns {Promise<Uint8Array>} The ciphertext output
105
- */
106
- encrypt: async function(plaintext, nonce, adata) {
107
- const [
108
- omacNonce,
109
- omacAdata
110
- ] = await Promise.all([
111
- omac(zero, nonce),
112
- omac(one, adata)
113
- ]);
114
- const ciphered = await ctr(plaintext, omacNonce);
115
- const omacCiphered = await omac(two, ciphered);
116
- const tag = omacCiphered; // Assumes that omac(*).length === tagLength.
117
- for (let i = 0; i < tagLength; i++) {
118
- tag[i] ^= omacAdata[i] ^ omacNonce[i];
119
- }
120
- return util.concatUint8Array([ciphered, tag]);
121
- },
122
-
123
- /**
124
- * Decrypt ciphertext input.
125
- * @param {Uint8Array} ciphertext The ciphertext input to be decrypted
126
- * @param {Uint8Array} nonce The nonce (16 bytes)
127
- * @param {Uint8Array} adata Associated data to verify
128
- * @returns {Promise<Uint8Array>} The plaintext output
129
- */
130
- decrypt: async function(ciphertext, nonce, adata) {
131
- if (ciphertext.length < tagLength) throw new Error('Invalid EAX ciphertext');
132
- const ciphered = ciphertext.subarray(0, -tagLength);
133
- const ctTag = ciphertext.subarray(-tagLength);
134
- const [
135
- omacNonce,
136
- omacAdata,
137
- omacCiphered
138
- ] = await Promise.all([
139
- omac(zero, nonce),
140
- omac(one, adata),
141
- omac(two, ciphered)
142
- ]);
143
- const tag = omacCiphered; // Assumes that omac(*).length === tagLength.
144
- for (let i = 0; i < tagLength; i++) {
145
- tag[i] ^= omacAdata[i] ^ omacNonce[i];
146
- }
147
- if (!util.equalsUint8Array(ctTag, tag)) throw new Error('Authentication tag mismatch');
148
- const plaintext = await ctr(ciphered, omacNonce);
149
- return plaintext;
150
- }
151
- };
152
- }
153
-
154
-
155
- /**
156
- * Get EAX nonce as defined by {@link https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-04#section-5.16.1|RFC4880bis-04, section 5.16.1}.
157
- * @param {Uint8Array} iv The initialization vector (16 bytes)
158
- * @param {Uint8Array} chunkIndex The chunk index (8 bytes)
159
- */
160
- EAX.getNonce = function(iv, chunkIndex) {
161
- const nonce = iv.slice();
162
- for (let i = 0; i < chunkIndex.length; i++) {
163
- nonce[8 + i] ^= chunkIndex[i];
164
- }
165
- return nonce;
166
- };
167
-
168
- EAX.blockLength = blockLength;
169
- EAX.ivLength = ivLength;
170
- EAX.tagLength = tagLength;
171
-
172
- export default EAX;