@amritk/nish-aarch64-linux 0.35.0 → 0.37.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 (128) hide show
  1. package/INSTALL.md +73 -59
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +50 -4
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +2 -0
  6. package/runtime/async.mjs +7 -0
  7. package/runtime/nish.d.ts +4849 -430
  8. package/runtime/nish.h +25 -0
  9. package/runtime/page.d.ts +53 -0
  10. package/runtime/page.mjs +146 -0
  11. package/runtime/runtime-async.c +557 -0
  12. package/runtime/runtime-wasm.c +63 -7
  13. package/runtime/vite.d.ts +30 -0
  14. package/runtime/vite.mjs +150 -0
  15. package/scripts/build.sh +252 -15
  16. package/std/README.md +107 -82
  17. package/std/base64.ts +34 -5
  18. package/std/bytes.ts +279 -59
  19. package/std/cli-args.ts +296 -70
  20. package/std/cli-flags.ts +184 -19
  21. package/std/cli.ts +475 -54
  22. package/std/collections.ts +121 -23
  23. package/std/compress/checksum.ts +44 -14
  24. package/std/compress/deflate.ts +224 -111
  25. package/std/compress/gzip.ts +217 -53
  26. package/std/compress/zlib.ts +63 -7
  27. package/std/crypto/aes.ts +128 -52
  28. package/std/crypto/base64url.ts +29 -5
  29. package/std/crypto/bcrypt-batch.ts +101 -34
  30. package/std/crypto/bcrypt.ts +93 -27
  31. package/std/crypto/chacha20poly1305.ts +69 -14
  32. package/std/crypto/ct.ts +32 -13
  33. package/std/crypto/ed25519.ts +66 -16
  34. package/std/crypto/hkdf.ts +198 -13
  35. package/std/crypto/hmac.ts +242 -37
  36. package/std/crypto/jwt.ts +204 -72
  37. package/std/crypto/p256.ts +222 -72
  38. package/std/crypto/p384.ts +51 -23
  39. package/std/crypto/pki-roots.ts +32 -2
  40. package/std/crypto/pki.ts +99 -30
  41. package/std/crypto/rsa.ts +56 -22
  42. package/std/crypto/sha1.ts +3 -0
  43. package/std/crypto/sha256.ts +24 -1
  44. package/std/crypto/sha512.ts +99 -14
  45. package/std/crypto/x25519.ts +163 -24
  46. package/std/crypto/x509-random.ts +10 -0
  47. package/std/crypto/x509.ts +133 -66
  48. package/std/host/ring.ts +124 -0
  49. package/std/json-equal.ts +31 -11
  50. package/std/json-format.ts +112 -13
  51. package/std/json-number.ts +76 -26
  52. package/std/json-pointer.ts +23 -1
  53. package/std/json-tokenizer.ts +126 -51
  54. package/std/json-typed.ts +757 -96
  55. package/std/json-value.ts +438 -157
  56. package/std/json.ts +170 -38
  57. package/std/map.ts +25 -7
  58. package/std/math.ts +36 -4
  59. package/std/net/buffer-pool.ts +42 -11
  60. package/std/net/dns-config.ts +109 -23
  61. package/std/net/dns-message.ts +62 -7
  62. package/std/net/dns-resolver.ts +247 -53
  63. package/std/net/dns.ts +203 -35
  64. package/std/net/fetch.ts +1267 -290
  65. package/std/net/handler.ts +296 -80
  66. package/std/net/hpack.ts +290 -77
  67. package/std/net/http-compress.ts +152 -36
  68. package/std/net/http-fields.ts +85 -9
  69. package/std/net/http1-client-codec.ts +129 -44
  70. package/std/net/http1-codec.ts +279 -114
  71. package/std/net/http1-server.ts +409 -139
  72. package/std/net/http2-frame.ts +289 -51
  73. package/std/net/http2-tls.ts +152 -49
  74. package/std/net/http2.ts +314 -160
  75. package/std/net/http3-frame.ts +165 -27
  76. package/std/net/http3-server.ts +282 -131
  77. package/std/net/http3.ts +377 -173
  78. package/std/net/outbound.ts +37 -3
  79. package/std/net/postgres-carrier.ts +144 -8
  80. package/std/net/postgres.ts +898 -260
  81. package/std/net/prefork.ts +202 -65
  82. package/std/net/qpack.ts +120 -32
  83. package/std/net/quic-conn-ack.ts +60 -12
  84. package/std/net/quic-conn-cid.ts +135 -27
  85. package/std/net/quic-conn-params.ts +109 -22
  86. package/std/net/quic-datagram.ts +50 -3
  87. package/std/net/quic-frame.ts +333 -23
  88. package/std/net/quic-listener.ts +161 -75
  89. package/std/net/quic-packet.ts +373 -73
  90. package/std/net/quic-recovery.ts +287 -65
  91. package/std/net/quic-stream.ts +436 -146
  92. package/std/net/quic.ts +693 -323
  93. package/std/net/router.ts +87 -40
  94. package/std/net/tls/client.ts +167 -51
  95. package/std/net/tls/codec.ts +494 -68
  96. package/std/net/tls/record-client.ts +140 -73
  97. package/std/net/tls/record-server.ts +233 -105
  98. package/std/net/tls/record.ts +142 -36
  99. package/std/net/tls/schedule.ts +235 -26
  100. package/std/net/tls/ticket.ts +161 -40
  101. package/std/net/tls/verify.ts +57 -10
  102. package/std/net/tls-tcp.ts +308 -95
  103. package/std/net/tls.ts +276 -102
  104. package/std/net/upstream.ts +81 -45
  105. package/std/net/url.ts +109 -22
  106. package/std/net/websocket.ts +140 -26
  107. package/std/net/webtransport.ts +243 -106
  108. package/std/pair.ts +10 -2
  109. package/std/random.ts +42 -15
  110. package/std/regex.ts +422 -257
  111. package/std/region.ts +70 -15
  112. package/std/secret.ts +52 -11
  113. package/std/sort.ts +17 -2
  114. package/std/sqlite.ts +256 -35
  115. package/std/sync/ring.ts +229 -56
  116. package/std/sync/shm.ts +203 -23
  117. package/std/term-io.ts +154 -78
  118. package/std/term-pixels.ts +144 -19
  119. package/std/term-probe.ts +77 -35
  120. package/std/term-rate.ts +199 -118
  121. package/std/term.ts +400 -175
  122. package/std/testing.ts +130 -32
  123. package/std/text.ts +84 -17
  124. package/std/threads.ts +248 -56
  125. package/std/yaml-json.ts +111 -97
  126. package/std/yaml-lines.ts +47 -15
  127. package/std/yaml-write.ts +45 -30
  128. package/std/yaml.ts +764 -384
@@ -23,14 +23,16 @@
23
23
  * cleartext until `install` gives it a suite and a traffic secret, then
24
24
  * TLSCiphertext (§5.2) under the AEAD the suite names. Each record's nonce is
25
25
  * the traffic IV XORed with the 64-bit record sequence number (§5.3), and its
26
- * additional data is the record's own header. Inside, the content is followed
27
- * by its real type and by zero padding (§5.4); `open` finds the type by
28
- * reading every byte of the decrypted record whatever the padding is, so how
29
- * much padding there was is not in its timing, and refuses a record that is
30
- * padding and nothing else. Both `seal` and `open` write into a buffer the
31
- * caller owns and answer a count, and what they allocate on the way dies with
32
- * the call (docs/LANGUAGE.md, "Memory model"), so a connection that has
33
- * finished its handshake moves records without moving the arena.
26
+ * additional data is the record's own header.
27
+ *
28
+ * Inside, the content is followed by its real type and by zero padding (§5.4);
29
+ * `open` finds the type by reading every byte of the decrypted record whatever
30
+ * the padding is, so how much padding there was is not in its timing, and
31
+ * refuses a record that is padding and nothing else. Both `seal` and `open`
32
+ * write into a buffer the caller owns and answer a count, and what they
33
+ * allocate on the way dies with the call (docs/LANGUAGE.md, "Memory model"), so
34
+ * a connection that has finished its handshake moves records without moving the
35
+ * arena.
34
36
  *
35
37
  * **Refusals are alerts.** Every function that can refuse answers the alert to
36
38
  * send, negated, as a negative count: `-TLS_ALERT_BAD_RECORD_MAC` for a record
@@ -41,17 +43,20 @@
41
43
  * sends can make it panic.
42
44
  *
43
45
  * **Secrets.** A traffic secret arrives as plain bytes, since that is how
44
- * `nish/net/tls` hands it over. The key, the IV and the AES key schedule
45
- * derived from it are held in the protection's fields, because a `Secret` may
46
- * not be one (NL2430), in arrays the protection reuses for every key it is
47
- * given, and are zeroed when it is installed again or cleared — the key and
48
- * IV with `secureZero`, the schedule with `aesKeyWipe`, the volatile `wipe`
49
- * of its round keys (the kernels' powers of H included) and of H. The key and
50
- * IV are derived straight into place, in an HKDF scratch that is wiped after
51
- * each derivation, and the schedule is expanded into place by `aesKeyInto`
52
- * (TLS-2 in `docs/security/tls.md`). A caller that holds the traffic secret
53
- * as a `Secret` installs it with `installHeld` and ratchets it in place with
54
- * `advanceHeld`, so the secret itself is never plain outside `exposeWith`.
46
+ * `nish/net/tls` hands it over.
47
+ *
48
+ * The key, the IV and the AES key schedule derived from it are held in the
49
+ * protection's fields, because a `Secret` may not be one (NL2430), in arrays
50
+ * the protection reuses for every key it is given, and are zeroed when it is
51
+ * installed again or cleared — the key and IV with `secureZero`, the schedule
52
+ * with `aesKeyWipe`, the volatile `wipe` of its round keys (the kernels' powers
53
+ * of H included) and of H. The key and IV are derived straight into place, in
54
+ * an HKDF scratch that is wiped after each derivation, and the schedule is
55
+ * expanded into place by `aesKeyInto` (TLS-2 in `docs/security/tls.md`).
56
+ *
57
+ * A caller that holds the traffic secret as a `Secret` installs it with
58
+ * `installHeld` and ratchets it in place with `advanceHeld`, so the secret
59
+ * itself is never plain outside `exposeWith`.
55
60
  *
56
61
  * The record layer is sans-IO: `nish/net/tls/record-server` runs it over a
57
62
  * stream for `TlsServer`, and `nish/net/tls-tcp` puts that on a socket.
@@ -147,13 +152,25 @@ const TLS_RECORD_FROM: i32 = 0
147
152
  * `HKDF-Expand-Label(secret, "traffic upd", "", Hash.length)` over the hash
148
153
  * `hashLength` names, held as a `Secret`. The old secret is the caller's to
149
154
  * wipe.
155
+ *
156
+ * @param hashLength The hash length of the connection's suite, 32 or 48, and so how many bytes are derived.
157
+ * @param trafficSecret The direction's current traffic secret. This function does not wipe it.
158
+ * @returns A fresh `Secret` holding the next traffic secret, which the caller owns and wipes.
150
159
  */
151
160
  export const tlsNextTrafficSecret = (hashLength: i32, trafficSecret: Secret<u8[]>): Secret<u8[]> => {
152
161
  const none: u8[] = []
153
162
  return tlsExpandLabel(hashLength, trafficSecret, "traffic upd", none, hashLength)
154
163
  }
155
164
 
156
- /** Whether `[off, off + len)` is a window inside an array of `length` elements: every caller's range check. */
165
+ /**
166
+ * Whether `[off, off + len)` is a window inside an array of `length` elements: every caller's range check.
167
+ * It is written as `off <= length - len`, not `off + len <= length`, so a large `off` cannot overflow.
168
+ *
169
+ * @param length How many elements the array has.
170
+ * @param off Where the window starts.
171
+ * @param len How many elements the window spans.
172
+ * @returns `true` when `off` and `len` are not negative and the window ends at or before `length`.
173
+ */
157
174
  export const tlsRecordWindowFits = (length: i32, off: i32, len: i32): boolean =>
158
175
  off >= 0 && len >= 0 && off <= length - len
159
176
 
@@ -175,6 +192,13 @@ const tlsWriteRecordHeader = (out: u8[], at: i32, type: i32, len: i32): void =>
175
192
  * in, or the alert negated when the header announces a type this layer does
176
193
  * not know (`unexpected_message`) or a body past 2^14 + 256 bytes
177
194
  * (`record_overflow`, §5.2). The legacy version is not read (§5.1).
195
+ *
196
+ * @param buf The array that holds the bytes received so far.
197
+ * @param off Where in `buf` the record's header starts.
198
+ * @param avail How many bytes, from `off`, have arrived.
199
+ * @returns The whole record's length in bytes, header included; 0 while the header is not all in;
200
+ * `-TLS_ALERT_UNEXPECTED_MESSAGE` or `-TLS_ALERT_RECORD_OVERFLOW` for a header this layer refuses;
201
+ * `-TLS_ALERT_INTERNAL_ERROR` when `buf[off .. off + avail)` is not inside `buf`.
178
202
  */
179
203
  export const tlsRecordLength = (buf: u8[], off: i32, avail: i32): i32 => {
180
204
  if (!tlsRecordWindowFits(toI32(buf.length), off, avail)) {
@@ -211,14 +235,18 @@ export class TlsRecordReader {
211
235
  /** The bytes held, or `empty` while the reader holds none. */
212
236
  buffer: u8[]
213
237
  /** The zero-length array `buffer` is while no buffer is borrowed. */
214
- empty: u8[]
238
+ private empty: u8[]
215
239
  /** Where `buffer` comes from and goes back to. */
216
- pool: BufferPool
240
+ private pool: BufferPool
217
241
  /** The first byte not yet consumed. */
218
242
  start: i32 = 0
219
243
  /** One past the last byte pushed. */
220
244
  end: i32 = 0
221
245
 
246
+ /**
247
+ * Makes an empty reader with a pool of its own, one buffer deep. No buffer is
248
+ * taken until the first `push` brings a byte.
249
+ */
222
250
  constructor() {
223
251
  this.empty = []
224
252
  this.buffer = this.empty
@@ -228,6 +256,8 @@ export class TlsRecordReader {
228
256
  /**
229
257
  * Borrows from `pool` from now on, giving back first, zeroed, any buffer
230
258
  * held from the old one. Every byte held is forgotten, as `reset` does.
259
+ *
260
+ * @internal Read by `net/tls/record-server.ts`.
231
261
  */
232
262
  borrowFrom(pool: BufferPool): void {
233
263
  this.reset()
@@ -242,7 +272,7 @@ export class TlsRecordReader {
242
272
  }
243
273
 
244
274
  /** Gives the buffer back, zeroed, if one is held. */
245
- giveBack(): void {
275
+ private giveBack(): void {
246
276
  if (this.buffer.length > 0) {
247
277
  // Every byte of it may have held a record: zero all of it.
248
278
  this.pool.giveSecret(this.buffer, TLS_MAX_RECORD)
@@ -250,7 +280,11 @@ export class TlsRecordReader {
250
280
  }
251
281
  }
252
282
 
253
- /** How many bytes `push` would take now. */
283
+ /**
284
+ * How many bytes `push` would take now.
285
+ *
286
+ * @returns `TLS_MAX_RECORD` less the bytes held and not yet consumed.
287
+ */
254
288
  space(): i32 {
255
289
  return TLS_MAX_RECORD - (this.end - this.start)
256
290
  }
@@ -259,6 +293,12 @@ export class TlsRecordReader {
259
293
  * Copies as many of `data[off .. off + len)` as fit and answers how many,
260
294
  * after moving what is held to the front. A window outside `data` is
261
295
  * `-TLS_ALERT_INTERNAL_ERROR`, and nothing is copied.
296
+ *
297
+ * @param data The array that holds the bytes read from the stream.
298
+ * @param off Where in `data` the new bytes start.
299
+ * @param len How many bytes, from `off`, to offer.
300
+ * @returns How many of the bytes were taken, from 0 (the buffer is full) up to `len`; the caller offers the
301
+ * rest again once a record is consumed. `-TLS_ALERT_INTERNAL_ERROR` for a window outside `data`.
262
302
  */
263
303
  push(data: u8[], off: i32, len: i32): i32 {
264
304
  if (!tlsRecordWindowFits(toI32(data.length), off, len)) {
@@ -292,6 +332,9 @@ export class TlsRecordReader {
292
332
  * The length of the whole record at `start`, header included, once every
293
333
  * byte of it is in; 0 before; or the alert negated for a header this layer
294
334
  * refuses, as `tlsRecordLength` says.
335
+ *
336
+ * @returns The record's length in bytes, read at `buffer[start]`; 0 while it is not all in; or a negative
337
+ * `TLS_ALERT_*` value, the alert to send.
295
338
  */
296
339
  next(): i32 {
297
340
  const avail: i32 = this.end - this.start
@@ -303,6 +346,8 @@ export class TlsRecordReader {
303
346
  * Lets the first `n` held bytes go: the record `next` answered, once it is
304
347
  * handled. When none are left the next record starts at the front; the
305
348
  * buffer stays until `reset`.
349
+ *
350
+ * @param n How many bytes to let go, usually what `next` answered. Clamped to 0 and to the bytes held.
306
351
  */
307
352
  consume(n: i32): void {
308
353
  const held: i32 = this.end - this.start
@@ -345,19 +390,26 @@ export class TlsRecordProtection {
345
390
  */
346
391
  recordLimit: i64 = 0
347
392
  /** The negotiated suite, or 0 while the direction is cleartext. */
348
- suite: i32 = 0
393
+ private suite: i32 = 0
349
394
  /** The real content type of the last record `open` answered. */
350
395
  contentType: i32 = 0
351
396
  /** The arrays `key` and `aes` point into: AES-128's and ChaCha20's or AES-256's. */
352
397
  key16: u8[]
398
+ /** The 32-byte key array `key` points into for ChaCha20-Poly1305 and AES-256-GCM; `clear` zeroes it. */
353
399
  key32: u8[]
400
+ /** The expanded AES-128 key schedule `aes` points to under AES-128-GCM, 10 rounds; `clear` wipes it. */
354
401
  aes128: AesKey
402
+ /** The expanded AES-256 key schedule `aes` points to under AES-256-GCM, 14 rounds; `clear` wipes it. */
355
403
  aes256: AesKey
356
404
  /** Where `advance` derives the next traffic secret: HashLen bytes of it. */
357
405
  next: u8[]
358
406
  /** Where the keys are derived. */
359
- kdf: HkdfScratch
407
+ private kdf: HkdfScratch
360
408
 
409
+ /**
410
+ * Makes a cleartext direction and every array it will fill, so that no
411
+ * install allocates. The record limit starts at `TLS_KEY_UPDATE_RECORDS`.
412
+ */
361
413
  constructor() {
362
414
  this.key16 = new Array<u8>(16)
363
415
  this.key32 = new Array<u8>(32)
@@ -370,7 +422,11 @@ export class TlsRecordProtection {
370
422
  this.recordLimit = TLS_KEY_UPDATE_RECORDS
371
423
  }
372
424
 
373
- /** Whether records in this direction are protected. */
425
+ /**
426
+ * Whether records in this direction are protected.
427
+ *
428
+ * @returns `true` once a suite is installed; `false` while the direction is cleartext.
429
+ */
374
430
  isProtected(): boolean {
375
431
  return this.suite !== 0
376
432
  }
@@ -380,6 +436,8 @@ export class TlsRecordProtection {
380
436
  * and both AES key schedules wiped: the keys and IV with `secureZero`, and
381
437
  * each schedule with `aesKeyWipe`, its whole `roundKeys` and its H, which
382
438
  * the next `install` fills again (TLS-2).
439
+ *
440
+ * @internal Read by `net/tls/record-server.ts` and `net/tls/record-client.ts`.
383
441
  */
384
442
  clear(): void {
385
443
  secureZero(this.key16)
@@ -402,6 +460,11 @@ export class TlsRecordProtection {
402
460
  * The key, the IV and the AES key schedule are written into the arrays this
403
461
  * direction made when it was constructed, so an install allocates nothing
404
462
  * that outlives it (TLS-3).
463
+ *
464
+ * @param suite The negotiated cipher suite, such as `TLS_AES_128_GCM_SHA256`.
465
+ * @param secret The direction's traffic secret, plain. It is read, not kept, and stays the caller's to
466
+ * wipe.
467
+ * @returns `true` when the direction is now protected; `false` when it was left cleartext.
405
468
  */
406
469
  install(suite: i32, secret: u8[]): boolean {
407
470
  this.clear()
@@ -416,7 +479,7 @@ export class TlsRecordProtection {
416
479
  }
417
480
 
418
481
  /** Points `key`, and for AES `aes`, at the arrays of the size `suite` needs. */
419
- reserve(suite: i32): void {
482
+ private reserve(suite: i32): void {
420
483
  this.key = tlsSuiteKeyLength(suite) === 16 ? this.key16 : this.key32
421
484
  if (suite !== TLS_CHACHA20_POLY1305_SHA256) {
422
485
  this.aes = tlsSuiteKeyLength(suite) === 16 ? this.aes128 : this.aes256
@@ -431,7 +494,7 @@ export class TlsRecordProtection {
431
494
  * inside an arena block, so a key install leaves the arena where it was.
432
495
  * ChaCha20 has no schedule.
433
496
  */
434
- derive(suite: i32, secret: u8[]): void {
497
+ private derive(suite: i32, secret: u8[]): void {
435
498
  using _scope = arena()
436
499
  tlsTrafficKeysInto(this.kdf, suite, secret, this.key, this.iv)
437
500
  const aes: AesKey | null = this.aes
@@ -445,7 +508,7 @@ export class TlsRecordProtection {
445
508
  * ChaCha20: a protection that carried AES keeps its schedule for the next
446
509
  * AES key, and must not use it for ChaCha20.
447
510
  */
448
- aesForSuite(): AesKey | null {
511
+ private aesForSuite(): AesKey | null {
449
512
  return this.suite === TLS_CHACHA20_POLY1305_SHA256 ? null : this.aes
450
513
  }
451
514
 
@@ -455,6 +518,11 @@ export class TlsRecordProtection {
455
518
  * and the keys are installed from it; the next secret's own copy is wiped.
456
519
  * The next secret is derived in place inside an arena block, as `derive`'s
457
520
  * keys are. False, as `install` answers, when the keys do not install.
521
+ *
522
+ * @param secret The direction's current traffic secret, plain, which is overwritten with the next one. It
523
+ * stays the caller's to wipe. One shorter than the suite's hash length panics in HKDF.
524
+ * @returns `true` when the next keys are installed; `false` for a cleartext direction, or when the install
525
+ * fails and the direction is left cleartext.
458
526
  */
459
527
  advance(secret: u8[]): boolean {
460
528
  const suite: i32 = this.suite
@@ -492,6 +560,10 @@ export class TlsRecordProtection {
492
560
  * plain output of each derivation wiped, and the AES key schedule expanded
493
561
  * into place inside an arena block, as `derive`'s is. `false`, leaving the
494
562
  * direction cleartext, where `install` answers it.
563
+ *
564
+ * @param suite The negotiated cipher suite, such as `TLS_AES_128_GCM_SHA256`.
565
+ * @param secret The direction's held traffic secret. It is read, not kept, and stays the caller's to wipe.
566
+ * @returns `true` when the direction is now protected; `false` when it was left cleartext.
495
567
  */
496
568
  installHeld(suite: i32, secret: Secret<u8[]>): boolean {
497
569
  this.clear()
@@ -516,6 +588,11 @@ export class TlsRecordProtection {
516
588
  * ratchet that leaves no copy of either, and the keys are installed from
517
589
  * it with `installHeld`. False, as `advance` answers, when the keys do not
518
590
  * install; a secret that is not its hash's length is left as it was.
591
+ *
592
+ * @param secret The direction's held traffic secret, overwritten in place with the next one. It stays the
593
+ * caller's to wipe.
594
+ * @returns `true` when the next keys are installed; `false` for a cleartext direction, or when the install
595
+ * fails and the direction is left cleartext.
519
596
  */
520
597
  advanceHeld(secret: Secret<u8[]>): boolean {
521
598
  const suite: i32 = this.suite
@@ -534,7 +611,13 @@ export class TlsRecordProtection {
534
611
  return this.installHeld(suite, secret)
535
612
  }
536
613
 
537
- /** The per-record nonce (§5.3): the IV with the sequence number XORed into its last eight bytes, big-endian. */
614
+ /**
615
+ * The per-record nonce (§5.3): the IV with the sequence number XORed into its last eight bytes,
616
+ * big-endian.
617
+ *
618
+ * @param nonce Where the nonce is written, from index 0: 12 bytes, or as many as fit. It is as secret as
619
+ * the IV, so the caller wipes it after use, as `seal` and `open` do.
620
+ */
538
621
  nonceInto(nonce: u8[]): void {
539
622
  for (let k: i32 = 0; k < TLS_IV_SIZE && k < toI32(nonce.length) && k < toI32(this.iv.length); k++) {
540
623
  const shift: i32 = (TLS_IV_SIZE - 1 - k) << 3
@@ -553,6 +636,16 @@ export class TlsRecordProtection {
553
636
  * with nothing written, for content over 2^14 bytes, a padding that would
554
637
  * take the inner plaintext past 2^14 + 1, a window outside `data`, too
555
638
  * little room at `out[at]`, or a sequence number that would wrap.
639
+ *
640
+ * @param type The content type to send, such as `TLS_CONTENT_HANDSHAKE`.
641
+ * @param data The array that holds the content.
642
+ * @param off Where in `data` the content starts.
643
+ * @param len How many bytes of content, from `off`, to seal: at most 2^14.
644
+ * @param padding How many zero bytes to add inside the protected record; 0 when cleartext.
645
+ * @param out Where the record is written.
646
+ * @param at Where in `out` the record's header starts.
647
+ * @returns The record's length in bytes, header included (and the tag, when protected), or
648
+ * `-TLS_ALERT_INTERNAL_ERROR`.
556
649
  */
557
650
  seal(type: i32, data: u8[], off: i32, len: i32, padding: i32, out: u8[], at: i32): i32 {
558
651
  if (!tlsRecordWindowFits(toI32(data.length), off, len) || len > TLS_MAX_PLAINTEXT || padding < 0) {
@@ -615,14 +708,27 @@ export class TlsRecordProtection {
615
708
  * `contentType`. Cleartext, the content is the body and the type the
616
709
  * header's: `application_data` is `unexpected_message`, since it is never
617
710
  * sent in the clear, and a body over 2^14 bytes is `record_overflow`.
711
+ *
618
712
  * Protected, the header's type must be `application_data`
619
713
  * (`unexpected_message` otherwise), the body must authenticate under this
620
714
  * record's nonce with the header as additional data (`bad_record_mac`), the
621
- * decrypted record may be no longer than 2^14 + 1 bytes
622
- * (`record_overflow`), and its last non-zero byte is the real type — one
623
- * that is all zeros is `unexpected_message` (§5.4). Every refusal is the
624
- * alert negated; a window outside `record`, a `len` that is not the
625
- * header's, or too little room at `out[at]` is `-TLS_ALERT_INTERNAL_ERROR`.
715
+ * decrypted record may be no longer than 2^14 + 1 bytes (`record_overflow`),
716
+ * and its last non-zero byte is the real type — one that is all zeros is
717
+ * `unexpected_message` (§5.4). Every refusal is the alert negated; a window
718
+ * outside `record`, a `len` that is not the header's, or too little room at
719
+ * `out[at]` is `-TLS_ALERT_INTERNAL_ERROR`.
720
+ *
721
+ * A protected record that fails to authenticate leaves the sequence number
722
+ * where it was; one that authenticates spends its number even when its
723
+ * content is then refused.
724
+ *
725
+ * @param record The array that holds the record, usually a reader's buffer.
726
+ * @param off Where in `record` the record's header starts.
727
+ * @param len The whole record's length in bytes, header included, as `tlsRecordLength` answered it.
728
+ * @param out Where the content is written.
729
+ * @param at Where in `out` the content starts.
730
+ * @returns The content's length in bytes, which may be 0, or a negative `TLS_ALERT_*` value, the alert to
731
+ * send.
626
732
  */
627
733
  open(record: u8[], off: i32, len: i32, out: u8[], at: i32): i32 {
628
734
  if (!tlsRecordWindowFits(toI32(record.length), off, len) || len < TLS_RECORD_HEADER_SIZE) {