Loading src/crypto/aes.cpp +200 −0 Changes for src/crypto/aes.cpp: 200 added lines, 0 removed lines. Original line number Diff line number Diff line #include "aes.h" // ============================================================ // Hardware acceleration: AES-NI + PCLMULQDQ (x86/x64 only) // Falls back to software on non-x86 or CPUs without AES-NI. // Works on GCC, Clang, and MSVC. // ============================================================ #if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86) #define NETPLUS_X86 1 #ifdef _MSC_VER #include <intrin.h> #else #include <cpuid.h> #endif #include <wmmintrin.h> #include <tmmintrin.h> #if defined(__GNUC__) || defined(__clang__) #define AESNI_FUNC __attribute__((target("aes,ssse3,pclmul"))) #else #define AESNI_FUNC #endif static bool detect_aesni_pclmul() { #ifdef _MSC_VER int info[4]; __cpuid(info, 1); bool aes = (info[2] & (1 << 25)) != 0; bool pclmul = (info[2] & (1 << 1)) != 0; return aes && pclmul; #else unsigned eax, ebx, ecx, edx; if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) { bool aes = (ecx & (1 << 25)) != 0; bool pclmul = (ecx & (1 << 1)) != 0; return aes && pclmul; } return false; #endif } static const bool g_has_hw_aes = detect_aesni_pclmul(); // ---- AES-NI encrypt/decrypt for AES-128 (10 rounds) ---- AESNI_FUNC static void aesni_encrypt_128(const uint8_t in[16], uint8_t out[16], const uint8_t* round_keys) { __m128i block = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in)); block = _mm_xor_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys))); for (int i = 1; i < 10; i++) block = _mm_aesenc_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16))); block = _mm_aesenclast_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 160))); _mm_storeu_si128(reinterpret_cast<__m128i*>(out), block); } AESNI_FUNC static void aesni_decrypt_128(const uint8_t in[16], uint8_t out[16], const uint8_t* round_keys) { __m128i block = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in)); block = _mm_xor_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 160))); for (int i = 9; i >= 1; i--) { __m128i rk = _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16)); block = _mm_aesdec_si128(block, _mm_aesimc_si128(rk)); } block = _mm_aesdeclast_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys))); _mm_storeu_si128(reinterpret_cast<__m128i*>(out), block); } // ---- AES-NI encrypt/decrypt for AES-256 (14 rounds) ---- AESNI_FUNC static void aesni_encrypt_256(const uint8_t in[16], uint8_t out[16], const uint8_t* round_keys) { __m128i block = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in)); block = _mm_xor_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys))); for (int i = 1; i < 14; i++) block = _mm_aesenc_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16))); block = _mm_aesenclast_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 224))); _mm_storeu_si128(reinterpret_cast<__m128i*>(out), block); } AESNI_FUNC static void aesni_decrypt_256(const uint8_t in[16], uint8_t out[16], const uint8_t* round_keys) { __m128i block = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in)); block = _mm_xor_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 224))); for (int i = 13; i >= 1; i--) { __m128i rk = _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16)); block = _mm_aesdec_si128(block, _mm_aesimc_si128(rk)); } block = _mm_aesdeclast_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys))); _mm_storeu_si128(reinterpret_cast<__m128i*>(out), block); } // ---- PCLMULQDQ-based GCM multiplication in GF(2^128) ---- // Uses full 128-bit reflection to convert between GCM bit-order // and CLMUL standard convention, then standard polynomial reduction. AESNI_FUNC static inline __m128i reflect128(__m128i v) { const __m128i bswap = _mm_set_epi8(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); v = _mm_shuffle_epi8(v, bswap); __m128i lo_mask = _mm_set1_epi8(0x0F); __m128i rev_lut = _mm_setr_epi8(0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15); __m128i hi = _mm_shuffle_epi8(rev_lut, _mm_and_si128(_mm_srli_epi16(v, 4), lo_mask)); __m128i lo = _mm_shuffle_epi8(rev_lut, _mm_and_si128(v, lo_mask)); return _mm_or_si128(_mm_slli_epi16(lo, 4), hi); } AESNI_FUNC static inline __m128i shl128(__m128i v, int k) { return _mm_or_si128( _mm_slli_epi64(v, k), _mm_slli_si128(_mm_srli_epi64(v, 64 - k), 8) ); } AESNI_FUNC static void gcm_mul_clmul(std::array<uint8_t,16>& X, const uint8_t H[16]) { __m128i x = reflect128(_mm_loadu_si128(reinterpret_cast<const __m128i*>(X.data()))); __m128i h = reflect128(_mm_loadu_si128(reinterpret_cast<const __m128i*>(H))); // Karatsuba CLMUL: 256-bit product [hi128:lo128] __m128i ll = _mm_clmulepi64_si128(x, h, 0x00); __m128i hh = _mm_clmulepi64_si128(x, h, 0x11); __m128i mid = _mm_xor_si128(_mm_clmulepi64_si128(x, h, 0x01), _mm_clmulepi64_si128(x, h, 0x10)); __m128i lo128 = _mm_xor_si128(ll, _mm_slli_si128(mid, 8)); __m128i hi128 = _mm_xor_si128(hh, _mm_srli_si128(mid, 8)); // Reduce [hi128:lo128] mod P(x) = x^128 + x^7 + x^2 + x + 1 // fold(hi128) = hi128 ^ (hi128<<1) ^ (hi128<<2) ^ (hi128<<7) __m128i s1 = shl128(hi128, 1); __m128i s2 = shl128(hi128, 2); __m128i s7 = shl128(hi128, 7); __m128i fold = _mm_xor_si128(hi128, _mm_xor_si128(s1, _mm_xor_si128(s2, s7))); // Overflow bits (128..134) from shifts of hi128 alignas(16) uint64_t hipair[2]; _mm_store_si128(reinterpret_cast<__m128i*>(hipair), hi128); uint64_t hi_hi = hipair[1]; uint64_t ov = (hi_hi >> 63) ^ (hi_hi >> 62) ^ (hi_hi >> 57); // Second-level fold of overflow (max 14 bits) uint64_t ov2 = ov ^ (ov << 1) ^ (ov << 2) ^ (ov << 7); __m128i result = _mm_xor_si128(lo128, fold); result = _mm_xor_si128(result, _mm_set_epi64x(0, static_cast<long long>(ov2))); _mm_storeu_si128(reinterpret_cast<__m128i*>(X.data()), reflect128(result)); } #endif // NETPLUS_X86 namespace netplus { // ============================================================ Loading Loading @@ -205,6 +369,12 @@ aes128::aes128(const key128& key) { // AES block encrypt/decrypt void aes128::encrypt_block(const uint8_t in[16], uint8_t out[16]) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { aesni_encrypt_128(in, out, m_roundKeyBytes.data()); return; } #endif uint8_t s[16]; std::memcpy(s, in, 16); Loading @@ -223,6 +393,12 @@ void aes128::encrypt_block(const uint8_t in[16], uint8_t out[16]) const { } void aes128::decrypt_block(const uint8_t in[16], uint8_t out[16]) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { aesni_decrypt_128(in, out, m_roundKeyBytes.data()); return; } #endif uint8_t s[16]; std::memcpy(s, in, 16); Loading Loading @@ -412,6 +588,12 @@ std::vector<uint8_t> aes128::decryptCTS(const std::vector<uint8_t>& ct, // GCM operations void aes128::gcm_mul(std::array<uint8_t,16>& X) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { gcm_mul_clmul(X, m_H); return; } #endif uint64_t Xhi = load_be64(X.data()); uint64_t Xlo = load_be64(X.data() + 8); Loading Loading @@ -754,6 +936,12 @@ aes256::aes256(const std::vector<uint8_t>& key) { } void aes256::encrypt_block(const uint8_t in[16], uint8_t out[16]) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { aesni_encrypt_256(in, out, m_roundKeyBytes.data()); return; } #endif uint8_t s[16]; std::memcpy(s, in, 16); Loading @@ -772,6 +960,12 @@ void aes256::encrypt_block(const uint8_t in[16], uint8_t out[16]) const { } void aes256::decrypt_block(const uint8_t in[16], uint8_t out[16]) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { aesni_decrypt_256(in, out, m_roundKeyBytes.data()); return; } #endif uint8_t s[16]; std::memcpy(s, in, 16); Loading Loading @@ -940,6 +1134,12 @@ std::vector<uint8_t> aes256::decryptCTS(const std::vector<uint8_t>& ct, } void aes256::gcm_mul(std::array<uint8_t,16>& X) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { gcm_mul_clmul(X, m_H); return; } #endif uint64_t Xhi = load_be64(X.data()); uint64_t Xlo = load_be64(X.data() + 8); Loading src/crypto/curve25519.cpp +27 −2 Changes for src/crypto/curve25519.cpp: 27 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -84,9 +84,34 @@ public: return out; } // S(o,a): square (TweetNaCl: S(o,a) = M(o,a,a)) // S(o,a): optimized square using symmetry (136 muls instead of 256) FieldElement square() const { return (*this) * (*this); int64_t t[31]; for (int i = 0; i < 31; ++i) t[i] = 0; // Off-diagonal: t[i+j] += 2 * limbs[i] * limbs[j] for i < j for (int i = 0; i < 15; ++i) for (int j = i + 1; j < 16; ++j) t[i + j] += limbs[i] * limbs[j]; // Double the off-diagonal contributions for (int i = 0; i < 31; ++i) t[i] *= 2; // Diagonal: t[2*i] += limbs[i]^2 for (int i = 0; i < 16; ++i) t[2 * i] += limbs[i] * limbs[i]; // Reduce and carry (same as operator*) for (int i = 0; i < 15; ++i) t[i] += 38 * t[i + 16]; FieldElement out; for (int i = 0; i < 16; ++i) out.limbs[i] = t[i]; out.carry25519(); out.carry25519(); return out; } // unpack25519 Loading Loading
src/crypto/aes.cpp +200 −0 Changes for src/crypto/aes.cpp: 200 added lines, 0 removed lines. Original line number Diff line number Diff line #include "aes.h" // ============================================================ // Hardware acceleration: AES-NI + PCLMULQDQ (x86/x64 only) // Falls back to software on non-x86 or CPUs without AES-NI. // Works on GCC, Clang, and MSVC. // ============================================================ #if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86) #define NETPLUS_X86 1 #ifdef _MSC_VER #include <intrin.h> #else #include <cpuid.h> #endif #include <wmmintrin.h> #include <tmmintrin.h> #if defined(__GNUC__) || defined(__clang__) #define AESNI_FUNC __attribute__((target("aes,ssse3,pclmul"))) #else #define AESNI_FUNC #endif static bool detect_aesni_pclmul() { #ifdef _MSC_VER int info[4]; __cpuid(info, 1); bool aes = (info[2] & (1 << 25)) != 0; bool pclmul = (info[2] & (1 << 1)) != 0; return aes && pclmul; #else unsigned eax, ebx, ecx, edx; if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) { bool aes = (ecx & (1 << 25)) != 0; bool pclmul = (ecx & (1 << 1)) != 0; return aes && pclmul; } return false; #endif } static const bool g_has_hw_aes = detect_aesni_pclmul(); // ---- AES-NI encrypt/decrypt for AES-128 (10 rounds) ---- AESNI_FUNC static void aesni_encrypt_128(const uint8_t in[16], uint8_t out[16], const uint8_t* round_keys) { __m128i block = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in)); block = _mm_xor_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys))); for (int i = 1; i < 10; i++) block = _mm_aesenc_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16))); block = _mm_aesenclast_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 160))); _mm_storeu_si128(reinterpret_cast<__m128i*>(out), block); } AESNI_FUNC static void aesni_decrypt_128(const uint8_t in[16], uint8_t out[16], const uint8_t* round_keys) { __m128i block = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in)); block = _mm_xor_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 160))); for (int i = 9; i >= 1; i--) { __m128i rk = _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16)); block = _mm_aesdec_si128(block, _mm_aesimc_si128(rk)); } block = _mm_aesdeclast_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys))); _mm_storeu_si128(reinterpret_cast<__m128i*>(out), block); } // ---- AES-NI encrypt/decrypt for AES-256 (14 rounds) ---- AESNI_FUNC static void aesni_encrypt_256(const uint8_t in[16], uint8_t out[16], const uint8_t* round_keys) { __m128i block = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in)); block = _mm_xor_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys))); for (int i = 1; i < 14; i++) block = _mm_aesenc_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16))); block = _mm_aesenclast_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 224))); _mm_storeu_si128(reinterpret_cast<__m128i*>(out), block); } AESNI_FUNC static void aesni_decrypt_256(const uint8_t in[16], uint8_t out[16], const uint8_t* round_keys) { __m128i block = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in)); block = _mm_xor_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 224))); for (int i = 13; i >= 1; i--) { __m128i rk = _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16)); block = _mm_aesdec_si128(block, _mm_aesimc_si128(rk)); } block = _mm_aesdeclast_si128(block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys))); _mm_storeu_si128(reinterpret_cast<__m128i*>(out), block); } // ---- PCLMULQDQ-based GCM multiplication in GF(2^128) ---- // Uses full 128-bit reflection to convert between GCM bit-order // and CLMUL standard convention, then standard polynomial reduction. AESNI_FUNC static inline __m128i reflect128(__m128i v) { const __m128i bswap = _mm_set_epi8(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); v = _mm_shuffle_epi8(v, bswap); __m128i lo_mask = _mm_set1_epi8(0x0F); __m128i rev_lut = _mm_setr_epi8(0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15); __m128i hi = _mm_shuffle_epi8(rev_lut, _mm_and_si128(_mm_srli_epi16(v, 4), lo_mask)); __m128i lo = _mm_shuffle_epi8(rev_lut, _mm_and_si128(v, lo_mask)); return _mm_or_si128(_mm_slli_epi16(lo, 4), hi); } AESNI_FUNC static inline __m128i shl128(__m128i v, int k) { return _mm_or_si128( _mm_slli_epi64(v, k), _mm_slli_si128(_mm_srli_epi64(v, 64 - k), 8) ); } AESNI_FUNC static void gcm_mul_clmul(std::array<uint8_t,16>& X, const uint8_t H[16]) { __m128i x = reflect128(_mm_loadu_si128(reinterpret_cast<const __m128i*>(X.data()))); __m128i h = reflect128(_mm_loadu_si128(reinterpret_cast<const __m128i*>(H))); // Karatsuba CLMUL: 256-bit product [hi128:lo128] __m128i ll = _mm_clmulepi64_si128(x, h, 0x00); __m128i hh = _mm_clmulepi64_si128(x, h, 0x11); __m128i mid = _mm_xor_si128(_mm_clmulepi64_si128(x, h, 0x01), _mm_clmulepi64_si128(x, h, 0x10)); __m128i lo128 = _mm_xor_si128(ll, _mm_slli_si128(mid, 8)); __m128i hi128 = _mm_xor_si128(hh, _mm_srli_si128(mid, 8)); // Reduce [hi128:lo128] mod P(x) = x^128 + x^7 + x^2 + x + 1 // fold(hi128) = hi128 ^ (hi128<<1) ^ (hi128<<2) ^ (hi128<<7) __m128i s1 = shl128(hi128, 1); __m128i s2 = shl128(hi128, 2); __m128i s7 = shl128(hi128, 7); __m128i fold = _mm_xor_si128(hi128, _mm_xor_si128(s1, _mm_xor_si128(s2, s7))); // Overflow bits (128..134) from shifts of hi128 alignas(16) uint64_t hipair[2]; _mm_store_si128(reinterpret_cast<__m128i*>(hipair), hi128); uint64_t hi_hi = hipair[1]; uint64_t ov = (hi_hi >> 63) ^ (hi_hi >> 62) ^ (hi_hi >> 57); // Second-level fold of overflow (max 14 bits) uint64_t ov2 = ov ^ (ov << 1) ^ (ov << 2) ^ (ov << 7); __m128i result = _mm_xor_si128(lo128, fold); result = _mm_xor_si128(result, _mm_set_epi64x(0, static_cast<long long>(ov2))); _mm_storeu_si128(reinterpret_cast<__m128i*>(X.data()), reflect128(result)); } #endif // NETPLUS_X86 namespace netplus { // ============================================================ Loading Loading @@ -205,6 +369,12 @@ aes128::aes128(const key128& key) { // AES block encrypt/decrypt void aes128::encrypt_block(const uint8_t in[16], uint8_t out[16]) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { aesni_encrypt_128(in, out, m_roundKeyBytes.data()); return; } #endif uint8_t s[16]; std::memcpy(s, in, 16); Loading @@ -223,6 +393,12 @@ void aes128::encrypt_block(const uint8_t in[16], uint8_t out[16]) const { } void aes128::decrypt_block(const uint8_t in[16], uint8_t out[16]) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { aesni_decrypt_128(in, out, m_roundKeyBytes.data()); return; } #endif uint8_t s[16]; std::memcpy(s, in, 16); Loading Loading @@ -412,6 +588,12 @@ std::vector<uint8_t> aes128::decryptCTS(const std::vector<uint8_t>& ct, // GCM operations void aes128::gcm_mul(std::array<uint8_t,16>& X) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { gcm_mul_clmul(X, m_H); return; } #endif uint64_t Xhi = load_be64(X.data()); uint64_t Xlo = load_be64(X.data() + 8); Loading Loading @@ -754,6 +936,12 @@ aes256::aes256(const std::vector<uint8_t>& key) { } void aes256::encrypt_block(const uint8_t in[16], uint8_t out[16]) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { aesni_encrypt_256(in, out, m_roundKeyBytes.data()); return; } #endif uint8_t s[16]; std::memcpy(s, in, 16); Loading @@ -772,6 +960,12 @@ void aes256::encrypt_block(const uint8_t in[16], uint8_t out[16]) const { } void aes256::decrypt_block(const uint8_t in[16], uint8_t out[16]) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { aesni_decrypt_256(in, out, m_roundKeyBytes.data()); return; } #endif uint8_t s[16]; std::memcpy(s, in, 16); Loading Loading @@ -940,6 +1134,12 @@ std::vector<uint8_t> aes256::decryptCTS(const std::vector<uint8_t>& ct, } void aes256::gcm_mul(std::array<uint8_t,16>& X) const { #ifdef NETPLUS_X86 if (g_has_hw_aes) { gcm_mul_clmul(X, m_H); return; } #endif uint64_t Xhi = load_be64(X.data()); uint64_t Xlo = load_be64(X.data() + 8); Loading
src/crypto/curve25519.cpp +27 −2 Changes for src/crypto/curve25519.cpp: 27 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -84,9 +84,34 @@ public: return out; } // S(o,a): square (TweetNaCl: S(o,a) = M(o,a,a)) // S(o,a): optimized square using symmetry (136 muls instead of 256) FieldElement square() const { return (*this) * (*this); int64_t t[31]; for (int i = 0; i < 31; ++i) t[i] = 0; // Off-diagonal: t[i+j] += 2 * limbs[i] * limbs[j] for i < j for (int i = 0; i < 15; ++i) for (int j = i + 1; j < 16; ++j) t[i + j] += limbs[i] * limbs[j]; // Double the off-diagonal contributions for (int i = 0; i < 31; ++i) t[i] *= 2; // Diagonal: t[2*i] += limbs[i]^2 for (int i = 0; i < 16; ++i) t[2 * i] += limbs[i] * limbs[i]; // Reduce and carry (same as operator*) for (int i = 0; i < 15; ++i) t[i] += 38 * t[i + 16]; FieldElement out; for (int i = 0; i < 16; ++i) out.limbs[i] = t[i]; out.carry25519(); out.carry25519(); return out; } // unpack25519 Loading