Files
ANSCORE/core/ANSModelPackage/ANSPackageBits.h
T

327 lines
11 KiB
C++
Raw Normal View History

#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