Commit c9c86705 authored by jan.koester's avatar jan.koester
Browse files

aes

parent 744e5e57
Loading
Loading
Loading
Loading
+12 −9
Changes for src/crypto/aes.cpp: 12 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -16,9 +16,14 @@
#endif
#include <wmmintrin.h>
#include <tmmintrin.h>
#include <smmintrin.h>

#if defined(__GNUC__) || defined(__clang__)
  #define AESNI_FUNC __attribute__((target("aes,ssse3,pclmul")))
  // sse4.1 (for _mm_insert_epi32 in make_ctr_block) is safe to assume
  // alongside aes/pclmul: every shipped x86 CPU with AES-NI (Westmere/
  // Bulldozer and later) also has SSE4.1/4.2 — there is no real-world AES-NI
  // CPU lacking it.
  #define AESNI_FUNC __attribute__((target("aes,ssse3,sse4.1,pclmul")))
#else
  #define AESNI_FUNC
#endif
@@ -111,15 +116,13 @@ static void aesni_decrypt_256(const uint8_t in[16], uint8_t out[16],

AESNI_FUNC
static inline __m128i make_ctr_block(const uint8_t base[16], uint32_t counter) {
    // Replace the last 4 bytes (lane 3) with the big-endian encoding of
    // `counter`, entirely in registers — no store/reload round trip through
    // memory. Byte-swapping `counter` first and inserting it as a native
    // (little-endian) 32-bit lane produces exactly the big-endian byte
    // sequence in memory order that the stack-buffer version built by hand.
    __m128i blk = _mm_loadu_si128(reinterpret_cast<const __m128i*>(base));
    // Replace last 4 bytes with big-endian counter
    alignas(16) uint8_t tmp[16];
    _mm_storeu_si128(reinterpret_cast<__m128i*>(tmp), blk);
    tmp[12] = uint8_t(counter >> 24);
    tmp[13] = uint8_t(counter >> 16);
    tmp[14] = uint8_t(counter >> 8);
    tmp[15] = uint8_t(counter);
    return _mm_loadu_si128(reinterpret_cast<const __m128i*>(tmp));
    return _mm_insert_epi32(blk, static_cast<int>(__builtin_bswap32(counter)), 3);
}

AESNI_FUNC
+2 −2
Changes for src/crypto/aes.h: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ public:
    ) const = 0;
};

class aes128 : public aes {
class aes128 final : public aes {
private:
    std::vector<uint8_t> m_roundKeyBytes; // 176 bytes for AES-128
    uint8_t m_H[16];                      // GHASH subkey
@@ -197,7 +197,7 @@ public:
    ) const override;
};

class aes256 : public aes {
class aes256 final : public aes {
private:
    std::vector<uint8_t> m_roundKeyBytes; // 240 bytes for AES-256
    uint8_t m_H[16];                      // GHASH subkey