315 lines
12 KiB
C++
315 lines
12 KiB
C++
#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
|