nish 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/docs/AI.md +50 -4
- package/docs/INSTALL.md +73 -59
- package/package.json +19 -5
- 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/scripts/fuzz.sh +1 -0
- package/scripts/undocumented.txt +14 -0
- 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/pki.ts
CHANGED
|
@@ -7,13 +7,22 @@
|
|
|
7
7
|
* import { pkiLoadSystemRoots } from "nish/crypto/pki-roots";
|
|
8
8
|
* import { PKI_OK, PkiResult, PkiTrustStore, pkiReason, pkiTrustStore, pkiVerifyServer } from "nish/crypto/pki";
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
10
|
+
* // chain: each certificate's DER as the server sent it, the leaf first
|
|
11
|
+
* const check = (chain: u8[][], host: string, store: PkiTrustStore): boolean => {
|
|
12
|
+
* const r: PkiResult = pkiVerifyServer(chain, host, toI64(Date.now()), store);
|
|
13
|
+
* if (r.code !== PKI_OK) {
|
|
14
|
+
* console.log(pkiReason(r.code));
|
|
15
|
+
* return false;
|
|
16
|
+
* }
|
|
17
|
+
* // r.path: the leaf first, the trusted root last
|
|
18
|
+
* return true;
|
|
19
|
+
* };
|
|
20
|
+
*
|
|
21
|
+
* export const main = (): i32 => {
|
|
22
|
+
* const store: PkiTrustStore = pkiTrustStore(pkiLoadSystemRoots().roots);
|
|
23
|
+
* const chain: u8[][] = [];
|
|
24
|
+
* return check(chain, "example.com", store) ? 0 : 1;
|
|
25
|
+
* };
|
|
17
26
|
*
|
|
18
27
|
* The network chooses every byte of `chain` (docs/security/crypto-pki.md,
|
|
19
28
|
* "Threat model"), so every refusal has a reason code of its own and every
|
|
@@ -21,22 +30,25 @@
|
|
|
21
30
|
* and subtrees of a certificate, the certificates of a path, the signatures
|
|
22
31
|
* checked and the name comparisons made by one call.
|
|
23
32
|
*
|
|
24
|
-
* The chain's first certificate is the leaf; the rest are a pool, in any
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
33
|
+
* The chain's first certificate is the leaf; the rest are a pool, in any order
|
|
34
|
+
* and possibly with extras, as servers send them. A path is searched for depth
|
|
35
|
+
* first from the leaf: an issuer is a root of the store, tried first, or a pool
|
|
36
|
+
* certificate, whose subject is the child's issuer Name byte for byte, and
|
|
37
|
+
* whose subjectKeyIdentifier, when both it and the child's
|
|
29
38
|
* authorityKeyIdentifier are present, is that identifier. A certificate is
|
|
30
|
-
* never twice in a path.
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
39
|
+
* never twice in a path.
|
|
40
|
+
*
|
|
41
|
+
* A path ends at a root, whose own signature is not read, but whose validity,
|
|
42
|
+
* critical extensions, basicConstraints, extKeyUsage, name constraints and key
|
|
43
|
+
* identifiers are checked as an intermediate's are, as x509-limbo's cases ask.
|
|
44
|
+
* Every check of an issuer is made as it is added, against the certificates
|
|
45
|
+
* below it, so a refusal ends that branch at once; the leaf's profile
|
|
46
|
+
* (CA/Browser Forum Baseline Requirements §7.1) is checked once a path is
|
|
47
|
+
* found.
|
|
48
|
+
*
|
|
49
|
+
* When no path is accepted, the reason is that of the attempt that got nearest
|
|
50
|
+
* a root: one that reached a root beats any that did not, a longer one a
|
|
51
|
+
* shorter, and the first found a later one of the same length.
|
|
40
52
|
*
|
|
41
53
|
* Revocation (CRLs, OCSP, Certificate Transparency), certificate policies,
|
|
42
54
|
* RFC 5280 §7.1's Name folding and an extension's criticality are not
|
|
@@ -155,9 +167,10 @@ const PKI_AT_ROOT: i32 = 1000
|
|
|
155
167
|
export class PkiTrustStore {
|
|
156
168
|
/** Every root, in the order given, no two with the same DER. */
|
|
157
169
|
roots: X509Certificate[]
|
|
158
|
-
/** The roots under each subject, keyed by the subject's DER in hex. */
|
|
170
|
+
/** @internal The roots under each subject, keyed by the subject's DER in hex. */
|
|
159
171
|
bySubject: Map<string, X509Certificate[]>
|
|
160
172
|
|
|
173
|
+
/** An empty store; `pkiTrustStore` builds a filled one. */
|
|
161
174
|
constructor() {
|
|
162
175
|
this.roots = []
|
|
163
176
|
this.bySubject = new Map<string, X509Certificate[]>()
|
|
@@ -171,6 +184,12 @@ export class PkiResult {
|
|
|
171
184
|
/** When `PKI_OK`, the path: the leaf first, the root last; otherwise empty. */
|
|
172
185
|
path: X509Certificate[]
|
|
173
186
|
|
|
187
|
+
/**
|
|
188
|
+
* One verification's answer, as `pkiVerifyServer` gives it.
|
|
189
|
+
*
|
|
190
|
+
* @param code `PKI_OK` or the reason the chain was refused.
|
|
191
|
+
* @param path The path found, kept as given, not copied.
|
|
192
|
+
*/
|
|
174
193
|
constructor(code: i32, path: X509Certificate[]) {
|
|
175
194
|
this.code = code
|
|
176
195
|
this.path = path
|
|
@@ -179,18 +198,25 @@ export class PkiResult {
|
|
|
179
198
|
|
|
180
199
|
/** One call's search: the pool, the host, the path so far, and what has been spent. */
|
|
181
200
|
class PkiSearch {
|
|
201
|
+
/** @internal */
|
|
182
202
|
store: PkiTrustStore
|
|
203
|
+
/** @internal */
|
|
183
204
|
pool: X509Certificate[]
|
|
205
|
+
/** @internal */
|
|
184
206
|
path: X509Certificate[]
|
|
185
|
-
/** The host as a DNS name, normalised, or `""` when it is an address. */
|
|
207
|
+
/** @internal The host as a DNS name, normalised, or `""` when it is an address. */
|
|
186
208
|
host: string
|
|
209
|
+
/** @internal */
|
|
187
210
|
nowMs: i64 = 0
|
|
211
|
+
/** @internal */
|
|
188
212
|
signatures: i32 = 0
|
|
213
|
+
/** @internal */
|
|
189
214
|
nameChecks: i32 = 0
|
|
190
|
-
/** The reason of the attempt nearest a root, and how near it got. */
|
|
215
|
+
/** @internal The reason of the attempt nearest a root, and how near it got. */
|
|
191
216
|
bestCode: i32 = 0
|
|
217
|
+
/** @internal */
|
|
192
218
|
bestScore: i32 = -1
|
|
193
|
-
/** Whether a bound was spent, which ends the search. */
|
|
219
|
+
/** @internal Whether a bound was spent, which ends the search. */
|
|
194
220
|
spent: boolean = false
|
|
195
221
|
|
|
196
222
|
constructor(store: PkiTrustStore, pool: X509Certificate[], host: string, nowMs: i64) {
|
|
@@ -485,7 +511,12 @@ const pkiHostDns = (hostname: string): string | null => {
|
|
|
485
511
|
* compared without case after one trailing dot is stripped from each side,
|
|
486
512
|
* against the dNSNames, a wildcard only as a whole leftmost label. The
|
|
487
513
|
* subject's common name is never read, so a certificate without a
|
|
488
|
-
* subjectAltName names no host.
|
|
514
|
+
* subjectAltName names no host. Nothing but the names is checked.
|
|
515
|
+
*
|
|
516
|
+
* @param leaf The certificate the server presented as its own.
|
|
517
|
+
* @param hostname The name or address dialled: a DNS name, ASCII, or an IPv4
|
|
518
|
+
* or IPv6 address, the IPv6 one bare or in brackets.
|
|
519
|
+
* @returns `true` when one of `leaf`'s names matches `hostname`.
|
|
489
520
|
*/
|
|
490
521
|
export const pkiMatchHostname = (leaf: X509Certificate, hostname: string): boolean => {
|
|
491
522
|
const ip: u8[] | null = pkiParseIp(pkiStripDot(hostname))
|
|
@@ -610,6 +641,11 @@ const pkiSignatureCode = (cert: X509Certificate, issuer: X509Certificate): i32 =
|
|
|
610
641
|
* or SHA-384 under P-256 or P-384, RSA PKCS#1 v1.5 with SHA-256, SHA-384 or
|
|
611
642
|
* SHA-512 and RSASSA-PSS under a key of `PKI_MIN_RSA_BITS` or more whose
|
|
612
643
|
* modulus is a whole number of bytes, and Ed25519. Nothing else about either certificate is checked.
|
|
644
|
+
*
|
|
645
|
+
* @param cert The certificate whose signature is checked.
|
|
646
|
+
* @param issuer The certificate whose subject public key is to have made it.
|
|
647
|
+
* @returns `true` when the signature verifies; `false` for a bad signature, a
|
|
648
|
+
* weak or unparsable key, or an algorithm not listed here.
|
|
613
649
|
*/
|
|
614
650
|
export const pkiVerifySignature = (cert: X509Certificate, issuer: X509Certificate): boolean =>
|
|
615
651
|
pkiSignatureCode(cert, issuer) === PKI_OK
|
|
@@ -1061,7 +1097,15 @@ const pkiLeafCode = (leaf: X509Certificate, hostname: string, nowMs: i64): i32 =
|
|
|
1061
1097
|
return pkiMatchHostname(leaf, hostname) ? PKI_OK : PKI_ERR_HOSTNAME
|
|
1062
1098
|
}
|
|
1063
1099
|
|
|
1064
|
-
/**
|
|
1100
|
+
/**
|
|
1101
|
+
* A store of `roots`, indexed by subject; a root whose DER is one already
|
|
1102
|
+
* given is kept once. Nothing about a root is checked here: its validity and
|
|
1103
|
+
* extensions are checked when a path reaches it.
|
|
1104
|
+
*
|
|
1105
|
+
* @param roots The certificates to trust, as `x509ParseCertificate` gave them.
|
|
1106
|
+
* @returns A new store, whose lists hold the given certificates themselves,
|
|
1107
|
+
* not copies; `roots` itself is not kept.
|
|
1108
|
+
*/
|
|
1065
1109
|
export const pkiTrustStore = (roots: X509Certificate[]): PkiTrustStore => {
|
|
1066
1110
|
const store = new PkiTrustStore()
|
|
1067
1111
|
for (const root of roots) {
|
|
@@ -1093,7 +1137,27 @@ export const pkiTrustStore = (roots: X509Certificate[]): PkiTrustStore => {
|
|
|
1093
1137
|
* ASCII (an internationalised name in its A-label form); `nowMs` is the
|
|
1094
1138
|
* instant to judge validity at, milliseconds since 1970-01-01 UTC; `store`
|
|
1095
1139
|
* holds the roots trusted. `PKI_OK` with the path, or the reason it was
|
|
1096
|
-
* refused
|
|
1140
|
+
* refused.
|
|
1141
|
+
*
|
|
1142
|
+
* When several reasons apply, the first in this order is given: an empty
|
|
1143
|
+
* chain is `PKI_ERR_MALFORMED`; more than `PKI_MAX_CHAIN_CERTS` certificates
|
|
1144
|
+
* or `PKI_MAX_CHAIN_BYTES` bytes is `PKI_ERR_CHAIN_TOO_LONG`; then, certificate
|
|
1145
|
+
* by certificate in the order sent, one that does not parse is
|
|
1146
|
+
* `PKI_ERR_MALFORMED` and one with more than `PKI_MAX_NAMES` names or
|
|
1147
|
+
* `PKI_MAX_SUBTREES` subtrees is `PKI_ERR_BUDGET`; then the leaf's own checks,
|
|
1148
|
+
* in turn its validity, an unknown critical extension, being a CA, its key
|
|
1149
|
+
* usage, its extended key usage and the host name; then the path search,
|
|
1150
|
+
* whose reason the module's header ranks, or `PKI_ERR_BUDGET` when it ran out
|
|
1151
|
+
* of signatures or name comparisons; and last, once a path is found, the
|
|
1152
|
+
* leaf's profile.
|
|
1153
|
+
*
|
|
1154
|
+
* @param chain Each certificate's DER as the server sent it, the leaf first.
|
|
1155
|
+
* @param hostname The name or address dialled.
|
|
1156
|
+
* @param nowMs The instant to judge validity at, in milliseconds since
|
|
1157
|
+
* 1970-01-01 UTC.
|
|
1158
|
+
* @param store The roots trusted.
|
|
1159
|
+
* @returns A new result: `PKI_OK` with a fresh path, or a reason with an
|
|
1160
|
+
* empty one.
|
|
1097
1161
|
*/
|
|
1098
1162
|
export const pkiVerifyServer = (
|
|
1099
1163
|
chain: u8[][],
|
|
@@ -1150,7 +1214,12 @@ export const pkiVerifyServer = (
|
|
|
1150
1214
|
return new PkiResult(s.spent ? PKI_ERR_BUDGET : s.bestCode, none)
|
|
1151
1215
|
}
|
|
1152
1216
|
|
|
1153
|
-
/**
|
|
1217
|
+
/**
|
|
1218
|
+
* A sentence for `code`, for a log or an error message.
|
|
1219
|
+
*
|
|
1220
|
+
* @param code `PKI_OK` or a `PKI_ERR_` constant.
|
|
1221
|
+
* @returns What the code means, or "unknown reason" for any other number.
|
|
1222
|
+
*/
|
|
1154
1223
|
export const pkiReason = (code: i32): string => {
|
|
1155
1224
|
if (code === PKI_OK) {
|
|
1156
1225
|
return "the chain is trusted and names the host"
|
package/std/crypto/rsa.ts
CHANGED
|
@@ -5,9 +5,14 @@
|
|
|
5
5
|
*
|
|
6
6
|
* import { RSA_HASH_SHA256, RsaPublicKey, rsaParsePublicKey, rsaVerifyPkcs1v15, rsaVerifyPss } from "nish/crypto/rsa";
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
* const
|
|
10
|
-
*
|
|
8
|
+
* // der: a PKCS#1 RSAPublicKey; sig: as long as the modulus
|
|
9
|
+
* const verify = (der: u8[], message: u8[], sig: u8[]): boolean => {
|
|
10
|
+
* const key: RsaPublicKey | null = rsaParsePublicKey(der);
|
|
11
|
+
* if (key === null) {
|
|
12
|
+
* return false;
|
|
13
|
+
* }
|
|
14
|
+
* return rsaVerifyPkcs1v15(key, RSA_HASH_SHA256, message, sig) || rsaVerifyPss(key, RSA_HASH_SHA256, 32, message, sig);
|
|
15
|
+
* };
|
|
11
16
|
*
|
|
12
17
|
* **The key** is the DER of PKCS#1's `RSAPublicKey ::= SEQUENCE { modulus
|
|
13
18
|
* INTEGER, publicExponent INTEGER }` (RFC 8017 §A.1.1), nothing before it and
|
|
@@ -22,14 +27,16 @@
|
|
|
22
27
|
* **Verification** answers `false`, never a panic, for an unknown hash id, a
|
|
23
28
|
* signature that is not exactly as long as the modulus or is not below it, a
|
|
24
29
|
* negative salt length, and any encoding that is not exactly the one RFC 8017
|
|
25
|
-
* defines. PKCS#1 v1.5 builds the whole encoded message it expects, `00 01 FF
|
|
26
|
-
*
|
|
30
|
+
* defines. PKCS#1 v1.5 builds the whole encoded message it expects, `00 01 FF …
|
|
31
|
+
* FF 00 DigestInfo`, and compares it with what the signature opens to, so no
|
|
27
32
|
* part of the opened block is parsed (Bleichenbacher's 2006 forgery against a
|
|
28
|
-
* parser that skipped trailing bytes).
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
+
* parser that skipped trailing bytes).
|
|
34
|
+
*
|
|
35
|
+
* The DigestInfo carries the NULL parameters RFC 8017 §9.2 note 1 prints; the
|
|
36
|
+
* form without them, which the RFC lets a verifier accept, is refused. PSS is
|
|
37
|
+
* RFC 8017 §8.1.2 and §9.1.2 step by step, with MGF1 over the same hash as the
|
|
38
|
+
* message, the leftmost bits of the masked block checked to be zero and the
|
|
39
|
+
* 0x01 separator checked.
|
|
33
40
|
*
|
|
34
41
|
* **Variable time, on purpose.** A public key, a signature and a message are
|
|
35
42
|
* all public, so nothing here has a secret to leak: the exponentiation
|
|
@@ -37,19 +44,21 @@
|
|
|
37
44
|
* the encodings are compared with an early exit. That is why this module does
|
|
38
45
|
* not use `nish:secret` or `nish/crypto/ct`.
|
|
39
46
|
*
|
|
40
|
-
* **The arithmetic** is written for this module from the textbook. Numbers
|
|
41
|
-
*
|
|
47
|
+
* **The arithmetic** is written for this module from the textbook. Numbers are
|
|
48
|
+
* little-endian `u32` limbs, products are taken in `u64`, and modular
|
|
42
49
|
* multiplication is Montgomery's (P. L. Montgomery, "Modular multiplication
|
|
43
50
|
* without trial division", Math. Comp. 44, 1985) in the coarsely integrated
|
|
44
|
-
* operand scanning form of Ç. K. Koç, T. Acar and B. S. Kaliski, "Analyzing
|
|
45
|
-
*
|
|
46
|
-
*
|
|
51
|
+
* operand scanning form of Ç. K. Koç, T. Acar and B. S. Kaliski, "Analyzing and
|
|
52
|
+
* comparing Montgomery multiplication algorithms" (IEEE Micro, 1996), the same
|
|
53
|
+
* algorithm as A. Menezes, P. van Oorschot and S. Vanstone, *Handbook of
|
|
47
54
|
* Applied Cryptography* (1996), Algorithm 14.36, with one conditional
|
|
48
|
-
* subtraction at the end.
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
55
|
+
* subtraction at the end.
|
|
56
|
+
*
|
|
57
|
+
* The exponent is applied left to right by square and multiply (HAC Algorithm
|
|
58
|
+
* 14.79's binary special case). The closest upstream is BearSSL's `i31` code
|
|
59
|
+
* (`br_i31_montymul`, `br_i31_modpow`), and nothing here follows it: BearSSL
|
|
60
|
+
* keeps 31-bit words and a constant-time structure, this keeps 32-bit words and
|
|
61
|
+
* branches freely.
|
|
53
62
|
*
|
|
54
63
|
* Private names carry the `rsa` prefix because a `std/` module's private
|
|
55
64
|
* functions share the importing program's flat symbol namespace
|
|
@@ -87,6 +96,13 @@ export class RsaPublicKey {
|
|
|
87
96
|
/** The modulus's length in bits: 2048 for a 2048-bit key. */
|
|
88
97
|
bits: i32
|
|
89
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Keeps the arrays given, without a copy or a check.
|
|
101
|
+
*
|
|
102
|
+
* @param n The modulus, big-endian with no leading zero byte.
|
|
103
|
+
* @param e The public exponent, big-endian with no leading zero byte.
|
|
104
|
+
* @param bits The modulus's length in bits.
|
|
105
|
+
*/
|
|
90
106
|
constructor(n: u8[], e: u8[], bits: i32) {
|
|
91
107
|
this.n = n
|
|
92
108
|
this.e = e
|
|
@@ -213,6 +229,10 @@ const rsaBytesBelow = (a: u8[], b: u8[]): boolean => {
|
|
|
213
229
|
* after it; for a modulus that is even or not `RSA_MIN_BITS` to
|
|
214
230
|
* `RSA_MAX_BITS` bits long; and for an exponent that is even, below 3, or
|
|
215
231
|
* not below 2^32 (which also keeps it below the modulus).
|
|
232
|
+
*
|
|
233
|
+
* @param der The key's DER, nothing before it and nothing after it.
|
|
234
|
+
* @returns A new key whose modulus and exponent are copied out of `der`, or
|
|
235
|
+
* `null`.
|
|
216
236
|
*/
|
|
217
237
|
export const rsaParsePublicKey = (der: u8[]): RsaPublicKey | null => {
|
|
218
238
|
const total: i32 = toI32(der.length)
|
|
@@ -327,8 +347,8 @@ const rsaSubInPlace = (a: u32[], n: u32[], count: i32): void => {
|
|
|
327
347
|
* Acar and Kaliski's CIOS. `t` is scratch of `count + 2` limbs. Each row adds
|
|
328
348
|
* `a b[i]` into `t`, then the multiple `m n` of the modulus that clears the
|
|
329
349
|
* low limb, and shifts down a limb; `t` stays below 2n, and one subtraction
|
|
330
|
-
* of `n` brings it below `n`. Every sum is
|
|
331
|
-
* + (2^32 - 1) = 2^64 - 1, so a `u64` holds it.
|
|
350
|
+
* of `n` brings it below `n`. Every sum is
|
|
351
|
+
* at most (2^32 - 1) + (2^32 - 1)^2 + (2^32 - 1) = 2^64 - 1, so a `u64` holds it.
|
|
332
352
|
*/
|
|
333
353
|
const rsaMontMul = (out: u32[], a: u32[], b: u32[], n: u32[], n0inv: u32, t: u32[], count: i32): void => {
|
|
334
354
|
if (
|
|
@@ -553,6 +573,12 @@ const rsaSameBytes = (a: u8[], b: u8[]): boolean => {
|
|
|
553
573
|
* §9.2) and compared with what it does open to. `false` for an unknown hash
|
|
554
574
|
* id, a modulus too short for the encoding, a signature that is not exactly
|
|
555
575
|
* as long as the modulus or not below it, and any other bytes.
|
|
576
|
+
*
|
|
577
|
+
* @param key The signer's public key.
|
|
578
|
+
* @param hash `RSA_HASH_SHA256`, `RSA_HASH_SHA384` or `RSA_HASH_SHA512`.
|
|
579
|
+
* @param msg The message signed, whole; it is hashed here.
|
|
580
|
+
* @param sig The signature, big-endian.
|
|
581
|
+
* @returns `true` only when `sig` is `key`'s signature of `msg`.
|
|
556
582
|
*/
|
|
557
583
|
export const rsaVerifyPkcs1v15 = (key: RsaPublicKey, hash: i32, msg: u8[], sig: u8[]): boolean => {
|
|
558
584
|
const hashSize: i32 = rsaHashSize(hash)
|
|
@@ -613,6 +639,14 @@ const rsaMgf1 = (hash: i32, seed: u8[], length: i32): u8[] => {
|
|
|
613
639
|
* the salt, and the hash. `false` for any of them, an unknown hash id, a
|
|
614
640
|
* negative `saltLen`, and a signature that is not exactly as long as the
|
|
615
641
|
* modulus or not below it.
|
|
642
|
+
*
|
|
643
|
+
* @param key The signer's public key.
|
|
644
|
+
* @param hash `RSA_HASH_SHA256`, `RSA_HASH_SHA384` or `RSA_HASH_SHA512`.
|
|
645
|
+
* @param saltLen The salt's length in bytes; a signature with any other
|
|
646
|
+
* length is refused.
|
|
647
|
+
* @param msg The message signed, whole; it is hashed here.
|
|
648
|
+
* @param sig The signature, big-endian.
|
|
649
|
+
* @returns `true` only when `sig` is `key`'s signature of `msg`.
|
|
616
650
|
*/
|
|
617
651
|
export const rsaVerifyPss = (key: RsaPublicKey, hash: i32, saltLen: i32, msg: u8[], sig: u8[]): boolean => {
|
|
618
652
|
const hashSize: i32 = rsaHashSize(hash)
|
package/std/crypto/sha1.ts
CHANGED
|
@@ -100,6 +100,9 @@ const sha1Compress = (h: u32[], w: u32[], src: u8[], at: i32): void => {
|
|
|
100
100
|
* An array longer than 2^31 - 1 bytes panics, for the reason `sha256` gives:
|
|
101
101
|
* under `--number-mode f64`, `toI32` of a longer length saturates, and the
|
|
102
102
|
* answer would be the digest of a prefix.
|
|
103
|
+
*
|
|
104
|
+
* @param data The whole message; it is read, not changed.
|
|
105
|
+
* @returns A fresh 20-byte digest.
|
|
103
106
|
*/
|
|
104
107
|
export const sha1 = (data: u8[]): u8[] => {
|
|
105
108
|
if (data.length > 2147483647) {
|
package/std/crypto/sha256.ts
CHANGED
|
@@ -435,6 +435,8 @@ export class Sha256 {
|
|
|
435
435
|
* The message length absorbed so far, in bytes. An `i64` because §5.1.1
|
|
436
436
|
* appends the length in bits as a 64-bit number, and a transcript can pass
|
|
437
437
|
* 2^31 bytes where an `i32` would wrap.
|
|
438
|
+
*
|
|
439
|
+
* @internal Set by `nish/crypto/hmac` and `nish/net/postgres` when they wipe, copy or restart a hasher.
|
|
438
440
|
*/
|
|
439
441
|
total: i64 = 0
|
|
440
442
|
/** H_0 .. H_7, the eight words of the intermediate hash value (§6.2.2 step 4). */
|
|
@@ -447,10 +449,19 @@ export class Sha256 {
|
|
|
447
449
|
* it once rather than once per block.
|
|
448
450
|
*/
|
|
449
451
|
schedule: u32[]
|
|
450
|
-
/**
|
|
452
|
+
/**
|
|
453
|
+
* How many bytes of `block` are live, always below `SHA256_BLOCK` between calls.
|
|
454
|
+
*
|
|
455
|
+
* @internal Set by `nish/crypto/hmac` and `nish/net/postgres` when they wipe, copy or restart a hasher.
|
|
456
|
+
*/
|
|
451
457
|
fill: i32 = 0
|
|
458
|
+
/** @internal Set by `nish/crypto/hmac` and `nish/net/postgres` when they wipe, copy or restart a hasher. */
|
|
452
459
|
finished: boolean = false
|
|
453
460
|
|
|
461
|
+
/**
|
|
462
|
+
* A hasher at the start of an empty message (FIPS 180-4 §5.3.3's initial hash
|
|
463
|
+
* value).
|
|
464
|
+
*/
|
|
454
465
|
constructor() {
|
|
455
466
|
// H^(0) of FIPS 180-4 §5.3.3: the first 32 bits of the fractional parts
|
|
456
467
|
// of the square roots of the first eight primes.
|
|
@@ -472,6 +483,10 @@ export class Sha256 {
|
|
|
472
483
|
* Absorbs `data[off .. off + len)`. A window outside `data` panics rather
|
|
473
484
|
* than hashing a short read, since a digest over the wrong bytes looks
|
|
474
485
|
* exactly like a digest over the right ones.
|
|
486
|
+
*
|
|
487
|
+
* @param data The buffer holding the window; it is read, not kept.
|
|
488
|
+
* @param off Where the window starts in `data`.
|
|
489
|
+
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
475
490
|
*/
|
|
476
491
|
update(data: u8[], off: i32, len: i32): void {
|
|
477
492
|
if (this.finished) {
|
|
@@ -513,6 +528,9 @@ export class Sha256 {
|
|
|
513
528
|
* An independent hasher at the same point in the same message: updating or
|
|
514
529
|
* digesting either one leaves the other as it was. This is how a caller
|
|
515
530
|
* takes the hash of a prefix and keeps going.
|
|
531
|
+
*
|
|
532
|
+
* @returns A new hasher with its own state, block and schedule; it shares no
|
|
533
|
+
* array with this one.
|
|
516
534
|
*/
|
|
517
535
|
copy(): Sha256 {
|
|
518
536
|
if (this.finished) {
|
|
@@ -539,6 +557,8 @@ export class Sha256 {
|
|
|
539
557
|
/**
|
|
540
558
|
* The 32-byte message digest (FIPS 180-4 §6.2.2, the final H^(N) in
|
|
541
559
|
* big-endian order), in a fresh array. Ends the computation.
|
|
560
|
+
*
|
|
561
|
+
* @returns A fresh 32-byte digest, which the caller owns.
|
|
542
562
|
*/
|
|
543
563
|
digest(): u8[] {
|
|
544
564
|
if (this.finished) {
|
|
@@ -590,6 +610,9 @@ export class Sha256 {
|
|
|
590
610
|
* hashing `toI32(data.length)` bytes would answer the digest of a prefix: one
|
|
591
611
|
* digest for every message that shares its first 2^31 - 1 bytes. The length
|
|
592
612
|
* is compared as the `number` it is, before any `toI32`.
|
|
613
|
+
*
|
|
614
|
+
* @param data The whole message; it is read, not changed.
|
|
615
|
+
* @returns A fresh 32-byte digest.
|
|
593
616
|
*/
|
|
594
617
|
export const sha256 = (data: u8[]): u8[] => {
|
|
595
618
|
if (data.length > 2147483647) {
|
package/std/crypto/sha512.ts
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `nish/crypto/sha512` — SHA-512 and SHA-384, written from FIPS 180-4.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* starts from its own initial value (§5.3.4) and keeps the first 48 bytes of
|
|
6
|
-
* the result (§6.5), SHA-512 starts from §5.3.5 and keeps all 64. So one
|
|
7
|
-
* engine does the work, and `Sha512` and `Sha384` are the two ways to start it.
|
|
4
|
+
* import { sha512, sha384, Sha512 } from "nish/crypto/sha512";
|
|
8
5
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
6
|
+
* const whole: u8[] = sha512(message); // 64 bytes; sha384 answers 48
|
|
7
|
+
*
|
|
8
|
+
* const h = new Sha512();
|
|
9
|
+
* h.update(first, 0, toI32(first.length));
|
|
10
|
+
* h.update(second, 0, toI32(second.length));
|
|
11
|
+
* const all: u8[] = h.digest();
|
|
12
|
+
*
|
|
13
|
+
* **One engine, two hashes.** Both hashes are one compression function over
|
|
14
|
+
* eight 64-bit words: SHA-384 starts from its own initial value (§5.3.4) and
|
|
15
|
+
* keeps the first 48 bytes of the result (§6.5), SHA-512 starts from §5.3.5 and
|
|
16
|
+
* keeps all 64. So one engine does the work, and `Sha512` and `Sha384` are the
|
|
17
|
+
* two ways to start it.
|
|
18
|
+
*
|
|
19
|
+
* **Time says only the length.** A hasher streams: `update` any number of
|
|
20
|
+
* windows, `copy` to take an independent hasher at the same point (TLS 1.3
|
|
21
|
+
* hashes its transcript and reads intermediate hashes of it), then `digest`
|
|
22
|
+
* once. The work depends on how many bytes arrive and never on their values:
|
|
23
|
+
* there is no branch and no table index on message data, so a secret message
|
|
24
|
+
* (an HMAC key, a transcript) is hashed in time that says nothing about it but
|
|
25
|
+
* its length.
|
|
15
26
|
*
|
|
16
27
|
* The constants are the fractional parts of the cube roots of the first eighty
|
|
17
28
|
* primes (§4.2.3) and of the square roots of the first sixteen (§5.3.4, §5.3.5),
|
|
@@ -276,8 +287,11 @@ class Sha512Engine {
|
|
|
276
287
|
block: u8[]
|
|
277
288
|
/** The message schedule W_0 .. W_79, kept so a block allocates nothing. */
|
|
278
289
|
schedule: u64[]
|
|
290
|
+
/** @internal Set by `nish/crypto/hmac` when it wipes, copies or restarts a hasher. */
|
|
279
291
|
count: u64 = 0
|
|
292
|
+
/** @internal Set by `nish/crypto/hmac` when it wipes, copies or restarts a hasher. */
|
|
280
293
|
filled: i32 = 0
|
|
294
|
+
/** @internal Set by `nish/crypto/hmac` when it wipes, copies or restarts a hasher. */
|
|
281
295
|
finished: boolean = false
|
|
282
296
|
|
|
283
297
|
constructor(initial: u64[]) {
|
|
@@ -287,7 +301,7 @@ class Sha512Engine {
|
|
|
287
301
|
}
|
|
288
302
|
|
|
289
303
|
/** One application of §6.4.2 to the block at `src[off]` .. `src[off + 127]`. */
|
|
290
|
-
compress(src: u8[], off: i32): void {
|
|
304
|
+
private compress(src: u8[], off: i32): void {
|
|
291
305
|
const w = this.schedule
|
|
292
306
|
const h = this.state
|
|
293
307
|
const wLen: i32 = toI32(w.length)
|
|
@@ -358,6 +372,7 @@ class Sha512Engine {
|
|
|
358
372
|
h[7] = h[7] + hh
|
|
359
373
|
}
|
|
360
374
|
|
|
375
|
+
/** @internal Called by `Sha512` and `Sha384`, which hold the engine. */
|
|
361
376
|
absorb(data: u8[], off: i32, len: i32): void {
|
|
362
377
|
if (this.finished) {
|
|
363
378
|
panic("sha512: update after digest")
|
|
@@ -397,6 +412,8 @@ class Sha512Engine {
|
|
|
397
412
|
/**
|
|
398
413
|
* Makes `twin` this engine at the same point. `twin` is a fresh engine of the
|
|
399
414
|
* same hash, so its arrays are reused rather than allocated a second time.
|
|
415
|
+
*
|
|
416
|
+
* @internal Called by `Sha512` and `Sha384`, which hold the engine.
|
|
400
417
|
*/
|
|
401
418
|
copyInto(twin: Sha512Engine): void {
|
|
402
419
|
for (let i: i32 = 0; i < 8; i++) {
|
|
@@ -410,7 +427,11 @@ class Sha512Engine {
|
|
|
410
427
|
twin.finished = this.finished
|
|
411
428
|
}
|
|
412
429
|
|
|
413
|
-
/**
|
|
430
|
+
/**
|
|
431
|
+
* Pads (§5.1.2), compresses what is left, and answers the first `size` bytes of H(N) (§6.4.2, §6.5).
|
|
432
|
+
*
|
|
433
|
+
* @internal Called by `Sha512` and `Sha384`, which hold the engine.
|
|
434
|
+
*/
|
|
414
435
|
finish(size: i32): u8[] {
|
|
415
436
|
if (this.finished) {
|
|
416
437
|
panic("sha512: digest taken twice")
|
|
@@ -446,23 +467,52 @@ class Sha512Engine {
|
|
|
446
467
|
* another `update` or `digest` panics.
|
|
447
468
|
*/
|
|
448
469
|
export class Sha512 {
|
|
470
|
+
/**
|
|
471
|
+
* The engine the hash runs on. `nish/crypto/hmac` and `nish/crypto/ed25519`
|
|
472
|
+
* reach into it to wipe a hasher, and the tests to check the wipe; a caller
|
|
473
|
+
* has no use for it.
|
|
474
|
+
*/
|
|
449
475
|
engine: Sha512Engine
|
|
450
476
|
|
|
477
|
+
/**
|
|
478
|
+
* A hasher at the start of an empty message (FIPS 180-4 §5.3.5's initial hash
|
|
479
|
+
* value).
|
|
480
|
+
*/
|
|
451
481
|
constructor() {
|
|
452
482
|
this.engine = new Sha512Engine(sha512Initial())
|
|
453
483
|
}
|
|
454
484
|
|
|
485
|
+
/**
|
|
486
|
+
* Absorbs `data[off .. off + len)`. A window outside `data`, or an update
|
|
487
|
+
* after `digest`, panics.
|
|
488
|
+
*
|
|
489
|
+
* @param data The buffer holding the window; it is read, not kept.
|
|
490
|
+
* @param off Where the window starts in `data`.
|
|
491
|
+
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
492
|
+
*/
|
|
455
493
|
update(data: u8[], off: i32, len: i32): void {
|
|
456
494
|
this.engine.absorb(data, off, len)
|
|
457
495
|
}
|
|
458
496
|
|
|
459
|
-
/**
|
|
497
|
+
/**
|
|
498
|
+
* An independent hasher at the same point: updating either leaves the other
|
|
499
|
+
* alone.
|
|
500
|
+
*
|
|
501
|
+
* @returns A new hasher with its own engine; it shares no array with this
|
|
502
|
+
* one.
|
|
503
|
+
*/
|
|
460
504
|
copy(): Sha512 {
|
|
461
505
|
const twin = new Sha512()
|
|
462
506
|
this.engine.copyInto(twin.engine)
|
|
463
507
|
return twin
|
|
464
508
|
}
|
|
465
509
|
|
|
510
|
+
/**
|
|
511
|
+
* Pads the message (§5.1.2) and answers its digest. Ends the computation: a
|
|
512
|
+
* second `digest` panics.
|
|
513
|
+
*
|
|
514
|
+
* @returns A fresh 64-byte digest, which the caller owns.
|
|
515
|
+
*/
|
|
466
516
|
digest(): u8[] {
|
|
467
517
|
return this.engine.finish(SHA512_SIZE)
|
|
468
518
|
}
|
|
@@ -473,23 +523,52 @@ export class Sha512 {
|
|
|
473
523
|
* fresh 48-byte array.
|
|
474
524
|
*/
|
|
475
525
|
export class Sha384 {
|
|
526
|
+
/**
|
|
527
|
+
* The engine the hash runs on. `nish/crypto/hmac` and `nish/crypto/ed25519`
|
|
528
|
+
* reach into it to wipe a hasher, and the tests to check the wipe; a caller
|
|
529
|
+
* has no use for it.
|
|
530
|
+
*/
|
|
476
531
|
engine: Sha512Engine
|
|
477
532
|
|
|
533
|
+
/**
|
|
534
|
+
* A hasher at the start of an empty message (FIPS 180-4 §5.3.4's initial hash
|
|
535
|
+
* value).
|
|
536
|
+
*/
|
|
478
537
|
constructor() {
|
|
479
538
|
this.engine = new Sha512Engine(sha384Initial())
|
|
480
539
|
}
|
|
481
540
|
|
|
541
|
+
/**
|
|
542
|
+
* Absorbs `data[off .. off + len)`. A window outside `data`, or an update
|
|
543
|
+
* after `digest`, panics.
|
|
544
|
+
*
|
|
545
|
+
* @param data The buffer holding the window; it is read, not kept.
|
|
546
|
+
* @param off Where the window starts in `data`.
|
|
547
|
+
* @param len How many bytes to absorb; 0 absorbs nothing.
|
|
548
|
+
*/
|
|
482
549
|
update(data: u8[], off: i32, len: i32): void {
|
|
483
550
|
this.engine.absorb(data, off, len)
|
|
484
551
|
}
|
|
485
552
|
|
|
486
|
-
/**
|
|
553
|
+
/**
|
|
554
|
+
* An independent hasher at the same point: updating either leaves the other
|
|
555
|
+
* alone.
|
|
556
|
+
*
|
|
557
|
+
* @returns A new hasher with its own engine; it shares no array with this
|
|
558
|
+
* one.
|
|
559
|
+
*/
|
|
487
560
|
copy(): Sha384 {
|
|
488
561
|
const twin = new Sha384()
|
|
489
562
|
this.engine.copyInto(twin.engine)
|
|
490
563
|
return twin
|
|
491
564
|
}
|
|
492
565
|
|
|
566
|
+
/**
|
|
567
|
+
* Pads the message (§5.1.2) and answers its digest, the first 48 bytes of the
|
|
568
|
+
* final hash value (§6.5). Ends the computation: a second `digest` panics.
|
|
569
|
+
*
|
|
570
|
+
* @returns A fresh 48-byte digest, which the caller owns.
|
|
571
|
+
*/
|
|
493
572
|
digest(): u8[] {
|
|
494
573
|
return this.engine.finish(SHA384_SIZE)
|
|
495
574
|
}
|
|
@@ -500,6 +579,9 @@ export class Sha384 {
|
|
|
500
579
|
* longer than 2^31 - 1 bytes panics, for the reason `sha256` gives: a longer
|
|
501
580
|
* length does not survive `toI32` under `--number-mode f64`, and the digest
|
|
502
581
|
* would be of a prefix.
|
|
582
|
+
*
|
|
583
|
+
* @param data The whole message; it is read, not changed.
|
|
584
|
+
* @returns A fresh 64-byte digest.
|
|
503
585
|
*/
|
|
504
586
|
export const sha512 = (data: u8[]): u8[] => {
|
|
505
587
|
if (data.length > 2147483647) {
|
|
@@ -513,6 +595,9 @@ export const sha512 = (data: u8[]): u8[] => {
|
|
|
513
595
|
/**
|
|
514
596
|
* The SHA-384 digest of all of `data`, as a fresh 48-byte array. An array
|
|
515
597
|
* longer than 2^31 - 1 bytes panics, as in `sha512`.
|
|
598
|
+
*
|
|
599
|
+
* @param data The whole message; it is read, not changed.
|
|
600
|
+
* @returns A fresh 48-byte digest.
|
|
516
601
|
*/
|
|
517
602
|
export const sha384 = (data: u8[]): u8[] => {
|
|
518
603
|
if (data.length > 2147483647) {
|