#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