-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg.js
467 lines (432 loc) · 9.89 KB
/
alg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
188
189
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
"use strict";
// For more documentation and notes, see the main Coze README.
export {
Algs,
FamAlgs,
GenAlgs,
Curves,
Uses,
Params,
Curve,
Family,
Genus,
HashAlg,
HashSize,
SigSize,
XSize,
DSize,
Use,
CurveOrder,
CurveHalfOrder,
}
/**
@typedef {import('./typedef.js').Params} Params
@typedef {import('./typedef.js').Alg} Alg
@typedef {import('./typedef.js').Hsh} Hsh
@typedef {import('./typedef.js').Gen} Gen
@typedef {import('./typedef.js').Fam} Fam
@typedef {import('./typedef.js').Crv} Crv
@typedef {import('./typedef.js').Use} Use
*/
/**
Algs holds all of the supported Coze algorithms.
*/
const Algs = {
UnknownAlg: "UnknownAlg",
ES224: "ES224",
ES256: "ES256",
ES384: "ES384",
ES512: "ES512",
Ed25519: "Ed25519",
Ed25519ph: "Ed25519ph",
Ed448: "Ed448",
SHA224: "SHA-224",
SHA256: "SHA-256",
SHA384: "SHA-384",
SHA512: "SHA-512",
SHA3224: "SHA3-224",
SHA3256: "SHA3-256",
SHA3384: "SHA3-384",
SHA3512: "SHA3-512",
SHAKE128: "SHAKE128",
SHAKE256: "SHAKE256",
};
/**
FamAlgs holds all of the supported Coze Family algorithms.
*/
const FamAlgs = {
EC: "EC",
SHA: "SHA",
RSA: "RSA",
};
/**
GenAlgs holds all of the supported Coze Genus algorithms.
*/
const GenAlgs = {
ECDSA: "ECDSA",
EdDSA: "EdDSA",
SHA2: "SHA2",
SHA3: "SHA3",
};
/**
Curves holds all of the supported Coze curve algorithms.
*/
const Curves = {
P224: "P-224",
P256: "P-256",
P384: "P-384",
P521: "P-521",
Curve25519: "Curve25519",
Curve448: "Curve448",
};
/**
Uses holds all of the supported Coze uses.
*/
const Uses = {
Sig: "sig",
Enc: "enc",
Hsh: "hsh",
};
/**
Param reports all relevant values for a given `alg`.
Returns Params object with populated values for relevant fields.
All functions defined in this file will throw an error when given an
unsupported algorithm.
@param {Alg} alg
@returns {Params}
@throws {error}
*/
function Params(alg) {
/** @type {Params} */
let p = {};
p.Name = alg;
p.Genus = Genus(alg);
p.Family = Family(alg);
p.Use = Use(alg);
p.Hash = HashAlg(alg);
p.HashSize = HashSize(alg);
p.HashSizeB64 = Math.ceil(4 * p.HashSize / 3);
// SigAlg parameters
try {
p.XSize = XSize(alg);
p.XSizeB64 = Math.ceil(4 * p.XSize / 3);
p.DSize = DSize(alg);
p.DSizeB64 = Math.ceil(4 * p.DSize / 3);
p.Curve = Curve(alg);
p.SigSize = SigSize(alg);
p.SigSizeB64 = Math.ceil(4 * p.SigSize / 3);
} catch (e) {
// ignore error
}
return p;
}
/**
Genus returns the genus for an alg (ECDSA, EdDSA, SHA-2, SHA-3).
See notes on the Go implementation of Coze for more on genus.
@param {Alg} alg
@returns {Gen}
@throws {error}
*/
function Genus(alg) {
switch (alg) {
case Algs.ES224:
case Algs.ES256:
case Algs.ES384:
case Algs.ES512:
return GenAlgs.ECDSA;
case Algs.Ed25519:
case Algs.Ed25519ph:
case Algs.Ed448:
return GenAlgs.EdDSA;
case Algs.SHA224:
case Algs.SHA256:
case Algs.SHA384:
case Algs.SHA512:
return GenAlgs.SHA2;
case Algs.SHA3224:
case Algs.SHA3256:
case Algs.SHA3384:
case Algs.SHA3512:
case Algs.SHAKE128:
case Algs.SHAKE256:
return GenAlgs.SHA3;
default:
throw new Error("alg.Genus: unsupported algorithm: " + alg);
}
}
/**
Family returns the family for an alg (EC and SHA).
See notes on the Go implementation of Coze for more on family.
@param {Alg} alg
@returns {Fam}
@throws {error}
*/
function Family(alg) {
switch (alg) {
case Algs.ES224:
case Algs.ES256:
case Algs.ES384:
case Algs.ES512:
case Algs.Ed25519:
case Algs.Ed25519ph:
case Algs.Ed448:
return FamAlgs.EC;
case Algs.SHA224:
case Algs.SHA256:
case Algs.SHA384:
case Algs.SHA512:
case Algs.SHA3224:
case Algs.SHA3256:
case Algs.SHA3384:
case Algs.SHA3512:
case Algs.SHAKE128:
case Algs.SHAKE256:
return FamAlgs.SHA
default:
throw new Error("alg.Family: unsupported algorithm: " + alg);
}
}
/**
Hash returns the hashing algorithm for the given algorithm. A hash alg can
return itself.
See notes on the Go implementation of Coze for more.
@param {Alg} alg
@returns {Hsh}
@throws {error} Unsupported algorithm.
*/
function HashAlg(alg) {
switch (alg) {
case Algs.ES224:
case Algs.SHA224:
return Algs.SHA224;
case Algs.SHA256:
case Algs.ES256:
return Algs.SHA256;
case Algs.SHA384:
case Algs.ES384:
return Algs.SHA384;
case Algs.SHA512:
case Algs.ES512: // P-521 is not ES512/SHA-512. The curve != the alg/hash.
case Algs.Ed25519:
case Algs.Ed25519ph:
return Algs.SHA512;
case Algs.SHAKE128:
return Algs.SHAKE128
case Algs.SHAKE256:
case Algs.Ed448:
return Algs.SHAKE256
case Algs.SHA3224:
return Algs.SHA3224
case Algs.SHA3256:
return Algs.SHA3256
case Algs.SHA3384:
return Algs.SHA3384
case Algs.SHA3512:
return Algs.SHA3512
default:
throw new Error("alg.HashAlg: unsupported algorithm: " + alg);
}
}
/**
HashSize returns the hashing algorithm size for the given algorithm in bytes
E.g. 32.
SHAKE128 has 128 bits of pre-collision resistance and a capacity of 256,
although it has arbitrary output size. SHAKE256 has 256 bits of pre-collision
resistance and a capacity of 512, although it has arbitrary output size.
See notes on the Go implementation of Coze for more.
@param {Alg} alg
@returns {number}
@throws {error}
*/
function HashSize(alg) {
switch (HashAlg(alg)) {
case Algs.SHA224:
case Algs.SHA3224:
return 28;
case Algs.SHA256:
case Algs.SHA3256:
case Algs.SHAKE128:
return 32;
case Algs.SHA384:
case Algs.SHA3384:
return 48;
case Algs.SHA512:
case Algs.SHA3512:
case Algs.SHAKE256:
return 64;
default:
throw new Error("alg.HashSize: unsupported algorithm: " + alg);
}
}
/**
SigSize returns the signature size for the given algorithm in bytes.
Curve P-521 uses 521 bits. This is then padded up the the nearest byte (528)
for R and S. 132 = (528*2)/8
See notes on the Go implementation of Coze for more.
@param {Alg} alg
@returns {number}
@throws {error}
*/
function SigSize(alg) {
switch (alg) {
case Algs.ES224:
return 56
case Algs.ES256:
case Algs.Ed25519:
case Algs.Ed25519ph:
return 64
case Algs.ES384:
return 96
case Algs.Ed448:
return 114
case Algs.ES512:
return 132
default:
throw new Error("alg.SigSize: unsupported algorithm: " + alg);
}
}
/**
XSize returns the signature size for the given signature algorithm in bytes.
E.g. 64.
ES512 uses Curve P-521 that's 521 bits is padded up the the nearest byte
(528) for R and S. (528*2)/8 = 132.
See notes on the Go implementation of Coze for more.
@param {Alg} alg
@returns {number}
@throws {error}
*/
function XSize(alg) {
switch (alg) {
case Algs.Ed25519:
case Algs.Ed25519ph:
return 32
case Algs.ES224:
return 56
case Algs.Ed448:
return 57
case Algs.ES256:
return 64
case Algs.ES384:
return 96
case Algs.ES512:
return 132 // X and Y are 66 bytes (Rounded up for P521)
default:
throw new Error("alg.XSize: unsupported algorithm: " + alg);
}
}
/**
DSize returns the signature size for the given signature algorithm in bytes.
E.g. 64.
ES512 uses Curve P-521 that's 521 bits is padded up the the nearest byte
(528). (528)/8 = 66.
See notes on the Go implementation of Coze for more.
@param {Alg} alg
@returns {number}
@throws {error}
*/
function DSize(alg) {
switch (alg) {
case Algs.ES224:
return 28
case Algs.ES256:
case Algs.Ed25519:
case Algs.Ed25519ph:
return 32
case Algs.ES384:
return 48
case Algs.Ed448:
return 57
case Algs.ES512:
return 66
default:
throw new Error("alg.DSize: unsupported algorithm: " + alg);
}
}
/**
Curve returns the curve algorithm for the given signature algorithm.
E.g. "P-256".
See notes on the Go implementation of Coze for more.
@param {Alg} alg
@returns {Crv}
@throws {error}
*/
function Curve(alg) {
switch (alg) {
default:
throw new Error("alg.Curve: unsupported algorithm: " + alg);
case Algs.ES224:
return Curves.P224;
case Algs.ES256:
return Curves.P256;
case Algs.ES384:
return Curves.P384;
case Algs.ES512: // P-521 is not ES512/SHA-512. The curve != the alg/hash.
return Curves.P521;
case Algs.Ed25519:
case Algs.Ed25519ph:
return Curves.Curve25519;
case Algs.Ed448:
return Curves.Curve448;
}
}
/**
Use returns the use for the given algorithm. Only "sig", "enc", and "dig"
are currently valid.
Encryption ("enc") is currently not supported in Coze.
See notes on the Go implementation of Coze for more.
@param {Alg} alg
@returns {Use}
@throws {error}
*/
function Use(alg) {
switch (Genus(alg)) {
default:
throw new Error("alg.Use: unsupported algorithm: " + alg);
case GenAlgs.EdDSA:
case GenAlgs.ECDSA:
return Uses.Sig;
case GenAlgs.SHA2:
case GenAlgs.SHA3:
return Uses.Hsh;
}
}
const order = {
"ES224" : BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"),
"ES256" : BigInt("0xFFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"),
"ES384" : BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"),
"ES512" : BigInt("0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409"),
}
const halfOrder = {
"ES224" : order["ES224"] >> BigInt(1),
"ES256" : order["ES256"] >> BigInt(1),
"ES384" : order["ES384"] >> BigInt(1),
"ES512" : order["ES512"] >> BigInt(1),
}
/**
Curve Order returns the Curve's order.
@param {Alg} Alg
@returns {BigInt}
@throws {error}
*/
function CurveOrder(alg) {
switch (alg) {
default:
throw new Error("CurveOrder: unsupported curve: " + alg);
case "ES224": case "ES256": case "ES384": case "ES512":
return order[alg];
}
}
/**
Curve Order returns the Curve's order halved.
@param {Alg} Alg
@returns {BigInt}
@throws {error}
*/
function CurveHalfOrder(alg) {
switch (alg) {
default:
throw new Error("CurveHalfOrder: unsupported curve: " + alg);
case "ES224": case "ES256": case "ES384": case "ES512":
return halfOrder[alg];
}
}