nish 0.44.0 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/docs/INSTALL.md +3 -1
  4. package/package.json +5 -5
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. 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
- * Where the contents of the element at `der[at]` start, when it has the
119
- * identifier `tag` and a definite length in its shortest form that fits
120
- * before `end`; -1 otherwise. The length is `rsaDerLength`'s answer for the
121
- * same arguments. Lengths of more than two octets are refused: the largest
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 rsaDerContents = (der: u8[], at: i32, end: i32, tag: i32): i32 => {
125
- if (at < 0 || at + 2 > end || toI32(der[at]) !== tag) {
126
- return -1
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 + 3 > end) {
133
- return -1
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 -1
151
+ return null
138
152
  }
139
153
  start = at + 3
140
154
  } else if (first === 0x82) {
141
- if (at + 4 > end) {
142
- return -1
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 -1
160
+ return null
147
161
  }
148
162
  start = at + 4
149
163
  } else if (first >= 0x80) {
150
- return -1
164
+ return null
151
165
  }
152
166
  if (length > end - start) {
153
- return -1
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
- return first
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
- * start + length)`, big-endian with no leading zero byte; `null` for an empty
173
- * one, a negative one, and one not in its shortest form (a leading 0x00 that
174
- * a clear top bit after it did not need).
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, length: i32): u8[] | null => {
177
- const end: i32 = start + length
178
- if (start < 0 || length < 1 || end > toI32(der.length) || toI32(der[start]) >= 0x80) {
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
- if (length === 1 || toI32(der[start + 1]) < 0x80) {
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 out: u8[] = new Array<u8>(end - from)
189
- for (let i: i32 = from; i < end; i++) {
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
- /** The number of significant bits in the big-endian `v`, which has no leading zero byte. */
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
- const length: i32 = toI32(v.length)
198
- if (length === 0) {
202
+ if (toI32(v.length) < 1) {
199
203
  return 0
200
204
  }
201
- let top: i32 = toI32(v[0])
202
- let bits: i32 = (length - 1) << 3
203
- while (top > 0) {
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
- for (let i: i32 = 0; i < aLength && i < bLength; i++) {
218
- if (a[i] !== b[i]) {
219
- return a[i] < b[i]
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
- return false
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: i32 = rsaDerContents(der, 0, total, 0x30)
240
- if (body < 0 || body + rsaDerLength(der, 0) !== total) {
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 nStart: i32 = rsaDerContents(der, body, total, 0x02)
244
- if (nStart < 0) {
238
+ const nSpan: RsaDerSpan | null = rsaDerElement(der, body.start, total, 0x02)
239
+ if (nSpan === null) {
245
240
  return null
246
241
  }
247
- const nEnd: i32 = nStart + rsaDerLength(der, body)
248
- const eStart: i32 = rsaDerContents(der, nEnd, total, 0x02)
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 eLength: i32 = rsaDerLength(der, nEnd)
253
- if (eStart + eLength !== total) {
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[] | null = rsaDerUnsigned(der, nStart, nEnd - nStart)
257
- const e: u8[] | null = rsaDerUnsigned(der, eStart, eLength)
258
- if (n === null || e === null) {
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 || (toI32(n[toI32(n.length) - 1]) & 1) === 0) {
261
+ if (bits < RSA_MIN_BITS || bits > RSA_MAX_BITS || (nLast & 1) === 0 || eLength > 4) {
263
262
  return null
264
263
  }
265
- const eLast: i32 = toI32(e[toI32(e.length) - 1])
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
- /** The big-endian `bytes` as `limbs` little-endian `u32` limbs, limb 0 the least significant; `bytes` fits. */
277
- const rsaLimbsFromBytes = (bytes: u8[], limbs: i32): u32[] => {
278
- const out: u32[] = new Array<u32>(limbs)
279
- const size: i32 = toI32(out.length)
280
- const length: i32 = toI32(bytes.length)
281
- for (let i: i32 = 0; i < length; i++) {
282
- const shift: i32 = length - 1 - i
283
- const limb: i32 = shift >> 2
284
- if (limb >= 0 && limb < size) {
285
- out[limb] = out[limb] | (toU32(bytes[i]) << toU32((shift & 3) << 3))
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
- /** The limbs `a` as `size` big-endian bytes; the value fits. */
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>(size)
294
- const limbs: i32 = toI32(a.length)
295
- for (let i: i32 = 0; i < toI32(out.length); i++) {
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
- return out
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
- if (
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 `count`-limb odd `n`. Writing 32 count
417
- * as `s 2^j` with `s` odd, doubling 1 modulo `n` 32 count + s times gives
418
- * R 2^s, and each Montgomery squaring of R 2^x gives R 2^2x, so `j` of them
419
- * reach R 2^(32 count) = R^2: about a quarter of the work of doubling all the
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): u32[] => {
423
- let s: i32 = count << 5
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
- while (s > 0 && (s & 1) === 0) {
426
- s = s >> 1
427
- squarings += 1
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 < (count << 5) + s; 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 `size` big-endian bytes, for an odd `n` and an `s` below it.
442
- * R^2 mod n comes from `rsaMontR2`; `s` is moved into
443
- * the Montgomery domain by one multiplication by it, raised by square and
444
- * multiply from the exponent's top bit, and moved out by a multiplication by 1.
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[], size: i32): u8[] => {
447
- const count: i32 = (toI32(n.length) + 3) >> 2
448
- const nl: u32[] = rsaLimbsFromBytes(n, count)
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>(count + 2)
451
- const r2: u32[] = rsaMontR2(nl, n0inv, t, count)
452
- const base: u32[] = new Array<u32>(count)
453
- rsaMontMul(base, rsaLimbsFromBytes(s, count), r2, nl, n0inv, t, count)
454
- const acc: u32[] = new Array<u32>(count)
455
- for (let i: i32 = 0; i < toI32(acc.length) && i < toI32(base.length); i++) {
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
- const bits: i32 = rsaBitLength(e)
459
- const eLength: i32 = toI32(e.length)
460
- for (let k: i32 = bits - 2; k >= 0; k--) {
461
- rsaMontMul(acc, acc, acc, nl, n0inv, t, count)
462
- const at: i32 = eLength - 1 - (k >> 3)
463
- if (at >= 0 && at < eLength && ((toI32(e[at]) >> (k & 7)) & 1) === 1) {
464
- rsaMontMul(acc, acc, base, nl, n0inv, t, count)
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>(count)
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, size)
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
- let lead: i32 = 0
486
- while (lead < k && toI32(sig[lead]) === 0) {
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[] = rsaModPow(trimmed, key.e, key.n, k)
509
+ const m: u8[] = opened
497
510
  const mLength: i32 = toI32(m.length)
498
- for (let i: i32 = 0; i < mLength - size && i < mLength; i++) {
499
- if (toI32(m[i]) !== 0) {
500
- return null
501
- }
511
+ if (mLength < size) {
512
+ return null
502
513
  }
503
- const out: u8[] = new Array<u8>(size)
504
- for (let i: i32 = 0; i < toI32(out.length); i++) {
505
- out[i] = m[mLength - size + i]
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
- out[1] = toU8(hashSize + 17)
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
- const tLength: i32 = toI32(prefix.length) + hashSize
588
- if (hashSize === 0 || k < tLength + 11) {
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 rsaSameBytes(em, expected)
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`, `length` bytes:
611
- * the digests of `seed || C` for the four-byte counters C = 0, 1, …, joined.
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 rsaMgf1 = (hash: i32, seed: u8[], length: i32): u8[] => {
614
- const out: u8[] = []
615
- for (let counter: i32 = 0; toI32(out.length) < length; counter++) {
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(out.length) < length) {
626
- out.push(byte)
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
- if (hashSize === 0 || saltLen < 0) {
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 = rsaBitLength(key.n) - 1
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 em: u8[] | null = rsaOpen(key, sig, emLen)
662
- if (em === null || toI32(em[emLen - 1]) !== 0xbc) {
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 dbLength: i32 = emLen - hashSize - 1
666
- const full: i32 = 0xff
667
- const topMask: i32 = full >> ((emLen << 3) - emBits)
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
- for (let i: i32 = dbLength; i < emLen - 1; i++) {
673
- h.push(em[i])
674
- }
675
- const db: u8[] = rsaMgf1(hash, h, dbLength)
676
- for (let i: i32 = 0; i < dbLength && i < toI32(db.length); i++) {
677
- db[i] = db[i] ^ em[i]
678
- }
679
- db[0] = toU8(toI32(db[0]) & topMask)
680
- const zeros: i32 = emLen - hashSize - saltLen - 2
681
- for (let i: i32 = 0; i < zeros && i < toI32(db.length); i++) {
682
- if (toI32(db[i]) !== 0) {
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 (toI32(db[zeros]) !== 0x01) {
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
- for (let i: i32 = dbLength - saltLen; i >= 0 && i < dbLength && i < toI32(db.length); i++) {
694
- prime.push(db[i])
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 rsaSameBytes(rsaDigest(hash, prime), h)
739
+ return timingSafeEqual(rsaDigest(hash, prime), h)
697
740
  }