#include "BuiltinHash.hpp" #include #include #include #include #include #include #include #include #include namespace Void::Crypto::Detail { namespace { [[nodiscard]] inline std::uint32_t load_be32(const std::byte* p) noexcept { return (static_cast(std::to_integer(p[0])) << 24U) | (static_cast(std::to_integer(p[1])) << 16U) | (static_cast(std::to_integer(p[2])) << 8U) | static_cast(std::to_integer(p[3])); } inline void store_be32(std::byte* p, std::uint32_t v) noexcept { p[0] = static_cast((v >> 24U) & 0xFFU); p[1] = static_cast((v >> 16U) & 0xFFU); p[2] = static_cast((v >> 8U) & 0xFFU); p[3] = static_cast(v & 0xFFU); } [[nodiscard]] inline std::uint64_t load_be64(const std::byte* p) noexcept { return (static_cast(load_be32(p)) << 32U) | static_cast(load_be32(p + 4)); } inline void store_be64(std::byte* p, std::uint64_t v) noexcept { store_be32(p, static_cast((v >> 32U) & 0xFFFFFFFFU)); store_be32(p + 4, static_cast(v & 0xFFFFFFFFU)); } [[nodiscard]] inline std::uint64_t load_le64(const std::byte* p) noexcept { return static_cast(std::to_integer(p[0])) | (static_cast(std::to_integer(p[1])) << 8U) | (static_cast(std::to_integer(p[2])) << 16U) | (static_cast(std::to_integer(p[3])) << 24U) | (static_cast(std::to_integer(p[4])) << 32U) | (static_cast(std::to_integer(p[5])) << 40U) | (static_cast(std::to_integer(p[6])) << 48U) | (static_cast(std::to_integer(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((v >> (8U * i)) & 0xFFU); } } // ── FIPS 180-4 SHA-1 ───────────────────────────────────────────────── struct Sha1State { std::array h{0x67452301U, 0xefcdab89U, 0x98badcfeU, 0x10325476U, 0xc3d2e1f0U}; std::array buf{}; std::size_t buf_len{0}; std::uint64_t total_bytes{0}; void compress(const std::byte* block) noexcept { std::array 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 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 out20) noexcept { buf[buf_len++] = std::byte{0x80}; if (buf_len > 56) { std::fill(buf.begin() + static_cast(buf_len), buf.end(), std::byte{0}); compress(buf.data()); buf_len = 0; } std::fill(buf.begin() + static_cast(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 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 h{0x6a09e667U, 0xbb67ae85U, 0x3c6ef372U, 0xa54ff53aU, 0x510e527fU, 0x9b05688cU, 0x1f83d9abU, 0x5be0cd19U}; std::array buf{}; std::size_t buf_len{0}; std::uint64_t total_bytes{0}; void compress(const std::byte* block) noexcept { std::array 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 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 out32) noexcept { buf[buf_len++] = std::byte{0x80}; if (buf_len > 56) { std::fill(buf.begin() + static_cast(buf_len), buf.end(), std::byte{0}); compress(buf.data()); buf_len = 0; } std::fill(buf.begin() + static_cast(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 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 h{ 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL}; std::array buf{}; std::size_t buf_len{0}; std::uint64_t total_bytes{0}; void compress(const std::byte* block) noexcept { std::array 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 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 out64) noexcept { buf[buf_len++] = std::byte{0x80}; if (buf_len > 112) { std::fill(buf.begin() + static_cast(buf_len), buf.end(), std::byte{0}); compress(buf.data()); buf_len = 0; } std::fill(buf.begin() + static_cast(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 kIv = { 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL}; static constexpr std::array, 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 h{kIv}; std::array t{0, 0}; std::array 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(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 m{}; for (std::size_t i = 0; i < 16; ++i) { m[i] = load_le64(block + i * 8); } std::array 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 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 out) noexcept { add_counter(static_cast(buf_len)); std::fill(buf.begin() + static_cast(buf_len), buf.end(), std::byte{0}); compress(buf.data(), true); std::array 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 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 lhs, std::span 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(lhs[i]); const auto b = static_cast(rhs[i]); diff = static_cast(diff | (a ^ b)); } std::atomic_signal_fence(std::memory_order_seq_cst); return diff == 0; } struct BuiltinHasher::State { std::variant impl{}; void wipe() noexcept { std::visit( [](auto& st) { using T = std::decay_t; if constexpr (!std::is_same_v) { st.wipe(); } }, impl); impl.template emplace(); } }; 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(); return true; case HashAlgorithm::Sha256: m_state->impl.emplace(); return true; case HashAlgorithm::Sha512: m_state->impl.emplace(); return true; case HashAlgorithm::Blake2b256: { auto& b = m_state->impl.emplace(); b.init(32); return true; } case HashAlgorithm::Blake2b512: { auto& b = m_state->impl.emplace(); b.init(64); return true; } } return false; } void BuiltinHasher::update(std::span data) noexcept { if (m_state == nullptr || data.empty()) { return; } std::visit( [&](auto& st) { using T = std::decay_t; if constexpr (!std::is_same_v) { 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; if constexpr (std::is_same_v) { out.resize(20); st.finalize_into(out); } else if constexpr (std::is_same_v) { out.resize(32); st.finalize_into(out); } else if constexpr (std::is_same_v) { out.resize(64); st.finalize_into(out); } else if constexpr (std::is_same_v) { out.resize(st.out_len); st.finalize_into(out); } }, m_state->impl); return out; } void builtin_hmac_sha256(std::span secret_key, std::span message, std::span output_32) noexcept { std::array 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 ipad{}; std::array 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 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 secret_input, std::span salt, std::span context, std::span secret_output) noexcept { std::array zero_salt{}; const auto effective_salt = salt.empty() ? std::span{zero_salt} : salt; std::array prk{}; builtin_hmac_sha256(effective_salt, secret_input, prk); std::array t{}; std::size_t t_len = 0; std::size_t written = 0; std::uint8_t counter = 1; while (written < secret_output.size()) { std::array ipad{}; std::array 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 inner_hash{}; Sha256State inner; inner.update(ipad); if (t_len > 0) { inner.update(std::span{t.data(), t_len}); } if (!context.empty()) { inner.update(context); } const std::byte ctr_byte = static_cast(counter); inner.update(std::span{&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(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