fix(ANSModelPackage): seal model packages with CSPRNG keys and nonces, not a seed

pack() derived the content key, the ephemeral X25519 private key and every
nonce base from an 8-bit nonceSeed. Anyone holding a package could try the
256 seeds and recover its content key without the base private key.

The content key, ephemeral key, wrap nonce and nonce bases now come from
the OS CSPRNG (BCryptGenRandom / getrandom / arc4random_buf). Nonce bases
are redrawn until distinct within a package. The seeded path survives only
as insecureDeterministicForTests + testSeed, honoured only when built with
ANSMODEL_DETERMINISTIC_PACK_FOR_TESTS; otherwise pack() refuses. The
on-disk format is unchanged. Same change in ANSTSCORE, ANSMINDCORE and
ANSCORE, which keep pack() in step.

Regression test: two packs of identical inputs have different content
keys, ephemeral keys, wrap nonces and ciphertext, neither key is one a
seed could make, and both still open with the base key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-26 18:15:13 +10:00
co-authored by Claude Opus 5.5
parent cb964907f0
commit c1c26c400b
4 changed files with 215 additions and 20 deletions
@@ -162,7 +162,6 @@ static void testRoundTripAndRefusals(const fs::path& dir) {
req.useX25519 = true;
req.basePublic = id.basePub;
req.chunk = 2048;
req.nonceSeed = 9;
req.signerId = "anssig-2026-09";
req.signerSecret = id.signerSk;
req.signerPublic = id.signerPk;
@@ -252,6 +251,79 @@ static void testRoundTripAndRefusals(const fs::path& dir) {
// ANSODBase asks these three questions and nothing else: is this path a
// package, can this build open one, and what came out of it. The engines
// themselves need CUDA to compile, so this is where that contract is checked.
// ── every package gets its own key ────────────────────────────────────────
// pack() used to derive the content key, the ephemeral key and the nonces from
// one byte of seed, and the training box passed a seed made of the project's
// name and version: anyone holding a package could try the 256 seeds and read
// it without the base key. Two packs of the very same inputs must now share
// nothing that seals them — and still open with the base key.
static void testEachPackIsSealedAfresh(const fs::path& dir) {
const Identity id = fixtureIdentity();
const modelpkg::Keyring ring = fixtureKeyring(id);
modelpkg::PackRequest req;
req.keyId = "ansk-v1-base";
req.useX25519 = true;
req.basePublic = id.basePub;
req.chunk = 2048;
req.manifest = Json{{"task", "detection"},
{"family", "yolo"},
{"runtime", "onnx"},
{"models", Json::array({Json{{"id", "m0"},
{"payload", "p0"},
{"entry", true}}})}};
const auto model = fakeModel(5000, 11);
req.models.push_back({"p0", model});
req.models.push_back({"p1", model}); // two parts under one key: bases differ
const std::string pa = (dir / "fresh-a.ansmodel").string();
const std::string pb = (dir / "fresh-b.ansmodel").string();
std::string err;
CHECK(modelpkg::pack(pa, req, err), ("pack a: " + err).c_str());
CHECK(modelpkg::pack(pb, req, err), ("pack b: " + err).c_str());
modelpkg::Package a, b;
CHECK(modelpkg::open(pa, ring, a, err) && modelpkg::open(pb, ring, b, err),
("both open with the base key: " + err).c_str());
CHECK(a.contentKey != b.contentKey,
"two packs of identical inputs have different content keys");
bool seeded = false;
for (int seed = 0; seed < 256; ++seed) {
modelpkg::Key guess{};
for (size_t i = 0; i < guess.size(); ++i)
guess[i] = uint8_t(seed * 31 + i * 7 + 11);
seeded = seeded || guess == a.contentKey || guess == b.contentKey;
}
CHECK(!seeded, "and neither is one of the 256 a seed could have made");
const Json& wa = a.header["wraps"][0];
const Json& wb = b.header["wraps"][0];
CHECK(wa.value("epk", std::string()) != wb.value("epk", std::string()),
"each has its own ephemeral key");
CHECK(wa.value("nonce", std::string()) != wb.value("nonce", std::string()) &&
wa.value("ct", std::string()) != wb.value("ct", std::string()),
"and its own wrap nonce and wrapped key");
CHECK(a.header["payloads"][0].value("sha256", std::string()) !=
b.header["payloads"][0].value("sha256", std::string()),
"so the same model seals to different ciphertext");
CHECK(a.header["payloads"][0].value("nonceBase", std::string()) !=
a.header["payloads"][1].value("nonceBase", std::string()) &&
a.header["payloads"][0].value("nonceBase", std::string()) !=
a.header["manifest"].value("nonceBase", std::string()),
"and no two parts of one package share a nonce base");
std::vector<uint8_t> ga, gb;
CHECK(modelpkg::readPayload(a, "p0", ga, err) && ga == model &&
modelpkg::readPayload(b, "p1", gb, err) && gb == model,
"and both still give back the model that went in");
// The seeded path is for tests built as such; this one is not.
req.insecureDeterministicForTests = true;
CHECK(!modelpkg::pack((dir / "seeded.ansmodel").string(), req, err) &&
err.find("tests only") != std::string::npos,
"a build without the test switch refuses to pack from a seed");
}
static void testOpenForAnEngine(const fs::path& dataDir) {
const std::string path = (dataDir / "interop.ansmodel").string();
if (!fs::exists(path)) {
@@ -376,7 +448,6 @@ static void testShapeFromTheGraph(const fs::path& dir) {
req.keyId = "ansk-v1-base";
req.useX25519 = true;
req.basePublic = id.basePub;
req.nonceSeed = 21;
req.signerId = "anssig-2026-09";
req.signerSecret = id.signerSk;
req.signerPublic = id.signerPk;
@@ -472,6 +543,7 @@ int main(int argc, char** argv) {
testVectors();
testInterop(dataDir);
testRoundTripAndRefusals(tmp);
testEachPackIsSealedAfresh(tmp);
testOpenForAnEngine(dataDir);
testShapeFromTheGraph(tmp);