feat(crypto,archive): implement VL-007 built-in hashes and VL-006 archive creation/gzip/tar fallback

This commit is contained in:
faumaray committed 2026-11-26 20:55:45 +03:00
1 parent b50131f52e
commit 22c8538754
19 files changed
+3326 -133

No files matched your search

+895
View File
@@ -0,0 +1,895 @@
#include "BuiltinHash.hpp"
#include <algorithm>
#include <array>
#include <atomic>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <utility>
#include <variant>
namespace Void::Crypto::Detail
{
namespace
{
[[nodiscard]] inline std::uint32_t load_be32(const std::byte* p) noexcept
{
return (static_cast<std::uint32_t>(std::to_integer<std::uint8_t>(p[0])) << 24U) |
(static_cast<std::uint32_t>(std::to_integer<std::uint8_t>(p[1])) << 16U) |
(static_cast<std::uint32_t>(std::to_integer<std::uint8_t>(p[2])) << 8U) |
static_cast<std::uint32_t>(std::to_integer<std::uint8_t>(p[3]));
}
inline void store_be32(std::byte* p, std::uint32_t v) noexcept
{
p[0] = static_cast<std::byte>((v >> 24U) & 0xFFU);
p[1] = static_cast<std::byte>((v >> 16U) & 0xFFU);
p[2] = static_cast<std::byte>((v >> 8U) & 0xFFU);
p[3] = static_cast<std::byte>(v & 0xFFU);
}
[[nodiscard]] inline std::uint64_t load_be64(const std::byte* p) noexcept
{
return (static_cast<std::uint64_t>(load_be32(p)) << 32U) |
static_cast<std::uint64_t>(load_be32(p + 4));
}
inline void store_be64(std::byte* p, std::uint64_t v) noexcept
{
store_be32(p, static_cast<std::uint32_t>((v >> 32U) & 0xFFFFFFFFU));
store_be32(p + 4, static_cast<std::uint32_t>(v & 0xFFFFFFFFU));
}
[[nodiscard]] inline std::uint64_t load_le64(const std::byte* p) noexcept
{
return static_cast<std::uint64_t>(std::to_integer<std::uint8_t>(p[0])) |
(static_cast<std::uint64_t>(std::to_integer<std::uint8_t>(p[1])) << 8U) |
(static_cast<std::uint64_t>(std::to_integer<std::uint8_t>(p[2])) << 16U) |
(static_cast<std::uint64_t>(std::to_integer<std::uint8_t>(p[3])) << 24U) |
(static_cast<std::uint64_t>(std::to_integer<std::uint8_t>(p[4])) << 32U) |
(static_cast<std::uint64_t>(std::to_integer<std::uint8_t>(p[5])) << 40U) |
(static_cast<std::uint64_t>(std::to_integer<std::uint8_t>(p[6])) << 48U) |
(static_cast<std::uint64_t>(std::to_integer<std::uint8_t>(p[7])) << 56U);
}
inline void store_le64(std::byte* p, std::uint64_t v) noexcept
{
for (std::size_t i = 0; i < 8; ++i) {
p[i] = static_cast<std::byte>((v >> (8U * i)) & 0xFFU);
}
}
// ── FIPS 180-4 SHA-1 ─────────────────────────────────────────────────
struct Sha1State {
std::array<std::uint32_t, 5> h{0x67452301U, 0xefcdab89U,
0x98badcfeU, 0x10325476U,
0xc3d2e1f0U};
std::array<std::byte, 64> buf{};
std::size_t buf_len{0};
std::uint64_t total_bytes{0};
void compress(const std::byte* block) noexcept
{
std::array<std::uint32_t, 80> w{};
for (std::size_t i = 0; i < 16; ++i) {
w[i] = load_be32(block + i * 4);
}
for (std::size_t i = 16; i < 80; ++i) {
w[i] = std::rotl(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^
w[i - 16],
1);
}
std::uint32_t a = h[0];
std::uint32_t b = h[1];
std::uint32_t c = h[2];
std::uint32_t d = h[3];
std::uint32_t e = h[4];
for (std::size_t i = 0; i < 80; ++i) {
std::uint32_t f = 0;
std::uint32_t k = 0;
if (i < 20) {
f = (b & c) | ((~b) & d);
k = 0x5a827999U;
} else if (i < 40) {
f = b ^ c ^ d;
k = 0x6ed9eba1U;
} else if (i < 60) {
f = (b & c) | (b & d) | (c & d);
k = 0x8f1bbcdcU;
} else {
f = b ^ c ^ d;
k = 0xca62c1d6U;
}
const std::uint32_t temp =
std::rotl(a, 5) + f + e + k + w[i];
e = d;
d = c;
c = std::rotl(b, 30);
b = a;
a = temp;
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
builtin_secure_wipe(std::as_writable_bytes(std::span{w}));
}
void update(std::span<const std::byte> data) noexcept
{
total_bytes += data.size();
std::size_t off = 0;
if (buf_len > 0) {
const std::size_t take =
std::min(data.size(), buf.size() - buf_len);
std::memcpy(buf.data() + buf_len, data.data(), take);
buf_len += take;
off += take;
if (buf_len == buf.size()) {
compress(buf.data());
buf_len = 0;
}
}
while (off + buf.size() <= data.size()) {
compress(data.data() + off);
off += buf.size();
}
if (off < data.size()) {
const std::size_t rem = data.size() - off;
std::memcpy(buf.data(), data.data() + off, rem);
buf_len = rem;
}
}
void finalize_into(std::span<std::byte> out20) noexcept
{
buf[buf_len++] = std::byte{0x80};
if (buf_len > 56) {
std::fill(buf.begin() + static_cast<std::ptrdiff_t>(buf_len),
buf.end(), std::byte{0});
compress(buf.data());
buf_len = 0;
}
std::fill(buf.begin() + static_cast<std::ptrdiff_t>(buf_len),
buf.begin() + 56, std::byte{0});
store_be64(buf.data() + 56, total_bytes << 3U);
compress(buf.data());
for (std::size_t i = 0; i < 5; ++i) {
store_be32(out20.data() + i * 4, h[i]);
}
wipe();
}
void wipe() noexcept
{
builtin_secure_wipe(std::as_writable_bytes(std::span{h}));
builtin_secure_wipe(buf);
buf_len = 0;
total_bytes = 0;
}
};
// ── FIPS 180-4 SHA-256 ───────────────────────────────────────────────
struct Sha256State {
static constexpr std::array<std::uint32_t, 64> kRound = {
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};
std::array<std::uint32_t, 8> h{0x6a09e667U, 0xbb67ae85U,
0x3c6ef372U, 0xa54ff53aU,
0x510e527fU, 0x9b05688cU,
0x1f83d9abU, 0x5be0cd19U};
std::array<std::byte, 64> buf{};
std::size_t buf_len{0};
std::uint64_t total_bytes{0};
void compress(const std::byte* block) noexcept
{
std::array<std::uint32_t, 64> w{};
for (std::size_t i = 0; i < 16; ++i) {
w[i] = load_be32(block + i * 4);
}
for (std::size_t i = 16; i < 64; ++i) {
const std::uint32_t s0 = std::rotr(w[i - 15], 7) ^
std::rotr(w[i - 15], 18) ^
(w[i - 15] >> 3U);
const std::uint32_t s1 = std::rotr(w[i - 2], 17) ^
std::rotr(w[i - 2], 19) ^
(w[i - 2] >> 10U);
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
std::uint32_t a = h[0];
std::uint32_t b = h[1];
std::uint32_t c = h[2];
std::uint32_t d = h[3];
std::uint32_t e = h[4];
std::uint32_t f = h[5];
std::uint32_t g = h[6];
std::uint32_t hv = h[7];
for (std::size_t i = 0; i < 64; ++i) {
const std::uint32_t S1 = std::rotr(e, 6) ^
std::rotr(e, 11) ^
std::rotr(e, 25);
const std::uint32_t ch = (e & f) ^ ((~e) & g);
const std::uint32_t temp1 =
hv + S1 + ch + kRound[i] + w[i];
const std::uint32_t S0 = std::rotr(a, 2) ^
std::rotr(a, 13) ^
std::rotr(a, 22);
const std::uint32_t maj =
(a & b) ^ (a & c) ^ (b & c);
const std::uint32_t temp2 = S0 + maj;
hv = g;
g = f;
f = e;
e = d + temp1;
d = c;
c = b;
b = a;
a = temp1 + temp2;
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
h[5] += f;
h[6] += g;
h[7] += hv;
builtin_secure_wipe(std::as_writable_bytes(std::span{w}));
}
void update(std::span<const std::byte> data) noexcept
{
total_bytes += data.size();
std::size_t off = 0;
if (buf_len > 0) {
const std::size_t take =
std::min(data.size(), buf.size() - buf_len);
std::memcpy(buf.data() + buf_len, data.data(), take);
buf_len += take;
off += take;
if (buf_len == buf.size()) {
compress(buf.data());
buf_len = 0;
}
}
while (off + buf.size() <= data.size()) {
compress(data.data() + off);
off += buf.size();
}
if (off < data.size()) {
const std::size_t rem = data.size() - off;
std::memcpy(buf.data(), data.data() + off, rem);
buf_len = rem;
}
}
void finalize_into(std::span<std::byte> out32) noexcept
{
buf[buf_len++] = std::byte{0x80};
if (buf_len > 56) {
std::fill(buf.begin() + static_cast<std::ptrdiff_t>(buf_len),
buf.end(), std::byte{0});
compress(buf.data());
buf_len = 0;
}
std::fill(buf.begin() + static_cast<std::ptrdiff_t>(buf_len),
buf.begin() + 56, std::byte{0});
store_be64(buf.data() + 56, total_bytes << 3U);
compress(buf.data());
for (std::size_t i = 0; i < 8; ++i) {
store_be32(out32.data() + i * 4, h[i]);
}
wipe();
}
void wipe() noexcept
{
builtin_secure_wipe(std::as_writable_bytes(std::span{h}));
builtin_secure_wipe(buf);
buf_len = 0;
total_bytes = 0;
}
};
// ── FIPS 180-4 SHA-512 ───────────────────────────────────────────────
struct Sha512State {
static constexpr std::array<std::uint64_t, 80> kRound = {
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL};
std::array<std::uint64_t, 8> h{
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
std::array<std::byte, 128> buf{};
std::size_t buf_len{0};
std::uint64_t total_bytes{0};
void compress(const std::byte* block) noexcept
{
std::array<std::uint64_t, 80> w{};
for (std::size_t i = 0; i < 16; ++i) {
w[i] = load_be64(block + i * 8);
}
for (std::size_t i = 16; i < 80; ++i) {
const std::uint64_t s0 = std::rotr(w[i - 15], 1) ^
std::rotr(w[i - 15], 8) ^
(w[i - 15] >> 7U);
const std::uint64_t s1 = std::rotr(w[i - 2], 19) ^
std::rotr(w[i - 2], 61) ^
(w[i - 2] >> 6U);
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
}
std::uint64_t a = h[0];
std::uint64_t b = h[1];
std::uint64_t c = h[2];
std::uint64_t d = h[3];
std::uint64_t e = h[4];
std::uint64_t f = h[5];
std::uint64_t g = h[6];
std::uint64_t hv = h[7];
for (std::size_t i = 0; i < 80; ++i) {
const std::uint64_t S1 = std::rotr(e, 14) ^
std::rotr(e, 18) ^
std::rotr(e, 41);
const std::uint64_t ch = (e & f) ^ ((~e) & g);
const std::uint64_t temp1 =
hv + S1 + ch + kRound[i] + w[i];
const std::uint64_t S0 = std::rotr(a, 28) ^
std::rotr(a, 34) ^
std::rotr(a, 39);
const std::uint64_t maj =
(a & b) ^ (a & c) ^ (b & c);
const std::uint64_t temp2 = S0 + maj;
hv = g;
g = f;
f = e;
e = d + temp1;
d = c;
c = b;
b = a;
a = temp1 + temp2;
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
h[5] += f;
h[6] += g;
h[7] += hv;
builtin_secure_wipe(std::as_writable_bytes(std::span{w}));
}
void update(std::span<const std::byte> data) noexcept
{
total_bytes += data.size();
std::size_t off = 0;
if (buf_len > 0) {
const std::size_t take =
std::min(data.size(), buf.size() - buf_len);
std::memcpy(buf.data() + buf_len, data.data(), take);
buf_len += take;
off += take;
if (buf_len == buf.size()) {
compress(buf.data());
buf_len = 0;
}
}
while (off + buf.size() <= data.size()) {
compress(data.data() + off);
off += buf.size();
}
if (off < data.size()) {
const std::size_t rem = data.size() - off;
std::memcpy(buf.data(), data.data() + off, rem);
buf_len = rem;
}
}
void finalize_into(std::span<std::byte> out64) noexcept
{
buf[buf_len++] = std::byte{0x80};
if (buf_len > 112) {
std::fill(buf.begin() + static_cast<std::ptrdiff_t>(buf_len),
buf.end(), std::byte{0});
compress(buf.data());
buf_len = 0;
}
std::fill(buf.begin() + static_cast<std::ptrdiff_t>(buf_len),
buf.begin() + 112, std::byte{0});
store_be64(buf.data() + 112, total_bytes >> 61U);
store_be64(buf.data() + 120, total_bytes << 3U);
compress(buf.data());
for (std::size_t i = 0; i < 8; ++i) {
store_be64(out64.data() + i * 8, h[i]);
}
wipe();
}
void wipe() noexcept
{
builtin_secure_wipe(std::as_writable_bytes(std::span{h}));
builtin_secure_wipe(buf);
buf_len = 0;
total_bytes = 0;
}
};
// ── RFC 7693 BLAKE2b ─────────────────────────────────────────────────
struct Blake2bState {
static constexpr std::array<std::uint64_t, 8> kIv = {
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
static constexpr std::array<std::array<std::uint8_t, 16>, 12>
kSigma = {{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
{11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
{7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
{9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
{2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
{12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
{13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
{6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
{10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
{14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
}};
std::array<std::uint64_t, 8> h{kIv};
std::array<std::uint64_t, 2> t{0, 0};
std::array<std::byte, 128> buf{};
std::size_t buf_len{0};
std::size_t out_len{64};
void init(std::size_t digest_len) noexcept
{
h = kIv;
out_len = digest_len;
h[0] ^= 0x01010000ULL ^ static_cast<std::uint64_t>(out_len);
t = {0, 0};
buf.fill(std::byte{0});
buf_len = 0;
}
void add_counter(std::uint64_t inc) noexcept
{
t[0] += inc;
if (t[0] < inc) {
++t[1];
}
}
void compress(const std::byte* block, bool is_last) noexcept
{
std::array<std::uint64_t, 16> m{};
for (std::size_t i = 0; i < 16; ++i) {
m[i] = load_le64(block + i * 8);
}
std::array<std::uint64_t, 16> v{};
for (std::size_t i = 0; i < 8; ++i) {
v[i] = h[i];
v[i + 8] = kIv[i];
}
v[12] ^= t[0];
v[13] ^= t[1];
if (is_last) {
v[14] = ~v[14];
}
auto mix = [&](std::size_t r, std::size_t p,
std::size_t ia, std::size_t ib,
std::size_t ic, std::size_t id) noexcept {
const auto s0 = kSigma[r][2 * p];
const auto s1 = kSigma[r][2 * p + 1];
v[ia] = v[ia] + v[ib] + m[s0];
v[id] = std::rotr(v[id] ^ v[ia], 32);
v[ic] = v[ic] + v[id];
v[ib] = std::rotr(v[ib] ^ v[ic], 24);
v[ia] = v[ia] + v[ib] + m[s1];
v[id] = std::rotr(v[id] ^ v[ia], 16);
v[ic] = v[ic] + v[id];
v[ib] = std::rotr(v[ib] ^ v[ic], 63);
};
for (std::size_t r = 0; r < 12; ++r) {
mix(r, 0, 0, 4, 8, 12);
mix(r, 1, 1, 5, 9, 13);
mix(r, 2, 2, 6, 10, 14);
mix(r, 3, 3, 7, 11, 15);
mix(r, 4, 0, 5, 10, 15);
mix(r, 5, 1, 6, 11, 12);
mix(r, 6, 2, 7, 8, 13);
mix(r, 7, 3, 4, 9, 14);
}
for (std::size_t i = 0; i < 8; ++i) {
h[i] ^= v[i] ^ v[i + 8];
}
builtin_secure_wipe(std::as_writable_bytes(std::span{m}));
builtin_secure_wipe(std::as_writable_bytes(std::span{v}));
}
void update(std::span<const std::byte> data) noexcept
{
if (data.empty()) {
return;
}
std::size_t off = 0;
if (buf_len > 0) {
const std::size_t take =
std::min(data.size(), buf.size() - buf_len);
std::memcpy(buf.data() + buf_len, data.data(), take);
buf_len += take;
off += take;
// Only flush a full buffer when more bytes follow so
// finalize() sees the true final block.
if (off < data.size() && buf_len == buf.size()) {
add_counter(128);
compress(buf.data(), false);
buf_len = 0;
}
}
while (off + buf.size() < data.size()) {
add_counter(128);
compress(data.data() + off, false);
off += buf.size();
}
if (off < data.size()) {
const std::size_t rem = data.size() - off;
std::memcpy(buf.data() + buf_len, data.data() + off,
rem);
buf_len += rem;
}
}
void finalize_into(std::span<std::byte> out) noexcept
{
add_counter(static_cast<std::uint64_t>(buf_len));
std::fill(buf.begin() + static_cast<std::ptrdiff_t>(buf_len),
buf.end(), std::byte{0});
compress(buf.data(), true);
std::array<std::byte, 64> full{};
for (std::size_t i = 0; i < 8; ++i) {
store_le64(full.data() + i * 8, h[i]);
}
std::memcpy(out.data(), full.data(), out_len);
builtin_secure_wipe(full);
wipe();
}
void wipe() noexcept
{
builtin_secure_wipe(std::as_writable_bytes(std::span{h}));
builtin_secure_wipe(std::as_writable_bytes(std::span{t}));
builtin_secure_wipe(buf);
buf_len = 0;
}
};
} // namespace
void builtin_secure_wipe(std::span<std::byte> buffer) noexcept
{
if (buffer.empty()) {
return;
}
volatile std::byte* p = buffer.data();
for (std::size_t i = 0; i < buffer.size(); ++i) {
p[i] = std::byte{0};
}
std::atomic_signal_fence(std::memory_order_seq_cst);
}
bool builtin_constant_time_equal(std::span<const std::byte> lhs,
std::span<const std::byte> rhs) noexcept
{
if (lhs.size() != rhs.size()) {
return false;
}
volatile unsigned char diff = 0;
for (std::size_t i = 0; i < lhs.size(); ++i) {
const auto a = static_cast<unsigned char>(lhs[i]);
const auto b = static_cast<unsigned char>(rhs[i]);
diff = static_cast<unsigned char>(diff | (a ^ b));
}
std::atomic_signal_fence(std::memory_order_seq_cst);
return diff == 0;
}
struct BuiltinHasher::State {
std::variant<std::monostate, Sha1State, Sha256State, Sha512State,
Blake2bState>
impl{};
void wipe() noexcept
{
std::visit(
[](auto& st) {
using T = std::decay_t<decltype(st)>;
if constexpr (!std::is_same_v<T, std::monostate>) {
st.wipe();
}
},
impl);
impl.template emplace<std::monostate>();
}
};
BuiltinHasher::~BuiltinHasher() noexcept { destroy(); }
BuiltinHasher::BuiltinHasher(BuiltinHasher&& other) noexcept
: m_state(std::exchange(other.m_state, nullptr))
{
}
BuiltinHasher& BuiltinHasher::operator=(BuiltinHasher&& other) noexcept
{
if (this != &other) {
destroy();
m_state = std::exchange(other.m_state, nullptr);
}
return *this;
}
void BuiltinHasher::destroy() noexcept
{
if (m_state != nullptr) {
m_state->wipe();
delete m_state;
m_state = nullptr;
}
}
bool BuiltinHasher::init(HashAlgorithm algorithm) noexcept
{
if (m_state == nullptr) {
m_state = new (std::nothrow) State();
if (m_state == nullptr) {
return false;
}
} else {
m_state->wipe();
}
switch (algorithm) {
case HashAlgorithm::Sha1:
m_state->impl.emplace<Sha1State>();
return true;
case HashAlgorithm::Sha256:
m_state->impl.emplace<Sha256State>();
return true;
case HashAlgorithm::Sha512:
m_state->impl.emplace<Sha512State>();
return true;
case HashAlgorithm::Blake2b256: {
auto& b = m_state->impl.emplace<Blake2bState>();
b.init(32);
return true;
}
case HashAlgorithm::Blake2b512: {
auto& b = m_state->impl.emplace<Blake2bState>();
b.init(64);
return true;
}
}
return false;
}
void BuiltinHasher::update(std::span<const std::byte> data) noexcept
{
if (m_state == nullptr || data.empty()) {
return;
}
std::visit(
[&](auto& st) {
using T = std::decay_t<decltype(st)>;
if constexpr (!std::is_same_v<T, std::monostate>) {
st.update(data);
}
},
m_state->impl);
}
Digest BuiltinHasher::finalize()
{
Digest out;
if (m_state == nullptr) {
return out;
}
std::visit(
[&](auto& st) {
using T = std::decay_t<decltype(st)>;
if constexpr (std::is_same_v<T, Sha1State>) {
out.resize(20);
st.finalize_into(out);
} else if constexpr (std::is_same_v<T, Sha256State>) {
out.resize(32);
st.finalize_into(out);
} else if constexpr (std::is_same_v<T, Sha512State>) {
out.resize(64);
st.finalize_into(out);
} else if constexpr (std::is_same_v<T, Blake2bState>) {
out.resize(st.out_len);
st.finalize_into(out);
}
},
m_state->impl);
return out;
}
void builtin_hmac_sha256(std::span<const std::byte> secret_key,
std::span<const std::byte> message,
std::span<std::byte> output_32) noexcept
{
std::array<std::byte, 64> key_block{};
if (secret_key.size() > 64) {
Sha256State kh;
kh.update(secret_key);
kh.finalize_into(std::span{key_block}.subspan(0, 32));
} else if (!secret_key.empty()) {
std::memcpy(key_block.data(), secret_key.data(), secret_key.size());
}
std::array<std::byte, 64> ipad{};
std::array<std::byte, 64> opad{};
for (std::size_t i = 0; i < 64; ++i) {
ipad[i] = key_block[i] ^ std::byte{0x36};
opad[i] = key_block[i] ^ std::byte{0x5C};
}
std::array<std::byte, 32> inner_hash{};
{
Sha256State inner;
inner.update(ipad);
inner.update(message);
inner.finalize_into(inner_hash);
}
{
Sha256State outer;
outer.update(opad);
outer.update(inner_hash);
outer.finalize_into(output_32);
}
builtin_secure_wipe(key_block);
builtin_secure_wipe(ipad);
builtin_secure_wipe(opad);
builtin_secure_wipe(inner_hash);
}
void builtin_hkdf_sha256(std::span<const std::byte> secret_input,
std::span<const std::byte> salt,
std::span<const std::byte> context,
std::span<std::byte> secret_output) noexcept
{
std::array<std::byte, 32> zero_salt{};
const auto effective_salt =
salt.empty() ? std::span<const std::byte>{zero_salt} : salt;
std::array<std::byte, 32> prk{};
builtin_hmac_sha256(effective_salt, secret_input, prk);
std::array<std::byte, 32> t{};
std::size_t t_len = 0;
std::size_t written = 0;
std::uint8_t counter = 1;
while (written < secret_output.size()) {
std::array<std::byte, 64> ipad{};
std::array<std::byte, 64> opad{};
for (std::size_t i = 0; i < 32; ++i) {
ipad[i] = prk[i] ^ std::byte{0x36};
opad[i] = prk[i] ^ std::byte{0x5C};
}
for (std::size_t i = 32; i < 64; ++i) {
ipad[i] = std::byte{0x36};
opad[i] = std::byte{0x5C};
}
std::array<std::byte, 32> inner_hash{};
Sha256State inner;
inner.update(ipad);
if (t_len > 0) {
inner.update(std::span<const std::byte>{t.data(), t_len});
}
if (!context.empty()) {
inner.update(context);
}
const std::byte ctr_byte = static_cast<std::byte>(counter);
inner.update(std::span<const std::byte>{&ctr_byte, 1});
inner.finalize_into(inner_hash);
Sha256State outer;
outer.update(opad);
outer.update(inner_hash);
outer.finalize_into(t);
t_len = 32;
const std::size_t copy_n =
std::min<std::size_t>(32, secret_output.size() - written);
std::memcpy(secret_output.data() + written, t.data(), copy_n);
written += copy_n;
++counter;
builtin_secure_wipe(ipad);
builtin_secure_wipe(opad);
builtin_secure_wipe(inner_hash);
}
builtin_secure_wipe(prk);
builtin_secure_wipe(t);
}
} // namespace Void::Crypto::Detail
+49
View File
@@ -0,0 +1,49 @@
#pragma once
#include <Void/Crypto/Crypto.hpp>
#include <Void/Crypto/Types.hpp>
#include <cstddef>
#include <span>
namespace Void::Crypto::Detail
{
void builtin_secure_wipe(std::span<std::byte> buffer) noexcept;
[[nodiscard]] bool
builtin_constant_time_equal(std::span<const std::byte> lhs,
std::span<const std::byte> rhs) noexcept;
class BuiltinHasher
{
public:
BuiltinHasher() noexcept = default;
~BuiltinHasher() noexcept;
BuiltinHasher(BuiltinHasher&& other) noexcept;
BuiltinHasher& operator=(BuiltinHasher&& other) noexcept;
BuiltinHasher(const BuiltinHasher&) = delete;
BuiltinHasher& operator=(const BuiltinHasher&) = delete;
[[nodiscard]] bool init(HashAlgorithm algorithm) noexcept;
void update(std::span<const std::byte> data) noexcept;
[[nodiscard]] Digest finalize();
private:
struct State;
State* m_state{nullptr};
void destroy() noexcept;
};
void builtin_hmac_sha256(std::span<const std::byte> secret_key,
std::span<const std::byte> message,
std::span<std::byte> output_32) noexcept;
void builtin_hkdf_sha256(std::span<const std::byte> secret_input,
std::span<const std::byte> salt,
std::span<const std::byte> context,
std::span<std::byte> secret_output) noexcept;
} // namespace Void::Crypto::Detail
+297 -24
View File
@@ -1,19 +1,24 @@
#include <Void/Crypto/Crypto.hpp>
#include "BuiltinHash.hpp"
#include <array>
#include <cstdint>
#include <cstring>
#include <format>
#include <fstream>
#include <limits>
#include <optional>
#include <utility>
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
#include <openssl/core_names.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/kdf.h>
#include <openssl/params.h>
#endif
namespace Void::Crypto
{
@@ -42,6 +47,36 @@ namespace Void::Crypto
std::format("{}: injected crypto backend fault", operation));
}
[[nodiscard]] bool spans_overlap(std::span<const std::byte> a,
std::span<const std::byte> b) noexcept
{
if (a.empty() || b.empty()) {
return false;
}
const auto a_begin = reinterpret_cast<std::uintptr_t>(a.data());
const auto a_end = a_begin + a.size();
const auto b_begin = reinterpret_cast<std::uintptr_t>(b.data());
const auto b_end = b_begin + b.size();
return a_begin < b_end && b_begin < a_end;
}
[[nodiscard]] bool use_builtin_hash(
HashAlgorithm algorithm, const ProviderOptions& provider) noexcept
{
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
if (provider.prefer_builtin ||
algorithm == HashAlgorithm::Blake2b256) {
return true;
}
return false;
#else
(void)algorithm;
(void)provider;
return true;
#endif
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
[[nodiscard]] const char*
algorithm_name(HashAlgorithm algorithm) noexcept
{
@@ -52,6 +87,10 @@ namespace Void::Crypto
return "SHA256";
case HashAlgorithm::Sha512:
return "SHA512";
case HashAlgorithm::Blake2b256:
return nullptr;
case HashAlgorithm::Blake2b512:
return "BLAKE2B512";
}
return nullptr;
}
@@ -78,19 +117,6 @@ namespace Void::Crypto
code, std::format("{}: {}", operation, buffer.data()));
}
[[nodiscard]] bool spans_overlap(std::span<const std::byte> a,
std::span<const std::byte> b) noexcept
{
if (a.empty() || b.empty()) {
return false;
}
const auto a_begin = reinterpret_cast<std::uintptr_t>(a.data());
const auto a_end = a_begin + a.size();
const auto b_begin = reinterpret_cast<std::uintptr_t>(b.data());
const auto b_end = b_begin + b.size();
return a_begin < b_end && b_begin < a_end;
}
using EvpMdPtr = std::unique_ptr<EVP_MD, decltype(&EVP_MD_free)>;
using EvpMdCtxPtr =
std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>;
@@ -110,6 +136,7 @@ namespace Void::Crypto
? &kEmptyByte
: reinterpret_cast<const unsigned char*>(span.data());
}
#endif
[[nodiscard]] Void::Result::Result<bool>
verify_ed25519_impl(std::span<const std::byte> message,
@@ -144,6 +171,12 @@ namespace Void::Crypto
return std::unexpected{std::move(*fault)};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
if (provider.prefer_builtin) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::verifyEd25519: Ed25519 requires OpenSSL backend")};
}
ERR_clear_error();
const char* propq = propq_or_null(provider);
EvpPkeyPtr key{EVP_PKEY_new_raw_public_key_ex(
@@ -182,14 +215,33 @@ namespace Void::Crypto
}
ERR_clear_error();
return result == 1;
#else
(void)provider;
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::verifyEd25519: built without OpenSSL backend")};
#endif
}
} // namespace
bool has_openssl_backend() noexcept
{
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
return true;
#else
return false;
#endif
}
void secure_wipe(std::span<std::byte> buffer) noexcept
{
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
if (!buffer.empty()) {
OPENSSL_cleanse(buffer.data(), buffer.size());
}
#else
Detail::builtin_secure_wipe(buffer);
#endif
}
bool constant_time_equal(std::span<const std::byte> lhs,
@@ -201,7 +253,11 @@ namespace Void::Crypto
if (lhs.empty()) {
return true;
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
return CRYPTO_memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
#else
return Detail::builtin_constant_time_equal(lhs, rhs);
#endif
}
ScopedFaultGuard::ScopedFaultGuard(FaultConfig config) noexcept
@@ -218,8 +274,12 @@ namespace Void::Crypto
}
struct Hasher::Impl {
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
EvpMdCtxPtr context{nullptr, &EVP_MD_CTX_free};
EvpMdPtr algorithm{nullptr, &EVP_MD_free};
#endif
Detail::BuiltinHasher builtin{};
bool using_builtin{false};
HasherState state{HasherState::Ready};
};
@@ -244,18 +304,50 @@ namespace Void::Crypto
Void::Result::Result<Hasher>
Hasher::create(HashAlgorithm algorithm, const ProviderOptions& provider)
{
const char* md_name = algorithm_name(algorithm);
if (md_name == nullptr) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Hasher: unsupported algorithm")};
switch (algorithm) {
case HashAlgorithm::Sha1:
case HashAlgorithm::Sha256:
case HashAlgorithm::Sha512:
case HashAlgorithm::Blake2b256:
case HashAlgorithm::Blake2b512:
break;
default:
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Hasher: unsupported algorithm")};
}
if (auto fault = maybe_inject_fault(FaultOperation::hash_create,
"Crypto::Hasher::create");
fault.has_value()) {
return std::unexpected{std::move(*fault)};
}
if (use_builtin_hash(algorithm, provider)) {
if (!provider.property_query.empty()) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::provider_error,
"Crypto::Hasher: property_query unavailable on built-in "
"backend")};
}
auto impl = std::make_unique<Impl>();
if (!impl->builtin.init(algorithm)) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::backend_error,
"Crypto::Hasher: built-in initialization failed")};
}
impl->using_builtin = true;
impl->state = HasherState::Ready;
return Hasher{std::move(impl)};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
const char* md_name = algorithm_name(algorithm);
if (md_name == nullptr) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Hasher: unsupported OpenSSL digest algorithm")};
}
ERR_clear_error();
auto impl = std::make_unique<Impl>();
impl->algorithm = EvpMdPtr{
@@ -278,8 +370,14 @@ namespace Void::Crypto
CryptoErrorCode::provider_error,
"Crypto::Hasher: digest initialization failed")};
}
impl->using_builtin = false;
impl->state = HasherState::Ready;
return Hasher{std::move(impl)};
#else
return std::unexpected{make_crypto_error(
CryptoErrorCode::backend_error,
"Crypto::Hasher: OpenSSL backend unavailable")};
#endif
}
Void::Result::Result<void> Hasher::update(std::span<const std::byte> data)
@@ -301,6 +399,12 @@ namespace Void::Crypto
m_impl->state = HasherState::Failed;
return std::unexpected{std::move(*fault)};
}
if (m_impl->using_builtin) {
m_impl->builtin.update(data);
m_impl->state = HasherState::Updating;
return {};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
if (!data.empty() &&
EVP_DigestUpdate(m_impl->context.get(), data.data(),
data.size()) != 1) {
@@ -311,6 +415,12 @@ namespace Void::Crypto
}
m_impl->state = HasherState::Updating;
return {};
#else
m_impl->state = HasherState::Failed;
return std::unexpected{make_crypto_error(
CryptoErrorCode::backend_error,
"Crypto::Hasher::update: invalid backend")};
#endif
}
Void::Result::Result<void> Hasher::update(std::string_view data)
@@ -337,6 +447,12 @@ namespace Void::Crypto
m_impl->state = HasherState::Failed;
return std::unexpected{std::move(*fault)};
}
if (m_impl->using_builtin) {
Digest digest = m_impl->builtin.finalize();
m_impl->state = HasherState::Finalized;
return digest;
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
const int digest_size = EVP_MD_get_size(m_impl->algorithm.get());
if (digest_size <= 0) {
m_impl->state = HasherState::Failed;
@@ -357,6 +473,12 @@ namespace Void::Crypto
m_impl->state = HasherState::Finalized;
digest.resize(written);
return digest;
#else
m_impl->state = HasherState::Failed;
return std::unexpected{make_crypto_error(
CryptoErrorCode::backend_error,
"Crypto::Hasher::finalize: invalid backend")};
#endif
}
auto hash(HashAlgorithm algorithm, std::span<const std::byte> data)
@@ -388,18 +510,31 @@ namespace Void::Crypto
auto hashFile(HashAlgorithm algorithm, const std::filesystem::path& path)
-> Void::Result::Result<Digest>
{
return hashFile(algorithm, path, ProviderOptions{});
return hash_file(algorithm, path, ProviderOptions{});
}
auto hashFile(HashAlgorithm algorithm, const std::filesystem::path& path,
const ProviderOptions& provider)
-> Void::Result::Result<Digest>
{
return hash_file(algorithm, path, provider);
}
auto hash_file(HashAlgorithm algorithm, const std::filesystem::path& path)
-> Void::Result::Result<Digest>
{
return hash_file(algorithm, path, ProviderOptions{});
}
auto hash_file(HashAlgorithm algorithm, const std::filesystem::path& path,
const ProviderOptions& provider)
-> Void::Result::Result<Digest>
{
std::ifstream input{path, std::ios::binary};
if (!input.is_open()) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::io_error,
std::format("Crypto::hashFile: cannot open '{}'",
std::format("Crypto::hash_file: cannot open '{}'",
path.string()))};
}
auto hasher = Hasher::create(algorithm, provider);
@@ -422,7 +557,7 @@ namespace Void::Crypto
if (input.bad() || (!input.eof() && input.fail())) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::io_error,
std::format("Crypto::hashFile: I/O error reading '{}'",
std::format("Crypto::hash_file: I/O error reading '{}'",
path.string()))};
}
return hasher->finalize();
@@ -452,21 +587,63 @@ namespace Void::Crypto
{
return hash(HashAlgorithm::Sha512, data);
}
Void::Result::Result<Digest> blake2b256(std::span<const std::byte> data)
{
return hash(HashAlgorithm::Blake2b256, data);
}
Void::Result::Result<Digest> blake2b256(std::string_view data)
{
return hash(HashAlgorithm::Blake2b256, data);
}
Void::Result::Result<Digest> blake2b512(std::span<const std::byte> data)
{
return hash(HashAlgorithm::Blake2b512, data);
}
Void::Result::Result<Digest> blake2b512(std::string_view data)
{
return hash(HashAlgorithm::Blake2b512, data);
}
Void::Result::Result<Digest> sha1File(const std::filesystem::path& path)
{
return hashFile(HashAlgorithm::Sha1, path);
return hash_file(HashAlgorithm::Sha1, path);
}
Void::Result::Result<Digest> sha256File(const std::filesystem::path& path)
{
return hashFile(HashAlgorithm::Sha256, path);
return hash_file(HashAlgorithm::Sha256, path);
}
Void::Result::Result<Digest> sha512File(const std::filesystem::path& path)
{
return hashFile(HashAlgorithm::Sha512, path);
return hash_file(HashAlgorithm::Sha512, path);
}
Void::Result::Result<Digest> sha1_file(const std::filesystem::path& path)
{
return hash_file(HashAlgorithm::Sha1, path);
}
Void::Result::Result<Digest> sha256_file(const std::filesystem::path& path)
{
return hash_file(HashAlgorithm::Sha256, path);
}
Void::Result::Result<Digest> sha512_file(const std::filesystem::path& path)
{
return hash_file(HashAlgorithm::Sha512, path);
}
Void::Result::Result<Digest>
blake2b256_file(const std::filesystem::path& path)
{
return hash_file(HashAlgorithm::Blake2b256, path);
}
Void::Result::Result<Digest>
blake2b512_file(const std::filesystem::path& path)
{
return hash_file(HashAlgorithm::Blake2b512, path);
}
struct Ed25519PrivateKey::Impl {
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
EvpPkeyPtr pkey{nullptr, &EVP_PKEY_free};
#else
std::uint8_t unused{0};
#endif
};
Ed25519PrivateKey::Ed25519PrivateKey(std::unique_ptr<Impl> impl) noexcept
@@ -481,7 +658,11 @@ namespace Void::Crypto
bool Ed25519PrivateKey::valid() const noexcept
{
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
return m_impl != nullptr && m_impl->pkey != nullptr;
#else
return false;
#endif
}
Void::Result::Result<Ed25519PrivateKey>
@@ -492,6 +673,12 @@ namespace Void::Crypto
fault.has_value()) {
return std::unexpected{std::move(*fault)};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
if (provider.prefer_builtin) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Ed25519PrivateKey::generate requires OpenSSL backend")};
}
ERR_clear_error();
EvpPkeyPtr key{
EVP_PKEY_Q_keygen(nullptr, propq_or_null(provider), "ED25519"),
@@ -504,6 +691,13 @@ namespace Void::Crypto
auto impl = std::make_unique<Impl>();
impl->pkey = std::move(key);
return Ed25519PrivateKey{std::move(impl)};
#else
(void)provider;
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Ed25519PrivateKey::generate: built without OpenSSL "
"backend")};
#endif
}
Void::Result::Result<Ed25519PrivateKey>
@@ -523,6 +717,13 @@ namespace Void::Crypto
fault.has_value()) {
return std::unexpected{std::move(*fault)};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
if (provider.prefer_builtin) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Ed25519PrivateKey::from_raw_seed requires OpenSSL "
"backend")};
}
ERR_clear_error();
EvpPkeyPtr key{EVP_PKEY_new_raw_private_key_ex(
nullptr, "ED25519", propq_or_null(provider),
@@ -536,6 +737,13 @@ namespace Void::Crypto
auto impl = std::make_unique<Impl>();
impl->pkey = std::move(key);
return Ed25519PrivateKey{std::move(impl)};
#else
(void)provider;
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Ed25519PrivateKey::from_raw_seed: built without OpenSSL "
"backend")};
#endif
}
Void::Result::Result<Ed25519PublicKey> Ed25519PrivateKey::public_key() const
@@ -546,6 +754,7 @@ namespace Void::Crypto
"Crypto::Ed25519PrivateKey::public_key: invalid or moved-from "
"key")};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
Ed25519PublicKey pub;
std::size_t len = pub.bytes.size();
if (EVP_PKEY_get_raw_public_key(
@@ -558,6 +767,12 @@ namespace Void::Crypto
"Crypto::Ed25519PrivateKey::public_key failed")};
}
return pub;
#else
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Ed25519PrivateKey::public_key: built without OpenSSL "
"backend")};
#endif
}
Void::Result::Result<Ed25519Seed> Ed25519PrivateKey::export_raw_seed() const
@@ -568,6 +783,7 @@ namespace Void::Crypto
"Crypto::Ed25519PrivateKey::export_raw_seed: invalid or "
"moved-from key")};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
std::array<std::byte, Ed25519SeedSize> raw{};
std::size_t len = raw.size();
if (EVP_PKEY_get_raw_private_key(
@@ -582,6 +798,12 @@ namespace Void::Crypto
auto result = Ed25519Seed::from_bytes(raw);
secure_wipe(raw);
return result;
#else
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Ed25519PrivateKey::export_raw_seed: built without OpenSSL "
"backend")};
#endif
}
Void::Result::Result<Ed25519Signature>
@@ -607,6 +829,12 @@ namespace Void::Crypto
return std::unexpected{std::move(*fault)};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
if (provider.prefer_builtin) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::sign_ed25519 requires OpenSSL backend")};
}
ERR_clear_error();
EvpMdCtxPtr context{EVP_MD_CTX_new(), &EVP_MD_CTX_free};
if (!context) {
@@ -635,6 +863,12 @@ namespace Void::Crypto
"Crypto::sign_ed25519 failed")};
}
return signature;
#else
(void)provider;
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::sign_ed25519: built without OpenSSL backend")};
#endif
}
Void::Result::Result<bool> verifyEd25519(std::span<const std::byte> message,
@@ -682,6 +916,19 @@ namespace Void::Crypto
return std::unexpected{std::move(*fault)};
}
if (use_builtin_hash(HashAlgorithm::Sha256, provider)) {
if (!provider.property_query.empty()) {
secure_wipe(output);
return std::unexpected{make_crypto_error(
CryptoErrorCode::provider_error,
"Crypto::hmac_sha256_into: property_query unavailable on "
"built-in backend")};
}
Detail::builtin_hmac_sha256(secret_key, message, output);
return {};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
ERR_clear_error();
EvpMacPtr mac{EVP_MAC_fetch(nullptr, "HMAC", propq_or_null(provider)),
&EVP_MAC_free};
@@ -732,6 +979,12 @@ namespace Void::Crypto
"Crypto::hmac_sha256_into: EVP_MAC_final failed")};
}
return {};
#else
secure_wipe(output);
return std::unexpected{make_crypto_error(
CryptoErrorCode::backend_error,
"Crypto::hmac_sha256_into: OpenSSL backend unavailable")};
#endif
}
Void::Result::Result<Digest> hmac_sha256(std::span<const std::byte> secret_key,
@@ -780,6 +1033,20 @@ namespace Void::Crypto
return std::unexpected{std::move(*fault)};
}
if (use_builtin_hash(HashAlgorithm::Sha256, provider)) {
if (!provider.property_query.empty()) {
secure_wipe(secret_output);
return std::unexpected{make_crypto_error(
CryptoErrorCode::provider_error,
"Crypto::hkdf_sha256: property_query unavailable on "
"built-in backend")};
}
Detail::builtin_hkdf_sha256(secret_input, salt, context,
secret_output);
return {};
}
#if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL
ERR_clear_error();
EvpKdfPtr kdf{EVP_KDF_fetch(nullptr, "HKDF", propq_or_null(provider)),
&EVP_KDF_free};
@@ -825,6 +1092,12 @@ namespace Void::Crypto
"Crypto::hkdf_sha256: EVP_KDF_derive failed")};
}
return {};
#else
secure_wipe(secret_output);
return std::unexpected{make_crypto_error(
CryptoErrorCode::backend_error,
"Crypto::hkdf_sha256: OpenSSL backend unavailable")};
#endif
}
} // namespace Void::Crypto
+16 -1
View File
@@ -1,6 +1,21 @@
set(_void_crypto_ext_deps "")
set(_void_crypto_has_openssl 0)
if(NOT CMAKE_DISABLE_FIND_PACKAGE_OpenSSL)
if(NOT TARGET OpenSSL::Crypto)
find_package(OpenSSL QUIET)
endif()
if(TARGET OpenSSL::Crypto)
list(APPEND _void_crypto_ext_deps OpenSSL::Crypto)
set(_void_crypto_has_openssl 1)
endif()
endif()
void_add_module(Crypto
INTERNAL_DEPS Functional
EXTERNAL_DEPS OpenSSL::Crypto
EXTERNAL_DEPS ${_void_crypto_ext_deps}
EXCLUDE_HEADERS
Void/Crypto/AsyncHash.hpp
)
target_compile_definitions(Void_Crypto PUBLIC
VOID_CRYPTO_HAS_OPENSSL=${_void_crypto_has_openssl}
)