@amritk/nish-aarch64-linux 0.38.0 → 0.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +74 -129
- package/bin/nish +0 -0
- package/docs/AI.md +455 -65
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +320 -33
- package/runtime/nish.h +84 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +97 -12
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +64 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +404 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +437 -0
- package/runtime/runtime.c +5 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +42 -8
- package/std/README.md +78 -91
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1629 -2732
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +151 -13
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +120 -10
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +344 -111
- package/std/json-typed.ts +256 -12
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/handler.ts +130 -16
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +636 -162
- package/std/net/http1-server.ts +569 -42
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +131 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- 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 +373 -27
- package/std/net/quic-stream.ts +195 -37
- package/std/net/quic.ts +516 -22
- package/std/net/router.ts +407 -38
- package/std/net/serve.ts +1022 -110
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls/verify.ts +19 -1
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +129 -19
- package/std/net/webtransport.ts +185 -1
- package/std/raster.ts +837 -0
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/release.ts +593 -0
- package/std/secret.ts +26 -59
- package/std/synth.ts +143 -0
- package/std/text.ts +233 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
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
|
|
@@ -142,6 +144,11 @@ export const PKI_ERR_UNSUPPORTED_ALGORITHM: i32 = 14
|
|
|
142
144
|
export const PKI_ERR_CHAIN_TOO_LONG: i32 = 15
|
|
143
145
|
/** The chain asks for more work than one call does: signatures, names or subtrees. */
|
|
144
146
|
export const PKI_ERR_BUDGET: i32 = 16
|
|
147
|
+
/**
|
|
148
|
+
* A certificate on the path has a key the caller distrusts: the SHA-256 of
|
|
149
|
+
* its subjectPublicKeyInfo is one `pkiDistrustSpki` added to the store.
|
|
150
|
+
*/
|
|
151
|
+
export const PKI_ERR_DISTRUSTED: i32 = 17
|
|
145
152
|
|
|
146
153
|
/** The most certificates a chain may hold, the leaf among them. */
|
|
147
154
|
export const PKI_MAX_CHAIN_CERTS: i32 = 128
|
|
@@ -169,11 +176,14 @@ export class PkiTrustStore {
|
|
|
169
176
|
roots: X509Certificate[]
|
|
170
177
|
/** @internal The roots under each subject, keyed by the subject's DER in hex. */
|
|
171
178
|
bySubject: Map<string, X509Certificate[]>
|
|
179
|
+
/** @internal The SHA-256 of each distrusted subjectPublicKeyInfo, in hex; empty unless `pkiDistrustSpki` filled it. */
|
|
180
|
+
distrusted: Map<string, boolean>
|
|
172
181
|
|
|
173
182
|
/** An empty store; `pkiTrustStore` builds a filled one. */
|
|
174
183
|
constructor() {
|
|
175
184
|
this.roots = []
|
|
176
185
|
this.bySubject = new Map<string, X509Certificate[]>()
|
|
186
|
+
this.distrusted = new Map<string, boolean>()
|
|
177
187
|
}
|
|
178
188
|
}
|
|
179
189
|
|
|
@@ -738,7 +748,70 @@ const pkiIpWithin = (ip: u8[], t: X509GeneralSubtree): boolean => {
|
|
|
738
748
|
return true
|
|
739
749
|
}
|
|
740
750
|
|
|
741
|
-
/** Whether
|
|
751
|
+
/** Whether the first 12 octets of `a` are `::ffff:`, the prefix of an IPv4-mapped IPv6 address. */
|
|
752
|
+
const pkiIpMappedPrefix = (a: u8[]): boolean => {
|
|
753
|
+
if (toI32(a.length) < 12) {
|
|
754
|
+
return false
|
|
755
|
+
}
|
|
756
|
+
for (let k: i32 = 0; k < 12 && k < toI32(a.length); k++) {
|
|
757
|
+
if (toI32(a[k]) !== (k < 10 ? 0 : 0xff)) {
|
|
758
|
+
return false
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
return true
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/** Whether `ip`'s four octets from `at` equal `t`'s from `from` under `t`'s mask. */
|
|
765
|
+
const pkiIpV4Within = (ip: u8[], at: i32, t: X509GeneralSubtree, from: i32): boolean => {
|
|
766
|
+
for (let k: i32 = 0; k < 4; k++) {
|
|
767
|
+
const a: i32 = at + k
|
|
768
|
+
const b: i32 = from + k
|
|
769
|
+
if (a < 0 || a >= toI32(ip.length) || b < 0 || b >= toI32(t.ip.length) || b >= toI32(t.mask.length)) {
|
|
770
|
+
return false
|
|
771
|
+
}
|
|
772
|
+
if ((toI32(ip[a]) & toI32(t.mask[b])) !== (toI32(t.ip[b]) & toI32(t.mask[b]))) {
|
|
773
|
+
return false
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return true
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Whether the excluded iPAddress subtree `t` holds `ip`: as `pkiIpWithin`,
|
|
781
|
+
* and across the families for an IPv4 address and its IPv4-mapped IPv6 form
|
|
782
|
+
* (`::ffff:a.b.c.d`), which name the same host. An IPv4 subtree holds the
|
|
783
|
+
* mapped form of every address it holds; an IPv6 subtree whose mask covers
|
|
784
|
+
* the whole `::ffff:0:0/96` prefix holds the IPv4 addresses it maps. A
|
|
785
|
+
* wider IPv6 subtree, such as `::/0`, holds no IPv4 address (PKI-33).
|
|
786
|
+
*/
|
|
787
|
+
const pkiIpExcludedBy = (ip: u8[], t: X509GeneralSubtree): boolean => {
|
|
788
|
+
if (pkiIpWithin(ip, t)) {
|
|
789
|
+
return true
|
|
790
|
+
}
|
|
791
|
+
const n: i32 = toI32(ip.length)
|
|
792
|
+
const tn: i32 = toI32(t.ip.length)
|
|
793
|
+
if (tn !== toI32(t.mask.length)) {
|
|
794
|
+
return false
|
|
795
|
+
}
|
|
796
|
+
if (n === 16 && tn === 4) {
|
|
797
|
+
return pkiIpMappedPrefix(ip) && pkiIpV4Within(ip, 12, t, 0)
|
|
798
|
+
}
|
|
799
|
+
if (n === 4 && tn === 16) {
|
|
800
|
+
for (let k: i32 = 0; k < 12 && k < toI32(t.mask.length); k++) {
|
|
801
|
+
if (toI32(t.mask[k]) !== 0xff) {
|
|
802
|
+
return false
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
return pkiIpMappedPrefix(t.ip) && pkiIpV4Within(ip, 0, t, 12)
|
|
806
|
+
}
|
|
807
|
+
return false
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* Whether `issuer`'s iPAddress constraints allow `ip`. A permitted subtree
|
|
812
|
+
* of the other family never holds it; an excluded one holds an address's
|
|
813
|
+
* IPv4-mapped twin as `pkiIpExcludedBy` says, so the exclusion fails closed.
|
|
814
|
+
*/
|
|
742
815
|
const pkiIpAllowed = (issuer: X509Certificate, ip: u8[]): boolean => {
|
|
743
816
|
let listed: boolean = false
|
|
744
817
|
let inside: boolean = false
|
|
@@ -754,7 +827,7 @@ const pkiIpAllowed = (issuer: X509Certificate, ip: u8[]): boolean => {
|
|
|
754
827
|
return false
|
|
755
828
|
}
|
|
756
829
|
for (const e of issuer.ncExcluded) {
|
|
757
|
-
if (e.kind === X509_GN_IP &&
|
|
830
|
+
if (e.kind === X509_GN_IP && pkiIpExcludedBy(ip, e)) {
|
|
758
831
|
return false
|
|
759
832
|
}
|
|
760
833
|
}
|
|
@@ -852,12 +925,16 @@ const pkiTimeCode = (c: X509Certificate, nowMs: i64): i32 => {
|
|
|
852
925
|
* The key identifiers an issuer `cand` must carry (RFC 5280 §4.2.1.1 and
|
|
853
926
|
* §4.2.1.2), as a code: a subjectKeyIdentifier, which a root may lack, as
|
|
854
927
|
* one Mozilla ships does, and an authorityKeyIdentifier unless it is
|
|
855
|
-
* self-issued, even on a root. A
|
|
856
|
-
*
|
|
928
|
+
* self-issued, even on a root. A root's authorityKeyIdentifier, when
|
|
929
|
+
* present, has a keyIdentifier and no authorityCertIssuer or serial, as
|
|
930
|
+
* webpki holds it; a self-issued root's is its own subjectKeyIdentifier.
|
|
857
931
|
*/
|
|
858
932
|
const pkiKeyIdsCode = (cand: X509Certificate, root: boolean): i32 => {
|
|
859
933
|
const skid: u8[] | null = cand.skid
|
|
860
934
|
const akid: u8[] | null = cand.akid
|
|
935
|
+
if (root && cand.akiPresent && (akid === null || cand.akiIssuerSerial)) {
|
|
936
|
+
return PKI_ERR_MALFORMED
|
|
937
|
+
}
|
|
861
938
|
if (skid === null) {
|
|
862
939
|
return root ? PKI_OK : PKI_ERR_MALFORMED
|
|
863
940
|
}
|
|
@@ -867,10 +944,19 @@ const pkiKeyIdsCode = (cand: X509Certificate, root: boolean): i32 => {
|
|
|
867
944
|
return root && akid !== null && !pkiBytesEqual(akid, skid) ? PKI_ERR_MALFORMED : PKI_OK
|
|
868
945
|
}
|
|
869
946
|
|
|
947
|
+
/**
|
|
948
|
+
* Whether the extensions RFC 5280 fixes the criticality of on every
|
|
949
|
+
* certificate are marked as it has them: authorityKeyIdentifier not critical
|
|
950
|
+
* (§4.2.1.1), policyConstraints critical (§4.2.1.11).
|
|
951
|
+
*/
|
|
952
|
+
const pkiCriticalitySound = (c: X509Certificate): boolean => !c.akiCritical && (!c.pcPresent || c.pcCritical)
|
|
953
|
+
|
|
870
954
|
/**
|
|
871
955
|
* The checks of the issuer `cand` added above the path, a root of the store
|
|
872
|
-
* when `root` holds and an intermediate otherwise:
|
|
873
|
-
*
|
|
956
|
+
* when `root` holds and an intermediate otherwise: that the store does not
|
|
957
|
+
* distrust its key (`pkiDistrustSpki`), its validity, its critical
|
|
958
|
+
* extensions and the criticality RFC 5280 fixes, among them a critical
|
|
959
|
+
* basicConstraints (§4.2.1.9), that it is a CA, its extKeyUsage, its name constraints, its
|
|
874
960
|
* signature over `child` — the only check that costs — and, once that
|
|
875
961
|
* verifies, its key identifiers, so a wrong key reads as a signature. An
|
|
876
962
|
* intermediate also needs room for a root above it and a keyUsage that
|
|
@@ -883,6 +969,9 @@ const pkiIssuerCode = (s: PkiSearch, child: X509Certificate, cand: X509Certifica
|
|
|
883
969
|
if (!root && toI32(s.path.length) + 2 > PKI_MAX_PATH) {
|
|
884
970
|
return PKI_ERR_CHAIN_TOO_LONG
|
|
885
971
|
}
|
|
972
|
+
if (pkiIsDistrusted(s.store, cand)) {
|
|
973
|
+
return PKI_ERR_DISTRUSTED
|
|
974
|
+
}
|
|
886
975
|
const time: i32 = pkiTimeCode(cand, s.nowMs)
|
|
887
976
|
if (time !== PKI_OK) {
|
|
888
977
|
return time
|
|
@@ -893,6 +982,12 @@ const pkiIssuerCode = (s: PkiSearch, child: X509Certificate, cand: X509Certifica
|
|
|
893
982
|
if (cand.version !== 2 || !cand.bcPresent || !cand.bcCa) {
|
|
894
983
|
return PKI_ERR_NOT_CA
|
|
895
984
|
}
|
|
985
|
+
// The root too: limbo refuses a root with a non-critical basicConstraints,
|
|
986
|
+
// and every root of Mozilla's store is marked as these rules ask
|
|
987
|
+
// (crypto-pki.md, "Each structural rule on a root").
|
|
988
|
+
if (!cand.bcCritical || !pkiCriticalitySound(cand)) {
|
|
989
|
+
return PKI_ERR_MALFORMED
|
|
990
|
+
}
|
|
896
991
|
if (!root && cand.keyUsage >= 0 && (cand.keyUsage & X509_KU_KEY_CERT_SIGN) === 0) {
|
|
897
992
|
return PKI_ERR_KEY_USAGE
|
|
898
993
|
}
|
|
@@ -918,6 +1013,14 @@ const pkiIssuerCode = (s: PkiSearch, child: X509Certificate, cand: X509Certifica
|
|
|
918
1013
|
return signature !== PKI_OK ? signature : pkiKeyIdsCode(cand, root)
|
|
919
1014
|
}
|
|
920
1015
|
|
|
1016
|
+
/** Whether `cert`'s subjectPublicKeyInfo hashes to one `store` distrusts; no hash is taken while it distrusts none. */
|
|
1017
|
+
const pkiIsDistrusted = (store: PkiTrustStore, cert: X509Certificate): boolean => {
|
|
1018
|
+
if (store.distrusted.size === 0) {
|
|
1019
|
+
return false
|
|
1020
|
+
}
|
|
1021
|
+
return store.distrusted.has(pkiHex(sha256(cert.spki)))
|
|
1022
|
+
}
|
|
1023
|
+
|
|
921
1024
|
/** Keeps `code` as the answer when its attempt, `score` long, got nearer a root than any before it. */
|
|
922
1025
|
const pkiNote = (s: PkiSearch, code: i32, score: i32): void => {
|
|
923
1026
|
if (score > s.bestScore) {
|
|
@@ -1042,7 +1145,9 @@ const pkiSubscriberKeyCode = (leaf: X509Certificate): i32 => {
|
|
|
1042
1145
|
* The checks of the leaf's profile, once a path above it is found: no keyCertSign
|
|
1043
1146
|
* and no nameConstraints, no `anyExtendedKeyUsage`, a key a server may
|
|
1044
1147
|
* hold, a sound serial number, an authorityKeyIdentifier unless it is
|
|
1045
|
-
* self-issued,
|
|
1148
|
+
* self-issued, the criticality RFC 5280 fixes, a subjectAltName critical
|
|
1149
|
+
* exactly when the subject is empty (§4.2.1.6), an extKeyUsage that is not
|
|
1150
|
+
* critical, as webpki holds it, and dNSNames of letters, digits and hyphens. The common name
|
|
1046
1151
|
* is not read: limbo's own passing cases carry ones their subjectAltName
|
|
1047
1152
|
* lacks.
|
|
1048
1153
|
*/
|
|
@@ -1053,14 +1158,18 @@ const pkiLeafProfileCode = (leaf: X509Certificate): i32 => {
|
|
|
1053
1158
|
if (leaf.ncPresent) {
|
|
1054
1159
|
return PKI_ERR_NAME_CONSTRAINTS
|
|
1055
1160
|
}
|
|
1056
|
-
if (leaf.ekuAny) {
|
|
1161
|
+
if (leaf.ekuAny || leaf.ekuCritical) {
|
|
1057
1162
|
return PKI_ERR_EXT_KEY_USAGE
|
|
1058
1163
|
}
|
|
1059
1164
|
const key: i32 = pkiSubscriberKeyCode(leaf)
|
|
1060
1165
|
if (key !== PKI_OK) {
|
|
1061
1166
|
return key
|
|
1062
1167
|
}
|
|
1063
|
-
if (!pkiSerialSound(leaf) || (leaf.akid === null && !pkiSelfIssued(leaf))) {
|
|
1168
|
+
if (!pkiSerialSound(leaf) || (leaf.akid === null && !pkiSelfIssued(leaf)) || !pkiCriticalitySound(leaf)) {
|
|
1169
|
+
return PKI_ERR_MALFORMED
|
|
1170
|
+
}
|
|
1171
|
+
// An empty subject is the empty SEQUENCE, `30 00`.
|
|
1172
|
+
if (leaf.sanPresent && leaf.sanCritical !== (leaf.subject.length === 2)) {
|
|
1064
1173
|
return PKI_ERR_MALFORMED
|
|
1065
1174
|
}
|
|
1066
1175
|
for (const d of leaf.sanDns) {
|
|
@@ -1131,6 +1240,28 @@ export const pkiTrustStore = (roots: X509Certificate[]): PkiTrustStore => {
|
|
|
1131
1240
|
return store
|
|
1132
1241
|
}
|
|
1133
1242
|
|
|
1243
|
+
/**
|
|
1244
|
+
* Distrusts a key in `store`: from now on, `pkiVerifyServer` refuses with
|
|
1245
|
+
* `PKI_ERR_DISTRUSTED` a path that holds any certificate, the leaf, an
|
|
1246
|
+
* intermediate or a root, whose subjectPublicKeyInfo DER hashes to
|
|
1247
|
+
* `spkiSha256` under SHA-256. A certificate's DER is `X509Certificate.spki`;
|
|
1248
|
+
* `sha256` of `nish/crypto/sha256` hashes it. The key is refused whatever
|
|
1249
|
+
* name or certificate carries it, so one entry covers a CA's cross-signs and
|
|
1250
|
+
* reissues. A store distrusts nothing until this is called.
|
|
1251
|
+
*
|
|
1252
|
+
* @param store The store to add the hash to.
|
|
1253
|
+
* @param spkiSha256 The SHA-256 of the subjectPublicKeyInfo DER, 32 bytes.
|
|
1254
|
+
* @returns Whether the hash was added: false, and nothing added, unless it is
|
|
1255
|
+
* 32 bytes long.
|
|
1256
|
+
*/
|
|
1257
|
+
export const pkiDistrustSpki = (store: PkiTrustStore, spkiSha256: u8[]): boolean => {
|
|
1258
|
+
if (toI32(spkiSha256.length) !== 32) {
|
|
1259
|
+
return false
|
|
1260
|
+
}
|
|
1261
|
+
store.distrusted.set(pkiHex(spkiSha256), true)
|
|
1262
|
+
return true
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1134
1265
|
/**
|
|
1135
1266
|
* Verifies a TLS server's chain: `chain` holds each certificate's DER as the
|
|
1136
1267
|
* server sent it, the leaf first; `hostname` is the name or address dialled,
|
|
@@ -1146,7 +1277,8 @@ export const pkiTrustStore = (roots: X509Certificate[]): PkiTrustStore => {
|
|
|
1146
1277
|
* `PKI_ERR_MALFORMED` and one with more than `PKI_MAX_NAMES` names or
|
|
1147
1278
|
* `PKI_MAX_SUBTREES` subtrees is `PKI_ERR_BUDGET`; then the leaf's own checks,
|
|
1148
1279
|
* in turn its validity, an unknown critical extension, being a CA, its key
|
|
1149
|
-
* usage, its extended key usage and the host name; then
|
|
1280
|
+
* usage, its extended key usage and the host name; then whether its key is
|
|
1281
|
+
* distrusted (`pkiDistrustSpki`); then the path search,
|
|
1150
1282
|
* whose reason the module's header ranks, or `PKI_ERR_BUDGET` when it ran out
|
|
1151
1283
|
* of signatures or name comparisons; and last, once a path is found, the
|
|
1152
1284
|
* leaf's profile.
|
|
@@ -1199,6 +1331,9 @@ export const pkiVerifyServer = (
|
|
|
1199
1331
|
if (leafCode !== PKI_OK) {
|
|
1200
1332
|
return new PkiResult(leafCode, none)
|
|
1201
1333
|
}
|
|
1334
|
+
if (pkiIsDistrusted(store, leaf)) {
|
|
1335
|
+
return new PkiResult(PKI_ERR_DISTRUSTED, none)
|
|
1336
|
+
}
|
|
1202
1337
|
const pool: X509Certificate[] = []
|
|
1203
1338
|
for (let i: i32 = 1; i < toI32(certs.length); i++) {
|
|
1204
1339
|
pool.push(certs[i])
|
|
@@ -1272,5 +1407,8 @@ export const pkiReason = (code: i32): string => {
|
|
|
1272
1407
|
if (code === PKI_ERR_BUDGET) {
|
|
1273
1408
|
return "the chain asks for more work than one verification does"
|
|
1274
1409
|
}
|
|
1410
|
+
if (code === PKI_ERR_DISTRUSTED) {
|
|
1411
|
+
return "a certificate's key is distrusted"
|
|
1412
|
+
}
|
|
1275
1413
|
return "unknown reason"
|
|
1276
1414
|
}
|
package/std/crypto/sha256.ts
CHANGED
|
@@ -561,9 +561,31 @@ export class Sha256 {
|
|
|
561
561
|
* @returns A fresh 32-byte digest, which the caller owns.
|
|
562
562
|
*/
|
|
563
563
|
digest(): u8[] {
|
|
564
|
+
const out: u8[] = new Array<u8>(SHA256_SIZE)
|
|
565
|
+
const from: i32 = 0
|
|
566
|
+
this.digestInto(out, from)
|
|
567
|
+
return out
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Writes the 32-byte message digest (FIPS 180-4 §6.2.2, the final H^(N) in
|
|
572
|
+
* big-endian order) at `out[at .. at + 32)` and ends the computation, with
|
|
573
|
+
* nothing allocated. A window outside `out` panics before anything is
|
|
574
|
+
* written or the hasher is finished, rather than writing the part of the
|
|
575
|
+
* digest that fits. A second `digest` or `digestInto` panics, and so does an
|
|
576
|
+
* `update` after it.
|
|
577
|
+
*
|
|
578
|
+
* @param out The caller's array the digest is written into.
|
|
579
|
+
* @param at Where the 32 digest bytes start in `out`.
|
|
580
|
+
*/
|
|
581
|
+
digestInto(out: u8[], at: i32): void {
|
|
564
582
|
if (this.finished) {
|
|
565
583
|
panic("Sha256: digest called twice")
|
|
566
584
|
}
|
|
585
|
+
const size: i32 = toI32(out.length)
|
|
586
|
+
if (at < 0 || at > size - SHA256_SIZE) {
|
|
587
|
+
panic("Sha256: the digest window is outside its array")
|
|
588
|
+
}
|
|
567
589
|
this.finished = true
|
|
568
590
|
// §5.1.1: a single 1 bit, then zeros up to 56 bytes into a block, then the
|
|
569
591
|
// message length in bits as a big-endian 64-bit number. When fewer than
|
|
@@ -588,17 +610,23 @@ export class Sha256 {
|
|
|
588
610
|
block[SHA256_BLOCK - 1 - i] = toU8(bits >>> toU64(8 * i))
|
|
589
611
|
}
|
|
590
612
|
sha256Compress(this.state, this.schedule, block, 0)
|
|
591
|
-
const out: u8[] = new Array<u8>(SHA256_SIZE)
|
|
592
613
|
const state: u32[] = this.state
|
|
593
614
|
const words: i32 = toI32(state.length)
|
|
594
615
|
for (let i: i32 = 0; i < words; i += 1) {
|
|
595
616
|
const word: u32 = state[i]
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
617
|
+
const p: i32 = at + 4 * i
|
|
618
|
+
// Always true once the window check above passed; written out so the
|
|
619
|
+
// prover sees each byte's index is inside `out`, and a panic rather
|
|
620
|
+
// than a skipped write if an edit ever makes it false.
|
|
621
|
+
if (p >= 0 && p + 3 < toI32(out.length)) {
|
|
622
|
+
out[p] = toU8(word >>> 24)
|
|
623
|
+
out[p + 1] = toU8(word >>> 16)
|
|
624
|
+
out[p + 2] = toU8(word >>> 8)
|
|
625
|
+
out[p + 3] = toU8(word)
|
|
626
|
+
} else {
|
|
627
|
+
panic("Sha256: a digest word fell outside its window")
|
|
628
|
+
}
|
|
600
629
|
}
|
|
601
|
-
return out
|
|
602
630
|
}
|
|
603
631
|
}
|
|
604
632
|
|
package/std/crypto/x25519.ts
CHANGED
|
@@ -54,21 +54,21 @@
|
|
|
54
54
|
* an input length.
|
|
55
55
|
*
|
|
56
56
|
* WP34 N6's disassembly check (`tests/ct-asm.js`) verifies the part a secret
|
|
57
|
-
* flows through: `tests/cases/ct_asm_x25519` holds copies of
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
57
|
+
* flows through: `tests/cases/ct_asm_x25519` holds copies of the field
|
|
58
|
+
* arithmetic, `f25519Swap`, the encodings and both halves of the ladder, and
|
|
59
|
+
* the check reads each one's `clang -O2` machine code for x86-64 and aarch64
|
|
60
|
+
* and finds no branch on a secret and no load or store at an address the
|
|
61
|
+
* scalar bit or a limb reaches. The rolled loops (the ladder's 255 steps, the
|
|
62
|
+
* inversion's square chains, the encodings) are read whole, back edge included.
|
|
63
|
+
*
|
|
64
|
+
* They are copies because a case compiles to one module. `tests/run.js` holds
|
|
65
|
+
* each to this module's text, with the early `return`s dropped (each guards a
|
|
66
|
+
* public length, but is a branch) and every index unchecked, so a change here
|
|
67
|
+
* that the copies miss fails that check. What stays outside the check is
|
|
68
|
+
* the allocation and wiping around the work in `x25519Ladder`, `f25519Decode`
|
|
69
|
+
* and `f25519Encode`, whose branches are all on public positions. Weekly,
|
|
70
|
+
* `tests/ct-timing.js` times the fixture's functions on real x86-64 and
|
|
71
|
+
* aarch64 hardware as well.
|
|
72
72
|
*
|
|
73
73
|
* **Loop bounds are literals.** A field element is always ten limbs and an
|
|
74
74
|
* encoding always 32 bytes, and each function that indexes one opens with an
|
|
@@ -280,6 +280,24 @@ export const f25519Sub = (out: i64[], f: i64[], g: i64[]): void => {
|
|
|
280
280
|
}
|
|
281
281
|
}
|
|
282
282
|
|
|
283
|
+
/**
|
|
284
|
+
* Row `i` of `f25519Mul`'s product: `f[i]` (doubled where both limbs are odd)
|
|
285
|
+
* times each limb of `g`, added into `wide[i + j]`. The ten rows are written
|
|
286
|
+
* out rather than looped: `clang -O2` then keeps `wide` in registers and
|
|
287
|
+
* stack slots it can name, where a rolled row loop indexes it by a counter,
|
|
288
|
+
* and an indexed store into the frame is one tests/ct-asm.js cannot place
|
|
289
|
+
* (`docs/security/ct-verification.md`).
|
|
290
|
+
*/
|
|
291
|
+
const f25519MulRow = (wide: i64[], f: i64[], doubled: i64[], g: i64[], i: i32): void => {
|
|
292
|
+
for (let j: i32 = 0; j < 10; j++) {
|
|
293
|
+
const k: i32 = i + j
|
|
294
|
+
const fi: i64 = (i & j & 1) === 1 ? doubled[i] : f[i]
|
|
295
|
+
if (k >= 0 && k < 19) {
|
|
296
|
+
wide[k] = f25519Add64(wide[k], f25519Mul64(fi, g[j]))
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
283
301
|
/**
|
|
284
302
|
* `out = f * g`, reduced. `out` may be `f` or `g`: the product is built in
|
|
285
303
|
* scratch arrays and only copied out at the end.
|
|
@@ -312,17 +330,18 @@ export const f25519Mul = (out: i64[], f: i64[], g: i64[]): void => {
|
|
|
312
330
|
for (let i: i32 = 0; i < 10; i++) {
|
|
313
331
|
doubled[i] = f25519Mul64(f[i], toI64((i & 1) + 1))
|
|
314
332
|
}
|
|
315
|
-
// The plain product, limb `i + j` of nineteen.
|
|
333
|
+
// The plain product, limb `i + j` of nineteen, a row of `f` at a time.
|
|
316
334
|
const wide: i64[] = new Array<i64>(19)
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
335
|
+
f25519MulRow(wide, f, doubled, g, 0)
|
|
336
|
+
f25519MulRow(wide, f, doubled, g, 1)
|
|
337
|
+
f25519MulRow(wide, f, doubled, g, 2)
|
|
338
|
+
f25519MulRow(wide, f, doubled, g, 3)
|
|
339
|
+
f25519MulRow(wide, f, doubled, g, 4)
|
|
340
|
+
f25519MulRow(wide, f, doubled, g, 5)
|
|
341
|
+
f25519MulRow(wide, f, doubled, g, 6)
|
|
342
|
+
f25519MulRow(wide, f, doubled, g, 7)
|
|
343
|
+
f25519MulRow(wide, f, doubled, g, 8)
|
|
344
|
+
f25519MulRow(wide, f, doubled, g, 9)
|
|
326
345
|
// Limbs 10 to 18 are past bit 255, and 2^255 = 19 (mod p): fold them down.
|
|
327
346
|
const h: i64[] = new Array<i64>(10)
|
|
328
347
|
for (let k: i32 = 0; k < 9; k++) {
|
|
@@ -488,6 +507,16 @@ export const f25519Decode = (bytes: u8[]): i64[] => {
|
|
|
488
507
|
if (toI32(bytes.length) < 32) {
|
|
489
508
|
return f
|
|
490
509
|
}
|
|
510
|
+
f25519DecodeInto(f, bytes)
|
|
511
|
+
return f
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* `f25519Decode`'s loop, into `f`: the 32 bytes of `bytes` streamed into ten
|
|
516
|
+
* limbs. Apart so that tests/cases/ct_asm_x25519 can read this loop in the
|
|
517
|
+
* assembly as the module has it (`docs/security/ct-verification.md`).
|
|
518
|
+
*/
|
|
519
|
+
const f25519DecodeInto = (f: i64[], bytes: u8[]): void => {
|
|
491
520
|
let acc: i64 = 0
|
|
492
521
|
let bits: i64 = 0
|
|
493
522
|
let limb: i32 = 0
|
|
@@ -508,7 +537,6 @@ export const f25519Decode = (bytes: u8[]): i64[] => {
|
|
|
508
537
|
}
|
|
509
538
|
}
|
|
510
539
|
}
|
|
511
|
-
return f
|
|
512
540
|
}
|
|
513
541
|
|
|
514
542
|
/**
|
|
@@ -530,6 +558,16 @@ export const f25519Decode = (bytes: u8[]): i64[] => {
|
|
|
530
558
|
* @internal Imported by `nish/crypto/ed25519`, which works over the same field.
|
|
531
559
|
*/
|
|
532
560
|
export const f25519Encode = (f: i64[]): u8[] => {
|
|
561
|
+
const out: u8[] = new Array<u8>(32)
|
|
562
|
+
f25519EncodeInto(out, f)
|
|
563
|
+
return out
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* `f25519Encode`'s reduction and packing, into `out`, 32 bytes. Apart for the
|
|
568
|
+
* same reason as `f25519DecodeInto`.
|
|
569
|
+
*/
|
|
570
|
+
const f25519EncodeInto = (out: u8[], f: i64[]): void => {
|
|
533
571
|
const h: i64[] = new Array<i64>(10)
|
|
534
572
|
f25519Copy(h, f)
|
|
535
573
|
f25519Carry(h)
|
|
@@ -543,7 +581,6 @@ export const f25519Encode = (f: i64[]): u8[] => {
|
|
|
543
581
|
f25519CarryChain(h)
|
|
544
582
|
h[9] = h[9] & f25519LimbMask(9)
|
|
545
583
|
|
|
546
|
-
const out: u8[] = new Array<u8>(32)
|
|
547
584
|
let acc: i64 = 0
|
|
548
585
|
let bits: i64 = 0
|
|
549
586
|
let at: i32 = 0
|
|
@@ -561,7 +598,94 @@ export const f25519Encode = (f: i64[]): u8[] => {
|
|
|
561
598
|
if (at >= 0 && at < 32) {
|
|
562
599
|
out[at] = toU8(acc & toI64(0xff))
|
|
563
600
|
}
|
|
564
|
-
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* One step of the ladder, from its two swaps on: `swap` is the bit the loop
|
|
605
|
+
* has just folded into its swap state, a secret that only feeds the swaps'
|
|
606
|
+
* masks. `a` to `t` are scratch, passed in so that `x25519Ladder` can wipe
|
|
607
|
+
* them (ECC-2).
|
|
608
|
+
*/
|
|
609
|
+
const x25519LadderStep = (
|
|
610
|
+
x2: i64[],
|
|
611
|
+
z2: i64[],
|
|
612
|
+
x3: i64[],
|
|
613
|
+
z3: i64[],
|
|
614
|
+
u: i64[],
|
|
615
|
+
swap: i64,
|
|
616
|
+
a: i64[],
|
|
617
|
+
aa: i64[],
|
|
618
|
+
b: i64[],
|
|
619
|
+
bb: i64[],
|
|
620
|
+
e: i64[],
|
|
621
|
+
c: i64[],
|
|
622
|
+
d: i64[],
|
|
623
|
+
da: i64[],
|
|
624
|
+
cb: i64[],
|
|
625
|
+
t: i64[]
|
|
626
|
+
): void => {
|
|
627
|
+
f25519Swap(x2, x3, swap)
|
|
628
|
+
f25519Swap(z2, z3, swap)
|
|
629
|
+
f25519Add(a, x2, z2)
|
|
630
|
+
f25519Square(aa, a)
|
|
631
|
+
f25519Sub(b, x2, z2)
|
|
632
|
+
f25519Square(bb, b)
|
|
633
|
+
f25519Sub(e, aa, bb)
|
|
634
|
+
f25519Add(c, x3, z3)
|
|
635
|
+
f25519Sub(d, x3, z3)
|
|
636
|
+
f25519Mul(da, d, a)
|
|
637
|
+
f25519Mul(cb, c, b)
|
|
638
|
+
f25519Add(t, da, cb)
|
|
639
|
+
f25519Square(x3, t)
|
|
640
|
+
f25519Sub(t, da, cb)
|
|
641
|
+
f25519Square(t, t)
|
|
642
|
+
f25519Mul(z3, u, t)
|
|
643
|
+
f25519Mul(x2, aa, bb)
|
|
644
|
+
f25519MulA24(t, e)
|
|
645
|
+
f25519Add(t, aa, t)
|
|
646
|
+
f25519Mul(z2, e, t)
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* The ladder's 255 steps on a clamped `k`, and the two swaps after them. The
|
|
651
|
+
* scratch is `x25519LadderStep`'s. Apart from `x25519Ladder` so that
|
|
652
|
+
* tests/cases/ct_asm_x25519 can read this loop in the assembly as the module
|
|
653
|
+
* has it (`docs/security/ct-verification.md`).
|
|
654
|
+
*/
|
|
655
|
+
const x25519LadderLoop = (
|
|
656
|
+
x2: i64[],
|
|
657
|
+
z2: i64[],
|
|
658
|
+
x3: i64[],
|
|
659
|
+
z3: i64[],
|
|
660
|
+
u: i64[],
|
|
661
|
+
k: u8[],
|
|
662
|
+
a: i64[],
|
|
663
|
+
aa: i64[],
|
|
664
|
+
b: i64[],
|
|
665
|
+
bb: i64[],
|
|
666
|
+
e: i64[],
|
|
667
|
+
c: i64[],
|
|
668
|
+
d: i64[],
|
|
669
|
+
da: i64[],
|
|
670
|
+
cb: i64[],
|
|
671
|
+
t: i64[]
|
|
672
|
+
): void => {
|
|
673
|
+
let swap: i64 = 0
|
|
674
|
+
// Bit 254 down to bit 0: the position is public and picks the byte and the
|
|
675
|
+
// shift; the bit itself is secret and only ever feeds a mask.
|
|
676
|
+
for (let byte: i32 = 31; byte >= 0; byte--) {
|
|
677
|
+
for (let shift: i32 = 7; shift >= 0; shift--) {
|
|
678
|
+
if (byte === 31 && shift === 7) {
|
|
679
|
+
continue
|
|
680
|
+
}
|
|
681
|
+
const bit: i64 = toI64(k[byte] >> toU8(shift)) & toI64(1)
|
|
682
|
+
swap = swap ^ bit
|
|
683
|
+
x25519LadderStep(x2, z2, x3, z3, u, swap, a, aa, b, bb, e, c, d, da, cb, t)
|
|
684
|
+
swap = bit
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
f25519Swap(x2, x3, swap)
|
|
688
|
+
f25519Swap(z2, z3, swap)
|
|
565
689
|
}
|
|
566
690
|
|
|
567
691
|
/**
|
|
@@ -594,42 +718,7 @@ const x25519Ladder = (k: u8[], u: i64[]): u8[] => {
|
|
|
594
718
|
const cb: i64[] = f25519Zero()
|
|
595
719
|
const t: i64[] = f25519Zero()
|
|
596
720
|
|
|
597
|
-
|
|
598
|
-
// Bit 254 down to bit 0: the position is public and picks the byte and the
|
|
599
|
-
// shift; the bit itself is secret and only ever feeds a mask.
|
|
600
|
-
for (let byte: i32 = 31; byte >= 0; byte--) {
|
|
601
|
-
for (let shift: i32 = 7; shift >= 0; shift--) {
|
|
602
|
-
if (byte === 31 && shift === 7) {
|
|
603
|
-
continue
|
|
604
|
-
}
|
|
605
|
-
const bit: i64 = toI64(k[byte] >> toU8(shift)) & toI64(1)
|
|
606
|
-
swap = swap ^ bit
|
|
607
|
-
f25519Swap(x2, x3, swap)
|
|
608
|
-
f25519Swap(z2, z3, swap)
|
|
609
|
-
swap = bit
|
|
610
|
-
|
|
611
|
-
f25519Add(a, x2, z2)
|
|
612
|
-
f25519Square(aa, a)
|
|
613
|
-
f25519Sub(b, x2, z2)
|
|
614
|
-
f25519Square(bb, b)
|
|
615
|
-
f25519Sub(e, aa, bb)
|
|
616
|
-
f25519Add(c, x3, z3)
|
|
617
|
-
f25519Sub(d, x3, z3)
|
|
618
|
-
f25519Mul(da, d, a)
|
|
619
|
-
f25519Mul(cb, c, b)
|
|
620
|
-
f25519Add(t, da, cb)
|
|
621
|
-
f25519Square(x3, t)
|
|
622
|
-
f25519Sub(t, da, cb)
|
|
623
|
-
f25519Square(t, t)
|
|
624
|
-
f25519Mul(z3, u, t)
|
|
625
|
-
f25519Mul(x2, aa, bb)
|
|
626
|
-
f25519MulA24(t, e)
|
|
627
|
-
f25519Add(t, aa, t)
|
|
628
|
-
f25519Mul(z2, e, t)
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
f25519Swap(x2, x3, swap)
|
|
632
|
-
f25519Swap(z2, z3, swap)
|
|
721
|
+
x25519LadderLoop(x2, z2, x3, z3, u, k, a, aa, b, bb, e, c, d, da, cb, t)
|
|
633
722
|
|
|
634
723
|
f25519Invert(t, z2)
|
|
635
724
|
f25519Mul(x2, x2, t)
|