@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,191 @@
|
|
|
1
|
+
import { getRandomBytes } from "@ac-kit/crypto-random";
|
|
2
|
+
import { constantTimeBytesIsEqual } from "@ac-kit/crypto-safe";
|
|
3
|
+
import { createOaepParameters, encodeRsaOaepAlgorithm, } from "../encoding/rsa-algorithm.js";
|
|
4
|
+
import { applyMask } from "../hash/rsa-hash.js";
|
|
5
|
+
import { livePrivateStateOf, publicStateOf } from "../key/_key-state.js";
|
|
6
|
+
import { rsaPrivateOperation, rsaPublicOperation } from "../key/_primitives.js";
|
|
7
|
+
import { RsaError } from "../rsa-error.js";
|
|
8
|
+
import { requireOaepAllowed, resolveHash, resolveMaskGeneration, } from "./_scheme-checks.js";
|
|
9
|
+
let newScheme_;
|
|
10
|
+
/**
|
|
11
|
+
* RSAES-OAEP (RFC 8017 §7.1) under fixed parameters, made by {@link rsaesOaep}.
|
|
12
|
+
* Decryption is constant time in the plaintext and the padding: every check
|
|
13
|
+
* runs, and a ciphertext that fails any of them throws one `RsaError`
|
|
14
|
+
* `decryptionError`, after the same work (§7.1.2 note).
|
|
15
|
+
*/
|
|
16
|
+
export class RsaesOaepScheme {
|
|
17
|
+
/** The scheme's frozen parameters; each read of `label` gives a copy. */
|
|
18
|
+
parameters;
|
|
19
|
+
constructor(parameters) {
|
|
20
|
+
this.parameters = parameters;
|
|
21
|
+
Object.freeze(this);
|
|
22
|
+
}
|
|
23
|
+
/** The longest message `key` takes: k − 2·hLen − 2 bytes (0 or less: none). */
|
|
24
|
+
maximumMessageLength(key) {
|
|
25
|
+
return key.modulusByteLength - 2 * this.parameters.hash.digestLength - 2;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The ciphertext of `message` under `key`, `modulusByteLength` bytes.
|
|
29
|
+
* `message` is copied into the encoding and the copy wiped.
|
|
30
|
+
*
|
|
31
|
+
* @throws {RsaError} `keyTooShort` when k < 2·hLen + 2; `messageTooLong`
|
|
32
|
+
* beyond {@link maximumMessageLength}; `invalidLength` for a `seed` of the
|
|
33
|
+
* wrong length; `algorithmMismatch` for a key restricted otherwise.
|
|
34
|
+
*/
|
|
35
|
+
encrypt(key, message, options = {}) {
|
|
36
|
+
const state = publicStateOf(key);
|
|
37
|
+
const { hash, maskGeneration } = this.parameters;
|
|
38
|
+
const label = options.label ?? this.parameters.label;
|
|
39
|
+
const hashLength = hash.digestLength;
|
|
40
|
+
const k = state.modulus.length;
|
|
41
|
+
requireOaepAllowed(state.algorithm, this.parameters, label);
|
|
42
|
+
if (k < 2 * hashLength + 2) {
|
|
43
|
+
throw new RsaError("keyTooShort", "The RSA key is too short for RSAES-OAEP with this hash");
|
|
44
|
+
}
|
|
45
|
+
if (message.length > k - 2 * hashLength - 2) {
|
|
46
|
+
throw new RsaError("messageTooLong", "The message is too long for RSAES-OAEP with this key and hash");
|
|
47
|
+
}
|
|
48
|
+
if (options.seed !== undefined && options.seed.length !== hashLength) {
|
|
49
|
+
throw new RsaError("invalidLength", `An OAEP seed must be ${hashLength} bytes long`);
|
|
50
|
+
}
|
|
51
|
+
const encoded = new Uint8Array(k);
|
|
52
|
+
const seed = encoded.subarray(1, 1 + hashLength);
|
|
53
|
+
const db = encoded.subarray(1 + hashLength);
|
|
54
|
+
try {
|
|
55
|
+
// DB = lHash ‖ PS ‖ 0x01 ‖ M.
|
|
56
|
+
db.set(hash.hash(label));
|
|
57
|
+
db[db.length - message.length - 1] = 0x01;
|
|
58
|
+
db.set(message, db.length - message.length);
|
|
59
|
+
if (options.seed === undefined) {
|
|
60
|
+
const random = getRandomBytes(hashLength);
|
|
61
|
+
seed.set(random);
|
|
62
|
+
random.fill(0);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
seed.set(options.seed);
|
|
66
|
+
}
|
|
67
|
+
applyMask(db, seed, maskGeneration);
|
|
68
|
+
applyMask(seed, db, maskGeneration);
|
|
69
|
+
return rsaPublicOperation(state, encoded);
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
encoded.fill(0);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The plaintext of `ciphertext` under `key`.
|
|
77
|
+
*
|
|
78
|
+
* @throws {RsaError} `decryptionError` for a ciphertext of the wrong length,
|
|
79
|
+
* out of range, or that does not decrypt to a valid encoding with this
|
|
80
|
+
* label — one error for all; `algorithmMismatch` for a key restricted
|
|
81
|
+
* otherwise; `disposed`; `privateKeyFault`.
|
|
82
|
+
*/
|
|
83
|
+
decrypt(key, ciphertext, options = {}) {
|
|
84
|
+
const state = livePrivateStateOf(key);
|
|
85
|
+
const publicState = publicStateOf(state.publicKey);
|
|
86
|
+
const { hash, maskGeneration } = this.parameters;
|
|
87
|
+
const label = options.label ?? this.parameters.label;
|
|
88
|
+
const hashLength = hash.digestLength;
|
|
89
|
+
const k = publicState.modulus.length;
|
|
90
|
+
requireOaepAllowed(publicState.algorithm, this.parameters, label);
|
|
91
|
+
// Depends on the key and the hash alone, never on the ciphertext.
|
|
92
|
+
if (k < 2 * hashLength + 2) {
|
|
93
|
+
throw decryptionError_();
|
|
94
|
+
}
|
|
95
|
+
// Every ciphertext becomes a k-byte value below n, zero for one of the
|
|
96
|
+
// wrong length or out of range, so that all run the same work below.
|
|
97
|
+
const input = new Uint8Array(k);
|
|
98
|
+
const element = publicState.ring.create();
|
|
99
|
+
let encoded;
|
|
100
|
+
try {
|
|
101
|
+
const lengthValid = ciphertext.length === k ? 1 : 0;
|
|
102
|
+
if (lengthValid === 1) {
|
|
103
|
+
input.set(ciphertext);
|
|
104
|
+
}
|
|
105
|
+
const inputValid = lengthValid & publicState.ring.fromBytesBe(element, input);
|
|
106
|
+
clearUnless_(input, inputValid);
|
|
107
|
+
const labelHash = hash.hash(label);
|
|
108
|
+
encoded = rsaPrivateOperation(state, input);
|
|
109
|
+
const seed = encoded.subarray(1, 1 + hashLength);
|
|
110
|
+
const db = encoded.subarray(1 + hashLength);
|
|
111
|
+
applyMask(seed, db, maskGeneration);
|
|
112
|
+
applyMask(db, seed, maskGeneration);
|
|
113
|
+
const found = findSeparator_(db, hashLength);
|
|
114
|
+
const valid = inputValid &
|
|
115
|
+
isZero_(encoded[0]) &
|
|
116
|
+
Number(constantTimeBytesIsEqual(db.subarray(0, hashLength), labelHash)) &
|
|
117
|
+
found.valid;
|
|
118
|
+
if (valid !== 1) {
|
|
119
|
+
throw decryptionError_();
|
|
120
|
+
}
|
|
121
|
+
return db.slice(found.index + 1);
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
input.fill(0);
|
|
125
|
+
element.fill(0);
|
|
126
|
+
encoded?.fill(0);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The DER `AlgorithmIdentifier` `id-RSAES-OAEP` of the scheme, for CMS (see
|
|
131
|
+
* `encodeRsaOaepAlgorithm`).
|
|
132
|
+
*/
|
|
133
|
+
toAlgorithmIdentifier() {
|
|
134
|
+
return encodeRsaOaepAlgorithm(this.parameters);
|
|
135
|
+
}
|
|
136
|
+
static {
|
|
137
|
+
newScheme_ = (parameters) => new RsaesOaepScheme(parameters);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The index of the 0x01 that ends PS in `db` (after the `hashLength`-byte label
|
|
142
|
+
* hash), and 1 when only zeros precede it, scanning every byte with no branch
|
|
143
|
+
* on their values.
|
|
144
|
+
*/
|
|
145
|
+
function findSeparator_(db, hashLength) {
|
|
146
|
+
let looking = 1;
|
|
147
|
+
let invalid = 0;
|
|
148
|
+
let index = 0;
|
|
149
|
+
for (let position = hashLength; position < db.length; position++) {
|
|
150
|
+
const byte = db[position];
|
|
151
|
+
const isOne = isZero_(byte ^ 0x01);
|
|
152
|
+
const hit = looking & isOne;
|
|
153
|
+
index |= -hit & position;
|
|
154
|
+
invalid |= looking & (1 - isOne) & (1 - isZero_(byte));
|
|
155
|
+
looking &= 1 - isOne;
|
|
156
|
+
}
|
|
157
|
+
return { valid: (1 - looking) & (1 - invalid), index };
|
|
158
|
+
}
|
|
159
|
+
/** 1 when `byte` is 0, else 0. */
|
|
160
|
+
function isZero_(byte) {
|
|
161
|
+
return ((byte | 0) - 1) >>> 31;
|
|
162
|
+
}
|
|
163
|
+
/** Zeroes `bytes` unless `flag` is 1, with no branch on `flag`. */
|
|
164
|
+
function clearUnless_(bytes, flag) {
|
|
165
|
+
const mask = -flag & 0xff;
|
|
166
|
+
for (let index = 0; index < bytes.length; index++) {
|
|
167
|
+
bytes[index] = bytes[index] & mask;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function decryptionError_() {
|
|
171
|
+
return new RsaError("decryptionError", "RSAES-OAEP decryption error");
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* RSAES-OAEP (RFC 8017 §7.1) over `options.hash`, with MGF1 over the same hash
|
|
175
|
+
* unless `maskGeneration` says otherwise and the given label. Parameters
|
|
176
|
+
* decoded from an `AlgorithmIdentifier` (`decodeRsaOaepAlgorithm`) are taken as
|
|
177
|
+
* they are.
|
|
178
|
+
*
|
|
179
|
+
* @throws {RsaError} `invalidParameters` for an unknown hash or a mask other
|
|
180
|
+
* than MGF1.
|
|
181
|
+
*/
|
|
182
|
+
export function rsaesOaep(options) {
|
|
183
|
+
const hash = resolveHash(options.hash);
|
|
184
|
+
const maskGeneration = resolveMaskGeneration(options.maskGeneration, hash);
|
|
185
|
+
if (maskGeneration.name !== "MGF1") {
|
|
186
|
+
throw new RsaError("invalidParameters", "RSAES-OAEP takes MGF1 as its mask generation function");
|
|
187
|
+
}
|
|
188
|
+
return newScheme_(createOaepParameters(hash, maskGeneration, options.label ?? EMPTY_LABEL_));
|
|
189
|
+
}
|
|
190
|
+
const EMPTY_LABEL_ = new Uint8Array(0);
|
|
191
|
+
//# sourceMappingURL=rsaes-oaep.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rsaes-oaep.js","sourceRoot":"","sources":["../../src/scheme/rsaes-oaep.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAE/D,OAAO,EACN,oBAAoB,EACpB,sBAAsB,GAEtB,MAAM,8BAA8B,CA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Y;IAC5B,OAAO,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC;AAChC,CAAC;AAED,mEAAmE;AACnE,SAAS,YAAY,CAAC,KAAiB,EAAE,IAAY;IACpD,MAAM,IAAI,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;IAE1B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACnD,KAAK,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,KAAK,CAAE,GAAG,IAAI,CAAC;IACrC,CAAC;AACF,CAAC;AAED,SAAS,gBAAgB;IACxB,OAAO,IAAI,QAAQ,CAAC,iBAAiB,EAAE,6BAA6B,CAAC,CAAC;AACvE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,SAAS,CACxB,OAA6C;IAE7C,MAAM,IAAI,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACvC,MAAM,cAAc,GAAG,qBAAqB,CAAC,OAAO,CAAC,cAAc,EAAE,IAAI,CAAC,CAAC;IAE3E,IAAI,cAAc,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;QACpC,MAAM,IAAI,QAAQ,CACjB,mBAAmB,EACnB,uDAAuD,CACvD,CAAC;IACH,CAAC;IAED,OAAO,UAAU,CAChB,oBAAoB,CAAC,IAAI,EAAE,cAAc,EAAE,OAAO,CAAC,KAAK,IAAI,YAAY,CAAC,CACzE,CAAC;AACH,CAAC;AAED,MAAM,YAAY,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { RsaHash } from "../hash/rsa-hash.js";
|
|
2
|
+
import type { RsaPrivateKey } from "../key/rsa-private-key.js";
|
|
3
|
+
import type { RsaPublicKey } from "../key/rsa-public-key.js";
|
|
4
|
+
import { type RsaHashInput } from "./_scheme-checks.js";
|
|
5
|
+
/**
|
|
6
|
+
* RSASSA-PKCS1-v1_5 (RFC 8017 §8.2) with EMSA-PKCS1-v1_5 (§9.2) over one hash,
|
|
7
|
+
* made by {@link rsassaPkcs1V15}. Deterministic. Verification encodes the
|
|
8
|
+
* expected message representative and compares it whole, as §8.2.2 does, so no
|
|
9
|
+
* parsing of the decrypted padding can be fooled (Bleichenbacher's 2006
|
|
10
|
+
* forgery); the `DigestInfo` is DER with NULL hash parameters, the form RFC
|
|
11
|
+
* 8017 §9.2 note 1 lists, and the form without them is refused.
|
|
12
|
+
*/
|
|
13
|
+
export declare class RsassaPkcs1V15Scheme {
|
|
14
|
+
/** The hash. */
|
|
15
|
+
readonly hash: RsaHash;
|
|
16
|
+
private constructor();
|
|
17
|
+
/**
|
|
18
|
+
* The signature of `message` by `key`, `modulusByteLength` bytes.
|
|
19
|
+
*
|
|
20
|
+
* @throws {RsaError} `keyTooShort` when the key cannot hold the `DigestInfo`;
|
|
21
|
+
* `algorithmMismatch` for a key restricted to another scheme; `disposed`;
|
|
22
|
+
* `privateKeyFault`.
|
|
23
|
+
*/
|
|
24
|
+
sign(key: RsaPrivateKey, message: Uint8Array): Uint8Array<ArrayBuffer>;
|
|
25
|
+
/**
|
|
26
|
+
* {@link sign} for a digest computed elsewhere.
|
|
27
|
+
*
|
|
28
|
+
* @throws {RsaError} `invalidLength` when `digest` is not `digestLength`
|
|
29
|
+
* bytes; as {@link sign}.
|
|
30
|
+
*/
|
|
31
|
+
signPrehashed(key: RsaPrivateKey, digest: Uint8Array): Uint8Array<ArrayBuffer>;
|
|
32
|
+
/**
|
|
33
|
+
* Whether `signature` is a valid signature of `message` by `key`. Variable
|
|
34
|
+
* time: every input is public.
|
|
35
|
+
*
|
|
36
|
+
* @throws {RsaError} `algorithmMismatch` for a key restricted to another
|
|
37
|
+
* scheme.
|
|
38
|
+
*/
|
|
39
|
+
verify(key: RsaPublicKey, message: Uint8Array, signature: Uint8Array): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* {@link verify} for a digest computed elsewhere.
|
|
42
|
+
*
|
|
43
|
+
* @throws {RsaError} `invalidLength` when `digest` is not `digestLength`
|
|
44
|
+
* bytes; as {@link verify}.
|
|
45
|
+
*/
|
|
46
|
+
verifyPrehashed(key: RsaPublicKey, digest: Uint8Array, signature: Uint8Array): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* The DER `AlgorithmIdentifier` of the scheme (`sha256WithRSAEncryption`, …),
|
|
49
|
+
* for X.509 and CMS.
|
|
50
|
+
*
|
|
51
|
+
* @throws {RsaError} `invalidParameters` for a hash with no such identifier
|
|
52
|
+
* (SHAKE, a custom hash).
|
|
53
|
+
*/
|
|
54
|
+
toAlgorithmIdentifier(): Uint8Array<ArrayBuffer>;
|
|
55
|
+
private signDigest_;
|
|
56
|
+
private verifyDigest_;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* EMSA-PKCS1-v1_5 (RFC 8017 §9.2): `0x00 ‖ 0x01 ‖ 0xff… ‖ 0x00 ‖ DigestInfo`
|
|
60
|
+
* over `length` bytes, the DigestInfo DER with NULL hash parameters.
|
|
61
|
+
*
|
|
62
|
+
* @throws {RsaError} `keyTooShort` when `length` < tLen + 11.
|
|
63
|
+
*/
|
|
64
|
+
export declare function encodeEmsaPkcs1V15(hash: RsaHash, digest: Uint8Array, length: number): Uint8Array<ArrayBuffer>;
|
|
65
|
+
/**
|
|
66
|
+
* RSASSA-PKCS1-v1_5 (RFC 8017 §8.2) over `hash`. Prefer RSASSA-PSS for new
|
|
67
|
+
* protocols; this scheme remains what most X.509 certificates, JWS `RS256` and
|
|
68
|
+
* TLS 1.2 use.
|
|
69
|
+
*
|
|
70
|
+
* @throws {RsaError} `invalidParameters` for an unknown hash.
|
|
71
|
+
*/
|
|
72
|
+
export declare function rsassaPkcs1V15(hash: RsaHashInput): RsassaPkcs1V15Scheme;
|
|
73
|
+
//# sourceMappingURL=rsassa-pkcs1-v1_5.d.ts.map
|
|
@@ -0,0 +1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,IAAI,EAAE,YAAY,GAAG,oBAAoB,CAEvE"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { bytesIsEqual } from "@ac-kit/core";
|
|
2
|
+
import { encodeDigestInfo, encodeRsaSignatureAlgorithm, } from "../encoding/rsa-algorithm.js";
|
|
3
|
+
import { livePrivateStateOf, publicStateOf } from "../key/_key-state.js";
|
|
4
|
+
import { rsaPrivateOperation } from "../key/_primitives.js";
|
|
5
|
+
import { RsaError } from "../rsa-error.js";
|
|
6
|
+
import { checkLength, requireUnrestricted, resolveHash, } from "./_scheme-checks.js";
|
|
7
|
+
import { publicResultOf } from "./_signature.js";
|
|
8
|
+
let newScheme_;
|
|
9
|
+
/**
|
|
10
|
+
* RSASSA-PKCS1-v1_5 (RFC 8017 §8.2) with EMSA-PKCS1-v1_5 (§9.2) over one hash,
|
|
11
|
+
* made by {@link rsassaPkcs1V15}. Deterministic. Verification encodes the
|
|
12
|
+
* expected message representative and compares it whole, as §8.2.2 does, so no
|
|
13
|
+
* parsing of the decrypted padding can be fooled (Bleichenbacher's 2006
|
|
14
|
+
* forgery); the `DigestInfo` is DER with NULL hash parameters, the form RFC
|
|
15
|
+
* 8017 §9.2 note 1 lists, and the form without them is refused.
|
|
16
|
+
*/
|
|
17
|
+
export class RsassaPkcs1V15Scheme {
|
|
18
|
+
/** The hash. */
|
|
19
|
+
hash;
|
|
20
|
+
constructor(hash) {
|
|
21
|
+
this.hash = hash;
|
|
22
|
+
Object.freeze(this);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The signature of `message` by `key`, `modulusByteLength` bytes.
|
|
26
|
+
*
|
|
27
|
+
* @throws {RsaError} `keyTooShort` when the key cannot hold the `DigestInfo`;
|
|
28
|
+
* `algorithmMismatch` for a key restricted to another scheme; `disposed`;
|
|
29
|
+
* `privateKeyFault`.
|
|
30
|
+
*/
|
|
31
|
+
sign(key, message) {
|
|
32
|
+
return this.signDigest_(key, this.hash.hash(message));
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* {@link sign} for a digest computed elsewhere.
|
|
36
|
+
*
|
|
37
|
+
* @throws {RsaError} `invalidLength` when `digest` is not `digestLength`
|
|
38
|
+
* bytes; as {@link sign}.
|
|
39
|
+
*/
|
|
40
|
+
signPrehashed(key, digest) {
|
|
41
|
+
checkLength(digest, this.hash.digestLength, "A PKCS #1 v1.5 digest");
|
|
42
|
+
return this.signDigest_(key, digest);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Whether `signature` is a valid signature of `message` by `key`. Variable
|
|
46
|
+
* time: every input is public.
|
|
47
|
+
*
|
|
48
|
+
* @throws {RsaError} `algorithmMismatch` for a key restricted to another
|
|
49
|
+
* scheme.
|
|
50
|
+
*/
|
|
51
|
+
verify(key, message, signature) {
|
|
52
|
+
return this.verifyDigest_(key, this.hash.hash(message), signature);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* {@link verify} for a digest computed elsewhere.
|
|
56
|
+
*
|
|
57
|
+
* @throws {RsaError} `invalidLength` when `digest` is not `digestLength`
|
|
58
|
+
* bytes; as {@link verify}.
|
|
59
|
+
*/
|
|
60
|
+
verifyPrehashed(key, digest, signature) {
|
|
61
|
+
checkLength(digest, this.hash.digestLength, "A PKCS #1 v1.5 digest");
|
|
62
|
+
return this.verifyDigest_(key, digest, signature);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The DER `AlgorithmIdentifier` of the scheme (`sha256WithRSAEncryption`, …),
|
|
66
|
+
* for X.509 and CMS.
|
|
67
|
+
*
|
|
68
|
+
* @throws {RsaError} `invalidParameters` for a hash with no such identifier
|
|
69
|
+
* (SHAKE, a custom hash).
|
|
70
|
+
*/
|
|
71
|
+
toAlgorithmIdentifier() {
|
|
72
|
+
return encodeRsaSignatureAlgorithm({
|
|
73
|
+
name: "RSASSA-PKCS1-v1_5",
|
|
74
|
+
hash: this.hash,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
signDigest_(key, digest) {
|
|
78
|
+
const state = livePrivateStateOf(key);
|
|
79
|
+
const publicState = publicStateOf(state.publicKey);
|
|
80
|
+
requireUnrestricted(publicState.algorithm);
|
|
81
|
+
return rsaPrivateOperation(state, encodeEmsaPkcs1V15(this.hash, digest, publicState.modulus.length));
|
|
82
|
+
}
|
|
83
|
+
verifyDigest_(key, digest, signature) {
|
|
84
|
+
const publicState = publicStateOf(key);
|
|
85
|
+
requireUnrestricted(publicState.algorithm);
|
|
86
|
+
const representative = publicResultOf(publicState, signature);
|
|
87
|
+
if (representative === undefined) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
let expected;
|
|
91
|
+
try {
|
|
92
|
+
expected = encodeEmsaPkcs1V15(this.hash, digest, publicState.modulus.length);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
if (error instanceof RsaError && error.reason === "keyTooShort") {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
return bytesIsEqual(representative, expected);
|
|
101
|
+
}
|
|
102
|
+
static {
|
|
103
|
+
newScheme_ = (hash) => new RsassaPkcs1V15Scheme(hash);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* EMSA-PKCS1-v1_5 (RFC 8017 §9.2): `0x00 ‖ 0x01 ‖ 0xff… ‖ 0x00 ‖ DigestInfo`
|
|
108
|
+
* over `length` bytes, the DigestInfo DER with NULL hash parameters.
|
|
109
|
+
*
|
|
110
|
+
* @throws {RsaError} `keyTooShort` when `length` < tLen + 11.
|
|
111
|
+
*/
|
|
112
|
+
export function encodeEmsaPkcs1V15(hash, digest, length) {
|
|
113
|
+
const digestInfo = encodeDigestInfo(hash, digest);
|
|
114
|
+
if (length < digestInfo.length + 11) {
|
|
115
|
+
throw new RsaError("keyTooShort", "The RSA key is too short for this hash's DigestInfo");
|
|
116
|
+
}
|
|
117
|
+
const encoded = new Uint8Array(length);
|
|
118
|
+
encoded[1] = 0x01;
|
|
119
|
+
encoded.fill(0xff, 2, length - digestInfo.length - 1);
|
|
120
|
+
encoded.set(digestInfo, length - digestInfo.length);
|
|
121
|
+
return encoded;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* RSASSA-PKCS1-v1_5 (RFC 8017 §8.2) over `hash`. Prefer RSASSA-PSS for new
|
|
125
|
+
* protocols; this scheme remains what most X.509 certificates, JWS `RS256` and
|
|
126
|
+
* TLS 1.2 use.
|
|
127
|
+
*
|
|
128
|
+
* @throws {RsaError} `invalidParameters` for an unknown hash.
|
|
129
|
+
*/
|
|
130
|
+
export function rsassaPkcs1V15(hash) {
|
|
131
|
+
return newScheme_(resolveHash(hash));
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=rsassa-pkcs1-v1_5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rsassa-pkcs1-v1_5.js","sourceRoot":"","sources":["../../src/scheme/rsassa-pkcs1-v1_5.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EACN,gBAAgB,EAChB,2BAA2B,GAC3B,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAG5D,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EACN,WAAW,EACX,mBAAmB,EACnB,WAAW,GAEX,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEjD,IAAI,UAAmD,CAAC;AAExD;;;;;;;GAOG;AACH,MAAM,OAAO,oBAAoB;IAChC,gBAAgB;IACP,IAAI,CAAU;IAEvB,YAAoB,IAAa;QAChC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC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CACjB,aAAa,EACb,qDAAqD,CACrD,CAAC;IACH,CAAC;IAED,MAAM,OAAO,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IAEvC,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IAClB,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,EAAE,MAAM,GAAG,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IACtD,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;IAEpD,OAAO,OAAO,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,IAAkB;IAChD,OAAO,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { type RsaPssParameters } from "../encoding/rsa-algorithm.js";
|
|
2
|
+
import { type RsaMaskGeneration } from "../hash/rsa-hash.js";
|
|
3
|
+
import type { RsaPrivateKey } from "../key/rsa-private-key.js";
|
|
4
|
+
import type { RsaPublicKey } from "../key/rsa-public-key.js";
|
|
5
|
+
import { type RsaHashInput } from "./_scheme-checks.js";
|
|
6
|
+
/** Options of {@link rsassaPss}. */
|
|
7
|
+
export interface RsassaPssOptions {
|
|
8
|
+
/** The message hash. */
|
|
9
|
+
readonly hash: RsaHashInput;
|
|
10
|
+
/**
|
|
11
|
+
* The mask generation function. Default: SHAKE itself for a SHAKE hash (RFC
|
|
12
|
+
* 8702), MGF1 over `hash` otherwise.
|
|
13
|
+
*/
|
|
14
|
+
readonly maskGeneration?: RsaMaskGeneration;
|
|
15
|
+
/** Salt length in bytes. Default the digest length (RFC 8017 §9.1 note 4). */
|
|
16
|
+
readonly saltLength?: number;
|
|
17
|
+
}
|
|
18
|
+
/** Options of {@link RsassaPssScheme.sign}. */
|
|
19
|
+
export interface RsassaPssSignOptions {
|
|
20
|
+
/**
|
|
21
|
+
* The salt, `saltLength` bytes, instead of fresh random bytes: for
|
|
22
|
+
* known-answer tests. A fixed salt makes the scheme deterministic, which RFC
|
|
23
|
+
* 8017 allows but which gives up its tighter security proof.
|
|
24
|
+
*/
|
|
25
|
+
readonly salt?: Uint8Array;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* RSASSA-PSS (RFC 8017 §8.1) with EMSA-PSS (§9.1) under fixed parameters, made
|
|
29
|
+
* by {@link rsassaPss}. Signing draws its salt from `crypto.getRandomValues`;
|
|
30
|
+
* verification expects exactly `saltLength` bytes of salt.
|
|
31
|
+
*/
|
|
32
|
+
export declare class RsassaPssScheme {
|
|
33
|
+
/** The scheme's frozen parameters. */
|
|
34
|
+
readonly parameters: RsaPssParameters;
|
|
35
|
+
private constructor();
|
|
36
|
+
/**
|
|
37
|
+
* The signature of `message` by `key`, `modulusByteLength` bytes.
|
|
38
|
+
*
|
|
39
|
+
* @throws {RsaError} `keyTooShort` when the key cannot hold the digest and
|
|
40
|
+
* salt; `algorithmMismatch` when the key is restricted to other parameters;
|
|
41
|
+
* `invalidLength` for a `salt` of the wrong length; `disposed`;
|
|
42
|
+
* `privateKeyFault`.
|
|
43
|
+
*/
|
|
44
|
+
sign(key: RsaPrivateKey, message: Uint8Array, options?: RsassaPssSignOptions): Uint8Array<ArrayBuffer>;
|
|
45
|
+
/**
|
|
46
|
+
* {@link sign} for a digest of the message under the scheme's hash, computed
|
|
47
|
+
* elsewhere.
|
|
48
|
+
*
|
|
49
|
+
* @throws {RsaError} `invalidLength` when `digest` is not `digestLength`
|
|
50
|
+
* bytes; as {@link sign}.
|
|
51
|
+
*/
|
|
52
|
+
signPrehashed(key: RsaPrivateKey, digest: Uint8Array, options?: RsassaPssSignOptions): Uint8Array<ArrayBuffer>;
|
|
53
|
+
/**
|
|
54
|
+
* Whether `signature` is a valid signature of `message` by `key`. A signature
|
|
55
|
+
* of the wrong length or out of range is simply invalid. Variable time: every
|
|
56
|
+
* input is public.
|
|
57
|
+
*
|
|
58
|
+
* @throws {RsaError} `algorithmMismatch` when the key is restricted to other
|
|
59
|
+
* parameters.
|
|
60
|
+
*/
|
|
61
|
+
verify(key: RsaPublicKey, message: Uint8Array, signature: Uint8Array): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* {@link verify} for a digest computed elsewhere.
|
|
64
|
+
*
|
|
65
|
+
* @throws {RsaError} `invalidLength` when `digest` is not `digestLength`
|
|
66
|
+
* bytes; as {@link verify}.
|
|
67
|
+
*/
|
|
68
|
+
verifyPrehashed(key: RsaPublicKey, digest: Uint8Array, signature: Uint8Array): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* The DER `AlgorithmIdentifier` of the scheme, for X.509 and CMS (see
|
|
71
|
+
* `encodeRsaSignatureAlgorithm`).
|
|
72
|
+
*/
|
|
73
|
+
toAlgorithmIdentifier(): Uint8Array<ArrayBuffer>;
|
|
74
|
+
private signDigest_;
|
|
75
|
+
private verifyDigest_;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* RSASSA-PSS (RFC 8017 §8.1) over `options.hash`, with MGF1 over the same hash
|
|
79
|
+
* (or SHAKE for a SHAKE hash, RFC 8702) and a salt as long as the digest unless
|
|
80
|
+
* the options say otherwise. Parameters decoded from an `AlgorithmIdentifier`
|
|
81
|
+
* (`decodeRsaSignatureAlgorithm`, a key's `algorithm.parameters`) are taken as
|
|
82
|
+
* they are.
|
|
83
|
+
*
|
|
84
|
+
* @throws {RsaError} `invalidParameters` for an unknown hash or mask, or a salt
|
|
85
|
+
* length that is not an integer in [0, 65535].
|
|
86
|
+
*/
|
|
87
|
+
export declare function rsassaPss(options: RsassaPssOptions | RsaPssParameters): RsassaPssScheme;
|
|
88
|
+
//# sourceMappingURL=rsassa-pss.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rsassa-pss.d.ts","sourceRoot":"","sources":["../../src/scheme/rsassa-pss.ts"],"names":[],"mappings":"AAGA,OAAO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