@ac-kit/crypto-rsa 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +68 -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 @@
|
|
|
1
|
+
{"version":3,"file":"rsa-algorithm.d.ts","sourceRoot":"","sources":["../../src/encoding/rsa-algorithm.ts"],"names":[],"mappings":"AAuBA,OAAO,EAGN,KAAK,OAAO,EACZ,KAAK,iBA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,KAAK,EAAE,UAAU,GACf,iBAAiB,CAUnB;AAwDD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC/B,IAAI,EAAE,OAAO,EACb,MAAM,EAAE,UAAU,GAChB,UAAU,CAAC,WAAW,CAAC,CAUzB;AAOD;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACpC,SAAS,EAAE,eAAe,GACxB,UAAU,CAAC,WAAW,CAAC,CAkBzB;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,UAAU,GAAG,eAAe,CA2BtE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,2BAA2B,CAC1C,SAAS,EAAE,qBAAqB,GAC9B,UAAU,CAAC,WAAW,CAAC,CAkBzB;AAED;;;;;;;;GAQG;AACH,wBAAgB,2BAA2B,CAC1C,GAAG,EAAE,UAAU,GACb,qBAAqB,CAkBvB;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACrC,UAAU,EAAE,iBAAiB,GAC3B,UAAU,CAAC,WAAW,CAAC,CAyBzB;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,UAAU,GAAG,iBAAiB,CAazE;AAED,4EAA4E;AAC5E,wBAAgB,kBAAkB,CACjC,CAAC,EAAE,gBAAgB,EACnB,CAAC,EAAE,gBAAgB,GACjB,OAAO,CAMT;AAED,0DAA0D;AAC1D,wBAAgB,mBAAmB,CAClC,CAAC,EAAE,iBAAiB,EACpB,CAAC,EAAE,iBAAiB,GAClB,OAAO,CAMT"}
|
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import { any, component, contextTag, DecodingError, EncodingError, integer, objectIdentifier, octetString, ref, sequence, } from "@ac-kit/format-asn1";
|
|
2
|
+
import { derDecode, derEncode } from "@ac-kit/format-asn1/der";
|
|
3
|
+
import { oidEqual, oidFromDotted, oidToDotted, } from "@ac-kit/format-oid";
|
|
4
|
+
import { mgf1, RSA_HASHES, } from "../hash/rsa-hash.js";
|
|
5
|
+
import { RsaError } from "../rsa-error.js";
|
|
6
|
+
/** `{ name: "rsaEncryption" }`, the algorithm of an unrestricted key. */
|
|
7
|
+
export const RSA_ENCRYPTION_ALGORITHM = Object.freeze({
|
|
8
|
+
name: "rsaEncryption",
|
|
9
|
+
});
|
|
10
|
+
const RSA_ENCRYPTION_ = oidFromDotted("1.2.840.113549.1.1.1");
|
|
11
|
+
const RSAES_OAEP_ = oidFromDotted("1.2.840.113549.1.1.7");
|
|
12
|
+
const MGF1_ = oidFromDotted("1.2.840.113549.1.1.8");
|
|
13
|
+
const P_SPECIFIED_ = oidFromDotted("1.2.840.113549.1.1.9");
|
|
14
|
+
const RSASSA_PSS_ = oidFromDotted("1.2.840.113549.1.1.10");
|
|
15
|
+
const RSASSA_PSS_SHAKE128_ = oidFromDotted("1.3.6.1.5.5.7.6.30");
|
|
16
|
+
const RSASSA_PSS_SHAKE256_ = oidFromDotted("1.3.6.1.5.5.7.6.31");
|
|
17
|
+
/**
|
|
18
|
+
* `<hash>WithRSAEncryption` and `id-rsassa-pkcs1-v1_5-with-<sha3>`, by hash
|
|
19
|
+
* name.
|
|
20
|
+
*/
|
|
21
|
+
const PKCS1_SIGNATURE_OIDS_ = {
|
|
22
|
+
"SHA-1": oidFromDotted("1.2.840.113549.1.1.5"),
|
|
23
|
+
"SHA-224": oidFromDotted("1.2.840.113549.1.1.14"),
|
|
24
|
+
"SHA-256": oidFromDotted("1.2.840.113549.1.1.11"),
|
|
25
|
+
"SHA-384": oidFromDotted("1.2.840.113549.1.1.12"),
|
|
26
|
+
"SHA-512": oidFromDotted("1.2.840.113549.1.1.13"),
|
|
27
|
+
"SHA3-224": oidFromDotted("2.16.840.1.101.3.4.3.13"),
|
|
28
|
+
"SHA3-256": oidFromDotted("2.16.840.1.101.3.4.3.14"),
|
|
29
|
+
"SHA3-384": oidFromDotted("2.16.840.1.101.3.4.3.15"),
|
|
30
|
+
"SHA3-512": oidFromDotted("2.16.840.1.101.3.4.3.16"),
|
|
31
|
+
};
|
|
32
|
+
const NULL_ENCODING_ = Uint8Array.of(0x05, 0x00);
|
|
33
|
+
const DEFAULT_SALT_LENGTH_ = 20;
|
|
34
|
+
const MAXIMUM_SALT_LENGTH_ = 0xffff;
|
|
35
|
+
/**
|
|
36
|
+
* @throws {RsaError} `invalidParameters` unless `saltLength` is an integer from
|
|
37
|
+
* 0 to 65535.
|
|
38
|
+
*/
|
|
39
|
+
export function checkPssSaltLength(saltLength) {
|
|
40
|
+
if (!Number.isInteger(saltLength) ||
|
|
41
|
+
saltLength < 0 ||
|
|
42
|
+
saltLength > MAXIMUM_SALT_LENGTH_) {
|
|
43
|
+
throw new RsaError("invalidParameters", `A PSS salt length must be an integer from 0 to ${MAXIMUM_SALT_LENGTH_}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Frozen OAEP parameters over a private copy of `label`, which `label` reads as
|
|
48
|
+
* a fresh copy each time, so that no caller can change the label a key or
|
|
49
|
+
* scheme enforces.
|
|
50
|
+
*/
|
|
51
|
+
export function createOaepParameters(hash, maskGeneration, label) {
|
|
52
|
+
const stored = label.slice();
|
|
53
|
+
return Object.freeze({
|
|
54
|
+
hash,
|
|
55
|
+
maskGeneration,
|
|
56
|
+
get label() {
|
|
57
|
+
return stored.slice();
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
const ALGORITHM_IDENTIFIER_TYPE_ = sequence([
|
|
62
|
+
component("algorithm", objectIdentifier()),
|
|
63
|
+
component("parameters", any()).optional(),
|
|
64
|
+
]);
|
|
65
|
+
const ALGORITHM_IDENTIFIER_ = ref(ALGORITHM_IDENTIFIER_TYPE_);
|
|
66
|
+
/*
|
|
67
|
+
* The DEFAULTs of both parameter types are handled here rather than declared
|
|
68
|
+
* on the schema: an absent component reads as its default, and a present one
|
|
69
|
+
* equal to it is refused, as DER requires (X.690 §11.5), with "equal" meaning
|
|
70
|
+
* the same algorithm whether its hash parameters are NULL or absent (RFC 4055
|
|
71
|
+
* §2.1 makes the two equivalent).
|
|
72
|
+
*/
|
|
73
|
+
const PSS_PARAMETERS_ = ref(sequence([
|
|
74
|
+
component("hashAlgorithm", contextTag(0, ALGORITHM_IDENTIFIER_TYPE_)).optional(),
|
|
75
|
+
component("maskGenAlgorithm", contextTag(1, ALGORITHM_IDENTIFIER_TYPE_)).optional(),
|
|
76
|
+
component("saltLength", contextTag(2, integer())).optional(),
|
|
77
|
+
component("trailerField", contextTag(3, integer())).optional(),
|
|
78
|
+
]));
|
|
79
|
+
const OAEP_PARAMETERS_ = ref(sequence([
|
|
80
|
+
component("hashAlgorithm", contextTag(0, ALGORITHM_IDENTIFIER_TYPE_)).optional(),
|
|
81
|
+
component("maskGenAlgorithm", contextTag(1, ALGORITHM_IDENTIFIER_TYPE_)).optional(),
|
|
82
|
+
component("pSourceAlgorithm", contextTag(2, ALGORITHM_IDENTIFIER_TYPE_)).optional(),
|
|
83
|
+
]));
|
|
84
|
+
const OCTET_STRING_ = ref(octetString());
|
|
85
|
+
const DIGEST_INFO_ = ref(sequence([
|
|
86
|
+
component("digestAlgorithm", ALGORITHM_IDENTIFIER_TYPE_),
|
|
87
|
+
component("digest", octetString()),
|
|
88
|
+
]));
|
|
89
|
+
/**
|
|
90
|
+
* The DER `DigestInfo` of EMSA-PKCS1-v1_5 (RFC 8017 §9.2 step 2): the hash's
|
|
91
|
+
* identifier with NULL parameters, the form §9.2 note 1 lists for SHA-1 and
|
|
92
|
+
* SHA-2 and the one OpenSSL and the test corpora use for SHA-3, then the
|
|
93
|
+
* digest.
|
|
94
|
+
*/
|
|
95
|
+
export function encodeDigestInfo(hash, digest) {
|
|
96
|
+
return new Uint8Array(derEncode(DIGEST_INFO_, {
|
|
97
|
+
digestAlgorithm: {
|
|
98
|
+
algorithm: oidFromDotted(hash.oid),
|
|
99
|
+
parameters: { encoded: NULL_ENCODING_ },
|
|
100
|
+
},
|
|
101
|
+
digest,
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The DER `AlgorithmIdentifier` of a key (RFC 8017 Appendix A.1, RFC 4055 §1.2
|
|
106
|
+
* and §4.1, RFC 8702 §4).
|
|
107
|
+
*
|
|
108
|
+
* @throws {RsaError} `invalidParameters` for parameters no identifier
|
|
109
|
+
* expresses, a PSS salt length included.
|
|
110
|
+
*/
|
|
111
|
+
export function encodeRsaKeyAlgorithm(algorithm) {
|
|
112
|
+
switch (algorithm.name) {
|
|
113
|
+
case "rsaEncryption": {
|
|
114
|
+
return encodeAlgorithmIdentifier_(RSA_ENCRYPTION_, NULL_ENCODING_);
|
|
115
|
+
}
|
|
116
|
+
case "RSASSA-PSS": {
|
|
117
|
+
return algorithm.parameters === undefined
|
|
118
|
+
? encodeAlgorithmIdentifier_(RSASSA_PSS_, undefined)
|
|
119
|
+
: encodePss_(algorithm.parameters);
|
|
120
|
+
}
|
|
121
|
+
case "RSAES-OAEP": {
|
|
122
|
+
return algorithm.parameters === undefined
|
|
123
|
+
? encodeAlgorithmIdentifier_(RSAES_OAEP_, undefined)
|
|
124
|
+
: encodeRsaOaepAlgorithm(algorithm.parameters);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The key algorithm a DER `AlgorithmIdentifier` names, read strictly.
|
|
130
|
+
*
|
|
131
|
+
* @throws {RsaError} `invalidEncoding` for malformed DER or parameters that
|
|
132
|
+
* break their type's rules; `unsupportedAlgorithm` for an algorithm or hash
|
|
133
|
+
* this package does not implement.
|
|
134
|
+
*/
|
|
135
|
+
export function decodeRsaKeyAlgorithm(der) {
|
|
136
|
+
const identifier = decodeAlgorithmIdentifier_(der);
|
|
137
|
+
const { algorithm, parameters } = identifier;
|
|
138
|
+
if (oidEqual(algorithm, RSA_ENCRYPTION_)) {
|
|
139
|
+
requireNull_(parameters, "rsaEncryption");
|
|
140
|
+
return Object.freeze({ name: "rsaEncryption" });
|
|
141
|
+
}
|
|
142
|
+
if (oidEqual(algorithm, RSAES_OAEP_)) {
|
|
143
|
+
return Object.freeze({
|
|
144
|
+
name: "RSAES-OAEP",
|
|
145
|
+
parameters: parameters === undefined
|
|
146
|
+
? undefined
|
|
147
|
+
: decodeOaepParameters_(parameters),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
return Object.freeze({
|
|
151
|
+
name: "RSASSA-PSS",
|
|
152
|
+
parameters: oidEqual(algorithm, RSASSA_PSS_) && parameters === undefined
|
|
153
|
+
? undefined
|
|
154
|
+
: decodePssIdentifier_(identifier),
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The DER `AlgorithmIdentifier` of a signature algorithm, as X.509 and CMS
|
|
159
|
+
* carry it: `<hash>WithRSAEncryption` with NULL parameters (RFC 8017 Appendix
|
|
160
|
+
* A.2.4, RFC 4055 §5) or `id-rsassa-pkcs1-v1_5-with-sha3-*` with none (NIST
|
|
161
|
+
* CSOR); `id-RSASSA-PSS` with its parameters (RFC 4055 §3.1), or
|
|
162
|
+
* `id-RSASSA-PSS-SHAKE128`/`SHAKE256` with none when the parameters are those
|
|
163
|
+
* RFC 8702 §3.1 fixes.
|
|
164
|
+
*
|
|
165
|
+
* @throws {RsaError} `invalidParameters` for a combination no identifier
|
|
166
|
+
* expresses (PKCS #1 v1.5 with SHAKE, SHAKE masks with other hashes, a mask
|
|
167
|
+
* over a SHAKE) or a PSS salt length that is not an integer in [0, 65535].
|
|
168
|
+
*/
|
|
169
|
+
export function encodeRsaSignatureAlgorithm(algorithm) {
|
|
170
|
+
if (algorithm.name === "RSASSA-PSS") {
|
|
171
|
+
return encodePss_(algorithm.parameters);
|
|
172
|
+
}
|
|
173
|
+
const oid = PKCS1_SIGNATURE_OIDS_[algorithm.hash.name];
|
|
174
|
+
if (oid === undefined || !isKnownHash_(algorithm.hash)) {
|
|
175
|
+
throw new RsaError("invalidParameters", "RSASSA-PKCS1-v1_5 has no algorithm identifier for this hash");
|
|
176
|
+
}
|
|
177
|
+
return encodeAlgorithmIdentifier_(oid, algorithm.hash.name.startsWith("SHA3-") ? undefined : NULL_ENCODING_);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The signature algorithm a DER `AlgorithmIdentifier` names, read strictly.
|
|
181
|
+
* PKCS #1 v1.5 identifiers take NULL parameters for SHA-1 and SHA-2 (RFC 4055
|
|
182
|
+
* §5 says MUST) and none for SHA-3 (absent or NULL accepted for both, as RFC
|
|
183
|
+
* 4055 §2.1 asks of readers).
|
|
184
|
+
*
|
|
185
|
+
* @throws {RsaError} `invalidEncoding`; `unsupportedAlgorithm` for an algorithm
|
|
186
|
+
* or hash this package does not implement.
|
|
187
|
+
*/
|
|
188
|
+
export function decodeRsaSignatureAlgorithm(der) {
|
|
189
|
+
const identifier = decodeAlgorithmIdentifier_(der);
|
|
190
|
+
for (const [name, oid] of Object.entries(PKCS1_SIGNATURE_OIDS_)) {
|
|
191
|
+
if (oidEqual(identifier.algorithm, oid)) {
|
|
192
|
+
requireNullOrAbsent_(identifier.parameters, name);
|
|
193
|
+
return Object.freeze({
|
|
194
|
+
name: "RSASSA-PKCS1-v1_5",
|
|
195
|
+
hash: RSA_HASHES[name],
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return Object.freeze({
|
|
200
|
+
name: "RSASSA-PSS",
|
|
201
|
+
parameters: decodePssIdentifier_(identifier),
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* The DER `AlgorithmIdentifier` `id-RSAES-OAEP` with its parameters (RFC 8017
|
|
206
|
+
* Appendix A.2.1, RFC 4055 §4.1), as CMS `KeyTransRecipientInfo` carries it;
|
|
207
|
+
* fields equal to their defaults (SHA-1, MGF1 with SHA-1, an empty label) are
|
|
208
|
+
* omitted.
|
|
209
|
+
*
|
|
210
|
+
* @throws {RsaError} `invalidParameters` for a mask other than MGF1.
|
|
211
|
+
*/
|
|
212
|
+
export function encodeRsaOaepAlgorithm(parameters) {
|
|
213
|
+
const { hash, maskGeneration, label } = parameters;
|
|
214
|
+
if (maskGeneration.name !== "MGF1") {
|
|
215
|
+
throw new RsaError("invalidParameters", "RSAES-OAEP-params can only name MGF1 as the mask generation");
|
|
216
|
+
}
|
|
217
|
+
const contents = derEncode(OAEP_PARAMETERS_, {
|
|
218
|
+
hashAlgorithm: isSha1_(hash) ? undefined : hashIdentifier_(hash),
|
|
219
|
+
maskGenAlgorithm: isSha1_(maskGeneration.hash)
|
|
220
|
+
? undefined
|
|
221
|
+
: mgf1Identifier_(maskGeneration.hash),
|
|
222
|
+
pSourceAlgorithm: label.length === 0
|
|
223
|
+
? undefined
|
|
224
|
+
: {
|
|
225
|
+
algorithm: P_SPECIFIED_,
|
|
226
|
+
parameters: { encoded: derEncode(OCTET_STRING_, label) },
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
return encodeAlgorithmIdentifier_(RSAES_OAEP_, contents);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* The OAEP parameters of a DER `AlgorithmIdentifier` `id-RSAES-OAEP`, read
|
|
233
|
+
* strictly; absent parameters mean every default.
|
|
234
|
+
*
|
|
235
|
+
* @throws {RsaError} `invalidEncoding`; `unsupportedAlgorithm` for another
|
|
236
|
+
* algorithm or a hash this package does not implement.
|
|
237
|
+
*/
|
|
238
|
+
export function decodeRsaOaepAlgorithm(der) {
|
|
239
|
+
const { algorithm, parameters } = decodeAlgorithmIdentifier_(der);
|
|
240
|
+
if (!oidEqual(algorithm, RSAES_OAEP_)) {
|
|
241
|
+
throw new RsaError("unsupportedAlgorithm", `The algorithm ${oidToDotted(algorithm)} is not RSAES-OAEP`);
|
|
242
|
+
}
|
|
243
|
+
return parameters === undefined
|
|
244
|
+
? defaultOaepParameters_()
|
|
245
|
+
: decodeOaepParameters_(parameters);
|
|
246
|
+
}
|
|
247
|
+
/** Whether two PSS parameter sets are the same (hashes compared by OID). */
|
|
248
|
+
export function pssParametersEqual(a, b) {
|
|
249
|
+
return (a.hash.oid === b.hash.oid &&
|
|
250
|
+
maskGenerationEqual(a.maskGeneration, b.maskGeneration) &&
|
|
251
|
+
a.saltLength === b.saltLength);
|
|
252
|
+
}
|
|
253
|
+
/** Whether two mask generation functions are the same. */
|
|
254
|
+
export function maskGenerationEqual(a, b) {
|
|
255
|
+
if (a.name === "MGF1" && b.name === "MGF1") {
|
|
256
|
+
return a.hash.oid === b.hash.oid;
|
|
257
|
+
}
|
|
258
|
+
return a.name === b.name;
|
|
259
|
+
}
|
|
260
|
+
function encodePss_(parameters) {
|
|
261
|
+
const { hash, maskGeneration, saltLength } = parameters;
|
|
262
|
+
checkPssSaltLength(saltLength);
|
|
263
|
+
const shake = shakeIdentifierFor_(parameters);
|
|
264
|
+
if (shake !== undefined) {
|
|
265
|
+
return encodeAlgorithmIdentifier_(shake, undefined);
|
|
266
|
+
}
|
|
267
|
+
if (maskGeneration.name !== "MGF1" ||
|
|
268
|
+
isShake_(hash) ||
|
|
269
|
+
isShake_(maskGeneration.hash)) {
|
|
270
|
+
throw new RsaError("invalidParameters", "RSASSA-PSS-params cannot express these parameters: SHAKE needs RFC 8702's fixed set");
|
|
271
|
+
}
|
|
272
|
+
const contents = derEncode(PSS_PARAMETERS_, {
|
|
273
|
+
hashAlgorithm: isSha1_(hash) ? undefined : hashIdentifier_(hash),
|
|
274
|
+
maskGenAlgorithm: isSha1_(maskGeneration.hash)
|
|
275
|
+
? undefined
|
|
276
|
+
: mgf1Identifier_(maskGeneration.hash),
|
|
277
|
+
saltLength: saltLength === DEFAULT_SALT_LENGTH_ ? undefined : BigInt(saltLength),
|
|
278
|
+
trailerField: undefined,
|
|
279
|
+
});
|
|
280
|
+
return encodeAlgorithmIdentifier_(RSASSA_PSS_, contents);
|
|
281
|
+
}
|
|
282
|
+
/** RFC 8702 §3.1: SHAKE128 or SHAKE256 for hash and mask, salt 32 or 64. */
|
|
283
|
+
function shakeIdentifierFor_(parameters) {
|
|
284
|
+
const { hash, maskGeneration, saltLength } = parameters;
|
|
285
|
+
if (hash.oid === RSA_HASHES.SHAKE128.oid &&
|
|
286
|
+
maskGeneration.name === "SHAKE128" &&
|
|
287
|
+
saltLength === 32) {
|
|
288
|
+
return RSASSA_PSS_SHAKE128_;
|
|
289
|
+
}
|
|
290
|
+
if (hash.oid === RSA_HASHES.SHAKE256.oid &&
|
|
291
|
+
maskGeneration.name === "SHAKE256" &&
|
|
292
|
+
saltLength === 64) {
|
|
293
|
+
return RSASSA_PSS_SHAKE256_;
|
|
294
|
+
}
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
function decodePssIdentifier_(identifier) {
|
|
298
|
+
const { algorithm, parameters } = identifier;
|
|
299
|
+
if (oidEqual(algorithm, RSASSA_PSS_SHAKE128_) ||
|
|
300
|
+
oidEqual(algorithm, RSASSA_PSS_SHAKE256_)) {
|
|
301
|
+
if (parameters !== undefined) {
|
|
302
|
+
throw new RsaError("invalidEncoding", "id-RSASSA-PSS-SHAKE128 and -SHAKE256 take no parameters (RFC 8702 §3.1)");
|
|
303
|
+
}
|
|
304
|
+
const is128 = oidEqual(algorithm, RSASSA_PSS_SHAKE128_);
|
|
305
|
+
return Object.freeze({
|
|
306
|
+
hash: is128 ? RSA_HASHES.SHAKE128 : RSA_HASHES.SHAKE256,
|
|
307
|
+
maskGeneration: Object.freeze({ name: is128 ? "SHAKE128" : "SHAKE256" }),
|
|
308
|
+
saltLength: is128 ? 32 : 64,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
if (!oidEqual(algorithm, RSASSA_PSS_)) {
|
|
312
|
+
throw new RsaError("unsupportedAlgorithm", `The algorithm ${oidToDotted(algorithm)} is not an RSA algorithm this package implements`);
|
|
313
|
+
}
|
|
314
|
+
if (parameters === undefined) {
|
|
315
|
+
throw new RsaError("invalidEncoding", "id-RSASSA-PSS needs its parameters in a signature algorithm (RFC 4055 §3.1)");
|
|
316
|
+
}
|
|
317
|
+
const value = decode_(PSS_PARAMETERS_, contentsOfAny_(parameters), "RSASSA-PSS-params");
|
|
318
|
+
const hash = value.hashAlgorithm === undefined
|
|
319
|
+
? RSA_HASHES["SHA-1"]
|
|
320
|
+
: hashFromIdentifier_(value.hashAlgorithm, true);
|
|
321
|
+
const maskHash = value.maskGenAlgorithm === undefined
|
|
322
|
+
? RSA_HASHES["SHA-1"]
|
|
323
|
+
: mgf1HashFromIdentifier_(value.maskGenAlgorithm);
|
|
324
|
+
const saltLength = value.saltLength === undefined
|
|
325
|
+
? DEFAULT_SALT_LENGTH_
|
|
326
|
+
: nonDefault_(value.saltLength, BigInt(DEFAULT_SALT_LENGTH_), "saltLength");
|
|
327
|
+
if (value.trailerField !== undefined) {
|
|
328
|
+
nonDefault_(value.trailerField, 1n, "trailerField");
|
|
329
|
+
throw new RsaError("unsupportedAlgorithm", "RSASSA-PSS-params names a trailer field other than 0xbc");
|
|
330
|
+
}
|
|
331
|
+
if (saltLength > MAXIMUM_SALT_LENGTH_) {
|
|
332
|
+
throw new RsaError("invalidEncoding", "The PSS salt length is out of range");
|
|
333
|
+
}
|
|
334
|
+
return Object.freeze({
|
|
335
|
+
hash,
|
|
336
|
+
maskGeneration: mgf1(maskHash),
|
|
337
|
+
saltLength: Number(saltLength),
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
function decodeOaepParameters_(parameters) {
|
|
341
|
+
const value = decode_(OAEP_PARAMETERS_, contentsOfAny_(parameters), "RSAES-OAEP-params");
|
|
342
|
+
const hash = value.hashAlgorithm === undefined
|
|
343
|
+
? RSA_HASHES["SHA-1"]
|
|
344
|
+
: hashFromIdentifier_(value.hashAlgorithm, true);
|
|
345
|
+
const maskHash = value.maskGenAlgorithm === undefined
|
|
346
|
+
? RSA_HASHES["SHA-1"]
|
|
347
|
+
: mgf1HashFromIdentifier_(value.maskGenAlgorithm);
|
|
348
|
+
let label = new Uint8Array(0);
|
|
349
|
+
if (value.pSourceAlgorithm !== undefined) {
|
|
350
|
+
const source = value.pSourceAlgorithm;
|
|
351
|
+
if (!oidEqual(source.algorithm, P_SPECIFIED_) ||
|
|
352
|
+
source.parameters === undefined) {
|
|
353
|
+
throw new RsaError("unsupportedAlgorithm", "RSAES-OAEP-params names a label source other than id-pSpecified");
|
|
354
|
+
}
|
|
355
|
+
label = decode_(OCTET_STRING_, encodedOf_(source.parameters), "The OAEP label").slice();
|
|
356
|
+
if (label.length === 0) {
|
|
357
|
+
throw new RsaError("invalidEncoding", "An empty OAEP label must be omitted, as it is the DER default");
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return createOaepParameters(hash, mgf1(maskHash), label);
|
|
361
|
+
}
|
|
362
|
+
function defaultOaepParameters_() {
|
|
363
|
+
return createOaepParameters(RSA_HASHES["SHA-1"], mgf1(RSA_HASHES["SHA-1"]), new Uint8Array(0));
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* The hash of an AlgorithmIdentifier inside PSS or OAEP parameters; with
|
|
367
|
+
* `explicit`, SHA-1, the DEFAULT, is refused.
|
|
368
|
+
*/
|
|
369
|
+
function hashFromIdentifier_(identifier, explicit) {
|
|
370
|
+
const dotted = oidToDotted(identifier.algorithm);
|
|
371
|
+
const hash = Object.values(RSA_HASHES).find((candidate) => candidate.oid === dotted);
|
|
372
|
+
if (hash === undefined || isShake_(hash)) {
|
|
373
|
+
throw new RsaError("unsupportedAlgorithm", `The hash ${dotted} is not one this package implements here`);
|
|
374
|
+
}
|
|
375
|
+
requireNullOrAbsent_(identifier.parameters, hash.name);
|
|
376
|
+
if (explicit && isSha1_(hash)) {
|
|
377
|
+
throw new RsaError("invalidEncoding", "SHA-1 is the DEFAULT and must be omitted in DER");
|
|
378
|
+
}
|
|
379
|
+
return hash;
|
|
380
|
+
}
|
|
381
|
+
function mgf1HashFromIdentifier_(identifier) {
|
|
382
|
+
if (!oidEqual(identifier.algorithm, MGF1_) ||
|
|
383
|
+
identifier.parameters === undefined) {
|
|
384
|
+
throw new RsaError("unsupportedAlgorithm", "The mask generation function is not MGF1 with a hash");
|
|
385
|
+
}
|
|
386
|
+
const inner = decodeAlgorithmIdentifier_(encodedOf_(identifier.parameters));
|
|
387
|
+
const hash = hashFromIdentifier_(inner, false);
|
|
388
|
+
if (isSha1_(hash)) {
|
|
389
|
+
throw new RsaError("invalidEncoding", "MGF1 with SHA-1 is the DEFAULT and must be omitted in DER");
|
|
390
|
+
}
|
|
391
|
+
return hash;
|
|
392
|
+
}
|
|
393
|
+
function hashIdentifier_(hash) {
|
|
394
|
+
if (!isKnownHash_(hash)) {
|
|
395
|
+
throw new RsaError("invalidParameters", "This hash has no object identifier this package knows");
|
|
396
|
+
}
|
|
397
|
+
return {
|
|
398
|
+
algorithm: oidFromDotted(hash.oid),
|
|
399
|
+
parameters: hash.name.startsWith("SHA3-")
|
|
400
|
+
? undefined
|
|
401
|
+
: { encoded: NULL_ENCODING_ },
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
function mgf1Identifier_(hash) {
|
|
405
|
+
return {
|
|
406
|
+
algorithm: MGF1_,
|
|
407
|
+
parameters: {
|
|
408
|
+
encoded: derEncode(ALGORITHM_IDENTIFIER_, hashIdentifier_(hash)),
|
|
409
|
+
},
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
function encodeAlgorithmIdentifier_(algorithm, parameters) {
|
|
413
|
+
return new Uint8Array(derEncode(ALGORITHM_IDENTIFIER_, {
|
|
414
|
+
algorithm,
|
|
415
|
+
parameters: parameters === undefined ? undefined : { encoded: parameters },
|
|
416
|
+
}));
|
|
417
|
+
}
|
|
418
|
+
function decodeAlgorithmIdentifier_(der) {
|
|
419
|
+
return decode_(ALGORITHM_IDENTIFIER_, der, "An AlgorithmIdentifier");
|
|
420
|
+
}
|
|
421
|
+
function decode_(schema, der, what) {
|
|
422
|
+
try {
|
|
423
|
+
return derDecode(schema, der);
|
|
424
|
+
}
|
|
425
|
+
catch (error) {
|
|
426
|
+
if (error instanceof DecodingError || error instanceof EncodingError) {
|
|
427
|
+
throw new RsaError("invalidEncoding", `${what} is not valid DER`, {
|
|
428
|
+
cause: error,
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
throw error;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
/** The bytes of an ANY value, a whole TLV. */
|
|
435
|
+
function encodedOf_(value) {
|
|
436
|
+
return value.encoded ?? new Uint8Array(0);
|
|
437
|
+
}
|
|
438
|
+
/** The ANY value of a PSS or OAEP parameters field, a whole SEQUENCE TLV. */
|
|
439
|
+
function contentsOfAny_(value) {
|
|
440
|
+
return encodedOf_(value);
|
|
441
|
+
}
|
|
442
|
+
/** A present DEFAULT-able INTEGER, refused when equal to its default. */
|
|
443
|
+
function nonDefault_(value, fallback, what) {
|
|
444
|
+
if (value === fallback) {
|
|
445
|
+
throw new RsaError("invalidEncoding", `${what} equals its DEFAULT and must be omitted in DER`);
|
|
446
|
+
}
|
|
447
|
+
if (value < 0n) {
|
|
448
|
+
throw new RsaError("invalidEncoding", `${what} must not be negative`);
|
|
449
|
+
}
|
|
450
|
+
return value;
|
|
451
|
+
}
|
|
452
|
+
function requireNull_(parameters, what) {
|
|
453
|
+
const bytes = parameters === undefined ? undefined : encodedOf_(parameters);
|
|
454
|
+
if (bytes === undefined ||
|
|
455
|
+
bytes.length !== 2 ||
|
|
456
|
+
bytes[0] !== 0x05 ||
|
|
457
|
+
bytes[1] !== 0x00) {
|
|
458
|
+
throw new RsaError("invalidEncoding", `The parameters of ${what} must be NULL`);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
function requireNullOrAbsent_(parameters, what) {
|
|
462
|
+
if (parameters !== undefined) {
|
|
463
|
+
requireNull_(parameters, what);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
function isSha1_(hash) {
|
|
467
|
+
return hash.oid === RSA_HASHES["SHA-1"].oid;
|
|
468
|
+
}
|
|
469
|
+
function isShake_(hash) {
|
|
470
|
+
return (hash.oid === RSA_HASHES.SHAKE128.oid || hash.oid === RSA_HASHES.SHAKE256.oid);
|
|
471
|
+
}
|
|
472
|
+
function isKnownHash_(hash) {
|
|
473
|
+
return Object.values(RSA_HASHES).some((candidate) => candidate.oid === hash.oid && candidate.name === hash.name);
|
|
474
|
+
}
|
|
475
|
+
//# sourceMappingURL=rsa-algorithm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rsa-algorithm.js","sourceRoot":"","sources":["../../src/encoding/rsa-algorithm.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,GAAG,EAGH,SAAS,EACT,UAAU,EACV,aAAa,EAEb,aAAa,EACb,OAAO,EACP,gBAAgB,EAChB,WAAW,EACX,GAAG,EACH,QAAQ,GACR,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAEN,QAAQ,EACR,aAAa,EACb,WAAW,GACX,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACN,IAAI,EACJ,UAAU,GAGV,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAgD3C,yEAAyE;AACzE,MAAM,CAAC,MAAM,wBAAwB,GAAoB,MAAM,CAAC,MAAM,CAAC;IACtE,IAAI,EAAE,eAAe;CACrB,CAAC,CAAC;AAOH,MAAM,eAAe,GAAG,aAAa,CAAC,sBAAsB,CAAC,CAAC;AAC9D,MAAM,WAAW,GAAG,aAAa,CAAC,sBAAsB,CAAC,CAAC;AAC1D,MAAM,KAAK,GAAG,aAAa,CAAC,sBAAsB,CAAC,CAAC;AACpD,MAAM,YAAY,GAAG,aAAa,CAAC,sBAAsB,CAAC,CAAC;AAC3D,MAAM,WAAW,GAAG,aAAa,CAAC,uBAAuB,CAAC,CAAC;AAC3D,MAAM,oBAAoB,GAAG,aAAa,CAAC,oBAAoB,CAAC,CAAC;AACjE,MAAM,oBAAoB,GAAG,aAAa,CAAC,oBAAoB,CAAC,CAA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CACxC,CAAC;IACH,CAAC;AACF,CAAC;AAED,SAAS,oBAAoB,CAC5B,UAAgC,EAChC,IAAY;IAEZ,IAAI,UAAU,KAAK,SAAS,EAAE,CAAC;QAC9B,YAAY,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IAChC,CAAC;AACF,CAAC;AAED,SAAS,OAAO,CAAC,IAAa;IAC7B,OAAO,IAAI,CAAC,GAAG,KAAK,UAAU,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC;AAC7C,CAAC;AAED,SAAS,QAAQ,CAAC,IAAa;IAC9B,OAAO,CACN,IAAI,CAAC,GAAG,KAAK,UAAU,CAAC,QAAQ,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,KAAK,UAAU,CAAC,QAAQ,CAAC,GAAG,CAC5E,CAAC;AACH,CAAC;AAED,SAAS,YAAY,CAAC,IAAa;IAClC,OAAO,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,IAAI,CACpC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,GAAG,KAAK,IAAI,CAAC,GAAG,IAAI,SAAS,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,CACzE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Callable } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* A hash for the RSA schemes: a synchronous one-shot function, its digest
|
|
4
|
+
* length and the object identifier that names it in a PKCS #1 `DigestInfo` and
|
|
5
|
+
* in the `RSASSA-PSS-params` and `RSAES-OAEP-params` of RFC 8017 Appendix A.
|
|
6
|
+
*/
|
|
7
|
+
export interface RsaHash {
|
|
8
|
+
/** The name the standards and the test corpora use, e.g. `"SHA-256"`. */
|
|
9
|
+
readonly name: string;
|
|
10
|
+
/** The dotted object identifier, e.g. `"2.16.840.1.101.3.4.2.1"`. */
|
|
11
|
+
readonly oid: string;
|
|
12
|
+
/** Digest length in bytes. */
|
|
13
|
+
readonly digestLength: number;
|
|
14
|
+
/** The digest of `data`, `digestLength` bytes. */
|
|
15
|
+
readonly hash: Callable<[data: Uint8Array], Uint8Array>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The mask generation function of RSASSA-PSS and RSAES-OAEP: MGF1 over a hash
|
|
19
|
+
* (RFC 8017 §B.2.1), or SHAKE128 or SHAKE256 used directly as the mask
|
|
20
|
+
* generator, as RFC 8702 §3.2 does for RSASSA-PSS with a SHAKE hash.
|
|
21
|
+
*/
|
|
22
|
+
export type RsaMaskGeneration = {
|
|
23
|
+
readonly name: "MGF1";
|
|
24
|
+
readonly hash: RsaHash;
|
|
25
|
+
} | {
|
|
26
|
+
readonly name: "SHAKE128";
|
|
27
|
+
} | {
|
|
28
|
+
readonly name: "SHAKE256";
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* The hashes the RSA schemes take, by the names RFC 8017, FIPS 186-5, Web
|
|
32
|
+
* Crypto and Wycheproof use. SHA-1 is kept for verifying legacy signatures and
|
|
33
|
+
* for MGF1, which RFC 8017 still lists; SHAKE128 and SHAKE256 have the fixed 32.
|
|
34
|
+
* and 64-byte outputs of RFC 8702. SHA-512/224 and SHA-512/256 wait for
|
|
35
|
+
* `@ac-kit/crypto-hash`.
|
|
36
|
+
*/
|
|
37
|
+
export declare const RSA_HASHES: Readonly<{
|
|
38
|
+
"SHA-1": RsaHash;
|
|
39
|
+
"SHA-224": RsaHash;
|
|
40
|
+
"SHA-256": RsaHash;
|
|
41
|
+
"SHA-384": RsaHash;
|
|
42
|
+
"SHA-512": RsaHash;
|
|
43
|
+
"SHA3-224": RsaHash;
|
|
44
|
+
"SHA3-256": RsaHash;
|
|
45
|
+
"SHA3-384": RsaHash;
|
|
46
|
+
"SHA3-512": RsaHash;
|
|
47
|
+
SHAKE128: RsaHash;
|
|
48
|
+
SHAKE256: RsaHash;
|
|
49
|
+
}>;
|
|
50
|
+
/** The name of a hash of {@link RSA_HASHES}. */
|
|
51
|
+
export type RsaHashName = keyof typeof RSA_HASHES;
|
|
52
|
+
/** A hash given by value or by its {@link RSA_HASHES} name. */
|
|
53
|
+
export type RsaHashInput = RsaHash | RsaHashName;
|
|
54
|
+
/** MGF1 over `hash` (RFC 8017 §B.2.1). */
|
|
55
|
+
export declare function mgf1(hash: RsaHash): RsaMaskGeneration;
|
|
56
|
+
/**
|
|
57
|
+
* The mask generation a scheme over `hash` uses by default: SHAKE itself for a
|
|
58
|
+
* SHAKE hash (RFC 8702), MGF1 over the same hash otherwise (RFC 8017 §9.1 and
|
|
59
|
+
* §7.1 recommend the same hash).
|
|
60
|
+
*/
|
|
61
|
+
export declare function defaultMaskGeneration(hash: RsaHash): RsaMaskGeneration;
|
|
62
|
+
/**
|
|
63
|
+
* XORs the `out.length`-byte mask of `seed` into `out` (RFC 8017 §B.2.1, RFC
|
|
64
|
+
* 8702 §3.2). The mask's bytes never exist apart from `out`, except inside the
|
|
65
|
+
* hash.
|
|
66
|
+
*/
|
|
67
|
+
export declare function applyMask(out: Uint8Array, seed: Uint8Array, maskGeneration: RsaMaskGeneration): void;
|
|
68
|
+
//# sourceMappingURL=rsa-hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rsa-hash.d.ts","sourceRoot":"","sources":["../../src/hash/rsa-hash.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAe7C;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACvB,yEAAyE;IACzE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,qEAAqE;IACrE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,8BAA8B;IAC9B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,kDAAkD;IAClD,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,IAAI,EAAE,UAAU,CAAC,EAAE,UAAU,CAAC,CAAC;CACxD;AAED;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,GAC1B;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GACjD;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAA;CAAE,GAC7B;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC;AAWjC;;;;;;GAMG;AACH,eAAO,MAAM,UAAU;;;;;;;;;;;;EAgBrB,CAAC;AAEH,gDAAgD;AAChD,MAAM,MAAM,WAAW,GAAG,MAAM,OAAO,UAAU,CAAC;AAElD,+DAA+D;AAC/D,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,WAAW,CAAC;AAEjD,0CAA0C;AAC1C,wBAAgB,IAAI,CAAC,IAAI,EAAE,OAAO,GAAG,iBAAiB,CAErD;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,OAAO,GAAG,iBAAiB,CAUtE;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CACxB,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,UAAU,EAChB,cAAc,EAAE,iBAAiB,GAC/B,IAAI,CAwCN"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { sha1Js, sha224, sha256Ts, sha384Ts, sha3_224, sha3_256, sha3_384, sha3_512, sha512Ts, shake128, shake256, } from "@ac-kit/crypto-hash";
|
|
2
|
+
function rsaHash_(name, oid, digestLength, hash) {
|
|
3
|
+
return Object.freeze({ name, oid, digestLength, hash });
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* The hashes the RSA schemes take, by the names RFC 8017, FIPS 186-5, Web
|
|
7
|
+
* Crypto and Wycheproof use. SHA-1 is kept for verifying legacy signatures and
|
|
8
|
+
* for MGF1, which RFC 8017 still lists; SHAKE128 and SHAKE256 have the fixed 32.
|
|
9
|
+
* and 64-byte outputs of RFC 8702. SHA-512/224 and SHA-512/256 wait for
|
|
10
|
+
* `@ac-kit/crypto-hash`.
|
|
11
|
+
*/
|
|
12
|
+
export const RSA_HASHES = Object.freeze({
|
|
13
|
+
"SHA-1": rsaHash_("SHA-1", "1.3.14.3.2.26", 20, sha1Js),
|
|
14
|
+
"SHA-224": rsaHash_("SHA-224", "2.16.840.1.101.3.4.2.4", 28, sha224),
|
|
15
|
+
"SHA-256": rsaHash_("SHA-256", "2.16.840.1.101.3.4.2.1", 32, sha256Ts),
|
|
16
|
+
"SHA-384": rsaHash_("SHA-384", "2.16.840.1.101.3.4.2.2", 48, sha384Ts),
|
|
17
|
+
"SHA-512": rsaHash_("SHA-512", "2.16.840.1.101.3.4.2.3", 64, sha512Ts),
|
|
18
|
+
"SHA3-224": rsaHash_("SHA3-224", "2.16.840.1.101.3.4.2.7", 28, sha3_224),
|
|
19
|
+
"SHA3-256": rsaHash_("SHA3-256", "2.16.840.1.101.3.4.2.8", 32, sha3_256),
|
|
20
|
+
"SHA3-384": rsaHash_("SHA3-384", "2.16.840.1.101.3.4.2.9", 48, sha3_384),
|
|
21
|
+
"SHA3-512": rsaHash_("SHA3-512", "2.16.840.1.101.3.4.2.10", 64, sha3_512),
|
|
22
|
+
SHAKE128: rsaHash_("SHAKE128", "2.16.840.1.101.3.4.2.11", 32, (data) => shake128(data, 32)),
|
|
23
|
+
SHAKE256: rsaHash_("SHAKE256", "2.16.840.1.101.3.4.2.12", 64, (data) => shake256(data, 64)),
|
|
24
|
+
});
|
|
25
|
+
/** MGF1 over `hash` (RFC 8017 §B.2.1). */
|
|
26
|
+
export function mgf1(hash) {
|
|
27
|
+
return Object.freeze({ name: "MGF1", hash });
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The mask generation a scheme over `hash` uses by default: SHAKE itself for a
|
|
31
|
+
* SHAKE hash (RFC 8702), MGF1 over the same hash otherwise (RFC 8017 §9.1 and
|
|
32
|
+
* §7.1 recommend the same hash).
|
|
33
|
+
*/
|
|
34
|
+
export function defaultMaskGeneration(hash) {
|
|
35
|
+
if (hash.oid === RSA_HASHES.SHAKE128.oid) {
|
|
36
|
+
return Object.freeze({ name: "SHAKE128" });
|
|
37
|
+
}
|
|
38
|
+
if (hash.oid === RSA_HASHES.SHAKE256.oid) {
|
|
39
|
+
return Object.freeze({ name: "SHAKE256" });
|
|
40
|
+
}
|
|
41
|
+
return mgf1(hash);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* XORs the `out.length`-byte mask of `seed` into `out` (RFC 8017 §B.2.1, RFC
|
|
45
|
+
* 8702 §3.2). The mask's bytes never exist apart from `out`, except inside the
|
|
46
|
+
* hash.
|
|
47
|
+
*/
|
|
48
|
+
export function applyMask(out, seed, maskGeneration) {
|
|
49
|
+
if (maskGeneration.name !== "MGF1") {
|
|
50
|
+
const mask = maskGeneration.name === "SHAKE128"
|
|
51
|
+
? shake128(seed, out.length)
|
|
52
|
+
: shake256(seed, out.length);
|
|
53
|
+
try {
|
|
54
|
+
xorInto_(out, mask, 0);
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
mask.fill(0);
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const { hash } = maskGeneration;
|
|
62
|
+
const input = new Uint8Array(seed.length + 4);
|
|
63
|
+
try {
|
|
64
|
+
input.set(seed);
|
|
65
|
+
for (let counter = 0, offset = 0; offset < out.length; counter++, offset += hash.digestLength) {
|
|
66
|
+
new DataView(input.buffer).setUint32(seed.length, counter);
|
|
67
|
+
const block = hash.hash(input);
|
|
68
|
+
try {
|
|
69
|
+
xorInto_(out, block, offset);
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
block.fill(0);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
input.fill(0);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function xorInto_(out, mask, offset) {
|
|
81
|
+
const end = Math.min(out.length, offset + mask.length);
|
|
82
|
+
for (let index = offset; index < end; index++) {
|
|
83
|
+
out[index] = out[index] ^ mask[index - offset];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=rsa-hash.js.map
|