diff --git a/core/ANSModelPackage/ANSCsprng.h b/core/ANSModelPackage/ANSCsprng.h new file mode 100644 index 0000000..895e6b0 --- /dev/null +++ b/core/ANSModelPackage/ANSCsprng.h @@ -0,0 +1,74 @@ +#ifndef ANSCSPRNG_H +#define ANSCSPRNG_H +#pragma once +// ANSCsprng — random bytes from the operating system, for keys, salts and +// nonces. +// +// TAKEN FROM ANSMINDCORE (src/core/csprng.hpp). NOT std::random_device or a +// seeded generator: fine for an id that only needs to be distinct, and wrong +// for anything a guesser must not predict. The OS generator is the only +// source used for key material here. +// +// Windows BCryptGenRandom with the system-preferred RNG (bcrypt.lib) +// Linux getrandom(2), looping over short reads and EINTR +// macOS arc4random_buf, which cannot fail +// +// Returns false when the OS refused — callers treat that as "cannot seal", +// never as "seal with whatever was in the buffer". + +#include +#include + +#if defined(_WIN32) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#include +#if defined(_MSC_VER) +#pragma comment(lib, "bcrypt.lib") +#endif +#elif defined(__APPLE__) +#include +#else +#include +#include +#endif + +namespace ANSCENTER { + +inline bool randomBytes(uint8_t* out, size_t n) { + if (n == 0) return true; + if (!out) return false; +#if defined(_WIN32) + while (n > 0) { + const ULONG chunk = n > 0x7fffffffu ? 0x7fffffffu : ULONG(n); + if (BCryptGenRandom(nullptr, out, chunk, BCRYPT_USE_SYSTEM_PREFERRED_RNG) != 0) + return false; + out += chunk; + n -= chunk; + } + return true; +#elif defined(__APPLE__) + arc4random_buf(out, n); + return true; +#else + while (n > 0) { + const ssize_t got = getrandom(out, n, 0); + if (got < 0) { + if (errno == EINTR) continue; + return false; + } + out += size_t(got); + n -= size_t(got); + } + return true; +#endif +} + +} // namespace ANSCENTER + +#endif // ANSCSPRNG_H diff --git a/core/ANSModelPackage/ANSModelPackage.h b/core/ANSModelPackage/ANSModelPackage.h index 4435451..1811e14 100644 --- a/core/ANSModelPackage/ANSModelPackage.h +++ b/core/ANSModelPackage/ANSModelPackage.h @@ -75,6 +75,7 @@ #include #include "ANSAead.h" +#include "ANSCsprng.h" #include "ANSPackageBits.h" extern "C" { @@ -578,9 +579,15 @@ struct PackRequest { Json card; // optional, counts only std::vector models; uint32_t chunk = kDefaultChunk; // smaller in tests, to exercise chunking - // Deterministic nonces are for TESTS ONLY: a real package draws them from - // the platform CSPRNG, because a repeated (key, nonce) pair is fatal. - uint8_t nonceSeed = 0; + // TESTS ONLY. pack() draws the content key, the ephemeral X25519 key and + // every nonce from the operating system's CSPRNG. Setting this derives them + // from `testSeed` instead, so a fixture can be written byte-for-byte again — + // and so anyone holding the package can recover its content key by trying + // the 256 seeds, without the base key. It is honoured only in a build that + // defines ANSMODEL_DETERMINISTIC_PACK_FOR_TESTS; anywhere else pack() refuses + // rather than write a package that only looks sealed. + bool insecureDeterministicForTests = false; + uint8_t testSeed = 0; }; inline bool pack(const std::string& path, const PackRequest& req, @@ -589,11 +596,58 @@ inline bool pack(const std::string& path, const PackRequest& req, err = "a package with no model describes nothing"; return false; } - // A content key per package. Tests pass a seed; a real packer must use the - // platform CSPRNG here. + // Everything the package's secrecy rests on, drawn up front: the content + // key, the ephemeral private key it is wrapped with, the wrap nonce and one + // nonce base per sealed part. From the OS CSPRNG — anything a holder of the + // file could re-derive would open it without the base key. Key contentKey{}; - for (size_t i = 0; i < contentKey.size(); ++i) - contentKey[i] = uint8_t(req.nonceSeed * 31 + i * 7 + 11); + Key esk{}; + std::array wrapNonce{}; + std::array manifestBase{}; + std::vector> bases(req.models.size()); + struct Wipe { + Key& a; + Key& b; + ~Wipe() { + crypto_wipe(a.data(), a.size()); + crypto_wipe(b.data(), b.size()); + } + } wipe{contentKey, esk}; + if (req.insecureDeterministicForTests) { +#if defined(ANSMODEL_DETERMINISTIC_PACK_FOR_TESTS) + for (size_t i = 0; i < contentKey.size(); ++i) + contentKey[i] = uint8_t(req.testSeed * 31 + i * 7 + 11); + for (size_t i = 0; i < esk.size(); ++i) + esk[i] = uint8_t(req.testSeed * 13 + i * 5 + 3); + for (size_t i = 0; i < wrapNonce.size(); ++i) + wrapNonce[i] = uint8_t(req.testSeed + 100 + i); + manifestBase = {uint8_t(req.testSeed + 1), 2, 3, 4}; + for (size_t i = 0; i < bases.size(); ++i) + bases[i] = {uint8_t(req.testSeed + 10 + i), 5, 6, 7}; +#else + err = "deterministic packing is for tests only, and this build is not one"; + return false; +#endif + } else { + bool ok = ANSCENTER::randomBytes(contentKey.data(), contentKey.size()) && + ANSCENTER::randomBytes(esk.data(), esk.size()) && + ANSCENTER::randomBytes(wrapNonce.data(), wrapNonce.size()) && + ANSCENTER::randomBytes(manifestBase.data(), manifestBase.size()); + // The parts share the content key, so their bases must differ: two parts + // on one base would reuse every chunk nonce they have in common. + for (size_t i = 0; ok && i < bases.size(); ++i) { + bool clash = true; + while (ok && clash) { + ok = ANSCENTER::randomBytes(bases[i].data(), bases[i].size()); + clash = bases[i] == manifestBase; + for (size_t j = 0; j < i; ++j) clash = clash || bases[i] == bases[j]; + } + } + if (!ok) { + err = "the operating system gave no random bytes, so nothing was sealed"; + return false; + } + } auto sealChunked = [&](const std::vector& plain, const std::string& payloadId, @@ -616,7 +670,6 @@ inline bool pack(const std::string& path, const PackRequest& req, const std::string manifestText = req.manifest.dump(); const std::vector manifestBytes(manifestText.begin(), manifestText.end()); - std::array manifestBase{uint8_t(req.nonceSeed + 1), 2, 3, 4}; std::string manifestBlob; { const auto sealed = @@ -628,20 +681,14 @@ inline bool pack(const std::string& path, const PackRequest& req, } std::vector blobs; - std::vector> bases; for (size_t i = 0; i < req.models.size(); ++i) { - std::array base{uint8_t(req.nonceSeed + 10 + i), 5, 6, 7}; std::string blob; - sealChunked(req.models[i].bytes, req.models[i].id, base, blob); + sealChunked(req.models[i].bytes, req.models[i].id, bases[i], blob); blobs.push_back(std::move(blob)); - bases.push_back(base); } // Wrap the content key to the base key. One wrap here; the header's shape // takes several, which is how a package opens on more than one key. - std::array wrapNonce{}; - for (size_t i = 0; i < wrapNonce.size(); ++i) - wrapNonce[i] = uint8_t(req.nonceSeed + 100 + i); const std::vector keyPlain(contentKey.begin(), contentKey.end()); Key wrapKey = req.key; @@ -650,9 +697,6 @@ inline bool pack(const std::string& path, const PackRequest& req, // An ephemeral key pair per package: its public half travels in the // header, its private half dies here, and the packer never holds anything // that opens the result. - Key esk{}; - for (size_t i = 0; i < esk.size(); ++i) - esk[i] = uint8_t(req.nonceSeed * 13 + i * 5 + 3); epk.assign(32, 0); crypto_x25519_public_key(epk.data(), esk.data()); uint8_t shared[32]; diff --git a/core/ANSModelPackage/CMakeLists.txt b/core/ANSModelPackage/CMakeLists.txt index 0aaac8f..13e459e 100644 --- a/core/ANSModelPackage/CMakeLists.txt +++ b/core/ANSModelPackage/CMakeLists.txt @@ -40,4 +40,9 @@ else() "(run ansmodel-keys emit --product anscore, then re-run cmake)") endif() +# BCryptGenRandom, behind the content key and nonces pack() draws (ANSCsprng.h). +if(WIN32) + target_link_libraries(ANSModelPackage PUBLIC bcrypt) +endif() + set_target_properties(ANSModelPackage PROPERTIES POSITION_INDEPENDENT_CODE ON) diff --git a/tests/ANSModelPackage-UnitTest/ANSModelPackage-UnitTest.cpp b/tests/ANSModelPackage-UnitTest/ANSModelPackage-UnitTest.cpp index 225dfb4..ff46d2d 100644 --- a/tests/ANSModelPackage-UnitTest/ANSModelPackage-UnitTest.cpp +++ b/tests/ANSModelPackage-UnitTest/ANSModelPackage-UnitTest.cpp @@ -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 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);