@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.
- package/README.md +311 -239
- package/dist/lightweight/bn.interface.min.mjs +3 -0
- package/dist/lightweight/bn.interface.min.mjs.map +1 -0
- package/dist/lightweight/bn.interface.mjs +340 -0
- package/dist/lightweight/bn.min.mjs +3 -0
- package/dist/lightweight/bn.min.mjs.map +1 -0
- package/dist/lightweight/bn.mjs +3434 -0
- package/dist/lightweight/elliptic.min.mjs +3 -0
- package/dist/lightweight/elliptic.min.mjs.map +1 -0
- package/dist/lightweight/elliptic.mjs +4313 -0
- package/dist/lightweight/openpgp.min.mjs +3 -0
- package/dist/lightweight/openpgp.min.mjs.map +1 -0
- package/dist/lightweight/openpgp.mjs +31379 -0
- package/dist/lightweight/ponyfill.es6.min.mjs +3 -0
- package/dist/lightweight/ponyfill.es6.min.mjs.map +1 -0
- package/dist/lightweight/ponyfill.es6.mjs +3831 -0
- package/dist/lightweight/web-streams-adapter.min.mjs +17 -0
- package/dist/lightweight/web-streams-adapter.min.mjs.map +1 -0
- package/dist/lightweight/web-streams-adapter.mjs +561 -0
- package/dist/node/openpgp.js +43947 -0
- package/dist/node/openpgp.min.js +17 -0
- package/dist/node/openpgp.min.js.map +1 -0
- package/dist/node/openpgp.min.mjs +17 -0
- package/dist/node/openpgp.min.mjs.map +1 -0
- package/dist/node/openpgp.mjs +43884 -0
- package/dist/openpgp.js +41082 -41563
- package/dist/openpgp.min.js +17 -2
- package/dist/openpgp.min.js.map +1 -0
- package/dist/openpgp.min.mjs +17 -0
- package/dist/openpgp.min.mjs.map +1 -0
- package/dist/openpgp.mjs +43872 -0
- package/lightweight/package.json +5 -0
- package/openpgp.d.ts +890 -0
- package/package.json +63 -57
- package/dist/compat/openpgp.js +0 -61067
- package/dist/compat/openpgp.min.js +0 -2
- package/dist/compat/openpgp.worker.js +0 -173
- package/dist/compat/openpgp.worker.min.js +0 -2
- package/dist/lightweight/elliptic.min.js +0 -5
- package/dist/lightweight/openpgp.js +0 -40024
- package/dist/lightweight/openpgp.min.js +0 -2
- package/dist/lightweight/openpgp.worker.js +0 -173
- package/dist/lightweight/openpgp.worker.min.js +0 -2
- package/dist/openpgp.worker.js +0 -173
- package/dist/openpgp.worker.min.js +0 -2
- package/src/cleartext.js +0 -220
- package/src/config/config.js +0 -224
- package/src/config/index.js +0 -7
- package/src/config/localStorage.js +0 -35
- package/src/crypto/aes_kw.js +0 -153
- package/src/crypto/cfb.js +0 -169
- package/src/crypto/cipher/aes.js +0 -27
- package/src/crypto/cipher/blowfish.js +0 -398
- package/src/crypto/cipher/cast5.js +0 -610
- package/src/crypto/cipher/des.js +0 -476
- package/src/crypto/cipher/index.js +0 -91
- package/src/crypto/cipher/twofish.js +0 -346
- package/src/crypto/cmac.js +0 -98
- package/src/crypto/crypto.js +0 -394
- package/src/crypto/eax.js +0 -172
- package/src/crypto/gcm.js +0 -141
- package/src/crypto/hash/index.js +0 -163
- package/src/crypto/hash/md5.js +0 -205
- package/src/crypto/index.js +0 -57
- package/src/crypto/ocb.js +0 -274
- package/src/crypto/pkcs1.js +0 -170
- package/src/crypto/pkcs5.js +0 -55
- package/src/crypto/public_key/dsa.js +0 -188
- package/src/crypto/public_key/elgamal.js +0 -137
- package/src/crypto/public_key/elliptic/curves.js +0 -385
- package/src/crypto/public_key/elliptic/ecdh.js +0 -414
- package/src/crypto/public_key/elliptic/ecdsa.js +0 -348
- package/src/crypto/public_key/elliptic/eddsa.js +0 -119
- package/src/crypto/public_key/elliptic/index.js +0 -34
- package/src/crypto/public_key/elliptic/indutnyKey.js +0 -85
- package/src/crypto/public_key/index.js +0 -28
- package/src/crypto/public_key/prime.js +0 -275
- package/src/crypto/public_key/rsa.js +0 -597
- package/src/crypto/random.js +0 -145
- package/src/crypto/signature.js +0 -137
- package/src/encoding/armor.js +0 -433
- package/src/encoding/base64.js +0 -96
- package/src/enums.js +0 -493
- package/src/hkp.js +0 -89
- package/src/index.js +0 -161
- package/src/key/factory.js +0 -326
- package/src/key/helper.js +0 -363
- package/src/key/index.js +0 -32
- package/src/key/key.js +0 -890
- package/src/key/subkey.js +0 -187
- package/src/key/user.js +0 -230
- package/src/keyring/index.js +0 -12
- package/src/keyring/keyring.js +0 -229
- package/src/keyring/localstore.js +0 -119
- package/src/lightweight_helper.js +0 -26
- package/src/message.js +0 -825
- package/src/openpgp.js +0 -717
- package/src/packet/all_packets.js +0 -116
- package/src/packet/clone.js +0 -189
- package/src/packet/compressed.js +0 -194
- package/src/packet/index.js +0 -20
- package/src/packet/literal.js +0 -168
- package/src/packet/marker.js +0 -62
- package/src/packet/one_pass_signature.js +0 -156
- package/src/packet/packet.js +0 -300
- package/src/packet/packetlist.js +0 -232
- package/src/packet/public_key.js +0 -280
- package/src/packet/public_key_encrypted_session_key.js +0 -156
- package/src/packet/public_subkey.js +0 -44
- package/src/packet/secret_key.js +0 -448
- package/src/packet/secret_subkey.js +0 -41
- package/src/packet/signature.js +0 -782
- package/src/packet/sym_encrypted_aead_protected.js +0 -189
- package/src/packet/sym_encrypted_integrity_protected.js +0 -139
- package/src/packet/sym_encrypted_session_key.js +0 -204
- package/src/packet/symmetrically_encrypted.js +0 -118
- package/src/packet/trust.js +0 -35
- package/src/packet/user_attribute.js +0 -94
- package/src/packet/userid.js +0 -87
- package/src/polyfills.js +0 -64
- package/src/signature.js +0 -73
- package/src/type/ecdh_symkey.js +0 -69
- package/src/type/kdf_params.js +0 -114
- package/src/type/keyid.js +0 -110
- package/src/type/mpi.js +0 -138
- package/src/type/oid.js +0 -110
- package/src/type/s2k.js +0 -203
- package/src/util.js +0 -836
- package/src/wkd.js +0 -88
- package/src/worker/async_proxy.js +0 -190
- package/src/worker/worker.js +0 -167
- package/test/crypto/aes_kw.js +0 -57
- package/test/crypto/cipher/aes.js +0 -86
- package/test/crypto/cipher/blowfish.js +0 -58
- package/test/crypto/cipher/cast5.js +0 -25
- package/test/crypto/cipher/des.js +0 -143
- package/test/crypto/cipher/index.js +0 -7
- package/test/crypto/cipher/twofish.js +0 -71
- package/test/crypto/crypto.js +0 -383
- package/test/crypto/eax.js +0 -150
- package/test/crypto/ecdh.js +0 -359
- package/test/crypto/elliptic.js +0 -251
- package/test/crypto/elliptic_data.js +0 -102
- package/test/crypto/hash/index.js +0 -5
- package/test/crypto/hash/md5.js +0 -16
- package/test/crypto/hash/ripemd.js +0 -14
- package/test/crypto/hash/sha.js +0 -20
- package/test/crypto/index.js +0 -14
- package/test/crypto/ocb.js +0 -183
- package/test/crypto/pkcs5.js +0 -39
- package/test/crypto/random.js +0 -79
- package/test/crypto/rsa.js +0 -180
- package/test/crypto/validate.js +0 -387
- package/test/general/armor.js +0 -408
- package/test/general/brainpool.js +0 -360
- package/test/general/decompression.js +0 -60
- package/test/general/ecc_nist.js +0 -115
- package/test/general/ecc_secp256k1.js +0 -242
- package/test/general/forwarding.js +0 -43
- package/test/general/hkp.js +0 -165
- package/test/general/index.js +0 -20
- package/test/general/key.js +0 -3402
- package/test/general/keyring.js +0 -336
- package/test/general/oid.js +0 -39
- package/test/general/openpgp.js +0 -2542
- package/test/general/packet.js +0 -937
- package/test/general/signature.js +0 -1665
- package/test/general/streaming.js +0 -944
- package/test/general/testInputs.js +0 -18
- package/test/general/util.js +0 -183
- package/test/general/wkd.js +0 -48
- package/test/general/x25519.js +0 -556
- package/test/unittests.js +0 -64
|
@@ -1,414 +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 Key encryption and decryption for RFC 6637 ECDH
|
|
20
|
-
* @requires bn.js
|
|
21
|
-
* @requires tweetnacl
|
|
22
|
-
* @requires crypto/public_key/elliptic/curve
|
|
23
|
-
* @requires crypto/aes_kw
|
|
24
|
-
* @requires crypto/cipher
|
|
25
|
-
* @requires crypto/random
|
|
26
|
-
* @requires crypto/hash
|
|
27
|
-
* @requires type/kdf_params
|
|
28
|
-
* @requires enums
|
|
29
|
-
* @requires util
|
|
30
|
-
* @module crypto/public_key/elliptic/ecdh
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
import BN from 'bn.js';
|
|
34
|
-
import nacl from 'tweetnacl/nacl-fast-light.js';
|
|
35
|
-
import Curve, { jwkToRawPublic, rawPublicToJwk, privateToJwk, validateStandardParams } from './curves';
|
|
36
|
-
import aes_kw from '../../aes_kw';
|
|
37
|
-
import cipher from '../../cipher';
|
|
38
|
-
import random from '../../random';
|
|
39
|
-
import hash from '../../hash';
|
|
40
|
-
import enums from '../../../enums';
|
|
41
|
-
import util from '../../../util';
|
|
42
|
-
import { keyFromPublic, keyFromPrivate, getIndutnyCurve } from './indutnyKey';
|
|
43
|
-
|
|
44
|
-
const webCrypto = util.getWebCrypto();
|
|
45
|
-
const nodeCrypto = util.getNodeCrypto();
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Validate ECDH parameters
|
|
49
|
-
* @param {module:type/oid} oid Elliptic curve object identifier
|
|
50
|
-
* @param {Uint8Array} Q ECDH public point
|
|
51
|
-
* @param {Uint8Array} d ECDH secret scalar
|
|
52
|
-
* @returns {Promise<Boolean>} whether params are valid
|
|
53
|
-
* @async
|
|
54
|
-
*/
|
|
55
|
-
async function validateParams(oid, Q, d) {
|
|
56
|
-
return validateStandardParams(enums.publicKey.ecdh, oid, Q, d);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Build Param for ECDH algorithm (RFC 6637)
|
|
60
|
-
function buildEcdhParam(public_algo, oid, kdfParams, fingerprint) {
|
|
61
|
-
return util.concatUint8Array([
|
|
62
|
-
oid.write(),
|
|
63
|
-
new Uint8Array([public_algo]),
|
|
64
|
-
kdfParams.replacementKDFParams || kdfParams.write(),
|
|
65
|
-
util.str_to_Uint8Array("Anonymous Sender "),
|
|
66
|
-
kdfParams.replacementFingerprint || fingerprint.subarray(0, 20)
|
|
67
|
-
]);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Parses MPI params and returns them as byte arrays of fixed length
|
|
72
|
-
* @param {Array} params key parameters
|
|
73
|
-
* @returns {Object} parameters in the form
|
|
74
|
-
* { oid, kdfParams, d: Uint8Array, Q: Uint8Array }
|
|
75
|
-
*/
|
|
76
|
-
function parseParams(params) {
|
|
77
|
-
if (params.length < 3 || params.length > 4) {
|
|
78
|
-
throw new Error('Unexpected number of parameters');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const oid = params[0];
|
|
82
|
-
const curve = new Curve(oid);
|
|
83
|
-
const parsedParams = { oid };
|
|
84
|
-
// The public point never has leading zeros, as it is prefixed by 0x40 or 0x04
|
|
85
|
-
parsedParams.Q = params[1].toUint8Array();
|
|
86
|
-
parsedParams.kdfParams = params[2];
|
|
87
|
-
|
|
88
|
-
if (params.length === 4) {
|
|
89
|
-
parsedParams.d = params[3].toUint8Array('be', curve.payloadSize);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return parsedParams;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// Key Derivation Function (RFC 6637)
|
|
96
|
-
async function kdf(hash_algo, X, length, param, stripLeading = false, stripTrailing = false) {
|
|
97
|
-
// Note: X is little endian for Curve25519, big-endian for all others.
|
|
98
|
-
// This is not ideal, but the RFC's are unclear
|
|
99
|
-
// https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-02#appendix-B
|
|
100
|
-
let i;
|
|
101
|
-
if (stripLeading) {
|
|
102
|
-
// Work around old go crypto bug
|
|
103
|
-
for (i = 0; i < X.length && X[i] === 0; i++);
|
|
104
|
-
X = X.subarray(i);
|
|
105
|
-
}
|
|
106
|
-
if (stripTrailing) {
|
|
107
|
-
// Work around old OpenPGP.js bug
|
|
108
|
-
for (i = X.length - 1; i >= 0 && X[i] === 0; i--);
|
|
109
|
-
X = X.subarray(0, i + 1);
|
|
110
|
-
}
|
|
111
|
-
const digest = await hash.digest(hash_algo, util.concatUint8Array([
|
|
112
|
-
new Uint8Array([0, 0, 0, 1]),
|
|
113
|
-
X,
|
|
114
|
-
param
|
|
115
|
-
]));
|
|
116
|
-
return digest.subarray(0, length);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Generate ECDHE ephemeral key and secret from public key
|
|
121
|
-
*
|
|
122
|
-
* @param {Curve} curve Elliptic curve object
|
|
123
|
-
* @param {Uint8Array} Q Recipient public key
|
|
124
|
-
* @returns {Promise<{publicKey: Uint8Array, sharedKey: Uint8Array}>}
|
|
125
|
-
* @async
|
|
126
|
-
*/
|
|
127
|
-
async function genPublicEphemeralKey(curve, Q) {
|
|
128
|
-
switch (curve.type) {
|
|
129
|
-
case 'curve25519': {
|
|
130
|
-
const d = await random.getRandomBytes(32);
|
|
131
|
-
const { secretKey, sharedKey } = await genPrivateEphemeralKey(curve, Q, null, d);
|
|
132
|
-
let { publicKey } = nacl.box.keyPair.fromSecretKey(secretKey);
|
|
133
|
-
publicKey = util.concatUint8Array([new Uint8Array([0x40]), publicKey]);
|
|
134
|
-
return { publicKey, sharedKey }; // Note: sharedKey is little-endian here, unlike below
|
|
135
|
-
}
|
|
136
|
-
case 'web':
|
|
137
|
-
if (curve.web && util.getWebCrypto()) {
|
|
138
|
-
try {
|
|
139
|
-
return await webPublicEphemeralKey(curve, Q);
|
|
140
|
-
} catch (err) {
|
|
141
|
-
util.print_debug_error(err);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
break;
|
|
145
|
-
case 'node':
|
|
146
|
-
return nodePublicEphemeralKey(curve, Q);
|
|
147
|
-
}
|
|
148
|
-
return ellipticPublicEphemeralKey(curve, Q);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Encrypt and wrap a session key
|
|
153
|
-
*
|
|
154
|
-
* @param {module:type/oid} oid Elliptic curve object identifier
|
|
155
|
-
* @param {module:type/kdf_params} kdfParams KDF params including cipher and algorithm to use
|
|
156
|
-
* @param {module:type/mpi} m Value derived from session key (RFC 6637)
|
|
157
|
-
* @param {Uint8Array} Q Recipient public key
|
|
158
|
-
* @param {Uint8Array} fingerprint Recipient fingerprint
|
|
159
|
-
* @returns {Promise<{publicKey: Uint8Array, wrappedKey: Uint8Array}>}
|
|
160
|
-
* @async
|
|
161
|
-
*/
|
|
162
|
-
async function encrypt(oid, kdfParams, m, Q, fingerprint) {
|
|
163
|
-
const curve = new Curve(oid);
|
|
164
|
-
const { publicKey, sharedKey } = await genPublicEphemeralKey(curve, Q);
|
|
165
|
-
const param = buildEcdhParam(enums.publicKey.ecdh, oid, kdfParams, fingerprint);
|
|
166
|
-
const cipher_algo = enums.read(enums.symmetric, kdfParams.cipher);
|
|
167
|
-
const Z = await kdf(kdfParams.hash, sharedKey, cipher[cipher_algo].keySize, param);
|
|
168
|
-
const wrappedKey = aes_kw.wrap(Z, m.toString());
|
|
169
|
-
return { publicKey, wrappedKey };
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Generate ECDHE secret from private key and public part of ephemeral key
|
|
174
|
-
*
|
|
175
|
-
* @param {Curve} curve Elliptic curve object
|
|
176
|
-
* @param {Uint8Array} V Public part of ephemeral key
|
|
177
|
-
* @param {Uint8Array} Q Recipient public key
|
|
178
|
-
* @param {Uint8Array} d Recipient private key
|
|
179
|
-
* @returns {Promise<{secretKey: Uint8Array, sharedKey: Uint8Array}>}
|
|
180
|
-
* @async
|
|
181
|
-
*/
|
|
182
|
-
async function genPrivateEphemeralKey(curve, V, Q, d) {
|
|
183
|
-
if (d.length !== curve.payloadSize) {
|
|
184
|
-
const privateKey = new Uint8Array(curve.payloadSize);
|
|
185
|
-
privateKey.set(d, curve.payloadSize - d.length);
|
|
186
|
-
d = privateKey;
|
|
187
|
-
}
|
|
188
|
-
switch (curve.type) {
|
|
189
|
-
case 'curve25519': {
|
|
190
|
-
const secretKey = d.slice().reverse();
|
|
191
|
-
const sharedKey = nacl.scalarMult(secretKey, V.subarray(1));
|
|
192
|
-
return { secretKey, sharedKey }; // Note: sharedKey is little-endian here, unlike below
|
|
193
|
-
}
|
|
194
|
-
case 'web':
|
|
195
|
-
if (curve.web && util.getWebCrypto()) {
|
|
196
|
-
try {
|
|
197
|
-
return await webPrivateEphemeralKey(curve, V, Q, d);
|
|
198
|
-
} catch (err) {
|
|
199
|
-
util.print_debug_error(err);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
break;
|
|
203
|
-
case 'node':
|
|
204
|
-
return nodePrivateEphemeralKey(curve, V, d);
|
|
205
|
-
}
|
|
206
|
-
return ellipticPrivateEphemeralKey(curve, V, d);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Decrypt and unwrap the value derived from session key
|
|
211
|
-
*
|
|
212
|
-
* @param {module:type/oid} oid Elliptic curve object identifier
|
|
213
|
-
* @param {module:type/kdf_params} kdfParams KDF params including cipher and algorithm to use
|
|
214
|
-
* @param {Uint8Array} V Public part of ephemeral key
|
|
215
|
-
* @param {Uint8Array} C Encrypted and wrapped value derived from session key
|
|
216
|
-
* @param {Uint8Array} Q Recipient public key
|
|
217
|
-
* @param {Uint8Array} d Recipient private key
|
|
218
|
-
* @param {Uint8Array} fingerprint Recipient fingerprint
|
|
219
|
-
* @returns {Promise<BN>} Value derived from session key
|
|
220
|
-
* @async
|
|
221
|
-
*/
|
|
222
|
-
async function decrypt(oid, kdfParams, V, C, Q, d, fingerprint) {
|
|
223
|
-
const curve = new Curve(oid);
|
|
224
|
-
const { sharedKey } = await genPrivateEphemeralKey(curve, V, Q, d);
|
|
225
|
-
const param = buildEcdhParam(enums.publicKey.ecdh, oid, kdfParams, fingerprint);
|
|
226
|
-
const cipher_algo = enums.read(enums.symmetric, kdfParams.cipher);
|
|
227
|
-
let err;
|
|
228
|
-
for (let i = 0; i < 3; i++) {
|
|
229
|
-
try {
|
|
230
|
-
// Work around old go crypto bug and old OpenPGP.js bug, respectively.
|
|
231
|
-
const Z = await kdf(kdfParams.hash, sharedKey, cipher[cipher_algo].keySize, param, i === 1, i === 2);
|
|
232
|
-
return new BN(aes_kw.unwrap(Z, C));
|
|
233
|
-
} catch (e) {
|
|
234
|
-
err = e;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
throw err;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Generate ECDHE secret from private key and public part of ephemeral key using webCrypto
|
|
242
|
-
*
|
|
243
|
-
* @param {Curve} curve Elliptic curve object
|
|
244
|
-
* @param {Uint8Array} V Public part of ephemeral key
|
|
245
|
-
* @param {Uint8Array} Q Recipient public key
|
|
246
|
-
* @param {Uint8Array} d Recipient private key
|
|
247
|
-
* @returns {Promise<{secretKey: Uint8Array, sharedKey: Uint8Array}>}
|
|
248
|
-
* @async
|
|
249
|
-
*/
|
|
250
|
-
async function webPrivateEphemeralKey(curve, V, Q, d) {
|
|
251
|
-
const recipient = privateToJwk(curve.payloadSize, curve.web.web, Q, d);
|
|
252
|
-
let privateKey = webCrypto.importKey(
|
|
253
|
-
"jwk",
|
|
254
|
-
recipient,
|
|
255
|
-
{
|
|
256
|
-
name: "ECDH",
|
|
257
|
-
namedCurve: curve.web.web
|
|
258
|
-
},
|
|
259
|
-
true,
|
|
260
|
-
["deriveKey", "deriveBits"]
|
|
261
|
-
);
|
|
262
|
-
const jwk = rawPublicToJwk(curve.payloadSize, curve.web.web, V);
|
|
263
|
-
let sender = webCrypto.importKey(
|
|
264
|
-
"jwk",
|
|
265
|
-
jwk,
|
|
266
|
-
{
|
|
267
|
-
name: "ECDH",
|
|
268
|
-
namedCurve: curve.web.web
|
|
269
|
-
},
|
|
270
|
-
true,
|
|
271
|
-
[]
|
|
272
|
-
);
|
|
273
|
-
[privateKey, sender] = await Promise.all([privateKey, sender]);
|
|
274
|
-
let S = webCrypto.deriveBits(
|
|
275
|
-
{
|
|
276
|
-
name: "ECDH",
|
|
277
|
-
namedCurve: curve.web.web,
|
|
278
|
-
public: sender
|
|
279
|
-
},
|
|
280
|
-
privateKey,
|
|
281
|
-
curve.web.sharedSize
|
|
282
|
-
);
|
|
283
|
-
let secret = webCrypto.exportKey(
|
|
284
|
-
"jwk",
|
|
285
|
-
privateKey
|
|
286
|
-
);
|
|
287
|
-
[S, secret] = await Promise.all([S, secret]);
|
|
288
|
-
const sharedKey = new Uint8Array(S);
|
|
289
|
-
const secretKey = util.b64_to_Uint8Array(secret.d, true);
|
|
290
|
-
return { secretKey, sharedKey };
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/**
|
|
294
|
-
* Generate ECDHE ephemeral key and secret from public key using webCrypto
|
|
295
|
-
*
|
|
296
|
-
* @param {Curve} curve Elliptic curve object
|
|
297
|
-
* @param {Uint8Array} Q Recipient public key
|
|
298
|
-
* @returns {Promise<{publicKey: Uint8Array, sharedKey: Uint8Array}>}
|
|
299
|
-
* @async
|
|
300
|
-
*/
|
|
301
|
-
async function webPublicEphemeralKey(curve, Q) {
|
|
302
|
-
const jwk = rawPublicToJwk(curve.payloadSize, curve.web.web, Q);
|
|
303
|
-
let keyPair = webCrypto.generateKey(
|
|
304
|
-
{
|
|
305
|
-
name: "ECDH",
|
|
306
|
-
namedCurve: curve.web.web
|
|
307
|
-
},
|
|
308
|
-
true,
|
|
309
|
-
["deriveKey", "deriveBits"]
|
|
310
|
-
);
|
|
311
|
-
let recipient = webCrypto.importKey(
|
|
312
|
-
"jwk",
|
|
313
|
-
jwk,
|
|
314
|
-
{
|
|
315
|
-
name: "ECDH",
|
|
316
|
-
namedCurve: curve.web.web
|
|
317
|
-
},
|
|
318
|
-
false,
|
|
319
|
-
[]
|
|
320
|
-
);
|
|
321
|
-
[keyPair, recipient] = await Promise.all([keyPair, recipient]);
|
|
322
|
-
let s = webCrypto.deriveBits(
|
|
323
|
-
{
|
|
324
|
-
name: "ECDH",
|
|
325
|
-
namedCurve: curve.web.web,
|
|
326
|
-
public: recipient
|
|
327
|
-
},
|
|
328
|
-
keyPair.privateKey,
|
|
329
|
-
curve.web.sharedSize
|
|
330
|
-
);
|
|
331
|
-
let p = webCrypto.exportKey(
|
|
332
|
-
"jwk",
|
|
333
|
-
keyPair.publicKey
|
|
334
|
-
);
|
|
335
|
-
[s, p] = await Promise.all([s, p]);
|
|
336
|
-
const sharedKey = new Uint8Array(s);
|
|
337
|
-
const publicKey = new Uint8Array(jwkToRawPublic(p));
|
|
338
|
-
return { publicKey, sharedKey };
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Generate ECDHE secret from private key and public part of ephemeral key using indutny/elliptic
|
|
343
|
-
*
|
|
344
|
-
* @param {Curve} curve Elliptic curve object
|
|
345
|
-
* @param {Uint8Array} V Public part of ephemeral key
|
|
346
|
-
* @param {Uint8Array} d Recipient private key
|
|
347
|
-
* @returns {Promise<{secretKey: Uint8Array, sharedKey: Uint8Array}>}
|
|
348
|
-
* @async
|
|
349
|
-
*/
|
|
350
|
-
async function ellipticPrivateEphemeralKey(curve, V, d) {
|
|
351
|
-
const indutnyCurve = await getIndutnyCurve(curve.name);
|
|
352
|
-
V = keyFromPublic(indutnyCurve, V);
|
|
353
|
-
d = keyFromPrivate(indutnyCurve, d);
|
|
354
|
-
const secretKey = new Uint8Array(d.getPrivate());
|
|
355
|
-
const S = d.derive(V.getPublic());
|
|
356
|
-
const len = indutnyCurve.curve.p.byteLength();
|
|
357
|
-
const sharedKey = S.toArrayLike(Uint8Array, 'be', len);
|
|
358
|
-
return { secretKey, sharedKey };
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Generate ECDHE ephemeral key and secret from public key using indutny/elliptic
|
|
363
|
-
*
|
|
364
|
-
* @param {Curve} curve Elliptic curve object
|
|
365
|
-
* @param {Uint8Array} Q Recipient public key
|
|
366
|
-
* @returns {Promise<{publicKey: Uint8Array, sharedKey: Uint8Array}>}
|
|
367
|
-
* @async
|
|
368
|
-
*/
|
|
369
|
-
async function ellipticPublicEphemeralKey(curve, Q) {
|
|
370
|
-
const indutnyCurve = await getIndutnyCurve(curve.name);
|
|
371
|
-
const v = await curve.genKeyPair();
|
|
372
|
-
Q = keyFromPublic(indutnyCurve, Q);
|
|
373
|
-
const V = keyFromPrivate(indutnyCurve, v.privateKey);
|
|
374
|
-
const publicKey = v.publicKey;
|
|
375
|
-
const S = V.derive(Q.getPublic());
|
|
376
|
-
const len = indutnyCurve.curve.p.byteLength();
|
|
377
|
-
const sharedKey = S.toArrayLike(Uint8Array, 'be', len);
|
|
378
|
-
return { publicKey, sharedKey };
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
/**
|
|
382
|
-
* Generate ECDHE secret from private key and public part of ephemeral key using nodeCrypto
|
|
383
|
-
*
|
|
384
|
-
* @param {Curve} curve Elliptic curve object
|
|
385
|
-
* @param {Uint8Array} V Public part of ephemeral key
|
|
386
|
-
* @param {Uint8Array} d Recipient private key
|
|
387
|
-
* @returns {Promise<{secretKey: Uint8Array, sharedKey: Uint8Array}>}
|
|
388
|
-
* @async
|
|
389
|
-
*/
|
|
390
|
-
async function nodePrivateEphemeralKey(curve, V, d) {
|
|
391
|
-
const recipient = nodeCrypto.createECDH(curve.node.node);
|
|
392
|
-
recipient.setPrivateKey(d);
|
|
393
|
-
const sharedKey = new Uint8Array(recipient.computeSecret(V));
|
|
394
|
-
const secretKey = new Uint8Array(recipient.getPrivateKey());
|
|
395
|
-
return { secretKey, sharedKey };
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* Generate ECDHE ephemeral key and secret from public key using nodeCrypto
|
|
400
|
-
*
|
|
401
|
-
* @param {Curve} curve Elliptic curve object
|
|
402
|
-
* @param {Uint8Array} Q Recipient public key
|
|
403
|
-
* @returns {Promise<{publicKey: Uint8Array, sharedKey: Uint8Array}>}
|
|
404
|
-
* @async
|
|
405
|
-
*/
|
|
406
|
-
async function nodePublicEphemeralKey(curve, Q) {
|
|
407
|
-
const sender = nodeCrypto.createECDH(curve.node.node);
|
|
408
|
-
sender.generateKeys();
|
|
409
|
-
const sharedKey = new Uint8Array(sender.computeSecret(Q));
|
|
410
|
-
const publicKey = new Uint8Array(sender.getPublicKey());
|
|
411
|
-
return { publicKey, sharedKey };
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
export default { encrypt, decrypt, genPublicEphemeralKey, genPrivateEphemeralKey, buildEcdhParam, kdf, webPublicEphemeralKey, webPrivateEphemeralKey, ellipticPublicEphemeralKey, ellipticPrivateEphemeralKey, nodePublicEphemeralKey, nodePrivateEphemeralKey, validateParams, parseParams };
|