@blamejs/pki 0.2.0 → 0.2.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/CHANGELOG.md +13 -0
- package/README.md +1 -0
- package/index.js +6 -0
- package/lib/constants.js +4 -0
- package/lib/framework-error.js +2 -0
- package/lib/oid.js +11 -1
- package/lib/shbs.js +348 -0
- package/package.json +1 -1
- package/sbom.cdx.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,19 @@ All notable changes to `@blamejs/pki` are documented here. The format
|
|
|
4
4
|
follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); this
|
|
5
5
|
project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
+
## v0.2.1 — 2026-07-12
|
|
8
|
+
|
|
9
|
+
Stateful hash-based signature verification (HSS/LMS) joins the toolkit as pki.shbs.
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- pki.shbs.verify(publicKey, message, signature) verifies an HSS (Hierarchical Signature System) signature -- the wire form RFC 9802 (X.509) and RFC 9708 (CMS) carry for id-alg-hss-lms-hashsig -- returning true only if every level of the hierarchy verifies. pki.shbs.verifyLms(publicKey, message, signature) verifies a single-tree LMS signature (the component HSS composes, and a standalone algorithm). Both take the raw octet blobs the parsers surface (a certificate's subjectPublicKeyInfo.publicKey.bytes, tbsBytes, and signatureValue.bytes); a malformed blob -- bad length, an unknown or unapproved typecode, truncation, a typecode the public key does not commit to -- throws a typed ShbsError, and a well-formed but wrong signature returns false. RFC 8554 / RFC 9802 / RFC 9708 / NIST SP 800-208.
|
|
14
|
+
- The OID registry gains id-alg-hss-lms-hashsig (1.2.840.113549.1.9.16.3.17), id-alg-xmss-hashsig, and id-alg-xmssmt-hashsig, all with parameters MUST be absent (RFC 9802 sec. 4) -- a stateful-hash-signature AlgorithmIdentifier carrying any parameters now fails closed at the shared algorithm-identifier gate, inherited by every format the toolkit parses. The error taxonomy gains ShbsError (shbs/*).
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- XMSS / XMSS^MT verification and automatic HSS/LMS verification inside pki.path.validate are not in this release -- see the roadmap. The former awaits an authoritative interoperability test vector (RFC 8391 ships none and NIST ACVP does not yet cover XMSS); the latter awaits a real HSS-signed certificate to prove the certification-path wiring end to end. Operators verify today by handing the raw certificate / CMS blobs to pki.shbs.verify directly.
|
|
19
|
+
|
|
7
20
|
## v0.2.0 — 2026-07-11
|
|
8
21
|
|
|
9
22
|
Trust-store ingestion, sharded-CRL revocation, and a hardened input-guard layer.
|
package/README.md
CHANGED
|
@@ -224,6 +224,7 @@ is callable today; nothing below is a stub.
|
|
|
224
224
|
| `pki.ct` | Parse RFC 6962 Certificate Transparency SCT lists — `parseSctList` decodes the `SignedCertificateTimestampList` a certificate or OCSP response carries in the SCT extension (a TLS-presentation-language payload inside the §3.3 double DER wrap) into per-SCT log id, exact `timestamp` (BigInt), named signature algorithm, and raw signature; `reconstructSignedData` rebuilds the exact `digitally-signed` preimage for external verification. Structure decoded, crypto surfaced raw, bounded decode, fail-closed — `parseSctList`, `reconstructSignedData` |
|
|
225
225
|
| `pki.merkle` | RFC 6962 / RFC 9162 Merkle-tree proof verification — `leafHash` / `nodeHash` / `emptyRootHash` build the domain-separated (0x00 leaf / 0x01 node) SHA-256 tree hashes; `verifyInclusion` folds an audit proof back to a root and `verifyConsistency` reconstructs both the old and new root (the append-only guarantee), each constant-time-compared to a trusted checkpoint root. Fail-closed on bad geometry, sync hashing, transport-free — `leafHash`, `nodeHash`, `emptyRootHash`, `verifyInclusion`, `verifyConsistency` |
|
|
226
226
|
| `pki.trust` | Mozilla / CCADB trust-store ingestion — `parseCertdata` reads the NSS `certdata.txt` object stream and `parseCcadbCsv` the CCADB CSV export into one identical constraint-carrying anchor shape: the per-purpose trust bits (only `CKT_NSS_TRUSTED_DELEGATOR` grants) and the per-purpose distrust-after dates the bare root list omits. Certificate and trust objects pair by byte-exact issuer + serial (never adjacency) and are cross-checked against the parsed DER, so metadata can never attach to the wrong root; `anchor()` hands an entry to `pki.path.validate({ trustAnchor, checkPurpose })`. Offline, fail-closed, bounded — `parseCertdata`, `parseCcadbCsv`, `anchor` |
|
|
227
|
+
| `pki.shbs` | Stateful hash-based signature **verification** — HSS/LMS (RFC 8554), carried in X.509 by RFC 9802 and CMS by RFC 9708, profiled by NIST SP 800-208 (CNSA 2.0 firmware signing). `verify` checks an HSS signature (every level must pass) and `verifyLms` a single-tree LMS, over the raw public-key / signature blobs the parsers already surface. Pure public-input SHA-256 / SHAKE256 hashing, a data-driven typecode registry, bounds-before-slice reads; a malformed blob throws a typed `ShbsError`, a well-formed-but-wrong signature returns `false`. **Verify only by design** — stateful signing needs atomic one-time-key state that belongs in an HSM — `verify`, `verifyLms` |
|
|
227
228
|
| `pki.C` / `pki.constants` | Version-stable constants — functional scale helpers (`C.TIME.*`, `C.BYTES.*`), codec `LIMITS`, `version` |
|
|
228
229
|
| `pki.errors` | The `PkiError` taxonomy — `defineClass` plus `ConstantsError` / `Asn1Error` / `OidError` / `PemError` / `CertificateError` / `CrlError` / `CsrError` / `Pkcs8Error` / `CmsError` / `OcspError` / `TspError` / `AttrCertError` / `CrmfError` / `Pkcs12Error` / `CmpError` / `PathError` / `CtError` / `JoseError` / `AcmeError`, each carrying a stable `code` in `domain/reason` form |
|
|
229
230
|
| `pki` CLI | `pki version`, `pki oid <dotted\|name>`, `pki parse <cert>` |
|
package/index.js
CHANGED
|
@@ -37,6 +37,7 @@ var schema = require("./lib/schema-all");
|
|
|
37
37
|
var path = require("./lib/path-validate");
|
|
38
38
|
var ct = require("./lib/ct");
|
|
39
39
|
var merkle = require("./lib/merkle");
|
|
40
|
+
var shbs = require("./lib/shbs");
|
|
40
41
|
var est = require("./lib/est");
|
|
41
42
|
var jose = require("./lib/jose");
|
|
42
43
|
var acme = require("./lib/acme");
|
|
@@ -73,6 +74,11 @@ module.exports = {
|
|
|
73
74
|
// verifyConsistency fold an audit / consistency proof and constant-time-
|
|
74
75
|
// compare to a checkpoint root. Pure sync hashing, fail-closed, transport-free.
|
|
75
76
|
merkle: merkle,
|
|
77
|
+
// `shbs` verifies stateful hash-based signatures -- HSS/LMS (RFC 8554),
|
|
78
|
+
// carried by RFC 9802 (X.509) and RFC 9708 (CMS), profiled by NIST SP 800-208.
|
|
79
|
+
// VERIFY ONLY by design: stateful signing requires atomic one-time-key index
|
|
80
|
+
// state that belongs in an HSM, so this module never mints a signature.
|
|
81
|
+
shbs: shbs,
|
|
76
82
|
// `est` is RFC 7030 / 8951 / 9908 Enrollment over Secure Transport -- the
|
|
77
83
|
// transport-agnostic client codecs (base64 transfer, multipart splitter),
|
|
78
84
|
// certs-only + serverkeygen validators over CMS, the enroll-attribute builders,
|
package/lib/constants.js
CHANGED
|
@@ -237,6 +237,10 @@ var LIMITS = {
|
|
|
237
237
|
// relayed response with a huge certs list would drive unbounded pre-auth
|
|
238
238
|
// signature verifies. 32 is generous headroom over a real chain.
|
|
239
239
|
OCSP_MAX_CERTS: 32,
|
|
240
|
+
// HSS hierarchy depth (RFC 8554 sec. 6.1: L is 1..8). The HSS verifier caps
|
|
241
|
+
// its per-level parse loop at this from the registry, never at the blob's own
|
|
242
|
+
// Nspk, so a hostile signature cannot drive an unbounded level walk.
|
|
243
|
+
HSS_MAX_LEVELS: 8,
|
|
240
244
|
// Trust-store ingestion ceilings (Mozilla/NSS certdata.txt + CCADB CSV).
|
|
241
245
|
// Every cap is refused BEFORE the offending allocation with a typed trust/*
|
|
242
246
|
// error -- never a silent truncate. A live certdata.txt is a few MiB and a
|
package/lib/framework-error.js
CHANGED
|
@@ -281,6 +281,7 @@ var AcmeError = defineClass("AcmeError", { withCause: true });
|
|
|
281
281
|
// not a trusted delegator for. Carries the underlying leaf fault (an x509/* or
|
|
282
282
|
// asn1/* error) as `.cause`.
|
|
283
283
|
var TrustError = defineClass("TrustError", { withCause: true });
|
|
284
|
+
var ShbsError = defineClass("ShbsError", { withCause: true });
|
|
284
285
|
|
|
285
286
|
module.exports = {
|
|
286
287
|
PkiError: PkiError,
|
|
@@ -304,6 +305,7 @@ module.exports = {
|
|
|
304
305
|
CmpError: CmpError,
|
|
305
306
|
PathError: PathError,
|
|
306
307
|
CtError: CtError,
|
|
308
|
+
ShbsError: ShbsError,
|
|
307
309
|
MerkleError: MerkleError,
|
|
308
310
|
SmimeError: SmimeError,
|
|
309
311
|
CsrattrsError: CsrattrsError,
|
package/lib/oid.js
CHANGED
|
@@ -201,7 +201,14 @@ var FAMILIES = {
|
|
|
201
201
|
// CEK-HKDF content-encryption wrapper (RFC 9709) a KEMRecipientInfo names, plus
|
|
202
202
|
// the RSA-KEM SPKI algorithm (RFC 9690). Parameters are absent for the HKDFs.
|
|
203
203
|
smimeAlg: { base: [1, 2, 840, 113549, 1, 9, 16, 3], of: {
|
|
204
|
-
"id-rsa-kem": 14,
|
|
204
|
+
"id-rsa-kem": 14, "id-alg-hss-lms-hashsig": 17,
|
|
205
|
+
hkdfWithSha256: 28, hkdfWithSha384: 29, hkdfWithSha512: 30, cekHkdfSha256: 31 } },
|
|
206
|
+
|
|
207
|
+
// PKIX algorithms arc -- the stateful hash-based signature algorithm
|
|
208
|
+
// identifiers (RFC 9802 sec. 4). HSS/LMS additionally has the SMIME
|
|
209
|
+
// id-alg-hss-lms-hashsig OID above (RFC 9708 / RFC 9802 share it).
|
|
210
|
+
pkixAlg: { base: [1, 3, 6, 1, 5, 5, 7, 6], of: {
|
|
211
|
+
"id-alg-xmss-hashsig": 34, "id-alg-xmssmt-hashsig": 35 } },
|
|
205
212
|
|
|
206
213
|
// RSA-KEM key-transport algorithm (RFC 9690, obsoletes RFC 5990) on the ISO
|
|
207
214
|
// 18033-2 arc -- the kem OID an RSA KEMRecipientInfo carries (distinct from the
|
|
@@ -501,6 +508,9 @@ var _PARAMS_ABSENT = new Set();
|
|
|
501
508
|
// HKDF key-derivation identifiers (RFC 8619 sec. 2: when any of these appear
|
|
502
509
|
// within AlgorithmIdentifier, the parameters component SHALL be absent).
|
|
503
510
|
"hkdfWithSha256", "hkdfWithSha384", "hkdfWithSha512",
|
|
511
|
+
// Stateful hash-based signatures (RFC 9802 sec. 4 / RFC 9708): the parameters
|
|
512
|
+
// field MUST be absent for HSS/LMS, XMSS, and XMSS^MT public keys and signatures.
|
|
513
|
+
"id-alg-hss-lms-hashsig", "id-alg-xmss-hashsig", "id-alg-xmssmt-hashsig",
|
|
504
514
|
].forEach(function (nm) {
|
|
505
515
|
var d = byName(nm);
|
|
506
516
|
// A seed-list typo must fail at module load -- admitting undefined would
|
package/lib/shbs.js
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (c) blamejs contributors
|
|
3
|
+
"use strict";
|
|
4
|
+
/**
|
|
5
|
+
* @module pki.shbs
|
|
6
|
+
* @nav Signatures
|
|
7
|
+
* @title Stateful hash-based
|
|
8
|
+
* @intro Stateful hash-based signature VERIFICATION -- HSS/LMS (RFC 8554),
|
|
9
|
+
* carried in X.509 by RFC 9802 and in CMS by RFC 9708, profiled by NIST
|
|
10
|
+
* SP 800-208. VERIFY ONLY, by deliberate design: stateful hash-based SIGNING
|
|
11
|
+
* is catastrophic to get wrong -- each one-time key must be used exactly once,
|
|
12
|
+
* so the private key embeds a monotonic index whose state must advance and
|
|
13
|
+
* persist atomically across every signature and every process restart. A single
|
|
14
|
+
* index reuse (a restored VM snapshot, a crashed writer, a concurrent signer)
|
|
15
|
+
* forfeits security and can leak enough one-time-key material to forge, which is
|
|
16
|
+
* why SP 800-208 sec. 8 constrains signing-state handling to hardware. So this
|
|
17
|
+
* module NEVER mints a signature -- it verifies signatures produced in an HSM
|
|
18
|
+
* elsewhere. Verification is pure public-input SHA-256 / SHAKE256 hashing
|
|
19
|
+
* (no secret, no side-channel surface), so a pure-JavaScript verifier is safe.
|
|
20
|
+
* @spec RFC 8554, RFC 9802, RFC 9708, NIST SP 800-208
|
|
21
|
+
* @card Verify HSS/LMS signatures (post-quantum, CNSA 2.0 firmware signing).
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
var nodeCrypto = require("crypto");
|
|
25
|
+
var frameworkError = require("./framework-error");
|
|
26
|
+
var guard = require("./guard-all");
|
|
27
|
+
var constants = require("./constants");
|
|
28
|
+
|
|
29
|
+
var ShbsError = frameworkError.ShbsError;
|
|
30
|
+
function _err(code, message, cause) { return new ShbsError(code, message, cause); }
|
|
31
|
+
|
|
32
|
+
// ---- big-endian serialization (RFC 8554 sec. 3.1: network byte order) --------
|
|
33
|
+
|
|
34
|
+
function u8(x) { return Buffer.from([x & 0xff]); }
|
|
35
|
+
function u16(x) { return Buffer.from([(x >> 8) & 0xff, x & 0xff]); }
|
|
36
|
+
function u32(x) { return Buffer.from([(x >>> 24) & 0xff, (x >>> 16) & 0xff, (x >>> 8) & 0xff, x & 0xff]); }
|
|
37
|
+
|
|
38
|
+
// Domain separators (RFC 8554 sec. 7.1). All exceed 264 (the max Winternitz
|
|
39
|
+
// digit index i) so a security-string D can never collide with a chain-hash i,
|
|
40
|
+
// which occupies the same 2-byte offset (sec. 9.1).
|
|
41
|
+
var D_PBLC = 0x8080; // LM-OTS public-key finalize
|
|
42
|
+
var D_MESG = 0x8181; // message hash
|
|
43
|
+
var D_LEAF = 0x8282; // LMS leaf hash
|
|
44
|
+
var D_INTR = 0x8383; // LMS interior node hash
|
|
45
|
+
|
|
46
|
+
// ---- typecode registry (IANA Leighton-Micali Signatures; NIST SP 800-208) ----
|
|
47
|
+
// LMS 0x05..0x18 : {m, h, hashFamily}. LM-OTS 0x01..0x10 : {n, w, hashFamily}.
|
|
48
|
+
// p and ls are DERIVED from (n, w) per RFC 8554 sec. 4.1 / Appendix B so the
|
|
49
|
+
// table stays data, not a switch, and a wrong hardcoded p cannot creep in.
|
|
50
|
+
|
|
51
|
+
function _ilog2(x) { var r = 0; while (x > 1) { x = Math.floor(x / 2); r += 1; } return r; }
|
|
52
|
+
|
|
53
|
+
// RFC 8554 Appendix B: u = ceil(8n/w) hash digits; v = the checksum digit count;
|
|
54
|
+
// p = u + v total Winternitz digits; ls = the left-shift aligning the checksum's
|
|
55
|
+
// significant bits into the digit positions coef reads.
|
|
56
|
+
function _deriveWinternitz(n, w) {
|
|
57
|
+
var u = Math.ceil((8 * n) / w);
|
|
58
|
+
var v = Math.ceil((_ilog2((Math.pow(2, w) - 1) * u) + 1) / w);
|
|
59
|
+
return { p: u + v, ls: 16 - v * w };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
var LMS_SETS = {};
|
|
63
|
+
var LMOTS_SETS = {};
|
|
64
|
+
(function seed() {
|
|
65
|
+
var heights = [5, 10, 15, 20, 25]; // H5..H25 in typecode order
|
|
66
|
+
var ws = [1, 2, 4, 8]; // W1..W8 in typecode order
|
|
67
|
+
// LMS families in IANA order: SHA-256/M32 (0x05), SHA-256/M24 (0x0A),
|
|
68
|
+
// SHAKE/M32 (0x0F), SHAKE/M24 (0x14).
|
|
69
|
+
[{ base: 0x05, m: 32, f: "sha256" }, { base: 0x0A, m: 24, f: "sha256" },
|
|
70
|
+
{ base: 0x0F, m: 32, f: "shake256" }, { base: 0x14, m: 24, f: "shake256" }].forEach(function (fam) {
|
|
71
|
+
heights.forEach(function (h, i) { LMS_SETS[fam.base + i] = { code: fam.base + i, m: fam.m, h: h, hashFamily: fam.f }; });
|
|
72
|
+
});
|
|
73
|
+
// LM-OTS families: SHA-256/N32 (0x01), SHA-256/N24 (0x05), SHAKE/N32 (0x09),
|
|
74
|
+
// SHAKE/N24 (0x0D).
|
|
75
|
+
[{ base: 0x01, n: 32, f: "sha256" }, { base: 0x05, n: 24, f: "sha256" },
|
|
76
|
+
{ base: 0x09, n: 32, f: "shake256" }, { base: 0x0D, n: 24, f: "shake256" }].forEach(function (fam) {
|
|
77
|
+
ws.forEach(function (w, i) {
|
|
78
|
+
var d = _deriveWinternitz(fam.n, w);
|
|
79
|
+
LMOTS_SETS[fam.base + i] = { code: fam.base + i, n: fam.n, w: w, p: d.p, ls: d.ls, hashFamily: fam.f };
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
})();
|
|
83
|
+
|
|
84
|
+
function _lmsSet(code) {
|
|
85
|
+
var s = LMS_SETS[code];
|
|
86
|
+
if (!s) throw _err("shbs/unsupported-parameter-set", "unrecognized or unapproved LMS typecode 0x" + code.toString(16));
|
|
87
|
+
return s;
|
|
88
|
+
}
|
|
89
|
+
function _lmotsSet(code) {
|
|
90
|
+
var s = LMOTS_SETS[code];
|
|
91
|
+
if (!s) throw _err("shbs/unsupported-parameter-set", "unrecognized or unapproved LM-OTS typecode 0x" + code.toString(16));
|
|
92
|
+
return s;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// hashFamily + output length -> the n-byte hash of the concatenated inputs.
|
|
96
|
+
// SHA-256 truncated to n (n=24 is SHA-256/192); SHAKE256 with an n-byte output.
|
|
97
|
+
function _hash(family, n, parts) {
|
|
98
|
+
var h;
|
|
99
|
+
if (family === "shake256") h = nodeCrypto.createHash("shake256", { outputLength: n });
|
|
100
|
+
else h = nodeCrypto.createHash("sha256");
|
|
101
|
+
for (var i = 0; i < parts.length; i++) h.update(parts[i]);
|
|
102
|
+
var d = h.digest();
|
|
103
|
+
return (family === "shake256" || n === 32) ? d : d.subarray(0, n);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ---- bounded big-endian reader (bounds-before-slice; the ct.js TlsReader model
|
|
107
|
+
// for a non-DER positional wire, kept shbs-local -- shbs needs u32 typecodes +
|
|
108
|
+
// fixed-n reads, not CT's length-prefixed vector helpers) --------------------
|
|
109
|
+
|
|
110
|
+
function Reader(buf, code, label) { this.buf = buf; this.pos = 0; this.code = code; this.label = label; }
|
|
111
|
+
Reader.prototype._need = function (k) {
|
|
112
|
+
if (k < 0 || this.pos + k > this.buf.length) {
|
|
113
|
+
throw _err(this.code, this.label + " is truncated (needed " + k + " byte(s) at offset " + this.pos + ", have " + (this.buf.length - this.pos) + ")");
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
Reader.prototype.u32 = function () { this._need(4); var v = this.buf.readUInt32BE(this.pos); this.pos += 4; return v; };
|
|
117
|
+
Reader.prototype.take = function (k) { this._need(k); var b = this.buf.subarray(this.pos, this.pos + k); this.pos += k; return b; };
|
|
118
|
+
Reader.prototype.remaining = function () { return this.buf.length - this.pos; };
|
|
119
|
+
Reader.prototype.atEnd = function () { return this.pos === this.buf.length; };
|
|
120
|
+
|
|
121
|
+
// ---- Winternitz digit + checksum (RFC 8554 sec. 3.1.3 / sec. 4.4) ------------
|
|
122
|
+
|
|
123
|
+
// coef(S, i, w): the i-th w-bit digit of S, big-endian WITHIN each byte (digit 0
|
|
124
|
+
// is the high-order w bits of byte 0). RFC 8554 sec. 3.1.3.
|
|
125
|
+
function _coef(S, i, w) {
|
|
126
|
+
var idx = Math.floor((i * w) / 8);
|
|
127
|
+
if (idx >= S.length) throw _err("shbs/bad-signature", "Winternitz coefficient index out of range");
|
|
128
|
+
var shift = 8 - (w * (i % (8 / w)) + w);
|
|
129
|
+
return ((1 << w) - 1) & (S[idx] >> shift);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Cksm(Q) per RFC 8554 sec. 4.4: sum over the u message digits of (2^w-1 - digit),
|
|
133
|
+
// left-shifted by ls, as a 2-byte big-endian value. Computed over Q ALONE; the
|
|
134
|
+
// coefficient index space in the chain walk spans Q || Cksm(Q).
|
|
135
|
+
function _cksm(Q, set) {
|
|
136
|
+
var w = set.w, sum = 0, u = Math.ceil((8 * set.n) / w);
|
|
137
|
+
for (var i = 0; i < u; i++) sum += ((1 << w) - 1) - _coef(Q, i, w);
|
|
138
|
+
sum = (sum << set.ls) & 0xffff;
|
|
139
|
+
return u16(sum);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ---- LM-OTS public-key candidate Kc (RFC 8554 Algorithm 4b) ------------------
|
|
143
|
+
// I, q identify the LMS leaf; otsSet is resolved from the PUBLIC KEY's otstype
|
|
144
|
+
// (the authority); C + y[] come from the signature. Returns the n-byte Kc.
|
|
145
|
+
|
|
146
|
+
function _lmotsKc(otsSet, I, q, C, y, message) {
|
|
147
|
+
var n = otsSet.n, w = otsSet.w, p = otsSet.p, f = otsSet.hashFamily;
|
|
148
|
+
var qb = u32(q);
|
|
149
|
+
var Q = _hash(f, n, [I, qb, u16(D_MESG), C, message]);
|
|
150
|
+
var Qc = Buffer.concat([Q, _cksm(Q, otsSet)]);
|
|
151
|
+
var z = [I, qb, u16(D_PBLC)];
|
|
152
|
+
for (var i = 0; i < p; i++) {
|
|
153
|
+
var a = _coef(Qc, i, w);
|
|
154
|
+
var tmp = y[i];
|
|
155
|
+
// Chain to 2^w - 1 total applications; the bound is EXCLUSIVE (last j = 2^w-2),
|
|
156
|
+
// matching key generation (RFC 8554 sec. 4.5 vs Algorithm 4b step 3).
|
|
157
|
+
for (var j = a; j < (1 << w) - 1; j++) tmp = _hash(f, n, [I, qb, u16(i), u8(j), tmp]);
|
|
158
|
+
z.push(tmp);
|
|
159
|
+
}
|
|
160
|
+
return _hash(f, n, z);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ---- LMS verification core (RFC 8554 Algorithm 6 / 6a) -----------------------
|
|
164
|
+
// pubBytes = lmstype(4) || otstype(4) || I(16) || T[1](m); sigBytes an LMS
|
|
165
|
+
// signature. Returns true iff the recomputed Merkle root equals T[1]. Throws
|
|
166
|
+
// ShbsError on any structural fault; a well-formed-but-wrong signature -> false.
|
|
167
|
+
|
|
168
|
+
function _lmsVerify(pubBytes, message, sigBytes) {
|
|
169
|
+
// -- public key (the authority for both typecodes; RFC 8554 Algorithm 6) --
|
|
170
|
+
if (pubBytes.length < 8) throw _err("shbs/bad-public-key", "LMS public key is shorter than 8 bytes");
|
|
171
|
+
var pr = new Reader(pubBytes, "shbs/bad-public-key", "LMS public key");
|
|
172
|
+
var lmsSet = _lmsSet(pr.u32());
|
|
173
|
+
var otsSet = _lmotsSet(pr.u32());
|
|
174
|
+
var m = lmsSet.m, h = lmsSet.h;
|
|
175
|
+
if (pubBytes.length !== 24 + m) throw _err("shbs/bad-public-key", "LMS public key must be exactly " + (24 + m) + " bytes");
|
|
176
|
+
var I = pr.take(16);
|
|
177
|
+
var T1 = pr.take(m);
|
|
178
|
+
|
|
179
|
+
// -- signature (RFC 8554 Algorithm 6a). q first, then the LM-OTS signature,
|
|
180
|
+
// then the LMS typecode (AFTER the LM-OTS sig), then the auth path. --
|
|
181
|
+
if (sigBytes.length < 8) throw _err("shbs/bad-signature", "LMS signature is shorter than 8 bytes");
|
|
182
|
+
var sr = new Reader(sigBytes, "shbs/bad-signature", "LMS signature");
|
|
183
|
+
var q = sr.u32();
|
|
184
|
+
var otsSigType = sr.u32();
|
|
185
|
+
// The public key -- never the attacker-controlled signature -- is the authority
|
|
186
|
+
// (downgrade defense): a signature whose OTS typecode does not equal the one the
|
|
187
|
+
// public key commits to cannot verify against it. RFC 8554 Algorithm 6a checks
|
|
188
|
+
// the typecode (step 2c, and 2g for the LMS type below) BEFORE the length (steps
|
|
189
|
+
// 2d / 2i), and returns INVALID -- a verification FAILURE (false), not a
|
|
190
|
+
// structural error. So a mismatch is `false` at this point EVEN for a blob too
|
|
191
|
+
// short to be a complete signature: the mismatch is decidable once q + typecode
|
|
192
|
+
// are read (the bounded reader already threw if those 8 bytes are absent). A
|
|
193
|
+
// matching typecode with a truncated body still throws below (bounds-before-
|
|
194
|
+
// slice). Re-sizing the body by the signature's own mismatched typecode -- to
|
|
195
|
+
// "fully validate before returning false" -- would violate this order and reject
|
|
196
|
+
// a legitimate typecode-mutation test vector as malformed instead of INVALID.
|
|
197
|
+
if (otsSigType !== otsSet.code) return false;
|
|
198
|
+
var n = otsSet.n, p = otsSet.p;
|
|
199
|
+
var C = sr.take(n);
|
|
200
|
+
var y = [];
|
|
201
|
+
for (var yi = 0; yi < p; yi++) y.push(sr.take(n));
|
|
202
|
+
var sigLmsType = sr.u32();
|
|
203
|
+
if (sigLmsType !== lmsSet.code) return false; // RFC 8554 Algorithm 6a step 2g -> INVALID
|
|
204
|
+
var path = [];
|
|
205
|
+
for (var pi = 0; pi < h; pi++) path.push(sr.take(m));
|
|
206
|
+
if (!sr.atEnd()) throw _err("shbs/bad-signature", "LMS signature has " + sr.remaining() + " trailing byte(s)");
|
|
207
|
+
// RFC 8554 Algorithm 6a step 2i: a leaf index q >= 2^h is INVALID -- a
|
|
208
|
+
// verification FAILURE (false), NOT a structural error. Checked here, AFTER the
|
|
209
|
+
// exact-length validation above (a truncated / trailing blob already threw
|
|
210
|
+
// typed), against the REGISTRY height, never the blob. 2^h fits Number (h<=25).
|
|
211
|
+
if (q >= Math.pow(2, h)) return false;
|
|
212
|
+
|
|
213
|
+
// -- recompute the LM-OTS public-key candidate, then fold the Merkle path --
|
|
214
|
+
var Kc = _lmotsKc(otsSet, I, q, C, y, message);
|
|
215
|
+
var nodeNum = Math.pow(2, h) + q;
|
|
216
|
+
var tmp = _hash(lmsSet.hashFamily, m, [I, u32(nodeNum), u16(D_LEAF), Kc]);
|
|
217
|
+
for (var i2 = 0; nodeNum > 1; i2++) {
|
|
218
|
+
var parent = Math.floor(nodeNum / 2);
|
|
219
|
+
if (nodeNum % 2 === 1) {
|
|
220
|
+
// odd node_num: the current node is a RIGHT child, sibling on the LEFT.
|
|
221
|
+
tmp = _hash(lmsSet.hashFamily, m, [I, u32(parent), u16(D_INTR), path[i2], tmp]);
|
|
222
|
+
} else {
|
|
223
|
+
tmp = _hash(lmsSet.hashFamily, m, [I, u32(parent), u16(D_INTR), tmp, path[i2]]);
|
|
224
|
+
}
|
|
225
|
+
nodeNum = parent;
|
|
226
|
+
}
|
|
227
|
+
// The root comparison is over PUBLIC values (T1 is in the public key, tmp is
|
|
228
|
+
// derived from public inputs -- no secret, hence the module's "no side-channel
|
|
229
|
+
// surface" note), so constant time is not strictly required. It routes through
|
|
230
|
+
// the shared crypto guard anyway, for one length-checked comparison primitive
|
|
231
|
+
// across the hash-tree verifiers (pki.merkle folds its root the same way).
|
|
232
|
+
return guard.crypto.constantTimeEqual(tmp, T1);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Consume exactly one LMS public key from a reader (an HSS inner key), returning
|
|
236
|
+
// its raw bytes; sized off the LMS typecode it leads with.
|
|
237
|
+
function _consumeLmsPublicKey(r) {
|
|
238
|
+
var start = r.pos;
|
|
239
|
+
var code = r.u32(); // lmstype
|
|
240
|
+
r.u32(); // otstype (validated when the key is verified)
|
|
241
|
+
var m = _lmsSet(code).m;
|
|
242
|
+
r.take(16 + m); // I || T[1]
|
|
243
|
+
return r.buf.subarray(start, r.pos);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// The exact byte length (12 + n*(p+1) + m*h) of an LMS signature verified under
|
|
247
|
+
// `keyBytes` -- the AUTHORITATIVE public key for its level. The concatenated HSS
|
|
248
|
+
// blob is split by the public key's typecodes, never the attacker-controlled
|
|
249
|
+
// signature's own: so a typecode-mutated signature is sliced to the length the
|
|
250
|
+
// key expects and then _lmsVerify returns false (a verification failure, RFC 8554
|
|
251
|
+
// Algorithm 6a 2c/2g), consistent with verifyLms, instead of throwing on a
|
|
252
|
+
// mis-sized slice. Bounds-guarded (no direct read that could escape as a raw
|
|
253
|
+
// RangeError on a truncated inner key).
|
|
254
|
+
function _lmsSigLen(keyBytes) {
|
|
255
|
+
if (keyBytes.length < 8) throw _err("shbs/bad-public-key", "LMS public key is shorter than 8 bytes");
|
|
256
|
+
var lmsSet = _lmsSet(keyBytes.readUInt32BE(0));
|
|
257
|
+
var otsSet = _lmotsSet(keyBytes.readUInt32BE(4));
|
|
258
|
+
return 12 + otsSet.n * (otsSet.p + 1) + lmsSet.m * lmsSet.h;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// ---- HSS verification (RFC 8554 sec. 6.3) ------------------------------------
|
|
262
|
+
|
|
263
|
+
function _hssVerify(pubBytes, message, sigBytes) {
|
|
264
|
+
if (pubBytes.length < 4) throw _err("shbs/bad-public-key", "HSS public key is shorter than 4 bytes");
|
|
265
|
+
var pr = new Reader(pubBytes, "shbs/bad-public-key", "HSS public key");
|
|
266
|
+
var L = pr.u32();
|
|
267
|
+
if (L < 1 || L > constants.LIMITS.HSS_MAX_LEVELS) throw _err("shbs/bad-public-key", "HSS level count L=" + L + " is outside 1.." + constants.LIMITS.HSS_MAX_LEVELS);
|
|
268
|
+
var topKey = pr.buf.subarray(pr.pos); // the top-level LMS public key -- its
|
|
269
|
+
// structure (including an under-length blob) is validated by _lmsVerify below
|
|
270
|
+
// (the < 8 and exact-24+m checks), so no direct read here that would bypass the
|
|
271
|
+
// bounded reader and escape as a raw RangeError on a truncated key.
|
|
272
|
+
|
|
273
|
+
var sr = new Reader(sigBytes, "shbs/bad-signature", "HSS signature");
|
|
274
|
+
var Nspk = sr.u32();
|
|
275
|
+
// The level-count gate, checked BEFORE parsing any component (RFC 8554 sec. 6.3).
|
|
276
|
+
if (Nspk + 1 !== L) throw _err("shbs/bad-signature", "HSS Nspk+1 (" + (Nspk + 1) + ") does not equal the public-key level count L (" + L + ")");
|
|
277
|
+
|
|
278
|
+
var key = topKey;
|
|
279
|
+
for (var i = 0; i < Nspk; i++) {
|
|
280
|
+
// Slice this level's LMS signature by the AUTHORITATIVE key's length (so a
|
|
281
|
+
// typecode mismatch is a false verdict via _lmsVerify, not a mis-sized throw).
|
|
282
|
+
var sig = sr.take(_lmsSigLen(key));
|
|
283
|
+
var nextKey = _consumeLmsPublicKey(sr);
|
|
284
|
+
// Each level signs the SERIALIZED next-level LMS public key; that recovered
|
|
285
|
+
// key becomes the verification key for the level below (chain of trust).
|
|
286
|
+
if (!_lmsVerify(key, nextKey, sig)) return false;
|
|
287
|
+
key = nextKey;
|
|
288
|
+
}
|
|
289
|
+
// The final signature is the remainder; _lmsVerify parses it, applies the
|
|
290
|
+
// public-key-authoritative typecode check (mismatch -> false), and rejects a
|
|
291
|
+
// trailing / truncated remainder (bounds-before-slice + its own atEnd check).
|
|
292
|
+
return _lmsVerify(key, message, sr.buf.subarray(sr.pos));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// ---- public surface ----------------------------------------------------------
|
|
296
|
+
|
|
297
|
+
function _asBytes(x, label) { return guard.bytes.source(x, ShbsError, "shbs/bad-input", label); }
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* @primitive pki.shbs.verify
|
|
301
|
+
* @signature pki.shbs.verify(publicKey, message, signature) -> boolean
|
|
302
|
+
* @since 0.2.1
|
|
303
|
+
* @status experimental
|
|
304
|
+
* @spec RFC 8554 sec. 6, RFC 9802, RFC 9708
|
|
305
|
+
* @related pki.shbs.verifyLms
|
|
306
|
+
*
|
|
307
|
+
* Verify an HSS (Hierarchical Signature System) signature over `message` under
|
|
308
|
+
* `publicKey` -- the wire form RFC 9802 (X.509) and RFC 9708 (CMS) carry for
|
|
309
|
+
* `id-alg-hss-lms-hashsig`. The public key and signature are the raw HSS octet
|
|
310
|
+
* blobs the certificate / CMS parsers already surface (no ASN.1 wrapping). Every
|
|
311
|
+
* level of the hierarchy must verify: a single failing level yields false.
|
|
312
|
+
* Returns true for a valid signature, false for a well-formed signature that does
|
|
313
|
+
* not verify; a malformed blob (bad length, unknown or unapproved typecode,
|
|
314
|
+
* truncation, a typecode disagreeing between the key and the signature) throws a
|
|
315
|
+
* typed `ShbsError`. VERIFY ONLY -- this module never signs.
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* var cert = pki.schema.x509.parse(der);
|
|
319
|
+
* var ok = pki.shbs.verify(cert.subjectPublicKeyInfo.publicKey.bytes,
|
|
320
|
+
* cert.tbsBytes, cert.signatureValue.bytes);
|
|
321
|
+
*/
|
|
322
|
+
function verify(publicKey, message, signature) {
|
|
323
|
+
return _hssVerify(_asBytes(publicKey, "public key"), _asBytes(message, "message"), _asBytes(signature, "signature"));
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* @primitive pki.shbs.verifyLms
|
|
328
|
+
* @signature pki.shbs.verifyLms(publicKey, message, signature) -> boolean
|
|
329
|
+
* @since 0.2.1
|
|
330
|
+
* @status experimental
|
|
331
|
+
* @spec RFC 8554 sec. 5
|
|
332
|
+
* @related pki.shbs.verify
|
|
333
|
+
*
|
|
334
|
+
* Verify a single-tree LMS (Leighton-Micali Signature) over `message` -- the
|
|
335
|
+
* component an HSS hierarchy composes at each level, and a standalone algorithm
|
|
336
|
+
* in its own right. Same verdict contract as `pki.shbs.verify`: true / false for
|
|
337
|
+
* a well-formed signature, a typed `ShbsError` for a malformed blob.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* // lmsPublicKey / lmsSignature are raw LMS blobs (an HSS level, or a bare
|
|
341
|
+
* // LMS-signed artifact). Shown here on arbitrary bytes, which fail closed.
|
|
342
|
+
* var ok = pki.shbs.verifyLms(bytes, bytes, bytes);
|
|
343
|
+
*/
|
|
344
|
+
function verifyLms(publicKey, message, signature) {
|
|
345
|
+
return _lmsVerify(_asBytes(publicKey, "public key"), _asBytes(message, "message"), _asBytes(signature, "signature"));
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
module.exports = { verify: verify, verifyLms: verifyLms };
|
package/package.json
CHANGED
package/sbom.cdx.json
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
"$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json",
|
|
3
3
|
"bomFormat": "CycloneDX",
|
|
4
4
|
"specVersion": "1.5",
|
|
5
|
-
"serialNumber": "urn:uuid:
|
|
5
|
+
"serialNumber": "urn:uuid:1e68f63c-e550-4432-9b59-4cca3a0ddc64",
|
|
6
6
|
"version": 1,
|
|
7
7
|
"metadata": {
|
|
8
|
-
"timestamp": "2026-07-
|
|
8
|
+
"timestamp": "2026-07-12T01:48:49.026Z",
|
|
9
9
|
"lifecycles": [
|
|
10
10
|
{
|
|
11
11
|
"phase": "build"
|
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
}
|
|
20
20
|
],
|
|
21
21
|
"component": {
|
|
22
|
-
"bom-ref": "@blamejs/pki@0.2.
|
|
22
|
+
"bom-ref": "@blamejs/pki@0.2.1",
|
|
23
23
|
"type": "application",
|
|
24
24
|
"name": "pki",
|
|
25
|
-
"version": "0.2.
|
|
25
|
+
"version": "0.2.1",
|
|
26
26
|
"scope": "required",
|
|
27
27
|
"author": "blamejs contributors",
|
|
28
28
|
"description": "Pure-JavaScript PKI toolkit that owns its stack — X.509, ASN.1/DER, CMS, PQC-first.",
|
|
29
|
-
"purl": "pkg:npm/%40blamejs/pki@0.2.
|
|
29
|
+
"purl": "pkg:npm/%40blamejs/pki@0.2.1",
|
|
30
30
|
"properties": [],
|
|
31
31
|
"externalReferences": [
|
|
32
32
|
{
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"components": [],
|
|
55
55
|
"dependencies": [
|
|
56
56
|
{
|
|
57
|
-
"ref": "@blamejs/pki@0.2.
|
|
57
|
+
"ref": "@blamejs/pki@0.2.1",
|
|
58
58
|
"dependsOn": []
|
|
59
59
|
}
|
|
60
60
|
]
|