feat(ANSModelPackage): read the ANS model package here too
The second reader. A package is one encrypted file — cleartext header, sealed manifest, model bytes sealed in chunks — and this opens it: the content key unwrapped with X25519 against the base key, the header verified as Ed25519 signed by a release key the root vouches for, the model decrypted into memory and handed to ANSModelSource, which the engines already take. It is a new library rather than code inside the engine layer, and deliberately free of the heavy dependencies around it: no CUDA, no OpenCV, no LabVIEW. That is what lets it build and be tested on every platform, including the ones where the engine layer does not build. THE TWO IMPLEMENTATIONS MUST AGREE, so this does not merely claim to: tests/ANSModelPackage-UnitTest/data/interop.ansmodel was written by ANSMINDCORE's packer, and the test opens it here — manifest, classes and a chunked payload, byte for byte. A change to either wire format shows up as this failing. The fixture's keys are derived by published rules, recorded beside it, so anyone can rebuild it. The AEAD and the small primitives are copies of ANSMINDCORE's, not reimplementations, for the same reason. Monocypher 4.0.2 is vendored here as it is there, including the RFC 8032 pair — its default EdDSA signs with BLAKE2b and would not interoperate. Verified by compiling and running the test directly: 19 checks, RFC 8439, RFC 7748 and RFC 8032 vectors, the interop fixture, a round trip, a flipped bit, an unknown root, a revoked signer, and rubbish bytes. ANSCORE's own CMake configure fails on this machine for reasons that predate this change — eight errors with or without it, all missing dependencies — so the new CMake targets still need a real build box. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Vendored
+167
@@ -0,0 +1,167 @@
|
|||||||
|
Monocypher as a whole is dual-licensed. Choose whichever licence you
|
||||||
|
want from the two licences listed below.
|
||||||
|
|
||||||
|
The first licence is a regular 2-clause BSD licence. The second licence
|
||||||
|
is the CC-0 from Creative Commons. It is intended to release Monocypher
|
||||||
|
to the public domain. The BSD licence serves as a fallback option.
|
||||||
|
|
||||||
|
See the individual files for specific information about who contributed
|
||||||
|
to what file during which years. See below for special notes.
|
||||||
|
|
||||||
|
Licence 1 (2-clause BSD)
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
Copyright (c) 2017-2023, Loup Vaillant
|
||||||
|
Copyright (c) 2017-2019, Michael Savage
|
||||||
|
Copyright (c) 2017-2023, Fabio Scotoni
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are
|
||||||
|
met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer in the
|
||||||
|
documentation and/or other materials provided with the
|
||||||
|
distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
|
||||||
|
Licence 2 (CC-0)
|
||||||
|
----------------
|
||||||
|
|
||||||
|
> CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
||||||
|
> LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
||||||
|
> ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
||||||
|
> INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
||||||
|
> REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
||||||
|
> PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
||||||
|
> THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
||||||
|
> HEREUNDER.
|
||||||
|
|
||||||
|
### Statement of Purpose
|
||||||
|
|
||||||
|
The laws of most jurisdictions throughout the world automatically confer
|
||||||
|
exclusive Copyright and Related Rights (defined below) upon the creator
|
||||||
|
and subsequent owner(s) (each and all, an "owner") of an original work
|
||||||
|
of authorship and/or a database (each, a "Work").
|
||||||
|
|
||||||
|
Certain owners wish to permanently relinquish those rights to a Work for
|
||||||
|
the purpose of contributing to a commons of creative, cultural and
|
||||||
|
scientific works ("Commons") that the public can reliably and without
|
||||||
|
fear of later claims of infringement build upon, modify, incorporate in
|
||||||
|
other works, reuse and redistribute as freely as possible in any form
|
||||||
|
whatsoever and for any purposes, including without limitation commercial
|
||||||
|
purposes. These owners may contribute to the Commons to promote the
|
||||||
|
ideal of a free culture and the further production of creative, cultural
|
||||||
|
and scientific works, or to gain reputation or greater distribution for
|
||||||
|
their Work in part through the use and efforts of others.
|
||||||
|
|
||||||
|
For these and/or other purposes and motivations, and without any
|
||||||
|
expectation of additional consideration or compensation, the person
|
||||||
|
associating CC0 with a Work (the "Affirmer"), to the extent that he or
|
||||||
|
she is an owner of Copyright and Related Rights in the Work, voluntarily
|
||||||
|
elects to apply CC0 to the Work and publicly distribute the Work under
|
||||||
|
its terms, with knowledge of his or her Copyright and Related Rights in
|
||||||
|
the Work and the meaning and intended legal effect of CC0 on those
|
||||||
|
rights.
|
||||||
|
|
||||||
|
1. **Copyright and Related Rights.** A Work made available under CC0 may
|
||||||
|
be protected by copyright and related or neighboring rights
|
||||||
|
("Copyright and Related Rights"). Copyright and Related Rights
|
||||||
|
include, but are not limited to, the following:
|
||||||
|
|
||||||
|
- the right to reproduce, adapt, distribute, perform, display,
|
||||||
|
communicate, and translate a Work;
|
||||||
|
- moral rights retained by the original author(s) and/or
|
||||||
|
performer(s); publicity and privacy rights pertaining to a person's
|
||||||
|
image or likeness depicted in a Work;
|
||||||
|
- rights protecting against unfair competition in regards to a Work,
|
||||||
|
subject to the limitations in paragraph 4(a), below;
|
||||||
|
- rights protecting the extraction, dissemination, use and reuse of
|
||||||
|
data in a Work;
|
||||||
|
- database rights (such as those arising under Directive 96/9/EC of
|
||||||
|
the European Parliament and of the Council of 11 March 1996 on the
|
||||||
|
legal protection of databases, and under any national
|
||||||
|
implementation thereof, including any amended or successor version
|
||||||
|
of such directive); and
|
||||||
|
- other similar, equivalent or corresponding rights throughout the
|
||||||
|
world based on applicable law or treaty, and any national
|
||||||
|
implementations thereof.
|
||||||
|
|
||||||
|
2. **Waiver.** To the greatest extent permitted by, but not in
|
||||||
|
contravention of, applicable law, Affirmer hereby overtly, fully,
|
||||||
|
permanently, irrevocably and unconditionally waives, abandons, and
|
||||||
|
surrenders all of Affirmer's Copyright and Related Rights and
|
||||||
|
associated claims and causes of action, whether now known or unknown
|
||||||
|
(including existing as well as future claims and causes of action),
|
||||||
|
in the Work (i) in all territories worldwide, (ii) for the maximum
|
||||||
|
duration provided by applicable law or treaty (including future time
|
||||||
|
extensions), (iii) in any current or future medium and for any number
|
||||||
|
of copies, and (iv) for any purpose whatsoever, including without
|
||||||
|
limitation commercial, advertising or promotional purposes (the
|
||||||
|
"Waiver"). Affirmer makes the Waiver for the benefit of each member
|
||||||
|
of the public at large and to the detriment of Affirmer's heirs and
|
||||||
|
successors, fully intending that such Waiver shall not be subject to
|
||||||
|
revocation, rescission, cancellation, termination, or any other legal
|
||||||
|
or equitable action to disrupt the quiet enjoyment of the Work by the
|
||||||
|
public as contemplated by Affirmer's express Statement of Purpose.
|
||||||
|
|
||||||
|
3. **Public License Fallback.** Should any part of the Waiver for any
|
||||||
|
reason be judged legally invalid or ineffective under applicable law,
|
||||||
|
then the Waiver shall be preserved to the maximum extent permitted
|
||||||
|
taking into account Affirmer's express Statement of Purpose. In
|
||||||
|
addition, to the extent the Waiver is so judged Affirmer hereby
|
||||||
|
grants to each affected person a royalty-free, non transferable, non
|
||||||
|
sublicensable, non exclusive, irrevocable and unconditional license
|
||||||
|
to exercise Affirmer's Copyright and Related Rights in the Work (i)
|
||||||
|
in all territories worldwide, (ii) for the maximum duration provided
|
||||||
|
by applicable law or treaty (including future time extensions), (iii)
|
||||||
|
in any current or future medium and for any number of copies, and
|
||||||
|
(iv) for any purpose whatsoever, including without limitation
|
||||||
|
commercial, advertising or promotional purposes (the "License"). The
|
||||||
|
License shall be deemed effective as of the date CC0 was applied by
|
||||||
|
Affirmer to the Work. Should any part of the License for any reason
|
||||||
|
be judged legally invalid or ineffective under applicable law, such
|
||||||
|
partial invalidity or ineffectiveness shall not invalidate the
|
||||||
|
remainder of the License, and in such case Affirmer hereby affirms
|
||||||
|
that he or she will not (i) exercise any of his or her remaining
|
||||||
|
Copyright and Related Rights in the Work or (ii) assert any
|
||||||
|
associated claims and causes of action with respect to the Work, in
|
||||||
|
either case contrary to Affirmer's express Statement of Purpose.
|
||||||
|
|
||||||
|
4. **Limitations and Disclaimers.**
|
||||||
|
|
||||||
|
- No trademark or patent rights held by Affirmer are waived,
|
||||||
|
abandoned, surrendered, licensed or otherwise affected by this
|
||||||
|
document.
|
||||||
|
- Affirmer offers the Work as-is and makes no representations or
|
||||||
|
warranties of any kind concerning the Work, express, implied,
|
||||||
|
statutory or otherwise, including without limitation warranties of
|
||||||
|
title, merchantability, fitness for a particular purpose, non
|
||||||
|
infringement, or the absence of latent or other defects, accuracy,
|
||||||
|
or the present or absence of errors, whether or not discoverable,
|
||||||
|
all to the greatest extent permissible under applicable law.
|
||||||
|
- Affirmer disclaims responsibility for clearing rights of other
|
||||||
|
persons that may apply to the Work or any use thereof, including
|
||||||
|
without limitation any person's Copyright and Related Rights in the
|
||||||
|
Work. Further, Affirmer disclaims responsibility for obtaining any
|
||||||
|
necessary consents, permissions or other rights required for any
|
||||||
|
use of the Work.
|
||||||
|
- Affirmer understands and acknowledges that Creative Commons is not
|
||||||
|
a party to this document and has no duty or obligation with respect
|
||||||
|
to this CC0 or use of the Work.
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
# Monocypher 4.0.2 — vendored
|
||||||
|
|
||||||
|
Source: https://monocypher.org/download/monocypher-4.0.2.tar.gz
|
||||||
|
Files: `monocypher.c/.h`, `monocypher-ed25519.c/.h`, `LICENCE.md`. Unmodified.
|
||||||
|
Licence: dual 2-clause BSD / CC-0.
|
||||||
|
|
||||||
|
Here for the model package format (`core/ANSModelPackage`): X25519 to unwrap a
|
||||||
|
package's content key, and RFC 8032 Ed25519 to prove where the package came
|
||||||
|
from. The `-ed25519` pair is not optional — Monocypher's own EdDSA signs with
|
||||||
|
BLAKE2b, which would not interoperate with a package signed anywhere else.
|
||||||
|
|
||||||
|
ANSMINDCORE vendors the same release for the same reason. The two readers must
|
||||||
|
stay byte-compatible, so update them together and run both suites.
|
||||||
+500
@@ -0,0 +1,500 @@
|
|||||||
|
// Monocypher version 4.0.2
|
||||||
|
//
|
||||||
|
// This file is dual-licensed. Choose whichever licence you want from
|
||||||
|
// the two licences listed below.
|
||||||
|
//
|
||||||
|
// The first licence is a regular 2-clause BSD licence. The second licence
|
||||||
|
// is the CC-0 from Creative Commons. It is intended to release Monocypher
|
||||||
|
// to the public domain. The BSD licence serves as a fallback option.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0
|
||||||
|
//
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Copyright (c) 2017-2019, Loup Vaillant
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without
|
||||||
|
// modification, are permitted provided that the following conditions are
|
||||||
|
// met:
|
||||||
|
//
|
||||||
|
// 1. Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
//
|
||||||
|
// 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer in the
|
||||||
|
// documentation and/or other materials provided with the
|
||||||
|
// distribution.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
//
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Written in 2017-2019 by Loup Vaillant
|
||||||
|
//
|
||||||
|
// To the extent possible under law, the author(s) have dedicated all copyright
|
||||||
|
// and related neighboring rights to this software to the public domain
|
||||||
|
// worldwide. This software is distributed without any warranty.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the CC0 Public Domain Dedication along
|
||||||
|
// with this software. If not, see
|
||||||
|
// <https://creativecommons.org/publicdomain/zero/1.0/>
|
||||||
|
|
||||||
|
#include "monocypher-ed25519.h"
|
||||||
|
|
||||||
|
#ifdef MONOCYPHER_CPP_NAMESPACE
|
||||||
|
namespace MONOCYPHER_CPP_NAMESPACE {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/////////////////
|
||||||
|
/// Utilities ///
|
||||||
|
/////////////////
|
||||||
|
#define FOR(i, min, max) for (size_t i = min; i < max; i++)
|
||||||
|
#define COPY(dst, src, size) FOR(_i_, 0, size) (dst)[_i_] = (src)[_i_]
|
||||||
|
#define ZERO(buf, size) FOR(_i_, 0, size) (buf)[_i_] = 0
|
||||||
|
#define WIPE_CTX(ctx) crypto_wipe(ctx , sizeof(*(ctx)))
|
||||||
|
#define WIPE_BUFFER(buffer) crypto_wipe(buffer, sizeof(buffer))
|
||||||
|
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
|
||||||
|
typedef uint8_t u8;
|
||||||
|
typedef uint64_t u64;
|
||||||
|
|
||||||
|
// Returns the smallest positive integer y such that
|
||||||
|
// (x + y) % pow_2 == 0
|
||||||
|
// Basically, it's how many bytes we need to add to "align" x.
|
||||||
|
// Only works when pow_2 is a power of 2.
|
||||||
|
// Note: we use ~x+1 instead of -x to avoid compiler warnings
|
||||||
|
static size_t align(size_t x, size_t pow_2)
|
||||||
|
{
|
||||||
|
return (~x + 1) & (pow_2 - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static u64 load64_be(const u8 s[8])
|
||||||
|
{
|
||||||
|
return((u64)s[0] << 56)
|
||||||
|
| ((u64)s[1] << 48)
|
||||||
|
| ((u64)s[2] << 40)
|
||||||
|
| ((u64)s[3] << 32)
|
||||||
|
| ((u64)s[4] << 24)
|
||||||
|
| ((u64)s[5] << 16)
|
||||||
|
| ((u64)s[6] << 8)
|
||||||
|
| (u64)s[7];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void store64_be(u8 out[8], u64 in)
|
||||||
|
{
|
||||||
|
out[0] = (in >> 56) & 0xff;
|
||||||
|
out[1] = (in >> 48) & 0xff;
|
||||||
|
out[2] = (in >> 40) & 0xff;
|
||||||
|
out[3] = (in >> 32) & 0xff;
|
||||||
|
out[4] = (in >> 24) & 0xff;
|
||||||
|
out[5] = (in >> 16) & 0xff;
|
||||||
|
out[6] = (in >> 8) & 0xff;
|
||||||
|
out[7] = in & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void load64_be_buf (u64 *dst, const u8 *src, size_t size) {
|
||||||
|
FOR(i, 0, size) { dst[i] = load64_be(src + i*8); }
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
/// SHA 512 ///
|
||||||
|
///////////////
|
||||||
|
static u64 rot(u64 x, int c ) { return (x >> c) | (x << (64 - c)); }
|
||||||
|
static u64 ch (u64 x, u64 y, u64 z) { return (x & y) ^ (~x & z); }
|
||||||
|
static u64 maj(u64 x, u64 y, u64 z) { return (x & y) ^ ( x & z) ^ (y & z); }
|
||||||
|
static u64 big_sigma0(u64 x) { return rot(x, 28) ^ rot(x, 34) ^ rot(x, 39); }
|
||||||
|
static u64 big_sigma1(u64 x) { return rot(x, 14) ^ rot(x, 18) ^ rot(x, 41); }
|
||||||
|
static u64 lit_sigma0(u64 x) { return rot(x, 1) ^ rot(x, 8) ^ (x >> 7); }
|
||||||
|
static u64 lit_sigma1(u64 x) { return rot(x, 19) ^ rot(x, 61) ^ (x >> 6); }
|
||||||
|
|
||||||
|
static const u64 K[80] = {
|
||||||
|
0x428a2f98d728ae22,0x7137449123ef65cd,0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc,
|
||||||
|
0x3956c25bf348b538,0x59f111f1b605d019,0x923f82a4af194f9b,0xab1c5ed5da6d8118,
|
||||||
|
0xd807aa98a3030242,0x12835b0145706fbe,0x243185be4ee4b28c,0x550c7dc3d5ffb4e2,
|
||||||
|
0x72be5d74f27b896f,0x80deb1fe3b1696b1,0x9bdc06a725c71235,0xc19bf174cf692694,
|
||||||
|
0xe49b69c19ef14ad2,0xefbe4786384f25e3,0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65,
|
||||||
|
0x2de92c6f592b0275,0x4a7484aa6ea6e483,0x5cb0a9dcbd41fbd4,0x76f988da831153b5,
|
||||||
|
0x983e5152ee66dfab,0xa831c66d2db43210,0xb00327c898fb213f,0xbf597fc7beef0ee4,
|
||||||
|
0xc6e00bf33da88fc2,0xd5a79147930aa725,0x06ca6351e003826f,0x142929670a0e6e70,
|
||||||
|
0x27b70a8546d22ffc,0x2e1b21385c26c926,0x4d2c6dfc5ac42aed,0x53380d139d95b3df,
|
||||||
|
0x650a73548baf63de,0x766a0abb3c77b2a8,0x81c2c92e47edaee6,0x92722c851482353b,
|
||||||
|
0xa2bfe8a14cf10364,0xa81a664bbc423001,0xc24b8b70d0f89791,0xc76c51a30654be30,
|
||||||
|
0xd192e819d6ef5218,0xd69906245565a910,0xf40e35855771202a,0x106aa07032bbd1b8,
|
||||||
|
0x19a4c116b8d2d0c8,0x1e376c085141ab53,0x2748774cdf8eeb99,0x34b0bcb5e19b48a8,
|
||||||
|
0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb,0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3,
|
||||||
|
0x748f82ee5defb2fc,0x78a5636f43172f60,0x84c87814a1f0ab72,0x8cc702081a6439ec,
|
||||||
|
0x90befffa23631e28,0xa4506cebde82bde9,0xbef9a3f7b2c67915,0xc67178f2e372532b,
|
||||||
|
0xca273eceea26619c,0xd186b8c721c0c207,0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178,
|
||||||
|
0x06f067aa72176fba,0x0a637dc5a2c898a6,0x113f9804bef90dae,0x1b710b35131c471b,
|
||||||
|
0x28db77f523047d84,0x32caab7b40c72493,0x3c9ebe0a15c9bebc,0x431d67c49c100d4c,
|
||||||
|
0x4cc5d4becb3e42b6,0x597f299cfc657e2a,0x5fcb6fab3ad6faec,0x6c44198c4a475817
|
||||||
|
};
|
||||||
|
|
||||||
|
static void sha512_compress(crypto_sha512_ctx *ctx)
|
||||||
|
{
|
||||||
|
u64 a = ctx->hash[0]; u64 b = ctx->hash[1];
|
||||||
|
u64 c = ctx->hash[2]; u64 d = ctx->hash[3];
|
||||||
|
u64 e = ctx->hash[4]; u64 f = ctx->hash[5];
|
||||||
|
u64 g = ctx->hash[6]; u64 h = ctx->hash[7];
|
||||||
|
|
||||||
|
FOR (j, 0, 16) {
|
||||||
|
u64 in = K[j] + ctx->input[j];
|
||||||
|
u64 t1 = big_sigma1(e) + ch (e, f, g) + h + in;
|
||||||
|
u64 t2 = big_sigma0(a) + maj(a, b, c);
|
||||||
|
h = g; g = f; f = e; e = d + t1;
|
||||||
|
d = c; c = b; b = a; a = t1 + t2;
|
||||||
|
}
|
||||||
|
size_t i16 = 0;
|
||||||
|
FOR(i, 1, 5) {
|
||||||
|
i16 += 16;
|
||||||
|
FOR (j, 0, 16) {
|
||||||
|
ctx->input[j] += lit_sigma1(ctx->input[(j- 2) & 15]);
|
||||||
|
ctx->input[j] += lit_sigma0(ctx->input[(j-15) & 15]);
|
||||||
|
ctx->input[j] += ctx->input[(j- 7) & 15];
|
||||||
|
u64 in = K[i16 + j] + ctx->input[j];
|
||||||
|
u64 t1 = big_sigma1(e) + ch (e, f, g) + h + in;
|
||||||
|
u64 t2 = big_sigma0(a) + maj(a, b, c);
|
||||||
|
h = g; g = f; f = e; e = d + t1;
|
||||||
|
d = c; c = b; b = a; a = t1 + t2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx->hash[0] += a; ctx->hash[1] += b;
|
||||||
|
ctx->hash[2] += c; ctx->hash[3] += d;
|
||||||
|
ctx->hash[4] += e; ctx->hash[5] += f;
|
||||||
|
ctx->hash[6] += g; ctx->hash[7] += h;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write 1 input byte
|
||||||
|
static void sha512_set_input(crypto_sha512_ctx *ctx, u8 input)
|
||||||
|
{
|
||||||
|
size_t word = ctx->input_idx >> 3;
|
||||||
|
size_t byte = ctx->input_idx & 7;
|
||||||
|
ctx->input[word] |= (u64)input << (8 * (7 - byte));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increment a 128-bit "word".
|
||||||
|
static void sha512_incr(u64 x[2], u64 y)
|
||||||
|
{
|
||||||
|
x[1] += y;
|
||||||
|
if (x[1] < y) {
|
||||||
|
x[0]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void crypto_sha512_init(crypto_sha512_ctx *ctx)
|
||||||
|
{
|
||||||
|
ctx->hash[0] = 0x6a09e667f3bcc908;
|
||||||
|
ctx->hash[1] = 0xbb67ae8584caa73b;
|
||||||
|
ctx->hash[2] = 0x3c6ef372fe94f82b;
|
||||||
|
ctx->hash[3] = 0xa54ff53a5f1d36f1;
|
||||||
|
ctx->hash[4] = 0x510e527fade682d1;
|
||||||
|
ctx->hash[5] = 0x9b05688c2b3e6c1f;
|
||||||
|
ctx->hash[6] = 0x1f83d9abfb41bd6b;
|
||||||
|
ctx->hash[7] = 0x5be0cd19137e2179;
|
||||||
|
ctx->input_size[0] = 0;
|
||||||
|
ctx->input_size[1] = 0;
|
||||||
|
ctx->input_idx = 0;
|
||||||
|
ZERO(ctx->input, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
void crypto_sha512_update(crypto_sha512_ctx *ctx,
|
||||||
|
const u8 *message, size_t message_size)
|
||||||
|
{
|
||||||
|
// Avoid undefined NULL pointer increments with empty messages
|
||||||
|
if (message_size == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Align ourselves with word boundaries
|
||||||
|
if ((ctx->input_idx & 7) != 0) {
|
||||||
|
size_t nb_bytes = MIN(align(ctx->input_idx, 8), message_size);
|
||||||
|
FOR (i, 0, nb_bytes) {
|
||||||
|
sha512_set_input(ctx, message[i]);
|
||||||
|
ctx->input_idx++;
|
||||||
|
}
|
||||||
|
message += nb_bytes;
|
||||||
|
message_size -= nb_bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Align ourselves with block boundaries
|
||||||
|
if ((ctx->input_idx & 127) != 0) {
|
||||||
|
size_t nb_words = MIN(align(ctx->input_idx, 128), message_size) >> 3;
|
||||||
|
load64_be_buf(ctx->input + (ctx->input_idx >> 3), message, nb_words);
|
||||||
|
ctx->input_idx += nb_words << 3;
|
||||||
|
message += nb_words << 3;
|
||||||
|
message_size -= nb_words << 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compress block if needed
|
||||||
|
if (ctx->input_idx == 128) {
|
||||||
|
sha512_incr(ctx->input_size, 1024); // size is in bits
|
||||||
|
sha512_compress(ctx);
|
||||||
|
ctx->input_idx = 0;
|
||||||
|
ZERO(ctx->input, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the message block by block
|
||||||
|
FOR (i, 0, message_size >> 7) { // number of blocks
|
||||||
|
load64_be_buf(ctx->input, message, 16);
|
||||||
|
sha512_incr(ctx->input_size, 1024); // size is in bits
|
||||||
|
sha512_compress(ctx);
|
||||||
|
ctx->input_idx = 0;
|
||||||
|
ZERO(ctx->input, 16);
|
||||||
|
message += 128;
|
||||||
|
}
|
||||||
|
message_size &= 127;
|
||||||
|
|
||||||
|
if (message_size != 0) {
|
||||||
|
// Remaining words
|
||||||
|
size_t nb_words = message_size >> 3;
|
||||||
|
load64_be_buf(ctx->input, message, nb_words);
|
||||||
|
ctx->input_idx += nb_words << 3;
|
||||||
|
message += nb_words << 3;
|
||||||
|
message_size -= nb_words << 3;
|
||||||
|
|
||||||
|
// Remaining bytes
|
||||||
|
FOR (i, 0, message_size) {
|
||||||
|
sha512_set_input(ctx, message[i]);
|
||||||
|
ctx->input_idx++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void crypto_sha512_final(crypto_sha512_ctx *ctx, u8 hash[64])
|
||||||
|
{
|
||||||
|
// Add padding bit
|
||||||
|
if (ctx->input_idx == 0) {
|
||||||
|
ZERO(ctx->input, 16);
|
||||||
|
}
|
||||||
|
sha512_set_input(ctx, 128);
|
||||||
|
|
||||||
|
// Update size
|
||||||
|
sha512_incr(ctx->input_size, ctx->input_idx * 8);
|
||||||
|
|
||||||
|
// Compress penultimate block (if any)
|
||||||
|
if (ctx->input_idx > 111) {
|
||||||
|
sha512_compress(ctx);
|
||||||
|
ZERO(ctx->input, 14);
|
||||||
|
}
|
||||||
|
// Compress last block
|
||||||
|
ctx->input[14] = ctx->input_size[0];
|
||||||
|
ctx->input[15] = ctx->input_size[1];
|
||||||
|
sha512_compress(ctx);
|
||||||
|
|
||||||
|
// Copy hash to output (big endian)
|
||||||
|
FOR (i, 0, 8) {
|
||||||
|
store64_be(hash + i*8, ctx->hash[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
WIPE_CTX(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void crypto_sha512(u8 hash[64], const u8 *message, size_t message_size)
|
||||||
|
{
|
||||||
|
crypto_sha512_ctx ctx;
|
||||||
|
crypto_sha512_init (&ctx);
|
||||||
|
crypto_sha512_update(&ctx, message, message_size);
|
||||||
|
crypto_sha512_final (&ctx, hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////
|
||||||
|
/// HMAC SHA 512 ///
|
||||||
|
////////////////////
|
||||||
|
void crypto_sha512_hmac_init(crypto_sha512_hmac_ctx *ctx,
|
||||||
|
const u8 *key, size_t key_size)
|
||||||
|
{
|
||||||
|
// hash key if it is too long
|
||||||
|
if (key_size > 128) {
|
||||||
|
crypto_sha512(ctx->key, key, key_size);
|
||||||
|
key = ctx->key;
|
||||||
|
key_size = 64;
|
||||||
|
}
|
||||||
|
// Compute inner key: padded key XOR 0x36
|
||||||
|
FOR (i, 0, key_size) { ctx->key[i] = key[i] ^ 0x36; }
|
||||||
|
FOR (i, key_size, 128) { ctx->key[i] = 0x36; }
|
||||||
|
// Start computing inner hash
|
||||||
|
crypto_sha512_init (&ctx->ctx);
|
||||||
|
crypto_sha512_update(&ctx->ctx, ctx->key, 128);
|
||||||
|
}
|
||||||
|
|
||||||
|
void crypto_sha512_hmac_update(crypto_sha512_hmac_ctx *ctx,
|
||||||
|
const u8 *message, size_t message_size)
|
||||||
|
{
|
||||||
|
crypto_sha512_update(&ctx->ctx, message, message_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void crypto_sha512_hmac_final(crypto_sha512_hmac_ctx *ctx, u8 hmac[64])
|
||||||
|
{
|
||||||
|
// Finish computing inner hash
|
||||||
|
crypto_sha512_final(&ctx->ctx, hmac);
|
||||||
|
// Compute outer key: padded key XOR 0x5c
|
||||||
|
FOR (i, 0, 128) {
|
||||||
|
ctx->key[i] ^= 0x36 ^ 0x5c;
|
||||||
|
}
|
||||||
|
// Compute outer hash
|
||||||
|
crypto_sha512_init (&ctx->ctx);
|
||||||
|
crypto_sha512_update(&ctx->ctx, ctx->key , 128);
|
||||||
|
crypto_sha512_update(&ctx->ctx, hmac, 64);
|
||||||
|
crypto_sha512_final (&ctx->ctx, hmac); // outer hash
|
||||||
|
WIPE_CTX(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void crypto_sha512_hmac(u8 hmac[64], const u8 *key, size_t key_size,
|
||||||
|
const u8 *message, size_t message_size)
|
||||||
|
{
|
||||||
|
crypto_sha512_hmac_ctx ctx;
|
||||||
|
crypto_sha512_hmac_init (&ctx, key, key_size);
|
||||||
|
crypto_sha512_hmac_update(&ctx, message, message_size);
|
||||||
|
crypto_sha512_hmac_final (&ctx, hmac);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////
|
||||||
|
/// HKDF SHA 512 ///
|
||||||
|
////////////////////
|
||||||
|
void crypto_sha512_hkdf_expand(u8 *okm, size_t okm_size,
|
||||||
|
const u8 *prk, size_t prk_size,
|
||||||
|
const u8 *info, size_t info_size)
|
||||||
|
{
|
||||||
|
int not_first = 0;
|
||||||
|
u8 ctr = 1;
|
||||||
|
u8 blk[64];
|
||||||
|
|
||||||
|
while (okm_size > 0) {
|
||||||
|
size_t out_size = MIN(okm_size, sizeof(blk));
|
||||||
|
|
||||||
|
crypto_sha512_hmac_ctx ctx;
|
||||||
|
crypto_sha512_hmac_init(&ctx, prk , prk_size);
|
||||||
|
if (not_first) {
|
||||||
|
// For some reason HKDF uses some kind of CBC mode.
|
||||||
|
// For some reason CTR mode alone wasn't enough.
|
||||||
|
// Like what, they didn't trust HMAC in 2010? Really??
|
||||||
|
crypto_sha512_hmac_update(&ctx, blk , sizeof(blk));
|
||||||
|
}
|
||||||
|
crypto_sha512_hmac_update(&ctx, info, info_size);
|
||||||
|
crypto_sha512_hmac_update(&ctx, &ctr, 1);
|
||||||
|
crypto_sha512_hmac_final(&ctx, blk);
|
||||||
|
|
||||||
|
COPY(okm, blk, out_size);
|
||||||
|
|
||||||
|
not_first = 1;
|
||||||
|
okm += out_size;
|
||||||
|
okm_size -= out_size;
|
||||||
|
ctr++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void crypto_sha512_hkdf(u8 *okm , size_t okm_size,
|
||||||
|
const u8 *ikm , size_t ikm_size,
|
||||||
|
const u8 *salt, size_t salt_size,
|
||||||
|
const u8 *info, size_t info_size)
|
||||||
|
{
|
||||||
|
// Extract
|
||||||
|
u8 prk[64];
|
||||||
|
crypto_sha512_hmac(prk, salt, salt_size, ikm, ikm_size);
|
||||||
|
|
||||||
|
// Expand
|
||||||
|
crypto_sha512_hkdf_expand(okm, okm_size, prk, sizeof(prk), info, info_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
/// Ed25519 ///
|
||||||
|
///////////////
|
||||||
|
void crypto_ed25519_key_pair(u8 secret_key[64], u8 public_key[32], u8 seed[32])
|
||||||
|
{
|
||||||
|
u8 a[64];
|
||||||
|
COPY(a, seed, 32); // a[ 0..31] = seed
|
||||||
|
crypto_wipe(seed, 32);
|
||||||
|
COPY(secret_key, a, 32); // secret key = seed
|
||||||
|
crypto_sha512(a, a, 32); // a[ 0..31] = scalar
|
||||||
|
crypto_eddsa_trim_scalar(a, a); // a[ 0..31] = trimmed scalar
|
||||||
|
crypto_eddsa_scalarbase(public_key, a); // public key = [trimmed scalar]B
|
||||||
|
COPY(secret_key + 32, public_key, 32); // secret key includes public half
|
||||||
|
WIPE_BUFFER(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void hash_reduce(u8 h[32],
|
||||||
|
const u8 *a, size_t a_size,
|
||||||
|
const u8 *b, size_t b_size,
|
||||||
|
const u8 *c, size_t c_size,
|
||||||
|
const u8 *d, size_t d_size)
|
||||||
|
{
|
||||||
|
u8 hash[64];
|
||||||
|
crypto_sha512_ctx ctx;
|
||||||
|
crypto_sha512_init (&ctx);
|
||||||
|
crypto_sha512_update(&ctx, a, a_size);
|
||||||
|
crypto_sha512_update(&ctx, b, b_size);
|
||||||
|
crypto_sha512_update(&ctx, c, c_size);
|
||||||
|
crypto_sha512_update(&ctx, d, d_size);
|
||||||
|
crypto_sha512_final (&ctx, hash);
|
||||||
|
crypto_eddsa_reduce(h, hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ed25519_dom_sign(u8 signature [64], const u8 secret_key[32],
|
||||||
|
const u8 *dom, size_t dom_size,
|
||||||
|
const u8 *message, size_t message_size)
|
||||||
|
{
|
||||||
|
u8 a[64]; // secret scalar and prefix
|
||||||
|
u8 r[32]; // secret deterministic "random" nonce
|
||||||
|
u8 h[32]; // publically verifiable hash of the message (not wiped)
|
||||||
|
u8 R[32]; // first half of the signature (allows overlapping inputs)
|
||||||
|
const u8 *pk = secret_key + 32;
|
||||||
|
|
||||||
|
crypto_sha512(a, secret_key, 32);
|
||||||
|
crypto_eddsa_trim_scalar(a, a);
|
||||||
|
hash_reduce(r, dom, dom_size, a + 32, 32, message, message_size, 0, 0);
|
||||||
|
crypto_eddsa_scalarbase(R, r);
|
||||||
|
hash_reduce(h, dom, dom_size, R, 32, pk, 32, message, message_size);
|
||||||
|
COPY(signature, R, 32);
|
||||||
|
crypto_eddsa_mul_add(signature + 32, h, a, r);
|
||||||
|
|
||||||
|
WIPE_BUFFER(a);
|
||||||
|
WIPE_BUFFER(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
void crypto_ed25519_sign(u8 signature [64], const u8 secret_key[64],
|
||||||
|
const u8 *message, size_t message_size)
|
||||||
|
{
|
||||||
|
ed25519_dom_sign(signature, secret_key, 0, 0, message, message_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
int crypto_ed25519_check(const u8 signature[64], const u8 public_key[32],
|
||||||
|
const u8 *msg, size_t msg_size)
|
||||||
|
{
|
||||||
|
u8 h_ram[32];
|
||||||
|
hash_reduce(h_ram, signature, 32, public_key, 32, msg, msg_size, 0, 0);
|
||||||
|
return crypto_eddsa_check_equation(signature, public_key, h_ram);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const u8 domain[34] = "SigEd25519 no Ed25519 collisions\1";
|
||||||
|
|
||||||
|
void crypto_ed25519_ph_sign(uint8_t signature[64], const uint8_t secret_key[64],
|
||||||
|
const uint8_t message_hash[64])
|
||||||
|
{
|
||||||
|
ed25519_dom_sign(signature, secret_key, domain, sizeof(domain),
|
||||||
|
message_hash, 64);
|
||||||
|
}
|
||||||
|
|
||||||
|
int crypto_ed25519_ph_check(const uint8_t sig[64], const uint8_t pk[32],
|
||||||
|
const uint8_t msg_hash[64])
|
||||||
|
{
|
||||||
|
u8 h_ram[32];
|
||||||
|
hash_reduce(h_ram, domain, sizeof(domain), sig, 32, pk, 32, msg_hash, 64);
|
||||||
|
return crypto_eddsa_check_equation(sig, pk, h_ram);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef MONOCYPHER_CPP_NAMESPACE
|
||||||
|
}
|
||||||
|
#endif
|
||||||
+140
@@ -0,0 +1,140 @@
|
|||||||
|
// Monocypher version 4.0.2
|
||||||
|
//
|
||||||
|
// This file is dual-licensed. Choose whichever licence you want from
|
||||||
|
// the two licences listed below.
|
||||||
|
//
|
||||||
|
// The first licence is a regular 2-clause BSD licence. The second licence
|
||||||
|
// is the CC-0 from Creative Commons. It is intended to release Monocypher
|
||||||
|
// to the public domain. The BSD licence serves as a fallback option.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0
|
||||||
|
//
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Copyright (c) 2017-2019, Loup Vaillant
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without
|
||||||
|
// modification, are permitted provided that the following conditions are
|
||||||
|
// met:
|
||||||
|
//
|
||||||
|
// 1. Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
//
|
||||||
|
// 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer in the
|
||||||
|
// documentation and/or other materials provided with the
|
||||||
|
// distribution.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
//
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Written in 2017-2019 by Loup Vaillant
|
||||||
|
//
|
||||||
|
// To the extent possible under law, the author(s) have dedicated all copyright
|
||||||
|
// and related neighboring rights to this software to the public domain
|
||||||
|
// worldwide. This software is distributed without any warranty.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the CC0 Public Domain Dedication along
|
||||||
|
// with this software. If not, see
|
||||||
|
// <https://creativecommons.org/publicdomain/zero/1.0/>
|
||||||
|
|
||||||
|
#ifndef ED25519_H
|
||||||
|
#define ED25519_H
|
||||||
|
|
||||||
|
#include "monocypher.h"
|
||||||
|
|
||||||
|
#ifdef MONOCYPHER_CPP_NAMESPACE
|
||||||
|
namespace MONOCYPHER_CPP_NAMESPACE {
|
||||||
|
#elif defined(__cplusplus)
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
////////////////////////
|
||||||
|
/// Type definitions ///
|
||||||
|
////////////////////////
|
||||||
|
|
||||||
|
// Do not rely on the size or content on any of those types,
|
||||||
|
// they may change without notice.
|
||||||
|
typedef struct {
|
||||||
|
uint64_t hash[8];
|
||||||
|
uint64_t input[16];
|
||||||
|
uint64_t input_size[2];
|
||||||
|
size_t input_idx;
|
||||||
|
} crypto_sha512_ctx;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t key[128];
|
||||||
|
crypto_sha512_ctx ctx;
|
||||||
|
} crypto_sha512_hmac_ctx;
|
||||||
|
|
||||||
|
|
||||||
|
// SHA 512
|
||||||
|
// -------
|
||||||
|
void crypto_sha512_init (crypto_sha512_ctx *ctx);
|
||||||
|
void crypto_sha512_update(crypto_sha512_ctx *ctx,
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
void crypto_sha512_final (crypto_sha512_ctx *ctx, uint8_t hash[64]);
|
||||||
|
void crypto_sha512(uint8_t hash[64],
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
|
||||||
|
// SHA 512 HMAC
|
||||||
|
// ------------
|
||||||
|
void crypto_sha512_hmac_init(crypto_sha512_hmac_ctx *ctx,
|
||||||
|
const uint8_t *key, size_t key_size);
|
||||||
|
void crypto_sha512_hmac_update(crypto_sha512_hmac_ctx *ctx,
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
void crypto_sha512_hmac_final(crypto_sha512_hmac_ctx *ctx, uint8_t hmac[64]);
|
||||||
|
void crypto_sha512_hmac(uint8_t hmac[64],
|
||||||
|
const uint8_t *key , size_t key_size,
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
|
||||||
|
// SHA 512 HKDF
|
||||||
|
// ------------
|
||||||
|
void crypto_sha512_hkdf_expand(uint8_t *okm, size_t okm_size,
|
||||||
|
const uint8_t *prk, size_t prk_size,
|
||||||
|
const uint8_t *info, size_t info_size);
|
||||||
|
void crypto_sha512_hkdf(uint8_t *okm , size_t okm_size,
|
||||||
|
const uint8_t *ikm , size_t ikm_size,
|
||||||
|
const uint8_t *salt, size_t salt_size,
|
||||||
|
const uint8_t *info, size_t info_size);
|
||||||
|
|
||||||
|
// Ed25519
|
||||||
|
// -------
|
||||||
|
// Signatures (EdDSA with curve25519 + SHA-512)
|
||||||
|
// --------------------------------------------
|
||||||
|
void crypto_ed25519_key_pair(uint8_t secret_key[64],
|
||||||
|
uint8_t public_key[32],
|
||||||
|
uint8_t seed[32]);
|
||||||
|
void crypto_ed25519_sign(uint8_t signature [64],
|
||||||
|
const uint8_t secret_key[64],
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
int crypto_ed25519_check(const uint8_t signature [64],
|
||||||
|
const uint8_t public_key[32],
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
|
||||||
|
// Pre-hash variants
|
||||||
|
void crypto_ed25519_ph_sign(uint8_t signature [64],
|
||||||
|
const uint8_t secret_key [64],
|
||||||
|
const uint8_t message_hash[64]);
|
||||||
|
int crypto_ed25519_ph_check(const uint8_t signature [64],
|
||||||
|
const uint8_t public_key [32],
|
||||||
|
const uint8_t message_hash[64]);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // ED25519_H
|
||||||
Vendored
+2956
File diff suppressed because it is too large
Load Diff
Vendored
+321
@@ -0,0 +1,321 @@
|
|||||||
|
// Monocypher version 4.0.2
|
||||||
|
//
|
||||||
|
// This file is dual-licensed. Choose whichever licence you want from
|
||||||
|
// the two licences listed below.
|
||||||
|
//
|
||||||
|
// The first licence is a regular 2-clause BSD licence. The second licence
|
||||||
|
// is the CC-0 from Creative Commons. It is intended to release Monocypher
|
||||||
|
// to the public domain. The BSD licence serves as a fallback option.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0
|
||||||
|
//
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Copyright (c) 2017-2019, Loup Vaillant
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Redistribution and use in source and binary forms, with or without
|
||||||
|
// modification, are permitted provided that the following conditions are
|
||||||
|
// met:
|
||||||
|
//
|
||||||
|
// 1. Redistributions of source code must retain the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer.
|
||||||
|
//
|
||||||
|
// 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
// notice, this list of conditions and the following disclaimer in the
|
||||||
|
// documentation and/or other materials provided with the
|
||||||
|
// distribution.
|
||||||
|
//
|
||||||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
//
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// Written in 2017-2019 by Loup Vaillant
|
||||||
|
//
|
||||||
|
// To the extent possible under law, the author(s) have dedicated all copyright
|
||||||
|
// and related neighboring rights to this software to the public domain
|
||||||
|
// worldwide. This software is distributed without any warranty.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the CC0 Public Domain Dedication along
|
||||||
|
// with this software. If not, see
|
||||||
|
// <https://creativecommons.org/publicdomain/zero/1.0/>
|
||||||
|
|
||||||
|
#ifndef MONOCYPHER_H
|
||||||
|
#define MONOCYPHER_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef MONOCYPHER_CPP_NAMESPACE
|
||||||
|
namespace MONOCYPHER_CPP_NAMESPACE {
|
||||||
|
#elif defined(__cplusplus)
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Constant time comparisons
|
||||||
|
// -------------------------
|
||||||
|
|
||||||
|
// Return 0 if a and b are equal, -1 otherwise
|
||||||
|
int crypto_verify16(const uint8_t a[16], const uint8_t b[16]);
|
||||||
|
int crypto_verify32(const uint8_t a[32], const uint8_t b[32]);
|
||||||
|
int crypto_verify64(const uint8_t a[64], const uint8_t b[64]);
|
||||||
|
|
||||||
|
|
||||||
|
// Erase sensitive data
|
||||||
|
// --------------------
|
||||||
|
void crypto_wipe(void *secret, size_t size);
|
||||||
|
|
||||||
|
|
||||||
|
// Authenticated encryption
|
||||||
|
// ------------------------
|
||||||
|
void crypto_aead_lock(uint8_t *cipher_text,
|
||||||
|
uint8_t mac [16],
|
||||||
|
const uint8_t key [32],
|
||||||
|
const uint8_t nonce[24],
|
||||||
|
const uint8_t *ad, size_t ad_size,
|
||||||
|
const uint8_t *plain_text, size_t text_size);
|
||||||
|
int crypto_aead_unlock(uint8_t *plain_text,
|
||||||
|
const uint8_t mac [16],
|
||||||
|
const uint8_t key [32],
|
||||||
|
const uint8_t nonce[24],
|
||||||
|
const uint8_t *ad, size_t ad_size,
|
||||||
|
const uint8_t *cipher_text, size_t text_size);
|
||||||
|
|
||||||
|
// Authenticated stream
|
||||||
|
// --------------------
|
||||||
|
typedef struct {
|
||||||
|
uint64_t counter;
|
||||||
|
uint8_t key[32];
|
||||||
|
uint8_t nonce[8];
|
||||||
|
} crypto_aead_ctx;
|
||||||
|
|
||||||
|
void crypto_aead_init_x(crypto_aead_ctx *ctx,
|
||||||
|
const uint8_t key[32], const uint8_t nonce[24]);
|
||||||
|
void crypto_aead_init_djb(crypto_aead_ctx *ctx,
|
||||||
|
const uint8_t key[32], const uint8_t nonce[8]);
|
||||||
|
void crypto_aead_init_ietf(crypto_aead_ctx *ctx,
|
||||||
|
const uint8_t key[32], const uint8_t nonce[12]);
|
||||||
|
|
||||||
|
void crypto_aead_write(crypto_aead_ctx *ctx,
|
||||||
|
uint8_t *cipher_text,
|
||||||
|
uint8_t mac[16],
|
||||||
|
const uint8_t *ad , size_t ad_size,
|
||||||
|
const uint8_t *plain_text, size_t text_size);
|
||||||
|
int crypto_aead_read(crypto_aead_ctx *ctx,
|
||||||
|
uint8_t *plain_text,
|
||||||
|
const uint8_t mac[16],
|
||||||
|
const uint8_t *ad , size_t ad_size,
|
||||||
|
const uint8_t *cipher_text, size_t text_size);
|
||||||
|
|
||||||
|
|
||||||
|
// General purpose hash (BLAKE2b)
|
||||||
|
// ------------------------------
|
||||||
|
|
||||||
|
// Direct interface
|
||||||
|
void crypto_blake2b(uint8_t *hash, size_t hash_size,
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
|
||||||
|
void crypto_blake2b_keyed(uint8_t *hash, size_t hash_size,
|
||||||
|
const uint8_t *key, size_t key_size,
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
|
||||||
|
// Incremental interface
|
||||||
|
typedef struct {
|
||||||
|
// Do not rely on the size or contents of this type,
|
||||||
|
// for they may change without notice.
|
||||||
|
uint64_t hash[8];
|
||||||
|
uint64_t input_offset[2];
|
||||||
|
uint64_t input[16];
|
||||||
|
size_t input_idx;
|
||||||
|
size_t hash_size;
|
||||||
|
} crypto_blake2b_ctx;
|
||||||
|
|
||||||
|
void crypto_blake2b_init(crypto_blake2b_ctx *ctx, size_t hash_size);
|
||||||
|
void crypto_blake2b_keyed_init(crypto_blake2b_ctx *ctx, size_t hash_size,
|
||||||
|
const uint8_t *key, size_t key_size);
|
||||||
|
void crypto_blake2b_update(crypto_blake2b_ctx *ctx,
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
void crypto_blake2b_final(crypto_blake2b_ctx *ctx, uint8_t *hash);
|
||||||
|
|
||||||
|
|
||||||
|
// Password key derivation (Argon2)
|
||||||
|
// --------------------------------
|
||||||
|
#define CRYPTO_ARGON2_D 0
|
||||||
|
#define CRYPTO_ARGON2_I 1
|
||||||
|
#define CRYPTO_ARGON2_ID 2
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t algorithm; // Argon2d, Argon2i, Argon2id
|
||||||
|
uint32_t nb_blocks; // memory hardness, >= 8 * nb_lanes
|
||||||
|
uint32_t nb_passes; // CPU hardness, >= 1 (>= 3 recommended for Argon2i)
|
||||||
|
uint32_t nb_lanes; // parallelism level (single threaded anyway)
|
||||||
|
} crypto_argon2_config;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const uint8_t *pass;
|
||||||
|
const uint8_t *salt;
|
||||||
|
uint32_t pass_size;
|
||||||
|
uint32_t salt_size; // 16 bytes recommended
|
||||||
|
} crypto_argon2_inputs;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const uint8_t *key; // may be NULL if no key
|
||||||
|
const uint8_t *ad; // may be NULL if no additional data
|
||||||
|
uint32_t key_size; // 0 if no key (32 bytes recommended otherwise)
|
||||||
|
uint32_t ad_size; // 0 if no additional data
|
||||||
|
} crypto_argon2_extras;
|
||||||
|
|
||||||
|
extern const crypto_argon2_extras crypto_argon2_no_extras;
|
||||||
|
|
||||||
|
void crypto_argon2(uint8_t *hash, uint32_t hash_size, void *work_area,
|
||||||
|
crypto_argon2_config config,
|
||||||
|
crypto_argon2_inputs inputs,
|
||||||
|
crypto_argon2_extras extras);
|
||||||
|
|
||||||
|
|
||||||
|
// Key exchange (X-25519)
|
||||||
|
// ----------------------
|
||||||
|
|
||||||
|
// Shared secrets are not quite random.
|
||||||
|
// Hash them to derive an actual shared key.
|
||||||
|
void crypto_x25519_public_key(uint8_t public_key[32],
|
||||||
|
const uint8_t secret_key[32]);
|
||||||
|
void crypto_x25519(uint8_t raw_shared_secret[32],
|
||||||
|
const uint8_t your_secret_key [32],
|
||||||
|
const uint8_t their_public_key [32]);
|
||||||
|
|
||||||
|
// Conversion to EdDSA
|
||||||
|
void crypto_x25519_to_eddsa(uint8_t eddsa[32], const uint8_t x25519[32]);
|
||||||
|
|
||||||
|
// scalar "division"
|
||||||
|
// Used for OPRF. Be aware that exponential blinding is less secure
|
||||||
|
// than Diffie-Hellman key exchange.
|
||||||
|
void crypto_x25519_inverse(uint8_t blind_salt [32],
|
||||||
|
const uint8_t private_key[32],
|
||||||
|
const uint8_t curve_point[32]);
|
||||||
|
|
||||||
|
// "Dirty" versions of x25519_public_key().
|
||||||
|
// Use with crypto_elligator_rev().
|
||||||
|
// Leaks 3 bits of the private key.
|
||||||
|
void crypto_x25519_dirty_small(uint8_t pk[32], const uint8_t sk[32]);
|
||||||
|
void crypto_x25519_dirty_fast (uint8_t pk[32], const uint8_t sk[32]);
|
||||||
|
|
||||||
|
|
||||||
|
// Signatures
|
||||||
|
// ----------
|
||||||
|
|
||||||
|
// EdDSA with curve25519 + BLAKE2b
|
||||||
|
void crypto_eddsa_key_pair(uint8_t secret_key[64],
|
||||||
|
uint8_t public_key[32],
|
||||||
|
uint8_t seed[32]);
|
||||||
|
void crypto_eddsa_sign(uint8_t signature [64],
|
||||||
|
const uint8_t secret_key[64],
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
int crypto_eddsa_check(const uint8_t signature [64],
|
||||||
|
const uint8_t public_key[32],
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
|
||||||
|
// Conversion to X25519
|
||||||
|
void crypto_eddsa_to_x25519(uint8_t x25519[32], const uint8_t eddsa[32]);
|
||||||
|
|
||||||
|
// EdDSA building blocks
|
||||||
|
void crypto_eddsa_trim_scalar(uint8_t out[32], const uint8_t in[32]);
|
||||||
|
void crypto_eddsa_reduce(uint8_t reduced[32], const uint8_t expanded[64]);
|
||||||
|
void crypto_eddsa_mul_add(uint8_t r[32],
|
||||||
|
const uint8_t a[32],
|
||||||
|
const uint8_t b[32],
|
||||||
|
const uint8_t c[32]);
|
||||||
|
void crypto_eddsa_scalarbase(uint8_t point[32], const uint8_t scalar[32]);
|
||||||
|
int crypto_eddsa_check_equation(const uint8_t signature[64],
|
||||||
|
const uint8_t public_key[32],
|
||||||
|
const uint8_t h_ram[32]);
|
||||||
|
|
||||||
|
|
||||||
|
// Chacha20
|
||||||
|
// --------
|
||||||
|
|
||||||
|
// Specialised hash.
|
||||||
|
// Used to hash X25519 shared secrets.
|
||||||
|
void crypto_chacha20_h(uint8_t out[32],
|
||||||
|
const uint8_t key[32],
|
||||||
|
const uint8_t in [16]);
|
||||||
|
|
||||||
|
// Unauthenticated stream cipher.
|
||||||
|
// Don't forget to add authentication.
|
||||||
|
uint64_t crypto_chacha20_djb(uint8_t *cipher_text,
|
||||||
|
const uint8_t *plain_text,
|
||||||
|
size_t text_size,
|
||||||
|
const uint8_t key[32],
|
||||||
|
const uint8_t nonce[8],
|
||||||
|
uint64_t ctr);
|
||||||
|
uint32_t crypto_chacha20_ietf(uint8_t *cipher_text,
|
||||||
|
const uint8_t *plain_text,
|
||||||
|
size_t text_size,
|
||||||
|
const uint8_t key[32],
|
||||||
|
const uint8_t nonce[12],
|
||||||
|
uint32_t ctr);
|
||||||
|
uint64_t crypto_chacha20_x(uint8_t *cipher_text,
|
||||||
|
const uint8_t *plain_text,
|
||||||
|
size_t text_size,
|
||||||
|
const uint8_t key[32],
|
||||||
|
const uint8_t nonce[24],
|
||||||
|
uint64_t ctr);
|
||||||
|
|
||||||
|
|
||||||
|
// Poly 1305
|
||||||
|
// ---------
|
||||||
|
|
||||||
|
// This is a *one time* authenticator.
|
||||||
|
// Disclosing the mac reveals the key.
|
||||||
|
// See crypto_lock() on how to use it properly.
|
||||||
|
|
||||||
|
// Direct interface
|
||||||
|
void crypto_poly1305(uint8_t mac[16],
|
||||||
|
const uint8_t *message, size_t message_size,
|
||||||
|
const uint8_t key[32]);
|
||||||
|
|
||||||
|
// Incremental interface
|
||||||
|
typedef struct {
|
||||||
|
// Do not rely on the size or contents of this type,
|
||||||
|
// for they may change without notice.
|
||||||
|
uint8_t c[16]; // chunk of the message
|
||||||
|
size_t c_idx; // How many bytes are there in the chunk.
|
||||||
|
uint32_t r [4]; // constant multiplier (from the secret key)
|
||||||
|
uint32_t pad[4]; // random number added at the end (from the secret key)
|
||||||
|
uint32_t h [5]; // accumulated hash
|
||||||
|
} crypto_poly1305_ctx;
|
||||||
|
|
||||||
|
void crypto_poly1305_init (crypto_poly1305_ctx *ctx, const uint8_t key[32]);
|
||||||
|
void crypto_poly1305_update(crypto_poly1305_ctx *ctx,
|
||||||
|
const uint8_t *message, size_t message_size);
|
||||||
|
void crypto_poly1305_final (crypto_poly1305_ctx *ctx, uint8_t mac[16]);
|
||||||
|
|
||||||
|
|
||||||
|
// Elligator 2
|
||||||
|
// -----------
|
||||||
|
|
||||||
|
// Elligator mappings proper
|
||||||
|
void crypto_elligator_map(uint8_t curve [32], const uint8_t hidden[32]);
|
||||||
|
int crypto_elligator_rev(uint8_t hidden[32], const uint8_t curve [32],
|
||||||
|
uint8_t tweak);
|
||||||
|
|
||||||
|
// Easy to use key pair generation
|
||||||
|
void crypto_elligator_key_pair(uint8_t hidden[32], uint8_t secret_key[32],
|
||||||
|
uint8_t seed[32]);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MONOCYPHER_H
|
||||||
@@ -155,6 +155,7 @@ include(cmake/Dependencies.cmake)
|
|||||||
add_subdirectory(core/anslicensing)
|
add_subdirectory(core/anslicensing)
|
||||||
add_subdirectory(core/ANSLicensingSystem)
|
add_subdirectory(core/ANSLicensingSystem)
|
||||||
add_subdirectory(core/ANSLibsLoader)
|
add_subdirectory(core/ANSLibsLoader)
|
||||||
|
add_subdirectory(core/ANSModelPackage)
|
||||||
|
|
||||||
# Modules — feature DLLs (cross-platform)
|
# Modules — feature DLLs (cross-platform)
|
||||||
add_subdirectory(modules/ANSMOT)
|
add_subdirectory(modules/ANSMOT)
|
||||||
|
|||||||
@@ -0,0 +1,314 @@
|
|||||||
|
#ifndef ANSAEAD_H
|
||||||
|
#define ANSAEAD_H
|
||||||
|
#pragma once
|
||||||
|
// ANSAead — the AEAD the model package format is sealed with (RFC 8439).
|
||||||
|
//
|
||||||
|
// KEPT IN STEP WITH ANSMINDCORE's src/core/chacha20poly1305.hpp — the same
|
||||||
|
// algorithm, byte for byte, because a package written by one product must open
|
||||||
|
// in the other. Change them together or not at all.
|
||||||
|
//
|
||||||
|
// WHY IN-TREE, and why this cipher. A versioned crypto dependency on every
|
||||||
|
// platform we ship, for a primitive with published test vectors, is a worse
|
||||||
|
// trade than the algorithm itself. ChaCha20-Poly1305 is the one modern AEAD
|
||||||
|
// that is a few
|
||||||
|
// hundred lines of 32-bit arithmetic with no tables, no S-boxes and no
|
||||||
|
// timing-sensitive lookups — AES-GCM needs either hardware instructions or a
|
||||||
|
// GHASH that is easy to get subtly wrong. The suite checks this against the
|
||||||
|
// RFC's own vectors, so it is verified rather than merely present.
|
||||||
|
//
|
||||||
|
// NOT a general crypto library. It seals and opens model packages. The nonce
|
||||||
|
// rule is the caller's to keep: a key and nonce pair must never encrypt twice,
|
||||||
|
// which the package format satisfies by deriving the nonce from a random
|
||||||
|
// per-payload base and the chunk index.
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace ANSCENTER {
|
||||||
|
namespace aead {
|
||||||
|
|
||||||
|
// ── ChaCha20 (RFC 8439 §2.4) ──────────────────────────────────────────────
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
inline uint32_t rotl32(uint32_t v, int n) { return (v << n) | (v >> (32 - n)); }
|
||||||
|
|
||||||
|
inline uint32_t load32(const uint8_t* p) {
|
||||||
|
return uint32_t(p[0]) | (uint32_t(p[1]) << 8) | (uint32_t(p[2]) << 16) |
|
||||||
|
(uint32_t(p[3]) << 24);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void store32(uint8_t* p, uint32_t v) {
|
||||||
|
p[0] = uint8_t(v);
|
||||||
|
p[1] = uint8_t(v >> 8);
|
||||||
|
p[2] = uint8_t(v >> 16);
|
||||||
|
p[3] = uint8_t(v >> 24);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void chachaBlock(const uint8_t key[32], uint32_t counter,
|
||||||
|
const uint8_t nonce[12], uint8_t out[64]) {
|
||||||
|
static const char kSigma[] = "expand 32-byte k";
|
||||||
|
uint32_t s[16];
|
||||||
|
s[0] = load32(reinterpret_cast<const uint8_t*>(kSigma));
|
||||||
|
s[1] = load32(reinterpret_cast<const uint8_t*>(kSigma) + 4);
|
||||||
|
s[2] = load32(reinterpret_cast<const uint8_t*>(kSigma) + 8);
|
||||||
|
s[3] = load32(reinterpret_cast<const uint8_t*>(kSigma) + 12);
|
||||||
|
for (int i = 0; i < 8; ++i) s[4 + i] = load32(key + 4 * i);
|
||||||
|
s[12] = counter;
|
||||||
|
for (int i = 0; i < 3; ++i) s[13 + i] = load32(nonce + 4 * i);
|
||||||
|
|
||||||
|
uint32_t x[16];
|
||||||
|
std::memcpy(x, s, sizeof(x));
|
||||||
|
auto qr = [&x](int a, int b, int c, int d) {
|
||||||
|
x[a] += x[b]; x[d] = rotl32(x[d] ^ x[a], 16);
|
||||||
|
x[c] += x[d]; x[b] = rotl32(x[b] ^ x[c], 12);
|
||||||
|
x[a] += x[b]; x[d] = rotl32(x[d] ^ x[a], 8);
|
||||||
|
x[c] += x[d]; x[b] = rotl32(x[b] ^ x[c], 7);
|
||||||
|
};
|
||||||
|
for (int i = 0; i < 10; ++i) { // 20 rounds = 10 double rounds
|
||||||
|
qr(0, 4, 8, 12); qr(1, 5, 9, 13); qr(2, 6, 10, 14); qr(3, 7, 11, 15);
|
||||||
|
qr(0, 5, 10, 15); qr(1, 6, 11, 12); qr(2, 7, 8, 13); qr(3, 4, 9, 14);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 16; ++i) store32(out + 4 * i, x[i] + s[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// XOR a buffer with the keystream, starting at `counter`.
|
||||||
|
inline void chacha20Xor(const uint8_t key[32], uint32_t counter,
|
||||||
|
const uint8_t nonce[12], const uint8_t* in, size_t n,
|
||||||
|
uint8_t* out) {
|
||||||
|
uint8_t block[64];
|
||||||
|
size_t done = 0;
|
||||||
|
while (done < n) {
|
||||||
|
chachaBlock(key, counter++, nonce, block);
|
||||||
|
const size_t take = (n - done < 64) ? (n - done) : 64;
|
||||||
|
for (size_t i = 0; i < take; ++i) out[done + i] = in[done + i] ^ block[i];
|
||||||
|
done += take;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── Poly1305 (RFC 8439 §2.5), 130-bit accumulator in 26-bit limbs ─────────
|
||||||
|
class Poly1305 {
|
||||||
|
public:
|
||||||
|
explicit Poly1305(const uint8_t key[32]) {
|
||||||
|
r_[0] = (load32(key) ) & 0x3ffffff;
|
||||||
|
r_[1] = (load32(key + 3) >> 2) & 0x3ffff03;
|
||||||
|
r_[2] = (load32(key + 6) >> 4) & 0x3ffc0ff;
|
||||||
|
r_[3] = (load32(key + 9) >> 6) & 0x3f03fff;
|
||||||
|
r_[4] = (load32(key + 12) >> 8) & 0x00fffff;
|
||||||
|
for (int i = 0; i < 4; ++i) pad_[i] = load32(key + 16 + 4 * i);
|
||||||
|
}
|
||||||
|
|
||||||
|
void update(const uint8_t* m, size_t n) {
|
||||||
|
while (n > 0) {
|
||||||
|
const size_t take = (16 - bufLen_ < n) ? (16 - bufLen_) : n;
|
||||||
|
std::memcpy(buf_ + bufLen_, m, take);
|
||||||
|
bufLen_ += take;
|
||||||
|
m += take;
|
||||||
|
n -= take;
|
||||||
|
if (bufLen_ == 16) {
|
||||||
|
block(buf_, false);
|
||||||
|
bufLen_ = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void finish(uint8_t tag[16]) {
|
||||||
|
if (bufLen_ > 0) {
|
||||||
|
buf_[bufLen_++] = 1;
|
||||||
|
while (bufLen_ < 16) buf_[bufLen_++] = 0;
|
||||||
|
block(buf_, true);
|
||||||
|
}
|
||||||
|
// Carry propagation, then the conditional subtraction of p = 2^130-5.
|
||||||
|
uint32_t c;
|
||||||
|
c = h_[1] >> 26; h_[1] &= 0x3ffffff; h_[2] += c;
|
||||||
|
c = h_[2] >> 26; h_[2] &= 0x3ffffff; h_[3] += c;
|
||||||
|
c = h_[3] >> 26; h_[3] &= 0x3ffffff; h_[4] += c;
|
||||||
|
c = h_[4] >> 26; h_[4] &= 0x3ffffff; h_[0] += c * 5;
|
||||||
|
c = h_[0] >> 26; h_[0] &= 0x3ffffff; h_[1] += c;
|
||||||
|
|
||||||
|
uint32_t g[5];
|
||||||
|
g[0] = h_[0] + 5; c = g[0] >> 26; g[0] &= 0x3ffffff;
|
||||||
|
g[1] = h_[1] + c; c = g[1] >> 26; g[1] &= 0x3ffffff;
|
||||||
|
g[2] = h_[2] + c; c = g[2] >> 26; g[2] &= 0x3ffffff;
|
||||||
|
g[3] = h_[3] + c; c = g[3] >> 26; g[3] &= 0x3ffffff;
|
||||||
|
g[4] = h_[4] + c - (uint32_t(1) << 26);
|
||||||
|
|
||||||
|
uint32_t mask = (g[4] >> 31) - 1; // all ones when g >= p
|
||||||
|
for (int i = 0; i < 5; ++i) g[i] &= mask;
|
||||||
|
mask = ~mask;
|
||||||
|
for (int i = 0; i < 5; ++i) h_[i] = (h_[i] & mask) | g[i];
|
||||||
|
|
||||||
|
// h modulo 2^128, as four 32-bit words. The narrowing is deliberate: each
|
||||||
|
// word takes the low 32 bits of two limbs, and the only carry between
|
||||||
|
// words is the one the pad addition produces. Keeping the spilled limb
|
||||||
|
// bits in a 64-bit accumulator instead counts them twice — which is a
|
||||||
|
// tag that verifies against itself and against nothing else.
|
||||||
|
const uint32_t w0 = uint32_t((h_[0]) | (h_[1] << 26));
|
||||||
|
const uint32_t w1 = uint32_t((h_[1] >> 6) | (h_[2] << 20));
|
||||||
|
const uint32_t w2 = uint32_t((h_[2] >> 12) | (h_[3] << 14));
|
||||||
|
const uint32_t w3 = uint32_t((h_[3] >> 18) | (h_[4] << 8));
|
||||||
|
|
||||||
|
uint64_t f = uint64_t(w0) + pad_[0];
|
||||||
|
store32(tag, uint32_t(f));
|
||||||
|
f = uint64_t(w1) + pad_[1] + (f >> 32);
|
||||||
|
store32(tag + 4, uint32_t(f));
|
||||||
|
f = uint64_t(w2) + pad_[2] + (f >> 32);
|
||||||
|
store32(tag + 8, uint32_t(f));
|
||||||
|
f = uint64_t(w3) + pad_[3] + (f >> 32);
|
||||||
|
store32(tag + 12, uint32_t(f));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void block(const uint8_t m[16], bool last) {
|
||||||
|
const uint32_t hibit = last ? 0 : (uint32_t(1) << 24);
|
||||||
|
h_[0] += (load32(m) ) & 0x3ffffff;
|
||||||
|
h_[1] += (load32(m + 3) >> 2) & 0x3ffffff;
|
||||||
|
h_[2] += (load32(m + 6) >> 4) & 0x3ffffff;
|
||||||
|
h_[3] += (load32(m + 9) >> 6) & 0x3ffffff;
|
||||||
|
h_[4] += (load32(m + 12) >> 8) | hibit;
|
||||||
|
|
||||||
|
const uint32_t s1 = r_[1] * 5, s2 = r_[2] * 5, s3 = r_[3] * 5, s4 = r_[4] * 5;
|
||||||
|
uint64_t d0 = uint64_t(h_[0]) * r_[0] + uint64_t(h_[1]) * s4 +
|
||||||
|
uint64_t(h_[2]) * s3 + uint64_t(h_[3]) * s2 +
|
||||||
|
uint64_t(h_[4]) * s1;
|
||||||
|
uint64_t d1 = uint64_t(h_[0]) * r_[1] + uint64_t(h_[1]) * r_[0] +
|
||||||
|
uint64_t(h_[2]) * s4 + uint64_t(h_[3]) * s3 +
|
||||||
|
uint64_t(h_[4]) * s2;
|
||||||
|
uint64_t d2 = uint64_t(h_[0]) * r_[2] + uint64_t(h_[1]) * r_[1] +
|
||||||
|
uint64_t(h_[2]) * r_[0] + uint64_t(h_[3]) * s4 +
|
||||||
|
uint64_t(h_[4]) * s3;
|
||||||
|
uint64_t d3 = uint64_t(h_[0]) * r_[3] + uint64_t(h_[1]) * r_[2] +
|
||||||
|
uint64_t(h_[2]) * r_[1] + uint64_t(h_[3]) * r_[0] +
|
||||||
|
uint64_t(h_[4]) * s4;
|
||||||
|
uint64_t d4 = uint64_t(h_[0]) * r_[4] + uint64_t(h_[1]) * r_[3] +
|
||||||
|
uint64_t(h_[2]) * r_[2] + uint64_t(h_[3]) * r_[1] +
|
||||||
|
uint64_t(h_[4]) * r_[0];
|
||||||
|
|
||||||
|
uint32_t c = uint32_t(d0 >> 26); h_[0] = uint32_t(d0) & 0x3ffffff;
|
||||||
|
d1 += c; c = uint32_t(d1 >> 26); h_[1] = uint32_t(d1) & 0x3ffffff;
|
||||||
|
d2 += c; c = uint32_t(d2 >> 26); h_[2] = uint32_t(d2) & 0x3ffffff;
|
||||||
|
d3 += c; c = uint32_t(d3 >> 26); h_[3] = uint32_t(d3) & 0x3ffffff;
|
||||||
|
d4 += c; c = uint32_t(d4 >> 26); h_[4] = uint32_t(d4) & 0x3ffffff;
|
||||||
|
h_[0] += c * 5; c = h_[0] >> 26; h_[0] &= 0x3ffffff;
|
||||||
|
h_[1] += c;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t r_[5]{};
|
||||||
|
uint32_t h_[5]{};
|
||||||
|
uint32_t pad_[4]{};
|
||||||
|
uint8_t buf_[16]{};
|
||||||
|
size_t bufLen_ = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void poly1305Pad16(Poly1305& p, size_t n) {
|
||||||
|
static const uint8_t zeros[16] = {0};
|
||||||
|
const size_t rem = n % 16;
|
||||||
|
if (rem) p.update(zeros, 16 - rem);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void lenLE64(uint64_t v, uint8_t out[8]) {
|
||||||
|
for (int i = 0; i < 8; ++i) out[i] = uint8_t(v >> (8 * i));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equal in constant time, so a wrong tag leaks nothing through timing.
|
||||||
|
inline bool equalCT(const uint8_t* a, const uint8_t* b, size_t n) {
|
||||||
|
uint8_t diff = 0;
|
||||||
|
for (size_t i = 0; i < n; ++i) diff = uint8_t(diff | (a[i] ^ b[i]));
|
||||||
|
return diff == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
// ── AEAD (RFC 8439 §2.8) ──────────────────────────────────────────────────
|
||||||
|
|
||||||
|
inline constexpr size_t kKeyBytes = 32;
|
||||||
|
inline constexpr size_t kNonceBytes = 12;
|
||||||
|
inline constexpr size_t kTagBytes = 16;
|
||||||
|
|
||||||
|
// Seals `plain` in place-independent fashion: `out` receives the ciphertext,
|
||||||
|
// `tag` the authenticator over ciphertext and associated data.
|
||||||
|
inline void aeadSeal(const uint8_t key[kKeyBytes], const uint8_t nonce[kNonceBytes],
|
||||||
|
const uint8_t* aad, size_t aadLen, const uint8_t* plain,
|
||||||
|
size_t plainLen, uint8_t* out, uint8_t tag[kTagBytes]) {
|
||||||
|
uint8_t polyKey[64];
|
||||||
|
const uint8_t zeroIn[64] = {0};
|
||||||
|
detail::chacha20Xor(key, 0, nonce, zeroIn, 64, polyKey); // block 0
|
||||||
|
if (plainLen) detail::chacha20Xor(key, 1, nonce, plain, plainLen, out);
|
||||||
|
|
||||||
|
detail::Poly1305 mac(polyKey);
|
||||||
|
if (aadLen) {
|
||||||
|
mac.update(aad, aadLen);
|
||||||
|
detail::poly1305Pad16(mac, aadLen);
|
||||||
|
}
|
||||||
|
if (plainLen) {
|
||||||
|
mac.update(out, plainLen);
|
||||||
|
detail::poly1305Pad16(mac, plainLen);
|
||||||
|
}
|
||||||
|
uint8_t lens[16];
|
||||||
|
detail::lenLE64(aadLen, lens);
|
||||||
|
detail::lenLE64(plainLen, lens + 8);
|
||||||
|
mac.update(lens, 16);
|
||||||
|
mac.finish(tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Opens what aeadSeal produced. Returns false — without writing plaintext the
|
||||||
|
// caller can trust — when the tag does not verify.
|
||||||
|
inline bool aeadOpen(const uint8_t key[kKeyBytes], const uint8_t nonce[kNonceBytes],
|
||||||
|
const uint8_t* aad, size_t aadLen, const uint8_t* cipher,
|
||||||
|
size_t cipherLen, const uint8_t tag[kTagBytes],
|
||||||
|
uint8_t* out) {
|
||||||
|
uint8_t polyKey[64];
|
||||||
|
const uint8_t zeroIn[64] = {0};
|
||||||
|
detail::chacha20Xor(key, 0, nonce, zeroIn, 64, polyKey);
|
||||||
|
|
||||||
|
detail::Poly1305 mac(polyKey);
|
||||||
|
if (aadLen) {
|
||||||
|
mac.update(aad, aadLen);
|
||||||
|
detail::poly1305Pad16(mac, aadLen);
|
||||||
|
}
|
||||||
|
if (cipherLen) {
|
||||||
|
mac.update(cipher, cipherLen);
|
||||||
|
detail::poly1305Pad16(mac, cipherLen);
|
||||||
|
}
|
||||||
|
uint8_t lens[16];
|
||||||
|
detail::lenLE64(aadLen, lens);
|
||||||
|
detail::lenLE64(cipherLen, lens + 8);
|
||||||
|
mac.update(lens, 16);
|
||||||
|
uint8_t want[kTagBytes];
|
||||||
|
mac.finish(want);
|
||||||
|
if (!detail::equalCT(want, tag, kTagBytes)) return false;
|
||||||
|
|
||||||
|
if (cipherLen) detail::chacha20Xor(key, 1, nonce, cipher, cipherLen, out);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convenience over vectors: ciphertext with the 16-byte tag appended.
|
||||||
|
inline std::vector<uint8_t> seal(const std::array<uint8_t, kKeyBytes>& key,
|
||||||
|
const std::array<uint8_t, kNonceBytes>& nonce,
|
||||||
|
const std::string& aad,
|
||||||
|
const std::vector<uint8_t>& plain) {
|
||||||
|
std::vector<uint8_t> out(plain.size() + kTagBytes);
|
||||||
|
aeadSeal(key.data(), nonce.data(),
|
||||||
|
reinterpret_cast<const uint8_t*>(aad.data()), aad.size(),
|
||||||
|
plain.data(), plain.size(), out.data(), out.data() + plain.size());
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool open(const std::array<uint8_t, kKeyBytes>& key,
|
||||||
|
const std::array<uint8_t, kNonceBytes>& nonce,
|
||||||
|
const std::string& aad, const std::vector<uint8_t>& sealed,
|
||||||
|
std::vector<uint8_t>& plain) {
|
||||||
|
if (sealed.size() < kTagBytes) return false;
|
||||||
|
const size_t n = sealed.size() - kTagBytes;
|
||||||
|
plain.assign(n, 0);
|
||||||
|
return aeadOpen(key.data(), nonce.data(),
|
||||||
|
reinterpret_cast<const uint8_t*>(aad.data()), aad.size(),
|
||||||
|
sealed.data(), n, sealed.data() + n, plain.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace aead
|
||||||
|
} // namespace ANSCENTER
|
||||||
|
|
||||||
|
#endif // ANSAEAD_H
|
||||||
@@ -0,0 +1,768 @@
|
|||||||
|
#ifndef ANSMODELPACKAGE_H
|
||||||
|
#define ANSMODELPACKAGE_H
|
||||||
|
#pragma once
|
||||||
|
// ANSModelPackage — reading (and, for tools and tests, writing) an ANS model
|
||||||
|
// package: one encrypted file carrying a model, its classes and how to run it.
|
||||||
|
//
|
||||||
|
// KEPT IN STEP WITH ANSMINDCORE's src/model/ansmodel.hpp. The two are the same
|
||||||
|
// format and must stay byte-compatible: the unit test here opens a package
|
||||||
|
// written by THAT implementation, which is what makes "compatible" a fact
|
||||||
|
// rather than an intention. Change them together.
|
||||||
|
//
|
||||||
|
// WHAT THIS REPLACES. A model shipped as a zip under a password every copy of
|
||||||
|
// the software knew — a password that has since been recovered — holding
|
||||||
|
// Categories/<name>.<id>, classes.names, a zero-length backend marker and
|
||||||
|
// train_last.onnx. Opening one meant inflating it to a directory, so the
|
||||||
|
// unprotected model sat on disk, was optimised, and was zipped back into a
|
||||||
|
// cache. This format keeps every fact that bundle carried and changes how it
|
||||||
|
// is carried: the manifest names the classes and the model family, and the
|
||||||
|
// bytes go straight from decryption into the runtime (engine/ibackend's
|
||||||
|
// ModelSource) without touching the filesystem.
|
||||||
|
//
|
||||||
|
// LAYOUT (all integers little-endian, offsets absolute):
|
||||||
|
//
|
||||||
|
// 0 8 magic "ANSMDL\0\0"
|
||||||
|
// 8 2 format version (1)
|
||||||
|
// 10 2 flags, bit 0 = more than one model
|
||||||
|
// 12 4 header length
|
||||||
|
// 16 n cleartext header, UTF-8 JSON — readable without any key
|
||||||
|
// … manifest, sealed — classes, task, family, models
|
||||||
|
// … payloads, sealed in chunks — the model bytes
|
||||||
|
//
|
||||||
|
// WHAT IS IN THE CLEAR, and why so little. The header carries only what a
|
||||||
|
// reader needs before it can decrypt — which key opens the file, where the
|
||||||
|
// sealed parts are — plus an optional card of counts for a store listing.
|
||||||
|
// Class names, metrics and the model's shape are in the manifest, sealed.
|
||||||
|
//
|
||||||
|
// SEALING. ChaCha20-Poly1305 (core/chacha20poly1305) with a content key that
|
||||||
|
// is random per package. Each chunk's nonce is a random per-payload base
|
||||||
|
// followed by the chunk index, and the associated data binds the key id, the
|
||||||
|
// payload id and that index — so a chunk cannot be moved between payloads or
|
||||||
|
// between packages, and a reordered file fails on the first chunk.
|
||||||
|
//
|
||||||
|
// WRAPPING. The content key is sealed to the base key with X25519: an
|
||||||
|
// ephemeral key pair per package, a shared secret against the base public key,
|
||||||
|
// and a key derived from it with HMAC-SHA256. Packaging tools therefore hold
|
||||||
|
// only the PUBLIC half and cannot open what they produce — which is the point
|
||||||
|
// of doing it asymmetrically when a symmetric key would have been simpler. A
|
||||||
|
// package may carry several wraps; a reader takes the first one it holds a key
|
||||||
|
// for. The older symmetric wrap is still read, so packages written before this
|
||||||
|
// keep opening.
|
||||||
|
//
|
||||||
|
// SIGNING. Ed25519 (RFC 8032, via the vendored monocypher-ed25519) over the
|
||||||
|
// header with its own `sig` removed. The signature is made by a RELEASE key
|
||||||
|
// whose public half travels in the package inside a certificate the ROOT key
|
||||||
|
// signed — so an engine that has never seen that release still verifies it,
|
||||||
|
// from the one root key it was built with. That is what lets signing keys
|
||||||
|
// rotate without stranding an engine that cannot be updated.
|
||||||
|
//
|
||||||
|
// EXPIRY AND REVOCATION. An expired certificate WARNS and loads: the date says
|
||||||
|
// when a key was in service, not whether the package is genuine, and an
|
||||||
|
// air-gapped box's clock drifts. A revoked key refuses outright. Strict mode
|
||||||
|
// turns expiry into a refusal for sites that keep accurate time.
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
#include <ctime>
|
||||||
|
#include <fstream>
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
#include "ANSAead.h"
|
||||||
|
#include "ANSPackageBits.h"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include "monocypher-ed25519.h"
|
||||||
|
#include "monocypher.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace ANSCENTER {
|
||||||
|
namespace modelpkg {
|
||||||
|
|
||||||
|
using Json = nlohmann::json;
|
||||||
|
|
||||||
|
inline constexpr char kMagic[8] = {'A', 'N', 'S', 'M', 'D', 'L', '\0', '\0'};
|
||||||
|
inline constexpr uint16_t kVersion = 1;
|
||||||
|
inline constexpr uint16_t kFlagMultiModel = 1u << 0;
|
||||||
|
inline constexpr const char* kWrapSymmetric = "chacha20poly1305-key";
|
||||||
|
inline constexpr const char* kWrapX25519 = "x25519-hkdf-sha256-chacha20poly1305";
|
||||||
|
inline constexpr const char* kSigEd25519 = "ed25519";
|
||||||
|
inline constexpr uint32_t kDefaultChunk = 4u * 1024 * 1024;
|
||||||
|
|
||||||
|
using Key = std::array<uint8_t, aead::kKeyBytes>;
|
||||||
|
|
||||||
|
// What this build can open and what it trusts.
|
||||||
|
//
|
||||||
|
// A reader holds every base key it has ever shipped, because the base key does
|
||||||
|
// not rotate: a package names the key it was sealed to, and the answer to an
|
||||||
|
// unknown id is "this package needs a newer engine", not a decryption failure.
|
||||||
|
// It holds root public keys, which is how a release signing key it has never
|
||||||
|
// seen is still verified. And it holds the list of signing keys that have been
|
||||||
|
// burned, which is the only thing that refuses a validly signed package.
|
||||||
|
struct Keyring {
|
||||||
|
std::map<std::string, Key> keys; // symmetric base keys (legacy wrap)
|
||||||
|
std::map<std::string, Key> x25519; // base PRIVATE keys, by id
|
||||||
|
std::map<std::string, Key> roots; // root PUBLIC keys, by id
|
||||||
|
std::vector<std::string> revokedSigners;
|
||||||
|
bool strictExpiry = false; // off: an expired cert warns and loads
|
||||||
|
int64_t nowSec = 0; // 0 = ask the clock
|
||||||
|
|
||||||
|
void add(std::string id, const Key& k) { keys[std::move(id)] = k; }
|
||||||
|
void addX25519(std::string id, const Key& priv) { x25519[std::move(id)] = priv; }
|
||||||
|
void addRoot(std::string id, const Key& pub) { roots[std::move(id)] = pub; }
|
||||||
|
bool revoked(const std::string& signerId) const {
|
||||||
|
for (const auto& r : revokedSigners)
|
||||||
|
if (r == signerId) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const Key* find(const std::string& id) const {
|
||||||
|
const auto it = keys.find(id);
|
||||||
|
return it == keys.end() ? nullptr : &it->second;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// One model inside a package: where its bytes are and how they are sealed.
|
||||||
|
struct PayloadRef {
|
||||||
|
std::string id;
|
||||||
|
uint64_t off = 0;
|
||||||
|
uint64_t len = 0; // sealed length, tags included
|
||||||
|
uint32_t chunk = kDefaultChunk;
|
||||||
|
std::array<uint8_t, 4> nonceBase{};
|
||||||
|
};
|
||||||
|
|
||||||
|
// An opened package: everything but the model bytes, which are read on demand
|
||||||
|
// so a two-gigabyte model is never decrypted to answer "what is this?".
|
||||||
|
struct Package {
|
||||||
|
std::string path;
|
||||||
|
uint16_t version = 0;
|
||||||
|
uint16_t flags = 0;
|
||||||
|
std::string keyId;
|
||||||
|
Json header; // the cleartext header, as read
|
||||||
|
Json manifest; // decrypted
|
||||||
|
std::vector<PayloadRef> payloads;
|
||||||
|
Key contentKey{};
|
||||||
|
std::string signerId; // the release key that signed it
|
||||||
|
bool certExpired = false; // loaded anyway; see the header comment
|
||||||
|
std::string warning; // what a caller should show, if anything
|
||||||
|
|
||||||
|
bool multiModel() const { return (flags & kFlagMultiModel) != 0; }
|
||||||
|
// The model a reader should run when it is given no choice: the manifest's
|
||||||
|
// entry model, or the first one.
|
||||||
|
std::string entryPayloadId() const {
|
||||||
|
const auto models = manifest.value("models", Json::array());
|
||||||
|
for (const auto& m : models)
|
||||||
|
if (m.value("entry", false)) return m.value("payload", std::string());
|
||||||
|
if (!models.empty()) return models[0].value("payload", std::string());
|
||||||
|
return payloads.empty() ? std::string() : payloads.front().id;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
inline uint16_t rd16(const uint8_t* p) { return uint16_t(p[0] | (p[1] << 8)); }
|
||||||
|
inline uint32_t rd32(const uint8_t* p) {
|
||||||
|
return uint32_t(p[0]) | (uint32_t(p[1]) << 8) | (uint32_t(p[2]) << 16) |
|
||||||
|
(uint32_t(p[3]) << 24);
|
||||||
|
}
|
||||||
|
inline void wr16(std::string& out, uint16_t v) {
|
||||||
|
out.push_back(char(v & 0xff));
|
||||||
|
out.push_back(char(v >> 8));
|
||||||
|
}
|
||||||
|
inline void wr32(std::string& out, uint32_t v) {
|
||||||
|
for (int i = 0; i < 4; ++i) out.push_back(char((v >> (8 * i)) & 0xff));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::string b64(const std::vector<uint8_t>& v) {
|
||||||
|
return bits::base64Encode(std::string(v.begin(), v.end()));
|
||||||
|
}
|
||||||
|
inline std::vector<uint8_t> unb64(const std::string& s) {
|
||||||
|
std::vector<uint8_t> out;
|
||||||
|
if (!bits::base64Decode(s, out)) out.clear();
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The nonce for one chunk: a random per-payload base, then the index. Unique
|
||||||
|
// per (payload, chunk) under a content key that is unique per package.
|
||||||
|
inline std::array<uint8_t, aead::kNonceBytes> chunkNonce(
|
||||||
|
const std::array<uint8_t, 4>& base, uint64_t index) {
|
||||||
|
std::array<uint8_t, aead::kNonceBytes> n{};
|
||||||
|
std::memcpy(n.data(), base.data(), 4);
|
||||||
|
for (int i = 0; i < 8; ++i) n[4 + i] = uint8_t(index >> (8 * i));
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// What each chunk is bound to. Moving a chunk to another payload, another
|
||||||
|
// index or another package changes this, and the tag then fails.
|
||||||
|
inline std::string chunkAad(const std::string& keyId, const std::string& payloadId,
|
||||||
|
uint64_t index, uint64_t chunkCount) {
|
||||||
|
return "ansmodel/v1|" + keyId + "|" + payloadId + "|" + std::to_string(index) +
|
||||||
|
"|" + std::to_string(chunkCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The wrapping key from an X25519 shared secret. HMAC-SHA256 over the secret
|
||||||
|
// with a fixed label, which is HKDF-Extract: the raw secret is not uniform and
|
||||||
|
// must never be used as a key directly.
|
||||||
|
inline Key deriveWrapKey(const uint8_t shared[32], const std::string& keyId) {
|
||||||
|
const std::string salt = "ansmodel/v1|wrap|" + keyId;
|
||||||
|
const auto mac = bits::hmacSha256(
|
||||||
|
reinterpret_cast<const uint8_t*>(salt.data()), salt.size(), shared, 32);
|
||||||
|
Key k{};
|
||||||
|
std::memcpy(k.data(), mac.data(), k.size());
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The bytes a signature covers: the header with its own `sig` taken out, as
|
||||||
|
// canonical JSON. nlohmann orders object keys, so a reader that removes the
|
||||||
|
// same field and re-serialises reproduces exactly what the packer signed.
|
||||||
|
inline std::string signedBytes(Json header) {
|
||||||
|
header.erase("sig");
|
||||||
|
return header.dump();
|
||||||
|
}
|
||||||
|
|
||||||
|
// What the root vouches for: this release key, this id, until this date.
|
||||||
|
inline std::string certBytes(const Json& cert) {
|
||||||
|
return "ansmodel/v1|cert|" + cert.value("keyId", std::string()) + "|" +
|
||||||
|
cert.value("pub", std::string()) + "|" +
|
||||||
|
cert.value("notAfter", std::string());
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int64_t nowSeconds() {
|
||||||
|
return int64_t(std::chrono::duration_cast<std::chrono::seconds>(
|
||||||
|
std::chrono::system_clock::now().time_since_epoch())
|
||||||
|
.count());
|
||||||
|
}
|
||||||
|
|
||||||
|
// "2027-09-01T00:00:00Z" → seconds. Returns 0 when it cannot be read, which
|
||||||
|
// is treated as "no expiry stated" rather than "expired".
|
||||||
|
inline int64_t parseIso8601(const std::string& t) {
|
||||||
|
std::tm tm{};
|
||||||
|
if (t.size() < 19) return 0;
|
||||||
|
tm.tm_year = std::atoi(t.substr(0, 4).c_str()) - 1900;
|
||||||
|
tm.tm_mon = std::atoi(t.substr(5, 2).c_str()) - 1;
|
||||||
|
tm.tm_mday = std::atoi(t.substr(8, 2).c_str());
|
||||||
|
tm.tm_hour = std::atoi(t.substr(11, 2).c_str());
|
||||||
|
tm.tm_min = std::atoi(t.substr(14, 2).c_str());
|
||||||
|
tm.tm_sec = std::atoi(t.substr(17, 2).c_str());
|
||||||
|
if (tm.tm_year <= 0 || tm.tm_mon < 0 || tm.tm_mon > 11) return 0;
|
||||||
|
#if defined(_WIN32)
|
||||||
|
return int64_t(_mkgmtime(&tm));
|
||||||
|
#else
|
||||||
|
return int64_t(timegm(&tm));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
inline uint64_t chunkCountFor(uint64_t sealedLen, uint32_t chunk) {
|
||||||
|
const uint64_t perChunk = chunk + aead::kTagBytes;
|
||||||
|
return (sealedLen + perChunk - 1) / perChunk;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
// ── reading ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
// Open the header and manifest. Does not read a single model byte.
|
||||||
|
inline bool open(const std::string& path, const Keyring& ring, Package& out,
|
||||||
|
std::string& err) {
|
||||||
|
out = Package{};
|
||||||
|
out.path = path;
|
||||||
|
std::ifstream f(path, std::ios::binary);
|
||||||
|
if (!f) {
|
||||||
|
err = "cannot open " + path;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
uint8_t fixed[16];
|
||||||
|
f.read(reinterpret_cast<char*>(fixed), sizeof(fixed));
|
||||||
|
if (f.gcount() != std::streamsize(sizeof(fixed))) {
|
||||||
|
err = "not a model package: file is too short";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (std::memcmp(fixed, kMagic, sizeof(kMagic)) != 0) {
|
||||||
|
err = "not a model package: wrong magic";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out.version = detail::rd16(fixed + 8);
|
||||||
|
out.flags = detail::rd16(fixed + 10);
|
||||||
|
if (out.version > kVersion) {
|
||||||
|
err = "this package is format version " + std::to_string(out.version) +
|
||||||
|
", which needs a newer ANS engine";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const uint32_t headerLen = detail::rd32(fixed + 12);
|
||||||
|
if (headerLen == 0 || headerLen > 8u * 1024 * 1024) {
|
||||||
|
err = "header length is not plausible";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::string headerText(headerLen, '\0');
|
||||||
|
f.read(headerText.data(), headerLen);
|
||||||
|
if (f.gcount() != std::streamsize(headerLen)) {
|
||||||
|
err = "truncated: the header is not all there";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out.header = Json::parse(headerText, nullptr, false);
|
||||||
|
if (out.header.is_discarded()) {
|
||||||
|
err = "the header is not readable JSON";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── the signature, before anything is decrypted ────────────────────────
|
||||||
|
const Json sig = out.header.value("sig", Json::object());
|
||||||
|
if (sig.is_null() || sig.empty()) {
|
||||||
|
err = "the package is not signed";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const std::string sigAlg = sig.value("alg", std::string());
|
||||||
|
if (sigAlg == kSigEd25519) {
|
||||||
|
const Json cert = sig.value("cert", Json::object());
|
||||||
|
const std::string signerId = cert.value("keyId", std::string());
|
||||||
|
const std::string rootId = cert.value("rootKeyId", std::string());
|
||||||
|
const auto signerPub = detail::unb64(cert.value("pub", std::string()));
|
||||||
|
const auto rootSig = detail::unb64(cert.value("rootSig", std::string()));
|
||||||
|
const auto headerSig = detail::unb64(sig.value("value", std::string()));
|
||||||
|
if (signerPub.size() != 32 || rootSig.size() != 64 || headerSig.size() != 64) {
|
||||||
|
err = "the signature block is malformed";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (ring.revoked(signerId)) {
|
||||||
|
err = "this package was signed with a key that has been revoked (" +
|
||||||
|
signerId + ")";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const auto rootIt = ring.roots.find(rootId);
|
||||||
|
if (rootIt == ring.roots.end()) {
|
||||||
|
err = "the package is vouched for by an unknown root (" + rootId + ")";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// The root vouches for the release key; the release key signs the header.
|
||||||
|
const std::string cb = detail::certBytes(cert);
|
||||||
|
if (crypto_ed25519_check(rootSig.data(), rootIt->second.data(),
|
||||||
|
reinterpret_cast<const uint8_t*>(cb.data()),
|
||||||
|
cb.size()) != 0) {
|
||||||
|
err = "the signing key is not vouched for by the root it names";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const std::string sb = detail::signedBytes(out.header);
|
||||||
|
if (crypto_ed25519_check(headerSig.data(), signerPub.data(),
|
||||||
|
reinterpret_cast<const uint8_t*>(sb.data()),
|
||||||
|
sb.size()) != 0) {
|
||||||
|
err = "the package signature does not verify — it has been altered";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out.signerId = signerId;
|
||||||
|
// Expiry is a fact about the key's service, not about this package's
|
||||||
|
// authenticity, and an air-gapped clock drifts. Warn and load; strict
|
||||||
|
// mode is for sites that would rather fail closed.
|
||||||
|
const int64_t notAfter = detail::parseIso8601(cert.value("notAfter", std::string()));
|
||||||
|
const int64_t now = ring.nowSec > 0 ? ring.nowSec : detail::nowSeconds();
|
||||||
|
if (notAfter > 0 && now > notAfter) {
|
||||||
|
out.certExpired = true;
|
||||||
|
out.warning = "signed with a certificate that expired on " +
|
||||||
|
cert.value("notAfter", std::string()) + " (key " + signerId + ")";
|
||||||
|
if (ring.strictExpiry) {
|
||||||
|
err = out.warning + " — refused because strict expiry is on";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (sigAlg != "none") {
|
||||||
|
err = "the package is signed in a way this build does not know (" + sigAlg + ")";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Which key opens it. Every wrap is tried, so a package sealed to several
|
||||||
|
// keys opens on any engine holding one of them.
|
||||||
|
const auto wraps = out.header.value("wraps", Json::array());
|
||||||
|
if (wraps.empty()) {
|
||||||
|
err = "the package names no key";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::vector<std::string> unknown;
|
||||||
|
bool unwrapped = false;
|
||||||
|
for (const auto& w : wraps) {
|
||||||
|
const std::string keyId = w.value("keyId", std::string());
|
||||||
|
const std::string alg = w.value("alg", std::string());
|
||||||
|
const auto nonceBytes = detail::unb64(w.value("nonce", std::string()));
|
||||||
|
const auto ct = detail::unb64(w.value("ct", std::string()));
|
||||||
|
if (nonceBytes.size() != aead::kNonceBytes ||
|
||||||
|
ct.size() != aead::kKeyBytes + aead::kTagBytes) {
|
||||||
|
err = "the wrapped key is malformed";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::array<uint8_t, aead::kNonceBytes> nonce{};
|
||||||
|
std::memcpy(nonce.data(), nonceBytes.data(), nonce.size());
|
||||||
|
|
||||||
|
// The key that opens THIS wrap: either an X25519 shared secret against the
|
||||||
|
// ephemeral public key the packer left here, or — for packages written
|
||||||
|
// before the curve was vendored — the symmetric base key.
|
||||||
|
Key unwrapKey{};
|
||||||
|
if (alg == kWrapX25519) {
|
||||||
|
const auto it = ring.x25519.find(keyId);
|
||||||
|
if (it == ring.x25519.end()) {
|
||||||
|
unknown.push_back(keyId);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const auto epk = detail::unb64(w.value("epk", std::string()));
|
||||||
|
if (epk.size() != 32) {
|
||||||
|
err = "the wrap carries no usable ephemeral key";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
uint8_t shared[32];
|
||||||
|
crypto_x25519(shared, it->second.data(), epk.data());
|
||||||
|
unwrapKey = detail::deriveWrapKey(shared, keyId);
|
||||||
|
crypto_wipe(shared, sizeof(shared));
|
||||||
|
} else if (alg == kWrapSymmetric) {
|
||||||
|
const Key* k = ring.find(keyId);
|
||||||
|
if (!k) {
|
||||||
|
unknown.push_back(keyId);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
unwrapKey = *k;
|
||||||
|
} else {
|
||||||
|
err = "the package uses an unsupported wrap (" + alg + ")";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t> plain;
|
||||||
|
if (!aead::open(unwrapKey, nonce, "ansmodel/v1|wrap|" + keyId, ct, plain)) {
|
||||||
|
err = "the wrapped key does not open with " + keyId +
|
||||||
|
" — the file is damaged or was sealed to a different key";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::memcpy(out.contentKey.data(), plain.data(), out.contentKey.size());
|
||||||
|
out.keyId = keyId;
|
||||||
|
unwrapped = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!unwrapped) {
|
||||||
|
err = "this package needs a newer ANS engine: it is sealed to key " +
|
||||||
|
(unknown.empty() ? std::string("(unnamed)") : unknown.front());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where the sealed parts are.
|
||||||
|
for (const auto& p : out.header.value("payloads", Json::array())) {
|
||||||
|
PayloadRef r;
|
||||||
|
r.id = p.value("id", std::string());
|
||||||
|
r.off = p.value("off", uint64_t(0));
|
||||||
|
r.len = p.value("len", uint64_t(0));
|
||||||
|
r.chunk = p.value("chunk", kDefaultChunk);
|
||||||
|
const auto nb = detail::unb64(p.value("nonceBase", std::string()));
|
||||||
|
if (r.id.empty() || r.len == 0 || r.chunk == 0 || nb.size() != 4) {
|
||||||
|
err = "a payload entry is malformed";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::memcpy(r.nonceBase.data(), nb.data(), 4);
|
||||||
|
out.payloads.push_back(r);
|
||||||
|
}
|
||||||
|
if (out.payloads.empty()) {
|
||||||
|
err = "the package carries no model";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The manifest, sealed as a single chunk under the payload id "manifest".
|
||||||
|
const Json m = out.header.value("manifest", Json::object());
|
||||||
|
const uint64_t moff = m.value("off", uint64_t(0));
|
||||||
|
const uint64_t mlen = m.value("len", uint64_t(0));
|
||||||
|
const auto mnb = detail::unb64(m.value("nonceBase", std::string()));
|
||||||
|
if (mlen == 0 || mnb.size() != 4) {
|
||||||
|
err = "the manifest entry is malformed";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::vector<uint8_t> sealed(static_cast<size_t>(mlen), 0);
|
||||||
|
f.seekg(std::streamoff(moff));
|
||||||
|
f.read(reinterpret_cast<char*>(sealed.data()), std::streamsize(mlen));
|
||||||
|
if (f.gcount() != std::streamsize(mlen)) {
|
||||||
|
err = "truncated: the manifest is not all there";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::array<uint8_t, 4> mbase{};
|
||||||
|
std::memcpy(mbase.data(), mnb.data(), 4);
|
||||||
|
std::vector<uint8_t> manifestText;
|
||||||
|
if (!aead::open(out.contentKey, detail::chunkNonce(mbase, 0),
|
||||||
|
detail::chunkAad(out.keyId, "manifest", 0, 1), sealed,
|
||||||
|
manifestText)) {
|
||||||
|
err = "the manifest does not verify — the package has been altered";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out.manifest =
|
||||||
|
Json::parse(std::string(manifestText.begin(), manifestText.end()), nullptr,
|
||||||
|
false);
|
||||||
|
if (out.manifest.is_discarded()) {
|
||||||
|
err = "the manifest is not readable JSON";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt one model into memory. Chunk by chunk, each verified as it is read,
|
||||||
|
// so a damaged file fails on the chunk that is wrong rather than after the
|
||||||
|
// whole thing has been decrypted.
|
||||||
|
inline bool readPayload(const Package& pkg, const std::string& payloadId,
|
||||||
|
std::vector<uint8_t>& out, std::string& err) {
|
||||||
|
const PayloadRef* ref = nullptr;
|
||||||
|
for (const auto& p : pkg.payloads)
|
||||||
|
if (p.id == payloadId) ref = &p;
|
||||||
|
if (!ref) {
|
||||||
|
err = "no model called " + payloadId + " in this package";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::ifstream f(pkg.path, std::ios::binary);
|
||||||
|
if (!f) {
|
||||||
|
err = "cannot reopen " + pkg.path;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
f.seekg(std::streamoff(ref->off));
|
||||||
|
const uint64_t chunks = detail::chunkCountFor(ref->len, ref->chunk);
|
||||||
|
out.clear();
|
||||||
|
out.reserve(size_t(ref->len));
|
||||||
|
std::vector<uint8_t> sealed;
|
||||||
|
std::vector<uint8_t> plain;
|
||||||
|
uint64_t remaining = ref->len;
|
||||||
|
for (uint64_t i = 0; i < chunks; ++i) {
|
||||||
|
const uint64_t take =
|
||||||
|
remaining < uint64_t(ref->chunk) + aead::kTagBytes
|
||||||
|
? remaining
|
||||||
|
: uint64_t(ref->chunk) + aead::kTagBytes;
|
||||||
|
sealed.assign(size_t(take), 0);
|
||||||
|
f.read(reinterpret_cast<char*>(sealed.data()), std::streamsize(take));
|
||||||
|
if (f.gcount() != std::streamsize(take)) {
|
||||||
|
err = "truncated: model " + payloadId + " stops at chunk " +
|
||||||
|
std::to_string(i);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!aead::open(pkg.contentKey, detail::chunkNonce(ref->nonceBase, i),
|
||||||
|
detail::chunkAad(pkg.keyId, ref->id, i, chunks), sealed,
|
||||||
|
plain)) {
|
||||||
|
err = "chunk " + std::to_string(i) + " of " + payloadId +
|
||||||
|
" does not verify — the package has been altered";
|
||||||
|
out.clear();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out.insert(out.end(), plain.begin(), plain.end());
|
||||||
|
remaining -= take;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── writing (tools and tests) ─────────────────────────────────────────────
|
||||||
|
//
|
||||||
|
// The product's packages are written by the training box. This exists so the
|
||||||
|
// reader can be tested against files built by something other than itself,
|
||||||
|
// and so a converter can turn today's zip bundles into packages.
|
||||||
|
|
||||||
|
struct PackModel {
|
||||||
|
std::string id; // payload id, e.g. "p0"
|
||||||
|
std::vector<uint8_t> bytes;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PackRequest {
|
||||||
|
std::string keyId;
|
||||||
|
Key key{}; // symmetric base key (legacy wrap)
|
||||||
|
// Wrap to a base PUBLIC key instead, which is what a real packer does: it
|
||||||
|
// can then produce packages it cannot itself open.
|
||||||
|
bool useX25519 = false;
|
||||||
|
Key basePublic{};
|
||||||
|
// Sign as a release key the root vouches for. Empty signerSecret writes an
|
||||||
|
// unsigned package, which only tests should ever want.
|
||||||
|
std::string signerId;
|
||||||
|
std::vector<uint8_t> signerSecret; // 64 bytes, RFC 8032
|
||||||
|
std::vector<uint8_t> signerPublic; // 32 bytes
|
||||||
|
std::string rootKeyId;
|
||||||
|
std::vector<uint8_t> rootSig; // 64 bytes over the certificate
|
||||||
|
std::string notAfter; // ISO-8601, may be empty
|
||||||
|
Json manifest;
|
||||||
|
Json card; // optional, counts only
|
||||||
|
std::vector<PackModel> 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;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline bool pack(const std::string& path, const PackRequest& req,
|
||||||
|
std::string& err) {
|
||||||
|
if (req.models.empty()) {
|
||||||
|
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.
|
||||||
|
Key contentKey{};
|
||||||
|
for (size_t i = 0; i < contentKey.size(); ++i)
|
||||||
|
contentKey[i] = uint8_t(req.nonceSeed * 31 + i * 7 + 11);
|
||||||
|
|
||||||
|
auto sealChunked = [&](const std::vector<uint8_t>& plain,
|
||||||
|
const std::string& payloadId,
|
||||||
|
const std::array<uint8_t, 4>& base,
|
||||||
|
std::string& blob) {
|
||||||
|
const uint64_t chunks =
|
||||||
|
plain.empty() ? 1 : (plain.size() + req.chunk - 1) / req.chunk;
|
||||||
|
for (uint64_t i = 0; i < chunks; ++i) {
|
||||||
|
const size_t from = size_t(i * req.chunk);
|
||||||
|
const size_t take = std::min<size_t>(req.chunk, plain.size() - from);
|
||||||
|
std::vector<uint8_t> part(plain.begin() + from, plain.begin() + from + take);
|
||||||
|
const auto sealed =
|
||||||
|
aead::seal(contentKey, detail::chunkNonce(base, i),
|
||||||
|
detail::chunkAad(req.keyId, payloadId, i, chunks), part);
|
||||||
|
blob.append(reinterpret_cast<const char*>(sealed.data()), sealed.size());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// The manifest first, then each model, so the header can name their offsets.
|
||||||
|
const std::string manifestText = req.manifest.dump();
|
||||||
|
const std::vector<uint8_t> manifestBytes(manifestText.begin(),
|
||||||
|
manifestText.end());
|
||||||
|
std::array<uint8_t, 4> manifestBase{uint8_t(req.nonceSeed + 1), 2, 3, 4};
|
||||||
|
std::string manifestBlob;
|
||||||
|
{
|
||||||
|
const auto sealed =
|
||||||
|
aead::seal(contentKey, detail::chunkNonce(manifestBase, 0),
|
||||||
|
detail::chunkAad(req.keyId, "manifest", 0, 1),
|
||||||
|
manifestBytes);
|
||||||
|
manifestBlob.assign(reinterpret_cast<const char*>(sealed.data()),
|
||||||
|
sealed.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> blobs;
|
||||||
|
std::vector<std::array<uint8_t, 4>> bases;
|
||||||
|
for (size_t i = 0; i < req.models.size(); ++i) {
|
||||||
|
std::array<uint8_t, 4> base{uint8_t(req.nonceSeed + 10 + i), 5, 6, 7};
|
||||||
|
std::string blob;
|
||||||
|
sealChunked(req.models[i].bytes, req.models[i].id, base, 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<uint8_t, aead::kNonceBytes> wrapNonce{};
|
||||||
|
for (size_t i = 0; i < wrapNonce.size(); ++i)
|
||||||
|
wrapNonce[i] = uint8_t(req.nonceSeed + 100 + i);
|
||||||
|
const std::vector<uint8_t> keyPlain(contentKey.begin(), contentKey.end());
|
||||||
|
|
||||||
|
Key wrapKey = req.key;
|
||||||
|
std::vector<uint8_t> epk;
|
||||||
|
if (req.useX25519) {
|
||||||
|
// 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];
|
||||||
|
crypto_x25519(shared, esk.data(), req.basePublic.data());
|
||||||
|
wrapKey = detail::deriveWrapKey(shared, req.keyId);
|
||||||
|
crypto_wipe(shared, sizeof(shared));
|
||||||
|
crypto_wipe(esk.data(), esk.size());
|
||||||
|
}
|
||||||
|
const auto wrapped = aead::seal(wrapKey, wrapNonce,
|
||||||
|
"ansmodel/v1|wrap|" + req.keyId, keyPlain);
|
||||||
|
|
||||||
|
// Header offsets are known once the sizes are: the header is written first,
|
||||||
|
// so its own length has to be settled before the offsets inside it. Build it
|
||||||
|
// with placeholder offsets, measure, then fill them in — the size cannot
|
||||||
|
// change, because the numbers are written with a fixed width.
|
||||||
|
auto buildHeader = [&](uint64_t headerLen) {
|
||||||
|
Json h;
|
||||||
|
h["format"] = "ansmodel";
|
||||||
|
h["version"] = int(kVersion);
|
||||||
|
Json wrap{{"keyId", req.keyId},
|
||||||
|
{"alg", req.useX25519 ? kWrapX25519 : kWrapSymmetric},
|
||||||
|
{"nonce", detail::b64(std::vector<uint8_t>(wrapNonce.begin(),
|
||||||
|
wrapNonce.end()))},
|
||||||
|
{"ct", detail::b64(wrapped)}};
|
||||||
|
if (req.useX25519) wrap["epk"] = detail::b64(epk);
|
||||||
|
h["wraps"] = Json::array({wrap});
|
||||||
|
uint64_t at = 16 + headerLen;
|
||||||
|
h["manifest"] = Json{{"off", at},
|
||||||
|
{"len", uint64_t(manifestBlob.size())},
|
||||||
|
{"nonceBase", detail::b64(std::vector<uint8_t>(
|
||||||
|
manifestBase.begin(), manifestBase.end()))}};
|
||||||
|
at += manifestBlob.size();
|
||||||
|
Json payloads = Json::array();
|
||||||
|
for (size_t i = 0; i < blobs.size(); ++i) {
|
||||||
|
payloads.push_back(Json{
|
||||||
|
{"id", req.models[i].id},
|
||||||
|
{"off", at},
|
||||||
|
{"len", uint64_t(blobs[i].size())},
|
||||||
|
{"chunk", req.chunk},
|
||||||
|
{"nonceBase", detail::b64(std::vector<uint8_t>(bases[i].begin(),
|
||||||
|
bases[i].end()))},
|
||||||
|
{"sha256", bits::sha256Hex(std::string(blobs[i].begin(), blobs[i].end()))}});
|
||||||
|
at += blobs[i].size();
|
||||||
|
}
|
||||||
|
h["payloads"] = payloads;
|
||||||
|
if (!req.card.is_null()) h["card"] = req.card;
|
||||||
|
// The signature is written last, over everything above: `sig` is added
|
||||||
|
// with a placeholder of the exact length the real one takes, so signing
|
||||||
|
// cannot change the header's size and the offsets stay true.
|
||||||
|
if (req.signerSecret.size() == 64 && req.signerPublic.size() == 32) {
|
||||||
|
h["sig"] = Json{{"alg", kSigEd25519},
|
||||||
|
{"value", std::string(88, 'A')},
|
||||||
|
{"cert", Json{{"keyId", req.signerId},
|
||||||
|
{"pub", detail::b64(req.signerPublic)},
|
||||||
|
{"notAfter", req.notAfter},
|
||||||
|
{"rootKeyId", req.rootKeyId},
|
||||||
|
{"rootSig", detail::b64(req.rootSig)}}}};
|
||||||
|
} else {
|
||||||
|
h["sig"] = Json{{"alg", "none"}, {"value", ""}};
|
||||||
|
}
|
||||||
|
return h;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Two passes: the first measures, the second is written. Offsets are plain
|
||||||
|
// JSON numbers, so a longer offset could lengthen the header — hence the
|
||||||
|
// loop, which settles in one extra step.
|
||||||
|
std::string headerText = buildHeader(0).dump();
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
const std::string next = buildHeader(headerText.size()).dump();
|
||||||
|
if (next.size() == headerText.size()) {
|
||||||
|
headerText = next;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
headerText = next;
|
||||||
|
}
|
||||||
|
Json header = buildHeader(headerText.size());
|
||||||
|
if (req.signerSecret.size() == 64) {
|
||||||
|
// Sign the header as the reader will read it: without `sig`, canonically
|
||||||
|
// serialised. The placeholder above keeps the length identical, so putting
|
||||||
|
// the real signature in changes no offset.
|
||||||
|
const std::string sb = detail::signedBytes(header);
|
||||||
|
std::vector<uint8_t> value(64, 0);
|
||||||
|
crypto_ed25519_sign(value.data(), req.signerSecret.data(),
|
||||||
|
reinterpret_cast<const uint8_t*>(sb.data()), sb.size());
|
||||||
|
header["sig"]["value"] = detail::b64(value);
|
||||||
|
}
|
||||||
|
headerText = header.dump();
|
||||||
|
|
||||||
|
std::string file(kMagic, sizeof(kMagic));
|
||||||
|
detail::wr16(file, kVersion);
|
||||||
|
detail::wr16(file, req.models.size() > 1 ? kFlagMultiModel : uint16_t(0));
|
||||||
|
detail::wr32(file, uint32_t(headerText.size()));
|
||||||
|
file += headerText;
|
||||||
|
file += manifestBlob;
|
||||||
|
for (const auto& b : blobs) file += b;
|
||||||
|
|
||||||
|
std::ofstream o(path, std::ios::binary | std::ios::trunc);
|
||||||
|
if (!o) {
|
||||||
|
err = "cannot write " + path;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
o.write(file.data(), std::streamsize(file.size()));
|
||||||
|
if (!o) {
|
||||||
|
err = "failed while writing " + path;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace modelpkg
|
||||||
|
} // namespace ANSCENTER
|
||||||
|
|
||||||
|
#endif // ANSMODELPACKAGE_H
|
||||||
@@ -0,0 +1,326 @@
|
|||||||
|
#ifndef ANSPACKAGEBITS_H
|
||||||
|
#define ANSPACKAGEBITS_H
|
||||||
|
#pragma once
|
||||||
|
// ANSPackageBits — base64, SHA-256 and HMAC-SHA256 for the model package.
|
||||||
|
//
|
||||||
|
// TAKEN FROM ANSMINDCORE (src/core/base64.hpp, src/core/sha256.hpp) unchanged,
|
||||||
|
// for the same reason the AEAD is: a package written by one product must open
|
||||||
|
// in the other, and two implementations of the same transform are two chances
|
||||||
|
// to disagree. The suite checks these against the published vectors.
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace ANSCENTER {
|
||||||
|
namespace bits {
|
||||||
|
|
||||||
|
// core/base64 — RFC 4648 base64, encode and decode.
|
||||||
|
//
|
||||||
|
// Needed by ONVIF's WS-Security UsernameToken (the nonce and the password
|
||||||
|
// digest both travel base64-encoded) and useful anywhere bytes must cross a
|
||||||
|
// text channel. Decode is strict about alphabet and padding: this parses
|
||||||
|
// values that arrive from a camera on the network, and quietly accepting
|
||||||
|
// malformed input is how a length assumption downstream turns into a bug.
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// ── base64 ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
inline std::string base64Encode(const uint8_t* p, size_t n) {
|
||||||
|
static const char* kAlpha =
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
std::string out;
|
||||||
|
out.reserve(((n + 2) / 3) * 4);
|
||||||
|
size_t i = 0;
|
||||||
|
for (; i + 2 < n; i += 3) {
|
||||||
|
const uint32_t v = uint32_t(p[i]) << 16 | uint32_t(p[i + 1]) << 8 | p[i + 2];
|
||||||
|
out += kAlpha[(v >> 18) & 0x3F];
|
||||||
|
out += kAlpha[(v >> 12) & 0x3F];
|
||||||
|
out += kAlpha[(v >> 6) & 0x3F];
|
||||||
|
out += kAlpha[v & 0x3F];
|
||||||
|
}
|
||||||
|
if (i + 1 == n) {
|
||||||
|
const uint32_t v = uint32_t(p[i]) << 16;
|
||||||
|
out += kAlpha[(v >> 18) & 0x3F];
|
||||||
|
out += kAlpha[(v >> 12) & 0x3F];
|
||||||
|
out += "==";
|
||||||
|
} else if (i + 2 == n) {
|
||||||
|
const uint32_t v = uint32_t(p[i]) << 16 | uint32_t(p[i + 1]) << 8;
|
||||||
|
out += kAlpha[(v >> 18) & 0x3F];
|
||||||
|
out += kAlpha[(v >> 12) & 0x3F];
|
||||||
|
out += kAlpha[(v >> 6) & 0x3F];
|
||||||
|
out += '=';
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
inline std::string base64Encode(const std::string& s) {
|
||||||
|
return base64Encode(reinterpret_cast<const uint8_t*>(s.data()), s.size());
|
||||||
|
}
|
||||||
|
template <size_t N>
|
||||||
|
inline std::string base64Encode(const std::array<uint8_t, N>& a) {
|
||||||
|
return base64Encode(a.data(), a.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns false on any character outside the alphabet, bad padding, or a
|
||||||
|
// length that is not a multiple of 4. Whitespace is skipped: XML puts
|
||||||
|
// newlines inside base64 elements.
|
||||||
|
inline bool base64Decode(const std::string& in, std::vector<uint8_t>& out) {
|
||||||
|
auto val = [](char c) -> int {
|
||||||
|
if (c >= 'A' && c <= 'Z') return c - 'A';
|
||||||
|
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
|
||||||
|
if (c >= '0' && c <= '9') return c - '0' + 52;
|
||||||
|
if (c == '+') return 62;
|
||||||
|
if (c == '/') return 63;
|
||||||
|
return -1;
|
||||||
|
};
|
||||||
|
constexpr int kPad = -2;
|
||||||
|
out.clear();
|
||||||
|
int quad[4] = {0, 0, 0, 0};
|
||||||
|
int have = 0;
|
||||||
|
bool padded = false; // once padding starts, only more padding may follow
|
||||||
|
for (char c : in) {
|
||||||
|
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') continue;
|
||||||
|
int v;
|
||||||
|
if (c == '=') {
|
||||||
|
v = kPad;
|
||||||
|
padded = true;
|
||||||
|
} else {
|
||||||
|
if (padded) return false; // data after padding
|
||||||
|
v = val(c);
|
||||||
|
if (v < 0) return false;
|
||||||
|
}
|
||||||
|
quad[have++] = v;
|
||||||
|
if (have < 4) continue;
|
||||||
|
|
||||||
|
// Padding is only legal in the last one or two positions.
|
||||||
|
if (quad[0] == kPad || quad[1] == kPad) return false;
|
||||||
|
if (quad[2] == kPad && quad[3] != kPad) return false;
|
||||||
|
const int pad = (quad[2] == kPad ? 2 : 0) + (quad[2] != kPad &&
|
||||||
|
quad[3] == kPad ? 1 : 0);
|
||||||
|
const uint32_t acc = uint32_t(quad[0]) << 18 | uint32_t(quad[1]) << 12 |
|
||||||
|
uint32_t(pad >= 2 ? 0 : quad[2]) << 6 |
|
||||||
|
uint32_t(pad >= 1 ? 0 : quad[3]);
|
||||||
|
out.push_back(uint8_t(acc >> 16));
|
||||||
|
if (pad < 2) out.push_back(uint8_t(acc >> 8));
|
||||||
|
if (pad < 1) out.push_back(uint8_t(acc));
|
||||||
|
have = 0;
|
||||||
|
}
|
||||||
|
return have == 0; // a trailing partial group is malformed
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// core/sha256 — SHA-256 and HMAC-SHA256, in-tree.
|
||||||
|
//
|
||||||
|
// WHY IN-TREE. The only consumer is AWS Signature Version 4 request signing
|
||||||
|
// for the S3 evidence store. Linking OpenSSL for two hash functions would put
|
||||||
|
// a versioned crypto dependency into the worker's Depends on every platform
|
||||||
|
// we ship — the same trap the bundled ONNX Runtime exists to avoid — in
|
||||||
|
// exchange for ~150 lines of algorithm that has fixed, published test
|
||||||
|
// vectors. The suite checks this against the FIPS 180-4 and RFC 4231 vectors,
|
||||||
|
// so it is verified rather than merely present.
|
||||||
|
//
|
||||||
|
// NOT a general crypto library. It exists to sign requests: there is no
|
||||||
|
// constant-time comparison here, and nothing in this file should ever be
|
||||||
|
// used for password hashing or key derivation from a low-entropy secret.
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// ── SHA-256 and HMAC-SHA256 ──────────────────────────────────────────────
|
||||||
|
|
||||||
|
class Sha256 {
|
||||||
|
public:
|
||||||
|
static constexpr size_t kDigestLen = 32;
|
||||||
|
|
||||||
|
Sha256() { reset(); }
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
len_ = 0;
|
||||||
|
bufLen_ = 0;
|
||||||
|
static const uint32_t kInit[8] = {0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u,
|
||||||
|
0xa54ff53au, 0x510e527fu, 0x9b05688cu,
|
||||||
|
0x1f83d9abu, 0x5be0cd19u};
|
||||||
|
std::memcpy(h_, kInit, sizeof h_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void update(const uint8_t* p, size_t n) {
|
||||||
|
len_ += n;
|
||||||
|
while (n) {
|
||||||
|
const size_t take = (n < 64 - bufLen_) ? n : (64 - bufLen_);
|
||||||
|
std::memcpy(buf_ + bufLen_, p, take);
|
||||||
|
bufLen_ += take;
|
||||||
|
p += take;
|
||||||
|
n -= take;
|
||||||
|
if (bufLen_ == 64) {
|
||||||
|
block(buf_);
|
||||||
|
bufLen_ = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void update(const std::string& s) {
|
||||||
|
update(reinterpret_cast<const uint8_t*>(s.data()), s.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consumes the state; call reset() to reuse.
|
||||||
|
void finish(uint8_t out[kDigestLen]) {
|
||||||
|
const uint64_t bits = uint64_t(len_) * 8; // captured BEFORE padding
|
||||||
|
uint8_t b = 0x80;
|
||||||
|
update(&b, 1);
|
||||||
|
b = 0;
|
||||||
|
while (bufLen_ != 56) update(&b, 1);
|
||||||
|
uint8_t lenBe[8];
|
||||||
|
for (int i = 0; i < 8; ++i) lenBe[i] = uint8_t(bits >> (56 - 8 * i));
|
||||||
|
update(lenBe, 8);
|
||||||
|
for (int i = 0; i < 8; ++i) {
|
||||||
|
out[i * 4 + 0] = uint8_t(h_[i] >> 24);
|
||||||
|
out[i * 4 + 1] = uint8_t(h_[i] >> 16);
|
||||||
|
out[i * 4 + 2] = uint8_t(h_[i] >> 8);
|
||||||
|
out[i * 4 + 3] = uint8_t(h_[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static uint32_t rotr(uint32_t x, int n) {
|
||||||
|
return (x >> n) | (x << (32 - n));
|
||||||
|
}
|
||||||
|
|
||||||
|
void block(const uint8_t* p) {
|
||||||
|
// First 32 bits of the fractional parts of the cube roots of the first
|
||||||
|
// 64 primes (FIPS 180-4 §4.2.2).
|
||||||
|
static const uint32_t k[64] = {
|
||||||
|
0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u, 0x3956c25bu,
|
||||||
|
0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u, 0xd807aa98u, 0x12835b01u,
|
||||||
|
0x243185beu, 0x550c7dc3u, 0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u,
|
||||||
|
0xc19bf174u, 0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu,
|
||||||
|
0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau, 0x983e5152u,
|
||||||
|
0xa831c66du, 0xb00327c8u, 0xbf597fc7u, 0xc6e00bf3u, 0xd5a79147u,
|
||||||
|
0x06ca6351u, 0x14292967u, 0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu,
|
||||||
|
0x53380d13u, 0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u,
|
||||||
|
0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u, 0xd192e819u,
|
||||||
|
0xd6990624u, 0xf40e3585u, 0x106aa070u, 0x19a4c116u, 0x1e376c08u,
|
||||||
|
0x2748774cu, 0x34b0bcb5u, 0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu,
|
||||||
|
0x682e6ff3u, 0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u,
|
||||||
|
0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u};
|
||||||
|
uint32_t w[64];
|
||||||
|
for (int i = 0; i < 16; ++i)
|
||||||
|
w[i] = uint32_t(p[i * 4]) << 24 | uint32_t(p[i * 4 + 1]) << 16 |
|
||||||
|
uint32_t(p[i * 4 + 2]) << 8 | uint32_t(p[i * 4 + 3]);
|
||||||
|
for (int i = 16; i < 64; ++i) {
|
||||||
|
const uint32_t s0 =
|
||||||
|
rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
||||||
|
const uint32_t s1 =
|
||||||
|
rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
||||||
|
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
||||||
|
}
|
||||||
|
uint32_t a = h_[0], b = h_[1], c = h_[2], d = h_[3];
|
||||||
|
uint32_t e = h_[4], f = h_[5], g = h_[6], hh = h_[7];
|
||||||
|
for (int i = 0; i < 64; ++i) {
|
||||||
|
const uint32_t S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
||||||
|
const uint32_t ch = (e & f) ^ (~e & g);
|
||||||
|
const uint32_t t1 = hh + S1 + ch + k[i] + w[i];
|
||||||
|
const uint32_t S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
||||||
|
const uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
|
||||||
|
const uint32_t t2 = S0 + maj;
|
||||||
|
hh = g; g = f; f = e; e = d + t1;
|
||||||
|
d = c; c = b; b = a; a = t1 + t2;
|
||||||
|
}
|
||||||
|
h_[0] += a; h_[1] += b; h_[2] += c; h_[3] += d;
|
||||||
|
h_[4] += e; h_[5] += f; h_[6] += g; h_[7] += hh;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t h_[8];
|
||||||
|
uint8_t buf_[64];
|
||||||
|
size_t bufLen_ = 0;
|
||||||
|
uint64_t len_ = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
using Digest256 = std::array<uint8_t, 32>;
|
||||||
|
|
||||||
|
inline Digest256 sha256(const uint8_t* p, size_t n) {
|
||||||
|
Sha256 s;
|
||||||
|
s.update(p, n);
|
||||||
|
Digest256 out{};
|
||||||
|
s.finish(out.data());
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
inline Digest256 sha256(const std::string& s) {
|
||||||
|
return sha256(reinterpret_cast<const uint8_t*>(s.data()), s.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::string toHex(const uint8_t* p, size_t n) {
|
||||||
|
static const char* kHex = "0123456789abcdef";
|
||||||
|
std::string out;
|
||||||
|
out.reserve(n * 2);
|
||||||
|
for (size_t i = 0; i < n; ++i) {
|
||||||
|
out += kHex[p[i] >> 4];
|
||||||
|
out += kHex[p[i] & 0xF];
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
inline std::string toHex(const Digest256& d) {
|
||||||
|
return toHex(d.data(), d.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::string sha256Hex(const uint8_t* p, size_t n) {
|
||||||
|
return toHex(sha256(p, n));
|
||||||
|
}
|
||||||
|
inline std::string sha256Hex(const std::string& s) {
|
||||||
|
return toHex(sha256(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
// RFC 2104 HMAC with SHA-256.
|
||||||
|
inline Digest256 hmacSha256(const uint8_t* key, size_t keyLen,
|
||||||
|
const uint8_t* msg, size_t msgLen) {
|
||||||
|
uint8_t k[64] = {0};
|
||||||
|
if (keyLen > 64) {
|
||||||
|
Sha256 s;
|
||||||
|
s.update(key, keyLen);
|
||||||
|
s.finish(k); // remaining 32 bytes stay zero-padded
|
||||||
|
} else {
|
||||||
|
std::memcpy(k, key, keyLen);
|
||||||
|
}
|
||||||
|
uint8_t ipad[64], opad[64];
|
||||||
|
for (int i = 0; i < 64; ++i) {
|
||||||
|
ipad[i] = uint8_t(k[i] ^ 0x36);
|
||||||
|
opad[i] = uint8_t(k[i] ^ 0x5c);
|
||||||
|
}
|
||||||
|
uint8_t inner[32];
|
||||||
|
{
|
||||||
|
Sha256 s;
|
||||||
|
s.update(ipad, 64);
|
||||||
|
s.update(msg, msgLen);
|
||||||
|
s.finish(inner);
|
||||||
|
}
|
||||||
|
Digest256 out{};
|
||||||
|
{
|
||||||
|
Sha256 s;
|
||||||
|
s.update(opad, 64);
|
||||||
|
s.update(inner, 32);
|
||||||
|
s.finish(out.data());
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
inline Digest256 hmacSha256(const std::string& key, const std::string& msg) {
|
||||||
|
return hmacSha256(reinterpret_cast<const uint8_t*>(key.data()), key.size(),
|
||||||
|
reinterpret_cast<const uint8_t*>(msg.data()), msg.size());
|
||||||
|
}
|
||||||
|
inline Digest256 hmacSha256(const Digest256& key, const std::string& msg) {
|
||||||
|
return hmacSha256(key.data(), key.size(),
|
||||||
|
reinterpret_cast<const uint8_t*>(msg.data()), msg.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace bits
|
||||||
|
} // namespace ANSCENTER
|
||||||
|
|
||||||
|
#endif // ANSPACKAGEBITS_H
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
# ANSModelPackage — reading an ANS model package (.ansmodel).
|
||||||
|
#
|
||||||
|
# Header-only apart from the vendored curves, and deliberately free of the
|
||||||
|
# heavy dependencies around it: no CUDA, no OpenCV, no LabVIEW. That is what
|
||||||
|
# lets it build — and be tested — on every platform, including the ones where
|
||||||
|
# the engine layer does not build at all.
|
||||||
|
add_library(ANSModelPackage STATIC
|
||||||
|
${CMAKE_SOURCE_DIR}/3rdparty/monocypher/monocypher.c
|
||||||
|
${CMAKE_SOURCE_DIR}/3rdparty/monocypher/monocypher-ed25519.c
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(ANSModelPackage PUBLIC
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
|
${CMAKE_SOURCE_DIR}/3rdparty/monocypher
|
||||||
|
)
|
||||||
|
|
||||||
|
# nlohmann/json comes from vcpkg on Windows and the system elsewhere, the same
|
||||||
|
# way ANSFR already takes it.
|
||||||
|
if(WIN32)
|
||||||
|
target_include_directories(ANSModelPackage PUBLIC ${VCPKG_INCLUDE_DIR})
|
||||||
|
else()
|
||||||
|
find_path(ANSCORE_NLOHMANN_INCLUDE nlohmann/json.hpp
|
||||||
|
HINTS /opt/homebrew/include /usr/local/include /usr/include)
|
||||||
|
if(ANSCORE_NLOHMANN_INCLUDE)
|
||||||
|
target_include_directories(ANSModelPackage PUBLIC ${ANSCORE_NLOHMANN_INCLUDE})
|
||||||
|
else()
|
||||||
|
message(WARNING "nlohmann/json.hpp not found — ANSModelPackage will not build")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set_target_properties(ANSModelPackage PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
||||||
@@ -0,0 +1,263 @@
|
|||||||
|
// ANSModelPackage-UnitTest — the model package format, and its agreement with
|
||||||
|
// the other implementation.
|
||||||
|
//
|
||||||
|
// Usage: ANSModelPackage-UnitTest [data-dir]
|
||||||
|
//
|
||||||
|
// The interop fixture in data/ was written by ANSMINDCORE's packer. Opening it
|
||||||
|
// here is what makes "the two products read the same format" a fact rather
|
||||||
|
// than an intention: a change to either implementation that alters the wire
|
||||||
|
// shows up as this test failing.
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "ANSModelPackage.h"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include "monocypher-ed25519.h"
|
||||||
|
#include "monocypher.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
using namespace ANSCENTER;
|
||||||
|
using modelpkg::Json;
|
||||||
|
|
||||||
|
static int gChecks = 0, gFailures = 0;
|
||||||
|
static void CHECK(bool ok, const char* what) {
|
||||||
|
++gChecks;
|
||||||
|
if (!ok) {
|
||||||
|
++gFailures;
|
||||||
|
std::printf("FAIL %s\n", what);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The fixture's keys, by the rules its generator recorded (see data/README).
|
||||||
|
struct Identity {
|
||||||
|
modelpkg::Key basePriv{}, basePub{};
|
||||||
|
std::vector<uint8_t> rootSk = std::vector<uint8_t>(64);
|
||||||
|
std::vector<uint8_t> rootPk = std::vector<uint8_t>(32);
|
||||||
|
std::vector<uint8_t> signerSk = std::vector<uint8_t>(64);
|
||||||
|
std::vector<uint8_t> signerPk = std::vector<uint8_t>(32);
|
||||||
|
};
|
||||||
|
|
||||||
|
static Identity fixtureIdentity() {
|
||||||
|
Identity id;
|
||||||
|
std::vector<uint8_t> seedRoot(32), seedSigner(32);
|
||||||
|
for (size_t i = 0; i < 32; ++i) {
|
||||||
|
id.basePriv[i] = uint8_t(i * 3 + 9);
|
||||||
|
seedRoot[i] = uint8_t(i * 7 + 1);
|
||||||
|
seedSigner[i] = uint8_t(i * 11 + 5);
|
||||||
|
}
|
||||||
|
crypto_x25519_public_key(id.basePub.data(), id.basePriv.data());
|
||||||
|
crypto_ed25519_key_pair(id.rootSk.data(), id.rootPk.data(), seedRoot.data());
|
||||||
|
crypto_ed25519_key_pair(id.signerSk.data(), id.signerPk.data(), seedSigner.data());
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
static modelpkg::Keyring fixtureKeyring(const Identity& id) {
|
||||||
|
modelpkg::Keyring ring;
|
||||||
|
ring.addX25519("ansk-v1-base", id.basePriv);
|
||||||
|
modelpkg::Key rootPub{};
|
||||||
|
std::memcpy(rootPub.data(), id.rootPk.data(), rootPub.size());
|
||||||
|
ring.addRoot("ansroot-v1", rootPub);
|
||||||
|
ring.nowSec = 1788000000; // a fixed "now", so the fixture does not age
|
||||||
|
return ring;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── the primitives, against their own published numbers ───────────────────
|
||||||
|
static void testVectors() {
|
||||||
|
auto hex = [](const std::string& h) {
|
||||||
|
std::vector<uint8_t> v;
|
||||||
|
for (size_t i = 0; i + 1 < h.size(); i += 2)
|
||||||
|
v.push_back(uint8_t(std::stoi(h.substr(i, 2), nullptr, 16)));
|
||||||
|
return v;
|
||||||
|
};
|
||||||
|
const std::string text =
|
||||||
|
"Ladies and Gentlemen of the class of '99: If I could offer you only one "
|
||||||
|
"tip for the future, sunscreen would be it.";
|
||||||
|
const auto key = hex("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f");
|
||||||
|
const auto aad = hex("50515253c0c1c2c3c4c5c6c7");
|
||||||
|
const auto nonce = hex("070000004041424344454647");
|
||||||
|
const std::vector<uint8_t> plain(text.begin(), text.end());
|
||||||
|
std::vector<uint8_t> ct(plain.size());
|
||||||
|
uint8_t tag[16];
|
||||||
|
aead::aeadSeal(key.data(), nonce.data(), aad.data(), aad.size(), plain.data(),
|
||||||
|
plain.size(), ct.data(), tag);
|
||||||
|
CHECK(std::memcmp(tag, hex("1ae10b594f09e26a7e902ecbd0600691").data(), 16) == 0,
|
||||||
|
"RFC 8439 §2.8.2: the AEAD tag is the published one");
|
||||||
|
|
||||||
|
const auto aSec = hex("77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
|
||||||
|
const auto bSec = hex("5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb");
|
||||||
|
std::vector<uint8_t> aPub(32), bPub(32), s1(32), s2(32);
|
||||||
|
crypto_x25519_public_key(aPub.data(), aSec.data());
|
||||||
|
crypto_x25519_public_key(bPub.data(), bSec.data());
|
||||||
|
crypto_x25519(s1.data(), aSec.data(), bPub.data());
|
||||||
|
crypto_x25519(s2.data(), bSec.data(), aPub.data());
|
||||||
|
CHECK(s1 == hex("4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742") &&
|
||||||
|
s1 == s2,
|
||||||
|
"RFC 7748: X25519 reaches the published shared secret");
|
||||||
|
|
||||||
|
auto seed = hex("4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb");
|
||||||
|
const auto msg = hex("72");
|
||||||
|
std::vector<uint8_t> sk(64), pk(32), sig(64);
|
||||||
|
crypto_ed25519_key_pair(sk.data(), pk.data(), seed.data());
|
||||||
|
crypto_ed25519_sign(sig.data(), sk.data(), msg.data(), msg.size());
|
||||||
|
CHECK(sig == hex("92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da"
|
||||||
|
"085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00"),
|
||||||
|
"RFC 8032 test 2: the signature is the published one, so this is "
|
||||||
|
"SHA-512 Ed25519 rather than Monocypher's BLAKE2b EdDSA");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── the package ANSMINDCORE wrote ─────────────────────────────────────────
|
||||||
|
static void testInterop(const fs::path& dataDir) {
|
||||||
|
const std::string path = (dataDir / "interop.ansmodel").string();
|
||||||
|
if (!fs::exists(path)) {
|
||||||
|
std::printf("SKIP interop: %s is not here\n", path.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const Identity id = fixtureIdentity();
|
||||||
|
const modelpkg::Keyring ring = fixtureKeyring(id);
|
||||||
|
|
||||||
|
modelpkg::Package pkg;
|
||||||
|
std::string err;
|
||||||
|
CHECK(modelpkg::open(path, ring, pkg, err),
|
||||||
|
("a package written by ANSMINDCORE opens here: " + err).c_str());
|
||||||
|
if (!err.empty()) return;
|
||||||
|
CHECK(pkg.signerId == "anssig-2026-09", "signed by the release key it names");
|
||||||
|
CHECK(!pkg.certExpired, "with a certificate still in date");
|
||||||
|
CHECK(pkg.manifest.value("project", std::string()) == "Interop",
|
||||||
|
"the manifest decrypts to what was packed");
|
||||||
|
CHECK(pkg.manifest.value("family", std::string()) == "yolo", "including the family");
|
||||||
|
CHECK(pkg.manifest.value("classes", Json::array()).size() == 2, "and its classes");
|
||||||
|
CHECK(pkg.entryPayloadId() == "p0", "and names the model to run");
|
||||||
|
|
||||||
|
std::vector<uint8_t> got;
|
||||||
|
CHECK(modelpkg::readPayload(pkg, "p0", got, err), ("read: " + err).c_str());
|
||||||
|
std::vector<uint8_t> want(12000);
|
||||||
|
for (size_t i = 0; i < want.size(); ++i) want[i] = uint8_t((i * 31 + 21) & 0xff);
|
||||||
|
CHECK(got == want, "and the model comes back byte for byte, across chunks");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── this implementation on its own ────────────────────────────────────────
|
||||||
|
static std::vector<uint8_t> fakeModel(size_t n, uint8_t seed) {
|
||||||
|
std::vector<uint8_t> v(n);
|
||||||
|
for (size_t i = 0; i < n; ++i) v[i] = uint8_t((i * 31 + seed) & 0xff);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void testRoundTripAndRefusals(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.nonceSeed = 9;
|
||||||
|
req.signerId = "anssig-2026-09";
|
||||||
|
req.signerSecret = id.signerSk;
|
||||||
|
req.signerPublic = id.signerPk;
|
||||||
|
req.rootKeyId = "ansroot-v1";
|
||||||
|
req.notAfter = "2030-01-01T00:00:00Z";
|
||||||
|
{
|
||||||
|
Json cert{{"keyId", req.signerId},
|
||||||
|
{"pub", bits::base64Encode(
|
||||||
|
std::string(id.signerPk.begin(), id.signerPk.end()))},
|
||||||
|
{"notAfter", req.notAfter}};
|
||||||
|
const std::string cb = modelpkg::detail::certBytes(cert);
|
||||||
|
req.rootSig.assign(64, 0);
|
||||||
|
crypto_ed25519_sign(req.rootSig.data(), id.rootSk.data(),
|
||||||
|
reinterpret_cast<const uint8_t*>(cb.data()), cb.size());
|
||||||
|
}
|
||||||
|
req.manifest = Json{{"task", "segmentation"},
|
||||||
|
{"family", "rfdetr"},
|
||||||
|
{"runtime", "onnx"},
|
||||||
|
{"classes", Json::array({Json{{"id", 1}, {"name", "Road"}}})},
|
||||||
|
{"models", Json::array({Json{{"id", "m0"},
|
||||||
|
{"payload", "p0"},
|
||||||
|
{"entry", true}}})}};
|
||||||
|
const auto model = fakeModel(7000, 3);
|
||||||
|
req.models.push_back({"p0", model});
|
||||||
|
|
||||||
|
const std::string path = (dir / "core.ansmodel").string();
|
||||||
|
std::string err;
|
||||||
|
CHECK(modelpkg::pack(path, req, err), ("pack: " + err).c_str());
|
||||||
|
|
||||||
|
modelpkg::Package pkg;
|
||||||
|
CHECK(modelpkg::open(path, ring, pkg, err), ("open: " + err).c_str());
|
||||||
|
std::vector<uint8_t> got;
|
||||||
|
CHECK(modelpkg::readPayload(pkg, "p0", got, err) && got == model,
|
||||||
|
"what this implementation writes, it reads back");
|
||||||
|
|
||||||
|
auto bytes = [&](const std::string& p) {
|
||||||
|
std::ifstream f(p, std::ios::binary);
|
||||||
|
return std::string((std::istreambuf_iterator<char>(f)),
|
||||||
|
std::istreambuf_iterator<char>());
|
||||||
|
};
|
||||||
|
auto write = [&](const std::string& p, const std::string& b) {
|
||||||
|
std::ofstream f(p, std::ios::binary | std::ios::trunc);
|
||||||
|
f.write(b.data(), std::streamsize(b.size()));
|
||||||
|
};
|
||||||
|
const std::string good = bytes(path);
|
||||||
|
|
||||||
|
{ // a flipped bit in the model body
|
||||||
|
std::string bad = good;
|
||||||
|
bad[bad.size() - 30] = char(bad[bad.size() - 30] ^ 0x01);
|
||||||
|
const std::string p = (dir / "flipped.ansmodel").string();
|
||||||
|
write(p, bad);
|
||||||
|
modelpkg::Package t;
|
||||||
|
std::vector<uint8_t> out;
|
||||||
|
CHECK(modelpkg::open(p, ring, t, err) &&
|
||||||
|
!modelpkg::readPayload(t, "p0", out, err) && out.empty(),
|
||||||
|
"a flipped bit fails its chunk and yields no partial model");
|
||||||
|
}
|
||||||
|
{ // an unknown root
|
||||||
|
modelpkg::Keyring stranger;
|
||||||
|
stranger.addX25519("ansk-v1-base", id.basePriv);
|
||||||
|
stranger.addRoot("ansroot-other", modelpkg::Key{});
|
||||||
|
modelpkg::Package t;
|
||||||
|
CHECK(!modelpkg::open(path, stranger, t, err) &&
|
||||||
|
err.find("unknown root") != std::string::npos,
|
||||||
|
"an unknown root is refused");
|
||||||
|
}
|
||||||
|
{ // a revoked signer
|
||||||
|
modelpkg::Keyring revoking = ring;
|
||||||
|
revoking.revokedSigners.push_back("anssig-2026-09");
|
||||||
|
modelpkg::Package t;
|
||||||
|
CHECK(!modelpkg::open(path, revoking, t, err) &&
|
||||||
|
err.find("revoked") != std::string::npos,
|
||||||
|
"a revoked signing key is refused");
|
||||||
|
}
|
||||||
|
{ // not a package
|
||||||
|
const std::string p = (dir / "rubbish.bin").string();
|
||||||
|
write(p, "nothing to see here");
|
||||||
|
modelpkg::Package t;
|
||||||
|
CHECK(!modelpkg::open(p, ring, t, err) &&
|
||||||
|
err.find("not a model package") != std::string::npos,
|
||||||
|
"something else entirely is refused by its magic");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
const fs::path dataDir = argc > 1 ? fs::path(argv[1]) : fs::path("data");
|
||||||
|
const auto stamp = std::chrono::steady_clock::now().time_since_epoch().count();
|
||||||
|
const fs::path tmp =
|
||||||
|
fs::temp_directory_path() / ("anscore-modelpkg-" + std::to_string(stamp));
|
||||||
|
std::error_code ec;
|
||||||
|
fs::create_directories(tmp, ec);
|
||||||
|
|
||||||
|
testVectors();
|
||||||
|
testInterop(dataDir);
|
||||||
|
testRoundTripAndRefusals(tmp);
|
||||||
|
|
||||||
|
fs::remove_all(tmp, ec);
|
||||||
|
std::printf("%d checks, %d failures\n", gChecks, gFailures);
|
||||||
|
return gFailures == 0 ? 0 : 1;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
# The model package format: its primitives against published vectors, a
|
||||||
|
# package written by ANSMINDCORE, and this implementation's own round trip.
|
||||||
|
add_executable(ANSModelPackage-UnitTest ANSModelPackage-UnitTest.cpp)
|
||||||
|
|
||||||
|
target_link_libraries(ANSModelPackage-UnitTest PRIVATE ANSModelPackage)
|
||||||
|
|
||||||
|
add_test(NAME ANSModelPackage
|
||||||
|
COMMAND ANSModelPackage-UnitTest
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/data)
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# interop.ansmodel
|
||||||
|
|
||||||
|
Written by **ANSMINDCORE's** packer (`src/model/ansmodel.hpp`), opened by
|
||||||
|
**this** implementation in the unit test. That is the point of it: the two
|
||||||
|
products must read the same bytes, and a fixture one of them produced is the
|
||||||
|
only way to prove it.
|
||||||
|
|
||||||
|
Keys, so any implementation can reconstruct them:
|
||||||
|
|
||||||
|
| Material | Rule |
|
||||||
|
|---|---|
|
||||||
|
| base X25519 private key | byte *i* = `i*3 + 9` |
|
||||||
|
| root Ed25519 seed | byte *i* = `i*7 + 1` |
|
||||||
|
| release Ed25519 seed | byte *i* = `i*11 + 5` |
|
||||||
|
| model payload, 12000 bytes | byte *i* = `(i*31 + 21) & 0xff` |
|
||||||
|
|
||||||
|
Ids: base key `ansk-v1-base`, root `ansroot-v1`, signer `anssig-2026-09`,
|
||||||
|
certificate valid until 2030-01-01. Chunk size 4096, so the payload really is
|
||||||
|
chunked.
|
||||||
|
|
||||||
|
To regenerate after a deliberate format change, pack the same manifest from
|
||||||
|
ANSMINDCORE with those keys and replace this file — and expect both suites to
|
||||||
|
fail until they agree again, which is the alarm working.
|
||||||
Binary file not shown.
@@ -8,5 +8,6 @@ add_subdirectory(ANSUtilities-UnitTest)
|
|||||||
add_subdirectory(ANSRabbitMQ-UnitTest)
|
add_subdirectory(ANSRabbitMQ-UnitTest)
|
||||||
add_subdirectory(ANSIO-UnitTest)
|
add_subdirectory(ANSIO-UnitTest)
|
||||||
add_subdirectory(ANSLS-UnitTest)
|
add_subdirectory(ANSLS-UnitTest)
|
||||||
|
add_subdirectory(ANSModelPackage-UnitTest)
|
||||||
add_subdirectory(ANSONVIF-UnitTest)
|
add_subdirectory(ANSONVIF-UnitTest)
|
||||||
add_subdirectory(ANSODTRE-UnitTest)
|
add_subdirectory(ANSODTRE-UnitTest)
|
||||||
|
|||||||
Reference in New Issue
Block a user