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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/LICENSE +28 -0
  3. package/README.md +113 -2
  4. package/dist/_bytes.d.ts +18 -0
  5. package/dist/_bytes.d.ts.map +1 -0
  6. package/dist/_bytes.js +42 -0
  7. package/dist/_bytes.js.map +1 -0
  8. package/dist/encoding/_der.d.ts +51 -0
  9. package/dist/encoding/_der.d.ts.map +1 -0
  10. package/dist/encoding/_der.js +173 -0
  11. package/dist/encoding/_der.js.map +1 -0
  12. package/dist/encoding/rsa-algorithm.d.ts +132 -0
  13. package/dist/encoding/rsa-algorithm.d.ts.map +1 -0
  14. package/dist/encoding/rsa-algorithm.js +475 -0
  15. package/dist/encoding/rsa-algorithm.js.map +1 -0
  16. package/dist/hash/rsa-hash.d.ts +68 -0
  17. package/dist/hash/rsa-hash.d.ts.map +1 -0
  18. package/dist/hash/rsa-hash.js +86 -0
  19. package/dist/hash/rsa-hash.js.map +1 -0
  20. package/dist/index.d.ts +9 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +9 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/key/_generate.d.ts +39 -0
  25. package/dist/key/_generate.d.ts.map +1 -0
  26. package/dist/key/_generate.js +124 -0
  27. package/dist/key/_generate.js.map +1 -0
  28. package/dist/key/_key-state.d.ts +53 -0
  29. package/dist/key/_key-state.d.ts.map +1 -0
  30. package/dist/key/_key-state.js +26 -0
  31. package/dist/key/_key-state.js.map +1 -0
  32. package/dist/key/_primality.d.ts +22 -0
  33. package/dist/key/_primality.d.ts.map +1 -0
  34. package/dist/key/_primality.js +156 -0
  35. package/dist/key/_primality.js.map +1 -0
  36. package/dist/key/_primitives.d.ts +27 -0
  37. package/dist/key/_primitives.d.ts.map +1 -0
  38. package/dist/key/_primitives.js +247 -0
  39. package/dist/key/_primitives.js.map +1 -0
  40. package/dist/key/_validate.d.ts +60 -0
  41. package/dist/key/_validate.d.ts.map +1 -0
  42. package/dist/key/_validate.js +328 -0
  43. package/dist/key/_validate.js.map +1 -0
  44. package/dist/key/rsa-private-key.d.ts +180 -0
  45. package/dist/key/rsa-private-key.d.ts.map +1 -0
  46. package/dist/key/rsa-private-key.js +479 -0
  47. package/dist/key/rsa-private-key.js.map +1 -0
  48. package/dist/key/rsa-public-key.d.ts +117 -0
  49. package/dist/key/rsa-public-key.d.ts.map +1 -0
  50. package/dist/key/rsa-public-key.js +196 -0
  51. package/dist/key/rsa-public-key.js.map +1 -0
  52. package/dist/rsa-error.d.ts +54 -0
  53. package/dist/rsa-error.d.ts.map +1 -0
  54. package/dist/rsa-error.js +14 -0
  55. package/dist/rsa-error.js.map +1 -0
  56. package/dist/scheme/_scheme-checks.d.ts +50 -0
  57. package/dist/scheme/_scheme-checks.d.ts.map +1 -0
  58. package/dist/scheme/_scheme-checks.js +162 -0
  59. package/dist/scheme/_scheme-checks.js.map +1 -0
  60. package/dist/scheme/_signature.d.ts +8 -0
  61. package/dist/scheme/_signature.d.ts.map +1 -0
  62. package/dist/scheme/_signature.js +13 -0
  63. package/dist/scheme/_signature.js.map +1 -0
  64. package/dist/scheme/rsaes-oaep.d.ts +75 -0
  65. package/dist/scheme/rsaes-oaep.d.ts.map +1 -0
  66. package/dist/scheme/rsaes-oaep.js +191 -0
  67. package/dist/scheme/rsaes-oaep.js.map +1 -0
  68. package/dist/scheme/rsassa-pkcs1-v1_5.d.ts +73 -0
  69. package/dist/scheme/rsassa-pkcs1-v1_5.d.ts.map +1 -0
  70. package/dist/scheme/rsassa-pkcs1-v1_5.js +133 -0
  71. package/dist/scheme/rsassa-pkcs1-v1_5.js.map +1 -0
  72. package/dist/scheme/rsassa-pss.d.ts +88 -0
  73. package/dist/scheme/rsassa-pss.d.ts.map +1 -0
  74. package/dist/scheme/rsassa-pss.js +175 -0
  75. package/dist/scheme/rsassa-pss.js.map +1 -0
  76. package/package.json +75 -3
@@ -0,0 +1,196 @@
1
+ import { bytesIsEqual } from "@ac-kit/core";
2
+ import { ConstantTimeModulus, INSPECT_CUSTOM } from "@ac-kit/crypto-safe";
3
+ import { bitLengthOf, minimalBytesOf, stripLeadingZeros, unsignedOf, } from "../_bytes.js";
4
+ import { derElement, derUnsignedInteger, enterSequence, readDer, readOctetAlignedBitString, readUnsignedInteger, TAG_BIT_STRING, TAG_SEQUENCE, } from "../encoding/_der.js";
5
+ import { decodeRsaKeyAlgorithm, encodeRsaKeyAlgorithm, RSA_ENCRYPTION_ALGORITHM, } from "../encoding/rsa-algorithm.js";
6
+ import { RsaError } from "../rsa-error.js";
7
+ import { copyKeyAlgorithm } from "../scheme/_scheme-checks.js";
8
+ import { publicStateOf, setPublicState } from "./_key-state.js";
9
+ export { RSA_ENCRYPTION_ALGORITHM } from "../encoding/rsa-algorithm.js";
10
+ /** The smallest modulus this package accepts, in bits. */
11
+ export const RSA_MINIMUM_MODULUS_BITS = 1024;
12
+ /** The largest modulus this package accepts, in bits. */
13
+ export const RSA_MAXIMUM_MODULUS_BITS = 16384;
14
+ let newPublicKey_;
15
+ /**
16
+ * Checks and copies public components, returning their minimal encodings.
17
+ *
18
+ * @throws {RsaError} `unsupportedKeySize` for a modulus outside [1024, 16384]
19
+ * bits; `invalidKey` for an even modulus or an exponent that is even, below 3
20
+ * or not below n.
21
+ */
22
+ export function checkPublicComponents(components) {
23
+ const modulus = stripLeadingZeros(components.modulus).slice();
24
+ const publicExponent = stripLeadingZeros(components.publicExponent).slice();
25
+ const bits = bitLengthOf(modulus);
26
+ if (bits < RSA_MINIMUM_MODULUS_BITS || bits > RSA_MAXIMUM_MODULUS_BITS) {
27
+ throw new RsaError("unsupportedKeySize", `An RSA modulus must have ${RSA_MINIMUM_MODULUS_BITS} to ${RSA_MAXIMUM_MODULUS_BITS} bits`);
28
+ }
29
+ if ((modulus.at(-1) & 1) === 0) {
30
+ throw new RsaError("invalidKey", "An RSA modulus must be odd");
31
+ }
32
+ const e = unsignedOf(publicExponent);
33
+ if (e < 3n || (e & 1n) === 0n || e >= unsignedOf(modulus)) {
34
+ throw new RsaError("invalidKey", "An RSA public exponent must be odd, at least 3 and below n");
35
+ }
36
+ return { modulus, publicExponent };
37
+ }
38
+ /**
39
+ * An RSA public key (n, e) and the algorithm its `AlgorithmIdentifier` allows.
40
+ * Every input is public, so its operations may run in variable time; they run
41
+ * on the same constant-time arithmetic as the private key anyway, since
42
+ * encryption raises a secret message.
43
+ *
44
+ * Keys are frozen and immutable; `util.inspect` shows the modulus size, the
45
+ * exponent and the algorithm.
46
+ */
47
+ export class RsaPublicKey {
48
+ constructor(modulus, publicExponent, algorithm) {
49
+ const kept = copyKeyAlgorithm(algorithm);
50
+ setPublicState(this, {
51
+ modulus,
52
+ modulusBitLength: bitLengthOf(modulus),
53
+ publicExponent,
54
+ ring: new ConstantTimeModulus(modulus),
55
+ algorithm: kept,
56
+ });
57
+ Object.freeze(this);
58
+ }
59
+ /**
60
+ * The key for the integers n and e, copied.
61
+ *
62
+ * @throws {RsaError} `unsupportedKeySize` for a modulus outside [1024, 16384]
63
+ * bits; `invalidKey` for an even modulus or an exponent that is even, below
64
+ * 3 or not below n; `invalidParameters` for an `algorithm` with an unknown
65
+ * name, hash or mask, a PSS salt length that is not an integer in [0,
66
+ * 65535], or an OAEP mask other than MGF1 or label that is not bytes.
67
+ */
68
+ static fromComponents(components, options = {}) {
69
+ const { modulus, publicExponent } = checkPublicComponents(components);
70
+ return newPublicKey_(modulus, publicExponent, options.algorithm ?? RSA_ENCRYPTION_ALGORITHM);
71
+ }
72
+ /**
73
+ * The key of a DER `RSAPublicKey` (RFC 8017 Appendix A.1.1), read strictly.
74
+ *
75
+ * @throws {RsaError} `invalidEncoding` for anything but that DER structure;
76
+ * as {@link fromComponents} for its integers.
77
+ */
78
+ static fromPkcs1(der, options = {}) {
79
+ return RsaPublicKey.fromComponents(readPkcs1PublicKey(der), options);
80
+ }
81
+ /**
82
+ * The key of a DER `SubjectPublicKeyInfo` (RFC 5280 §4.1.2.7) whose algorithm
83
+ * is `rsaEncryption` (RFC 8017 Appendix A.1), `id-RSASSA-PSS` with or without
84
+ * parameters (RFC 4055 §1.2), `id-RSASSA-PSS-SHAKE128` or `-SHAKE256` (RFC
85
+ * 8702 §4) or `id-RSAES-OAEP` (RFC 4055 §4.1), read strictly; the key keeps
86
+ * that algorithm as its {@link algorithm}.
87
+ *
88
+ * @throws {RsaError} `invalidEncoding` for malformed DER;
89
+ * `unsupportedAlgorithm` for another algorithm; as {@link fromComponents}
90
+ * for the integers.
91
+ */
92
+ static fromSpki(der) {
93
+ const { algorithm, key } = readDer(der, "A SubjectPublicKeyInfo", (reader) => {
94
+ const sequence = enterSequence(reader, "A SubjectPublicKeyInfo");
95
+ const algorithmBytes = readAlgorithmElement(sequence, "The key algorithm");
96
+ const keyBytes = readOctetAlignedBitString(sequence, "The subjectPublicKey");
97
+ sequence.finish();
98
+ return { algorithm: algorithmBytes, key: keyBytes };
99
+ });
100
+ return RsaPublicKey.fromComponents(readPkcs1PublicKey(key), {
101
+ algorithm: decodeRsaKeyAlgorithm(algorithm),
102
+ });
103
+ }
104
+ /** Bit length of n. */
105
+ get modulusBitLength() {
106
+ return publicStateOf(this).modulusBitLength;
107
+ }
108
+ /** Byte length k of n, and of every signature and ciphertext. */
109
+ get modulusByteLength() {
110
+ return publicStateOf(this).modulus.length;
111
+ }
112
+ /** What the key may be used for. */
113
+ get algorithm() {
114
+ return publicStateOf(this).algorithm;
115
+ }
116
+ /** Copies of n and e, minimal big-endian. */
117
+ exportComponents() {
118
+ const state = publicStateOf(this);
119
+ return {
120
+ modulus: state.modulus.slice(),
121
+ publicExponent: state.publicExponent.slice(),
122
+ };
123
+ }
124
+ /** The DER `RSAPublicKey` (RFC 8017 Appendix A.1.1). */
125
+ toPkcs1() {
126
+ const state = publicStateOf(this);
127
+ return derElement(TAG_SEQUENCE, [
128
+ derUnsignedInteger(state.modulus),
129
+ derUnsignedInteger(state.publicExponent),
130
+ ]);
131
+ }
132
+ /**
133
+ * The DER `SubjectPublicKeyInfo` of the key under its {@link algorithm}.
134
+ *
135
+ * @throws {RsaError} `invalidParameters` when the algorithm's parameters have
136
+ * no `AlgorithmIdentifier` (see `encodeRsaKeyAlgorithm`).
137
+ */
138
+ toSpki() {
139
+ return derElement(TAG_SEQUENCE, [
140
+ encodeRsaKeyAlgorithm(this.algorithm),
141
+ derElement(TAG_BIT_STRING, [Uint8Array.of(0), this.toPkcs1()]),
142
+ ]);
143
+ }
144
+ /**
145
+ * The same n and e under `algorithm`, for instance to restrict a key to
146
+ * RSASSA-PSS before exporting it.
147
+ *
148
+ * @throws {RsaError} `invalidParameters` as {@link fromComponents} for the
149
+ * algorithm.
150
+ */
151
+ withAlgorithm(algorithm) {
152
+ const state = publicStateOf(this);
153
+ return newPublicKey_(state.modulus, state.publicExponent, algorithm);
154
+ }
155
+ /** Whether `other` has the same n and e (whatever their algorithms). */
156
+ equals(other) {
157
+ const state = publicStateOf(this);
158
+ const otherState = publicStateOf(other);
159
+ return (bytesIsEqual(state.modulus, otherState.modulus) &&
160
+ bytesIsEqual(state.publicExponent, otherState.publicExponent));
161
+ }
162
+ /** `RsaPublicKey { 2048 bits, e = 65537, rsaEncryption }`, for `util.inspect`. */
163
+ [INSPECT_CUSTOM]() {
164
+ const state = publicStateOf(this);
165
+ return `RsaPublicKey { ${state.modulusBitLength} bits, e = ${unsignedOf(state.publicExponent)}, ${state.algorithm.name} }`;
166
+ }
167
+ static {
168
+ newPublicKey_ = (modulus, publicExponent, algorithm) => new RsaPublicKey(modulus, publicExponent, algorithm);
169
+ }
170
+ }
171
+ /** Builds a key from components already checked by {@link checkPublicComponents}. */
172
+ export function createPublicKey(modulus, publicExponent, algorithm) {
173
+ return newPublicKey_(modulus, publicExponent, algorithm);
174
+ }
175
+ /** The minimal encoding of a public exponent given as a number or bigint. */
176
+ export function publicExponentBytes(value) {
177
+ return minimalBytesOf(BigInt(value));
178
+ }
179
+ /** The next element, an `AlgorithmIdentifier` SEQUENCE, as its whole TLV. */
180
+ export function readAlgorithmElement(reader, what) {
181
+ if (reader.atEnd) {
182
+ throw new RsaError("invalidEncoding", `${what} is missing`);
183
+ }
184
+ return reader.readElement();
185
+ }
186
+ /** The integers of a DER `RSAPublicKey`, read strictly. */
187
+ export function readPkcs1PublicKey(der) {
188
+ return readDer(der, "An RSAPublicKey", (reader) => {
189
+ const sequence = enterSequence(reader, "An RSAPublicKey");
190
+ const modulus = readUnsignedInteger(sequence, "The modulus");
191
+ const publicExponent = readUnsignedInteger(sequence, "The public exponent");
192
+ sequence.finish();
193
+ return { modulus, publicExponent };
194
+ });
195
+ }
196
+ //# sourceMappingURL=rsa-public-key.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rsa-public-key.js","sourceRoot":"","sources":["../../src/key/rsa-public-key.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAG1E,OAAO,EACN,WAAW,EACX,cAAc,EACd,iBAAiB,EACjB,UAAU,GACV,MAAM,cAAc,CAAC;AACtB,OAAO,EACN,UAAU,EACV,kBAAkB,EAClB,aAAa,EACb,OAAO,EACP,yBAAyB,EACzB,mBAAmB,EACnB,cAAc,EACd,YAAY,GACZ,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACN,qBAAqB,EACrB,qBAAqB,EACrB,wBAAwB,GAExB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEhE,OAAO,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAExE,0DAA0D;AAC1D,MAAM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IAAI,QAAQ,CAAC,iBAAiB,EAAE,GAAG,IAAI,aAAa,CAAC,CAAC;IAC7D,CAAC;IAED,OAAO,MAAM,CAAC,WAAW,EAAE,CAAC;AAC7B,CAAC;AAED,2DAA2D;AAC3D,MAAM,UAAU,kBAAkB,CAAC,GAAe;IACjD,OAAO,OAAO,CAAC,GAAG,EAAE,iBAAiB,EAAE,CAAC,MAAM,EAAE,EAAE;QACjD,MAAM,QAAQ,GAAG,aAAa,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC;QAC1D,MAAM,OAAO,GAAG,mBAAmB,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC;QAC7D,MAAM,cAAc,GAAG,mBAAmB,CAAC,QAAQ,EAAE,qBAAqB,CAAC,CAAC;QAE5E,QAAQ,CAAC,MAAM,EAAE,CAAC;QAElB,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC;IACpC,CAAC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,54 @@
1
+ /** Why an operation of this package refused its input. */
2
+ export type RsaErrorReason =
3
+ /**
4
+ * A signature, ciphertext, digest or other fixed-size input has the wrong
5
+ * length.
6
+ */
7
+ "invalidLength"
8
+ /**
9
+ * A DER encoding is malformed, not canonical, or not of the expected
10
+ * structure.
11
+ */
12
+ | "invalidEncoding"
13
+ /**
14
+ * Key components are out of range or inconsistent with each other (n is not
15
+ * the product of the primes, e·d ≢ 1, a CRT value does not match, …).
16
+ */
17
+ | "invalidKey"
18
+ /** The modulus has fewer or more bits than this package handles. */
19
+ | "unsupportedKeySize"
20
+ /** Scheme or generation options are out of range or inconsistent. */
21
+ | "invalidParameters"
22
+ /**
23
+ * The key's `AlgorithmIdentifier` restricts it (RFC 4055 §1.2, §4.1) to
24
+ * another scheme, or to other parameters, than the operation asked for.
25
+ */
26
+ | "algorithmMismatch"
27
+ /** An algorithm identifier names an algorithm this package does not implement. */
28
+ | "unsupportedAlgorithm"
29
+ /** The message is too long for the scheme and the key. */
30
+ | "messageTooLong"
31
+ /** The key is too short for the scheme's hash and salt or label hash. */
32
+ | "keyTooShort"
33
+ /**
34
+ * A ciphertext is out of range or, for RSAES-OAEP, does not decrypt: one
35
+ * error, whatever check failed, after the same work.
36
+ */
37
+ | "decryptionError"
38
+ /** No key could be generated within the iteration bounds of FIPS 186-5. */
39
+ | "keyGenerationFailed"
40
+ /** A private-key result failed its own check (a fault while computing). */
41
+ | "privateKeyFault"
42
+ /** The private key has been disposed. */
43
+ | "disposed";
44
+ /**
45
+ * An input this package refuses. The message names the rule broken, never a
46
+ * value: no key, prime or plaintext byte reaches an error.
47
+ */
48
+ export declare class RsaError extends Error {
49
+ readonly name = "RsaError";
50
+ /** Stable, machine-readable cause. */
51
+ readonly reason: RsaErrorReason;
52
+ constructor(reason: RsaErrorReason, message: string, options?: ErrorOptions);
53
+ }
54
+ //# sourceMappingURL=rsa-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rsa-error.d.ts","sourceRoot":"","sources":["../src/rsa-error.ts"],"names":[],"mappings":"AAAA,0DAA0D;AAC1D,MAAM,MAAM,cAAc;AACzB;;;GAGG;AACD,eAAe;AACjB;;;GAGG;GACD,iBAAiB;AACnB;;;GAGG;GACD,YAAY;AACd,oEAAoE;GAClE,oBAAoB;AACtB,qEAAqE;GACnE,mBAAmB;AACrB;;;GAGG;GACD,mBAAmB;AACrB,kFAAkF;GAChF,sBAAsB;AACxB,0DAA0D;GACxD,gBAAgB;AAClB,yEAAyE;GACvE,aAAa;AACf;;;GAGG;GACD,iBAAiB;AACnB,2EAA2E;GACzE,qBAAqB;AACvB,2EAA2E;GACzE,iBAAiB;AACnB,yCAAyC;GACvC,UAAU,CAAC;AAEd;;;GAGG;AACH,qBAAa,QAAS,SAAQ,KAAK;IAClC,SAAkB,IAAI,cAAc;IACpC,sCAAsC;IACtC,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC;IAEhC,YAAY,MAAM,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,EAG1E;CACD"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * An input this package refuses. The message names the rule broken, never a
3
+ * value: no key, prime or plaintext byte reaches an error.
4
+ */
5
+ export class RsaError extends Error {
6
+ name = "RsaError";
7
+ /** Stable, machine-readable cause. */
8
+ reason;
9
+ constructor(reason, message, options) {
10
+ super(message, options);
11
+ this.reason = reason;
12
+ }
13
+ }
14
+ //# sourceMappingURL=rsa-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rsa-error.js","sourceRoot":"","sources":["../src/rsa-error.ts"],"names":[],"mappings":"AA4CA;;;GAGG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IAChB,IAAI,GAAG,UAAU,CAAC;IACpC,sCAAsC;IAC7B,MAAM,CAAiB;IAEhC,YAAY,MAAsB,EAAE,OAAe,EAAE,OAAsB;QAC1E,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACtB,CAAC;CACD"}
@@ -0,0 +1,50 @@
1
+ import { type RsaKeyAlgorithm, type RsaOaepParameters, type RsaPssParameters } from "../encoding/rsa-algorithm.js";
2
+ import { type RsaHash, type RsaHashInput, type RsaMaskGeneration } from "../hash/rsa-hash.js";
3
+ export type { RsaHashInput } from "../hash/rsa-hash.js";
4
+ /**
5
+ * The hash `input` names, checked to be usable: a known name, or an object with
6
+ * a non-empty name and OID, a positive integer digest length and a function.
7
+ * Returns a frozen copy, so that a caller cannot swap it later.
8
+ *
9
+ * @throws {RsaError} `invalidParameters`.
10
+ */
11
+ export declare function resolveHash(input: RsaHashInput): RsaHash;
12
+ /** The mask generation `input` gives, or the default for `hash`, checked. */
13
+ export declare function resolveMaskGeneration(input: RsaMaskGeneration | undefined, hash: RsaHash): RsaMaskGeneration;
14
+ /**
15
+ * A validated, frozen deep copy of a key's `algorithm`: its hash and mask
16
+ * generation resolved as {@link resolveHash} does, a PSS salt length checked, an
17
+ * OAEP label copied and read back as a copy, so that a caller who keeps or gets
18
+ * the object cannot change what the key enforces.
19
+ *
20
+ * @throws {RsaError} `invalidParameters` for an unknown algorithm name or
21
+ * parameters no scheme takes.
22
+ */
23
+ export declare function copyKeyAlgorithm(algorithm: RsaKeyAlgorithm): RsaKeyAlgorithm;
24
+ /** @throws {RsaError} `invalidLength` when `bytes` is not `length` long. */
25
+ export declare function checkLength(bytes: Uint8Array, length: number, what: string): void;
26
+ /**
27
+ * Checks that a key whose algorithm is `algorithm` may sign or verify with
28
+ * RSASSA-PSS under `parameters`: an `rsaEncryption` key may; an `RSASSA-PSS`
29
+ * key may when it names no parameters, or the same hash and mask generation and
30
+ * a salt no longer than `parameters`' (RFC 4055 §3.1).
31
+ *
32
+ * @throws {RsaError} `algorithmMismatch`.
33
+ */
34
+ export declare function requirePssAllowed(algorithm: RsaKeyAlgorithm, parameters: RsaPssParameters): void;
35
+ /**
36
+ * Checks that a key may encrypt or decrypt with RSAES-OAEP under `parameters`
37
+ * and `label`: an `rsaEncryption` key may; an `RSAES-OAEP` key may when it
38
+ * names no parameters or the same ones (RFC 4055 §4.1).
39
+ *
40
+ * @throws {RsaError} `algorithmMismatch`.
41
+ */
42
+ export declare function requireOaepAllowed(algorithm: RsaKeyAlgorithm, parameters: RsaOaepParameters, label: Uint8Array): void;
43
+ /**
44
+ * Checks that a key may be used with a scheme its `AlgorithmIdentifier` does
45
+ * not name (RSASSA-PKCS1-v1_5): only an `rsaEncryption` key may.
46
+ *
47
+ * @throws {RsaError} `algorithmMismatch`.
48
+ */
49
+ export declare function requireUnrestricted(algorithm: RsaKeyAlgorithm): void;
50
+ //# sourceMappingURL=_scheme-checks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_scheme-checks.d.ts","sourceRoot":"","sources":["../../src/scheme/_scheme-checks.ts"],"names":[],"mappings":"AAEA,OAAO,EAKN,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAGN,KAAK,OAAO,EACZ,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,MAAM,qBAAqB,CAAC;AAG7B,YAAY,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CA2BxD;AAED,6EAA6E;AAC7E,wBAAgB,qBAAqB,CACpC,KAAK,EAAE,iBAAiB,GAAG,SAAS,EACpC,IAAI,EAAE,OAAO,GACX,iBAAiB,CAcnB;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,eAAe,GAAG,eAAe,CA2D5E;AAED,4EAA4E;AAC5E,wBAAgB,WAAW,CAC1B,KAAK,EAAE,UAAU,EACjB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,GACV,IAAI,CAIN;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAChC,SAAS,EAAE,eAAe,EAC1B,UAAU,EAAE,gBAAgB,GAC1B,IAAI,CAyBN;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CACjC,SAAS,EAAE,eAAe,EAC1B,UAAU,EAAE,iBAAiB,EAC7B,KAAK,EAAE,UAAU,GACf,IAAI,CAyBN;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,eAAe,GAAG,IAAI,CAIpE"}
@@ -0,0 +1,162 @@
1
+ import { bytesIsEqual } from "@ac-kit/core";
2
+ import { checkPssSaltLength, createOaepParameters, maskGenerationEqual, RSA_ENCRYPTION_ALGORITHM, } from "../encoding/rsa-algorithm.js";
3
+ import { defaultMaskGeneration, RSA_HASHES, } from "../hash/rsa-hash.js";
4
+ import { RsaError } from "../rsa-error.js";
5
+ /**
6
+ * The hash `input` names, checked to be usable: a known name, or an object with
7
+ * a non-empty name and OID, a positive integer digest length and a function.
8
+ * Returns a frozen copy, so that a caller cannot swap it later.
9
+ *
10
+ * @throws {RsaError} `invalidParameters`.
11
+ */
12
+ export function resolveHash(input) {
13
+ if (typeof input === "string") {
14
+ if (!Object.hasOwn(RSA_HASHES, input)) {
15
+ throw new RsaError("invalidParameters", `Unknown hash name ${input}`);
16
+ }
17
+ return RSA_HASHES[input];
18
+ }
19
+ if (typeof input?.hash !== "function" ||
20
+ typeof input.name !== "string" ||
21
+ input.name === "" ||
22
+ typeof input.oid !== "string" ||
23
+ input.oid === "" ||
24
+ !Number.isInteger(input.digestLength) ||
25
+ input.digestLength <= 0) {
26
+ throw new RsaError("invalidParameters", "Not a usable RsaHash");
27
+ }
28
+ return Object.freeze({
29
+ name: input.name,
30
+ oid: input.oid,
31
+ digestLength: input.digestLength,
32
+ hash: input.hash,
33
+ });
34
+ }
35
+ /** The mask generation `input` gives, or the default for `hash`, checked. */
36
+ export function resolveMaskGeneration(input, hash) {
37
+ if (input === undefined) {
38
+ return defaultMaskGeneration(hash);
39
+ }
40
+ if (input.name === "MGF1") {
41
+ return Object.freeze({ name: "MGF1", hash: resolveHash(input.hash) });
42
+ }
43
+ if (input.name === "SHAKE128" || input.name === "SHAKE256") {
44
+ return Object.freeze({ name: input.name });
45
+ }
46
+ throw new RsaError("invalidParameters", "Unknown mask generation function");
47
+ }
48
+ /**
49
+ * A validated, frozen deep copy of a key's `algorithm`: its hash and mask
50
+ * generation resolved as {@link resolveHash} does, a PSS salt length checked, an
51
+ * OAEP label copied and read back as a copy, so that a caller who keeps or gets
52
+ * the object cannot change what the key enforces.
53
+ *
54
+ * @throws {RsaError} `invalidParameters` for an unknown algorithm name or
55
+ * parameters no scheme takes.
56
+ */
57
+ export function copyKeyAlgorithm(algorithm) {
58
+ switch (algorithm.name) {
59
+ case "rsaEncryption": {
60
+ return RSA_ENCRYPTION_ALGORITHM;
61
+ }
62
+ case "RSASSA-PSS": {
63
+ const { name, parameters } = algorithm;
64
+ if (parameters === undefined) {
65
+ return Object.freeze({ name, parameters });
66
+ }
67
+ const hash = resolveHash(parameters.hash);
68
+ const maskGeneration = resolveMaskGeneration(parameters.maskGeneration, hash);
69
+ const { saltLength } = parameters;
70
+ checkPssSaltLength(saltLength);
71
+ return Object.freeze({
72
+ name,
73
+ parameters: Object.freeze({ hash, maskGeneration, saltLength }),
74
+ });
75
+ }
76
+ case "RSAES-OAEP": {
77
+ const { name, parameters } = algorithm;
78
+ if (parameters === undefined) {
79
+ return Object.freeze({ name, parameters });
80
+ }
81
+ const hash = resolveHash(parameters.hash);
82
+ const maskGeneration = resolveMaskGeneration(parameters.maskGeneration, hash);
83
+ const { label } = parameters;
84
+ if (maskGeneration.name !== "MGF1" || !(label instanceof Uint8Array)) {
85
+ throw new RsaError("invalidParameters", "RSAES-OAEP parameters take MGF1 and a Uint8Array label");
86
+ }
87
+ return Object.freeze({
88
+ name,
89
+ parameters: createOaepParameters(hash, maskGeneration, label),
90
+ });
91
+ }
92
+ default: {
93
+ throw new RsaError("invalidParameters", "Unknown RSA key algorithm");
94
+ }
95
+ }
96
+ }
97
+ /** @throws {RsaError} `invalidLength` when `bytes` is not `length` long. */
98
+ export function checkLength(bytes, length, what) {
99
+ if (bytes.length !== length) {
100
+ throw new RsaError("invalidLength", `${what} must be ${length} bytes long`);
101
+ }
102
+ }
103
+ /**
104
+ * Checks that a key whose algorithm is `algorithm` may sign or verify with
105
+ * RSASSA-PSS under `parameters`: an `rsaEncryption` key may; an `RSASSA-PSS`
106
+ * key may when it names no parameters, or the same hash and mask generation and
107
+ * a salt no longer than `parameters`' (RFC 4055 §3.1).
108
+ *
109
+ * @throws {RsaError} `algorithmMismatch`.
110
+ */
111
+ export function requirePssAllowed(algorithm, parameters) {
112
+ if (algorithm.name === "rsaEncryption") {
113
+ return;
114
+ }
115
+ if (algorithm.name !== "RSASSA-PSS") {
116
+ throw mismatch_(algorithm.name);
117
+ }
118
+ const restriction = algorithm.parameters;
119
+ if (restriction !== undefined &&
120
+ (restriction.hash.oid !== parameters.hash.oid ||
121
+ !maskGenerationEqual(restriction.maskGeneration, parameters.maskGeneration) ||
122
+ parameters.saltLength < restriction.saltLength)) {
123
+ throw new RsaError("algorithmMismatch", "The key is restricted to RSASSA-PSS with other parameters");
124
+ }
125
+ }
126
+ /**
127
+ * Checks that a key may encrypt or decrypt with RSAES-OAEP under `parameters`
128
+ * and `label`: an `rsaEncryption` key may; an `RSAES-OAEP` key may when it
129
+ * names no parameters or the same ones (RFC 4055 §4.1).
130
+ *
131
+ * @throws {RsaError} `algorithmMismatch`.
132
+ */
133
+ export function requireOaepAllowed(algorithm, parameters, label) {
134
+ if (algorithm.name === "rsaEncryption") {
135
+ return;
136
+ }
137
+ if (algorithm.name !== "RSAES-OAEP") {
138
+ throw mismatch_(algorithm.name);
139
+ }
140
+ const restriction = algorithm.parameters;
141
+ if (restriction !== undefined &&
142
+ (restriction.hash.oid !== parameters.hash.oid ||
143
+ !maskGenerationEqual(restriction.maskGeneration, parameters.maskGeneration) ||
144
+ !bytesIsEqual(restriction.label, label))) {
145
+ throw new RsaError("algorithmMismatch", "The key is restricted to RSAES-OAEP with other parameters");
146
+ }
147
+ }
148
+ /**
149
+ * Checks that a key may be used with a scheme its `AlgorithmIdentifier` does
150
+ * not name (RSASSA-PKCS1-v1_5): only an `rsaEncryption` key may.
151
+ *
152
+ * @throws {RsaError} `algorithmMismatch`.
153
+ */
154
+ export function requireUnrestricted(algorithm) {
155
+ if (algorithm.name !== "rsaEncryption") {
156
+ throw mismatch_(algorithm.name);
157
+ }
158
+ }
159
+ function mismatch_(name) {
160
+ return new RsaError("algorithmMismatch", `The key is restricted to ${name}`);
161
+ }
162
+ //# sourceMappingURL=_scheme-checks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_scheme-checks.js","sourceRoot":"","sources":["../../src/scheme/_scheme-checks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,EACN,kBAAkB,EAClB,oBAAoB,EACpB,mBAAm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2D,CAC3D,CAAC;IACH,CAAC;AACF,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,SAA0B;IAC7D,IAAI,SAAS,CAAC,IAAI,KAAK,eAAe,EAAE,CAAC;QACxC,MAAM,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IACjC,CAAC;AACF,CAAC;AAED,SAAS,SAAS,CAAC,IAAY;IAC9B,OAAO,IAAI,QAAQ,CAAC,mBAAmB,EAAE,4BAA4B,IAAI,EAAE,CAAC,CAAC;AAC9E,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { PublicKeyState } from "../key/_key-state.js";
2
+ /**
3
+ * RSAVP1 of a signature (RFC 8017 §8.1.2 steps 1 and 2): the `k`-byte message
4
+ * representative s^e mod n, or `undefined` when the signature is not `k` bytes
5
+ * long or not below n — an invalid signature, not an error.
6
+ */
7
+ export declare function publicResultOf(state: PublicKeyState, signature: Uint8Array): Uint8Array<ArrayBuffer> | undefined;
8
+ //# sourceMappingURL=_signature.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_signature.d.ts","sourceRoot":"","sources":["../../src/scheme/_signature.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAG3D;;;;GAIG;AACH,wBAAgB,cAAc,CAC7B,KAAK,EAAE,cAAc,EACrB,SAAS,EAAE,UAAU,GACnB,UAAU,CAAC,WAAW,CAAC,GAAG,SAAS,CAMrC"}
@@ -0,0 +1,13 @@
1
+ import { rsaPublicOperation } from "../key/_primitives.js";
2
+ /**
3
+ * RSAVP1 of a signature (RFC 8017 §8.1.2 steps 1 and 2): the `k`-byte message
4
+ * representative s^e mod n, or `undefined` when the signature is not `k` bytes
5
+ * long or not below n — an invalid signature, not an error.
6
+ */
7
+ export function publicResultOf(state, signature) {
8
+ if (signature.length !== state.modulus.length) {
9
+ return undefined;
10
+ }
11
+ return rsaPublicOperation(state, signature);
12
+ }
13
+ //# sourceMappingURL=_signature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_signature.js","sourceRoot":"","sources":["../../src/scheme/_signature.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAE3D;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAC7B,KAAqB,EACrB,SAAqB;IAErB,IAAI,SAAS,CAAC,MAAM,KAAK,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;QAC/C,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,OAAO,kBAAkB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;AAC7C,CAAC"}
@@ -0,0 +1,75 @@
1
+ import { type RsaOaepParameters } 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 rsaesOaep}. */
7
+ export interface RsaesOaepOptions {
8
+ /** The label hash. */
9
+ readonly hash: RsaHashInput;
10
+ /** The mask generation function. Default MGF1 over `hash`. */
11
+ readonly maskGeneration?: RsaMaskGeneration;
12
+ /** The label L, copied. Default empty. */
13
+ readonly label?: Uint8Array;
14
+ }
15
+ /** Options of {@link RsaesOaepScheme.encrypt} and {@link RsaesOaepScheme.decrypt}. */
16
+ export interface RsaesOaepCallOptions {
17
+ /** The label for this call instead of the scheme's. */
18
+ readonly label?: Uint8Array;
19
+ }
20
+ /** Options of {@link RsaesOaepScheme.encrypt}. */
21
+ export interface RsaesOaepEncryptOptions extends RsaesOaepCallOptions {
22
+ /**
23
+ * The seed, `digestLength` bytes, instead of fresh random bytes: for
24
+ * known-answer tests only. A fixed seed makes encryption deterministic and
25
+ * voids its security.
26
+ */
27
+ readonly seed?: Uint8Array;
28
+ }
29
+ /**
30
+ * RSAES-OAEP (RFC 8017 §7.1) under fixed parameters, made by {@link rsaesOaep}.
31
+ * Decryption is constant time in the plaintext and the padding: every check
32
+ * runs, and a ciphertext that fails any of them throws one `RsaError`
33
+ * `decryptionError`, after the same work (§7.1.2 note).
34
+ */
35
+ export declare class RsaesOaepScheme {
36
+ /** The scheme's frozen parameters; each read of `label` gives a copy. */
37
+ readonly parameters: RsaOaepParameters;
38
+ private constructor();
39
+ /** The longest message `key` takes: k − 2·hLen − 2 bytes (0 or less: none). */
40
+ maximumMessageLength(key: RsaPublicKey): number;
41
+ /**
42
+ * The ciphertext of `message` under `key`, `modulusByteLength` bytes.
43
+ * `message` is copied into the encoding and the copy wiped.
44
+ *
45
+ * @throws {RsaError} `keyTooShort` when k < 2·hLen + 2; `messageTooLong`
46
+ * beyond {@link maximumMessageLength}; `invalidLength` for a `seed` of the
47
+ * wrong length; `algorithmMismatch` for a key restricted otherwise.
48
+ */
49
+ encrypt(key: RsaPublicKey, message: Uint8Array, options?: RsaesOaepEncryptOptions): Uint8Array<ArrayBuffer>;
50
+ /**
51
+ * The plaintext of `ciphertext` under `key`.
52
+ *
53
+ * @throws {RsaError} `decryptionError` for a ciphertext of the wrong length,
54
+ * out of range, or that does not decrypt to a valid encoding with this
55
+ * label — one error for all; `algorithmMismatch` for a key restricted
56
+ * otherwise; `disposed`; `privateKeyFault`.
57
+ */
58
+ decrypt(key: RsaPrivateKey, ciphertext: Uint8Array, options?: RsaesOaepCallOptions): Uint8Array<ArrayBuffer>;
59
+ /**
60
+ * The DER `AlgorithmIdentifier` `id-RSAES-OAEP` of the scheme, for CMS (see
61
+ * `encodeRsaOaepAlgorithm`).
62
+ */
63
+ toAlgorithmIdentifier(): Uint8Array<ArrayBuffer>;
64
+ }
65
+ /**
66
+ * RSAES-OAEP (RFC 8017 §7.1) over `options.hash`, with MGF1 over the same hash
67
+ * unless `maskGeneration` says otherwise and the given label. Parameters
68
+ * decoded from an `AlgorithmIdentifier` (`decodeRsaOaepAlgorithm`) are taken as
69
+ * they are.
70
+ *
71
+ * @throws {RsaError} `invalidParameters` for an unknown hash or a mask other
72
+ * than MGF1.
73
+ */
74
+ export declare function rsaesOaep(options: RsaesOaepOptions | RsaOaepParameters): RsaesOaepScheme;
75
+ //# sourceMappingURL=rsaes-oaep.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rsaes-oaep.d.ts","sourceRoot":"","sources":["../../src/scheme/rsaes-oaep.ts"],"names":[],"mappings":"AAGA,OAAO,EAGN,KAAK,iBAAiB,EACtB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAa,KAAK,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAGxE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAE7D,OAAO,EAIN,KAAK,YAAY,EACjB,MAAM,qBAAqB,CAAC;AAE7B,oCAAoC;AACpC,MAAM,WAAW,gBAAgB;IAChC,sBAAsB;IACtB,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,8DAA8D;IAC9D,QAAQ,CAAC,cAAc,CAAC,EAAE,iBAAiB,CAAC;IAC5C,0CAA0C;IAC1C,QAAQ,CAAC,KAAK,CAAC,EAAE,UAAU,CAAC;CAC5B;AAED,sFAAsF;AACtF,MAAM,WAAW,oBAAoB;IACpC,uDAAuD;IACvD,QAAQ,CAAC,KAAK,CAAC,EAAE,UAAU,CAAC;CAC5B;AAED,kDAAkD;AAClD,MAAM,WAAW,uBAAwB,SAAQ,oBAAoB;IACpE;;;;OAIG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,UAAU,CAAC;CAC3B;AAID;;;;;GAKG;AACH,qBAAa,eAAe;IAC3B,yEAAyE;IACzE,QAAQ,CAAC,UAAU,EAAE,iBAAiB,CAAC;IAEvC,OAAO,eAGN;IAED,+EAA+E;IAC/E,oBAAoB,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM,CAE9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