From 86cb94c844ef1529aa8db6bce597d4d99be4cab1 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Thu, 3 Dec 2026 17:58:13 +0300 Subject: [PATCH] Crypto: add a built-in Ed25519 backend (closes #52) --- src/Crypto/BuiltinEd25519.hpp | 482 ++++++++++++++++++++ src/Crypto/Crypto.cpp | 79 +++- tests/standalone/Crypto/CryptoHardening.cpp | 44 +- 3 files changed, 539 insertions(+), 66 deletions(-) create mode 100644 src/Crypto/BuiltinEd25519.hpp diff --git a/src/Crypto/BuiltinEd25519.hpp b/src/Crypto/BuiltinEd25519.hpp new file mode 100644 index 0000000..105dbe8 --- /dev/null +++ b/src/Crypto/BuiltinEd25519.hpp @@ -0,0 +1,482 @@ +#pragma once + +// Portable Ed25519 built on the module's own SHA-512 implementation. +// +// The field arithmetic follows the compact public-domain approach used by +// TweetNaCl: elements of GF(2^255-19) are 16 signed limbs of 16 bits and the +// group law is evaluated in extended coordinates. Secret scalars and the +// hash-derived key material are wiped before returning. + +#include "BuiltinHash.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace Void::Crypto::Detail::Ed25519 +{ + using U8 = std::uint8_t; + using I64 = std::int64_t; + using Gf = std::array; + using Point = std::array; + using Sha512 = std::array; + + inline constexpr Gf gf0{}; + inline constexpr Gf gf1{1}; + + // Curve constants in 16-bit limbs (GF(2^255-19)). + inline constexpr Gf D{ + 0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, + 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203}; + inline constexpr Gf D2{ + 0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, + 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406}; + inline constexpr Gf I{ + 0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, + 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83}; + inline constexpr Gf base_x{ + 0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, + 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169}; + inline constexpr Gf base_y{ + 0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, + 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666}; + + // Group order L = 2^252 + 27742317777372353535851937790883648493. + inline constexpr std::array L{ + 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, + 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0x10}; + + inline void wipe(std::span bytes) noexcept + { + builtin_secure_wipe(std::span{ + reinterpret_cast(bytes.data()), bytes.size()}); + } + + inline void hash_sha512(std::initializer_list> parts, + Sha512& out) + { + BuiltinHasher hasher; + if (!hasher.init(HashAlgorithm::Sha512)) { + out.fill(0); + return; + } + for (const auto part : parts) { + hasher.update(std::span{ + reinterpret_cast(part.data()), part.size()}); + } + const Digest digest = hasher.finalize(); + const std::size_t count = std::min(digest.size(), out.size()); + std::memcpy(out.data(), digest.data(), count); + if (count < out.size()) { + std::fill(out.begin() + static_cast(count), out.end(), 0); + } + } + + [[nodiscard]] inline int verify_32(const U8* a, const U8* b) noexcept + { + unsigned int diff = 0; + for (int i = 0; i < 32; ++i) { + diff |= static_cast(a[i] ^ b[i]); + } + return static_cast((1 & ((diff - 1) >> 8)) - 1); + } + + inline void set25519(Gf& r, const Gf& a) noexcept + { + r = a; + } + + inline void car25519(Gf& o) noexcept + { + for (int i = 0; i < 16; ++i) { + o[i] += I64{1} << 16; + const I64 c = o[i] >> 16; + o[(i + 1) * (i < 15)] += c - 1 + 37 * (c - 1) * (i == 15); + o[i] -= c << 16; + } + } + + inline void sel25519(Gf& p, Gf& q, int b) noexcept + { + const I64 c = ~(static_cast(b) - 1); + for (int i = 0; i < 16; ++i) { + const I64 t = c & (p[i] ^ q[i]); + p[i] ^= t; + q[i] ^= t; + } + } + + inline void pack25519(U8* o, const Gf& n) noexcept + { + Gf m{}; + Gf t = n; + car25519(t); + car25519(t); + car25519(t); + for (int j = 0; j < 2; ++j) { + m[0] = t[0] - 0xffed; + for (int i = 1; i < 15; ++i) { + m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1); + m[i - 1] &= 0xffff; + } + m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1); + const int b = static_cast((m[15] >> 16) & 1); + m[14] &= 0xffff; + sel25519(t, m, 1 - b); + } + for (int i = 0; i < 16; ++i) { + o[2 * i] = static_cast(t[i] & 0xff); + o[2 * i + 1] = static_cast(t[i] >> 8); + } + } + + [[nodiscard]] inline int neq25519(const Gf& a, const Gf& b) noexcept + { + std::array c{}; + std::array d{}; + pack25519(c.data(), a); + pack25519(d.data(), b); + return verify_32(c.data(), d.data()); + } + + [[nodiscard]] inline U8 par25519(const Gf& a) noexcept + { + std::array d{}; + pack25519(d.data(), a); + return d[0] & 1; + } + + inline void unpack25519(Gf& o, const U8* n) noexcept + { + for (int i = 0; i < 16; ++i) { + o[i] = n[2 * i] + (static_cast(n[2 * i + 1]) << 8); + } + o[15] &= 0x7fff; + } + + inline void add(Gf& o, const Gf& a, const Gf& b) noexcept + { + for (int i = 0; i < 16; ++i) { + o[i] = a[i] + b[i]; + } + } + + inline void sub(Gf& o, const Gf& a, const Gf& b) noexcept + { + for (int i = 0; i < 16; ++i) { + o[i] = a[i] - b[i]; + } + } + + inline void mul(Gf& o, const Gf& a, const Gf& b) noexcept + { + std::array t{}; + for (int i = 0; i < 16; ++i) { + for (int j = 0; j < 16; ++j) { + t[i + j] += a[i] * b[j]; + } + } + for (int i = 0; i < 15; ++i) { + t[i] += 38 * t[i + 16]; + } + for (int i = 0; i < 16; ++i) { + o[i] = t[i]; + } + car25519(o); + car25519(o); + } + + inline void square(Gf& o, const Gf& a) noexcept + { + mul(o, a, a); + } + + inline void invert(Gf& o, const Gf& in) noexcept + { + Gf c = in; + for (int a = 253; a >= 0; --a) { + square(c, c); + if (a != 2 && a != 4) { + mul(c, c, in); + } + } + o = c; + } + + inline void pow2523(Gf& o, const Gf& in) noexcept + { + Gf c = in; + for (int a = 250; a >= 0; --a) { + square(c, c); + if (a != 1) { + mul(c, c, in); + } + } + o = c; + } + + inline void point_add(Point& p, const Point& q) noexcept + { + Gf a{}; + Gf b{}; + Gf c{}; + Gf d{}; + Gf e{}; + Gf f{}; + Gf g{}; + Gf h{}; + Gf t{}; + sub(a, p[1], p[0]); + sub(t, q[1], q[0]); + mul(a, a, t); + add(b, p[0], p[1]); + add(t, q[0], q[1]); + mul(b, b, t); + mul(c, p[3], q[3]); + mul(c, c, D2); + mul(d, p[2], q[2]); + add(d, d, d); + sub(e, b, a); + sub(f, d, c); + add(g, d, c); + add(h, b, a); + mul(p[0], e, f); + mul(p[1], h, g); + mul(p[2], g, f); + mul(p[3], e, h); + } + + inline void point_cswap(Point& p, Point& q, int b) noexcept + { + for (int i = 0; i < 4; ++i) { + sel25519(p[i], q[i], b); + } + } + + inline void point_pack(U8* r, const Point& p) noexcept + { + Gf tx{}; + Gf ty{}; + Gf zi{}; + invert(zi, p[2]); + mul(tx, p[0], zi); + mul(ty, p[1], zi); + pack25519(r, ty); + r[31] ^= static_cast(par25519(tx) << 7); + } + + inline void scalarmult(Point& p, Point& q, const U8* s) noexcept + { + p[0].fill(0); + p[1].fill(0); + p[2].fill(0); + p[3].fill(0); + p[1][0] = 1; + p[2][0] = 1; + for (int i = 255; i >= 0; --i) { + const int b = (s[i / 8] >> (i & 7)) & 1; + point_cswap(p, q, b); + point_add(q, p); + point_add(p, p); + point_cswap(p, q, b); + } + } + + inline void scalarbase(Point& p, const U8* s) noexcept + { + Point q{}; + set25519(q[0], base_x); + set25519(q[1], base_y); + set25519(q[2], gf1); + mul(q[3], base_x, base_y); + scalarmult(p, q, s); + } + + inline int unpackneg(Point& r, const U8* p) noexcept + { + Gf t{}; + Gf chk{}; + Gf num{}; + Gf den{}; + Gf den2{}; + Gf den4{}; + Gf den6{}; + set25519(r[2], gf1); + unpack25519(r[1], p); + square(num, r[1]); + mul(den, num, D); + sub(num, num, r[2]); + add(den, r[2], den); + + square(den2, den); + square(den4, den2); + mul(den6, den4, den2); + mul(t, den6, num); + mul(t, t, den); + + pow2523(t, t); + mul(t, t, num); + mul(t, t, den); + mul(t, t, den); + mul(r[0], t, den); + + square(chk, r[0]); + mul(chk, chk, den); + if (neq25519(chk, num) != 0) { + mul(r[0], r[0], I); + } + + square(chk, r[0]); + mul(chk, chk, den); + if (neq25519(chk, num) != 0) { + return -1; + } + + if (par25519(r[0]) == (p[31] >> 7)) { + sub(r[0], gf0, r[0]); + } + + mul(r[3], r[0], r[1]); + return 0; + } + + inline void modL(U8* r, std::array& x) noexcept + { + I64 carry = 0; + int i = 0; + int j = 0; + for (i = 63; i >= 32; --i) { + carry = 0; + for (j = i - 32; j < i - 12; ++j) { + x[j] += carry - 16 * x[i] * L[j - (i - 32)]; + carry = (x[j] + 128) >> 8; + x[j] -= carry << 8; + } + x[j] += carry; + x[i] = 0; + } + carry = 0; + for (j = 0; j < 32; ++j) { + x[j] += carry - (x[31] >> 4) * L[j]; + carry = x[j] >> 8; + x[j] &= 255; + } + for (j = 0; j < 32; ++j) { + x[j] -= carry * L[j]; + } + for (i = 0; i < 32; ++i) { + x[i + 1] += x[i] >> 8; + r[i] = static_cast(x[i] & 255); + } + } + + inline void reduce(U8* r) noexcept + { + std::array x{}; + for (int i = 0; i < 64; ++i) { + x[i] = r[i]; + } + std::fill(r, r + 64, U8{0}); + modL(r, x); + } + + /// Derive the public key and the clamped secret scalar from a 32-byte seed. + inline void expand_seed(const U8* seed, Sha512& digest, U8* public_key) noexcept + { + hash_sha512({std::span{seed, 32}}, digest); + digest[0] &= 248; + digest[31] &= 127; + digest[31] |= 64; + Point p{}; + scalarbase(p, digest.data()); + if (public_key != nullptr) { + point_pack(public_key, p); + } + } + + inline void sign(const U8* seed, const U8* public_key, + std::span message, U8* signature) noexcept + { + Sha512 digest{}; + Sha512 nonce{}; + Sha512 challenge{}; + Point p{}; + expand_seed(seed, digest, nullptr); + + const auto message_bytes = std::span{ + reinterpret_cast(message.data()), message.size()}; + hash_sha512({std::span{digest.data() + 32, 32}, message_bytes}, + nonce); + reduce(nonce.data()); + scalarbase(p, nonce.data()); + point_pack(signature, p); + + hash_sha512({std::span{signature, 32}, + std::span{public_key, 32}, message_bytes}, + challenge); + reduce(challenge.data()); + + std::array x{}; + for (int i = 0; i < 32; ++i) { + x[i] = nonce[i]; + } + for (int i = 0; i < 32; ++i) { + for (int j = 0; j < 32; ++j) { + x[i + j] += static_cast(challenge[i]) * digest[j]; + } + } + modL(signature + 32, x); + + wipe(digest); + wipe(nonce); + wipe(challenge); + } + + [[nodiscard]] inline bool verify(const U8* public_key, const U8* signature, + std::span message) noexcept + { + // Reject a non-canonical scalar S >= L (and the top three bits, which + // must be clear because L < 2^253). + if ((signature[63] & 224) != 0) { + return false; + } + std::array s_wide{}; + std::memcpy(s_wide.data(), signature + 32, 32); + reduce(s_wide.data()); + if (verify_32(s_wide.data(), signature + 32) != 0) { + return false; + } + + Point q{}; + Point p{}; + if (unpackneg(q, public_key) != 0) { + return false; + } + + Sha512 challenge{}; + hash_sha512({std::span{signature, 32}, + std::span{public_key, 32}, + std::span{ + reinterpret_cast(message.data()), + message.size()}}, + challenge); + reduce(challenge.data()); + + scalarmult(p, q, challenge.data()); + scalarbase(q, signature + 32); + point_add(p, q); + std::array check{}; + point_pack(check.data(), p); + const bool ok = verify_32(signature, check.data()) == 0; + wipe(challenge); + return ok; + } +} // namespace Void::Crypto::Detail::Ed25519 diff --git a/src/Crypto/Crypto.cpp b/src/Crypto/Crypto.cpp index 04627de..615b9a2 100644 --- a/src/Crypto/Crypto.cpp +++ b/src/Crypto/Crypto.cpp @@ -1,7 +1,9 @@ #include +#include "BuiltinEd25519.hpp" #include "BuiltinHash.hpp" +#include #include #include #include @@ -9,6 +11,7 @@ #include #include #include +#include #include #if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL @@ -217,9 +220,9 @@ namespace Void::Crypto return result == 1; #else (void)provider; - return std::unexpected{make_crypto_error( - CryptoErrorCode::unsupported_algorithm, - "Crypto::verifyEd25519: built without OpenSSL backend")}; + return Detail::Ed25519::verify( + reinterpret_cast(public_key.data()), + reinterpret_cast(signature.data()), message); #endif } @@ -771,10 +774,27 @@ namespace Void::Crypto #if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL EvpPkeyPtr pkey{nullptr, &EVP_PKEY_free}; #else - std::uint8_t unused{0}; + std::array seed{}; + std::array public_key{}; #endif }; +#if !defined(VOID_CRYPTO_HAS_OPENSSL) || !VOID_CRYPTO_HAS_OPENSSL + namespace + { + void derive_ed25519_public_key( + const std::array& seed, + std::array& public_key) + { + Detail::Ed25519::Sha512 digest{}; + Detail::Ed25519::expand_seed( + reinterpret_cast(seed.data()), digest, + reinterpret_cast(public_key.data())); + Detail::Ed25519::wipe(digest); + } + } // namespace +#endif + Ed25519PrivateKey::Ed25519PrivateKey(std::unique_ptr impl) noexcept : m_impl(std::move(impl)) { @@ -790,7 +810,7 @@ namespace Void::Crypto #if defined(VOID_CRYPTO_HAS_OPENSSL) && VOID_CRYPTO_HAS_OPENSSL return m_impl != nullptr && m_impl->pkey != nullptr; #else - return false; + return m_impl != nullptr; #endif } @@ -822,10 +842,20 @@ namespace Void::Crypto return Ed25519PrivateKey{std::move(impl)}; #else (void)provider; - return std::unexpected{make_crypto_error( - CryptoErrorCode::unsupported_algorithm, - "Crypto::Ed25519PrivateKey::generate: built without OpenSSL " - "backend")}; + std::random_device random; + std::array seed{}; + for (std::size_t offset = 0; offset < seed.size();) { + const std::uint32_t word = random(); + const std::size_t count = + std::min(sizeof(word), seed.size() - offset); + std::memcpy(seed.data() + offset, &word, count); + offset += count; + } + auto impl = std::make_unique(); + impl->seed = seed; + derive_ed25519_public_key(impl->seed, impl->public_key); + secure_wipe(seed); + return Ed25519PrivateKey{std::move(impl)}; #endif } @@ -868,10 +898,10 @@ namespace Void::Crypto 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")}; + auto impl = std::make_unique(); + std::memcpy(impl->seed.data(), seed.data(), Ed25519SeedSize); + derive_ed25519_public_key(impl->seed, impl->public_key); + return Ed25519PrivateKey{std::move(impl)}; #endif } @@ -897,10 +927,10 @@ namespace Void::Crypto } return pub; #else - return std::unexpected{make_crypto_error( - CryptoErrorCode::unsupported_algorithm, - "Crypto::Ed25519PrivateKey::public_key: built without OpenSSL " - "backend")}; + Ed25519PublicKey pub; + std::memcpy(pub.bytes.data(), m_impl->public_key.data(), + Ed25519PublicKeySize); + return pub; #endif } @@ -928,10 +958,7 @@ namespace Void::Crypto secure_wipe(raw); return result; #else - return std::unexpected{make_crypto_error( - CryptoErrorCode::unsupported_algorithm, - "Crypto::Ed25519PrivateKey::export_raw_seed: built without OpenSSL " - "backend")}; + return Ed25519Seed::from_bytes(m_impl->seed); #endif } @@ -994,9 +1021,13 @@ namespace Void::Crypto return signature; #else (void)provider; - return std::unexpected{make_crypto_error( - CryptoErrorCode::unsupported_algorithm, - "Crypto::sign_ed25519: built without OpenSSL backend")}; + Ed25519Signature signature; + Detail::Ed25519::sign( + reinterpret_cast(private_key.m_impl->seed.data()), + reinterpret_cast( + private_key.m_impl->public_key.data()), + message, reinterpret_cast(signature.bytes.data())); + return signature; #endif } diff --git a/tests/standalone/Crypto/CryptoHardening.cpp b/tests/standalone/Crypto/CryptoHardening.cpp index 6fe6f63..f7d73fc 100644 --- a/tests/standalone/Crypto/CryptoHardening.cpp +++ b/tests/standalone/Crypto/CryptoHardening.cpp @@ -123,35 +123,8 @@ Void::Testing::Test crypto_ed25519_signing_vectors_and_boundaries() { using namespace Void::Crypto; - if (!has_openssl_backend()) { - auto gen_unavail = Ed25519PrivateKey::generate(); - Void::Testing::require(!gen_unavail.has_value()); - Void::Testing::expect_eq( - gen_unavail.error().code().value, - static_cast(CryptoErrorCode::unsupported_algorithm)); - - const std::array dummy_pub{}; - const std::array dummy_sig{}; - for (const std::size_t bad_key_len : {0U, 1U, 31U, 33U, 64U}) { - std::vector bad_key(bad_key_len, std::byte{0x11}); - auto r = verifyEd25519({}, dummy_sig, bad_key); - Void::Testing::require(!r.has_value()); - Void::Testing::expect_eq( - r.error().code().value, - static_cast(CryptoErrorCode::invalid_key_size)); - } - for (const std::size_t bad_sig_len : {0U, 1U, 63U, 65U}) { - std::vector bad_sig(bad_sig_len, std::byte{0x22}); - auto r = verifyEd25519({}, bad_sig, dummy_pub); - Void::Testing::require(!r.has_value()); - Void::Testing::expect_eq( - r.error().code().value, - static_cast( - CryptoErrorCode::invalid_signature_size)); - } - return; - } - + // Runs on the OpenSSL backend and on the portable built-in backend, so + // the same RFC 8032 vectors qualify both. struct Rfc8032Vector { std::string_view seed_hex; std::string_view pub_hex; @@ -286,19 +259,6 @@ Void::Testing::Test crypto_ed25519_convenience_api() "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e06522490155" "5fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b"); - if (!has_openssl_backend()) { - auto keypair = ed25519_keypair(); - Void::Testing::require(!keypair.has_value()); - Void::Testing::expect_eq( - keypair.error().code().value, - static_cast(CryptoErrorCode::unsupported_algorithm)); - auto signature = ed25519_sign(seed, {}); - Void::Testing::require(!signature.has_value()); - auto verified = ed25519_verify(public_key, {}, expected_signature); - Void::Testing::require(!verified.has_value()); - return; - } - auto signature = ed25519_sign(seed, {}); Void::Testing::require(signature.has_value()); Void::Testing::expect_eq(to_hex(signature->view()),