nish 0.42.0 → 0.45.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/README.md +11 -5
- package/docs/AI.md +287 -129
- package/package.json +9 -9
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/skills/writing-nish/SKILL.md +12 -4
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/crypto/rsa.ts
CHANGED
|
@@ -64,6 +64,7 @@
|
|
|
64
64
|
* functions share the importing program's flat symbol namespace
|
|
65
65
|
* (`docs/wp26-stdlib.md` §3e).
|
|
66
66
|
*/
|
|
67
|
+
import { timingSafeEqual } from "nish/crypto/ct"
|
|
67
68
|
import { sha256 } from "nish/crypto/sha256"
|
|
68
69
|
import { sha384, sha512 } from "nish/crypto/sha512"
|
|
69
70
|
|
|
@@ -114,112 +115,106 @@ export class RsaPublicKey {
|
|
|
114
115
|
// The key's DER (X.690 §8.1, §8.3, §10.1)
|
|
115
116
|
// ---------------------------------------------------------------------------
|
|
116
117
|
|
|
118
|
+
/** Where an element's contents lie in its DER: `der[start .. end)`. */
|
|
119
|
+
class RsaDerSpan {
|
|
120
|
+
/** The first byte of the contents. */
|
|
121
|
+
start: i32
|
|
122
|
+
/** One past the last byte of the contents. */
|
|
123
|
+
end: i32
|
|
124
|
+
|
|
125
|
+
constructor(start: i32, end: i32) {
|
|
126
|
+
this.start = start
|
|
127
|
+
this.end = end
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
117
131
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* key's DER is about a kilobyte.
|
|
132
|
+
* The contents of the element at `der[at]`, when it has the identifier `tag`
|
|
133
|
+
* and a definite length in its shortest form that fits before `end`; `null`
|
|
134
|
+
* otherwise, and for an `at` or `end` outside `der`. Lengths of more than two
|
|
135
|
+
* octets are refused: the largest key's DER is about a kilobyte.
|
|
123
136
|
*/
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
137
|
+
const rsaDerElement = (der: u8[], at: i32, end: i32, tag: i32): RsaDerSpan | null => {
|
|
138
|
+
const total: i32 = toI32(der.length)
|
|
139
|
+
if (end < 2 || end > total || at < 0 || at > end - 2 || at > total - 2 || toI32(der[at]) !== tag) {
|
|
140
|
+
return null
|
|
127
141
|
}
|
|
128
142
|
const first: i32 = toI32(der[at + 1])
|
|
129
143
|
let start: i32 = at + 2
|
|
130
144
|
let length: i32 = first
|
|
131
145
|
if (first === 0x81) {
|
|
132
|
-
if (at
|
|
133
|
-
return
|
|
146
|
+
if (at > end - 3 || at > total - 3) {
|
|
147
|
+
return null
|
|
134
148
|
}
|
|
135
149
|
length = toI32(der[at + 2])
|
|
136
150
|
if (length < 0x80) {
|
|
137
|
-
return
|
|
151
|
+
return null
|
|
138
152
|
}
|
|
139
153
|
start = at + 3
|
|
140
154
|
} else if (first === 0x82) {
|
|
141
|
-
if (at
|
|
142
|
-
return
|
|
155
|
+
if (at > end - 4 || at > total - 4) {
|
|
156
|
+
return null
|
|
143
157
|
}
|
|
144
158
|
length = (toI32(der[at + 2]) << 8) | toI32(der[at + 3])
|
|
145
159
|
if (length < 0x100) {
|
|
146
|
-
return
|
|
160
|
+
return null
|
|
147
161
|
}
|
|
148
162
|
start = at + 4
|
|
149
163
|
} else if (first >= 0x80) {
|
|
150
|
-
return
|
|
164
|
+
return null
|
|
151
165
|
}
|
|
152
166
|
if (length > end - start) {
|
|
153
|
-
return
|
|
154
|
-
}
|
|
155
|
-
return start
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/** The contents length of the element `rsaDerContents` found at `der[at]`. */
|
|
159
|
-
const rsaDerLength = (der: u8[], at: i32): i32 => {
|
|
160
|
-
const first: i32 = toI32(der[at + 1])
|
|
161
|
-
if (first === 0x81) {
|
|
162
|
-
return toI32(der[at + 2])
|
|
163
|
-
}
|
|
164
|
-
if (first === 0x82) {
|
|
165
|
-
return (toI32(der[at + 2]) << 8) | toI32(der[at + 3])
|
|
167
|
+
return null
|
|
166
168
|
}
|
|
167
|
-
|
|
169
|
+
// In i64, as the prover cannot add two i32s it only bounds apart; the check
|
|
170
|
+
// above keeps the sum at most `end`.
|
|
171
|
+
return new RsaDerSpan(start, toI32(toI64(start) + toI64(length)))
|
|
168
172
|
}
|
|
169
173
|
|
|
170
174
|
/**
|
|
171
175
|
* The magnitude of the positive INTEGER whose contents are `der[start ..
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
176
|
+
* end)`, big-endian with no leading zero byte; `null` for an empty one, a
|
|
177
|
+
* negative one, one not in its shortest form (a leading 0x00 that a clear top
|
|
178
|
+
* bit after it did not need), and a range outside `der`.
|
|
175
179
|
*/
|
|
176
|
-
const rsaDerUnsigned = (der: u8[], start: i32,
|
|
177
|
-
const
|
|
178
|
-
if (start < 0 ||
|
|
180
|
+
const rsaDerUnsigned = (der: u8[], start: i32, end: i32): u8[] | null => {
|
|
181
|
+
const total: i32 = toI32(der.length)
|
|
182
|
+
if (start < 0 || start >= end || start >= total || end > total || toI32(der[start]) >= 0x80) {
|
|
179
183
|
return null
|
|
180
184
|
}
|
|
181
185
|
let from: i32 = start
|
|
182
186
|
if (toI32(der[start]) === 0) {
|
|
183
|
-
|
|
187
|
+
from = start + 1
|
|
188
|
+
if (from >= end || from >= total || toI32(der[from]) < 0x80) {
|
|
184
189
|
return null
|
|
185
190
|
}
|
|
186
|
-
from = start + 1
|
|
187
191
|
}
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
out[i - from] = der[i]
|
|
191
|
-
}
|
|
192
|
-
return out
|
|
192
|
+
const first: i32 = from
|
|
193
|
+
return der.filter((_b: u8, k: number): boolean => toI32(k) >= first && toI32(k) < end)
|
|
193
194
|
}
|
|
194
195
|
|
|
195
|
-
/**
|
|
196
|
+
/**
|
|
197
|
+
* The number of significant bits in the big-endian `v`, which has no leading
|
|
198
|
+
* zero byte; -1 for one of 2^28 bytes or more, whose count an `i32` might
|
|
199
|
+
* not hold.
|
|
200
|
+
*/
|
|
196
201
|
const rsaBitLength = (v: u8[]): i32 => {
|
|
197
|
-
|
|
198
|
-
if (length === 0) {
|
|
202
|
+
if (toI32(v.length) < 1) {
|
|
199
203
|
return 0
|
|
200
204
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
bits += 1
|
|
205
|
-
top = top >> 1
|
|
206
|
-
}
|
|
207
|
-
return bits
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/** Whether the big-endian `a` is below `b`, both without a leading zero byte. */
|
|
211
|
-
const rsaBytesBelow = (a: u8[], b: u8[]): boolean => {
|
|
212
|
-
const aLength: i32 = toI32(a.length)
|
|
213
|
-
const bLength: i32 = toI32(b.length)
|
|
214
|
-
if (aLength !== bLength) {
|
|
215
|
-
return aLength < bLength
|
|
205
|
+
const length: i32 = toI32(v.length)
|
|
206
|
+
if (length > 0x0fffffff) {
|
|
207
|
+
return -1
|
|
216
208
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
209
|
+
const top: i32 = toI32(v[0])
|
|
210
|
+
let topBits: i32 = 0
|
|
211
|
+
for (let b: i32 = 0; b < 8; b++) {
|
|
212
|
+
if (top >> b !== 0) {
|
|
213
|
+
topBits = b + 1
|
|
220
214
|
}
|
|
221
215
|
}
|
|
222
|
-
|
|
216
|
+
// At most (2^28 - 2) 8 + 8, below 2^31, so the u32 sum is the count.
|
|
217
|
+
return toI32(toU32(length - 1) * 8 + toU32(topBits))
|
|
223
218
|
}
|
|
224
219
|
|
|
225
220
|
/**
|
|
@@ -236,34 +231,37 @@ const rsaBytesBelow = (a: u8[], b: u8[]): boolean => {
|
|
|
236
231
|
*/
|
|
237
232
|
export const rsaParsePublicKey = (der: u8[]): RsaPublicKey | null => {
|
|
238
233
|
const total: i32 = toI32(der.length)
|
|
239
|
-
const body:
|
|
240
|
-
if (body
|
|
234
|
+
const body: RsaDerSpan | null = rsaDerElement(der, 0, total, 0x30)
|
|
235
|
+
if (body === null || body.end !== total) {
|
|
241
236
|
return null
|
|
242
237
|
}
|
|
243
|
-
const
|
|
244
|
-
if (
|
|
238
|
+
const nSpan: RsaDerSpan | null = rsaDerElement(der, body.start, total, 0x02)
|
|
239
|
+
if (nSpan === null) {
|
|
245
240
|
return null
|
|
246
241
|
}
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
if (eStart < 0) {
|
|
242
|
+
const eSpan: RsaDerSpan | null = rsaDerElement(der, nSpan.end, total, 0x02)
|
|
243
|
+
if (eSpan === null || eSpan.end !== total) {
|
|
250
244
|
return null
|
|
251
245
|
}
|
|
252
|
-
const
|
|
253
|
-
|
|
246
|
+
const nRead: u8[] | null = rsaDerUnsigned(der, nSpan.start, nSpan.end)
|
|
247
|
+
const eRead: u8[] | null = rsaDerUnsigned(der, eSpan.start, eSpan.end)
|
|
248
|
+
if (nRead === null || eRead === null) {
|
|
254
249
|
return null
|
|
255
250
|
}
|
|
256
|
-
const n: u8[]
|
|
257
|
-
const e: u8[]
|
|
258
|
-
|
|
251
|
+
const n: u8[] = nRead
|
|
252
|
+
const e: u8[] = eRead
|
|
253
|
+
const nLength: i32 = toI32(n.length)
|
|
254
|
+
const eLength: i32 = toI32(e.length)
|
|
255
|
+
if (nLength === 0 || eLength === 0) {
|
|
259
256
|
return null
|
|
260
257
|
}
|
|
258
|
+
const nLast: i32 = toI32(n[nLength - 1])
|
|
259
|
+
const eLast: i32 = toI32(e[eLength - 1])
|
|
261
260
|
const bits: i32 = rsaBitLength(n)
|
|
262
|
-
if (bits < RSA_MIN_BITS || bits > RSA_MAX_BITS || (
|
|
261
|
+
if (bits < RSA_MIN_BITS || bits > RSA_MAX_BITS || (nLast & 1) === 0 || eLength > 4) {
|
|
263
262
|
return null
|
|
264
263
|
}
|
|
265
|
-
|
|
266
|
-
if (toI32(e.length) > 4 || (eLast & 1) === 0 || (toI32(e.length) === 1 && eLast < 3)) {
|
|
264
|
+
if ((eLast & 1) === 0 || (eLength === 1 && eLast < 3)) {
|
|
267
265
|
return null
|
|
268
266
|
}
|
|
269
267
|
return new RsaPublicKey(n, e, bits)
|
|
@@ -273,33 +271,39 @@ export const rsaParsePublicKey = (der: u8[]): RsaPublicKey | null => {
|
|
|
273
271
|
// Montgomery arithmetic on little-endian u32 limbs
|
|
274
272
|
// ---------------------------------------------------------------------------
|
|
275
273
|
|
|
276
|
-
/**
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
274
|
+
/**
|
|
275
|
+
* The big-endian `bytes` into the first `limbs` little-endian `u32` limbs of
|
|
276
|
+
* `out`, limb 0 the least significant; `bytes` fits in them. A byte before the
|
|
277
|
+
* start of `bytes` is one of its leading zeros.
|
|
278
|
+
*/
|
|
279
|
+
const rsaLimbsFromBytes = (out: u32[], bytes: u8[], limbs: i32): void => {
|
|
280
|
+
// `at` walks `bytes` from its last byte; once it passes the first, the
|
|
281
|
+
// bytes left are the leading zeros.
|
|
282
|
+
let at: i32 = toI32(bytes.length) - 1
|
|
283
|
+
for (let limb: i32 = 0; limb < limbs; limb++) {
|
|
284
|
+
let v: u32 = 0
|
|
285
|
+
for (let b: i32 = 0; b < 4; b++) {
|
|
286
|
+
if (at >= 0) {
|
|
287
|
+
v = v | (toU32(bytes[at]) << toU32(b * 8))
|
|
288
|
+
at = at - 1
|
|
289
|
+
}
|
|
286
290
|
}
|
|
291
|
+
out[limb] = v
|
|
287
292
|
}
|
|
288
|
-
return out
|
|
289
293
|
}
|
|
290
294
|
|
|
291
|
-
/**
|
|
295
|
+
/**
|
|
296
|
+
* The limbs `a` as `size` big-endian bytes, `size` 1 to 1024; the value fits.
|
|
297
|
+
* Byte `j` from the bottom is built at `1023 - j` of a 1024-byte buffer, whose
|
|
298
|
+
* last `size` bytes are the answer.
|
|
299
|
+
*/
|
|
292
300
|
const rsaBytesFromLimbs = (a: u32[], size: i32): u8[] => {
|
|
293
|
-
const out: u8[] = new Array<u8>(
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
const shift: i32 = size - 1 - i
|
|
297
|
-
const limb: i32 = shift >> 2
|
|
298
|
-
if (limb >= 0 && limb < limbs) {
|
|
299
|
-
out[i] = toU8(a[limb] >> toU32((shift & 3) << 3))
|
|
300
|
-
}
|
|
301
|
+
const out: u8[] = new Array<u8>(1024)
|
|
302
|
+
for (let j: i32 = 0; j < size; j++) {
|
|
303
|
+
out[1023 - j] = toU8(a[toI32(j / 4)] >> toU32((j & 3) << 3))
|
|
301
304
|
}
|
|
302
|
-
|
|
305
|
+
const from: i32 = 1024 - size
|
|
306
|
+
return out.filter((_b: u8, k: number): boolean => toI32(k) >= from)
|
|
303
307
|
}
|
|
304
308
|
|
|
305
309
|
/**
|
|
@@ -318,9 +322,6 @@ const rsaMontInverse = (n0: u32): u32 => {
|
|
|
318
322
|
|
|
319
323
|
/** Whether the `count`-limb value `a` is at least `n`, compared from the top limb. */
|
|
320
324
|
const rsaAtLeast = (a: u32[], n: u32[], count: i32): boolean => {
|
|
321
|
-
if (count > toI32(a.length) || count > toI32(n.length)) {
|
|
322
|
-
return true
|
|
323
|
-
}
|
|
324
325
|
for (let i: i32 = count - 1; i >= 0; i--) {
|
|
325
326
|
if (a[i] !== n[i]) {
|
|
326
327
|
return a[i] > n[i]
|
|
@@ -331,9 +332,6 @@ const rsaAtLeast = (a: u32[], n: u32[], count: i32): boolean => {
|
|
|
331
332
|
|
|
332
333
|
/** `a = a - n` over `count` limbs, dropping the borrow out of the top. */
|
|
333
334
|
const rsaSubInPlace = (a: u32[], n: u32[], count: i32): void => {
|
|
334
|
-
if (count > toI32(a.length) || count > toI32(n.length)) {
|
|
335
|
-
return
|
|
336
|
-
}
|
|
337
335
|
let borrow: u64 = 0
|
|
338
336
|
for (let i: i32 = 0; i < count; i++) {
|
|
339
337
|
const d: u64 = toU64(a[i]) - toU64(n[i]) - borrow
|
|
@@ -351,19 +349,11 @@ const rsaSubInPlace = (a: u32[], n: u32[], count: i32): void => {
|
|
|
351
349
|
* at most (2^32 - 1) + (2^32 - 1)^2 + (2^32 - 1) = 2^64 - 1, so a `u64` holds it.
|
|
352
350
|
*/
|
|
353
351
|
const rsaMontMul = (out: u32[], a: u32[], b: u32[], n: u32[], n0inv: u32, t: u32[], count: i32): void => {
|
|
354
|
-
|
|
355
|
-
count < 1 ||
|
|
356
|
-
count > toI32(out.length) ||
|
|
357
|
-
count > toI32(a.length) ||
|
|
358
|
-
count > toI32(b.length) ||
|
|
359
|
-
count > toI32(n.length) ||
|
|
360
|
-
count + 2 > toI32(t.length)
|
|
361
|
-
) {
|
|
362
|
-
return
|
|
363
|
-
}
|
|
364
|
-
for (let i: i32 = 0; i < toI32(t.length); i++) {
|
|
352
|
+
for (let i: i32 = 0; i < count; i++) {
|
|
365
353
|
t[i] = 0
|
|
366
354
|
}
|
|
355
|
+
t[count] = 0
|
|
356
|
+
t[count + 1] = 0
|
|
367
357
|
for (let i: i32 = 0; i < count; i++) {
|
|
368
358
|
const bi: u64 = toU64(b[i])
|
|
369
359
|
let carry: u64 = 0
|
|
@@ -396,9 +386,6 @@ const rsaMontMul = (out: u32[], a: u32[], b: u32[], n: u32[], n0inv: u32, t: u32
|
|
|
396
386
|
|
|
397
387
|
/** `r = 2 r mod n` over `count` limbs, for `r` below `n`; `r[count]` takes the bit the doubling carries out, so the comparison with `n` sees it. */
|
|
398
388
|
const rsaDoubleMod = (r: u32[], n: u32[], count: i32): void => {
|
|
399
|
-
if (count < 1 || count > toI32(n.length) || count + 1 > toI32(r.length)) {
|
|
400
|
-
return
|
|
401
|
-
}
|
|
402
389
|
let carry: u32 = 0
|
|
403
390
|
for (let i: i32 = 0; i < count; i++) {
|
|
404
391
|
const v: u32 = r[i]
|
|
@@ -413,61 +400,94 @@ const rsaDoubleMod = (r: u32[], n: u32[], count: i32): void => {
|
|
|
413
400
|
}
|
|
414
401
|
|
|
415
402
|
/**
|
|
416
|
-
* R^2 mod n, R = 2^(32 count), for the
|
|
417
|
-
* as `s 2^j` with `s` odd, doubling 1
|
|
418
|
-
*
|
|
419
|
-
* reach R 2^(32 count) = R^2: about a
|
|
420
|
-
* way.
|
|
403
|
+
* R^2 mod n into `r`, which holds zeros, R = 2^(32 count), for the
|
|
404
|
+
* `count`-limb odd `n`. Writing 32 count as `s 2^j` with `s` odd, doubling 1
|
|
405
|
+
* modulo `n` 32 count + s times gives R 2^s, and each Montgomery squaring of
|
|
406
|
+
* R 2^x gives R 2^2x, so `j` of them reach R 2^(32 count) = R^2: about a
|
|
407
|
+
* quarter of the work of doubling all the way.
|
|
421
408
|
*/
|
|
422
|
-
const rsaMontR2 = (n: u32[], n0inv: u32, t: u32[], count: i32):
|
|
423
|
-
let s: i32 = count
|
|
409
|
+
const rsaMontR2 = (r: u32[], n: u32[], n0inv: u32, t: u32[], count: i32): void => {
|
|
410
|
+
let s: i32 = count * 32
|
|
424
411
|
let squarings: i32 = 0
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
412
|
+
// `s` is 32 to 8192, 2^13, so it halves to odd within 13 steps.
|
|
413
|
+
for (let j: i32 = 0; j < 13; j++) {
|
|
414
|
+
if ((s & 1) === 0) {
|
|
415
|
+
s = s >> 1
|
|
416
|
+
squarings = j + 1
|
|
417
|
+
}
|
|
428
418
|
}
|
|
429
|
-
const r: u32[] = new Array<u32>(count + 1)
|
|
430
419
|
r[0] = 1
|
|
431
|
-
for (let k: i32 = 0; k <
|
|
420
|
+
for (let k: i32 = 0; k < count * 32; k++) {
|
|
421
|
+
rsaDoubleMod(r, n, count)
|
|
422
|
+
}
|
|
423
|
+
for (let k: i32 = 0; k < s; k++) {
|
|
432
424
|
rsaDoubleMod(r, n, count)
|
|
433
425
|
}
|
|
434
426
|
for (let k: i32 = 0; k < squarings; k++) {
|
|
435
427
|
rsaMontMul(r, r, r, n, n0inv, t, count)
|
|
436
428
|
}
|
|
437
|
-
return r
|
|
438
429
|
}
|
|
439
430
|
|
|
440
431
|
/**
|
|
441
|
-
* `s^e mod n` as
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
*
|
|
432
|
+
* `s^e mod n` as big-endian bytes as long as `n`, for an odd `n`; `null`
|
|
433
|
+
* for an `n` of no bytes or more than 1024, an `e` of zero, and an `s` longer
|
|
434
|
+
* than `n` or not below it (RSAVP1's range check). Every limb array is 256 limbs, the most a 1024-byte `n` has, plus
|
|
435
|
+
* the room `rsaDoubleMod`'s carry and `rsaMontMul`'s scratch take. R^2 mod n
|
|
436
|
+
* comes from `rsaMontR2`; `s` is moved into the Montgomery domain by one
|
|
437
|
+
* multiplication by it, raised by square and multiply from the exponent's top
|
|
438
|
+
* bit, and moved out by a multiplication by 1.
|
|
445
439
|
*/
|
|
446
|
-
const rsaModPow = (s: u8[], e: u8[], n: u8[]
|
|
447
|
-
const
|
|
448
|
-
|
|
440
|
+
const rsaModPow = (s: u8[], e: u8[], n: u8[]): u8[] | null => {
|
|
441
|
+
const nLength: i32 = toI32(n.length)
|
|
442
|
+
if (nLength < 1 || nLength > 1024 || toI32(s.length) > nLength || rsaBitLength(e) < 1) {
|
|
443
|
+
return null
|
|
444
|
+
}
|
|
445
|
+
const count: i32 = toI32((nLength + 3) / 4)
|
|
446
|
+
const nl: u32[] = new Array<u32>(256)
|
|
447
|
+
rsaLimbsFromBytes(nl, n, count)
|
|
449
448
|
const n0inv: u32 = rsaMontInverse(nl[0])
|
|
450
|
-
const t: u32[] = new Array<u32>(
|
|
451
|
-
const r2: u32[] =
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
449
|
+
const t: u32[] = new Array<u32>(258)
|
|
450
|
+
const r2: u32[] = new Array<u32>(257)
|
|
451
|
+
rsaMontR2(r2, nl, n0inv, t, count)
|
|
452
|
+
const sl: u32[] = new Array<u32>(256)
|
|
453
|
+
rsaLimbsFromBytes(sl, s, count)
|
|
454
|
+
// RSAVP1's range check: `s` must be below `n`. Limbs past `count` are zero
|
|
455
|
+
// in both, so the compare walks all 256 from the top.
|
|
456
|
+
let below: i32 = 0
|
|
457
|
+
for (let i: i32 = 255; i >= 0; i--) {
|
|
458
|
+
if (below === 0 && sl[i] !== nl[i]) {
|
|
459
|
+
below = sl[i] < nl[i] ? 1 : -1
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (below !== 1) {
|
|
463
|
+
return null
|
|
464
|
+
}
|
|
465
|
+
const base: u32[] = new Array<u32>(256)
|
|
466
|
+
rsaMontMul(base, sl, r2, nl, n0inv, t, count)
|
|
467
|
+
const acc: u32[] = new Array<u32>(256)
|
|
468
|
+
for (let i: i32 = 0; i < count; i++) {
|
|
456
469
|
acc[i] = base[i]
|
|
457
470
|
}
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
471
|
+
// `acc` holds `s` for the top bit; each bit after it squares, and a set
|
|
472
|
+
// one multiplies by `s`.
|
|
473
|
+
let started: boolean = false
|
|
474
|
+
for (const byte of e) {
|
|
475
|
+
for (let b: i32 = 7; b >= 0; b--) {
|
|
476
|
+
const bit: i32 = (toI32(byte) >> b) & 1
|
|
477
|
+
if (started) {
|
|
478
|
+
rsaMontMul(acc, acc, acc, nl, n0inv, t, count)
|
|
479
|
+
if (bit === 1) {
|
|
480
|
+
rsaMontMul(acc, acc, base, nl, n0inv, t, count)
|
|
481
|
+
}
|
|
482
|
+
} else if (bit === 1) {
|
|
483
|
+
started = true
|
|
484
|
+
}
|
|
465
485
|
}
|
|
466
486
|
}
|
|
467
|
-
const one: u32[] = new Array<u32>(
|
|
487
|
+
const one: u32[] = new Array<u32>(256)
|
|
468
488
|
one[0] = 1
|
|
469
489
|
rsaMontMul(acc, acc, one, nl, n0inv, t, count)
|
|
470
|
-
return rsaBytesFromLimbs(acc,
|
|
490
|
+
return rsaBytesFromLimbs(acc, nLength)
|
|
471
491
|
}
|
|
472
492
|
|
|
473
493
|
/**
|
|
@@ -482,27 +502,25 @@ const rsaOpen = (key: RsaPublicKey, sig: u8[], size: i32): u8[] | null => {
|
|
|
482
502
|
if (k !== toI32(key.n.length) || size < 0 || size > k) {
|
|
483
503
|
return null
|
|
484
504
|
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
lead += 1
|
|
488
|
-
}
|
|
489
|
-
const trimmed: u8[] = new Array<u8>(k - lead)
|
|
490
|
-
for (let i: i32 = lead; i < k; i++) {
|
|
491
|
-
trimmed[i - lead] = sig[i]
|
|
492
|
-
}
|
|
493
|
-
if (!rsaBytesBelow(trimmed, key.n)) {
|
|
505
|
+
const opened: u8[] | null = rsaModPow(sig, key.e, key.n)
|
|
506
|
+
if (opened === null) {
|
|
494
507
|
return null
|
|
495
508
|
}
|
|
496
|
-
const m: u8[] =
|
|
509
|
+
const m: u8[] = opened
|
|
497
510
|
const mLength: i32 = toI32(m.length)
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
return null
|
|
501
|
-
}
|
|
511
|
+
if (mLength < size) {
|
|
512
|
+
return null
|
|
502
513
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
514
|
+
// The bytes above the last `size` are zeros, or the value is too large.
|
|
515
|
+
const out: u8[] = []
|
|
516
|
+
for (let i: i32 = 0; i < toI32(m.length); i++) {
|
|
517
|
+
if (i < mLength - size) {
|
|
518
|
+
if (toI32(m[i]) !== 0) {
|
|
519
|
+
return null
|
|
520
|
+
}
|
|
521
|
+
} else {
|
|
522
|
+
out.push(m[i])
|
|
523
|
+
}
|
|
506
524
|
}
|
|
507
525
|
return out
|
|
508
526
|
}
|
|
@@ -546,26 +564,13 @@ const rsaDigestInfoPrefix = (hash: i32, hashSize: i32): u8[] => {
|
|
|
546
564
|
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00,
|
|
547
565
|
0x04, 0x20,
|
|
548
566
|
]
|
|
549
|
-
|
|
567
|
+
// A digest is at most 64 bytes, so the u32 sum is the body's length.
|
|
568
|
+
out[1] = toU8(toU32(hashSize) + 17)
|
|
550
569
|
out[14] = toU8(hash)
|
|
551
570
|
out[18] = toU8(hashSize)
|
|
552
571
|
return out
|
|
553
572
|
}
|
|
554
573
|
|
|
555
|
-
/** Whether `a` and `b` hold the same bytes. Both are public, so it stops at the first difference. */
|
|
556
|
-
const rsaSameBytes = (a: u8[], b: u8[]): boolean => {
|
|
557
|
-
const length: i32 = toI32(a.length)
|
|
558
|
-
if (toI32(b.length) !== length) {
|
|
559
|
-
return false
|
|
560
|
-
}
|
|
561
|
-
for (let i: i32 = 0; i < length && i < toI32(b.length); i++) {
|
|
562
|
-
if (a[i] !== b[i]) {
|
|
563
|
-
return false
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
return true
|
|
567
|
-
}
|
|
568
|
-
|
|
569
574
|
/**
|
|
570
575
|
* Verifies an RSASSA-PKCS1-v1_5 signature (RFC 8017 §8.2.2) of `msg` under
|
|
571
576
|
* `key`, with the hash id `hash`. The encoded message the signature should
|
|
@@ -583,9 +588,14 @@ const rsaSameBytes = (a: u8[], b: u8[]): boolean => {
|
|
|
583
588
|
export const rsaVerifyPkcs1v15 = (key: RsaPublicKey, hash: i32, msg: u8[], sig: u8[]): boolean => {
|
|
584
589
|
const hashSize: i32 = rsaHashSize(hash)
|
|
585
590
|
const k: i32 = toI32(key.n.length)
|
|
591
|
+
// 1024 is RSA_MAX_BITS / 8: a longer modulus is one the parser refuses.
|
|
592
|
+
if (hashSize < 1 || hashSize > 64 || k > 1024) {
|
|
593
|
+
return false
|
|
594
|
+
}
|
|
586
595
|
const prefix: u8[] = rsaDigestInfoPrefix(hash, hashSize)
|
|
587
|
-
|
|
588
|
-
|
|
596
|
+
// The prefix is the 19-byte literal `rsaDigestInfoPrefix` builds.
|
|
597
|
+
const tLength: i32 = 19 + hashSize
|
|
598
|
+
if (k < tLength + 11) {
|
|
589
599
|
return false
|
|
590
600
|
}
|
|
591
601
|
const em: u8[] | null = rsaOpen(key, sig, k)
|
|
@@ -603,16 +613,19 @@ export const rsaVerifyPkcs1v15 = (key: RsaPublicKey, hash: i32, msg: u8[], sig:
|
|
|
603
613
|
for (const byte of rsaDigest(hash, msg)) {
|
|
604
614
|
expected.push(byte)
|
|
605
615
|
}
|
|
606
|
-
return
|
|
616
|
+
return timingSafeEqual(em, expected)
|
|
607
617
|
}
|
|
608
618
|
|
|
609
619
|
/**
|
|
610
|
-
* MGF1 (RFC 8017 §B.2.1) with the hash id `hash` over `seed
|
|
611
|
-
* the digests of `seed || C` for the
|
|
620
|
+
* XORs MGF1 (RFC 8017 §B.2.1) with the hash id `hash` over `seed` into
|
|
621
|
+
* `target`, as many bytes as it holds: the digests of `seed || C` for the
|
|
622
|
+
* four-byte counters C = 0, 1, …, joined.
|
|
612
623
|
*/
|
|
613
|
-
const
|
|
614
|
-
|
|
615
|
-
|
|
624
|
+
const rsaMgf1Xor = (hash: i32, seed: u8[], target: u8[]): void => {
|
|
625
|
+
let pos: i32 = 0
|
|
626
|
+
let counter: u32 = 0
|
|
627
|
+
// Each round's digest is at least 32 bytes, so the loop ends.
|
|
628
|
+
while (pos < toI32(target.length)) {
|
|
616
629
|
const input: u8[] = []
|
|
617
630
|
for (const byte of seed) {
|
|
618
631
|
input.push(byte)
|
|
@@ -622,12 +635,13 @@ const rsaMgf1 = (hash: i32, seed: u8[], length: i32): u8[] => {
|
|
|
622
635
|
input.push(toU8(counter >> 8))
|
|
623
636
|
input.push(toU8(counter))
|
|
624
637
|
for (const byte of rsaDigest(hash, input)) {
|
|
625
|
-
if (toI32(
|
|
626
|
-
|
|
638
|
+
if (pos < toI32(target.length)) {
|
|
639
|
+
target[pos] = target[pos] ^ byte
|
|
640
|
+
pos += 1
|
|
627
641
|
}
|
|
628
642
|
}
|
|
643
|
+
counter = counter + 1
|
|
629
644
|
}
|
|
630
|
-
return out
|
|
631
645
|
}
|
|
632
646
|
|
|
633
647
|
/**
|
|
@@ -650,48 +664,77 @@ const rsaMgf1 = (hash: i32, seed: u8[], length: i32): u8[] => {
|
|
|
650
664
|
*/
|
|
651
665
|
export const rsaVerifyPss = (key: RsaPublicKey, hash: i32, saltLen: i32, msg: u8[], sig: u8[]): boolean => {
|
|
652
666
|
const hashSize: i32 = rsaHashSize(hash)
|
|
653
|
-
|
|
667
|
+
const bits: i32 = rsaBitLength(key.n)
|
|
668
|
+
// 8192 is RSA_MAX_BITS: a longer modulus is one the parser refuses.
|
|
669
|
+
if (hashSize < 1 || hashSize > 64 || saltLen < 0 || bits < 2 || bits > 8192) {
|
|
654
670
|
return false
|
|
655
671
|
}
|
|
656
|
-
const emBits: i32 =
|
|
672
|
+
const emBits: i32 = bits - 1
|
|
657
673
|
const emLen: i32 = (emBits + 7) >> 3
|
|
658
|
-
if (saltLen > emLen || emLen < hashSize + saltLen + 2) {
|
|
674
|
+
if (emLen < 1 || emLen > 1024 || saltLen > emLen || emLen < hashSize + saltLen + 2) {
|
|
659
675
|
return false
|
|
660
676
|
}
|
|
661
|
-
const
|
|
662
|
-
|
|
677
|
+
const dbLength: i32 = emLen - hashSize - 1
|
|
678
|
+
const opened: u8[] | null = rsaOpen(key, sig, emLen)
|
|
679
|
+
if (opened === null) {
|
|
663
680
|
return false
|
|
664
681
|
}
|
|
665
|
-
const
|
|
666
|
-
const
|
|
667
|
-
|
|
668
|
-
if ((toI32(em[0]) & ~topMask) !== 0) {
|
|
682
|
+
const em: u8[] = opened
|
|
683
|
+
const emLength: i32 = toI32(em.length)
|
|
684
|
+
if (emLength !== emLen) {
|
|
669
685
|
return false
|
|
670
686
|
}
|
|
687
|
+
const full: i32 = 0xff
|
|
688
|
+
const topMask: i32 = full >> (emLen * 8 - emBits)
|
|
689
|
+
// EM is maskedDB, then H, then the trailer 0xbc; the leftmost bits of
|
|
690
|
+
// maskedDB above emBits must be zero.
|
|
691
|
+
const db: u8[] = []
|
|
671
692
|
const h: u8[] = []
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
return false
|
|
693
|
+
let trailer: i32 = 0
|
|
694
|
+
for (let i: i32 = 0; i < toI32(em.length); i++) {
|
|
695
|
+
if (i < dbLength) {
|
|
696
|
+
if (i === 0 && (toI32(em[i]) & ~topMask) !== 0) {
|
|
697
|
+
return false
|
|
698
|
+
}
|
|
699
|
+
db.push(em[i])
|
|
700
|
+
} else if (i < emLength - 1) {
|
|
701
|
+
h.push(em[i])
|
|
702
|
+
} else {
|
|
703
|
+
trailer = toI32(em[i])
|
|
684
704
|
}
|
|
685
705
|
}
|
|
686
|
-
if (
|
|
706
|
+
if (trailer !== 0xbc) {
|
|
687
707
|
return false
|
|
688
708
|
}
|
|
709
|
+
rsaMgf1Xor(hash, h, db)
|
|
710
|
+
// DB is PS (zeros), 0x01, then the salt, which M' takes after the
|
|
711
|
+
// message's digest.
|
|
712
|
+
const zeros: i32 = emLen - hashSize - saltLen - 2
|
|
689
713
|
const prime: u8[] = [0, 0, 0, 0, 0, 0, 0, 0]
|
|
690
714
|
for (const byte of rsaDigest(hash, msg)) {
|
|
691
715
|
prime.push(byte)
|
|
692
716
|
}
|
|
693
|
-
|
|
694
|
-
|
|
717
|
+
let separated: boolean = false
|
|
718
|
+
for (let i: i32 = 0; i < toI32(db.length); i++) {
|
|
719
|
+
let v: i32 = toI32(db[i])
|
|
720
|
+
if (i === 0) {
|
|
721
|
+
v = v & topMask
|
|
722
|
+
}
|
|
723
|
+
if (i < zeros) {
|
|
724
|
+
if (v !== 0) {
|
|
725
|
+
return false
|
|
726
|
+
}
|
|
727
|
+
} else if (i === zeros) {
|
|
728
|
+
if (v !== 0x01) {
|
|
729
|
+
return false
|
|
730
|
+
}
|
|
731
|
+
separated = true
|
|
732
|
+
} else {
|
|
733
|
+
prime.push(toU8(v))
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
if (!separated) {
|
|
737
|
+
return false
|
|
695
738
|
}
|
|
696
|
-
return
|
|
739
|
+
return timingSafeEqual(rsaDigest(hash, prime), h)
|
|
697
740
|
}
|