@certcrypt/verifier-toolkit 0.0.0-stage → 1.0.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.
Potentially problematic release.
This version of @certcrypt/verifier-toolkit might be problematic. Click here for more details.
- package/CHANGELOG.md +72 -0
- package/LICENSE +178 -0
- package/README.md +563 -2
- package/SECURITY.md +77 -0
- package/dist/cli/certcrypt-verify.d.ts +2 -0
- package/dist/cli/certcrypt-verify.js +174 -0
- package/dist/cli/certcrypt-verify.js.map +1 -0
- package/dist/core/config/certcrypt-foundation.d.ts +13 -0
- package/dist/core/config/certcrypt-foundation.js +123 -0
- package/dist/core/config/certcrypt-foundation.js.map +1 -0
- package/dist/core/config/default.d.ts +47 -0
- package/dist/core/config/default.js +93 -0
- package/dist/core/config/default.js.map +1 -0
- package/dist/core/config/test-keys.d.ts +40 -0
- package/dist/core/config/test-keys.js +41 -0
- package/dist/core/config/test-keys.js.map +1 -0
- package/dist/core/conformance/attestation.d.ts +4 -0
- package/dist/core/conformance/attestation.js +4 -0
- package/dist/core/conformance/attestation.js.map +1 -0
- package/dist/core/consistency/verify-consistency.d.ts +92 -0
- package/dist/core/consistency/verify-consistency.js +353 -0
- package/dist/core/consistency/verify-consistency.js.map +1 -0
- package/dist/core/crypto/canonical-json.d.ts +14 -0
- package/dist/core/crypto/canonical-json.js +13 -0
- package/dist/core/crypto/canonical-json.js.map +1 -0
- package/dist/core/crypto/canonical-subject.d.ts +76 -0
- package/dist/core/crypto/canonical-subject.js +248 -0
- package/dist/core/crypto/canonical-subject.js.map +1 -0
- package/dist/core/crypto/collection-merkle.d.ts +91 -0
- package/dist/core/crypto/collection-merkle.js +243 -0
- package/dist/core/crypto/collection-merkle.js.map +1 -0
- package/dist/core/crypto/composite.d.ts +73 -0
- package/dist/core/crypto/composite.js +206 -0
- package/dist/core/crypto/composite.js.map +1 -0
- package/dist/core/crypto/digest.d.ts +18 -0
- package/dist/core/crypto/digest.js +37 -0
- package/dist/core/crypto/digest.js.map +1 -0
- package/dist/core/crypto/ecdsa-p256.d.ts +12 -0
- package/dist/core/crypto/ecdsa-p256.js +70 -0
- package/dist/core/crypto/ecdsa-p256.js.map +1 -0
- package/dist/core/crypto/ed25519.d.ts +10 -0
- package/dist/core/crypto/ed25519.js +28 -0
- package/dist/core/crypto/ed25519.js.map +1 -0
- package/dist/core/crypto/holder-keyset.d.ts +23 -0
- package/dist/core/crypto/holder-keyset.js +64 -0
- package/dist/core/crypto/holder-keyset.js.map +1 -0
- package/dist/core/crypto/jcs.d.ts +9 -0
- package/dist/core/crypto/jcs.js +59 -0
- package/dist/core/crypto/jcs.js.map +1 -0
- package/dist/core/crypto/merkle.d.ts +34 -0
- package/dist/core/crypto/merkle.js +147 -0
- package/dist/core/crypto/merkle.js.map +1 -0
- package/dist/core/crypto/ml-dsa.d.ts +25 -0
- package/dist/core/crypto/ml-dsa.js +104 -0
- package/dist/core/crypto/ml-dsa.js.map +1 -0
- package/dist/core/crypto/provider/bytes.d.ts +31 -0
- package/dist/core/crypto/provider/bytes.js +106 -0
- package/dist/core/crypto/provider/bytes.js.map +1 -0
- package/dist/core/crypto/provider/index.d.ts +45 -0
- package/dist/core/crypto/provider/index.js +60 -0
- package/dist/core/crypto/provider/index.js.map +1 -0
- package/dist/core/crypto/provider/node.d.ts +2 -0
- package/dist/core/crypto/provider/node.js +96 -0
- package/dist/core/crypto/provider/node.js.map +1 -0
- package/dist/core/crypto/provider/register-node.d.ts +11 -0
- package/dist/core/crypto/provider/register-node.js +16 -0
- package/dist/core/crypto/provider/register-node.js.map +1 -0
- package/dist/core/crypto/provider/types.d.ts +77 -0
- package/dist/core/crypto/provider/types.js +16 -0
- package/dist/core/crypto/provider/types.js.map +1 -0
- package/dist/core/crypto/provider/web.d.ts +2 -0
- package/dist/core/crypto/provider/web.js +122 -0
- package/dist/core/crypto/provider/web.js.map +1 -0
- package/dist/core/crypto/pseudonym.d.ts +25 -0
- package/dist/core/crypto/pseudonym.js +37 -0
- package/dist/core/crypto/pseudonym.js.map +1 -0
- package/dist/core/crypto/subject-json-preconditions.d.ts +47 -0
- package/dist/core/crypto/subject-json-preconditions.js +373 -0
- package/dist/core/crypto/subject-json-preconditions.js.map +1 -0
- package/dist/core/domain/verify-domain.d.ts +13 -0
- package/dist/core/domain/verify-domain.js +39 -0
- package/dist/core/domain/verify-domain.js.map +1 -0
- package/dist/core/foundation/anti-equivocation.d.ts +87 -0
- package/dist/core/foundation/anti-equivocation.js +0 -0
- package/dist/core/foundation/anti-equivocation.js.map +1 -0
- package/dist/core/foundation/verify-foundation.d.ts +36 -0
- package/dist/core/foundation/verify-foundation.js +559 -0
- package/dist/core/foundation/verify-foundation.js.map +1 -0
- package/dist/core/independent/anti-replay.d.ts +15 -0
- package/dist/core/independent/anti-replay.js +27 -0
- package/dist/core/independent/anti-replay.js.map +1 -0
- package/dist/core/independent/anti-splicing.d.ts +16 -0
- package/dist/core/independent/anti-splicing.js +54 -0
- package/dist/core/independent/anti-splicing.js.map +1 -0
- package/dist/core/independent/capacity-binding.d.ts +26 -0
- package/dist/core/independent/capacity-binding.js +57 -0
- package/dist/core/independent/capacity-binding.js.map +1 -0
- package/dist/core/independent/capacity-evidence.d.ts +38 -0
- package/dist/core/independent/capacity-evidence.js +286 -0
- package/dist/core/independent/capacity-evidence.js.map +1 -0
- package/dist/core/independent/effective-units.d.ts +42 -0
- package/dist/core/independent/effective-units.js +147 -0
- package/dist/core/independent/effective-units.js.map +1 -0
- package/dist/core/independent/issuance-participation.d.ts +13 -0
- package/dist/core/independent/issuance-participation.js +24 -0
- package/dist/core/independent/issuance-participation.js.map +1 -0
- package/dist/core/independent/leaf-binding.d.ts +5 -0
- package/dist/core/independent/leaf-binding.js +135 -0
- package/dist/core/independent/leaf-binding.js.map +1 -0
- package/dist/core/independent/operator-recognition-registry.d.ts +33 -0
- package/dist/core/independent/operator-recognition-registry.js +116 -0
- package/dist/core/independent/operator-recognition-registry.js.map +1 -0
- package/dist/core/independent/proof-structure.d.ts +8 -0
- package/dist/core/independent/proof-structure.js +167 -0
- package/dist/core/independent/proof-structure.js.map +1 -0
- package/dist/core/independent/proof-views.d.ts +26 -0
- package/dist/core/independent/proof-views.js +58 -0
- package/dist/core/independent/proof-views.js.map +1 -0
- package/dist/core/independent/settlement.d.ts +18 -0
- package/dist/core/independent/settlement.js +548 -0
- package/dist/core/independent/settlement.js.map +1 -0
- package/dist/core/independent/verify-independent.d.ts +30 -0
- package/dist/core/independent/verify-independent.js +123 -0
- package/dist/core/independent/verify-independent.js.map +1 -0
- package/dist/core/independent/verify-operator-recognition.d.ts +26 -0
- package/dist/core/independent/verify-operator-recognition.js +60 -0
- package/dist/core/independent/verify-operator-recognition.js.map +1 -0
- package/dist/core/independent/verify-proof-signature.d.ts +20 -0
- package/dist/core/independent/verify-proof-signature.js +97 -0
- package/dist/core/independent/verify-proof-signature.js.map +1 -0
- package/dist/core/input/fs-bridge.d.ts +30 -0
- package/dist/core/input/fs-bridge.js +35 -0
- package/dist/core/input/fs-bridge.js.map +1 -0
- package/dist/core/input/load-artifacts.d.ts +2 -0
- package/dist/core/input/load-artifacts.js +80 -0
- package/dist/core/input/load-artifacts.js.map +1 -0
- package/dist/core/input/load-original-material.d.ts +11 -0
- package/dist/core/input/load-original-material.js +126 -0
- package/dist/core/input/load-original-material.js.map +1 -0
- package/dist/core/input/register-node-fs.d.ts +1 -0
- package/dist/core/input/register-node-fs.js +24 -0
- package/dist/core/input/register-node-fs.js.map +1 -0
- package/dist/core/input/resolve-target.d.ts +16 -0
- package/dist/core/input/resolve-target.js +43 -0
- package/dist/core/input/resolve-target.js.map +1 -0
- package/dist/core/input/strict-json.d.ts +25 -0
- package/dist/core/input/strict-json.js +167 -0
- package/dist/core/input/strict-json.js.map +1 -0
- package/dist/core/input/validate.d.ts +7 -0
- package/dist/core/input/validate.js +83 -0
- package/dist/core/input/validate.js.map +1 -0
- package/dist/core/issued/verify-collection-element.d.ts +66 -0
- package/dist/core/issued/verify-collection-element.js +137 -0
- package/dist/core/issued/verify-collection-element.js.map +1 -0
- package/dist/core/issued/verify-issued.d.ts +15 -0
- package/dist/core/issued/verify-issued.js +361 -0
- package/dist/core/issued/verify-issued.js.map +1 -0
- package/dist/core/issued/verify-original-material.d.ts +35 -0
- package/dist/core/issued/verify-original-material.js +418 -0
- package/dist/core/issued/verify-original-material.js.map +1 -0
- package/dist/core/package/verify-package.d.ts +34 -0
- package/dist/core/package/verify-package.js +109 -0
- package/dist/core/package/verify-package.js.map +1 -0
- package/dist/core/public-api.d.ts +178 -0
- package/dist/core/public-api.js +182 -0
- package/dist/core/public-api.js.map +1 -0
- package/dist/core/result/builder.d.ts +92 -0
- package/dist/core/result/builder.js +408 -0
- package/dist/core/result/builder.js.map +1 -0
- package/dist/core/rules/verify-corpus.d.ts +88 -0
- package/dist/core/rules/verify-corpus.js +165 -0
- package/dist/core/rules/verify-corpus.js.map +1 -0
- package/dist/core/rules/verify-rules.d.ts +14 -0
- package/dist/core/rules/verify-rules.js +62 -0
- package/dist/core/rules/verify-rules.js.map +1 -0
- package/dist/core/testing/validate-against-schema.d.ts +31 -0
- package/dist/core/testing/validate-against-schema.js +93 -0
- package/dist/core/testing/validate-against-schema.js.map +1 -0
- package/dist/core/types/artifacts.d.ts +224 -0
- package/dist/core/types/artifacts.js +2 -0
- package/dist/core/types/artifacts.js.map +1 -0
- package/dist/core/types/capacity-evidence.d.ts +85 -0
- package/dist/core/types/capacity-evidence.js +51 -0
- package/dist/core/types/capacity-evidence.js.map +1 -0
- package/dist/core/types/check.d.ts +61 -0
- package/dist/core/types/check.js +2 -0
- package/dist/core/types/check.js.map +1 -0
- package/dist/core/types/config.d.ts +97 -0
- package/dist/core/types/config.js +2 -0
- package/dist/core/types/config.js.map +1 -0
- package/dist/core/types/foundation.d.ts +121 -0
- package/dist/core/types/foundation.js +25 -0
- package/dist/core/types/foundation.js.map +1 -0
- package/dist/core/types/index.d.ts +9 -0
- package/dist/core/types/index.js +2 -0
- package/dist/core/types/index.js.map +1 -0
- package/dist/core/types/operator-recognition.d.ts +71 -0
- package/dist/core/types/operator-recognition.js +24 -0
- package/dist/core/types/operator-recognition.js.map +1 -0
- package/dist/core/types/proof.d.ts +94 -0
- package/dist/core/types/proof.js +2 -0
- package/dist/core/types/proof.js.map +1 -0
- package/dist/core/types/request.d.ts +59 -0
- package/dist/core/types/request.js +2 -0
- package/dist/core/types/request.js.map +1 -0
- package/dist/core/types/result.d.ts +164 -0
- package/dist/core/types/result.js +2 -0
- package/dist/core/types/result.js.map +1 -0
- package/dist/core/types/settlement-material.d.ts +130 -0
- package/dist/core/types/settlement-material.js +2 -0
- package/dist/core/types/settlement-material.js.map +1 -0
- package/dist/core/verify-node.d.ts +14 -0
- package/dist/core/verify-node.js +17 -0
- package/dist/core/verify-node.js.map +1 -0
- package/dist/core/verify-web.d.ts +1 -0
- package/dist/core/verify-web.js +25 -0
- package/dist/core/verify-web.js.map +1 -0
- package/dist/core/verify.d.ts +18 -0
- package/dist/core/verify.js +183 -0
- package/dist/core/verify.js.map +1 -0
- package/package.json +71 -4
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export declare const COMPOSITE_DOMAIN_PREFIX = "CERTCRYPT-Composite-Signature-v1";
|
|
2
|
+
export declare const PROFILE_P256_MLDSA65 = "certcrypt-composite-p256-mldsa65-v1";
|
|
3
|
+
export declare const PROFILE_ED25519_MLDSA65 = "certcrypt-composite-ed25519-mldsa65-v1";
|
|
4
|
+
export declare const ALG_ECDSA_P256 = "ECDSA-P256-SHA3-256";
|
|
5
|
+
export declare const ALG_ED25519 = "Ed25519";
|
|
6
|
+
export declare const ALG_MLDSA65 = "ML-DSA-65";
|
|
7
|
+
/** One detached component signature (a `signatures[]` entry). */
|
|
8
|
+
export interface ComponentSignature {
|
|
9
|
+
alg: string;
|
|
10
|
+
encoding?: string;
|
|
11
|
+
low_s?: boolean;
|
|
12
|
+
sig_b64: string;
|
|
13
|
+
}
|
|
14
|
+
/** The composite signature wire object. */
|
|
15
|
+
export interface CompositeSignatureWire {
|
|
16
|
+
alg: string;
|
|
17
|
+
mode: string;
|
|
18
|
+
signatures: ComponentSignature[];
|
|
19
|
+
}
|
|
20
|
+
/** Public keys for composite verification (classical + the PQC ML-DSA key). */
|
|
21
|
+
export interface CompositePublicKeys {
|
|
22
|
+
/** Classical pubkey: P-256 SEC1 uncompressed (65 bytes), or Ed25519 raw (32 bytes), base64. */
|
|
23
|
+
classicalPubB64: string;
|
|
24
|
+
/** ML-DSA-65 pkEncode (1952 bytes), base64. */
|
|
25
|
+
mldsaPubB64: string;
|
|
26
|
+
}
|
|
27
|
+
/** FIPS 204 ML-DSA-65 public-key length. */
|
|
28
|
+
export declare const MLDSA65_PUBLIC_KEY_LEN = 1952;
|
|
29
|
+
/** SEC1 uncompressed P-256 point length (0x04 || X || Y). */
|
|
30
|
+
export declare const P256_SEC1_UNCOMPRESSED_LEN = 65;
|
|
31
|
+
/** RFC 8032 Ed25519 public-key length. */
|
|
32
|
+
export declare const ED25519_PUBLIC_KEY_LEN = 32;
|
|
33
|
+
/**
|
|
34
|
+
* Why `sig` is not a well-formed composite under `compositeAlgId` with `keys`,
|
|
35
|
+
* or null when its layout and key encodings are exactly the profile's.
|
|
36
|
+
* Exported so that a carrying artifact can report malformed material as such.
|
|
37
|
+
*/
|
|
38
|
+
export declare function compositeLayoutError(compositeAlgId: string, sig: unknown, keys: CompositePublicKeys): string | null;
|
|
39
|
+
/**
|
|
40
|
+
* Build the domain-separated message M' every component signs.
|
|
41
|
+
*
|
|
42
|
+
* @param compositeAlgId the PROFILE id (bound in → anti cross-profile).
|
|
43
|
+
* @param payloadCanonDigestHex lowercase-hex SHA3-256(JCS(payload)).
|
|
44
|
+
*/
|
|
45
|
+
export declare function compositeMPrime(compositeAlgId: string, payloadCanonDigestHex: string): Uint8Array;
|
|
46
|
+
export interface CompositeVerifyResult {
|
|
47
|
+
/** True iff ALL components verified (AND) and the PQC floor was present. */
|
|
48
|
+
valid: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Set when the signature or a key is malformed for the profile (layout,
|
|
51
|
+
* member set, fixed values, key encoding): a structural defect, distinct from
|
|
52
|
+
* a well-formed signature that does not verify.
|
|
53
|
+
*/
|
|
54
|
+
malformed: string | null;
|
|
55
|
+
classical: {
|
|
56
|
+
alg: string | null;
|
|
57
|
+
passed: boolean;
|
|
58
|
+
reason: string | null;
|
|
59
|
+
};
|
|
60
|
+
mldsa: {
|
|
61
|
+
performed: boolean;
|
|
62
|
+
passed: boolean | null;
|
|
63
|
+
reason: string | null;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Verify a composite signature over `payloadCanonDigestHex` under
|
|
68
|
+
* `compositeAlgId` — the profile the CONTEXT requires, never the one the
|
|
69
|
+
* signature declares. The layout and key encodings must be exactly the
|
|
70
|
+
* profile's; then AND with fail-closed: ALL components must verify AND the
|
|
71
|
+
* ML-DSA component MUST pass.
|
|
72
|
+
*/
|
|
73
|
+
export declare function verifyComposite(compositeAlgId: string, payloadCanonDigestHex: string, sig: CompositeSignatureWire, keys: CompositePublicKeys): CompositeVerifyResult;
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Composite signature verification (CERTCRYPT Composite Signature Profile).
|
|
3
|
+
*
|
|
4
|
+
* The permanent trust chain (emission certificate, system-foundation root,
|
|
5
|
+
* Proof of Independence, Level-1 binding) signs as a COMPOSITE: every component
|
|
6
|
+
* signs the SAME domain-separated message M', and the composite verifies iff ALL
|
|
7
|
+
* components verify (AND). Binding the composite profile id into M' stops a
|
|
8
|
+
* component being lifted out and replayed under another profile (anti-stripping,
|
|
9
|
+
* anti cross-profile).
|
|
10
|
+
*
|
|
11
|
+
* M' = ASCII(domain_prefix) || 0x00 || ASCII(composite_alg_id) || 0x00
|
|
12
|
+
* || ASCII(payload_canon_digest_hex)
|
|
13
|
+
*
|
|
14
|
+
* domain_prefix = "CERTCRYPT-Composite-Signature-v1" (no year). The 0x00
|
|
15
|
+
* separators make the field split injective.
|
|
16
|
+
*
|
|
17
|
+
* Canonical profile `certcrypt-composite-p256-mldsa65-v1`: ECDSA-P256-SHA3-256
|
|
18
|
+
* (classical, first) + ML-DSA-65 (PQC). Additional profile
|
|
19
|
+
* `certcrypt-composite-ed25519-mldsa65-v1` swaps the classical for Ed25519.
|
|
20
|
+
*
|
|
21
|
+
* EXACT LAYOUT: the profile fixes the wire object completely — `{alg, mode,
|
|
22
|
+
* signatures}`, exactly two components in the profile's order (classical first),
|
|
23
|
+
* each with exactly its members, the ECDSA component with `encoding:"raw-rs-64"`
|
|
24
|
+
* and `low_s:true` — and one encoding per public key: P-256 as a 65-byte SEC1
|
|
25
|
+
* uncompressed point, Ed25519 as its raw 32 bytes, ML-DSA-65 as its 1952-byte
|
|
26
|
+
* pkEncode. Anything else is malformed and never reaches the cryptography, so two
|
|
27
|
+
* byte forms of one signature or one key can never both be accepted.
|
|
28
|
+
*
|
|
29
|
+
* COMPOSITE-ONLY: `verifyComposite` is the only signature verdict. The per-component
|
|
30
|
+
* verifiers (`verifyEcdsaP256Signature`, `verifyEd25519Signature`,
|
|
31
|
+
* `verifyMlDsa65Signature`) are primitives, never verdicts. Mirrors the reference
|
|
32
|
+
* producer and the conformance generator; M' bytes are
|
|
33
|
+
* load-bearing cross-language. Fail-closed: the ML-DSA (PQC) component is the
|
|
34
|
+
* post-quantum floor and can never be skipped.
|
|
35
|
+
*/
|
|
36
|
+
import { verifyEcdsaP256Signature } from "./ecdsa-p256.js";
|
|
37
|
+
import { verifyEd25519Signature } from "./ed25519.js";
|
|
38
|
+
import { verifyMlDsa65Signature } from "./ml-dsa.js";
|
|
39
|
+
import { strictBase64Decode } from "./provider/bytes.js";
|
|
40
|
+
export const COMPOSITE_DOMAIN_PREFIX = "CERTCRYPT-Composite-Signature-v1";
|
|
41
|
+
export const PROFILE_P256_MLDSA65 = "certcrypt-composite-p256-mldsa65-v1";
|
|
42
|
+
export const PROFILE_ED25519_MLDSA65 = "certcrypt-composite-ed25519-mldsa65-v1";
|
|
43
|
+
export const ALG_ECDSA_P256 = "ECDSA-P256-SHA3-256";
|
|
44
|
+
export const ALG_ED25519 = "Ed25519";
|
|
45
|
+
export const ALG_MLDSA65 = "ML-DSA-65";
|
|
46
|
+
/** FIPS 204 ML-DSA-65 public-key length. */
|
|
47
|
+
export const MLDSA65_PUBLIC_KEY_LEN = 1952;
|
|
48
|
+
/** SEC1 uncompressed P-256 point length (0x04 || X || Y). */
|
|
49
|
+
export const P256_SEC1_UNCOMPRESSED_LEN = 65;
|
|
50
|
+
/** RFC 8032 Ed25519 public-key length. */
|
|
51
|
+
export const ED25519_PUBLIC_KEY_LEN = 32;
|
|
52
|
+
const ECDSA_SLOT = {
|
|
53
|
+
alg: ALG_ECDSA_P256,
|
|
54
|
+
members: ["alg", "encoding", "low_s", "sig_b64"],
|
|
55
|
+
fixed: { encoding: "raw-rs-64", low_s: true },
|
|
56
|
+
};
|
|
57
|
+
const ED25519_SLOT = { alg: ALG_ED25519, members: ["alg", "sig_b64"] };
|
|
58
|
+
const MLDSA_SLOT = { alg: ALG_MLDSA65, members: ["alg", "sig_b64"] };
|
|
59
|
+
/** Each registered profile's exact component layout, in wire order. */
|
|
60
|
+
const PROFILE_LAYOUT = {
|
|
61
|
+
[PROFILE_P256_MLDSA65]: [ECDSA_SLOT, MLDSA_SLOT],
|
|
62
|
+
[PROFILE_ED25519_MLDSA65]: [ED25519_SLOT, MLDSA_SLOT],
|
|
63
|
+
};
|
|
64
|
+
const WIRE_MEMBERS = ["alg", "mode", "signatures"];
|
|
65
|
+
function isPlainObject(v) {
|
|
66
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
67
|
+
}
|
|
68
|
+
function hasExactly(obj, members) {
|
|
69
|
+
const keys = Object.keys(obj);
|
|
70
|
+
return keys.length === members.length && members.every((m) => Object.hasOwn(obj, m));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Why `sig` is not a well-formed composite under `compositeAlgId` with `keys`,
|
|
74
|
+
* or null when its layout and key encodings are exactly the profile's.
|
|
75
|
+
* Exported so that a carrying artifact can report malformed material as such.
|
|
76
|
+
*/
|
|
77
|
+
export function compositeLayoutError(compositeAlgId, sig, keys) {
|
|
78
|
+
const layout = PROFILE_LAYOUT[compositeAlgId];
|
|
79
|
+
if (!layout)
|
|
80
|
+
return `unsupported composite profile: ${compositeAlgId}`;
|
|
81
|
+
if (!isPlainObject(sig) || !hasExactly(sig, WIRE_MEMBERS)) {
|
|
82
|
+
return `signature must be an object with exactly ${WIRE_MEMBERS.join(", ")}`;
|
|
83
|
+
}
|
|
84
|
+
if (sig.alg !== compositeAlgId) {
|
|
85
|
+
return `profile mismatch: ${String(sig.alg)} != ${compositeAlgId}`;
|
|
86
|
+
}
|
|
87
|
+
if (sig.mode !== "AND")
|
|
88
|
+
return `unsupported composite mode: ${String(sig.mode)}`;
|
|
89
|
+
const comps = sig.signatures;
|
|
90
|
+
if (!Array.isArray(comps) || comps.length !== layout.length) {
|
|
91
|
+
return `signatures must hold exactly the ${layout.length} components of the profile`;
|
|
92
|
+
}
|
|
93
|
+
for (let i = 0; i < layout.length; i++) {
|
|
94
|
+
const slot = layout[i];
|
|
95
|
+
const c = comps[i];
|
|
96
|
+
if (!isPlainObject(c) || c.alg !== slot.alg) {
|
|
97
|
+
return `component ${i} must be ${slot.alg} (the profile fixes the order)`;
|
|
98
|
+
}
|
|
99
|
+
if (!hasExactly(c, slot.members)) {
|
|
100
|
+
return `component ${i} (${slot.alg}) must carry exactly ${slot.members.join(", ")}`;
|
|
101
|
+
}
|
|
102
|
+
for (const [member, value] of Object.entries(slot.fixed ?? {})) {
|
|
103
|
+
if (c[member] !== value) {
|
|
104
|
+
return `component ${i} (${slot.alg}) ${member} must be ${JSON.stringify(value)}`;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (typeof c.sig_b64 !== "string")
|
|
108
|
+
return `component ${i} sig_b64 must be a string`;
|
|
109
|
+
}
|
|
110
|
+
const keyLen = (b64, field) => {
|
|
111
|
+
try {
|
|
112
|
+
return strictBase64Decode(b64, field).length;
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
return err instanceof Error ? err.message : String(err);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const classical = layout[0].alg;
|
|
119
|
+
const cBytes = (() => {
|
|
120
|
+
try {
|
|
121
|
+
return strictBase64Decode(keys.classicalPubB64, "classical_public_key");
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
return err instanceof Error ? err.message : String(err);
|
|
125
|
+
}
|
|
126
|
+
})();
|
|
127
|
+
if (typeof cBytes === "string")
|
|
128
|
+
return cBytes;
|
|
129
|
+
if (classical === ALG_ECDSA_P256) {
|
|
130
|
+
if (cBytes.length !== P256_SEC1_UNCOMPRESSED_LEN || cBytes[0] !== 0x04) {
|
|
131
|
+
return "classical public key must be a SEC1 uncompressed P-256 point (65 bytes, 0x04)";
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
else if (cBytes.length !== ED25519_PUBLIC_KEY_LEN) {
|
|
135
|
+
return "classical public key must be a raw Ed25519 key (32 bytes)";
|
|
136
|
+
}
|
|
137
|
+
const mLen = keyLen(keys.mldsaPubB64, "pqc_public_key");
|
|
138
|
+
if (typeof mLen === "string")
|
|
139
|
+
return mLen;
|
|
140
|
+
if (mLen !== MLDSA65_PUBLIC_KEY_LEN) {
|
|
141
|
+
return `ML-DSA-65 public key must be ${MLDSA65_PUBLIC_KEY_LEN} bytes, got ${mLen}`;
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Build the domain-separated message M' every component signs.
|
|
147
|
+
*
|
|
148
|
+
* @param compositeAlgId the PROFILE id (bound in → anti cross-profile).
|
|
149
|
+
* @param payloadCanonDigestHex lowercase-hex SHA3-256(JCS(payload)).
|
|
150
|
+
*/
|
|
151
|
+
export function compositeMPrime(compositeAlgId, payloadCanonDigestHex) {
|
|
152
|
+
const enc = new TextEncoder();
|
|
153
|
+
const prefix = enc.encode(COMPOSITE_DOMAIN_PREFIX);
|
|
154
|
+
const alg = enc.encode(compositeAlgId);
|
|
155
|
+
const digest = enc.encode(payloadCanonDigestHex);
|
|
156
|
+
const out = new Uint8Array(prefix.length + 1 + alg.length + 1 + digest.length);
|
|
157
|
+
let o = 0;
|
|
158
|
+
out.set(prefix, o);
|
|
159
|
+
o += prefix.length;
|
|
160
|
+
out[o++] = 0x00;
|
|
161
|
+
out.set(alg, o);
|
|
162
|
+
o += alg.length;
|
|
163
|
+
out[o++] = 0x00;
|
|
164
|
+
out.set(digest, o);
|
|
165
|
+
return out;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Verify a composite signature over `payloadCanonDigestHex` under
|
|
169
|
+
* `compositeAlgId` — the profile the CONTEXT requires, never the one the
|
|
170
|
+
* signature declares. The layout and key encodings must be exactly the
|
|
171
|
+
* profile's; then AND with fail-closed: ALL components must verify AND the
|
|
172
|
+
* ML-DSA component MUST pass.
|
|
173
|
+
*/
|
|
174
|
+
export function verifyComposite(compositeAlgId, payloadCanonDigestHex, sig, keys) {
|
|
175
|
+
const layoutError = compositeLayoutError(compositeAlgId, sig, keys);
|
|
176
|
+
if (layoutError) {
|
|
177
|
+
return {
|
|
178
|
+
valid: false,
|
|
179
|
+
malformed: layoutError,
|
|
180
|
+
classical: { alg: null, passed: false, reason: `malformed: ${layoutError}` },
|
|
181
|
+
mldsa: { performed: false, passed: null, reason: `malformed: ${layoutError}` },
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const mPrime = compositeMPrime(compositeAlgId, payloadCanonDigestHex);
|
|
185
|
+
const [classicalComp, mldsaComp] = sig.signatures;
|
|
186
|
+
// Classical component.
|
|
187
|
+
let classical;
|
|
188
|
+
try {
|
|
189
|
+
const ok = classicalComp.alg === ALG_ECDSA_P256
|
|
190
|
+
? verifyEcdsaP256Signature(keys.classicalPubB64, mPrime, classicalComp.sig_b64)
|
|
191
|
+
: verifyEd25519Signature(keys.classicalPubB64, mPrime, classicalComp.sig_b64);
|
|
192
|
+
classical = { alg: classicalComp.alg, passed: ok, reason: ok ? null : "verification_failed" };
|
|
193
|
+
}
|
|
194
|
+
catch (err) {
|
|
195
|
+
classical = {
|
|
196
|
+
alg: classicalComp.alg,
|
|
197
|
+
passed: false,
|
|
198
|
+
reason: err instanceof Error ? err.message : String(err),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
// ML-DSA-65 component (the post-quantum floor).
|
|
202
|
+
const mldsa = verifyMlDsa65Signature(keys.mldsaPubB64, mPrime, mldsaComp.sig_b64);
|
|
203
|
+
const valid = classical.passed && mldsa.performed === true && mldsa.passed === true;
|
|
204
|
+
return { valid, malformed: null, classical, mldsa };
|
|
205
|
+
}
|
|
206
|
+
//# sourceMappingURL=composite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composite.js","sourceRoot":"","sources":["../../../core/crypto/composite.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAEzD,MAAM,CAAC,MAAM,uBAAuB,GAAG,kCAAkC,CAAC;AAE1E,MAAM,CAAC,MAAM,oBAAoB,GAAG,qCAAqC,CAAC;AAC1E,MAAM,CAAC,MAAM,uBAAuB,GAAG,wCAAwC,CAAC;AAEhF,MAAM,CAAC,MAAM,cAAc,GAAG,qBAAqB,CAAC;AACpD,MAAM,CAAC,MAAM,WAAW,GAAG,SAAS,CAAC;AACrC,MAAM,CAAC,MAAM,WAAW,GAAG,WAAW,CA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AAuB;IACvB,IAAI,SAAkE,CAAC;IACvE,IAAI,CAAC;QACH,MAAM,EAAE,GACN,aAAa,CAAC,GAAG,KAAK,cAAc;YAClC,CAAC,CAAC,wBAAwB,CAAC,IAAI,CAAC,eAAe,EAAE,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC;YAC/E,CAAC,CAAC,sBAAsB,CAAC,IAAI,CAAC,eAAe,EAAE,MAAM,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;QAClF,SAAS,GAAG,EAAE,GAAG,EAAE,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,qBAAqB,EAAE,CAAC;IAChG,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACtB,SAAS,GAAG;YACV,GAAG,EAAE,aAAa,CAAC,GAAG;YACtB,MAAM,EAAE,KAAK;YACb,MAAM,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;SACzD,CAAC;IACJ,CAAC;IAED,gDAAgD;IAChD,MAAM,KAAK,GAAG,sBAAsB,CAAC,IAAI,CAAC,WAAW,EAAE,MAAM,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC;IAElF,MAAM,KAAK,GAAG,SAAS,CAAC,MAAM,IAAI,KAAK,CAAC,SAAS,KAAK,IAAI,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,CAAC;IACpF,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare function digestHexBytes(data: Uint8Array): string;
|
|
2
|
+
export declare function digestHexText(text: string): string;
|
|
3
|
+
/**
|
|
4
|
+
* Incremental SHA3-256 over data the caller feeds in pieces.
|
|
5
|
+
*
|
|
6
|
+
* For material too large to hold in one buffer: read it in chunks, `update()` each, then
|
|
7
|
+
* `digestHex()`. The result equals `digestHexBytes()` over the concatenation — pinned in
|
|
8
|
+
* `provider.equivalence.test.ts` at chunk sizes straddling the 136-byte rate.
|
|
9
|
+
*
|
|
10
|
+
* Only meaningful where canonical_bytes ARE the input bytes, i.e. `subject.binary/1`. Every
|
|
11
|
+
* other subject profile transforms the material first, and none of those transformations is
|
|
12
|
+
* chunk-local.
|
|
13
|
+
*/
|
|
14
|
+
export declare function digestHexStream(): {
|
|
15
|
+
update(chunk: Uint8Array): void;
|
|
16
|
+
digestHex(): string;
|
|
17
|
+
};
|
|
18
|
+
export declare function digestBytes(data: Uint8Array): Uint8Array;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHA3-256 — the digest under every commitment in the spec.
|
|
3
|
+
*
|
|
4
|
+
* Delegates to the active crypto provider so the same code runs on Node
|
|
5
|
+
* (`node:crypto`) and in a browser (`@noble/hashes`). Note WebCrypto cannot
|
|
6
|
+
* serve this: it has no SHA-3 at any digest length.
|
|
7
|
+
*/
|
|
8
|
+
import { getCryptoProvider } from "./provider/index.js";
|
|
9
|
+
import { toHex, utf8Bytes } from "./provider/bytes.js";
|
|
10
|
+
export function digestHexBytes(data) {
|
|
11
|
+
return toHex(getCryptoProvider().sha3_256(data));
|
|
12
|
+
}
|
|
13
|
+
export function digestHexText(text) {
|
|
14
|
+
return toHex(getCryptoProvider().sha3_256(utf8Bytes(text)));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Incremental SHA3-256 over data the caller feeds in pieces.
|
|
18
|
+
*
|
|
19
|
+
* For material too large to hold in one buffer: read it in chunks, `update()` each, then
|
|
20
|
+
* `digestHex()`. The result equals `digestHexBytes()` over the concatenation — pinned in
|
|
21
|
+
* `provider.equivalence.test.ts` at chunk sizes straddling the 136-byte rate.
|
|
22
|
+
*
|
|
23
|
+
* Only meaningful where canonical_bytes ARE the input bytes, i.e. `subject.binary/1`. Every
|
|
24
|
+
* other subject profile transforms the material first, and none of those transformations is
|
|
25
|
+
* chunk-local.
|
|
26
|
+
*/
|
|
27
|
+
export function digestHexStream() {
|
|
28
|
+
const stream = getCryptoProvider().sha3_256_stream();
|
|
29
|
+
return {
|
|
30
|
+
update: (chunk) => stream.update(chunk),
|
|
31
|
+
digestHex: () => toHex(stream.digest())
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function digestBytes(data) {
|
|
35
|
+
return getCryptoProvider().sha3_256(data);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=digest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"digest.js","sourceRoot":"","sources":["../../../core/crypto/digest.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEvD,MAAM,UAAU,cAAc,CAAC,IAAgB;IAC7C,OAAO,KAAK,CAAC,iBAAiB,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;AACnD,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,OAAO,KAAK,CAAC,iBAAiB,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,eAAe;IAI7B,MAAM,MAAM,GAAG,iBAAiB,EAAE,CAAC,eAAe,EAAE,CAAC;IACrD,OAAO;QACL,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;QACvC,SAAS,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;KACxC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,IAAgB;IAC1C,OAAO,iBAAiB,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AAC5C,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verify an ECDSA P-256 signature over `payloadBytes` (raw message; SHA3-256 is
|
|
3
|
+
* applied internally, matching the signer).
|
|
4
|
+
*
|
|
5
|
+
* @param publicKeyB64 RFC 4648 base64 public key (SEC1 raw or SPKI DER).
|
|
6
|
+
* @param payloadBytes the exact message bytes that were signed (e.g. `M'`).
|
|
7
|
+
* @param signatureB64 RFC 4648 base64 signature in `raw-rs-64` form (64 bytes).
|
|
8
|
+
* @returns true iff the signature verifies AND is low-s. A high-s signature
|
|
9
|
+
* returns false (rejected, never normalized).
|
|
10
|
+
* @throws on malformed encoding, wrong signature length, or a crypto runtime error.
|
|
11
|
+
*/
|
|
12
|
+
export declare function verifyEcdsaP256Signature(publicKeyB64: string, payloadBytes: Uint8Array, signatureB64: string): boolean;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ECDSA P-256 (secp256r1) signature verification — the classical component of
|
|
3
|
+
* the CERTCRYPT Composite Signature Profile (`certcrypt-composite-p256-mldsa65-v1`).
|
|
4
|
+
*
|
|
5
|
+
* This module is a low-level cryptographic PRIMITIVE. It MUST NOT be used as a
|
|
6
|
+
* signature-verdict API on its own: a composite signature is valid only through
|
|
7
|
+
* `verifyComposite` (AND of all components), as `composite-signature/1` requires.
|
|
8
|
+
*
|
|
9
|
+
* Canonical wire form:
|
|
10
|
+
* - curve P-256 / secp256r1, hash **SHA3-256** over the signed message
|
|
11
|
+
* (CERTCRYPT's stack-wide hash — NOT SHA-2; ECDSA is hash-agnostic);
|
|
12
|
+
* - signature encoding `raw-rs-64`: r || s, 64 bytes (32+32), NOT DER;
|
|
13
|
+
* - low-s NORMATIVE: a high-s signature is REJECTED (not normalized).
|
|
14
|
+
*
|
|
15
|
+
* The digest is applied by the active crypto provider, which hashes the MESSAGE
|
|
16
|
+
* with SHA3-256 (matching the reference producer's digest reduced mod n) rather than
|
|
17
|
+
* accepting a pre-computed digest under a null algorithm, which does not
|
|
18
|
+
* interoperate.
|
|
19
|
+
*
|
|
20
|
+
* Public key: this primitive decodes a raw SEC1 point or SPKI DER. Which ONE
|
|
21
|
+
* encoding a key field admits is fixed by the artifact that carries it, and the
|
|
22
|
+
* composite verifier enforces it before calling here (`verifyComposite`: SEC1
|
|
23
|
+
* uncompressed, 65 bytes, for every P-256 field of the corpus).
|
|
24
|
+
*
|
|
25
|
+
* The low-s gate below is enforced HERE, not in a backend, so the normative
|
|
26
|
+
* anti-malleability rule is applied identically on every runtime.
|
|
27
|
+
*/
|
|
28
|
+
import { getCryptoProvider } from "./provider/index.js";
|
|
29
|
+
import { strictBase64Decode } from "./provider/bytes.js";
|
|
30
|
+
// secp256r1 group order n, and n/2 (the low-s ceiling, inclusive).
|
|
31
|
+
const P256_N = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n;
|
|
32
|
+
const P256_HALF_N = P256_N >> 1n;
|
|
33
|
+
/**
|
|
34
|
+
* Verify an ECDSA P-256 signature over `payloadBytes` (raw message; SHA3-256 is
|
|
35
|
+
* applied internally, matching the signer).
|
|
36
|
+
*
|
|
37
|
+
* @param publicKeyB64 RFC 4648 base64 public key (SEC1 raw or SPKI DER).
|
|
38
|
+
* @param payloadBytes the exact message bytes that were signed (e.g. `M'`).
|
|
39
|
+
* @param signatureB64 RFC 4648 base64 signature in `raw-rs-64` form (64 bytes).
|
|
40
|
+
* @returns true iff the signature verifies AND is low-s. A high-s signature
|
|
41
|
+
* returns false (rejected, never normalized).
|
|
42
|
+
* @throws on malformed encoding, wrong signature length, or a crypto runtime error.
|
|
43
|
+
*/
|
|
44
|
+
export function verifyEcdsaP256Signature(publicKeyB64, payloadBytes, signatureB64) {
|
|
45
|
+
const pubKeyBytes = strictBase64Decode(publicKeyB64, "public_key");
|
|
46
|
+
const sigBytes = strictBase64Decode(signatureB64, "signature");
|
|
47
|
+
if (sigBytes.length !== 64) {
|
|
48
|
+
throw new Error(`ECDSA-P256 signature must be raw-rs-64 (64 bytes r||s), got ${sigBytes.length} bytes`);
|
|
49
|
+
}
|
|
50
|
+
// Low-s gate FIRST: reject high-s outright (anti-malleability, normative).
|
|
51
|
+
// s is the second 32-byte half.
|
|
52
|
+
const s = beBytesToBigInt(sigBytes.subarray(32, 64));
|
|
53
|
+
if (s > P256_HALF_N || s === 0n) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
// r must also be in [1, n-1] for a well-formed signature.
|
|
57
|
+
const r = beBytesToBigInt(sigBytes.subarray(0, 32));
|
|
58
|
+
if (r === 0n || r >= P256_N) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
return getCryptoProvider().verifyEcdsaP256Sha3(pubKeyBytes, payloadBytes, sigBytes);
|
|
62
|
+
}
|
|
63
|
+
function beBytesToBigInt(bytes) {
|
|
64
|
+
let acc = 0n;
|
|
65
|
+
for (const b of bytes) {
|
|
66
|
+
acc = (acc << 8n) | BigInt(b);
|
|
67
|
+
}
|
|
68
|
+
return acc;
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=ecdsa-p256.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ecdsa-p256.js","sourceRoot":"","sources":["../../../core/crypto/ecdsa-p256.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAEzD,mEAAmE;AACnE,MAAM,MAAM,GACV,mEAAmE,CAAC;AACtE,MAAM,WAAW,GAAG,MAAM,IAAI,EAAE,CAAC;AAEjC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,wBAAwB,CACtC,YAAoB,EACpB,YAAwB,EACxB,YA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|
|
5
|
+
* @param payloadBytes - The exact bytes that were signed
|
|
6
|
+
* @param signatureB64 - RFC 4648 base64-encoded signature
|
|
7
|
+
* @returns true if valid, false if invalid
|
|
8
|
+
* @throws on malformed encoding or crypto runtime errors
|
|
9
|
+
*/
|
|
10
|
+
export declare function verifyEd25519Signature(publicKeyB64: string, payloadBytes: Uint8Array, signatureB64: string): boolean;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ed25519 signature verification.
|
|
3
|
+
*
|
|
4
|
+
* Supports both raw 32-byte public keys and SPKI DER-encoded keys.
|
|
5
|
+
* Strict RFC 4648 base64 decoding for all inputs.
|
|
6
|
+
*
|
|
7
|
+
* The primitive itself comes from the active crypto provider (node:crypto or
|
|
8
|
+
* @noble), so this path runs unchanged in a browser. Base64 strictness lives
|
|
9
|
+
* here rather than in a backend: it is a rule of the input format, identical on
|
|
10
|
+
* every runtime.
|
|
11
|
+
*/
|
|
12
|
+
import { getCryptoProvider } from "./provider/index.js";
|
|
13
|
+
import { strictBase64Decode } from "./provider/bytes.js";
|
|
14
|
+
/**
|
|
15
|
+
* Verify an Ed25519 signature.
|
|
16
|
+
*
|
|
17
|
+
* @param publicKeyB64 - RFC 4648 base64-encoded public key (raw 32 bytes or SPKI DER)
|
|
18
|
+
* @param payloadBytes - The exact bytes that were signed
|
|
19
|
+
* @param signatureB64 - RFC 4648 base64-encoded signature
|
|
20
|
+
* @returns true if valid, false if invalid
|
|
21
|
+
* @throws on malformed encoding or crypto runtime errors
|
|
22
|
+
*/
|
|
23
|
+
export function verifyEd25519Signature(publicKeyB64, payloadBytes, signatureB64) {
|
|
24
|
+
const pubKeyBytes = strictBase64Decode(publicKeyB64, "public_key");
|
|
25
|
+
const sigBytes = strictBase64Decode(signatureB64, "signature");
|
|
26
|
+
return getCryptoProvider().verifyEd25519(pubKeyBytes, payloadBytes, sigBytes);
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=ed25519.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ed25519.js","sourceRoot":"","sources":["../../../core/crypto/ed25519.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;;;;;;GAQG;AACH,MAAM,UAAU,sBAAsB,CACpC,YAAoB,EACpB,YAAwB,EACxB,YAAoB;IAEpB,MAAM,WAAW,GAAG,kBAAkB,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;IACnE,MAAM,QAAQ,GAAG,kBAAkB,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC;IAE/D,OAAO,iBAAiB,EAAE,CAAC,aAAa,CAAC,WAAW,EAAE,YAAY,EAAE,QAAQ,CAAC,CAAC;AAChF,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Membership of one disclosed operation key in a `holder_key_set_root`, under
|
|
3
|
+
* `holder-keyset.merkle-sha3-256/1` (§11 disclosure, §12 procedure, §13 failure
|
|
4
|
+
* conditions).
|
|
5
|
+
*
|
|
6
|
+
* The disclosure arrives inside adversary-controlled evidence, so it is checked
|
|
7
|
+
* as parsed JSON, not trusted as a typed object: exactly four members, integers
|
|
8
|
+
* that are JSON integers, canonical base64, lowercase hex. Fail-closed and
|
|
9
|
+
* never throws.
|
|
10
|
+
*
|
|
11
|
+
* A positive result proves membership only. It is not an identity: `tree_size`
|
|
12
|
+
* is not committed to by the root, and no signature covers the disclosure, so
|
|
13
|
+
* two disclosures with different bytes can prove the same membership.
|
|
14
|
+
*/
|
|
15
|
+
export interface DisclosureVerdict {
|
|
16
|
+
valid: boolean;
|
|
17
|
+
reason: string | null;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* @param disclosure the `operation_key` object of a `capacity.evidence/1`
|
|
21
|
+
* @param root the authorized `holder_key_set_root` (64-char lowercase hex)
|
|
22
|
+
*/
|
|
23
|
+
export declare function verifyOperationKeyDisclosure(disclosure: unknown, root: string): DisclosureVerdict;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Membership of one disclosed operation key in a `holder_key_set_root`, under
|
|
3
|
+
* `holder-keyset.merkle-sha3-256/1` (§11 disclosure, §12 procedure, §13 failure
|
|
4
|
+
* conditions).
|
|
5
|
+
*
|
|
6
|
+
* The disclosure arrives inside adversary-controlled evidence, so it is checked
|
|
7
|
+
* as parsed JSON, not trusted as a typed object: exactly four members, integers
|
|
8
|
+
* that are JSON integers, canonical base64, lowercase hex. Fail-closed and
|
|
9
|
+
* never throws.
|
|
10
|
+
*
|
|
11
|
+
* A positive result proves membership only. It is not an identity: `tree_size`
|
|
12
|
+
* is not committed to by the root, and no signature covers the disclosure, so
|
|
13
|
+
* two disclosures with different bytes can prove the same membership.
|
|
14
|
+
*/
|
|
15
|
+
import { computeLeafHash, verifyInclusionProof } from "./merkle.js";
|
|
16
|
+
import { strictBase64Decode } from "./provider/bytes.js";
|
|
17
|
+
const DISCLOSURE_MEMBERS = ["inclusion_proof", "leaf_index", "public_key_b64", "tree_size"];
|
|
18
|
+
function reject(reason) {
|
|
19
|
+
return { valid: false, reason };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @param disclosure the `operation_key` object of a `capacity.evidence/1`
|
|
23
|
+
* @param root the authorized `holder_key_set_root` (64-char lowercase hex)
|
|
24
|
+
*/
|
|
25
|
+
export function verifyOperationKeyDisclosure(disclosure, root) {
|
|
26
|
+
if (disclosure === null || typeof disclosure !== "object" || Array.isArray(disclosure)) {
|
|
27
|
+
return reject("disclosure is not a JSON object");
|
|
28
|
+
}
|
|
29
|
+
const d = disclosure;
|
|
30
|
+
const keys = Object.keys(d).sort();
|
|
31
|
+
if (keys.length !== DISCLOSURE_MEMBERS.length || keys.some((k, i) => k !== DISCLOSURE_MEMBERS[i])) {
|
|
32
|
+
return reject("disclosure members are not exactly leaf_index, public_key_b64, inclusion_proof, tree_size");
|
|
33
|
+
}
|
|
34
|
+
const n = d.tree_size;
|
|
35
|
+
const i = d.leaf_index;
|
|
36
|
+
if (typeof n !== "number" || !Number.isSafeInteger(n) || n < 1) {
|
|
37
|
+
return reject("tree_size is not a positive JSON integer");
|
|
38
|
+
}
|
|
39
|
+
if (typeof i !== "number" || !Number.isSafeInteger(i) || i < 0) {
|
|
40
|
+
return reject("leaf_index is not a non-negative JSON integer");
|
|
41
|
+
}
|
|
42
|
+
if (i >= n) {
|
|
43
|
+
return reject("leaf_index >= tree_size");
|
|
44
|
+
}
|
|
45
|
+
if (typeof d.public_key_b64 !== "string") {
|
|
46
|
+
return reject("public_key_b64 is not a string");
|
|
47
|
+
}
|
|
48
|
+
let pk;
|
|
49
|
+
try {
|
|
50
|
+
pk = strictBase64Decode(d.public_key_b64, "public_key_b64");
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return reject("public_key_b64 is not canonical base64");
|
|
54
|
+
}
|
|
55
|
+
const proof = d.inclusion_proof;
|
|
56
|
+
if (!Array.isArray(proof) || !proof.every((h) => typeof h === "string" && /^[0-9a-f]{64}$/.test(h))) {
|
|
57
|
+
return reject("an inclusion_proof entry is not 64-char lowercase hex");
|
|
58
|
+
}
|
|
59
|
+
if (!verifyInclusionProof(computeLeafHash(pk), i, proof, root, n)) {
|
|
60
|
+
return reject("the inclusion proof does not bind the key to the root");
|
|
61
|
+
}
|
|
62
|
+
return { valid: true, reason: null };
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=holder-keyset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"holder-keyset.js","sourceRoot":"","sources":["../../../core/crypto/holder-keyset.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACpE,OAAO,EAAE,kBAAkB,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,MAAM,CAAC,+CAA+C,CAAC,CAAC;IACjE,CAAC;IACD,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACX,OAAO,MAAM,CAAC,yBAAyB,CAAC,CAAC;IAC3C,CAAC;IACD,IAAI,OAAO,CAAC,CAAC,cAAc,KAAK,QAAQ,EAAE,CAAC;QACzC,OAAO,MAAM,CAAC,gCAAgC,CAAC,CAAC;IAClD,CAAC;IACD,IAAI,EAAc,CAAC;IACnB,IAAI,CAAC;QACH,EAAE,GAAG,kBAAkB,CAAC,CAAC,CAAC,cAAc,EAAE,gBAAgB,CAAC,CAAC;IAC9D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,MAAM,CAAC,wCAAwC,CAAC,CAAC;IAC1D,CAAC;IACD,MAAM,KAAK,GAAG,CAAC,CAAC,eAAe,CAAC;IAChC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACpG,OAAO,MAAM,CAAC,uDAAuD,CAAC,CAAC;IACzE,CAAC;IACD,IAAI,CAAC,oBAAoB,CAAC,eAAe,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,KAAiB,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;QAC9E,OAAO,MAAM,CAAC,uDAAuD,CAAC,CAAC;IACzE,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC 8785 JSON Canonicalization Scheme (JCS).
|
|
3
|
+
*
|
|
4
|
+
* Per CCCP/1: the canonical form for payload signing uses RFC 8785.
|
|
5
|
+
* This means lexicographic key ordering, no extra whitespace,
|
|
6
|
+
* and deterministic number serialization.
|
|
7
|
+
*/
|
|
8
|
+
export declare function jcsCanonicalizeBytes(obj: unknown): Uint8Array;
|
|
9
|
+
export declare function jcsCanonicalizeString(obj: unknown): string;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC 8785 JSON Canonicalization Scheme (JCS).
|
|
3
|
+
*
|
|
4
|
+
* Per CCCP/1: the canonical form for payload signing uses RFC 8785.
|
|
5
|
+
* This means lexicographic key ordering, no extra whitespace,
|
|
6
|
+
* and deterministic number serialization.
|
|
7
|
+
*/
|
|
8
|
+
export function jcsCanonicalizeBytes(obj) {
|
|
9
|
+
const canonical = jcsStringify(obj);
|
|
10
|
+
return new TextEncoder().encode(canonical);
|
|
11
|
+
}
|
|
12
|
+
export function jcsCanonicalizeString(obj) {
|
|
13
|
+
return jcsStringify(obj);
|
|
14
|
+
}
|
|
15
|
+
function jcsStringify(value) {
|
|
16
|
+
if (value === null)
|
|
17
|
+
return "null";
|
|
18
|
+
if (value === undefined)
|
|
19
|
+
return "null";
|
|
20
|
+
const t = typeof value;
|
|
21
|
+
if (t === "boolean")
|
|
22
|
+
return value ? "true" : "false";
|
|
23
|
+
if (t === "number") {
|
|
24
|
+
// RFC 8785 §3.2.2.3 mandates the ECMAScript Number-to-String algorithm.
|
|
25
|
+
// JS `String(n)` IS that algorithm, so it matches byte-for-byte for every
|
|
26
|
+
// finite double, including the cases sometimes assumed to diverge:
|
|
27
|
+
// `String(-0) === "0"` (RFC 8785 requires the negative-zero sign be dropped),
|
|
28
|
+
// exponent form for |n| ≥ 1e21 or < 1e-6, and large integers. Infinity/NaN
|
|
29
|
+
// are not valid JSON and are rejected. Note the string-only subject profiles
|
|
30
|
+
// (collection/ai-decision/context-snapshot) never reach this branch — their
|
|
31
|
+
// preconditions reject any JSON number before canonicalization; this branch
|
|
32
|
+
// serves numbers only in certificate structures (anchor/payload/proof).
|
|
33
|
+
if (!isFinite(value)) {
|
|
34
|
+
throw new Error("JCS does not support Infinity or NaN");
|
|
35
|
+
}
|
|
36
|
+
return String(value);
|
|
37
|
+
}
|
|
38
|
+
if (t === "string") {
|
|
39
|
+
return JSON.stringify(value);
|
|
40
|
+
}
|
|
41
|
+
if (Array.isArray(value)) {
|
|
42
|
+
const items = value.map((v) => jcsStringify(v));
|
|
43
|
+
return "[" + items.join(",") + "]";
|
|
44
|
+
}
|
|
45
|
+
if (t === "object" && value !== null) {
|
|
46
|
+
const obj = value;
|
|
47
|
+
// RFC 8785 §3.2.3: sort keys by UTF-16 code unit. JS `Array.prototype.sort()`
|
|
48
|
+
// on strings compares by UTF-16 code unit, which is exactly this (and differs
|
|
49
|
+
// from code-point order only for supplementary-plane keys — do NOT "fix" this
|
|
50
|
+
// to a code-point sort).
|
|
51
|
+
const keys = Object.keys(obj).sort();
|
|
52
|
+
const members = keys
|
|
53
|
+
.filter((k) => obj[k] !== undefined)
|
|
54
|
+
.map((k) => JSON.stringify(k) + ":" + jcsStringify(obj[k]));
|
|
55
|
+
return "{" + members.join(",") + "}";
|
|
56
|
+
}
|
|
57
|
+
throw new Error(`JCS: unsupported type ${t}`);
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=jcs.js.map
|