@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.
- package/CHANGELOG.md +79 -0
- package/LICENSE +28 -0
- package/README.md +113 -2
- package/dist/_bytes.d.ts +18 -0
- package/dist/_bytes.d.ts.map +1 -0
- package/dist/_bytes.js +42 -0
- package/dist/_bytes.js.map +1 -0
- package/dist/encoding/_der.d.ts +51 -0
- package/dist/encoding/_der.d.ts.map +1 -0
- package/dist/encoding/_der.js +173 -0
- package/dist/encoding/_der.js.map +1 -0
- package/dist/encoding/rsa-algorithm.d.ts +132 -0
- package/dist/encoding/rsa-algorithm.d.ts.map +1 -0
- package/dist/encoding/rsa-algorithm.js +475 -0
- package/dist/encoding/rsa-algorithm.js.map +1 -0
- package/dist/hash/rsa-hash.d.ts +68 -0
- package/dist/hash/rsa-hash.d.ts.map +1 -0
- package/dist/hash/rsa-hash.js +86 -0
- package/dist/hash/rsa-hash.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/key/_generate.d.ts +39 -0
- package/dist/key/_generate.d.ts.map +1 -0
- package/dist/key/_generate.js +124 -0
- package/dist/key/_generate.js.map +1 -0
- package/dist/key/_key-state.d.ts +53 -0
- package/dist/key/_key-state.d.ts.map +1 -0
- package/dist/key/_key-state.js +26 -0
- package/dist/key/_key-state.js.map +1 -0
- package/dist/key/_primality.d.ts +22 -0
- package/dist/key/_primality.d.ts.map +1 -0
- package/dist/key/_primality.js +156 -0
- package/dist/key/_primality.js.map +1 -0
- package/dist/key/_primitives.d.ts +27 -0
- package/dist/key/_primitives.d.ts.map +1 -0
- package/dist/key/_primitives.js +247 -0
- package/dist/key/_primitives.js.map +1 -0
- package/dist/key/_validate.d.ts +60 -0
- package/dist/key/_validate.d.ts.map +1 -0
- package/dist/key/_validate.js +328 -0
- package/dist/key/_validate.js.map +1 -0
- package/dist/key/rsa-private-key.d.ts +180 -0
- package/dist/key/rsa-private-key.d.ts.map +1 -0
- package/dist/key/rsa-private-key.js +479 -0
- package/dist/key/rsa-private-key.js.map +1 -0
- package/dist/key/rsa-public-key.d.ts +117 -0
- package/dist/key/rsa-public-key.d.ts.map +1 -0
- package/dist/key/rsa-public-key.js +196 -0
- package/dist/key/rsa-public-key.js.map +1 -0
- package/dist/rsa-error.d.ts +54 -0
- package/dist/rsa-error.d.ts.map +1 -0
- package/dist/rsa-error.js +14 -0
- package/dist/rsa-error.js.map +1 -0
- package/dist/scheme/_scheme-checks.d.ts +50 -0
- package/dist/scheme/_scheme-checks.d.ts.map +1 -0
- package/dist/scheme/_scheme-checks.js +162 -0
- package/dist/scheme/_scheme-checks.js.map +1 -0
- package/dist/scheme/_signature.d.ts +8 -0
- package/dist/scheme/_signature.d.ts.map +1 -0
- package/dist/scheme/_signature.js +13 -0
- package/dist/scheme/_signature.js.map +1 -0
- package/dist/scheme/rsaes-oaep.d.ts +75 -0
- package/dist/scheme/rsaes-oaep.d.ts.map +1 -0
- package/dist/scheme/rsaes-oaep.js +191 -0
- package/dist/scheme/rsaes-oaep.js.map +1 -0
- package/dist/scheme/rsassa-pkcs1-v1_5.d.ts +73 -0
- package/dist/scheme/rsassa-pkcs1-v1_5.d.ts.map +1 -0
- package/dist/scheme/rsassa-pkcs1-v1_5.js +133 -0
- package/dist/scheme/rsassa-pkcs1-v1_5.js.map +1 -0
- package/dist/scheme/rsassa-pss.d.ts +88 -0
- package/dist/scheme/rsassa-pss.d.ts.map +1 -0
- package/dist/scheme/rsassa-pss.js +175 -0
- package/dist/scheme/rsassa-pss.js.map +1 -0
- package/package.json +75 -3
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { getRandomBytes } from "@ac-kit/crypto-random";
|
|
2
|
+
import { ConstantTimeModulus, constantTimeUnsignedCompare, constantTimeUnsignedShiftRight, constantTimeUnsignedTrailingZeros, } from "@ac-kit/crypto-safe";
|
|
3
|
+
/**
|
|
4
|
+
* Probable-prime testing for secret candidates (FIPS 186-5 Appendix B.3): trial
|
|
5
|
+
* division by the odd primes below 2¹⁴ (B.3's optional step, its limit between
|
|
6
|
+
* 10³ and 10⁵), then Miller–Rabin rounds with random bases (B.3.1), all
|
|
7
|
+
* constant time in the candidate. A candidate that is accepted always runs
|
|
8
|
+
* every step to its end with the same work, so its timing carries nothing but
|
|
9
|
+
* its length; one that is refused may stop at the first failing step, which
|
|
10
|
+
* reveals only that a discarded value was composite. Key import, which must not
|
|
11
|
+
* reveal why a key was refused, runs both tests exhaustively.
|
|
12
|
+
*/
|
|
13
|
+
const TRIAL_DIVISION_LIMIT = 1 << 14;
|
|
14
|
+
/**
|
|
15
|
+
* The Miller–Rabin chain (B.3.1 step 4.5) runs a − 1 squarings, a being the
|
|
16
|
+
* 2-adic valuation of w − 1. It runs at least this many, masking the ones past
|
|
17
|
+
* a − 1, so that its length reveals a only for the 2⁻⁶⁴ of candidates whose a
|
|
18
|
+
* exceeds it.
|
|
19
|
+
*/
|
|
20
|
+
const MINIMUM_CHAIN = 64;
|
|
21
|
+
const SMALL_PRIMES_ = sieveOddPrimes_(TRIAL_DIVISION_LIMIT);
|
|
22
|
+
function sieveOddPrimes_(limit) {
|
|
23
|
+
const composite = new Uint8Array(limit);
|
|
24
|
+
const primes = [];
|
|
25
|
+
for (let candidate = 3; candidate < limit; candidate += 2) {
|
|
26
|
+
if (composite[candidate] === 0) {
|
|
27
|
+
primes.push(candidate);
|
|
28
|
+
for (let multiple = candidate * candidate; multiple < limit; multiple += 2 * candidate) {
|
|
29
|
+
composite[multiple] = 1;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return Float64Array.from(primes);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 1 when no odd prime below 2¹⁴ and below the odd `candidate` divides it, else
|
|
37
|
+
* 0: a small prime is not refused for dividing itself. Each residue is a Horner
|
|
38
|
+
* sum over the candidate's bytes reduced with a floating reciprocal and an
|
|
39
|
+
* arithmetic correction, so no division instruction sees the candidate. Stops
|
|
40
|
+
* at the first divisor found (that candidate is discarded) unless `exhaustive`,
|
|
41
|
+
* which runs every division whatever the candidate, for checks that must not
|
|
42
|
+
* reveal why a value was refused.
|
|
43
|
+
*/
|
|
44
|
+
export function passesTrialDivision(candidate, exhaustive = false) {
|
|
45
|
+
// Three bytes or more exceed every divisor; a shorter candidate is compared.
|
|
46
|
+
const small = candidate.length < 3 ? smallValueOf_(candidate) : TRIAL_DIVISION_LIMIT;
|
|
47
|
+
let passes = 1;
|
|
48
|
+
for (const prime of SMALL_PRIMES_) {
|
|
49
|
+
const reciprocal = 1 / prime;
|
|
50
|
+
let residue = 0;
|
|
51
|
+
for (const byte of candidate) {
|
|
52
|
+
// Below 2¹⁴ · 2⁸ + 2⁸: exact, and the quotient estimate is off by one at most.
|
|
53
|
+
const value = residue * 256 + byte;
|
|
54
|
+
let reduced = value - Math.floor(value * reciprocal) * prime;
|
|
55
|
+
reduced += prime & (reduced >> 31);
|
|
56
|
+
reduced -= prime & ~((reduced - prime) >> 31);
|
|
57
|
+
residue = reduced;
|
|
58
|
+
}
|
|
59
|
+
// A zero residue makes the candidate composite only above the divisor.
|
|
60
|
+
passes &= 1 - (((residue - 1) >>> 31) & ((prime - small) >>> 31));
|
|
61
|
+
if (!exhaustive && passes === 0) {
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return passes;
|
|
66
|
+
}
|
|
67
|
+
/** The value of a candidate of at most two bytes, with no branch on them. */
|
|
68
|
+
function smallValueOf_(candidate) {
|
|
69
|
+
let value = 0;
|
|
70
|
+
for (const byte of candidate) {
|
|
71
|
+
value = value * 256 + byte;
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* 1 when the odd `candidate` (w > 3) passes `rounds` Miller–Rabin rounds with
|
|
77
|
+
* bases drawn uniformly from [2, w − 2] (FIPS 186-5 B.3.1), else 0. Constant
|
|
78
|
+
* time in w: the decomposition w − 1 = 2^a·m is computed with a constant-time
|
|
79
|
+
* count and shift, z = b^m by the fixed-window exponentiation of
|
|
80
|
+
* `ConstantTimeModulus` over `w.length` bytes, and the chain of squarings masks
|
|
81
|
+
* its steps. `candidate` is minimal (its first byte non-zero); the caller
|
|
82
|
+
* ensures w is odd and above 3, for which a base exists. Stops at the first
|
|
83
|
+
* failing round unless `exhaustive`, which runs every round whatever w.
|
|
84
|
+
*/
|
|
85
|
+
export function passesMillerRabin(candidate, rounds, exhaustive = false) {
|
|
86
|
+
const length = candidate.length;
|
|
87
|
+
const ring = new ConstantTimeModulus(candidate);
|
|
88
|
+
const minusOne = candidate.slice();
|
|
89
|
+
const exponent = new Uint8Array(length);
|
|
90
|
+
const base = new Uint8Array(length);
|
|
91
|
+
const z = ring.create();
|
|
92
|
+
const minusOneElement = ring.create();
|
|
93
|
+
const oneElement = ring.create();
|
|
94
|
+
const bitLength = 8 * length - Math.clz32(candidate[0]) + 24;
|
|
95
|
+
try {
|
|
96
|
+
minusOne[length - 1] = minusOne[length - 1] & 0xfe;
|
|
97
|
+
const twoAdicity = constantTimeUnsignedTrailingZeros(minusOne);
|
|
98
|
+
constantTimeUnsignedShiftRight(exponent, minusOne, twoAdicity);
|
|
99
|
+
ring.fromBytesBe(minusOneElement, minusOne);
|
|
100
|
+
ring.fromBytesBe(oneElement, Uint8Array.of(1));
|
|
101
|
+
const chain = Math.max(twoAdicity - 1, MINIMUM_CHAIN);
|
|
102
|
+
let passes = 1;
|
|
103
|
+
for (let round = 0; round < rounds; round++) {
|
|
104
|
+
drawBase_(base, candidate, minusOne, bitLength);
|
|
105
|
+
ring.fromBytesBe(z, base);
|
|
106
|
+
ring.power(z, z, exponent);
|
|
107
|
+
let probablyPrime = ring.isEqual(z, oneElement) | ring.isEqual(z, minusOneElement);
|
|
108
|
+
for (let step = 1; step <= chain; step++) {
|
|
109
|
+
// 1 while step < a and no earlier square hit w − 1.
|
|
110
|
+
const active = ((step - twoAdicity) >>> 31) & (1 - probablyPrime);
|
|
111
|
+
ring.multiply(z, z, z);
|
|
112
|
+
probablyPrime |= active & ring.isEqual(z, minusOneElement);
|
|
113
|
+
}
|
|
114
|
+
passes &= probablyPrime;
|
|
115
|
+
if (!exhaustive && passes === 0) {
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return passes;
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
minusOne.fill(0);
|
|
123
|
+
exponent.fill(0);
|
|
124
|
+
base.fill(0);
|
|
125
|
+
z.fill(0);
|
|
126
|
+
minusOneElement.fill(0);
|
|
127
|
+
oneElement.fill(0);
|
|
128
|
+
ring[Symbol.dispose]();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* A base b with 1 < b < w − 1 from a string of len(w) random bits (B.3.1 steps
|
|
133
|
+
* 4.1 and 4.2). The top byte of `candidate` is secret, but its bit length is
|
|
134
|
+
* fixed by the caller (nlen/2), so masking with it reveals nothing new.
|
|
135
|
+
*/
|
|
136
|
+
function drawBase_(out, candidate, minusOne, bitLength) {
|
|
137
|
+
const excess = 8 * candidate.length - bitLength;
|
|
138
|
+
const one = new Uint8Array(candidate.length);
|
|
139
|
+
one[candidate.length - 1] = 1;
|
|
140
|
+
for (;;) {
|
|
141
|
+
const random = getRandomBytes(candidate.length);
|
|
142
|
+
try {
|
|
143
|
+
random[0] = random[0] & (0xff >>> excess);
|
|
144
|
+
// Rejection reveals only that a discarded base was out of range.
|
|
145
|
+
if (constantTimeUnsignedCompare(random, one) > 0 &&
|
|
146
|
+
constantTimeUnsignedCompare(random, minusOne) < 0) {
|
|
147
|
+
out.set(random);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
random.fill(0);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=_primality.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_primality.js","sourceRoot":"","sources":["../../src/key/_primality.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EACN,mBAAmB,EACnB,2BAA2B,EAC3B,8BAA8B,EAC9B,iCAAiC,GACjC,MAAM,qBAAqB,CAAC;AAE7B;;;;;;;;;GASG;AAEH,MAAM,oBAAoB,GAAG,CAAC,IAAI,EAAE,CAAC;AAErC;;;;;GAKG;AACH,MAAM,aAAa,GAAG,EAAE,CAAC;AAEzB,MAAM,aAAa,GAAG,eAAe,CAAC,oBAAoB,CAAC,CAAC;AAE5D,SAAS,eAAe,CAAC,KAAa;IACrC,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IACxC,MAAM,MAAM,GAAa,EAAE,CAAC;IAE5B,KAAK,IAAI,SAAS,GAAG,CAAC,EAAE,SAAS,GAAG,KAAK,EAAE,SAAS,IAAI,CAAC,EAAE,CAAC;QAC3D,IAAI,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;YAChC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YAEvB,KACC,IAAI,QAAQ,GAAG,SAAS,GAAG,SAAS,EACpC,QAAQ,GAAG,KAAK,EAChB,QAAQ,IAAI,CAAC,GAAG,SAAS,EACxB,CAAC;gBACF,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;YACzB,CAAC;QACF,CAAC;IACF,CAAC;IAED,OAAO,YAAY,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAClC,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAClC,SAAqB,EACrB,UAAU,GAAG,KAAK;IAElB,6EAA6E;IAC7E,MAAM,KAAK,GACV,SAAS,CAAC,MAAM,GAAG,CAAC,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,IAAI,CAAC,CAAC,CAAC,CAAC;QACnB,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;IACxB,CAAC;AACF,CAAC;AAED;;;;GAIG;AACH,SAAS,SAAS,CACjB,GAAe,EACf,SAAqB,EACrB,QAAoB,EACpB,SAAiB;IAEjB,MAAM,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC,MAAM,GAAG,SAAS,CAAC;IAChD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAE7C,GAAG,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;IAE9B,SAAS,CAAC;QACT,MAAM,MAAM,GAAG,cAAc,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QAEhD,IAAI,CAAC;YACJ,MAAM,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAE,GAAG,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC;YAE3C,iEAAiE;YACjE,IACC,2BAA2B,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC;gBAC5C,2BAA2B,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,CAAC,EAChD,CAAC;gBACF,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;gBAEhB,OAAO;YACR,CAAC;QACF,CAAC;gBAAS,CAAC;YACV,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAChB,CAAC;IACF,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type PrivateKeyState, type PublicKeyState } from "./_key-state.js";
|
|
2
|
+
/**
|
|
3
|
+
* RSAEP / RSAVP1 (RFC 8017 §5.1.1, §5.2.2): `input^e mod n` over `k` bytes, or
|
|
4
|
+
* `undefined` when `input` is not below n. Constant time in `input`, which an
|
|
5
|
+
* encryption keeps secret; the range check is the only branch on it, and no
|
|
6
|
+
* encoding this package makes is out of range.
|
|
7
|
+
*/
|
|
8
|
+
export declare function rsaPublicOperation(state: PublicKeyState, input: Uint8Array): Uint8Array<ArrayBuffer> | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* RSADP / RSASP1 (RFC 8017 §5.1.2, §5.2.1): `input^d mod n` over `k` bytes, for
|
|
11
|
+
* an `input` below n (the caller checks). Constant time in the key, the input
|
|
12
|
+
* and the result, with the defences of the CFRG's RSA implementation guidance
|
|
13
|
+
* (draft-irtf-cfrg-rsa-guidance §6) on every call:
|
|
14
|
+
*
|
|
15
|
+
* - Base blinding: the input is multiplied by bb^e for a fresh random bb, and the
|
|
16
|
+
* result by bb⁻¹;
|
|
17
|
+
* - Exponent blinding: each exponent d_i becomes d_i + b_i·(r_i − 1) for a fresh
|
|
18
|
+
* 64-bit b_i (d + b·(e·d − 1) for a key without primes);
|
|
19
|
+
* - Modulus blinding: each prime's exponentiation runs modulo g_i·r_i for a fresh
|
|
20
|
+
* odd 64-bit g_i;
|
|
21
|
+
* - The result is checked by raising it to e, so that a fault in the CRT
|
|
22
|
+
* computation cannot release a value that factors n (Bellcore).
|
|
23
|
+
*
|
|
24
|
+
* @throws {RsaError} `privateKeyFault` when the check fails.
|
|
25
|
+
*/
|
|
26
|
+
export declare function rsaPrivateOperation(state: PrivateKeyState, input: Uint8Array): Uint8Array<ArrayBuffer>;
|
|
27
|
+
//# sourceMappingURL=_primitives.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_primitives.d.ts","sourceRoot":"","sources":["../../src/key/_primitives.ts"],"names":[],"mappings":"AAUA,OAAO,EAEN,KAAK,eAAe,EACpB,KAAK,cAAc,EAEnB,MAAM,iBAAiB,CAAC;AAKzB;;;;;GAKG;AACH,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,cAAc,EACrB,KAAK,EAAE,UAAU,GACf,UAAU,CAAC,WAAW,CAAC,GAAG,SAAS,CAiBrC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,mBAAmB,CAClC,KAAK,EAAE,eAAe,EACtB,KAAK,EAAE,UAAU,GACf,UAAU,CAAC,WAAW,CAAC,CAoDzB"}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { getRandomBytes } from "@ac-kit/crypto-random";
|
|
2
|
+
import { ConstantTimeModulus, constantTimeUnsignedAdd, constantTimeUnsignedModInverse, constantTimeUnsignedMultiply, } from "@ac-kit/crypto-safe";
|
|
3
|
+
import { RsaError } from "../rsa-error.js";
|
|
4
|
+
import { publicStateOf, } from "./_key-state.js";
|
|
5
|
+
/** Bytes of each random blinding factor of the exponents and the moduli. */
|
|
6
|
+
const BLINDING_BYTES = 8;
|
|
7
|
+
/**
|
|
8
|
+
* RSAEP / RSAVP1 (RFC 8017 §5.1.1, §5.2.2): `input^e mod n` over `k` bytes, or
|
|
9
|
+
* `undefined` when `input` is not below n. Constant time in `input`, which an
|
|
10
|
+
* encryption keeps secret; the range check is the only branch on it, and no
|
|
11
|
+
* encoding this package makes is out of range.
|
|
12
|
+
*/
|
|
13
|
+
export function rsaPublicOperation(state, input) {
|
|
14
|
+
const { ring } = state;
|
|
15
|
+
const value = ring.create();
|
|
16
|
+
const out = new Uint8Array(state.modulus.length);
|
|
17
|
+
try {
|
|
18
|
+
if (ring.fromBytesBe(value, input) === 0) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
ring.power(value, value, state.publicExponent);
|
|
22
|
+
ring.toBytesBe(out, value);
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
value.fill(0);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* RSADP / RSASP1 (RFC 8017 §5.1.2, §5.2.1): `input^d mod n` over `k` bytes, for
|
|
31
|
+
* an `input` below n (the caller checks). Constant time in the key, the input
|
|
32
|
+
* and the result, with the defences of the CFRG's RSA implementation guidance
|
|
33
|
+
* (draft-irtf-cfrg-rsa-guidance §6) on every call:
|
|
34
|
+
*
|
|
35
|
+
* - Base blinding: the input is multiplied by bb^e for a fresh random bb, and the
|
|
36
|
+
* result by bb⁻¹;
|
|
37
|
+
* - Exponent blinding: each exponent d_i becomes d_i + b_i·(r_i − 1) for a fresh
|
|
38
|
+
* 64-bit b_i (d + b·(e·d − 1) for a key without primes);
|
|
39
|
+
* - Modulus blinding: each prime's exponentiation runs modulo g_i·r_i for a fresh
|
|
40
|
+
* odd 64-bit g_i;
|
|
41
|
+
* - The result is checked by raising it to e, so that a fault in the CRT
|
|
42
|
+
* computation cannot release a value that factors n (Bellcore).
|
|
43
|
+
*
|
|
44
|
+
* @throws {RsaError} `privateKeyFault` when the check fails.
|
|
45
|
+
*/
|
|
46
|
+
export function rsaPrivateOperation(state, input) {
|
|
47
|
+
const publicState = publicStateOf(state.publicKey);
|
|
48
|
+
const { ring } = publicState;
|
|
49
|
+
const length = publicState.modulus.length;
|
|
50
|
+
const original = ring.create();
|
|
51
|
+
const value = ring.create();
|
|
52
|
+
const blinding = ring.create();
|
|
53
|
+
const unblinding = ring.create();
|
|
54
|
+
const check = ring.create();
|
|
55
|
+
const blindingBytes = new Uint8Array(length);
|
|
56
|
+
const unblindingBytes = new Uint8Array(length);
|
|
57
|
+
const out = new Uint8Array(length);
|
|
58
|
+
try {
|
|
59
|
+
ring.fromBytesBe(original, input);
|
|
60
|
+
drawBlinding_(publicState, blindingBytes, unblindingBytes);
|
|
61
|
+
ring.fromBytesBe(blinding, blindingBytes);
|
|
62
|
+
ring.fromBytesBe(unblinding, unblindingBytes);
|
|
63
|
+
ring.power(value, blinding, publicState.publicExponent);
|
|
64
|
+
ring.multiply(value, value, original);
|
|
65
|
+
if (state.primes === undefined) {
|
|
66
|
+
exponentiateWithoutPrimes_(state, publicState, value);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
exponentiateCrt_(state.primes, publicState, value);
|
|
70
|
+
}
|
|
71
|
+
ring.multiply(value, value, unblinding);
|
|
72
|
+
ring.power(check, value, publicState.publicExponent);
|
|
73
|
+
const valid = ring.isEqual(check, original);
|
|
74
|
+
ring.toBytesBe(out, value);
|
|
75
|
+
if (valid === 0) {
|
|
76
|
+
out.fill(0);
|
|
77
|
+
throw new RsaError("privateKeyFault", "An RSA private-key result failed its check");
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
for (const element of [original, value, blinding, unblinding, check]) {
|
|
83
|
+
element.fill(0);
|
|
84
|
+
}
|
|
85
|
+
blindingBytes.fill(0);
|
|
86
|
+
unblindingBytes.fill(0);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* A uniform bb in [1, n − 1] invertible modulo n, and bb⁻¹. Rejection reveals
|
|
91
|
+
* only that a discarded candidate was refused.
|
|
92
|
+
*/
|
|
93
|
+
function drawBlinding_(state, blinding, unblinding) {
|
|
94
|
+
const { ring, modulus } = state;
|
|
95
|
+
const excess = 8 * modulus.length - state.modulusBitLength;
|
|
96
|
+
const element = ring.create();
|
|
97
|
+
try {
|
|
98
|
+
for (;;) {
|
|
99
|
+
const candidate = getRandomBytes(modulus.length);
|
|
100
|
+
try {
|
|
101
|
+
candidate[0] = candidate[0] & (0xff >>> excess);
|
|
102
|
+
const inRange = ring.fromBytesBe(element, candidate) & (1 - ring.isZero(element));
|
|
103
|
+
const invertible = constantTimeUnsignedModInverse(unblinding, candidate, modulus);
|
|
104
|
+
if ((inRange & invertible) === 1) {
|
|
105
|
+
blinding.set(candidate);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
candidate.fill(0);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
element.fill(0);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** `value = value^(d + b·(e·d − 1)) mod n` for a fresh 64-bit b. */
|
|
119
|
+
function exponentiateWithoutPrimes_(state, publicState, value) {
|
|
120
|
+
const exponent = blindExponent_(state.privateExponent, state.multipleOfLambda);
|
|
121
|
+
try {
|
|
122
|
+
publicState.ring.power(value, value, exponent);
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
exponent.fill(0);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** `exponent + b · multiple` for a fresh random 64-bit b. */
|
|
129
|
+
function blindExponent_(exponent, multiple) {
|
|
130
|
+
const factor = getRandomBytes(BLINDING_BYTES);
|
|
131
|
+
const product = new Uint8Array(multiple.length + BLINDING_BYTES);
|
|
132
|
+
const blinded = new Uint8Array(Math.max(product.length, exponent.length) + 1);
|
|
133
|
+
try {
|
|
134
|
+
constantTimeUnsignedMultiply(product, factor, multiple);
|
|
135
|
+
constantTimeUnsignedAdd(blinded, product, exponent);
|
|
136
|
+
return blinded;
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
factor.fill(0);
|
|
140
|
+
product.fill(0);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* `value = value^d mod n` by the CRT (RFC 8017 §5.1.2 step 2.b), each
|
|
145
|
+
* exponentiation blinded in its exponent and its modulus, then Garner's
|
|
146
|
+
* recombination in the arithmetic modulo n, where every partial sum is below n
|
|
147
|
+
* so the modular operations are exact.
|
|
148
|
+
*/
|
|
149
|
+
function exponentiateCrt_(primes, publicState, value) {
|
|
150
|
+
const { ring } = publicState;
|
|
151
|
+
const length = publicState.modulus.length;
|
|
152
|
+
const valueBytes = new Uint8Array(length);
|
|
153
|
+
const residues = [];
|
|
154
|
+
const scratch = [];
|
|
155
|
+
const elements = [];
|
|
156
|
+
try {
|
|
157
|
+
ring.toBytesBe(valueBytes, value);
|
|
158
|
+
for (const prime of primes) {
|
|
159
|
+
residues.push(exponentiateModPrime_(prime, valueBytes));
|
|
160
|
+
}
|
|
161
|
+
const first = primes[0];
|
|
162
|
+
const second = primes[1];
|
|
163
|
+
const secondBytes = bytesOf_(second.ring, residues[1], scratch);
|
|
164
|
+
const difference = first.ring.create();
|
|
165
|
+
const coefficient = first.ring.create();
|
|
166
|
+
elements.push(difference, coefficient);
|
|
167
|
+
// h = (m_1 − m_2)·qInv mod p.
|
|
168
|
+
first.ring.reduceBytesBe(difference, secondBytes);
|
|
169
|
+
first.ring.subtract(difference, residues[0], difference);
|
|
170
|
+
first.ring.fromBytesBe(coefficient, first.coefficient);
|
|
171
|
+
first.ring.multiply(difference, difference, coefficient);
|
|
172
|
+
// m = m_2 + q·h.
|
|
173
|
+
const factor = ring.create();
|
|
174
|
+
const term = ring.create();
|
|
175
|
+
elements.push(factor, term);
|
|
176
|
+
ring.fromBytesBe(value, secondBytes);
|
|
177
|
+
ring.fromBytesBe(factor, second.prime);
|
|
178
|
+
ring.fromBytesBe(term, bytesOf_(first.ring, difference, scratch));
|
|
179
|
+
ring.multiply(term, term, factor);
|
|
180
|
+
ring.add(value, value, term);
|
|
181
|
+
for (let index = 2; index < primes.length; index++) {
|
|
182
|
+
const prime = primes[index];
|
|
183
|
+
const partial = prime.ring.create();
|
|
184
|
+
const primeCoefficient = prime.ring.create();
|
|
185
|
+
elements.push(partial, primeCoefficient);
|
|
186
|
+
// h = (m_i − m)·t_i mod r_i, m = m + R·h with R = r_1 ⋯ r_(i−1).
|
|
187
|
+
ring.toBytesBe(valueBytes, value);
|
|
188
|
+
prime.ring.reduceBytesBe(partial, valueBytes);
|
|
189
|
+
prime.ring.subtract(partial, residues[index], partial);
|
|
190
|
+
prime.ring.fromBytesBe(primeCoefficient, prime.coefficient);
|
|
191
|
+
prime.ring.multiply(partial, partial, primeCoefficient);
|
|
192
|
+
ring.fromBytesBe(factor, prime.productBefore);
|
|
193
|
+
ring.fromBytesBe(term, bytesOf_(prime.ring, partial, scratch));
|
|
194
|
+
ring.multiply(term, term, factor);
|
|
195
|
+
ring.add(value, value, term);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
valueBytes.fill(0);
|
|
200
|
+
for (const array of [...residues, ...elements, ...scratch]) {
|
|
201
|
+
array.fill(0);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* `c^(d_i + b·(r_i − 1)) mod (g·r_i)`, reduced modulo r_i, for fresh random b
|
|
207
|
+
* and odd g of 64 bits (g's top bit set, so the blinded modulus has a fixed
|
|
208
|
+
* length).
|
|
209
|
+
*/
|
|
210
|
+
function exponentiateModPrime_(prime, valueBytes) {
|
|
211
|
+
const factor = getRandomBytes(BLINDING_BYTES);
|
|
212
|
+
const blindedModulus = new Uint8Array(prime.prime.length + BLINDING_BYTES);
|
|
213
|
+
const exponent = blindExponent_(prime.exponent, prime.primeMinusOne);
|
|
214
|
+
let blindedRing;
|
|
215
|
+
let blindedValue;
|
|
216
|
+
let blindedBytes;
|
|
217
|
+
try {
|
|
218
|
+
factor[0] = factor[0] | 0x80;
|
|
219
|
+
factor[BLINDING_BYTES - 1] = factor[BLINDING_BYTES - 1] | 1;
|
|
220
|
+
constantTimeUnsignedMultiply(blindedModulus, prime.prime, factor);
|
|
221
|
+
blindedRing = new ConstantTimeModulus(blindedModulus);
|
|
222
|
+
blindedValue = blindedRing.create();
|
|
223
|
+
blindedRing.reduceBytesBe(blindedValue, valueBytes);
|
|
224
|
+
blindedRing.power(blindedValue, blindedValue, exponent);
|
|
225
|
+
blindedBytes = new Uint8Array(blindedRing.byteLength);
|
|
226
|
+
blindedRing.toBytesBe(blindedBytes, blindedValue);
|
|
227
|
+
const residue = prime.ring.create();
|
|
228
|
+
prime.ring.reduceBytesBe(residue, blindedBytes);
|
|
229
|
+
return residue;
|
|
230
|
+
}
|
|
231
|
+
finally {
|
|
232
|
+
factor.fill(0);
|
|
233
|
+
blindedModulus.fill(0);
|
|
234
|
+
exponent.fill(0);
|
|
235
|
+
blindedValue?.fill(0);
|
|
236
|
+
blindedBytes?.fill(0);
|
|
237
|
+
blindedRing?.[Symbol.dispose]();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
/** The bytes of `element`, recorded in `scratch` for wiping. */
|
|
241
|
+
function bytesOf_(ring, element, scratch) {
|
|
242
|
+
const bytes = new Uint8Array(ring.byteLength);
|
|
243
|
+
ring.toBytesBe(bytes, element);
|
|
244
|
+
scratch.push(bytes);
|
|
245
|
+
return bytes;
|
|
246
|
+
}
|
|
247
|
+
//# sourceMappingURL=_primitives.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_primitives.js","sourceRoot":"","sources":["../../src/key/_primitives.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EACN,mBAAmB,EACnB,uBAAuB,EACvB,8BAA8B,EAC9B,4BAA4B,GAE5B,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAIN,aAAa,GACb,MAAM,iBAAiB,CAAC;AAEzB,4EAA4E;AAC5E,MAAM,cAAc,GAAG,CAAC,CAAC;AAEzB;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CACjC,KAAqB,EACrB,KAAiB;IAEjB,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;IACvB,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;IAC5B,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAEjD,IAAI,CAAC;QACJ,IAAI,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YAC1C,OAAO,SAAS,CAAC;QAClB,CAAC;QAED,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC;QAC/C,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAE3B,OAA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U,CAAC,WAAW,CAAC,UAAU,CAAC,CAAC;QACtD,WAAW,CAAC,SAAS,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;QAElD,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;QAEpC,KAAK,CAAC,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;QAEhD,OAAO,OAAO,CAAC;IAChB,CAAC;YAAS,CAAC;QACV,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACf,cAAc,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACvB,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACjB,YAAY,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;QACtB,YAAY,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;QACtB,WAAW,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;IACjC,CAAC;AACF,CAAC;AAED,gEAAgE;AAChE,SAAS,QAAQ,CAChB,IAAyB,EACzB,OAAuB,EACvB,OAAqB;IAErB,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAE9C,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC/B,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAEpB,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type PrimeState, type PrivateKeyState } from "./_key-state.js";
|
|
2
|
+
import type { RsaPublicKey } from "./rsa-public-key.js";
|
|
3
|
+
/** A prime of a CRT key with its exponent and coefficient (RFC 8017 §3.2). */
|
|
4
|
+
export type CrtPrime = {
|
|
5
|
+
readonly prime: Uint8Array;
|
|
6
|
+
readonly exponent: Uint8Array;
|
|
7
|
+
/** Q⁻¹ mod p for the first prime, ignored for the second, t_i after. */
|
|
8
|
+
readonly coefficient: Uint8Array;
|
|
9
|
+
};
|
|
10
|
+
/** The secret integers of a private key, big-endian. */
|
|
11
|
+
export type PrivateIntegers = {
|
|
12
|
+
readonly privateExponent: Uint8Array;
|
|
13
|
+
readonly primes: readonly CrtPrime[] | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* 0 when {@link deriveFromPrimes} stood in for a refused prime, so that the
|
|
16
|
+
* key is refused after the same work; 1 or absent otherwise.
|
|
17
|
+
*/
|
|
18
|
+
readonly derivationValid?: number;
|
|
19
|
+
};
|
|
20
|
+
/** Options of the private-key import functions. */
|
|
21
|
+
export interface RsaPrivateKeyImportOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Test each prime of a CRT key with trial division and five exhaustive
|
|
24
|
+
* Miller–Rabin rounds, as SP 800-56B §6.4.1.3 key-pair validation does.
|
|
25
|
+
* Default true. The test costs about five exponentiations modulo each prime,
|
|
26
|
+
* several times the consistency checks: about 80 ms for a 2048-bit key and
|
|
27
|
+
* half a second at 4096 bits on a current x86-64 machine, against 10 and 30
|
|
28
|
+
* ms without; pass false only for keys from a trusted source, such as this
|
|
29
|
+
* package's own exports. The consistency checks always run: they bind the
|
|
30
|
+
* primes to n, e and d, and every private-key operation checks its own
|
|
31
|
+
* result. A key of the first form (n, d) has no primes to test.
|
|
32
|
+
*/
|
|
33
|
+
readonly checkPrimality?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The state of the private key of `publicKey` with `integers`, after checking
|
|
37
|
+
* them: 0 < d < n; for a CRT key, that the primes multiply to n, each prime r
|
|
38
|
+
* is odd and above 3 (and, by default, probably prime), e·d_i ≡ 1 and d ≡ d_i
|
|
39
|
+
* modulo r − 1 with d_i < r − 1, and each coefficient is the inverse it must be
|
|
40
|
+
* and below its prime; without primes, that (2^e)^d ≡ 2 (mod n).
|
|
41
|
+
*
|
|
42
|
+
* @throws {RsaError} `invalidKey` when any check fails.
|
|
43
|
+
*/
|
|
44
|
+
export declare function buildPrivateState(publicKey: RsaPublicKey, integers: PrivateIntegers, options: RsaPrivateKeyImportOptions): PrivateKeyState;
|
|
45
|
+
/** Overwrites the secrets of `primes` and disposes of their arithmetic. */
|
|
46
|
+
export declare function wipePrimes(primes: readonly PrimeState[]): void;
|
|
47
|
+
/**
|
|
48
|
+
* The CRT integers of a key from its primes alone (FIPS 186-5 A.1.1 3 and RFC
|
|
49
|
+
* 8017 §3.2): n = ∏ r_i, λ = lcm(r_i − 1), d = e⁻¹ mod λ unless given, d_i = d
|
|
50
|
+
* mod (r_i − 1), q⁻¹ mod p and t_i = (r_1 ⋯ r_(i−1))⁻¹ mod r_i. Constant time
|
|
51
|
+
* in the primes; the inverses that do not exist come out as garbage, which
|
|
52
|
+
* {@link buildPrivateState} then refuses. A prime that is not odd and above 3
|
|
53
|
+
* (0, 1 or none included) is replaced by an odd stand-in of its length, so that
|
|
54
|
+
* the arithmetic neither throws nor loops, and `derivationValid` is 0.
|
|
55
|
+
*/
|
|
56
|
+
export declare function deriveFromPrimes(primes: readonly Uint8Array[], publicExponent: Uint8Array, privateExponent: Uint8Array | undefined): {
|
|
57
|
+
readonly modulus: Uint8Array<ArrayBuffer>;
|
|
58
|
+
readonly integers: PrivateIntegers;
|
|
59
|
+
};
|
|
60
|
+
//# sourceMappingURL=_validate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_validate.d.ts","sourceRoot":"","sources":["../../src/key/_validate.ts"],"names":[],"mappings":"AAYA,OAAO,EACN,KAAK,UAAU,EACf,KAAK,eAAe,EAEpB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAaxD,8EAA8E;AAC9E,MAAM,MAAM,QAAQ,GAAG;IACtB,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC;IAC9B,wEAAwE;IACxE,QAAQ,CAAC,WAAW,EAAE,UAAU,CAA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