entropy-protocol 0.1.0__py3-none-any.whl
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.
- entropy_protocol/__init__.py +11 -0
- entropy_protocol/_unicode14.py +120 -0
- entropy_protocol/attest.py +62 -0
- entropy_protocol/attested/__init__.py +6 -0
- entropy_protocol/attested/amd.py +221 -0
- entropy_protocol/attested/appraise.py +325 -0
- entropy_protocol/attested/attester.py +58 -0
- entropy_protocol/attested/authority.py +239 -0
- entropy_protocol/attested/azure_cgpu.py +186 -0
- entropy_protocol/attested/binding.py +71 -0
- entropy_protocol/attested/capture.py +109 -0
- entropy_protocol/attested/ear.py +94 -0
- entropy_protocol/attested/enroll.py +119 -0
- entropy_protocol/attested/evidence.py +106 -0
- entropy_protocol/attested/hcl.py +109 -0
- entropy_protocol/attested/nvidia.py +168 -0
- entropy_protocol/attested/ocsp.py +90 -0
- entropy_protocol/attested/pki.py +121 -0
- entropy_protocol/attested/rim.py +130 -0
- entropy_protocol/attested/service.py +242 -0
- entropy_protocol/attested/tpm.py +259 -0
- entropy_protocol/backend_hf.py +439 -0
- entropy_protocol/batch.py +537 -0
- entropy_protocol/calibrate.py +99 -0
- entropy_protocol/canon.py +321 -0
- entropy_protocol/canongen.py +325 -0
- entropy_protocol/cli.py +493 -0
- entropy_protocol/cose.py +260 -0
- entropy_protocol/crypto.py +140 -0
- entropy_protocol/delivery.py +31 -0
- entropy_protocol/detect.py +246 -0
- entropy_protocol/gateway.py +370 -0
- entropy_protocol/hpke.py +105 -0
- entropy_protocol/mcp.py +159 -0
- entropy_protocol/pack.py +217 -0
- entropy_protocol/plain.py +122 -0
- entropy_protocol/predicates.py +37 -0
- entropy_protocol/profiles.py +45 -0
- entropy_protocol/record.py +116 -0
- entropy_protocol/scrapi.py +203 -0
- entropy_protocol/seal.py +232 -0
- entropy_protocol/statements.py +148 -0
- entropy_protocol/stats.py +20 -0
- entropy_protocol/tlog.py +147 -0
- entropy_protocol/ts3161.py +205 -0
- entropy_protocol/typography.py +73 -0
- entropy_protocol/verify.py +232 -0
- entropy_protocol-0.1.0.dist-info/METADATA +255 -0
- entropy_protocol-0.1.0.dist-info/RECORD +54 -0
- entropy_protocol-0.1.0.dist-info/WHEEL +5 -0
- entropy_protocol-0.1.0.dist-info/entry_points.txt +2 -0
- entropy_protocol-0.1.0.dist-info/licenses/LICENSE +201 -0
- entropy_protocol-0.1.0.dist-info/licenses/NOTICE +4 -0
- entropy_protocol-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""NVIDIA reference integrity manifests (RIMs): fetch, signature, golden measurements, comparison.
|
|
2
|
+
|
|
3
|
+
Semantics follow NVIDIA's own local GPU verifier (nvtrust tag 2026.06.04.001, `rim/`, `verifier.py`,
|
|
4
|
+
`cc_admin_utils.py`), re-implemented so the appraisal does not depend on NVIDIA's code or service at check time:
|
|
5
|
+
- ids: driver `NV_GPU_DRIVER_GH100_<driver version>`, VBIOS `NV_GPU_VBIOS_<project>_<project sku>_<chip sku>_<vbios
|
|
6
|
+
version>`; served by https://rim.attestation.nvidia.com/v1/rim/<id> as JSON {"rim": base64 of the SWID XML};
|
|
7
|
+
- a RIM is an ISO/IEC 19770-2 SWID tag with an XML signature (verified with signxml against NVIDIA's pinned RIM
|
|
8
|
+
root); its Payload holds one Resource per measurement index {index, active, alternatives, size, name,
|
|
9
|
+
Hash0..Hash<n-1> in the namespace http://www.w3.org/2001/04/xmlenc#sha384};
|
|
10
|
+
- golden = the active measurements of both RIMs (an index active in both is an error); every golden index i must have
|
|
11
|
+
a runtime measurement equal to one of its alternatives, of the stated size - RIM indices count from 0, SPDM
|
|
12
|
+
measurement blocks from 1, so golden i is block i + 1 (nvtrust `get_measurements`: list[index - 1]); index 35 is
|
|
13
|
+
skipped when the report's opaque data says NVDEC0 is disabled (0x55).
|
|
14
|
+
"""
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import base64
|
|
18
|
+
import json
|
|
19
|
+
import urllib.request
|
|
20
|
+
|
|
21
|
+
RIM_URL = "https://rim.attestation.nvidia.com/v1/rim/"
|
|
22
|
+
HASH_NS = "{http://www.w3.org/2001/04/xmlenc#sha384}"
|
|
23
|
+
NVDEC_DISABLED = 0x55
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class RIMError(ValueError):
|
|
27
|
+
"""A RIM that cannot be read, is not signed by NVIDIA, or does not match."""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def driver_id(driver_version: str) -> str:
|
|
31
|
+
return "NV_GPU_DRIVER_GH100_" + driver_version
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def vbios_id(project: str, project_sku: str, chip_sku: str, vbios_version: str) -> str:
|
|
35
|
+
return f"NV_GPU_VBIOS_{project}_{project_sku}_{chip_sku}_{vbios_version}"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def fetch(rim_id: str, timeout: int = 20) -> bytes:
|
|
39
|
+
"""The RIM's XML as NVIDIA's service returns it (decoded from its JSON envelope)."""
|
|
40
|
+
with urllib.request.urlopen(RIM_URL + rim_id, timeout=timeout) as r: # noqa: S310 - fixed https URL
|
|
41
|
+
return base64.b64decode(json.loads(r.read())["rim"])
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _chain(tree) -> list:
|
|
45
|
+
"""The certificates the signature carries (ds:X509Certificate), ordered leaf first by issuer name."""
|
|
46
|
+
from cryptography import x509
|
|
47
|
+
certs = [x509.load_der_x509_certificate(base64.b64decode("".join(e.text.split())))
|
|
48
|
+
for e in tree.iter("{http://www.w3.org/2000/09/xmldsig#}X509Certificate")]
|
|
49
|
+
if not certs:
|
|
50
|
+
raise RIMError("RIM signature carries no certificate")
|
|
51
|
+
issuers = {c.issuer for c in certs if c.issuer != c.subject}
|
|
52
|
+
leaf = [c for c in certs if c.subject not in issuers]
|
|
53
|
+
if len(leaf) != 1:
|
|
54
|
+
raise RIMError("cannot tell the RIM signer among its certificates")
|
|
55
|
+
chain = leaf
|
|
56
|
+
while chain[-1].issuer != chain[-1].subject:
|
|
57
|
+
up = next((c for c in certs if c.subject == chain[-1].issuer), None)
|
|
58
|
+
if up is None:
|
|
59
|
+
raise RIMError("RIM certificate chain is incomplete")
|
|
60
|
+
chain.append(up)
|
|
61
|
+
return chain
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def verify(xml: bytes, at) -> object:
|
|
65
|
+
"""The signed XML subtree, if the signature verifies under the signer's certificate and that certificate chains
|
|
66
|
+
to NVIDIA's pinned RIM root; RIMError otherwise.
|
|
67
|
+
|
|
68
|
+
The chain is checked here, signature by signature, not by signxml: signxml 5.2 validates paths under RFC 5280
|
|
69
|
+
and refuses NVIDIA's RIM chain (a validity date before 2050 encoded as GeneralizedTime; checked 27.10.2026 on
|
|
70
|
+
NV_GPU_DRIVER_GH100_595.91.07), while NVIDIA's own verifier pins signxml 3.2.0, which does not. The signature
|
|
71
|
+
itself (ecdsa-sha384, C14N 1.1) is signxml's, under the signer certificate we validated."""
|
|
72
|
+
from cryptography.hazmat.primitives import serialization
|
|
73
|
+
from lxml import etree
|
|
74
|
+
from signxml import XMLVerifier
|
|
75
|
+
from cryptography.exceptions import InvalidSignature
|
|
76
|
+
from signxml.exceptions import SignXMLException
|
|
77
|
+
|
|
78
|
+
from .nvidia import GPUError, RIM_ROOT_SHA256, verify_chain
|
|
79
|
+
from .pki import UNREADABLE
|
|
80
|
+
parser = etree.XMLParser(resolve_entities=False, no_network=True, huge_tree=False)
|
|
81
|
+
try:
|
|
82
|
+
tree = etree.fromstring(xml, parser)
|
|
83
|
+
chain = _chain(tree)
|
|
84
|
+
verify_chain(chain, RIM_ROOT_SHA256, at)
|
|
85
|
+
leaf_pem = chain[0].public_bytes(serialization.Encoding.PEM).decode()
|
|
86
|
+
return XMLVerifier().verify(xml, x509_cert=leaf_pem, parser=parser).signed_xml
|
|
87
|
+
except (SignXMLException, InvalidSignature, etree.LxmlError, GPUError, *UNREADABLE) as exc:
|
|
88
|
+
raise RIMError(f"RIM signature does not verify: {exc}") from exc # every signxml and lxml error included
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def golden(tree) -> dict:
|
|
92
|
+
"""index -> {"active", "size", "values": [hex], "name"} from the Payload."""
|
|
93
|
+
payload = next((el for el in tree.iter() if isinstance(el.tag, str) and el.tag.endswith("Payload")), None)
|
|
94
|
+
if payload is None:
|
|
95
|
+
raise RIMError("RIM has no Payload")
|
|
96
|
+
out: dict = {}
|
|
97
|
+
for res in payload:
|
|
98
|
+
a = res.attrib
|
|
99
|
+
try:
|
|
100
|
+
idx, size = int(a["index"]), int(a["size"])
|
|
101
|
+
values = [a[f"{HASH_NS}Hash{i}"].lower() for i in range(int(a["alternatives"]))]
|
|
102
|
+
except (KeyError, ValueError) as exc:
|
|
103
|
+
raise RIMError(f"malformed measurement {a.get('name', '?')!r} ({type(exc).__name__}: {exc})") from exc
|
|
104
|
+
if idx in out:
|
|
105
|
+
raise RIMError(f"index {idx} appears twice")
|
|
106
|
+
out[idx] = {"active": a.get("active") != "False", "size": size, "name": a.get("name", ""), "values": values}
|
|
107
|
+
if not out:
|
|
108
|
+
raise RIMError("RIM has no measurements")
|
|
109
|
+
return out
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def compare(runtime: dict, driver: dict, vbios: dict, nvdec_status: int | None) -> list[tuple[int, str]]:
|
|
113
|
+
"""Mismatched (RIM index, source) pairs; empty means the GPU is in a state NVIDIA's manifests describe.
|
|
114
|
+
``runtime`` maps SPDM block index (from 1) to the measurement."""
|
|
115
|
+
gold = {i: (m, "driver") for i, m in driver.items() if m["active"]}
|
|
116
|
+
for i, m in vbios.items():
|
|
117
|
+
if m["active"]:
|
|
118
|
+
if i in gold:
|
|
119
|
+
raise RIMError(f"driver and VBIOS RIMs both define index {i}")
|
|
120
|
+
gold[i] = (m, "vbios")
|
|
121
|
+
if len(gold) > len(runtime):
|
|
122
|
+
return [(-1, "more golden measurements than runtime ones")]
|
|
123
|
+
bad = []
|
|
124
|
+
for i, (m, src) in sorted(gold.items()):
|
|
125
|
+
if i == 35 and nvdec_status == NVDEC_DISABLED:
|
|
126
|
+
continue
|
|
127
|
+
v = runtime.get(i + 1, b"")
|
|
128
|
+
if not (len(v) == m["size"] and v.hex() in m["values"]):
|
|
129
|
+
bad.append((i, src))
|
|
130
|
+
return bad
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"""The sealing service inside the confidential VM (ARCHITECTURE §7d): enroll, then seal and detect; and the
|
|
2
|
+
verifier's pass, run as a separate process.
|
|
3
|
+
|
|
4
|
+
`seal` - one process holds every secret in memory and writes none of them:
|
|
5
|
+
1. load the model; generate the master key K (and so K_wm) here;
|
|
6
|
+
2. for the statement key, then the detection key: wait for the authority's initiation in the conduit directory,
|
|
7
|
+
enroll (attester.prepare: a fresh Ed25519 key, its CSR, the binding into the paravisor's report, the quote and
|
|
8
|
+
every GPU's report), write the request, wait for the certificate and the attestation result, and check both
|
|
9
|
+
against the key before using it;
|
|
10
|
+
3. seal every prompt; deliver each record by HPKE to the verifier's public key; sign the generation statement under
|
|
11
|
+
the certified key, with the certificate (x5chain, label 33) and SHA-256 of the result ("ep1-ear") protected;
|
|
12
|
+
4. detect every sealed text and every control text with K_wm; sign each detection statement the same way.
|
|
13
|
+
`verify` - the verifier, with its own keys (an X25519 key the records were delivered to, an Ed25519 key it signs
|
|
14
|
+
under): every sealed text as delivered, with one word changed, and after ordinary transport.
|
|
15
|
+
The conduit directory is an untrusted transport; the authority runs elsewhere (authority.py).
|
|
16
|
+
"""
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import argparse
|
|
20
|
+
import hashlib
|
|
21
|
+
import json
|
|
22
|
+
import sys
|
|
23
|
+
import time
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
|
|
26
|
+
from .. import ENGINE, crypto, statements
|
|
27
|
+
from . import attester, ear, enroll, pki
|
|
28
|
+
|
|
29
|
+
ROLES = {"seal": enroll.CRED_STATEMENT, "detect": enroll.CRED_DETECTION}
|
|
30
|
+
EAR_REF = "ep1-ear"
|
|
31
|
+
X5CHAIN = 33
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def wait_for(path: Path, timeout: float, poll: float = 1.0) -> bytes:
|
|
35
|
+
end = time.time() + timeout
|
|
36
|
+
while time.time() < end:
|
|
37
|
+
if path.exists() and path.stat().st_size:
|
|
38
|
+
time.sleep(0.2) # a copy that is still being written
|
|
39
|
+
return path.read_bytes()
|
|
40
|
+
time.sleep(poll)
|
|
41
|
+
raise TimeoutError(f"{path.name} did not arrive within {timeout:.0f} s")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def params_map(params) -> dict:
|
|
45
|
+
"""The sealing parameters as canonical-CBOR-safe values: no floating point (micro-units), as on every ep1 wire."""
|
|
46
|
+
out = {}
|
|
47
|
+
for k, v in sorted(vars(params).items()):
|
|
48
|
+
if isinstance(v, bool) or v is None or isinstance(v, (int, str)):
|
|
49
|
+
out[k] = v
|
|
50
|
+
elif isinstance(v, float):
|
|
51
|
+
out[k + "_u"] = round(v * 1_000_000)
|
|
52
|
+
elif isinstance(v, dict):
|
|
53
|
+
out[k] = {kk: round(vv * 1_000_000) for kk, vv in sorted(v.items())}
|
|
54
|
+
else:
|
|
55
|
+
raise TypeError(f"parameter {k} of type {type(v).__name__}")
|
|
56
|
+
return out
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def sealing_params(cal_path: str | None, max_new_tokens: int, profile: str, weights: str):
|
|
60
|
+
from ..profiles import seal_overrides
|
|
61
|
+
from ..seal import SealParams, table_covers
|
|
62
|
+
prof = seal_overrides(profile)
|
|
63
|
+
if not cal_path:
|
|
64
|
+
return SealParams(max_new_tokens=max_new_tokens, **prof)
|
|
65
|
+
cal = json.loads(Path(cal_path).read_text())
|
|
66
|
+
if cal["weights_digest"] != weights:
|
|
67
|
+
raise SystemExit("calibration file belongs to a different model")
|
|
68
|
+
params = SealParams(max_new_tokens=max_new_tokens, tol_space=cal["space"], cal=cal["table"], **prof)
|
|
69
|
+
problem = table_covers(cal, params)
|
|
70
|
+
if problem:
|
|
71
|
+
raise SystemExit(problem)
|
|
72
|
+
return params
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def check_credential(cert_der: bytes, ear_bytes: bytes, root_der: bytes, root_sha256: str, key,
|
|
76
|
+
credential_type: str) -> dict:
|
|
77
|
+
"""The root is the one this sealer is configured with (the conduit could carry another); the certificate is the
|
|
78
|
+
root's, for this key; the result is the root's, affirming, about this key."""
|
|
79
|
+
from cryptography import x509
|
|
80
|
+
import datetime as dt
|
|
81
|
+
if hashlib.sha256(root_der).hexdigest() != root_sha256:
|
|
82
|
+
raise SystemExit("the conduit carries another authority's root")
|
|
83
|
+
root = x509.load_der_x509_certificate(root_der)
|
|
84
|
+
leaf_key = pki.check_leaf(cert_der, root, dt.datetime.now(dt.timezone.utc), pki.STATEMENT_EKU)
|
|
85
|
+
if pki.spki(leaf_key) != pki.spki(key.public_key()):
|
|
86
|
+
raise SystemExit("the certificate is for another key")
|
|
87
|
+
c = ear.read(ear_bytes, root.public_key())
|
|
88
|
+
ext = c.get(ear.EP1, {})
|
|
89
|
+
if c.get(ear.STATUS) != ear.AFFIRMING or ext.get("credential_type") != credential_type or \
|
|
90
|
+
ext.get("key") != hashlib.sha256(pki.spki(key.public_key())).digest():
|
|
91
|
+
raise SystemExit(f"the attestation result does not affirm this key: status {c.get(ear.STATUS)}, "
|
|
92
|
+
f"failed {ext.get('failed')}")
|
|
93
|
+
return c
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def enroll_role(io: Path, role: str, rrp_id: str, root_sha256: str, target: str, backend, sealing_class: str,
|
|
97
|
+
params: dict, timeout: float) -> tuple:
|
|
98
|
+
ini = wait_for(io / f"initiation-{role}.cbor", timeout)
|
|
99
|
+
t0 = time.time()
|
|
100
|
+
e = attester.prepare(initiation=ini, rrp_id=rrp_id, target=target, weights=bytes.fromhex(backend.weights_digest),
|
|
101
|
+
tokenizer=bytes.fromhex(backend.tokenizer_digest), sealing_class=sealing_class,
|
|
102
|
+
params=params, credential_type=ROLES[role])
|
|
103
|
+
(io / f"request-{role}.cbor").write_bytes(e.request)
|
|
104
|
+
evidence_s = time.time() - t0
|
|
105
|
+
cert = wait_for(io / f"cert-{role}.der", timeout)
|
|
106
|
+
result = wait_for(io / f"ear-{role}.cose", timeout)
|
|
107
|
+
check_credential(cert, result, (io / "root.der").read_bytes(), root_sha256, e.key, ROLES[role])
|
|
108
|
+
headers = {X5CHAIN: cert, EAR_REF: hashlib.sha256(result).digest()}
|
|
109
|
+
return e.key, headers, {"evidence_seconds": round(evidence_s, 3), "binding": e.binding.value().hex()}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def cmd_seal(a) -> int:
|
|
113
|
+
import torch
|
|
114
|
+
|
|
115
|
+
from ..backend_hf import HFBackend
|
|
116
|
+
from ..delivery import deliver
|
|
117
|
+
from ..detect import detect
|
|
118
|
+
from ..profiles import detect_gate_for
|
|
119
|
+
from ..seal import seal
|
|
120
|
+
io, out = Path(a.io), Path(a.out)
|
|
121
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
122
|
+
t_load = time.time()
|
|
123
|
+
backend = HFBackend(a.model, device="cuda", dtype=torch.float16)
|
|
124
|
+
params = sealing_params(a.cal, a.max_new_tokens, a.profile, backend.weights_digest)
|
|
125
|
+
pmap = params_map(params)
|
|
126
|
+
(io / "ready.json").write_text(json.dumps({
|
|
127
|
+
"model": backend.model_ref, "weights": backend.weights_digest, "tokenizer": backend.tokenizer_digest,
|
|
128
|
+
"class": backend.config_class, "params": pmap, "load_seconds": round(time.time() - t_load, 1),
|
|
129
|
+
"env": backend.environment}, indent=1))
|
|
130
|
+
master = crypto.new_master_key() # generated here, never written
|
|
131
|
+
k_wm = crypto.wm_key(master)
|
|
132
|
+
keys, headers, info = {}, {}, {}
|
|
133
|
+
for role in ("seal", "detect"):
|
|
134
|
+
keys[role], headers[role], info[role] = enroll_role(io, role, a.rrp_id, a.root_sha256, f"{a.target}/{role}",
|
|
135
|
+
backend, backend.config_class, pmap, a.timeout)
|
|
136
|
+
verifier_pub = bytes.fromhex(Path(a.verifier_pub).read_text().strip())
|
|
137
|
+
prompts = [json.loads(x) for x in Path(a.prompts).read_text().splitlines() if x.strip()][: a.n]
|
|
138
|
+
sealed = []
|
|
139
|
+
for p in prompts:
|
|
140
|
+
t0 = time.time()
|
|
141
|
+
r = seal(backend, p["prompt"], master, params)
|
|
142
|
+
log_key = crypto.log_key(master, r.doc_id)
|
|
143
|
+
gen, _ = statements.generation_statement(log_key, r.token, keys["seal"], f"{a.target}/seal",
|
|
144
|
+
extra=headers["seal"])
|
|
145
|
+
d = out / r.doc_id
|
|
146
|
+
d.mkdir()
|
|
147
|
+
(d / "text.txt").write_text(r.text)
|
|
148
|
+
(d / "record.delivered.cose").write_bytes(deliver(log_key, r.token, verifier_pub))
|
|
149
|
+
(d / "generation.cose").write_bytes(gen)
|
|
150
|
+
sealed.append({"doc": r.doc_id, "prompt_id": p["id"], "tokens": r.tokens, "entries": r.entries,
|
|
151
|
+
"seconds": round(time.time() - t0, 2)})
|
|
152
|
+
controls = [json.loads(x) for x in Path(a.controls).read_text().splitlines() if x.strip()][: a.n_controls] \
|
|
153
|
+
if a.controls else []
|
|
154
|
+
gate = detect_gate_for(params)
|
|
155
|
+
detections = []
|
|
156
|
+
for item in [{"doc": s["doc"], "text": (out / s["doc"] / "text.txt").read_text(), "kind": "sealed"} for s in sealed] + \
|
|
157
|
+
[{"doc": f"control-{c['id']}", "text": c["text"], "kind": "control"} for c in controls]:
|
|
158
|
+
det = detect(backend, item["text"], k_wm, interval=params.interval, k=params.k, context=params.context,
|
|
159
|
+
gate=gate, alpha=a.alpha)
|
|
160
|
+
st, body = statements.detection_statement(det, item["text"], backend, keys["detect"], f"{a.target}/detect",
|
|
161
|
+
extra=headers["detect"])
|
|
162
|
+
d = out / ("detections" if item["kind"] == "control" else item["doc"])
|
|
163
|
+
d.mkdir(exist_ok=True)
|
|
164
|
+
name = f"{item['doc']}.detection.cose" if item["kind"] == "control" else "detection.cose"
|
|
165
|
+
(d / name).write_bytes(st)
|
|
166
|
+
if item["kind"] == "control":
|
|
167
|
+
(d / f"{item['doc']}.txt").write_text(item["text"])
|
|
168
|
+
detections.append({"doc": item["doc"], "kind": item["kind"], "detected": det.detected,
|
|
169
|
+
"matched": det.matched, "checkpoints": det.checkpoints, "p": det.p_value})
|
|
170
|
+
manifest = {"engine": ENGINE, "model": backend.model_ref, "weights": backend.weights_digest,
|
|
171
|
+
"tokenizer": backend.tokenizer_digest, "class": backend.config_class, "profile": a.profile,
|
|
172
|
+
"params": pmap, "enrollment": info, "sealed": sealed, "detections": detections,
|
|
173
|
+
"detect_gate": gate, "alpha": a.alpha}
|
|
174
|
+
(out / "manifest.json").write_text(json.dumps(manifest, indent=1))
|
|
175
|
+
(io / "done.json").write_text(json.dumps({"sealed": len(sealed), "detections": len(detections)}))
|
|
176
|
+
return 0
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def cmd_verify(a) -> int:
|
|
180
|
+
import torch
|
|
181
|
+
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
|
|
182
|
+
|
|
183
|
+
from ..backend_hf import HFBackend
|
|
184
|
+
from ..verify import verify_delivered
|
|
185
|
+
out = Path(a.out)
|
|
186
|
+
backend = HFBackend(a.model, device="cuda", dtype=torch.float16)
|
|
187
|
+
priv = bytes.fromhex(Path(a.verifier_key).read_text().strip())
|
|
188
|
+
signer = Ed25519PrivateKey.generate()
|
|
189
|
+
(out / "verifier.pub").write_text(signer.public_key().public_bytes_raw().hex())
|
|
190
|
+
rows = []
|
|
191
|
+
for d in sorted(p for p in out.iterdir() if (p / "generation.cose").exists()):
|
|
192
|
+
text = (d / "text.txt").read_text()
|
|
193
|
+
delivered = (d / "record.delivered.cose").read_bytes()
|
|
194
|
+
words = text.split(" ")
|
|
195
|
+
mid = len(words) // 2
|
|
196
|
+
edited = " ".join(words[:mid] + ["remarkably"] + words[mid + 1:])
|
|
197
|
+
transported = text.replace("\n", "\r\n").replace(" ", " ", 3) + "\n\n"
|
|
198
|
+
for kind, cand in (("as-sealed", text), ("one-word-changed", edited), ("transported", transported)):
|
|
199
|
+
res = verify_delivered(backend, cand, delivered, priv)
|
|
200
|
+
st, _ = statements.verification_statement(res, cand, signer, a.verifier_id,
|
|
201
|
+
generation=(d / "generation.cose").read_bytes())
|
|
202
|
+
(d / f"verification.{kind}.cose").write_bytes(st)
|
|
203
|
+
if kind != "as-sealed":
|
|
204
|
+
(d / f"candidate.{kind}.txt").write_text(cand)
|
|
205
|
+
rows.append({"doc": d.name, "kind": kind, "verdict": res.verdict, "matched": res.matched,
|
|
206
|
+
"divergent": res.divergent, "indeterminate": res.indeterminate, "entries": res.entries,
|
|
207
|
+
"changed": res.changed_ranges})
|
|
208
|
+
(out / "verification.json").write_text(json.dumps(rows, indent=1))
|
|
209
|
+
return 0
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def main(argv: list[str] | None = None) -> int:
|
|
213
|
+
ap = argparse.ArgumentParser(prog="entropy-attested-service")
|
|
214
|
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
215
|
+
s = sub.add_parser("seal")
|
|
216
|
+
s.add_argument("--io", required=True, help="conduit directory shared with the authority's side")
|
|
217
|
+
s.add_argument("--out", required=True)
|
|
218
|
+
s.add_argument("--model", required=True)
|
|
219
|
+
s.add_argument("--cal")
|
|
220
|
+
s.add_argument("--profile", default="quality")
|
|
221
|
+
s.add_argument("--max-new-tokens", type=int, default=200)
|
|
222
|
+
s.add_argument("--prompts", required=True)
|
|
223
|
+
s.add_argument("--n", type=int, default=20)
|
|
224
|
+
s.add_argument("--controls")
|
|
225
|
+
s.add_argument("--n-controls", type=int, default=10)
|
|
226
|
+
s.add_argument("--alpha", type=float, default=1e-6)
|
|
227
|
+
s.add_argument("--verifier-pub", required=True, help="file with the verifier's X25519 public key (hex)")
|
|
228
|
+
s.add_argument("--rrp-id", required=True, help="the authority this sealer is configured for")
|
|
229
|
+
s.add_argument("--root-sha256", required=True, help="SHA-256 of that authority's root certificate (DER)")
|
|
230
|
+
s.add_argument("--target", required=True)
|
|
231
|
+
s.add_argument("--timeout", type=float, default=1800)
|
|
232
|
+
v = sub.add_parser("verify")
|
|
233
|
+
v.add_argument("--out", required=True)
|
|
234
|
+
v.add_argument("--model", required=True)
|
|
235
|
+
v.add_argument("--verifier-key", required=True, help="file with the verifier's X25519 private key (hex)")
|
|
236
|
+
v.add_argument("--verifier-id", required=True)
|
|
237
|
+
a = ap.parse_args(argv)
|
|
238
|
+
return cmd_seal(a) if a.cmd == "seal" else cmd_verify(a)
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
if __name__ == "__main__":
|
|
242
|
+
sys.exit(main())
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"""TPM 2.0 quotes and the TCG event log, read from the specifications.
|
|
2
|
+
|
|
3
|
+
TPMS_ATTEST (TPM 2.0 Library Part 2, big-endian): magic 0xff544347, type (0x8018 quote), qualifiedSigner TPM2B_NAME,
|
|
4
|
+
extraData TPM2B_DATA (the qualifying data), clockInfo {clock u64, resetCount u32, restartCount u32, safe u8},
|
|
5
|
+
firmwareVersion u64, TPMS_QUOTE_INFO {pcrSelect TPML_PCR_SELECTION, pcrDigest TPM2B_DIGEST}.
|
|
6
|
+
TPMT_SIGNATURE: sigAlg u16 (RSASSA 0x0014, RSAPSS 0x0016, ECDSA 0x0018), hash u16, then the signature.
|
|
7
|
+
Event log (TCG PC Client Platform Firmware Profile, little-endian): a SHA-1-format Spec ID event, then
|
|
8
|
+
TCG_PCR_EVENT2 {pcrIndex, eventType, TPML_DIGEST_VALUES, eventSize, event}. Replay: PCR = H(PCR || digest) for
|
|
9
|
+
every event but EV_NO_ACTION; a StartupLocality EV_NO_ACTION event sets PCR0's starting value to its locality.
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import hashlib
|
|
14
|
+
from dataclasses import dataclass, field
|
|
15
|
+
|
|
16
|
+
from cryptography.exceptions import InvalidSignature
|
|
17
|
+
from cryptography.hazmat.primitives import hashes
|
|
18
|
+
from cryptography.hazmat.primitives.asymmetric import padding
|
|
19
|
+
|
|
20
|
+
TPM_GENERATED = 0xFF544347
|
|
21
|
+
ST_ATTEST_QUOTE = 0x8018
|
|
22
|
+
ALG_SHA1, ALG_SHA256, ALG_SHA384, ALG_SHA512 = 0x0004, 0x000B, 0x000C, 0x000D
|
|
23
|
+
ALG_RSASSA, ALG_RSAPSS, ALG_ECDSA = 0x0014, 0x0016, 0x0018
|
|
24
|
+
DIGEST_SIZE = {ALG_SHA1: 20, ALG_SHA256: 32, ALG_SHA384: 48, ALG_SHA512: 64}
|
|
25
|
+
HASH = {ALG_SHA1: hashlib.sha1, ALG_SHA256: hashlib.sha256, ALG_SHA384: hashlib.sha384, ALG_SHA512: hashlib.sha512}
|
|
26
|
+
CRYPTO_HASH = {ALG_SHA256: hashes.SHA256, ALG_SHA384: hashes.SHA384, ALG_SHA512: hashes.SHA512}
|
|
27
|
+
EV_NO_ACTION = 0x00000003
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class TPMError(ValueError):
|
|
31
|
+
"""Malformed TPM structure or a check that fails."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class _R:
|
|
35
|
+
def __init__(self, b: bytes, little: bool = False):
|
|
36
|
+
self.b, self.o, self.order = b, 0, "little" if little else "big"
|
|
37
|
+
|
|
38
|
+
def take(self, n: int) -> bytes:
|
|
39
|
+
if self.o + n > len(self.b):
|
|
40
|
+
raise TPMError("structure truncated")
|
|
41
|
+
v = self.b[self.o:self.o + n]
|
|
42
|
+
self.o += n
|
|
43
|
+
return v
|
|
44
|
+
|
|
45
|
+
def u(self, n: int) -> int:
|
|
46
|
+
return int.from_bytes(self.take(n), self.order)
|
|
47
|
+
|
|
48
|
+
def tpm2b(self) -> bytes:
|
|
49
|
+
return self.take(self.u(2))
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass
|
|
53
|
+
class Quote:
|
|
54
|
+
signer: bytes
|
|
55
|
+
extra: bytes
|
|
56
|
+
clock: int
|
|
57
|
+
reset_count: int
|
|
58
|
+
restart_count: int
|
|
59
|
+
safe: int
|
|
60
|
+
firmware: int
|
|
61
|
+
selection: list # [(hash alg, [pcr indices])]
|
|
62
|
+
pcr_digest: bytes
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def parse_attest(msg: bytes) -> Quote:
|
|
66
|
+
r = _R(msg)
|
|
67
|
+
if r.u(4) != TPM_GENERATED:
|
|
68
|
+
raise TPMError("not TPM-generated (magic)")
|
|
69
|
+
if r.u(2) != ST_ATTEST_QUOTE:
|
|
70
|
+
raise TPMError("not a quote")
|
|
71
|
+
signer, extra = r.tpm2b(), r.tpm2b()
|
|
72
|
+
clock, reset, restart, safe = r.u(8), r.u(4), r.u(4), r.u(1)
|
|
73
|
+
fw = r.u(8)
|
|
74
|
+
sel = []
|
|
75
|
+
for _ in range(r.u(4)):
|
|
76
|
+
alg, size = r.u(2), r.u(1)
|
|
77
|
+
bits = r.take(size)
|
|
78
|
+
sel.append((alg, [i for i in range(size * 8) if bits[i // 8] >> (i % 8) & 1]))
|
|
79
|
+
digest = r.tpm2b()
|
|
80
|
+
if r.o != len(msg):
|
|
81
|
+
raise TPMError("trailing octets after the quote")
|
|
82
|
+
return Quote(signer, extra, clock, reset, restart, safe, fw, sel, digest)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def parse_signature(sig: bytes) -> tuple[int, int, bytes]:
|
|
86
|
+
"""(sigAlg, hash, signature octets) of an RSA TPMT_SIGNATURE."""
|
|
87
|
+
r = _R(sig)
|
|
88
|
+
alg, h = r.u(2), r.u(2)
|
|
89
|
+
if alg not in (ALG_RSASSA, ALG_RSAPSS):
|
|
90
|
+
raise TPMError(f"signature algorithm 0x{alg:04x} is not RSA")
|
|
91
|
+
s = r.tpm2b()
|
|
92
|
+
if r.o != len(sig):
|
|
93
|
+
raise TPMError("trailing octets after the signature")
|
|
94
|
+
return alg, h, s
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def verify_quote(msg: bytes, sig: bytes, rsa_public_key) -> None:
|
|
98
|
+
alg, h, s = parse_signature(sig)
|
|
99
|
+
if h not in CRYPTO_HASH:
|
|
100
|
+
raise TPMError(f"hash 0x{h:04x} not accepted")
|
|
101
|
+
pad = padding.PKCS1v15() if alg == ALG_RSASSA else padding.PSS(mgf=padding.MGF1(CRYPTO_HASH[h]()),
|
|
102
|
+
salt_length=padding.PSS.AUTO)
|
|
103
|
+
try:
|
|
104
|
+
rsa_public_key.verify(s, msg, pad, CRYPTO_HASH[h]())
|
|
105
|
+
except InvalidSignature as exc:
|
|
106
|
+
raise TPMError("quote signature does not verify") from exc
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def bank(alg: int) -> int:
|
|
110
|
+
"""The digest size of a PCR bank this module can replay and hash."""
|
|
111
|
+
if alg not in DIGEST_SIZE:
|
|
112
|
+
raise TPMError(f"PCR bank 0x{alg:04x} not supported")
|
|
113
|
+
return DIGEST_SIZE[alg]
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def pcr_digest(q: Quote, values: dict) -> bytes:
|
|
117
|
+
"""The digest a quote commits to: H(PCR values in selection order); ``values`` maps (alg, index) -> digest."""
|
|
118
|
+
if len(q.selection) != 1:
|
|
119
|
+
raise TPMError("one bank per quote expected")
|
|
120
|
+
alg, idx = q.selection[0]
|
|
121
|
+
bank(alg)
|
|
122
|
+
missing = [i for i in idx if (alg, i) not in values]
|
|
123
|
+
if missing:
|
|
124
|
+
raise TPMError(f"no value for PCRs {missing}")
|
|
125
|
+
return HASH[alg](b"".join(values[(alg, i)] for i in idx)).digest()
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def split_values(blob: bytes, q: Quote) -> dict:
|
|
129
|
+
"""tpm2_quote's "values" output (concatenated digests in selection order) as (alg, index) -> digest."""
|
|
130
|
+
out, off = {}, 0
|
|
131
|
+
for alg, idx in q.selection:
|
|
132
|
+
n = bank(alg)
|
|
133
|
+
for i in idx:
|
|
134
|
+
out[(alg, i)] = blob[off:off + n]
|
|
135
|
+
off += n
|
|
136
|
+
if off != len(blob):
|
|
137
|
+
raise TPMError("PCR values do not match the selection")
|
|
138
|
+
return out
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@dataclass
|
|
142
|
+
class Event:
|
|
143
|
+
pcr: int
|
|
144
|
+
type: int
|
|
145
|
+
digests: dict
|
|
146
|
+
data: bytes
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
@dataclass
|
|
150
|
+
class EventLog:
|
|
151
|
+
algs: dict # alg -> digest size (from the Spec ID event)
|
|
152
|
+
events: list = field(default_factory=list)
|
|
153
|
+
locality: int = 0
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def parse_event_log(blob: bytes) -> EventLog:
|
|
157
|
+
r = _R(blob, little=True)
|
|
158
|
+
r.u(4)
|
|
159
|
+
if r.u(4) != EV_NO_ACTION:
|
|
160
|
+
raise TPMError("first event is not the Spec ID event")
|
|
161
|
+
r.take(20)
|
|
162
|
+
spec = _R(r.take(r.u(4)), little=True)
|
|
163
|
+
if spec.take(16) != b"Spec ID Event03\x00":
|
|
164
|
+
raise TPMError("not a crypto-agile log")
|
|
165
|
+
spec.u(4), spec.take(3), spec.u(1)
|
|
166
|
+
algs = {}
|
|
167
|
+
for _ in range(spec.u(4)):
|
|
168
|
+
a, n = spec.u(2), spec.u(2)
|
|
169
|
+
if a in algs or n != DIGEST_SIZE.get(a, n):
|
|
170
|
+
raise TPMError(f"bank 0x{a:04x} announced twice or with a wrong digest size ({n})")
|
|
171
|
+
algs[a] = n
|
|
172
|
+
log = EventLog(algs=algs)
|
|
173
|
+
while r.o < len(blob):
|
|
174
|
+
pcr, etype = r.u(4), r.u(4)
|
|
175
|
+
digests = {}
|
|
176
|
+
for _ in range(r.u(4)):
|
|
177
|
+
a = r.u(2)
|
|
178
|
+
if a not in algs or a in digests:
|
|
179
|
+
raise TPMError(f"digest of algorithm 0x{a:04x} not announced, or given twice")
|
|
180
|
+
digests[a] = r.take(algs[a])
|
|
181
|
+
if digests.keys() != algs.keys(): # every event carries one digest of each announced bank
|
|
182
|
+
raise TPMError(f"event {len(log.events) + 1} lacks the digests of some announced banks")
|
|
183
|
+
data = r.take(r.u(4))
|
|
184
|
+
if etype == EV_NO_ACTION and data.startswith(b"StartupLocality\x00"):
|
|
185
|
+
if len(data) != 17:
|
|
186
|
+
raise TPMError("StartupLocality event is not 17 octets")
|
|
187
|
+
log.locality = data[16]
|
|
188
|
+
log.events.append(Event(pcr, etype, digests, data))
|
|
189
|
+
return log
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def replay(log: EventLog, alg: int = ALG_SHA256) -> dict:
|
|
193
|
+
"""PCR index -> value after replaying every measured event of the bank."""
|
|
194
|
+
if alg not in log.algs:
|
|
195
|
+
raise TPMError(f"bank 0x{alg:04x} is not in the log")
|
|
196
|
+
n = bank(alg)
|
|
197
|
+
pcrs: dict = {}
|
|
198
|
+
for e in log.events:
|
|
199
|
+
if e.type == EV_NO_ACTION:
|
|
200
|
+
continue
|
|
201
|
+
start = (bytes(n - 1) + bytes([log.locality])) if e.pcr == 0 else bytes(n)
|
|
202
|
+
pcrs[e.pcr] = HASH[alg](pcrs.get(e.pcr, start) + e.digests[alg]).digest()
|
|
203
|
+
return pcrs
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def parse_systemd_log(blob: bytes) -> list[tuple[int, bytes]]:
|
|
207
|
+
"""systemd's TPM2 measurement log (/run/log/systemd/tpm2-measure.log, JSON-SEQ, RFC 7464): (pcr, SHA-256 digest)
|
|
208
|
+
of every measurement systemd made after the firmware handed over (systemd-pcrphase into PCR 11, and others)."""
|
|
209
|
+
import json
|
|
210
|
+
out = []
|
|
211
|
+
for rec in blob.split(b"\x1e"):
|
|
212
|
+
rec = rec.strip()
|
|
213
|
+
if not rec:
|
|
214
|
+
continue
|
|
215
|
+
try:
|
|
216
|
+
r = json.loads(rec)
|
|
217
|
+
digests = r.get("digests", []) if isinstance(r, dict) else None
|
|
218
|
+
d = next((x["digest"] for x in digests if isinstance(x, dict) and x.get("hashAlg") == "sha256"), None) \
|
|
219
|
+
if isinstance(digests, list) else None
|
|
220
|
+
pcr = r.get("pcr") if isinstance(r, dict) else None
|
|
221
|
+
if d is None or type(pcr) is not int or not 0 <= pcr < 24:
|
|
222
|
+
raise TPMError("systemd log record without a PCR or a SHA-256 digest")
|
|
223
|
+
digest = bytes.fromhex(d)
|
|
224
|
+
if len(digest) != 32:
|
|
225
|
+
raise TPMError("systemd log record's SHA-256 digest is not 32 octets")
|
|
226
|
+
out.append((pcr, digest))
|
|
227
|
+
except (ValueError, TypeError, KeyError) as exc:
|
|
228
|
+
raise TPMError(f"malformed systemd log record ({type(exc).__name__})") from exc
|
|
229
|
+
return out
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def replay_with(log: EventLog, later: list[tuple[int, bytes]], alg: int = ALG_SHA256) -> dict:
|
|
233
|
+
"""The firmware log's replay, then the measurements made after boot, in order."""
|
|
234
|
+
pcrs = replay(log, alg)
|
|
235
|
+
n = bank(alg)
|
|
236
|
+
for pcr, digest in later:
|
|
237
|
+
pcrs[pcr] = HASH[alg](pcrs.get(pcr, bytes(n)) + digest).digest()
|
|
238
|
+
return pcrs
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def uki_sections(log: EventLog) -> dict:
|
|
242
|
+
"""systemd-stub's PCR 11 measurements of a unified kernel image: section name -> SHA-256 of its contents (the
|
|
243
|
+
stub measures each section's name, then its contents, as EV_IPL events)."""
|
|
244
|
+
if ALG_SHA256 not in log.algs:
|
|
245
|
+
raise TPMError("the log has no SHA-256 bank")
|
|
246
|
+
out, name = {}, None
|
|
247
|
+
for e in log.events:
|
|
248
|
+
if e.pcr != 11 or e.type != 0x0D:
|
|
249
|
+
continue
|
|
250
|
+
try:
|
|
251
|
+
text = e.data.decode("utf-16-le").rstrip("\x00")
|
|
252
|
+
except UnicodeDecodeError:
|
|
253
|
+
text = ""
|
|
254
|
+
if name is None:
|
|
255
|
+
name = text
|
|
256
|
+
else:
|
|
257
|
+
out[name] = e.digests[ALG_SHA256]
|
|
258
|
+
name = None
|
|
259
|
+
return out
|