@amritk/nish-aarch64-linux 0.37.0 → 0.39.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 +28 -16
- package/bin/nish +0 -0
- package/docs/AI.md +380 -47
- package/package.json +1 -1
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +645 -66
- package/runtime/nish.h +101 -15
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/runtime-aes.c +12 -4
- package/runtime/runtime-async.c +267 -51
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-net.c +32 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +170 -8
- package/runtime/runtime-sandbox.c +377 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +18 -3
- package/runtime/runtime.c +55 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +113 -36
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +7 -4
- package/std/array.ts +266 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/p256.ts +247 -74
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +35 -9
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +113 -10
- package/std/host/text.ts +86 -0
- package/std/json-tokenizer.ts +20 -0
- package/std/json-typed.ts +217 -8
- package/std/math.ts +743 -41
- package/std/net/dns-resolver.ts +46 -6
- package/std/net/dns.ts +50 -13
- package/std/net/fetch.ts +691 -129
- package/std/net/handler.ts +142 -16
- package/std/net/http-compress.ts +17 -9
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-codec.ts +517 -154
- package/std/net/http1-server.ts +508 -25
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +84 -0
- package/std/net/postgres-async.ts +2223 -0
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +129 -11
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +134 -19
- package/std/net/quic-stream.ts +174 -36
- package/std/net/quic.ts +418 -18
- package/std/net/router.ts +39 -17
- package/std/net/serve.ts +867 -0
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/websocket.ts +55 -18
- package/std/net/webtransport.ts +179 -1
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/secret.ts +26 -59
- package/std/text.ts +464 -5
package/std/crypto/p256.ts
CHANGED
|
@@ -46,14 +46,17 @@
|
|
|
46
46
|
* no arithmetic; it kept the input's pointer from being spilled in a way the
|
|
47
47
|
* aarch64 disassembly check once misread (#334). The scalar functions use the
|
|
48
48
|
* field's four helpers, which fiat generates identically for both. The nonce's
|
|
49
|
-
* inverse, `p256ScalarInverse`,
|
|
50
|
-
* function for function; the
|
|
51
|
-
* Everything else in this file — the
|
|
52
|
-
* and ECDSA — is written for
|
|
49
|
+
* inverse, `p256ScalarInverse`, and the field's, `p256FieldInvert`, are
|
|
50
|
+
* libsecp256k1's 32-bit safegcd (`modinv32`), function for function; the
|
|
51
|
+
* section that holds it says what changes. Everything else in this file — the
|
|
52
|
+
* curve, the encodings, verification's inversion and ECDSA — is written for
|
|
53
|
+
* this module from the documents it names.
|
|
53
54
|
*
|
|
54
55
|
* **The curve** is `y^2 = x^3 - 3x + b` over GF(p),
|
|
55
56
|
* p = 2^256 - 2^224 + 2^192 + 2^96 - 1, with the base point G and its order n from SEC 2 §2.4.2 (FIPS
|
|
56
|
-
* 186-4 §D.1.2.3).
|
|
57
|
+
* 186-4 §D.1.2.3).
|
|
58
|
+
*
|
|
59
|
+
* Points are projective (X : Y : Z), Z = 0 the identity, and
|
|
57
60
|
* are added and doubled with the complete formulas of J. Renes, C. Costello and
|
|
58
61
|
* L. Batina, "Complete addition formulas for prime order elliptic curves"
|
|
59
62
|
* (EUROCRYPT 2016), Algorithms 4 and 6 for a = -3. Complete means no input is
|
|
@@ -74,16 +77,18 @@
|
|
|
74
77
|
* none, and the sum is then discarded by the same conditional move rather than
|
|
75
78
|
* skipped, so neither a branch nor an address depends on the scalar.
|
|
76
79
|
* `p256ScalarMult`, a fixed 4-bit window over sixteen multiples of a point, is
|
|
77
|
-
* what
|
|
78
|
-
*
|
|
80
|
+
* what ECDH multiplies the peer's key by, and the reference the comb is held
|
|
81
|
+
* to bit for bit (`tests/link/crypto_p256_agree`).
|
|
79
82
|
*
|
|
80
83
|
* The signer inverts the nonce by Bernstein and Yang's safegcd,
|
|
81
84
|
* `p256ScalarInverse`: twenty batches of thirty divsteps, each a fixed run of
|
|
82
85
|
* masks and adds, whatever the scalar, so its loops count constants and no
|
|
83
86
|
* branch or address follows the nonce; Fermat's `p256ScalarInvert` is the
|
|
84
87
|
* reference it is held to bit for bit (`tests/link/crypto_p256_inverse`) and
|
|
85
|
-
* what verification still uses
|
|
86
|
-
*
|
|
88
|
+
* what verification still uses, raising to the public exponent n - 2, so its
|
|
89
|
+
* branches follow a constant. Every field inversion — of a Z that follows the
|
|
90
|
+
* key or the nonce in the public key, ECDH and signing — is the same safegcd
|
|
91
|
+
* modulo p, `p256FieldInvert`, on the same divstep and update functions.
|
|
87
92
|
*
|
|
88
93
|
* The branches that remain are on public facts: a length, a limb or bit
|
|
89
94
|
* position, whether a received key or signature is in range, and — once in
|
|
@@ -110,7 +115,12 @@
|
|
|
110
115
|
* limb loops among them), the encodings and the nonce derivation are constant
|
|
111
116
|
* time by construction rather than by that proof.
|
|
112
117
|
*
|
|
113
|
-
* Verification
|
|
118
|
+
* **Verification is variable time, on public values only.** `p256Verify`
|
|
119
|
+
* computes u1 G + u2 Q in one interleaved pass, `p256DoubleMultPublic`: both
|
|
120
|
+
* scalars recoded as width-5 wNAF, eight odd multiples of each point, the
|
|
121
|
+
* doublings shared, and both the digits and the table reads follow the
|
|
122
|
+
* scalars. Every input to it is the signature, the digest or the public key.
|
|
123
|
+
* Nothing secret may be passed to it.
|
|
114
124
|
*
|
|
115
125
|
* **The private key is a `Secret`** (`nish:secret`, docs/LANGUAGE.md
|
|
116
126
|
* "Secrets"). `p256PublicKey` and `p256Sign` read it only inside `expose` /
|
|
@@ -3750,25 +3760,6 @@ const p256Bit = (e: u32[], i: i32): boolean => {
|
|
|
3750
3760
|
return ((e[limb] >> toU32(i & 31)) & 1) === 1
|
|
3751
3761
|
}
|
|
3752
3762
|
|
|
3753
|
-
/**
|
|
3754
|
-
* `out = 1 / a` in the field, as `a^(p - 2)` (Fermat); zero for a zero `a`.
|
|
3755
|
-
* Square-and-multiply from the top bit: the branch is on the exponent's bits,
|
|
3756
|
-
* which are a constant.
|
|
3757
|
-
*/
|
|
3758
|
-
const p256FieldInvert = (out: u32[], a: u32[]): void => {
|
|
3759
|
-
const e: u32[] = p256Prime()
|
|
3760
|
-
e[0] = 0xfffffffd
|
|
3761
|
-
const acc: u32[] = p256Limbs()
|
|
3762
|
-
p256FiatSetOne(acc)
|
|
3763
|
-
for (let i: i32 = 255; i >= 0; i--) {
|
|
3764
|
-
p256FiatSquare(acc, acc)
|
|
3765
|
-
if (p256Bit(e, i)) {
|
|
3766
|
-
p256FiatMul(acc, acc, a)
|
|
3767
|
-
}
|
|
3768
|
-
}
|
|
3769
|
-
p256Copy(out, acc)
|
|
3770
|
-
}
|
|
3771
|
-
|
|
3772
3763
|
/**
|
|
3773
3764
|
* `out = 1 / a` mod n, as `a^(n - 2)` with the scalar Montgomery multiply;
|
|
3774
3765
|
* the exponent is public, so the square-and-multiply branches only on a
|
|
@@ -3789,14 +3780,14 @@ const p256ScalarInvert = (out: u32[], a: u32[]): void => {
|
|
|
3789
3780
|
p256Copy(out, acc)
|
|
3790
3781
|
}
|
|
3791
3782
|
|
|
3792
|
-
// ---- The nonce's
|
|
3783
|
+
// ---- The nonce's and the field's inverses by safegcd -------------------------
|
|
3793
3784
|
//
|
|
3794
3785
|
// Bernstein and Yang's constant-time inversion ("Fast constant-time gcd
|
|
3795
3786
|
// computation and modular inversion", 2019), as libsecp256k1's modinv32 writes
|
|
3796
3787
|
// it for 32-bit targets (MIT, std/crypto/LICENSE-libsecp256k1): the functions
|
|
3797
3788
|
// below are its secp256k1_modinv32_divsteps_30, _update_de_30, _update_fg_30,
|
|
3798
3789
|
// _normalize_30 and secp256k1_modinv32, with its scalar_8x32 conversions to and
|
|
3799
|
-
// from signed-30, ported to Nish for the
|
|
3790
|
+
// from signed-30, ported to Nish for the two moduli n and p. A value is nine limbs
|
|
3800
3791
|
// of 30 bits each, limb 8 carrying the sign, held as the u32 bit patterns of
|
|
3801
3792
|
// the C's int32_t: `u32` arithmetic wraps where the C's never overflows, so the
|
|
3802
3793
|
// low bits agree, and the arithmetic shifts go through `i32` and `i64`.
|
|
@@ -3925,12 +3916,13 @@ const p256Divsteps30 = (zeta: u32, f0: u32, g0: u32, t: u32[]): u32 => {
|
|
|
3925
3916
|
}
|
|
3926
3917
|
|
|
3927
3918
|
/**
|
|
3928
|
-
* `[d, e] = (t [d, e] + n [md, me]) / 2^30
|
|
3929
|
-
* that the division is exact: d and e stay in
|
|
3930
|
-
*
|
|
3931
|
-
*
|
|
3919
|
+
* `[d, e] = (t [d, e] + n [md, me]) / 2^30` for the modulus `n` (n or p),
|
|
3920
|
+
* with `md` and `me` chosen so that the division is exact: d and e stay in
|
|
3921
|
+
* (-2n, n) and keep tracking f and g modulo n
|
|
3922
|
+
* (`secp256k1_modinv32_update_de_30`). `inverse30` is n^-1 mod 2^30, which
|
|
3923
|
+
* makes the sum's bottom 30 bits zero. The products are 32 by 32 bits into 64.
|
|
3932
3924
|
*/
|
|
3933
|
-
const p256UpdateDe30 = (d: u32[], e: u32[], t: u32[], n: u32[]): void => {
|
|
3925
|
+
const p256UpdateDe30 = (d: u32[], e: u32[], t: u32[], n: u32[], inverse30: u32): void => {
|
|
3934
3926
|
if (toI32(d.length) < 9 || toI32(e.length) < 9 || toI32(t.length) < 4 || toI32(n.length) < 9) {
|
|
3935
3927
|
return
|
|
3936
3928
|
}
|
|
@@ -3938,8 +3930,6 @@ const p256UpdateDe30 = (d: u32[], e: u32[], t: u32[], n: u32[]): void => {
|
|
|
3938
3930
|
const v: u64 = p256Wide(t[1])
|
|
3939
3931
|
const q: u64 = p256Wide(t[2])
|
|
3940
3932
|
const r: u64 = p256Wide(t[3])
|
|
3941
|
-
// n^-1 mod 2^30, which makes the sum's bottom 30 bits zero below.
|
|
3942
|
-
const inverse30: u32 = 0x11ff43b1
|
|
3943
3933
|
// [md, me] start as [u, q] if d is negative plus [v, r] if e is.
|
|
3944
3934
|
const sd: u32 = p256SignMask(d[8])
|
|
3945
3935
|
const se: u32 = p256SignMask(e[8])
|
|
@@ -3998,8 +3988,8 @@ const p256UpdateFg30 = (f: u32[], g: u32[], t: u32[]): void => {
|
|
|
3998
3988
|
g[8] = toU32(cg)
|
|
3999
3989
|
}
|
|
4000
3990
|
|
|
4001
|
-
/** Add n to the signed-30 `a` when `mask` is all ones, then carry so every limb but the last is in [0, 2^30). */
|
|
4002
|
-
const
|
|
3991
|
+
/** Add the modulus `n` to the signed-30 `a` when `mask` is all ones, then carry so every limb but the last is in [0, 2^30). */
|
|
3992
|
+
const p256AddModulus30 = (a: u32[], n: u32[], mask: u32, negate: u32): void => {
|
|
4003
3993
|
if (toI32(a.length) < 9 || toI32(n.length) < 9) {
|
|
4004
3994
|
return
|
|
4005
3995
|
}
|
|
@@ -4013,48 +4003,52 @@ const p256AddOrder30 = (a: u32[], n: u32[], mask: u32, negate: u32): void => {
|
|
|
4013
4003
|
}
|
|
4014
4004
|
|
|
4015
4005
|
/**
|
|
4016
|
-
* `d` in (-2n, n) to [0, n)
|
|
4017
|
-
* ended as -1 rather than 1
|
|
4006
|
+
* `d` in (-2n, n) to [0, n) for the modulus `n`, negated first when `sign`'s
|
|
4007
|
+
* top bit is set: f ended as -1 rather than 1
|
|
4008
|
+
* (`secp256k1_modinv32_normalize_30`).
|
|
4018
4009
|
*/
|
|
4019
4010
|
const p256Normalize30 = (d: u32[], sign: u32, n: u32[]): void => {
|
|
4020
4011
|
if (toI32(d.length) < 9) {
|
|
4021
4012
|
return
|
|
4022
4013
|
}
|
|
4023
4014
|
const zero: u32 = 0
|
|
4024
|
-
|
|
4025
|
-
|
|
4015
|
+
p256AddModulus30(d, n, p256SignMask(d[8]), p256SignMask(sign))
|
|
4016
|
+
p256AddModulus30(d, n, p256SignMask(d[8]), zero)
|
|
4026
4017
|
}
|
|
4027
4018
|
|
|
4028
4019
|
/**
|
|
4029
|
-
* `out = 1 / a` mod
|
|
4030
|
-
*
|
|
4020
|
+
* `out = 1 / a` mod `m` for the plain limbs `a`, below m, and the odd
|
|
4021
|
+
* modulus `m` in signed-30 limbs (n or p), with `inverse30` = m^-1 mod 2^30;
|
|
4022
|
+
* zero for an `a` that is zero mod `m`. Twenty batches of thirty divsteps
|
|
4031
4023
|
* each: 600, past the 590 that bound any 256-bit input, so the count never
|
|
4032
4024
|
* depends on `a`, and every step and update runs the same instructions on any
|
|
4033
|
-
* value.
|
|
4034
|
-
* by R^3 takes `(a R)^-1` to `a^-1 R`. f, g, d, e and the matrix are wiped
|
|
4035
|
-
* before it returns (ECC-2).
|
|
4025
|
+
* value. f, g, d, e and the matrix are wiped before it returns (ECC-2).
|
|
4036
4026
|
*/
|
|
4037
|
-
const
|
|
4038
|
-
const n: u32[] = p256Order30()
|
|
4027
|
+
const p256Safegcd = (out: u32[], a: u32[], m: u32[], inverse30: u32): void => {
|
|
4039
4028
|
const d: u32[] = new Array<u32>(9)
|
|
4040
4029
|
const e: u32[] = new Array<u32>(9)
|
|
4041
|
-
const f: u32[] =
|
|
4030
|
+
const f: u32[] = new Array<u32>(9)
|
|
4042
4031
|
const g: u32[] = new Array<u32>(9)
|
|
4043
4032
|
const t: u32[] = new Array<u32>(4)
|
|
4033
|
+
if (toI32(m.length) < 9) {
|
|
4034
|
+
return
|
|
4035
|
+
}
|
|
4036
|
+
for (let i: i32 = 0; i < 9; i++) {
|
|
4037
|
+
f[i] = m[i]
|
|
4038
|
+
}
|
|
4044
4039
|
e[0] = 1
|
|
4045
4040
|
p256ToSigned30(g, a)
|
|
4046
4041
|
// zeta = -(delta + 1/2), delta starting at 1/2.
|
|
4047
4042
|
let zeta: u32 = 0xffffffff
|
|
4048
4043
|
for (let i: i32 = 0; i < 20; i++) {
|
|
4049
4044
|
zeta = p256Divsteps30(zeta, f[0], g[0], t)
|
|
4050
|
-
p256UpdateDe30(d, e, t,
|
|
4045
|
+
p256UpdateDe30(d, e, t, m, inverse30)
|
|
4051
4046
|
p256UpdateFg30(f, g, t)
|
|
4052
4047
|
}
|
|
4053
4048
|
// g is now 0 and f is 1 or -1, with d the inverse up to that sign; for a
|
|
4054
|
-
// zero `a`, f is
|
|
4055
|
-
p256Normalize30(d, f[8],
|
|
4049
|
+
// zero `a`, f is m and d stays 0.
|
|
4050
|
+
p256Normalize30(d, f[8], m)
|
|
4056
4051
|
p256FromSigned30(out, d)
|
|
4057
|
-
p256FiatScalarMul(out, out, p256ScalarR3())
|
|
4058
4052
|
wipe(d)
|
|
4059
4053
|
wipe(e)
|
|
4060
4054
|
wipe(f)
|
|
@@ -4062,6 +4056,51 @@ const p256ScalarInverse = (out: u32[], a: u32[]): void => {
|
|
|
4062
4056
|
wipe(t)
|
|
4063
4057
|
}
|
|
4064
4058
|
|
|
4059
|
+
/**
|
|
4060
|
+
* `out = 1 / a` mod n for `a` in the Montgomery domain, zero for a zero `a`:
|
|
4061
|
+
* the signing paths' inversion of the nonce, by `p256Safegcd`. The divsteps
|
|
4062
|
+
* invert `a R` as a plain number; one Montgomery multiply by R^3 takes
|
|
4063
|
+
* `(a R)^-1` to `a^-1 R`.
|
|
4064
|
+
*/
|
|
4065
|
+
const p256ScalarInverse = (out: u32[], a: u32[]): void => {
|
|
4066
|
+
p256Safegcd(out, a, p256Order30(), 0x11ff43b1)
|
|
4067
|
+
p256FiatScalarMul(out, out, p256ScalarR3())
|
|
4068
|
+
}
|
|
4069
|
+
|
|
4070
|
+
/** p in nine signed-30 limbs, limb 0 the least significant. */
|
|
4071
|
+
const p256Prime30 = (): u32[] => {
|
|
4072
|
+
const out: u32[] = new Array<u32>(9)
|
|
4073
|
+
out[0] = 0x3fffffff
|
|
4074
|
+
out[1] = 0x3fffffff
|
|
4075
|
+
out[2] = 0x3fffffff
|
|
4076
|
+
out[3] = 0x0000003f
|
|
4077
|
+
out[4] = 0
|
|
4078
|
+
out[5] = 0
|
|
4079
|
+
out[6] = 0x00001000
|
|
4080
|
+
out[7] = 0x3fffc000
|
|
4081
|
+
out[8] = 0x0000ffff
|
|
4082
|
+
return out
|
|
4083
|
+
}
|
|
4084
|
+
|
|
4085
|
+
/** R^3 mod p, which does for `p256FieldInvert` what `p256ScalarR3` does for the scalars. */
|
|
4086
|
+
const p256FieldR3 = (): u32[] => {
|
|
4087
|
+
const out: u32[] = p256Limbs()
|
|
4088
|
+
p256SetLimbs(out, 0xa, 0xfffffffd, 0xfffffff7, 0xffffffed, 0xfffffffc, 0x5, 0x1, 0x18)
|
|
4089
|
+
return out
|
|
4090
|
+
}
|
|
4091
|
+
|
|
4092
|
+
/**
|
|
4093
|
+
* `out = 1 / a` in the field for `a` in the Montgomery domain, zero for a
|
|
4094
|
+
* zero `a`, by `p256Safegcd` modulo p (p^-1 mod 2^30 is 2^30 - 1, as p is
|
|
4095
|
+
* -1 mod 2^96) and a Montgomery multiply by R^3. Constant time: what it
|
|
4096
|
+
* inverts is a Z that follows a secret scalar in signing, ECDH and the public
|
|
4097
|
+
* key. `out` may be `a`.
|
|
4098
|
+
*/
|
|
4099
|
+
const p256FieldInvert = (out: u32[], a: u32[]): void => {
|
|
4100
|
+
p256Safegcd(out, a, p256Prime30(), 0x3fffffff)
|
|
4101
|
+
p256FiatMul(out, out, p256FieldR3())
|
|
4102
|
+
}
|
|
4103
|
+
|
|
4065
4104
|
/**
|
|
4066
4105
|
* A point in projective coordinates (X : Y : Z), each coordinate a field
|
|
4067
4106
|
* element in the Montgomery domain. It stands for the affine (X/Z, Y/Z), and
|
|
@@ -5319,9 +5358,9 @@ export const p256Ecdh = (priv: Secret<u8[]>, peerPoint: u8[]): Secret<u8[]> | nu
|
|
|
5319
5358
|
|
|
5320
5359
|
/**
|
|
5321
5360
|
* `p256Ecdh` on the plain key, for `exposeWith` to run. The scratch, the
|
|
5322
|
-
* product and every limb of `X / Z` follow the key, so all are wiped;
|
|
5323
|
-
*
|
|
5324
|
-
*
|
|
5361
|
+
* product and every limb of `X / Z` follow the key, so all are wiped; Z is
|
|
5362
|
+
* inverted here, rather than through `p256AffineX`, so that its inverse is this
|
|
5363
|
+
* function's to wipe (`p256FieldInvert` wipes its own intermediates).
|
|
5325
5364
|
*/
|
|
5326
5365
|
const p256EcdhExposed = (priv: u8[], peerPoint: u8[]): u8[] | null => {
|
|
5327
5366
|
if (!p256ScalarInRange(priv)) {
|
|
@@ -5336,17 +5375,9 @@ const p256EcdhExposed = (priv: u8[], peerPoint: u8[]): u8[] | null => {
|
|
|
5336
5375
|
p256ScalarMult(point, priv, peer, scratch)
|
|
5337
5376
|
p256WipeScratch(scratch)
|
|
5338
5377
|
const infinity: boolean = p256FiatNonzero(point.z) === 0
|
|
5339
|
-
// 1 / Z
|
|
5340
|
-
const e: u32[] = p256Prime()
|
|
5341
|
-
e[0] = 0xfffffffd
|
|
5378
|
+
// 1 / Z, over limbs wiped below.
|
|
5342
5379
|
const acc: u32[] = p256Limbs()
|
|
5343
|
-
|
|
5344
|
-
for (let i: i32 = 255; i >= 0; i--) {
|
|
5345
|
-
p256FiatSquare(acc, acc)
|
|
5346
|
-
if (p256Bit(e, i)) {
|
|
5347
|
-
p256FiatMul(acc, acc, point.z)
|
|
5348
|
-
}
|
|
5349
|
-
}
|
|
5380
|
+
p256FieldInvert(acc, point.z)
|
|
5350
5381
|
const x: u32[] = p256Limbs()
|
|
5351
5382
|
p256FiatMul(x, point.x, acc)
|
|
5352
5383
|
p256FiatMul(x, x, p256PlainOne())
|
|
@@ -5387,6 +5418,148 @@ const p256SignExposed = (priv: u8[], digest: u8[]): u8[] | null => {
|
|
|
5387
5418
|
return p256SignScalar(priv, z)
|
|
5388
5419
|
}
|
|
5389
5420
|
|
|
5421
|
+
/** The width of `p256Verify`'s window: its digits are odd, in [-15, 15]. */
|
|
5422
|
+
const P256_WNAF_WIDTH: i32 = 5
|
|
5423
|
+
|
|
5424
|
+
/** The bits of a scalar `p256Wnaf` reads; its form has one more digit, for the last carry. */
|
|
5425
|
+
const P256_WNAF_BITS: i32 = 256
|
|
5426
|
+
|
|
5427
|
+
/** The number of odd multiples `p256OddMultiples` builds: 1, 3, ..., 15 times a point. */
|
|
5428
|
+
const P256_WNAF_TABLE: i32 = 8
|
|
5429
|
+
|
|
5430
|
+
/**
|
|
5431
|
+
* The width-5 non-adjacent form of the 32 big-endian bytes `k`: `out[i]`, for
|
|
5432
|
+
* `i` from 0 to 256, is zero or an odd digit in [-15, 15], `k` is the sum of
|
|
5433
|
+
* `out[i] * 2^i`, and of any five digits in a row at most one is nonzero. Read
|
|
5434
|
+
* from the bottom bit up, a window opening at each bit that differs from the
|
|
5435
|
+
* carry (libsecp256k1's `secp256k1_ecmult_wnaf`). Variable time: `k` is
|
|
5436
|
+
* public wherever this is used.
|
|
5437
|
+
*/
|
|
5438
|
+
const p256Wnaf = (out: i32[], k: u8[]): void => {
|
|
5439
|
+
if (toI32(out.length) <= P256_WNAF_BITS || toI32(k.length) < 32) {
|
|
5440
|
+
return
|
|
5441
|
+
}
|
|
5442
|
+
let carry: i32 = 0
|
|
5443
|
+
let bit: i32 = 0
|
|
5444
|
+
while (bit < P256_WNAF_BITS) {
|
|
5445
|
+
if (p256ByteBit(k, bit) === carry) {
|
|
5446
|
+
bit++
|
|
5447
|
+
continue
|
|
5448
|
+
}
|
|
5449
|
+
const left: i32 = P256_WNAF_BITS - bit
|
|
5450
|
+
const now: i32 = left < P256_WNAF_WIDTH ? left : P256_WNAF_WIDTH
|
|
5451
|
+
let word: i32 = carry
|
|
5452
|
+
for (let j: i32 = 0; j < now; j++) {
|
|
5453
|
+
word = word + (p256ByteBit(k, bit + j) << j)
|
|
5454
|
+
}
|
|
5455
|
+
carry = (word >> (P256_WNAF_WIDTH - 1)) & 1
|
|
5456
|
+
if (bit >= 0 && bit < toI32(out.length)) {
|
|
5457
|
+
out[bit] = word - (carry << P256_WNAF_WIDTH)
|
|
5458
|
+
}
|
|
5459
|
+
bit = bit + now
|
|
5460
|
+
}
|
|
5461
|
+
out[P256_WNAF_BITS] = carry
|
|
5462
|
+
}
|
|
5463
|
+
|
|
5464
|
+
/** Bit `i` of the 32 big-endian bytes `k`, 0 the least significant. */
|
|
5465
|
+
const p256ByteBit = (k: u8[], i: i32): i32 => {
|
|
5466
|
+
const at: i32 = (P256_WNAF_BITS >> 3) - 1 - (i >> 3)
|
|
5467
|
+
if (at < 0 || at >= toI32(k.length)) {
|
|
5468
|
+
return 0
|
|
5469
|
+
}
|
|
5470
|
+
return toI32((toU32(k[at]) >> toU32(i & 7)) & 1)
|
|
5471
|
+
}
|
|
5472
|
+
|
|
5473
|
+
/** `table[j] = (2 j + 1) p` for each `j` in `table`, eight of them: one doubling and seven additions. */
|
|
5474
|
+
const p256OddMultiples = (
|
|
5475
|
+
table: P256ProjectivePoint[],
|
|
5476
|
+
p: P256ProjectivePoint,
|
|
5477
|
+
s: P256PointScratch
|
|
5478
|
+
): void => {
|
|
5479
|
+
if (toI32(table.length) < P256_WNAF_TABLE) {
|
|
5480
|
+
return
|
|
5481
|
+
}
|
|
5482
|
+
const twice = new P256ProjectivePoint()
|
|
5483
|
+
p256PointDouble(twice, p, s)
|
|
5484
|
+
p256PointCopy(table[0], p)
|
|
5485
|
+
for (let j: i32 = 1; j < toI32(table.length); j++) {
|
|
5486
|
+
p256PointAdd(table[j], table[j - 1], twice, s)
|
|
5487
|
+
}
|
|
5488
|
+
}
|
|
5489
|
+
|
|
5490
|
+
/**
|
|
5491
|
+
* `acc += digit * p`, `digit` odd in [-15, 15], read from `p256OddMultiples`'
|
|
5492
|
+
* `table`; a negative digit adds the entry's negation (X : -Y : Z), built in
|
|
5493
|
+
* `neg`. Variable time: which entry is read, and its sign, follow the digit.
|
|
5494
|
+
*/
|
|
5495
|
+
const p256WnafAdd = (
|
|
5496
|
+
acc: P256ProjectivePoint,
|
|
5497
|
+
table: P256ProjectivePoint[],
|
|
5498
|
+
digit: i32,
|
|
5499
|
+
neg: P256ProjectivePoint,
|
|
5500
|
+
s: P256PointScratch
|
|
5501
|
+
): void => {
|
|
5502
|
+
const at: i32 = (digit < 0 ? -digit : digit) >> 1
|
|
5503
|
+
if (at < 0 || at >= toI32(table.length)) {
|
|
5504
|
+
return
|
|
5505
|
+
}
|
|
5506
|
+
if (digit > 0) {
|
|
5507
|
+
p256PointAdd(acc, acc, table[at], s)
|
|
5508
|
+
return
|
|
5509
|
+
}
|
|
5510
|
+
p256Copy(neg.x, table[at].x)
|
|
5511
|
+
p256SetZero(neg.y)
|
|
5512
|
+
p256FiatSub(neg.y, neg.y, table[at].y)
|
|
5513
|
+
p256Copy(neg.z, table[at].z)
|
|
5514
|
+
p256PointAdd(acc, acc, neg, s)
|
|
5515
|
+
}
|
|
5516
|
+
|
|
5517
|
+
/**
|
|
5518
|
+
* `out = a * G + b * q` for 32 big-endian bytes `a` and `b`, by Shamir's trick
|
|
5519
|
+
* over width-5 NAFs: one shared chain of doublings from the top digit down,
|
|
5520
|
+
* and an addition from the odd multiples of G or of `q` wherever a digit is
|
|
5521
|
+
* nonzero. The doublings before the first nonzero digit are skipped. **Not
|
|
5522
|
+
* constant time**: the additions, their table reads and their signs follow
|
|
5523
|
+
* the scalars' digits, so this is only for public scalars and points, as in
|
|
5524
|
+
* `p256Verify`.
|
|
5525
|
+
*/
|
|
5526
|
+
const p256DoubleMultPublic = (
|
|
5527
|
+
out: P256ProjectivePoint,
|
|
5528
|
+
a: u8[],
|
|
5529
|
+
b: u8[],
|
|
5530
|
+
q: P256ProjectivePoint,
|
|
5531
|
+
s: P256PointScratch
|
|
5532
|
+
): void => {
|
|
5533
|
+
const wa: i32[] = new Array<i32>(P256_WNAF_BITS + 1)
|
|
5534
|
+
const wb: i32[] = new Array<i32>(P256_WNAF_BITS + 1)
|
|
5535
|
+
p256Wnaf(wa, a)
|
|
5536
|
+
p256Wnaf(wb, b)
|
|
5537
|
+
const tableG: P256ProjectivePoint[] = []
|
|
5538
|
+
const tableQ: P256ProjectivePoint[] = []
|
|
5539
|
+
for (let j: i32 = 0; j < P256_WNAF_TABLE; j++) {
|
|
5540
|
+
tableG.push(new P256ProjectivePoint())
|
|
5541
|
+
tableQ.push(new P256ProjectivePoint())
|
|
5542
|
+
}
|
|
5543
|
+
p256OddMultiples(tableG, p256Generator(), s)
|
|
5544
|
+
p256OddMultiples(tableQ, q, s)
|
|
5545
|
+
const neg = new P256ProjectivePoint()
|
|
5546
|
+
p256SetIdentity(out)
|
|
5547
|
+
let started: boolean = false
|
|
5548
|
+
for (let i: i32 = P256_WNAF_BITS; i >= 0; i--) {
|
|
5549
|
+
if (started) {
|
|
5550
|
+
p256PointDouble(out, out, s)
|
|
5551
|
+
}
|
|
5552
|
+
if (i < toI32(wa.length) && wa[i] !== 0) {
|
|
5553
|
+
p256WnafAdd(out, tableG, wa[i], neg, s)
|
|
5554
|
+
started = true
|
|
5555
|
+
}
|
|
5556
|
+
if (i < toI32(wb.length) && wb[i] !== 0) {
|
|
5557
|
+
p256WnafAdd(out, tableQ, wb[i], neg, s)
|
|
5558
|
+
started = true
|
|
5559
|
+
}
|
|
5560
|
+
}
|
|
5561
|
+
}
|
|
5562
|
+
|
|
5390
5563
|
/**
|
|
5391
5564
|
* Whether `sig` (`r || s`) is a valid ECDSA signature of `digest` under the
|
|
5392
5565
|
* public key `pub` (FIPS 186-4 §6.4.2): `r` and `s` in [1, n), and the
|
|
@@ -5394,7 +5567,10 @@ const p256SignExposed = (priv: u8[], digest: u8[]): u8[] | null => {
|
|
|
5394
5567
|
* `u1 = z w` and `u2 = r w`. A key or signature of the wrong length, a key
|
|
5395
5568
|
* that is not a point on the curve, and a sum that is the identity all answer
|
|
5396
5569
|
* `false`. Every value here is public, so the answer may be reached by
|
|
5397
|
-
* branches
|
|
5570
|
+
* branches, and `u1 * G + u2 * Q` is one variable-time Shamir pass over
|
|
5571
|
+
* width-5 NAFs (`p256DoubleMultPublic`), not two constant-time
|
|
5572
|
+
* multiplications: its time follows `u1` and `u2`, which the signature and
|
|
5573
|
+
* digest already disclose.
|
|
5398
5574
|
*
|
|
5399
5575
|
* ECDSA is malleable, and so is this: when `(r, s)` verifies, `(r, n - s)`
|
|
5400
5576
|
* verifies too, as FIPS 186-4 and SEC 1 have it. Nothing here asks for the
|
|
@@ -5446,10 +5622,7 @@ export const p256Verify = (pub: u8[], digest: u8[], sig: u8[]): boolean => {
|
|
|
5446
5622
|
|
|
5447
5623
|
const scratch = new P256PointScratch()
|
|
5448
5624
|
const sum = new P256ProjectivePoint()
|
|
5449
|
-
|
|
5450
|
-
p256ScalarMult(sum, u1Bytes, p256Generator(), scratch)
|
|
5451
|
-
p256ScalarMult(other, u2Bytes, q, scratch)
|
|
5452
|
-
p256PointAdd(sum, sum, other, scratch)
|
|
5625
|
+
p256DoubleMultPublic(sum, u1Bytes, u2Bytes, q, scratch)
|
|
5453
5626
|
if (p256FiatNonzero(sum.z) === 0) {
|
|
5454
5627
|
return false
|
|
5455
5628
|
}
|
package/std/crypto/p384.ts
CHANGED
|
@@ -48,7 +48,9 @@
|
|
|
48
48
|
*
|
|
49
49
|
* **The curve** is `y^2 = x^3 - 3x + b` over GF(p),
|
|
50
50
|
* p = 2^384 - 2^128 - 2^96 + 2^32 - 1, with the base point G and its order n from SEC 2 §2.5.1 (FIPS
|
|
51
|
-
* 186-4 §D.1.2.4).
|
|
51
|
+
* 186-4 §D.1.2.4).
|
|
52
|
+
*
|
|
53
|
+
* Points are projective (X : Y : Z), Z = 0 the identity, and
|
|
52
54
|
* are added and doubled with the complete formulas of J. Renes, C. Costello and
|
|
53
55
|
* L. Batina, "Complete addition formulas for prime order elliptic curves"
|
|
54
56
|
* (EUROCRYPT 2016), Algorithms 4 and 6 for a = -3, as `nish/crypto/p256` uses
|
package/std/crypto/pki.ts
CHANGED
|
@@ -50,10 +50,12 @@
|
|
|
50
50
|
* a root: one that reached a root beats any that did not, a longer one a
|
|
51
51
|
* shorter, and the first found a later one of the same length.
|
|
52
52
|
*
|
|
53
|
+
* An extension marked against RFC 5280 §4.2 refuses the chain, but for a
|
|
54
|
+
* non-critical nameConstraints, which binds as a critical one does.
|
|
53
55
|
* Revocation (CRLs, OCSP, Certificate Transparency), certificate policies,
|
|
54
|
-
* RFC 5280 §7.1's Name folding and
|
|
55
|
-
*
|
|
56
|
-
*
|
|
56
|
+
* RFC 5280 §7.1's Name folding and that nameConstraints are not checked;
|
|
57
|
+
* each is a finding of docs/security/crypto-pki.md. Nothing here is secret —
|
|
58
|
+
* certificates, names and public keys — so nothing is wiped.
|
|
57
59
|
*
|
|
58
60
|
* Written for this module from RFC 5280 (§4.2 and §6.1), RFC 6125 (§6.4),
|
|
59
61
|
* RFC 4291 §2.2 and RFC 3986 §3.2.2 (address literals), RFC 5758, RFC 8017
|
|
@@ -852,12 +854,16 @@ const pkiTimeCode = (c: X509Certificate, nowMs: i64): i32 => {
|
|
|
852
854
|
* The key identifiers an issuer `cand` must carry (RFC 5280 §4.2.1.1 and
|
|
853
855
|
* §4.2.1.2), as a code: a subjectKeyIdentifier, which a root may lack, as
|
|
854
856
|
* one Mozilla ships does, and an authorityKeyIdentifier unless it is
|
|
855
|
-
* self-issued, even on a root. A
|
|
856
|
-
*
|
|
857
|
+
* self-issued, even on a root. A root's authorityKeyIdentifier, when
|
|
858
|
+
* present, has a keyIdentifier and no authorityCertIssuer or serial, as
|
|
859
|
+
* webpki holds it; a self-issued root's is its own subjectKeyIdentifier.
|
|
857
860
|
*/
|
|
858
861
|
const pkiKeyIdsCode = (cand: X509Certificate, root: boolean): i32 => {
|
|
859
862
|
const skid: u8[] | null = cand.skid
|
|
860
863
|
const akid: u8[] | null = cand.akid
|
|
864
|
+
if (root && cand.akiPresent && (akid === null || cand.akiIssuerSerial)) {
|
|
865
|
+
return PKI_ERR_MALFORMED
|
|
866
|
+
}
|
|
861
867
|
if (skid === null) {
|
|
862
868
|
return root ? PKI_OK : PKI_ERR_MALFORMED
|
|
863
869
|
}
|
|
@@ -867,10 +873,18 @@ const pkiKeyIdsCode = (cand: X509Certificate, root: boolean): i32 => {
|
|
|
867
873
|
return root && akid !== null && !pkiBytesEqual(akid, skid) ? PKI_ERR_MALFORMED : PKI_OK
|
|
868
874
|
}
|
|
869
875
|
|
|
876
|
+
/**
|
|
877
|
+
* Whether the extensions RFC 5280 fixes the criticality of on every
|
|
878
|
+
* certificate are marked as it has them: authorityKeyIdentifier not critical
|
|
879
|
+
* (§4.2.1.1), policyConstraints critical (§4.2.1.11).
|
|
880
|
+
*/
|
|
881
|
+
const pkiCriticalitySound = (c: X509Certificate): boolean => !c.akiCritical && (!c.pcPresent || c.pcCritical)
|
|
882
|
+
|
|
870
883
|
/**
|
|
871
884
|
* The checks of the issuer `cand` added above the path, a root of the store
|
|
872
885
|
* when `root` holds and an intermediate otherwise: its validity, its critical
|
|
873
|
-
* extensions
|
|
886
|
+
* extensions and the criticality RFC 5280 fixes, among them a critical
|
|
887
|
+
* basicConstraints (§4.2.1.9), that it is a CA, its extKeyUsage, its name constraints, its
|
|
874
888
|
* signature over `child` — the only check that costs — and, once that
|
|
875
889
|
* verifies, its key identifiers, so a wrong key reads as a signature. An
|
|
876
890
|
* intermediate also needs room for a root above it and a keyUsage that
|
|
@@ -893,6 +907,12 @@ const pkiIssuerCode = (s: PkiSearch, child: X509Certificate, cand: X509Certifica
|
|
|
893
907
|
if (cand.version !== 2 || !cand.bcPresent || !cand.bcCa) {
|
|
894
908
|
return PKI_ERR_NOT_CA
|
|
895
909
|
}
|
|
910
|
+
// The root too: limbo refuses a root with a non-critical basicConstraints,
|
|
911
|
+
// and every root of Mozilla's store is marked as these rules ask
|
|
912
|
+
// (crypto-pki.md, "Each structural rule on a root").
|
|
913
|
+
if (!cand.bcCritical || !pkiCriticalitySound(cand)) {
|
|
914
|
+
return PKI_ERR_MALFORMED
|
|
915
|
+
}
|
|
896
916
|
if (!root && cand.keyUsage >= 0 && (cand.keyUsage & X509_KU_KEY_CERT_SIGN) === 0) {
|
|
897
917
|
return PKI_ERR_KEY_USAGE
|
|
898
918
|
}
|
|
@@ -1042,7 +1062,9 @@ const pkiSubscriberKeyCode = (leaf: X509Certificate): i32 => {
|
|
|
1042
1062
|
* The checks of the leaf's profile, once a path above it is found: no keyCertSign
|
|
1043
1063
|
* and no nameConstraints, no `anyExtendedKeyUsage`, a key a server may
|
|
1044
1064
|
* hold, a sound serial number, an authorityKeyIdentifier unless it is
|
|
1045
|
-
* self-issued,
|
|
1065
|
+
* self-issued, the criticality RFC 5280 fixes, a subjectAltName critical
|
|
1066
|
+
* exactly when the subject is empty (§4.2.1.6), an extKeyUsage that is not
|
|
1067
|
+
* critical, as webpki holds it, and dNSNames of letters, digits and hyphens. The common name
|
|
1046
1068
|
* is not read: limbo's own passing cases carry ones their subjectAltName
|
|
1047
1069
|
* lacks.
|
|
1048
1070
|
*/
|
|
@@ -1053,14 +1075,18 @@ const pkiLeafProfileCode = (leaf: X509Certificate): i32 => {
|
|
|
1053
1075
|
if (leaf.ncPresent) {
|
|
1054
1076
|
return PKI_ERR_NAME_CONSTRAINTS
|
|
1055
1077
|
}
|
|
1056
|
-
if (leaf.ekuAny) {
|
|
1078
|
+
if (leaf.ekuAny || leaf.ekuCritical) {
|
|
1057
1079
|
return PKI_ERR_EXT_KEY_USAGE
|
|
1058
1080
|
}
|
|
1059
1081
|
const key: i32 = pkiSubscriberKeyCode(leaf)
|
|
1060
1082
|
if (key !== PKI_OK) {
|
|
1061
1083
|
return key
|
|
1062
1084
|
}
|
|
1063
|
-
if (!pkiSerialSound(leaf) || (leaf.akid === null && !pkiSelfIssued(leaf))) {
|
|
1085
|
+
if (!pkiSerialSound(leaf) || (leaf.akid === null && !pkiSelfIssued(leaf)) || !pkiCriticalitySound(leaf)) {
|
|
1086
|
+
return PKI_ERR_MALFORMED
|
|
1087
|
+
}
|
|
1088
|
+
// An empty subject is the empty SEQUENCE, `30 00`.
|
|
1089
|
+
if (leaf.sanPresent && leaf.sanCritical !== (leaf.subject.length === 2)) {
|
|
1064
1090
|
return PKI_ERR_MALFORMED
|
|
1065
1091
|
}
|
|
1066
1092
|
for (const d of leaf.sanDns) {
|