@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.
- package/INSTALL.md +73 -59
- package/bin/nish +0 -0
- package/docs/AI.md +50 -4
- package/package.json +1 -1
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/crypto/x25519.ts
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `nish/crypto/x25519` — the X25519 Diffie-Hellman function of RFC 7748.
|
|
3
3
|
*
|
|
4
|
+
* import { X25519_SIZE, x25519, x25519Base } from "nish/crypto/x25519";
|
|
5
|
+
* import { timingSafeEqual } from "nish/crypto/ct";
|
|
6
|
+
* import { Secret, expose, secret, wipe } from "nish:secret";
|
|
7
|
+
*
|
|
8
|
+
* const isZero = (s: u8[]): boolean => timingSafeEqual(s, new Array<u8>(X25519_SIZE))
|
|
9
|
+
*
|
|
10
|
+
* const raw: u8[] = new Array<u8>(X25519_SIZE)
|
|
11
|
+
* crypto.getRandomValues(raw) // any 32 bytes are a private key
|
|
12
|
+
* const mine: Secret<u8[]> = secret(raw)
|
|
13
|
+
* const myPublic: u8[] | null = x25519Base(mine) // 32 bytes, sent to the peer
|
|
14
|
+
* const peerPublic: u8[] = new Array<u8>(X25519_SIZE) // the peer's 32 bytes, received
|
|
15
|
+
* const shared: Secret<u8[]> | null = x25519(mine, peerPublic)
|
|
16
|
+
* wipe(mine)
|
|
17
|
+
* if (shared !== null) {
|
|
18
|
+
* if (!expose(shared, isZero)) { // a low-order peer key gives zero
|
|
19
|
+
* // derive keys from `shared` inside `expose`
|
|
20
|
+
* }
|
|
21
|
+
* wipe(shared)
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
4
24
|
* Written from the specification (RFC 7748 §4.1, §5 and §6.1), in this
|
|
5
25
|
* module's own structure: nothing here is ported from another implementation.
|
|
6
26
|
* The radix-2^25.5 limbs and the shape of the inversion chain are the ideas
|
|
@@ -38,16 +58,17 @@
|
|
|
38
58
|
* `f25519Square`, `f25519MulA24`, `f25519Add`, `f25519Sub`, `f25519Swap` and
|
|
39
59
|
* one pass of `x25519Ladder`'s loop, and the check reads each one's `clang -O2`
|
|
40
60
|
* machine code for x86-64 and aarch64 and finds no branch and no load or store
|
|
41
|
-
* at an address the scalar bit or a limb reaches.
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
61
|
+
* at an address the scalar bit or a limb reaches.
|
|
62
|
+
*
|
|
63
|
+
* They are copies because the check refuses every branch, loop back-edges
|
|
64
|
+
* included, and this module's own functions keep their loops and the early
|
|
65
|
+
* `return`s below; the copies drop the guards and write `f25519Mul`'s outer
|
|
66
|
+
* loop out a row at a time, and the fixture's `main` holds them to the same RFC
|
|
67
|
+
* 7748 vectors as this module. A change to one of those functions belongs in
|
|
68
|
+
* that fixture too. What stays discipline rather than proof is the rest: the
|
|
69
|
+
* loops around the step, `f25519Decode`, `f25519Encode` and `f25519Invert`,
|
|
70
|
+
* whose branches are all on public positions. Weekly, `tests/ct-timing.js`
|
|
71
|
+
* times the fixture's functions on real x86-64 and aarch64 hardware as well.
|
|
51
72
|
*
|
|
52
73
|
* **Loop bounds are literals.** A field element is always ten limbs and an
|
|
53
74
|
* encoding always 32 bytes, and each function that indexes one opens with an
|
|
@@ -84,14 +105,39 @@ export const X25519_SIZE: i32 = 32
|
|
|
84
105
|
const X25519_A24: i64 = 121665
|
|
85
106
|
|
|
86
107
|
/**
|
|
87
|
-
* `a + b
|
|
88
|
-
* `
|
|
89
|
-
* so the answer is the signed one bit for
|
|
90
|
-
* because signed arithmetic is checked for an
|
|
91
|
-
* branch on a secret limb. Each is one
|
|
108
|
+
* `a + b` on limbs: the `u64` sum, read back as `i64`; `f25519Sub64` and
|
|
109
|
+
* `f25519Mul64` are its difference and product. Every bound this module states
|
|
110
|
+
* keeps the true result inside `i64`, so the answer is the signed one bit for
|
|
111
|
+
* bit; the arithmetic is unsigned because signed arithmetic is checked for an
|
|
112
|
+
* overflow, and a check is a branch on a secret limb. Each is one
|
|
113
|
+
* instruction once LLVM inlines it.
|
|
114
|
+
*
|
|
115
|
+
* @param a The first limb.
|
|
116
|
+
* @param b The second limb.
|
|
117
|
+
* @returns The sum, which the caller's bounds keep inside `i64`.
|
|
118
|
+
*
|
|
119
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
92
120
|
*/
|
|
93
121
|
export const f25519Add64 = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
|
|
122
|
+
/**
|
|
123
|
+
* `a - b` on limbs, unsigned and read back as `i64`, as `f25519Add64` explains.
|
|
124
|
+
*
|
|
125
|
+
* @param a The limb subtracted from.
|
|
126
|
+
* @param b The limb subtracted.
|
|
127
|
+
* @returns The difference, which the caller's bounds keep inside `i64`.
|
|
128
|
+
*
|
|
129
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
130
|
+
*/
|
|
94
131
|
export const f25519Sub64 = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
|
|
132
|
+
/**
|
|
133
|
+
* `a * b` on limbs, unsigned and read back as `i64`, as `f25519Add64` explains.
|
|
134
|
+
*
|
|
135
|
+
* @param a The first limb.
|
|
136
|
+
* @param b The second limb.
|
|
137
|
+
* @returns The product, which the caller's bounds keep inside `i64`.
|
|
138
|
+
*
|
|
139
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
140
|
+
*/
|
|
95
141
|
export const f25519Mul64 = (a: i64, b: i64): i64 => toI64(toU64(a) * toU64(b))
|
|
96
142
|
|
|
97
143
|
/** The width of limb `i`: 26 bits for an even limb, 25 for an odd one. */
|
|
@@ -103,20 +149,40 @@ const f25519Width = (i: i32): i64 => {
|
|
|
103
149
|
/** The mask of limb `i`'s bits, `2^width - 1`. */
|
|
104
150
|
const f25519LimbMask = (i: i32): i64 => f25519Sub64(toI64(1) << f25519Width(i), toI64(1))
|
|
105
151
|
|
|
106
|
-
/**
|
|
152
|
+
/**
|
|
153
|
+
* A fresh field element, zero.
|
|
154
|
+
*
|
|
155
|
+
* @returns A new ten-limb element, every limb zero.
|
|
156
|
+
*
|
|
157
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
158
|
+
*/
|
|
107
159
|
export const f25519Zero = (): i64[] => {
|
|
108
160
|
const f: i64[] = new Array<i64>(10)
|
|
109
161
|
return f
|
|
110
162
|
}
|
|
111
163
|
|
|
112
|
-
/**
|
|
164
|
+
/**
|
|
165
|
+
* A fresh field element holding the small constant `n`.
|
|
166
|
+
*
|
|
167
|
+
* @param n The constant, held in limb 0.
|
|
168
|
+
* @returns A new element whose value is `n`.
|
|
169
|
+
*
|
|
170
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
171
|
+
*/
|
|
113
172
|
export const f25519Small = (n: i64): i64[] => {
|
|
114
173
|
const f: i64[] = f25519Zero()
|
|
115
174
|
f[0] = n
|
|
116
175
|
return f
|
|
117
176
|
}
|
|
118
177
|
|
|
119
|
-
/**
|
|
178
|
+
/**
|
|
179
|
+
* `out = f`, limb by limb.
|
|
180
|
+
*
|
|
181
|
+
* @param out Where the limbs go.
|
|
182
|
+
* @param f The element copied.
|
|
183
|
+
*
|
|
184
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
185
|
+
*/
|
|
120
186
|
export const f25519Copy = (out: i64[], f: i64[]): void => {
|
|
121
187
|
if (toI32(out.length) < 10 || toI32(f.length) < 10) {
|
|
122
188
|
return
|
|
@@ -157,6 +223,10 @@ const f25519CarryChain = (h: i64[]): void => {
|
|
|
157
223
|
* Each carry is `h >> width` (arithmetic, so a floor) and each remainder is
|
|
158
224
|
* `h & (2^width - 1)`, which is the matching non-negative remainder for a
|
|
159
225
|
* negative limb too, without shifting a negative number left.
|
|
226
|
+
*
|
|
227
|
+
* @param h The element, carried in place.
|
|
228
|
+
*
|
|
229
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
160
230
|
*/
|
|
161
231
|
export const f25519Carry = (h: i64[]): void => {
|
|
162
232
|
if (toI32(h.length) < 10) {
|
|
@@ -174,6 +244,12 @@ export const f25519Carry = (h: i64[]): void => {
|
|
|
174
244
|
/**
|
|
175
245
|
* `out = f + g`, not carried. With both reduced (limbs below 2^26 in
|
|
176
246
|
* magnitude) the sum's limbs are below 2^27, which `f25519Mul` accepts.
|
|
247
|
+
*
|
|
248
|
+
* @param out Where the sum goes; it may be `f` or `g`.
|
|
249
|
+
* @param f The first addend, reduced.
|
|
250
|
+
* @param g The second addend, reduced.
|
|
251
|
+
*
|
|
252
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
177
253
|
*/
|
|
178
254
|
export const f25519Add = (out: i64[], f: i64[], g: i64[]): void => {
|
|
179
255
|
if (toI32(out.length) < 10 || toI32(f.length) < 10 || toI32(g.length) < 10) {
|
|
@@ -188,6 +264,12 @@ export const f25519Add = (out: i64[], f: i64[], g: i64[]): void => {
|
|
|
188
264
|
* `out = f - g`, not carried. Signed limbs need no bias to stay positive; with
|
|
189
265
|
* both reduced the difference's limbs are below 2^27 in magnitude, which
|
|
190
266
|
* `f25519Mul` accepts.
|
|
267
|
+
*
|
|
268
|
+
* @param out Where the difference goes; it may be `f` or `g`.
|
|
269
|
+
* @param f The element subtracted from, reduced.
|
|
270
|
+
* @param g The element subtracted, reduced.
|
|
271
|
+
*
|
|
272
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
191
273
|
*/
|
|
192
274
|
export const f25519Sub = (out: i64[], f: i64[], g: i64[]): void => {
|
|
193
275
|
if (toI32(out.length) < 10 || toI32(f.length) < 10 || toI32(g.length) < 10) {
|
|
@@ -213,6 +295,12 @@ export const f25519Sub = (out: i64[], f: i64[], g: i64[]): void => {
|
|
|
213
295
|
* own, so no intermediate is larger. Giving `f25519Mul` anything wider — two unreduced
|
|
214
296
|
* sums added again, say — breaks this bound, which is why every caller carries
|
|
215
297
|
* or multiplies before it adds a second time.
|
|
298
|
+
*
|
|
299
|
+
* @param out Where the product goes; it may be `f` or `g`.
|
|
300
|
+
* @param f The first factor.
|
|
301
|
+
* @param g The second factor.
|
|
302
|
+
*
|
|
303
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
216
304
|
*/
|
|
217
305
|
export const f25519Mul = (out: i64[], f: i64[], g: i64[]): void => {
|
|
218
306
|
if (toI32(f.length) < 10 || toI32(g.length) < 10) {
|
|
@@ -245,12 +333,27 @@ export const f25519Mul = (out: i64[], f: i64[], g: i64[]): void => {
|
|
|
245
333
|
f25519Copy(out, h)
|
|
246
334
|
}
|
|
247
335
|
|
|
248
|
-
/**
|
|
336
|
+
/**
|
|
337
|
+
* `out = f * f`, reduced; the same bounds as `f25519Mul`.
|
|
338
|
+
*
|
|
339
|
+
* @param out Where the square goes; it may be `f`.
|
|
340
|
+
* @param f The element squared.
|
|
341
|
+
*
|
|
342
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
343
|
+
*/
|
|
249
344
|
export const f25519Square = (out: i64[], f: i64[]): void => {
|
|
250
345
|
f25519Mul(out, f, f)
|
|
251
346
|
}
|
|
252
347
|
|
|
253
|
-
/**
|
|
348
|
+
/**
|
|
349
|
+
* `out = f` squared `n` times in a row (`n` at least 1), reduced.
|
|
350
|
+
*
|
|
351
|
+
* @param out Where the result goes; it may be `f`.
|
|
352
|
+
* @param f The element squared.
|
|
353
|
+
* @param n How many squarings.
|
|
354
|
+
*
|
|
355
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
356
|
+
*/
|
|
254
357
|
export const f25519SquareTimes = (out: i64[], f: i64[], n: i32): void => {
|
|
255
358
|
f25519Square(out, f)
|
|
256
359
|
for (let i: i32 = 1; i < n; i++) {
|
|
@@ -280,6 +383,12 @@ const f25519MulA24 = (out: i64[], f: i64[]): void => {
|
|
|
280
383
|
* 5, 10, 20, 40, 50, 100, 200 and 250 from the rule
|
|
281
384
|
* `z^(2^(a+b) - 1) = (z^(2^a - 1))^(2^b) * z^(2^b - 1)`: 251 squarings and 11
|
|
282
385
|
* multiplications, the same for every `z`. `out` and `z11` may not be `z`.
|
|
386
|
+
*
|
|
387
|
+
* @param out Where `z^(2^250 - 1)` goes.
|
|
388
|
+
* @param z11 Where `z^11` goes.
|
|
389
|
+
* @param z The base, reduced.
|
|
390
|
+
*
|
|
391
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
283
392
|
*/
|
|
284
393
|
export const f25519Pow250 = (out: i64[], z11: i64[], z: i64[]): void => {
|
|
285
394
|
const z2: i64[] = f25519Zero()
|
|
@@ -322,6 +431,11 @@ export const f25519Pow250 = (out: i64[], z11: i64[], z: i64[]): void => {
|
|
|
322
431
|
* followed by `01011`: `f25519Pow250`, then five squarings and a
|
|
323
432
|
* multiplication by `z^11` — 256 squarings and 12 multiplications, the same
|
|
324
433
|
* for every `z`.
|
|
434
|
+
*
|
|
435
|
+
* @param out Where the inverse goes.
|
|
436
|
+
* @param z The element inverted, reduced.
|
|
437
|
+
*
|
|
438
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
325
439
|
*/
|
|
326
440
|
export const f25519Invert = (out: i64[], z: i64[]): void => {
|
|
327
441
|
const z11: i64[] = f25519Zero()
|
|
@@ -336,6 +450,12 @@ export const f25519Invert = (out: i64[], z: i64[]): void => {
|
|
|
336
450
|
* Swaps `f` and `g` when `bit` is 1 and leaves them when it is 0, with the
|
|
337
451
|
* same instructions either way: the mask is all ones or all zeros, and the
|
|
338
452
|
* swap is the masked xor of the two.
|
|
453
|
+
*
|
|
454
|
+
* @param f The first element, swapped in place.
|
|
455
|
+
* @param g The second element, swapped in place.
|
|
456
|
+
* @param bit 1 to swap, 0 to leave; a secret, so no branch reads it.
|
|
457
|
+
*
|
|
458
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
339
459
|
*/
|
|
340
460
|
export const f25519Swap = (f: i64[], g: i64[], bit: i64): void => {
|
|
341
461
|
if (toI32(f.length) < 10 || toI32(g.length) < 10) {
|
|
@@ -357,6 +477,11 @@ export const f25519Swap = (f: i64[], g: i64[], bit: i64): void => {
|
|
|
357
477
|
* lands in `[0, 2^width)` and the element is reduced. A value in
|
|
358
478
|
* `[p, 2^255)` is neither refused nor adjusted: §5 has it processed as its
|
|
359
479
|
* residue, and the arithmetic does that by itself.
|
|
480
|
+
*
|
|
481
|
+
* @param bytes The 32-byte little-endian encoding.
|
|
482
|
+
* @returns A new reduced element, or zero for fewer than 32 bytes.
|
|
483
|
+
*
|
|
484
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
360
485
|
*/
|
|
361
486
|
export const f25519Decode = (bytes: u8[]): i64[] => {
|
|
362
487
|
const f: i64[] = new Array<i64>(10)
|
|
@@ -398,6 +523,11 @@ export const f25519Decode = (bytes: u8[]): i64[] => {
|
|
|
398
523
|
* carry out of bit 255 tells the two apart without a branch — it is 1 exactly
|
|
399
524
|
* when the value is at least p — so the element adds `19 * q` and drops bit
|
|
400
525
|
* 255, which is subtracting `p * q`.
|
|
526
|
+
*
|
|
527
|
+
* @param f The element, reduced.
|
|
528
|
+
* @returns Its 32 canonical little-endian bytes.
|
|
529
|
+
*
|
|
530
|
+
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
401
531
|
*/
|
|
402
532
|
export const f25519Encode = (f: i64[]): u8[] => {
|
|
403
533
|
const h: i64[] = new Array<i64>(10)
|
|
@@ -554,10 +684,16 @@ const x25519Exposed = (scalar: u8[], u: u8[]): u8[] | null => {
|
|
|
554
684
|
*
|
|
555
685
|
* Answers `null` unless both arguments are exactly `X25519_SIZE` bytes. An
|
|
556
686
|
* all-zero answer is **returned, not refused**: it is what a low-order `u`
|
|
557
|
-
* gives, and RFC 7748 §6.1 leaves checking for it to the protocol.
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
*
|
|
687
|
+
* gives, and RFC 7748 §6.1 leaves checking for it to the protocol.
|
|
688
|
+
* `nish/net/tls` makes that check on both sides of its handshake; any other
|
|
689
|
+
* caller doing a key exchange must refuse an all-zero answer itself — inside
|
|
690
|
+
* `expose`, with `timingSafeEqual` against 32 zero bytes, as the module's
|
|
691
|
+
* example does.
|
|
692
|
+
*
|
|
693
|
+
* @param scalar The private key, 32 bytes, clamped on a copy.
|
|
694
|
+
* @param u The peer's u-coordinate (its public key), 32 bytes.
|
|
695
|
+
* @returns The shared secret, 32 bytes and possibly all zero, or `null` for an
|
|
696
|
+
* argument of the wrong length.
|
|
561
697
|
*/
|
|
562
698
|
export const x25519 = (scalar: Secret<u8[]>, u: u8[]): Secret<u8[]> | null => {
|
|
563
699
|
const shared: u8[] | null = exposeWith(scalar, u, x25519Exposed)
|
|
@@ -571,6 +707,9 @@ export const x25519 = (scalar: Secret<u8[]>, u: u8[]): Secret<u8[]> | null => {
|
|
|
571
707
|
* `x25519(scalar, 9)`: the public key for the private key `scalar`, since 9 is
|
|
572
708
|
* curve25519's base point (RFC 7748 §4.1, §6.1), and public, so plain bytes.
|
|
573
709
|
* Answers `null` unless `scalar` is `X25519_SIZE` bytes.
|
|
710
|
+
*
|
|
711
|
+
* @param scalar The private key, 32 bytes.
|
|
712
|
+
* @returns The public key, 32 bytes, or `null` for a key of the wrong length.
|
|
574
713
|
*/
|
|
575
714
|
export const x25519Base = (scalar: Secret<u8[]>): u8[] | null => {
|
|
576
715
|
const base: u8[] = new Array<u8>(32)
|
|
@@ -29,6 +29,8 @@ import { X509_MAX_SERIAL, x509MintSelfSigned } from "nish/crypto/x509"
|
|
|
29
29
|
* so every key is equally likely; a draw is refused with odds below 2^-32. A
|
|
30
30
|
* refused draw is wiped before the next, and the caller wipes the one
|
|
31
31
|
* returned.
|
|
32
|
+
*
|
|
33
|
+
* @returns The 32-byte scalar, big-endian, in a new `Secret` the caller owns and wipes.
|
|
32
34
|
*/
|
|
33
35
|
export const x509DrawP256Key = (): Secret<u8[]> => {
|
|
34
36
|
while (true) {
|
|
@@ -48,6 +50,8 @@ export const x509DrawP256Key = (): Secret<u8[]> => {
|
|
|
48
50
|
* so the INTEGER is positive, never zero, and always twenty octets long — 158
|
|
49
51
|
* random bits, past the 64 the CA/Browser Forum asks of a serial. A serial is
|
|
50
52
|
* public, written into the certificate, so it is a plain array.
|
|
53
|
+
*
|
|
54
|
+
* @returns The 20 serial octets, big-endian, in a fresh array.
|
|
51
55
|
*/
|
|
52
56
|
export const x509DrawSerial = (): u8[] => {
|
|
53
57
|
const serial: u8[] = new Array<u8>(X509_MAX_SERIAL)
|
|
@@ -62,6 +66,12 @@ export const x509DrawSerial = (): u8[] => {
|
|
|
62
66
|
* the mint answers `null` for. The key stays the caller's, since the server
|
|
63
67
|
* that presents the certificate signs its handshakes with it; draw one with
|
|
64
68
|
* `x509DrawP256Key`.
|
|
69
|
+
*
|
|
70
|
+
* @param priv The 32-byte P-256 private scalar; read, and left for the caller to wipe.
|
|
71
|
+
* @param commonName The subject's and issuer's common name, 1 to 64 bytes of UTF-8 without a NUL.
|
|
72
|
+
* @param notBeforeMs When validity starts, in milliseconds since 1970-01-01 UTC.
|
|
73
|
+
* @param days How long it is valid, 1 to `X509_MAX_DAYS`.
|
|
74
|
+
* @returns The certificate's DER, a fresh array, or `null`.
|
|
65
75
|
*/
|
|
66
76
|
export const x509MintSelfSignedDrawn = (
|
|
67
77
|
priv: Secret<u8[]>,
|