@gjsify/crypto 0.3.13 → 0.3.15
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/lib/esm/asn1.js +576 -450
- package/lib/esm/bigint-math.js +37 -28
- package/lib/esm/cipher.js +1252 -1229
- package/lib/esm/constants.js +12 -13
- package/lib/esm/crypto-utils.js +54 -36
- package/lib/esm/dh.js +408 -368
- package/lib/esm/ecdh.js +403 -321
- package/lib/esm/ecdsa.js +138 -111
- package/lib/esm/hash.js +100 -89
- package/lib/esm/hkdf.js +65 -47
- package/lib/esm/hmac.js +95 -90
- package/lib/esm/index.js +75 -148
- package/lib/esm/key-object.js +348 -307
- package/lib/esm/mgf1.js +30 -24
- package/lib/esm/pbkdf2.js +66 -59
- package/lib/esm/public-encrypt.js +203 -156
- package/lib/esm/random.js +137 -124
- package/lib/esm/rsa-oaep.js +94 -87
- package/lib/esm/rsa-pss.js +95 -88
- package/lib/esm/scrypt.js +116 -115
- package/lib/esm/sign.js +267 -237
- package/lib/esm/timing-safe-equal.js +16 -11
- package/lib/esm/x509.js +215 -206
- package/package.json +7 -7
package/lib/esm/ecdh.js
CHANGED
|
@@ -1,356 +1,438 @@
|
|
|
1
|
-
import { Buffer } from "node:buffer";
|
|
2
|
-
import { randomBytes } from "./random.js";
|
|
3
1
|
import { modPow } from "./bigint-math.js";
|
|
2
|
+
import { randomBytes } from "./random.js";
|
|
3
|
+
import { Buffer } from "node:buffer";
|
|
4
|
+
|
|
5
|
+
//#region src/ecdh.ts
|
|
4
6
|
const CURVES = {
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
7
|
+
secp256k1: {
|
|
8
|
+
p: 115792089237316195423570985008687907853269984665640564039457584007908834671663n,
|
|
9
|
+
a: 0n,
|
|
10
|
+
b: 7n,
|
|
11
|
+
Gx: 55066263022277343669578718895168534326250603453777594175500187360389116729240n,
|
|
12
|
+
Gy: 32670510020758816978083085130507043184471273380659243275938904335757337482424n,
|
|
13
|
+
n: 115792089237316195423570985008687907852837564279074904382605163141518161494337n,
|
|
14
|
+
byteLength: 32
|
|
15
|
+
},
|
|
16
|
+
prime256v1: {
|
|
17
|
+
p: 115792089210356248762697446949407573530086143415290314195533631308867097853951n,
|
|
18
|
+
a: 115792089210356248762697446949407573530086143415290314195533631308867097853951n - 3n,
|
|
19
|
+
b: 41058363725152142129326129780047268409114441015993725554835256314039467401291n,
|
|
20
|
+
Gx: 48439561293906451759052585252797914202762949526041747995844080717082404635286n,
|
|
21
|
+
Gy: 36134250956749795798585127919587881956611106672985015071877198253568414405109n,
|
|
22
|
+
n: 115792089210356248762697446949407573529996955224135760342422259061068512044369n,
|
|
23
|
+
byteLength: 32
|
|
24
|
+
},
|
|
25
|
+
secp384r1: {
|
|
26
|
+
p: 39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319n,
|
|
27
|
+
a: 39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319n - 3n,
|
|
28
|
+
b: 27580193559959705877849011840389048093056905856361568521428707301988689241309860865136260764883745107765439761230575n,
|
|
29
|
+
Gx: 26247035095799689268623156744566981891852923491109213387815615900925518854738050089022388053975719786650872476732087n,
|
|
30
|
+
Gy: 8325710961489029985546751289520108179287853048861315594709205902480503199884419224438643760392947333078086511627871n,
|
|
31
|
+
n: 39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643n,
|
|
32
|
+
byteLength: 48
|
|
33
|
+
},
|
|
34
|
+
secp521r1: {
|
|
35
|
+
p: 6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151n,
|
|
36
|
+
a: 6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151n - 3n,
|
|
37
|
+
b: 1093849038073734274511112390766805569936207598951683748994586394495953116150735016013708737573759623248592132296706313309438452531591012912142327488478985984n,
|
|
38
|
+
Gx: 2661740802050217063228768716723360960729859168756973147706671368418802944996427808491545080627771902352094241225065558662157113545570916814161637315895999846n,
|
|
39
|
+
Gy: 3757180025770020463545507224491183603594455134769762486694567779615544477440556316691234405012945539562144444537289428522585666729196580810124344277578376784n,
|
|
40
|
+
n: 6864797660130609714981900799081393217269435300143305409394463459185543183397655394245057746333217197532963996371363321113864768612440380340372808892707005449n,
|
|
41
|
+
byteLength: 66
|
|
42
|
+
}
|
|
41
43
|
};
|
|
42
44
|
const CURVE_ALIASES = {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
45
|
+
"secp256k1": "secp256k1",
|
|
46
|
+
"prime256v1": "prime256v1",
|
|
47
|
+
"secp256r1": "prime256v1",
|
|
48
|
+
"p-256": "prime256v1",
|
|
49
|
+
"p256": "prime256v1",
|
|
50
|
+
"secp384r1": "secp384r1",
|
|
51
|
+
"p-384": "secp384r1",
|
|
52
|
+
"p384": "secp384r1",
|
|
53
|
+
"secp521r1": "secp521r1",
|
|
54
|
+
"p-521": "secp521r1",
|
|
55
|
+
"p521": "secp521r1"
|
|
54
56
|
};
|
|
57
|
+
/**
|
|
58
|
+
* Non-negative modulus: always returns a value in [0, mod).
|
|
59
|
+
*/
|
|
55
60
|
function mod(a, m) {
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
const r = a % m;
|
|
62
|
+
return r < 0n ? r + m : r;
|
|
58
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Modular multiplicative inverse using extended Euclidean algorithm.
|
|
66
|
+
* Returns x such that (a * x) mod m = 1.
|
|
67
|
+
* Throws if a and m are not coprime.
|
|
68
|
+
*/
|
|
59
69
|
function modInverse(a, m) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
a = mod(a, m);
|
|
71
|
+
if (a === 0n) {
|
|
72
|
+
throw new Error("No modular inverse for zero");
|
|
73
|
+
}
|
|
74
|
+
let [old_r, r] = [a, m];
|
|
75
|
+
let [old_s, s] = [1n, 0n];
|
|
76
|
+
while (r !== 0n) {
|
|
77
|
+
const q = old_r / r;
|
|
78
|
+
[old_r, r] = [r, old_r - q * r];
|
|
79
|
+
[old_s, s] = [s, old_s - q * s];
|
|
80
|
+
}
|
|
81
|
+
if (old_r !== 1n) {
|
|
82
|
+
throw new Error("Modular inverse does not exist");
|
|
83
|
+
}
|
|
84
|
+
return mod(old_s, m);
|
|
75
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Add two points on the curve.
|
|
88
|
+
* Returns the point at infinity (null) when appropriate.
|
|
89
|
+
*/
|
|
76
90
|
function pointAdd(P, Q, curve) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
91
|
+
if (P === null) return Q;
|
|
92
|
+
if (Q === null) return P;
|
|
93
|
+
const { p } = curve;
|
|
94
|
+
if (P.x === Q.x) {
|
|
95
|
+
if (mod(P.y + Q.y, p) === 0n) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
return pointDouble(P, curve);
|
|
99
|
+
}
|
|
100
|
+
const dx = mod(Q.x - P.x, p);
|
|
101
|
+
const dy = mod(Q.y - P.y, p);
|
|
102
|
+
const lambda = mod(dy * modInverse(dx, p), p);
|
|
103
|
+
const x3 = mod(lambda * lambda - P.x - Q.x, p);
|
|
104
|
+
const y3 = mod(lambda * (P.x - x3) - P.y, p);
|
|
105
|
+
return {
|
|
106
|
+
x: x3,
|
|
107
|
+
y: y3
|
|
108
|
+
};
|
|
92
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Double a point on the curve.
|
|
112
|
+
*/
|
|
93
113
|
function pointDouble(P, curve) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
114
|
+
if (P === null) return null;
|
|
115
|
+
const { a, p } = curve;
|
|
116
|
+
if (P.y === 0n) return null;
|
|
117
|
+
const num = mod(3n * P.x * P.x + a, p);
|
|
118
|
+
const den = mod(2n * P.y, p);
|
|
119
|
+
const lambda = mod(num * modInverse(den, p), p);
|
|
120
|
+
const x3 = mod(lambda * lambda - 2n * P.x, p);
|
|
121
|
+
const y3 = mod(lambda * (P.x - x3) - P.y, p);
|
|
122
|
+
return {
|
|
123
|
+
x: x3,
|
|
124
|
+
y: y3
|
|
125
|
+
};
|
|
103
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Scalar multiplication using the double-and-add method (constant-time-ish
|
|
129
|
+
* with respect to the bit length of the scalar, but not fully
|
|
130
|
+
* constant-time — acceptable since we are not a production crypto library
|
|
131
|
+
* and match the behaviour of refs/create-ecdh which uses elliptic.js).
|
|
132
|
+
*
|
|
133
|
+
* Uses a fixed-window approach of width 1 (Montgomery ladder variant) to
|
|
134
|
+
* avoid the most trivial timing leaks.
|
|
135
|
+
*/
|
|
104
136
|
function scalarMul(k, P, curve) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
137
|
+
if (P === null) return null;
|
|
138
|
+
if (k === 0n) return null;
|
|
139
|
+
const { n } = curve;
|
|
140
|
+
k = mod(k, n);
|
|
141
|
+
if (k === 0n) return null;
|
|
142
|
+
let R0 = null;
|
|
143
|
+
let R1 = P;
|
|
144
|
+
const bits = k.toString(2);
|
|
145
|
+
for (let i = 0; i < bits.length; i++) {
|
|
146
|
+
if (bits[i] === "1") {
|
|
147
|
+
R0 = pointAdd(R0, R1, curve);
|
|
148
|
+
R1 = pointDouble(R1, curve);
|
|
149
|
+
} else {
|
|
150
|
+
R1 = pointAdd(R0, R1, curve);
|
|
151
|
+
R0 = pointDouble(R0, curve);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return R0;
|
|
123
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* Convert a BigInt to a Buffer of exactly `len` bytes (big-endian,
|
|
158
|
+
* zero-padded on the left).
|
|
159
|
+
*/
|
|
124
160
|
function bigintToBuffer(n, len) {
|
|
125
|
-
|
|
126
|
-
|
|
161
|
+
const hex = n.toString(16).padStart(len * 2, "0");
|
|
162
|
+
return Buffer.from(hex, "hex");
|
|
127
163
|
}
|
|
164
|
+
/**
|
|
165
|
+
* Convert a Buffer (big-endian unsigned) to a BigInt.
|
|
166
|
+
*/
|
|
128
167
|
function bufferToBigint(buf) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
168
|
+
const hex = Buffer.from(buf).toString("hex");
|
|
169
|
+
if (hex.length === 0) return 0n;
|
|
170
|
+
return BigInt("0x" + hex);
|
|
132
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* Encode a public key point to a Buffer.
|
|
174
|
+
*
|
|
175
|
+
* Formats:
|
|
176
|
+
* 'uncompressed' (default) — 0x04 || x || y
|
|
177
|
+
* 'compressed' — 0x02 (even y) or 0x03 (odd y) || x
|
|
178
|
+
* 'hybrid' — 0x06 (even y) or 0x07 (odd y) || x || y
|
|
179
|
+
*/
|
|
133
180
|
function encodePublicKey(point, byteLength, format = "uncompressed") {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
181
|
+
if (point === null) {
|
|
182
|
+
throw new Error("Cannot encode the point at infinity");
|
|
183
|
+
}
|
|
184
|
+
const xBuf = bigintToBuffer(point.x, byteLength);
|
|
185
|
+
if (format === "compressed") {
|
|
186
|
+
const prefix = point.y % 2n === 0n ? 2 : 3;
|
|
187
|
+
return Buffer.concat([Buffer.from([prefix]), xBuf]);
|
|
188
|
+
}
|
|
189
|
+
const yBuf = bigintToBuffer(point.y, byteLength);
|
|
190
|
+
if (format === "hybrid") {
|
|
191
|
+
const prefix = point.y % 2n === 0n ? 6 : 7;
|
|
192
|
+
return Buffer.concat([
|
|
193
|
+
Buffer.from([prefix]),
|
|
194
|
+
xBuf,
|
|
195
|
+
yBuf
|
|
196
|
+
]);
|
|
197
|
+
}
|
|
198
|
+
return Buffer.concat([
|
|
199
|
+
Buffer.from([4]),
|
|
200
|
+
xBuf,
|
|
201
|
+
yBuf
|
|
202
|
+
]);
|
|
148
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Decode a public key from a Buffer to an ECPoint.
|
|
206
|
+
*
|
|
207
|
+
* Supports uncompressed (0x04), compressed (0x02/0x03), and hybrid (0x06/0x07).
|
|
208
|
+
*/
|
|
149
209
|
function decodePublicKey(buf, curve) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
210
|
+
const data = Buffer.from(buf);
|
|
211
|
+
if (data.length === 0) {
|
|
212
|
+
throw new Error("Invalid public key: empty buffer");
|
|
213
|
+
}
|
|
214
|
+
const prefix = data[0];
|
|
215
|
+
const { p, a, b, byteLength } = curve;
|
|
216
|
+
if (prefix === 4 || prefix === 6 || prefix === 7) {
|
|
217
|
+
if (data.length !== 1 + 2 * byteLength) {
|
|
218
|
+
throw new Error(`Invalid public key length: expected ${1 + 2 * byteLength} bytes, got ${data.length}`);
|
|
219
|
+
}
|
|
220
|
+
const x = bufferToBigint(data.subarray(1, 1 + byteLength));
|
|
221
|
+
const y = bufferToBigint(data.subarray(1 + byteLength));
|
|
222
|
+
const lhs = mod(y * y, p);
|
|
223
|
+
const rhs = mod(x * x * x + a * x + b, p);
|
|
224
|
+
if (lhs !== rhs) {
|
|
225
|
+
throw new Error("Invalid public key: point is not on the curve");
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
x,
|
|
229
|
+
y
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
if (prefix === 2 || prefix === 3) {
|
|
233
|
+
if (data.length !== 1 + byteLength) {
|
|
234
|
+
throw new Error(`Invalid public key length: expected ${1 + byteLength} bytes, got ${data.length}`);
|
|
235
|
+
}
|
|
236
|
+
const x = bufferToBigint(data.subarray(1, 1 + byteLength));
|
|
237
|
+
const ySquared = mod(x * x * x + a * x + b, p);
|
|
238
|
+
const y = sqrtMod(ySquared, p);
|
|
239
|
+
if (y === null) {
|
|
240
|
+
throw new Error("Invalid public key: no valid y coordinate for the given x");
|
|
241
|
+
}
|
|
242
|
+
const isOdd = prefix === 3;
|
|
243
|
+
const finalY = y % 2n !== 0n === isOdd ? y : mod(p - y, p);
|
|
244
|
+
return {
|
|
245
|
+
x,
|
|
246
|
+
y: finalY
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
throw new Error(`Invalid public key prefix: 0x${prefix.toString(16).padStart(2, "0")}`);
|
|
188
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* Compute the modular square root of a modulo p (Tonelli-Shanks algorithm).
|
|
253
|
+
*
|
|
254
|
+
* For the special case p ≡ 3 (mod 4), uses the simpler formula: a^((p+1)/4) mod p.
|
|
255
|
+
* All four supported NIST curves have p ≡ 3 (mod 4), but we include
|
|
256
|
+
* Tonelli-Shanks for completeness.
|
|
257
|
+
*
|
|
258
|
+
* Returns null if a is not a quadratic residue mod p.
|
|
259
|
+
*/
|
|
189
260
|
function sqrtMod(a, p) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
261
|
+
a = mod(a, p);
|
|
262
|
+
if (a === 0n) return 0n;
|
|
263
|
+
if (modPow(a, (p - 1n) / 2n, p) !== 1n) {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
if (mod(p, 4n) === 3n) {
|
|
267
|
+
return modPow(a, (p + 1n) / 4n, p);
|
|
268
|
+
}
|
|
269
|
+
let S = 0n;
|
|
270
|
+
let Q = p - 1n;
|
|
271
|
+
while (mod(Q, 2n) === 0n) {
|
|
272
|
+
Q /= 2n;
|
|
273
|
+
S++;
|
|
274
|
+
}
|
|
275
|
+
let z = 2n;
|
|
276
|
+
while (modPow(z, (p - 1n) / 2n, p) !== p - 1n) {
|
|
277
|
+
z++;
|
|
278
|
+
}
|
|
279
|
+
let M = S;
|
|
280
|
+
let c = modPow(z, Q, p);
|
|
281
|
+
let t = modPow(a, Q, p);
|
|
282
|
+
let R = modPow(a, (Q + 1n) / 2n, p);
|
|
283
|
+
while (true) {
|
|
284
|
+
if (t === 0n) return 0n;
|
|
285
|
+
if (t === 1n) return R;
|
|
286
|
+
let i = 1n;
|
|
287
|
+
let temp = mod(t * t, p);
|
|
288
|
+
while (temp !== 1n) {
|
|
289
|
+
temp = mod(temp * temp, p);
|
|
290
|
+
i++;
|
|
291
|
+
}
|
|
292
|
+
const b = modPow(c, modPow(2n, M - i - 1n, p - 1n), p);
|
|
293
|
+
M = i;
|
|
294
|
+
c = mod(b * b, p);
|
|
295
|
+
t = mod(t * c, p);
|
|
296
|
+
R = mod(R * b, p);
|
|
297
|
+
}
|
|
227
298
|
}
|
|
299
|
+
/**
|
|
300
|
+
* Coerce an input value to a Buffer. Accepts Buffer, Uint8Array, or a string
|
|
301
|
+
* with an optional encoding.
|
|
302
|
+
*/
|
|
228
303
|
function toBuffer(value, encoding) {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
'The "key" argument must be of type string or an instance of Buffer, TypedArray, or DataView.'
|
|
239
|
-
);
|
|
304
|
+
if (Buffer.isBuffer(value)) return value;
|
|
305
|
+
if (value instanceof Uint8Array) return Buffer.from(value);
|
|
306
|
+
if (typeof value === "string") {
|
|
307
|
+
return Buffer.from(value, encoding || "utf8");
|
|
308
|
+
}
|
|
309
|
+
if (ArrayBuffer.isView(value)) {
|
|
310
|
+
return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
311
|
+
}
|
|
312
|
+
throw new TypeError("The \"key\" argument must be of type string or an instance of Buffer, TypedArray, or DataView.");
|
|
240
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Optionally encode a Buffer to a string. If encoding is null/undefined,
|
|
316
|
+
* return the raw Buffer.
|
|
317
|
+
*/
|
|
241
318
|
function formatReturnValue(buf, encoding) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
319
|
+
if (encoding) {
|
|
320
|
+
return buf.toString(encoding);
|
|
321
|
+
}
|
|
322
|
+
return buf;
|
|
246
323
|
}
|
|
247
324
|
function resolveCurve(curveName) {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
_curveName;
|
|
262
|
-
_privateKey = null;
|
|
263
|
-
_publicKey = null;
|
|
264
|
-
constructor(curveName) {
|
|
265
|
-
const resolved = resolveCurve(curveName);
|
|
266
|
-
this._curve = resolved.params;
|
|
267
|
-
this._curveName = resolved.canonical;
|
|
268
|
-
}
|
|
269
|
-
generateKeys(encoding, format) {
|
|
270
|
-
const { n, byteLength } = this._curve;
|
|
271
|
-
let k;
|
|
272
|
-
do {
|
|
273
|
-
const bytes = randomBytes(byteLength);
|
|
274
|
-
k = bufferToBigint(bytes);
|
|
275
|
-
} while (k === 0n || k >= n);
|
|
276
|
-
this._privateKey = k;
|
|
277
|
-
const G = { x: this._curve.Gx, y: this._curve.Gy };
|
|
278
|
-
this._publicKey = scalarMul(k, G, this._curve);
|
|
279
|
-
return this.getPublicKey(encoding, format);
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* Compute the shared secret using the other party's public key.
|
|
283
|
-
*/
|
|
284
|
-
computeSecret(otherPublicKey, inputEncoding, outputEncoding) {
|
|
285
|
-
if (this._privateKey === null) {
|
|
286
|
-
throw new Error("ECDH key not generated. Call generateKeys() or setPrivateKey() first.");
|
|
287
|
-
}
|
|
288
|
-
const pubBuf = toBuffer(otherPublicKey, inputEncoding);
|
|
289
|
-
const otherPoint = decodePublicKey(pubBuf, this._curve);
|
|
290
|
-
if (otherPoint === null) {
|
|
291
|
-
throw new Error("Invalid public key: point at infinity");
|
|
292
|
-
}
|
|
293
|
-
const sharedPoint = scalarMul(this._privateKey, otherPoint, this._curve);
|
|
294
|
-
if (sharedPoint === null) {
|
|
295
|
-
throw new Error("Shared secret computation resulted in the point at infinity");
|
|
296
|
-
}
|
|
297
|
-
const secret = bigintToBuffer(sharedPoint.x, this._curve.byteLength);
|
|
298
|
-
return formatReturnValue(secret, outputEncoding);
|
|
299
|
-
}
|
|
300
|
-
getPublicKey(encoding, format) {
|
|
301
|
-
if (this._publicKey === null) {
|
|
302
|
-
throw new Error("ECDH key not generated. Call generateKeys() or setPrivateKey() first.");
|
|
303
|
-
}
|
|
304
|
-
const buf = encodePublicKey(this._publicKey, this._curve.byteLength, format || "uncompressed");
|
|
305
|
-
return formatReturnValue(buf, encoding);
|
|
306
|
-
}
|
|
307
|
-
getPrivateKey(encoding) {
|
|
308
|
-
if (this._privateKey === null) {
|
|
309
|
-
throw new Error("ECDH key not generated. Call generateKeys() or setPrivateKey() first.");
|
|
310
|
-
}
|
|
311
|
-
const buf = bigintToBuffer(this._privateKey, this._curve.byteLength);
|
|
312
|
-
return formatReturnValue(buf, encoding);
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Set the public key. The key can be in uncompressed, compressed, or hybrid format.
|
|
316
|
-
*/
|
|
317
|
-
setPublicKey(key, encoding) {
|
|
318
|
-
const buf = toBuffer(key, encoding);
|
|
319
|
-
this._publicKey = decodePublicKey(buf, this._curve);
|
|
320
|
-
}
|
|
321
|
-
/**
|
|
322
|
-
* Set the private key and derive the corresponding public key.
|
|
323
|
-
*/
|
|
324
|
-
setPrivateKey(key, encoding) {
|
|
325
|
-
const buf = toBuffer(key, encoding);
|
|
326
|
-
const k = bufferToBigint(buf);
|
|
327
|
-
if (k === 0n || k >= this._curve.n) {
|
|
328
|
-
throw new Error("Private key is out of range [1, n-1]");
|
|
329
|
-
}
|
|
330
|
-
this._privateKey = k;
|
|
331
|
-
const G = { x: this._curve.Gx, y: this._curve.Gy };
|
|
332
|
-
this._publicKey = scalarMul(k, G, this._curve);
|
|
333
|
-
}
|
|
325
|
+
const lower = curveName.toLowerCase();
|
|
326
|
+
const canonical = CURVE_ALIASES[lower];
|
|
327
|
+
if (!canonical) {
|
|
328
|
+
throw new Error(`Unsupported curve: ${curveName}`);
|
|
329
|
+
}
|
|
330
|
+
const params = CURVES[canonical];
|
|
331
|
+
if (!params) {
|
|
332
|
+
throw new Error(`Unsupported curve: ${curveName}`);
|
|
333
|
+
}
|
|
334
|
+
return {
|
|
335
|
+
canonical,
|
|
336
|
+
params
|
|
337
|
+
};
|
|
334
338
|
}
|
|
339
|
+
var ECDH = class {
|
|
340
|
+
_curve;
|
|
341
|
+
_curveName;
|
|
342
|
+
_privateKey = null;
|
|
343
|
+
_publicKey = null;
|
|
344
|
+
constructor(curveName) {
|
|
345
|
+
const resolved = resolveCurve(curveName);
|
|
346
|
+
this._curve = resolved.params;
|
|
347
|
+
this._curveName = resolved.canonical;
|
|
348
|
+
}
|
|
349
|
+
generateKeys(encoding, format) {
|
|
350
|
+
const { n, byteLength } = this._curve;
|
|
351
|
+
let k;
|
|
352
|
+
do {
|
|
353
|
+
const bytes = randomBytes(byteLength);
|
|
354
|
+
k = bufferToBigint(bytes);
|
|
355
|
+
} while (k === 0n || k >= n);
|
|
356
|
+
this._privateKey = k;
|
|
357
|
+
const G = {
|
|
358
|
+
x: this._curve.Gx,
|
|
359
|
+
y: this._curve.Gy
|
|
360
|
+
};
|
|
361
|
+
this._publicKey = scalarMul(k, G, this._curve);
|
|
362
|
+
return this.getPublicKey(encoding, format);
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Compute the shared secret using the other party's public key.
|
|
366
|
+
*/
|
|
367
|
+
computeSecret(otherPublicKey, inputEncoding, outputEncoding) {
|
|
368
|
+
if (this._privateKey === null) {
|
|
369
|
+
throw new Error("ECDH key not generated. Call generateKeys() or setPrivateKey() first.");
|
|
370
|
+
}
|
|
371
|
+
const pubBuf = toBuffer(otherPublicKey, inputEncoding);
|
|
372
|
+
const otherPoint = decodePublicKey(pubBuf, this._curve);
|
|
373
|
+
if (otherPoint === null) {
|
|
374
|
+
throw new Error("Invalid public key: point at infinity");
|
|
375
|
+
}
|
|
376
|
+
const sharedPoint = scalarMul(this._privateKey, otherPoint, this._curve);
|
|
377
|
+
if (sharedPoint === null) {
|
|
378
|
+
throw new Error("Shared secret computation resulted in the point at infinity");
|
|
379
|
+
}
|
|
380
|
+
const secret = bigintToBuffer(sharedPoint.x, this._curve.byteLength);
|
|
381
|
+
return formatReturnValue(secret, outputEncoding);
|
|
382
|
+
}
|
|
383
|
+
getPublicKey(encoding, format) {
|
|
384
|
+
if (this._publicKey === null) {
|
|
385
|
+
throw new Error("ECDH key not generated. Call generateKeys() or setPrivateKey() first.");
|
|
386
|
+
}
|
|
387
|
+
const buf = encodePublicKey(this._publicKey, this._curve.byteLength, format || "uncompressed");
|
|
388
|
+
return formatReturnValue(buf, encoding);
|
|
389
|
+
}
|
|
390
|
+
getPrivateKey(encoding) {
|
|
391
|
+
if (this._privateKey === null) {
|
|
392
|
+
throw new Error("ECDH key not generated. Call generateKeys() or setPrivateKey() first.");
|
|
393
|
+
}
|
|
394
|
+
const buf = bigintToBuffer(this._privateKey, this._curve.byteLength);
|
|
395
|
+
return formatReturnValue(buf, encoding);
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Set the public key. The key can be in uncompressed, compressed, or hybrid format.
|
|
399
|
+
*/
|
|
400
|
+
setPublicKey(key, encoding) {
|
|
401
|
+
const buf = toBuffer(key, encoding);
|
|
402
|
+
this._publicKey = decodePublicKey(buf, this._curve);
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Set the private key and derive the corresponding public key.
|
|
406
|
+
*/
|
|
407
|
+
setPrivateKey(key, encoding) {
|
|
408
|
+
const buf = toBuffer(key, encoding);
|
|
409
|
+
const k = bufferToBigint(buf);
|
|
410
|
+
if (k === 0n || k >= this._curve.n) {
|
|
411
|
+
throw new Error("Private key is out of range [1, n-1]");
|
|
412
|
+
}
|
|
413
|
+
this._privateKey = k;
|
|
414
|
+
const G = {
|
|
415
|
+
x: this._curve.Gx,
|
|
416
|
+
y: this._curve.Gy
|
|
417
|
+
};
|
|
418
|
+
this._publicKey = scalarMul(k, G, this._curve);
|
|
419
|
+
}
|
|
420
|
+
};
|
|
335
421
|
function createECDH(curveName) {
|
|
336
|
-
|
|
422
|
+
return new ECDH(curveName);
|
|
337
423
|
}
|
|
424
|
+
/**
|
|
425
|
+
* Return a list of supported elliptic curve names.
|
|
426
|
+
*/
|
|
338
427
|
function getCurves() {
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
428
|
+
return [
|
|
429
|
+
"secp256k1",
|
|
430
|
+
"prime256v1",
|
|
431
|
+
"secp256r1",
|
|
432
|
+
"secp384r1",
|
|
433
|
+
"secp521r1"
|
|
434
|
+
];
|
|
346
435
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
createECDH,
|
|
351
|
-
getCurves,
|
|
352
|
-
mod,
|
|
353
|
-
modInverse,
|
|
354
|
-
pointAdd,
|
|
355
|
-
scalarMul
|
|
356
|
-
};
|
|
436
|
+
|
|
437
|
+
//#endregion
|
|
438
|
+
export { CURVES, CURVE_ALIASES, createECDH, getCurves, mod, modInverse, pointAdd, scalarMul };
|