@ac-kit/crypto-rsa 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/LICENSE +28 -0
  3. package/README.md +113 -2
  4. package/dist/_bytes.d.ts +18 -0
  5. package/dist/_bytes.d.ts.map +1 -0
  6. package/dist/_bytes.js +42 -0
  7. package/dist/_bytes.js.map +1 -0
  8. package/dist/encoding/_der.d.ts +51 -0
  9. package/dist/encoding/_der.d.ts.map +1 -0
  10. package/dist/encoding/_der.js +173 -0
  11. package/dist/encoding/_der.js.map +1 -0
  12. package/dist/encoding/rsa-algorithm.d.ts +132 -0
  13. package/dist/encoding/rsa-algorithm.d.ts.map +1 -0
  14. package/dist/encoding/rsa-algorithm.js +475 -0
  15. package/dist/encoding/rsa-algorithm.js.map +1 -0
  16. package/dist/hash/rsa-hash.d.ts +68 -0
  17. package/dist/hash/rsa-hash.d.ts.map +1 -0
  18. package/dist/hash/rsa-hash.js +86 -0
  19. package/dist/hash/rsa-hash.js.map +1 -0
  20. package/dist/index.d.ts +9 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +9 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/key/_generate.d.ts +39 -0
  25. package/dist/key/_generate.d.ts.map +1 -0
  26. package/dist/key/_generate.js +124 -0
  27. package/dist/key/_generate.js.map +1 -0
  28. package/dist/key/_key-state.d.ts +53 -0
  29. package/dist/key/_key-state.d.ts.map +1 -0
  30. package/dist/key/_key-state.js +26 -0
  31. package/dist/key/_key-state.js.map +1 -0
  32. package/dist/key/_primality.d.ts +22 -0
  33. package/dist/key/_primality.d.ts.map +1 -0
  34. package/dist/key/_primality.js +156 -0
  35. package/dist/key/_primality.js.map +1 -0
  36. package/dist/key/_primitives.d.ts +27 -0
  37. package/dist/key/_primitives.d.ts.map +1 -0
  38. package/dist/key/_primitives.js +247 -0
  39. package/dist/key/_primitives.js.map +1 -0
  40. package/dist/key/_validate.d.ts +60 -0
  41. package/dist/key/_validate.d.ts.map +1 -0
  42. package/dist/key/_validate.js +328 -0
  43. package/dist/key/_validate.js.map +1 -0
  44. package/dist/key/rsa-private-key.d.ts +180 -0
  45. package/dist/key/rsa-private-key.d.ts.map +1 -0
  46. package/dist/key/rsa-private-key.js +479 -0
  47. package/dist/key/rsa-private-key.js.map +1 -0
  48. package/dist/key/rsa-public-key.d.ts +117 -0
  49. package/dist/key/rsa-public-key.d.ts.map +1 -0
  50. package/dist/key/rsa-public-key.js +196 -0
  51. package/dist/key/rsa-public-key.js.map +1 -0
  52. package/dist/rsa-error.d.ts +54 -0
  53. package/dist/rsa-error.d.ts.map +1 -0
  54. package/dist/rsa-error.js +14 -0
  55. package/dist/rsa-error.js.map +1 -0
  56. package/dist/scheme/_scheme-checks.d.ts +50 -0
  57. package/dist/scheme/_scheme-checks.d.ts.map +1 -0
  58. package/dist/scheme/_scheme-checks.js +162 -0
  59. package/dist/scheme/_scheme-checks.js.map +1 -0
  60. package/dist/scheme/_signature.d.ts +8 -0
  61. package/dist/scheme/_signature.d.ts.map +1 -0
  62. package/dist/scheme/_signature.js +13 -0
  63. package/dist/scheme/_signature.js.map +1 -0
  64. package/dist/scheme/rsaes-oaep.d.ts +75 -0
  65. package/dist/scheme/rsaes-oaep.d.ts.map +1 -0
  66. package/dist/scheme/rsaes-oaep.js +191 -0
  67. package/dist/scheme/rsaes-oaep.js.map +1 -0
  68. package/dist/scheme/rsassa-pkcs1-v1_5.d.ts +73 -0
  69. package/dist/scheme/rsassa-pkcs1-v1_5.d.ts.map +1 -0
  70. package/dist/scheme/rsassa-pkcs1-v1_5.js +133 -0
  71. package/dist/scheme/rsassa-pkcs1-v1_5.js.map +1 -0
  72. package/dist/scheme/rsassa-pss.d.ts +88 -0
  73. package/dist/scheme/rsassa-pss.d.ts.map +1 -0
  74. package/dist/scheme/rsassa-pss.js +175 -0
  75. package/dist/scheme/rsassa-pss.js.map +1 -0
  76. package/package.json +75 -3
@@ -0,0 +1,479 @@
1
+ import { runInTimeSlices, runToCompletion } from "@ac-kit/core";
2
+ import { constantTimeUnsignedCompare, INSPECT_CUSTOM, redactSecret, } from "@ac-kit/crypto-safe";
3
+ import { bitLengthOf, bytesOfUnsigned, leftPad, unsignedOf, } from "../_bytes.js";
4
+ import { derElement, derUnsignedInteger, enterSequence, nextHasTag, readDer, readOctetString, readSmallInteger, readUnsignedInteger, TAG_OCTET_STRING, TAG_SEQUENCE, wipeAll, } from "../encoding/_der.js";
5
+ import { decodeRsaKeyAlgorithm, encodeRsaKeyAlgorithm, } from "../encoding/rsa-algorithm.js";
6
+ import { RsaError } from "../rsa-error.js";
7
+ import { generatePrimeSteps } from "./_generate.js";
8
+ import { livePrivateStateOf, privateStateOf, publicStateOf, setPrivateState, } from "./_key-state.js";
9
+ import { buildPrivateState, deriveFromPrimes, wipePrimes, } from "./_validate.js";
10
+ import { checkPublicComponents, createPublicKey, publicExponentBytes, RSA_ENCRYPTION_ALGORITHM, RSA_MAXIMUM_MODULUS_BITS, readAlgorithmElement, RsaPublicKey, } from "./rsa-public-key.js";
11
+ const MINIMUM_GENERATED_BITS_ = 2048;
12
+ const DEFAULT_PUBLIC_EXPONENT_ = 65537n;
13
+ let newPrivateKey_;
14
+ /**
15
+ * An RSA private key (RFC 8017 §3.2), in CRT form (two or more primes) or in
16
+ * the first form (n, d) alone. Every operation on it — signing, decryption,
17
+ * import checks and generation — runs in constant time in the secret integers,
18
+ * with base, exponent and modulus blinding and a check of every result (see the
19
+ * package documentation).
20
+ *
21
+ * The key's integers live outside the instance: the key is frozen, has no data
22
+ * properties and shows `RsaPrivateKey { <redacted> }` to `util.inspect` and
23
+ * `JSON.stringify`. Disposing of it (`using`, or `[Symbol.dispose]()`)
24
+ * overwrites them; a disposed key throws `RsaError` `disposed` on use. Imports
25
+ * copy their input and never write to it.
26
+ */
27
+ export class RsaPrivateKey {
28
+ constructor(state) {
29
+ setPrivateState(this, state);
30
+ Object.freeze(this);
31
+ }
32
+ /**
33
+ * The key for its integers, after checking that they are consistent (see
34
+ * {@link RsaPrivateKeyImportOptions}): with CRT integers, that the primes
35
+ * multiply to n, every exponent and coefficient is right and, unless
36
+ * `checkPrimality` is false, each prime is probably prime; without, that d
37
+ * works as an inverse of e.
38
+ *
39
+ * @throws {RsaError} As {@link RsaPublicKey.fromComponents} for n, e and the
40
+ * algorithm; `invalidKey` when the private integers are out of range or
41
+ * inconsistent.
42
+ */
43
+ static fromComponents(components, options = {}) {
44
+ const { modulus, publicExponent } = checkPublicComponents(components);
45
+ const publicKey = createPublicKey(modulus, publicExponent, options.algorithm ?? RSA_ENCRYPTION_ALGORITHM);
46
+ return newPrivateKey_(buildPrivateState(publicKey, integersOf_(components.privateExponent, components.crt), options));
47
+ }
48
+ /**
49
+ * The key for its primes and e, deriving n, d (unless given) and the CRT
50
+ * integers (FIPS 186-5 A.1.1, RFC 8017 §3.2), in constant time.
51
+ *
52
+ * @throws {RsaError} `invalidKey` for fewer than two primes, a prime that is
53
+ * not odd and above 3 (0, 1 and empty ones included), a prime that fails
54
+ * the primality test (unless `checkPrimality` is false) or primes that do
55
+ * not make a key with e; as {@link RsaPublicKey.fromComponents} for n and
56
+ * e.
57
+ */
58
+ static fromPrimes(input, options = {}) {
59
+ if (input.primes.length < 2) {
60
+ throw new RsaError("invalidKey", "An RSA key needs two primes at least");
61
+ }
62
+ const exponent = publicExponentBytes(unsignedOf(input.publicExponent));
63
+ const derived = deriveFromPrimes(input.primes, exponent, input.privateExponent);
64
+ try {
65
+ const { modulus, publicExponent } = checkPublicComponents({
66
+ modulus: derived.modulus,
67
+ publicExponent: exponent,
68
+ });
69
+ const publicKey = createPublicKey(modulus, publicExponent, options.algorithm ?? RSA_ENCRYPTION_ALGORITHM);
70
+ return newPrivateKey_(buildPrivateState(publicKey, derived.integers, options));
71
+ }
72
+ finally {
73
+ wipeIntegers_(derived.integers);
74
+ }
75
+ }
76
+ /**
77
+ * The key of a DER `RSAPrivateKey` (RFC 8017 Appendix A.1.2), version 0 (two
78
+ * primes) or 1 (multi-prime, with `otherPrimeInfos`), read strictly.
79
+ *
80
+ * @throws {RsaError} `invalidEncoding` for anything but that DER structure;
81
+ * as {@link fromComponents} for its integers.
82
+ */
83
+ static fromPkcs1(der, options = {}) {
84
+ const components = readPkcs1PrivateKey_(der);
85
+ try {
86
+ return RsaPrivateKey.fromComponents(components, options);
87
+ }
88
+ finally {
89
+ wipeComponents_(components);
90
+ }
91
+ }
92
+ /**
93
+ * The key of a DER PKCS #8 `PrivateKeyInfo` (RFC 5208) or `OneAsymmetricKey`
94
+ * (RFC 5958, version 1 with its optional public key, which must match), whose
95
+ * algorithm is one {@link RsaPublicKey.fromSpki} takes; the key keeps that
96
+ * algorithm. Attributes are read past and dropped.
97
+ *
98
+ * @throws {RsaError} `invalidEncoding` for malformed DER or a public key that
99
+ * does not match; `unsupportedAlgorithm` for another algorithm; as
100
+ * {@link fromComponents} for the integers.
101
+ */
102
+ static fromPkcs8(der, options = {}) {
103
+ const parsed = readDer(der, "A PrivateKeyInfo", (reader) => {
104
+ const sequence = enterSequence(reader, "A PrivateKeyInfo");
105
+ const version = readSmallInteger(sequence, "The PrivateKeyInfo version");
106
+ if (version > 1) {
107
+ throw new RsaError("invalidEncoding", "A PrivateKeyInfo must be version 0 or 1");
108
+ }
109
+ const algorithm = readAlgorithmElement(sequence, "The key algorithm");
110
+ const privateKey = readOctetString(sequence, "The privateKey");
111
+ if (nextHasTag(sequence, ATTRIBUTES_TAG_)) {
112
+ sequence.skip();
113
+ }
114
+ let publicKey;
115
+ if (nextHasTag(sequence, PUBLIC_KEY_TAG_)) {
116
+ if (version !== 1) {
117
+ throw new RsaError("invalidEncoding", "Only a version 1 OneAsymmetricKey carries a public key");
118
+ }
119
+ publicKey = readImplicitBitString_(sequence);
120
+ }
121
+ sequence.finish();
122
+ return { algorithm, privateKey, publicKey };
123
+ });
124
+ const algorithm = decodeRsaKeyAlgorithm(parsed.algorithm);
125
+ const key = RsaPrivateKey.fromPkcs1(parsed.privateKey, {
126
+ ...options,
127
+ algorithm,
128
+ });
129
+ if (parsed.publicKey !== undefined) {
130
+ let matches = false;
131
+ try {
132
+ matches = RsaPublicKey.fromPkcs1(parsed.publicKey).equals(key.publicKey);
133
+ }
134
+ catch (error) {
135
+ key[Symbol.dispose]();
136
+ throw error;
137
+ }
138
+ if (!matches) {
139
+ key[Symbol.dispose]();
140
+ throw new RsaError("invalidEncoding", "The OneAsymmetricKey public key does not match its private key");
141
+ }
142
+ }
143
+ return key;
144
+ }
145
+ /**
146
+ * A new key by FIPS 186-5 A.1.3 (random probable primes, Miller–Rabin rounds
147
+ * of Table B.1), with d = e⁻¹ mod λ(n) and d > 2^(nlen/2) as A.1.1 requires,
148
+ * drawing from `crypto.getRandomValues`. Synchronous: a 2048-bit key takes
149
+ * about a second, a 4096-bit key several; see {@link generateAsync} to keep
150
+ * the event loop free.
151
+ *
152
+ * @throws {RsaError} `invalidParameters` for a size or exponent FIPS 186-5
153
+ * refuses; `keyGenerationFailed` when its iteration bounds run out.
154
+ */
155
+ static generate(options) {
156
+ return runToCompletion(generateSteps_(options));
157
+ }
158
+ /**
159
+ * {@link generate}, yielding to the event loop between candidates and
160
+ * honouring `signal`.
161
+ */
162
+ static generateAsync(options) {
163
+ return runInTimeSlices(generateSteps_(options), {
164
+ signal: options.signal,
165
+ sliceMs: options.sliceMs,
166
+ });
167
+ }
168
+ /** The public key (n, e), with the key's algorithm. */
169
+ get publicKey() {
170
+ return livePrivateStateOf(this).publicKey;
171
+ }
172
+ /** Bit length of n. */
173
+ get modulusBitLength() {
174
+ return this.publicKey.modulusBitLength;
175
+ }
176
+ /** Byte length k of n. */
177
+ get modulusByteLength() {
178
+ return this.publicKey.modulusByteLength;
179
+ }
180
+ /** The number of primes: 2 or more, or 0 for a key of the first form. */
181
+ get primeCount() {
182
+ return livePrivateStateOf(this).primes?.length ?? 0;
183
+ }
184
+ /** Whether the key has been disposed. */
185
+ get disposed() {
186
+ return privateStateOf(this).disposed;
187
+ }
188
+ /** Copies of the key's integers, minimal big-endian, for storage. */
189
+ exportComponents() {
190
+ const state = livePrivateStateOf(this);
191
+ const { modulus, publicExponent } = state.publicKey.exportComponents();
192
+ const privateExponent = minimalSecret_(state.privateExponent);
193
+ if (state.primes === undefined) {
194
+ return { modulus, publicExponent, privateExponent };
195
+ }
196
+ const [first, second, ...others] = state.primes;
197
+ return {
198
+ modulus,
199
+ publicExponent,
200
+ privateExponent,
201
+ crt: {
202
+ prime1: first.prime.slice(),
203
+ prime2: second.prime.slice(),
204
+ exponent1: minimalSecret_(first.exponent),
205
+ exponent2: minimalSecret_(second.exponent),
206
+ coefficient: minimalSecret_(first.coefficient),
207
+ otherPrimes: others.map((prime) => ({
208
+ prime: prime.prime.slice(),
209
+ exponent: minimalSecret_(prime.exponent),
210
+ coefficient: minimalSecret_(prime.coefficient),
211
+ })),
212
+ },
213
+ };
214
+ }
215
+ /**
216
+ * The DER `RSAPrivateKey` (RFC 8017 Appendix A.1.2): version 0 for two
217
+ * primes, 1 with `otherPrimeInfos` for more. The DER INTEGER lengths reveal
218
+ * each integer's byte length, as any encoding does.
219
+ *
220
+ * @throws {RsaError} `invalidKey` for a key of the first form, which
221
+ * `RSAPrivateKey` cannot carry.
222
+ */
223
+ toPkcs1() {
224
+ const state = livePrivateStateOf(this);
225
+ const publicState = publicStateOf(state.publicKey);
226
+ if (state.primes === undefined) {
227
+ throw new RsaError("invalidKey", "An RSAPrivateKey needs the primes, which this key does not have");
228
+ }
229
+ const [first, second, ...others] = state.primes;
230
+ const parts = [];
231
+ try {
232
+ parts.push(derUnsignedInteger(Uint8Array.of(others.length === 0 ? 0 : 1)), derUnsignedInteger(publicState.modulus), derUnsignedInteger(publicState.publicExponent), derUnsignedInteger(state.privateExponent), derUnsignedInteger(first.prime), derUnsignedInteger(second.prime), derUnsignedInteger(first.exponent), derUnsignedInteger(second.exponent), derUnsignedInteger(first.coefficient));
233
+ if (others.length > 0) {
234
+ const infos = [];
235
+ try {
236
+ for (const prime of others) {
237
+ const fields = [
238
+ derUnsignedInteger(prime.prime),
239
+ derUnsignedInteger(prime.exponent),
240
+ derUnsignedInteger(prime.coefficient),
241
+ ];
242
+ infos.push(derElement(TAG_SEQUENCE, fields));
243
+ wipeAll(fields);
244
+ }
245
+ parts.push(derElement(TAG_SEQUENCE, infos));
246
+ }
247
+ finally {
248
+ wipeAll(infos);
249
+ }
250
+ }
251
+ return derElement(TAG_SEQUENCE, parts);
252
+ }
253
+ finally {
254
+ wipeAll(parts);
255
+ }
256
+ }
257
+ /**
258
+ * The DER PKCS #8 `PrivateKeyInfo` (RFC 5208, version 0) of the key under its
259
+ * public key's algorithm.
260
+ *
261
+ * @throws {RsaError} As {@link toPkcs1}; `invalidParameters` as
262
+ * `RsaPublicKey.toSpki` for the algorithm.
263
+ */
264
+ toPkcs8() {
265
+ const algorithm = encodeRsaKeyAlgorithm(this.publicKey.algorithm);
266
+ const pkcs1 = this.toPkcs1();
267
+ const privateKey = derElement(TAG_OCTET_STRING, [pkcs1]);
268
+ try {
269
+ return derElement(TAG_SEQUENCE, [
270
+ derUnsignedInteger(Uint8Array.of(0)),
271
+ algorithm,
272
+ privateKey,
273
+ ]);
274
+ }
275
+ finally {
276
+ pkcs1.fill(0);
277
+ privateKey.fill(0);
278
+ }
279
+ }
280
+ /** Overwrites the key's private integers. Idempotent. */
281
+ [Symbol.dispose]() {
282
+ const state = privateStateOf(this);
283
+ if (state.disposed) {
284
+ return;
285
+ }
286
+ state.privateExponent.fill(0);
287
+ state.multipleOfLambda.fill(0);
288
+ if (state.primes !== undefined) {
289
+ wipePrimes(state.primes);
290
+ }
291
+ state.disposed = true;
292
+ }
293
+ /** `RsaPrivateKey { <redacted> }`, for `util.inspect` and `console.log`. */
294
+ [INSPECT_CUSTOM]() {
295
+ return redactSecret("RsaPrivateKey");
296
+ }
297
+ /** `RsaPrivateKey { <redacted> }`: `JSON.stringify` never sees the key. */
298
+ toJSON() {
299
+ return redactSecret("RsaPrivateKey");
300
+ }
301
+ static {
302
+ newPrivateKey_ = (state) => new RsaPrivateKey(state);
303
+ }
304
+ }
305
+ /** `[0] IMPLICIT Attributes`, constructed. */
306
+ const ATTRIBUTES_TAG_ = 0xa0;
307
+ /** `[1] IMPLICIT BIT STRING`, primitive. */
308
+ const PUBLIC_KEY_TAG_ = 0x81;
309
+ function readImplicitBitString_(reader) {
310
+ const contents = reader.readContents();
311
+ if (contents.length === 0 || contents[0] !== 0) {
312
+ throw new RsaError("invalidEncoding", "The OneAsymmetricKey public key must be a BIT STRING of whole octets");
313
+ }
314
+ return contents.subarray(1);
315
+ }
316
+ function* generateSteps_(options) {
317
+ const { modulusBits } = options;
318
+ const exponentInput = options.publicExponent ?? DEFAULT_PUBLIC_EXPONENT_;
319
+ if (typeof exponentInput !== "bigint" && !Number.isInteger(exponentInput)) {
320
+ throw new RsaError("invalidParameters", "An RSA public exponent must be an integer");
321
+ }
322
+ const exponentValue = BigInt(exponentInput);
323
+ if (!Number.isInteger(modulusBits) ||
324
+ modulusBits < MINIMUM_GENERATED_BITS_ ||
325
+ modulusBits > RSA_MAXIMUM_MODULUS_BITS ||
326
+ modulusBits % 2 !== 0) {
327
+ throw new RsaError("invalidParameters", `An RSA key is generated with an even modulus size from ${MINIMUM_GENERATED_BITS_} to ${RSA_MAXIMUM_MODULUS_BITS} bits`);
328
+ }
329
+ if (exponentValue <= 1n << 16n ||
330
+ exponentValue >= 1n << 256n ||
331
+ (exponentValue & 1n) === 0n) {
332
+ throw new RsaError("invalidParameters", "FIPS 186-5 needs an odd public exponent e with 2^16 < e < 2^256");
333
+ }
334
+ const publicExponent = publicExponentBytes(exponentValue);
335
+ const half = modulusBits / 2;
336
+ const algorithm = options.algorithm ?? RSA_ENCRYPTION_ALGORITHM;
337
+ for (;;) {
338
+ const p = yield* generatePrimeSteps(half, publicExponent, undefined);
339
+ let q;
340
+ try {
341
+ q = yield* generatePrimeSteps(half, publicExponent, p);
342
+ const derived = deriveFromPrimes([p, q], publicExponent, undefined);
343
+ try {
344
+ if (!privateExponentLargeEnough_(derived.integers.privateExponent, half)) {
345
+ // A.1.1 3(a): d ≤ 2^(nlen/2) is refused; new primes are drawn.
346
+ continue;
347
+ }
348
+ const publicKey = createPublicKey(derived.modulus, publicExponent, algorithm);
349
+ return newPrivateKey_(
350
+ // The primes passed Table B.1's rounds already.
351
+ buildPrivateState(publicKey, derived.integers, {
352
+ checkPrimality: false,
353
+ }));
354
+ }
355
+ finally {
356
+ wipeIntegers_(derived.integers);
357
+ }
358
+ }
359
+ finally {
360
+ p.fill(0);
361
+ q?.fill(0);
362
+ }
363
+ }
364
+ }
365
+ /** D > 2^half, compared in constant time. */
366
+ function privateExponentLargeEnough_(privateExponent, half) {
367
+ const bound = bytesOfUnsigned(1n << BigInt(half), Math.ceil((half + 1) / 8));
368
+ return constantTimeUnsignedCompare(privateExponent, bound) > 0;
369
+ }
370
+ function integersOf_(privateExponent, crt) {
371
+ if (crt === undefined) {
372
+ return { privateExponent, primes: undefined };
373
+ }
374
+ const primes = [
375
+ {
376
+ prime: crt.prime1,
377
+ exponent: crt.exponent1,
378
+ coefficient: crt.coefficient,
379
+ },
380
+ {
381
+ prime: crt.prime2,
382
+ exponent: crt.exponent2,
383
+ coefficient: new Uint8Array(0),
384
+ },
385
+ ];
386
+ for (const other of crt.otherPrimes ?? []) {
387
+ primes.push({
388
+ prime: other.prime,
389
+ exponent: other.exponent,
390
+ coefficient: other.coefficient,
391
+ });
392
+ }
393
+ return { privateExponent, primes };
394
+ }
395
+ function wipeIntegers_(integers) {
396
+ integers.privateExponent.fill(0);
397
+ for (const prime of integers.primes ?? []) {
398
+ prime.prime.fill(0);
399
+ prime.exponent.fill(0);
400
+ prime.coefficient.fill(0);
401
+ }
402
+ }
403
+ function wipeComponents_(components) {
404
+ components.privateExponent.fill(0);
405
+ const { crt } = components;
406
+ if (crt === undefined) {
407
+ return;
408
+ }
409
+ wipeAll([
410
+ crt.prime1,
411
+ crt.prime2,
412
+ crt.exponent1,
413
+ crt.exponent2,
414
+ crt.coefficient,
415
+ ]);
416
+ for (const other of crt.otherPrimes ?? []) {
417
+ wipeAll([other.prime, other.exponent, other.coefficient]);
418
+ }
419
+ }
420
+ /**
421
+ * A copy of a secret integer without its leading zero bytes: the scan reveals
422
+ * its byte length, which its encoding reveals anyway.
423
+ */
424
+ function minimalSecret_(bytes) {
425
+ const bits = bitLengthOf(bytes);
426
+ return leftPad(bytes, Math.max(1, Math.ceil(bits / 8)));
427
+ }
428
+ /** The integers of a DER `RSAPrivateKey`, copied (the caller wipes them). */
429
+ function readPkcs1PrivateKey_(der) {
430
+ const read = [];
431
+ const integer = (reader, what) => {
432
+ const value = readUnsignedInteger(reader, what);
433
+ read.push(value);
434
+ return value;
435
+ };
436
+ try {
437
+ return readDer(der, "An RSAPrivateKey", (reader) => {
438
+ const sequence = enterSequence(reader, "An RSAPrivateKey");
439
+ const version = readSmallInteger(sequence, "The RSAPrivateKey version");
440
+ if (version > 1) {
441
+ throw new RsaError("invalidEncoding", "An RSAPrivateKey must be version 0 (two-prime) or 1 (multi)");
442
+ }
443
+ const modulus = integer(sequence, "The modulus");
444
+ const publicExponent = integer(sequence, "The public exponent");
445
+ const privateExponent = integer(sequence, "The private exponent");
446
+ const prime1 = integer(sequence, "prime1");
447
+ const prime2 = integer(sequence, "prime2");
448
+ const exponent1 = integer(sequence, "exponent1");
449
+ const exponent2 = integer(sequence, "exponent2");
450
+ const coefficient = integer(sequence, "The coefficient");
451
+ const otherPrimes = [];
452
+ if (version === 1) {
453
+ const infos = enterSequence(sequence, "The otherPrimeInfos");
454
+ do {
455
+ const info = enterSequence(infos, "An OtherPrimeInfo");
456
+ otherPrimes.push({
457
+ prime: integer(info, "An other prime"),
458
+ exponent: integer(info, "An other prime's exponent"),
459
+ coefficient: integer(info, "An other prime's coefficient"),
460
+ });
461
+ info.finish();
462
+ } while (!infos.atEnd);
463
+ infos.finish();
464
+ }
465
+ sequence.finish();
466
+ return {
467
+ modulus,
468
+ publicExponent,
469
+ privateExponent,
470
+ crt: { prime1, prime2, exponent1, exponent2, coefficient, otherPrimes },
471
+ };
472
+ });
473
+ }
474
+ catch (error) {
475
+ wipeAll(read);
476
+ throw error;
477
+ }
478
+ }
479
+ //# sourceMappingURL=rsa-private-key.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rsa-private-key.js","sourceRoot":"","sources":["../../src/key/rsa-private-key.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EACN,2BAA2B,EAC3B,cAAc,EACd,YAAY,GACZ,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EACN,WAAW,EACX,eAAe,EACf,OAAO,EACP,UAAU,GACV,MAAM,cAAc,CAAC;AACtB,OAAO,EACN,UAAU,EACV,kBAAkB,EAClB,aAAa,EACb,UAAU,EACV,OAAO,EACP,eAAe,EACf,gBAAgB,EAChB,mBAAmB,EACnB,gBAAgB,EAChB,YAAY,EACZ,OAAO,GACP,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACN,qBAAqB,EACrB,qBAAqB,GACrB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EACN,kBAAkB,EAElB,cAAc,EACd,aAAa,EACb,eAAe,GACf,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,iBAAiB,EAEjB,gBAAgB,EAGhB,UAAU,GACV,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,qBAAqB,EACrB,eAAe,EACf,mBAAmB,EACnB,wBAAwB,EACxB,wBAAwB,EACxB,oBAAoB,EAEpB,YAAY,GAEZ,MAAM,qBAAqB,CAAC;AA6E7B,MAAM,uBAAuB,GAAG,IAAI,CAAC;AACrC,MAAM,wBAAwB,GAAG,MA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W,CAAC,IAAI,CAAC;wBAChB,KAAK,EAAE,OAAO,CAAC,IAAI,EAAE,gBAAgB,CAAC;wBACtC,QAAQ,EAAE,OAAO,CAAC,IAAI,EAAE,2BAA2B,CAAC;wBACpD,WAAW,EAAE,OAAO,CAAC,IAAI,EAAE,8BAA8B,CAAC;qBAC1D,CAAC,CAAC;oBACH,IAAI,CAAC,MAAM,EAAE,CAAC;gBACf,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,EAAE;gBAEvB,KAAK,CAAC,MAAM,EAAE,CAAC;YAChB,CAAC;YAED,QAAQ,CAAC,MAAM,EAAE,CAAC;YAElB,OAAO;gBACN,OAAO;gBACP,cAAc;gBACd,eAAe;gBACf,GAAG,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,WAAW,EAAE,WAAW,EAAE;aACvE,CAAC;QACH,CAAC,CAAC,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,OAAO,CAAC,IAAI,CAAC,CAAC;QAEd,MAAM,KAAK,CAAC;IACb,CAAC;AACF,CAAC"}
@@ -0,0 +1,117 @@
1
+ import { INSPECT_CUSTOM } from "@ac-kit/crypto-safe";
2
+ import type { BerReader } from "@ac-kit/format-asn1";
3
+ import { type RsaKeyAlgorithm } from "../encoding/rsa-algorithm.js";
4
+ export { RSA_ENCRYPTION_ALGORITHM } from "../encoding/rsa-algorithm.js";
5
+ /** The smallest modulus this package accepts, in bits. */
6
+ export declare const RSA_MINIMUM_MODULUS_BITS = 1024;
7
+ /** The largest modulus this package accepts, in bits. */
8
+ export declare const RSA_MAXIMUM_MODULUS_BITS = 16384;
9
+ /** The integers of an RSA public key (RFC 8017 §3.1), big-endian. */
10
+ export interface RsaPublicKeyComponents {
11
+ /** N, big-endian; leading zero bytes are ignored. */
12
+ readonly modulus: Uint8Array;
13
+ /** E, big-endian; leading zero bytes are ignored. */
14
+ readonly publicExponent: Uint8Array;
15
+ }
16
+ /** Options of the key constructors that take no `AlgorithmIdentifier`. */
17
+ export interface RsaKeyOptions {
18
+ /**
19
+ * What the key may be used for, written by `toSpki` and `toPkcs8` and
20
+ * enforced by every scheme; the key keeps a validated, frozen copy. Default
21
+ * `{ name: "rsaEncryption" }`: any scheme.
22
+ */
23
+ readonly algorithm?: RsaKeyAlgorithm;
24
+ }
25
+ /**
26
+ * Checks and copies public components, returning their minimal encodings.
27
+ *
28
+ * @throws {RsaError} `unsupportedKeySize` for a modulus outside [1024, 16384]
29
+ * bits; `invalidKey` for an even modulus or an exponent that is even, below 3
30
+ * or not below n.
31
+ */
32
+ export declare function checkPublicComponents(components: RsaPublicKeyComponents): {
33
+ readonly modulus: Uint8Array<ArrayBuffer>;
34
+ readonly publicExponent: Uint8Array<ArrayBuffer>;
35
+ };
36
+ /**
37
+ * An RSA public key (n, e) and the algorithm its `AlgorithmIdentifier` allows.
38
+ * Every input is public, so its operations may run in variable time; they run
39
+ * on the same constant-time arithmetic as the private key anyway, since
40
+ * encryption raises a secret message.
41
+ *
42
+ * Keys are frozen and immutable; `util.inspect` shows the modulus size, the
43
+ * exponent and the algorithm.
44
+ */
45
+ export declare class RsaPublicKey {
46
+ private constructor();
47
+ /**
48
+ * The key for the integers n and e, copied.
49
+ *
50
+ * @throws {RsaError} `unsupportedKeySize` for a modulus outside [1024, 16384]
51
+ * bits; `invalidKey` for an even modulus or an exponent that is even, below
52
+ * 3 or not below n; `invalidParameters` for an `algorithm` with an unknown
53
+ * name, hash or mask, a PSS salt length that is not an integer in [0,
54
+ * 65535], or an OAEP mask other than MGF1 or label that is not bytes.
55
+ */
56
+ static fromComponents(components: RsaPublicKeyComponents, options?: RsaKeyOptions): RsaPublicKey;
57
+ /**
58
+ * The key of a DER `RSAPublicKey` (RFC 8017 Appendix A.1.1), read strictly.
59
+ *
60
+ * @throws {RsaError} `invalidEncoding` for anything but that DER structure;
61
+ * as {@link fromComponents} for its integers.
62
+ */
63
+ static fromPkcs1(der: Uint8Array, options?: RsaKeyOptions): RsaPublicKey;
64
+ /**
65
+ * The key of a DER `SubjectPublicKeyInfo` (RFC 5280 §4.1.2.7) whose algorithm
66
+ * is `rsaEncryption` (RFC 8017 Appendix A.1), `id-RSASSA-PSS` with or without
67
+ * parameters (RFC 4055 §1.2), `id-RSASSA-PSS-SHAKE128` or `-SHAKE256` (RFC
68
+ * 8702 §4) or `id-RSAES-OAEP` (RFC 4055 §4.1), read strictly; the key keeps
69
+ * that algorithm as its {@link algorithm}.
70
+ *
71
+ * @throws {RsaError} `invalidEncoding` for malformed DER;
72
+ * `unsupportedAlgorithm` for another algorithm; as {@link fromComponents}
73
+ * for the integers.
74
+ */
75
+ static fromSpki(der: Uint8Array): RsaPublicKey;
76
+ /** Bit length of n. */
77
+ get modulusBitLength(): number;
78
+ /** Byte length k of n, and of every signature and ciphertext. */
79
+ get modulusByteLength(): number;
80
+ /** What the key may be used for. */
81
+ get algorithm(): RsaKeyAlgorithm;
82
+ /** Copies of n and e, minimal big-endian. */
83
+ exportComponents(): {
84
+ readonly modulus: Uint8Array<ArrayBuffer>;
85
+ readonly publicExponent: Uint8Array<ArrayBuffer>;
86
+ };
87
+ /** The DER `RSAPublicKey` (RFC 8017 Appendix A.1.1). */
88
+ toPkcs1(): Uint8Array<ArrayBuffer>;
89
+ /**
90
+ * The DER `SubjectPublicKeyInfo` of the key under its {@link algorithm}.
91
+ *
92
+ * @throws {RsaError} `invalidParameters` when the algorithm's parameters have
93
+ * no `AlgorithmIdentifier` (see `encodeRsaKeyAlgorithm`).
94
+ */
95
+ toSpki(): Uint8Array<ArrayBuffer>;
96
+ /**
97
+ * The same n and e under `algorithm`, for instance to restrict a key to
98
+ * RSASSA-PSS before exporting it.
99
+ *
100
+ * @throws {RsaError} `invalidParameters` as {@link fromComponents} for the
101
+ * algorithm.
102
+ */
103
+ withAlgorithm(algorithm: RsaKeyAlgorithm): RsaPublicKey;
104
+ /** Whether `other` has the same n and e (whatever their algorithms). */
105
+ equals(other: RsaPublicKey): boolean;
106
+ /** `RsaPublicKey { 2048 bits, e = 65537, rsaEncryption }`, for `util.inspect`. */
107
+ [INSPECT_CUSTOM](): string;
108
+ }
109
+ /** Builds a key from components already checked by {@link checkPublicComponents}. */
110
+ export declare function createPublicKey(modulus: Uint8Array, publicExponent: Uint8Array, algorithm: RsaKeyAlgorithm): RsaPublicKey;
111
+ /** The minimal encoding of a public exponent given as a number or bigint. */
112
+ export declare function publicExponentBytes(value: bigint | number): Uint8Array<ArrayBuffer>;
113
+ /** The next element, an `AlgorithmIdentifier` SEQUENCE, as its whole TLV. */
114
+ export declare function readAlgorithmElement(reader: BerReader, what: string): Uint8Array;
115
+ /** The integers of a DER `RSAPublicKey`, read strictly. */
116
+ export declare function readPkcs1PublicKey(der: Uint8Array): RsaPublicKeyComponents;
117
+ //# sourceMappingURL=rsa-public-key.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rsa-public-key.d.ts","sourceRoot":"","sources":["../../src/key/rsa-public-key.ts"],"names":[],"mappings":"AACA,OAAO,EAAuB,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAC1E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAkBrD,OAAO,EAIN,KAAK,eAAe,EACpB,MAAM,8BAA8B,CAAC;AAKtC,OAAO,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAExE,0DAA0D;AAC1D,eAAO,MAAM,wBAAwB,OAAO,CAAC;AAC7C,yDAAyD;AACzD,eAAO,MAAM,wBAAwB,QAAS,CAAC;AAE/C,qEAAqE;AACrE,MAAM,WAAW,sBAAsB;IACtC,qDAAqD;IACrD,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC;IAC7B,qDAAqD;IACrD,QAAQ,CAAC,cAAc,EAAE,UAAU,CAAC;CACpC;AAED,0EAA0E;AAC1E,MAAM,WAAW,aAAa;IAC7B;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,eAAe,CAAC;CACrC;AAQ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