Commit 77be4634 authored by jan.koester's avatar jan.koester
Browse files

test

parent c9c86705
Loading
Loading
Loading
Loading
+316 −14
Changes for src/crypto/aes.cpp: 316 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -531,6 +531,154 @@ static void aesni_gcm_ctr_ghash_128(
    _mm_storeu_si128(reinterpret_cast<__m128i*>(tag16), tag);
}

// ---- Interleaved AES-CTR-decrypt + GHASH for AES-128 GCM (decrypt side) ----
// Unlike the encrypt direction, the ciphertext GHASH needs is already fully
// available up front (it's the input, `in`) rather than being produced
// block-by-block — so GHASH-of-ciphertext and CTR-decrypt of that same
// block have no data dependency on each other and can be computed from the
// same loaded ciphertext register, instead of the old two-pass shape (one
// full GHASH pass over `ct`, then a full separate CTR pass). Writes
// plaintext to `out` and the *computed* tag to `tag16` — the caller (see
// aes128::aes_gcm_decrypt) compares it against the wire tag itself.
AESNI_FUNC
static void aesni_gcm_ctr_ghash_decrypt_128(
    const uint8_t* in, uint8_t* out, size_t len,
    const uint8_t J0[16],
    const uint8_t* round_keys,
    const uint8_t* aad, size_t aad_len,
    const uint8_t H[16],
    uint8_t tag16[16]
) {
    __m128i H_ref = reflect128(_mm_loadu_si128(reinterpret_cast<const __m128i*>(H)));
    __m128i Y = _mm_setzero_si128(); // GHASH accumulator

    // Process AAD first
    {
        size_t aoff = 0;
        while (aoff + 16 <= aad_len) {
            __m128i a = _mm_loadu_si128(reinterpret_cast<const __m128i*>(aad + aoff));
            Y = ghash_update(Y, a, H_ref);
            aoff += 16;
        }
        if (aoff < aad_len) {
            alignas(16) uint8_t last[16] = {};
            std::memcpy(last, aad + aoff, aad_len - aoff);
            Y = ghash_update(Y, _mm_loadu_si128(reinterpret_cast<const __m128i*>(last)), H_ref);
        }
    }

    uint32_t ctr = (uint32_t(J0[12]) << 24) | (uint32_t(J0[13]) << 16) |
                   (uint32_t(J0[14]) << 8)  |  uint32_t(J0[15]);
    size_t off = 0;

    // Main loop: 4 blocks at a time. Ciphertext is loaded once per block and
    // used for both GHASH (immediately) and the CTR XOR (once the keystream
    // is ready) — no dependency chain between the two, unlike the encrypt
    // side.
    while (off + 64 <= len) {
        __m128i ct0 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off));
        __m128i ct1 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 16));
        __m128i ct2 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 32));
        __m128i ct3 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 48));

        Y = ghash_update(Y, ct0, H_ref);
        Y = ghash_update(Y, ct1, H_ref);
        Y = ghash_update(Y, ct2, H_ref);
        Y = ghash_update(Y, ct3, H_ref);

        __m128i c0 = make_ctr_block(J0, ++ctr);
        __m128i c1 = make_ctr_block(J0, ++ctr);
        __m128i c2 = make_ctr_block(J0, ++ctr);
        __m128i c3 = make_ctr_block(J0, ++ctr);

        __m128i rk0 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys));
        c0 = _mm_xor_si128(c0, rk0);
        c1 = _mm_xor_si128(c1, rk0);
        c2 = _mm_xor_si128(c2, rk0);
        c3 = _mm_xor_si128(c3, rk0);

        for (int i = 1; i < 10; i++) {
            __m128i rk = _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16));
            c0 = _mm_aesenc_si128(c0, rk);
            c1 = _mm_aesenc_si128(c1, rk);
            c2 = _mm_aesenc_si128(c2, rk);
            c3 = _mm_aesenc_si128(c3, rk);
        }

        __m128i rkLast = _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 160));
        c0 = _mm_aesenclast_si128(c0, rkLast);
        c1 = _mm_aesenclast_si128(c1, rkLast);
        c2 = _mm_aesenclast_si128(c2, rkLast);
        c3 = _mm_aesenclast_si128(c3, rkLast);

        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off),      _mm_xor_si128(c0, ct0));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 16), _mm_xor_si128(c1, ct1));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 32), _mm_xor_si128(c2, ct2));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 48), _mm_xor_si128(c3, ct3));

        off += 64;
    }

    // Remaining full blocks
    while (off + 16 <= len) {
        __m128i ct_blk = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off));
        Y = ghash_update(Y, ct_blk, H_ref);

        __m128i c = make_ctr_block(J0, ++ctr);
        c = _mm_xor_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys)));
        for (int i = 1; i < 10; i++)
            c = _mm_aesenc_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16)));
        c = _mm_aesenclast_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 160)));

        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off), _mm_xor_si128(c, ct_blk));
        off += 16;
    }

    // Final partial block
    if (off < len) {
        alignas(16) uint8_t partial[16] = {};
        size_t rem = len - off;
        std::memcpy(partial, in + off, rem);
        Y = ghash_update(Y, _mm_loadu_si128(reinterpret_cast<const __m128i*>(partial)), H_ref);

        __m128i c = make_ctr_block(J0, ++ctr);
        c = _mm_xor_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys)));
        for (int i = 1; i < 10; i++)
            c = _mm_aesenc_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16)));
        c = _mm_aesenclast_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 160)));

        alignas(16) uint8_t ks[16];
        _mm_storeu_si128(reinterpret_cast<__m128i*>(ks), c);
        for (size_t i = 0; i < rem; i++)
            out[off + i] = in[off + i] ^ ks[i];
    }

    // Finalize GHASH: append length block
    alignas(16) uint8_t lenblk[16] = {};
    uint64_t aad_bits = uint64_t(aad_len) * 8;
    uint64_t ct_bits  = uint64_t(len) * 8;
    lenblk[0] = uint8_t(aad_bits >> 56); lenblk[1] = uint8_t(aad_bits >> 48);
    lenblk[2] = uint8_t(aad_bits >> 40); lenblk[3] = uint8_t(aad_bits >> 32);
    lenblk[4] = uint8_t(aad_bits >> 24); lenblk[5] = uint8_t(aad_bits >> 16);
    lenblk[6] = uint8_t(aad_bits >> 8);  lenblk[7] = uint8_t(aad_bits);
    lenblk[8]  = uint8_t(ct_bits >> 56); lenblk[9]  = uint8_t(ct_bits >> 48);
    lenblk[10] = uint8_t(ct_bits >> 40); lenblk[11] = uint8_t(ct_bits >> 32);
    lenblk[12] = uint8_t(ct_bits >> 24); lenblk[13] = uint8_t(ct_bits >> 16);
    lenblk[14] = uint8_t(ct_bits >> 8);  lenblk[15] = uint8_t(ct_bits);

    Y = ghash_update(Y, _mm_loadu_si128(reinterpret_cast<const __m128i*>(lenblk)), H_ref);

    // Encrypt J0 for tag
    __m128i j0_block = _mm_loadu_si128(reinterpret_cast<const __m128i*>(J0));
    j0_block = _mm_xor_si128(j0_block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys)));
    for (int i = 1; i < 10; i++)
        j0_block = _mm_aesenc_si128(j0_block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16)));
    j0_block = _mm_aesenclast_si128(j0_block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 160)));

    __m128i tag = _mm_xor_si128(j0_block, Y);
    _mm_storeu_si128(reinterpret_cast<__m128i*>(tag16), tag);
}

// ---- Interleaved AES-CTR + GHASH for AES-256 GCM ----
AESNI_FUNC
static void aesni_gcm_ctr_ghash_256(
@@ -671,6 +819,139 @@ static void aesni_gcm_ctr_ghash_256(
    _mm_storeu_si128(reinterpret_cast<__m128i*>(tag16), tag);
}

// ---- Interleaved AES-CTR-decrypt + GHASH for AES-256 GCM (decrypt side) ----
// See aesni_gcm_ctr_ghash_decrypt_128's comment — same fusion, 14 rounds.
AESNI_FUNC
static void aesni_gcm_ctr_ghash_decrypt_256(
    const uint8_t* in, uint8_t* out, size_t len,
    const uint8_t J0[16],
    const uint8_t* round_keys,
    const uint8_t* aad, size_t aad_len,
    const uint8_t H[16],
    uint8_t tag16[16]
) {
    __m128i H_ref = reflect128(_mm_loadu_si128(reinterpret_cast<const __m128i*>(H)));
    __m128i Y = _mm_setzero_si128();

    {
        size_t aoff = 0;
        while (aoff + 16 <= aad_len) {
            __m128i a = _mm_loadu_si128(reinterpret_cast<const __m128i*>(aad + aoff));
            Y = ghash_update(Y, a, H_ref);
            aoff += 16;
        }
        if (aoff < aad_len) {
            alignas(16) uint8_t last[16] = {};
            std::memcpy(last, aad + aoff, aad_len - aoff);
            Y = ghash_update(Y, _mm_loadu_si128(reinterpret_cast<const __m128i*>(last)), H_ref);
        }
    }

    uint32_t ctr = (uint32_t(J0[12]) << 24) | (uint32_t(J0[13]) << 16) |
                   (uint32_t(J0[14]) << 8)  |  uint32_t(J0[15]);
    size_t off = 0;

    while (off + 64 <= len) {
        __m128i ct0 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off));
        __m128i ct1 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 16));
        __m128i ct2 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 32));
        __m128i ct3 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 48));

        Y = ghash_update(Y, ct0, H_ref);
        Y = ghash_update(Y, ct1, H_ref);
        Y = ghash_update(Y, ct2, H_ref);
        Y = ghash_update(Y, ct3, H_ref);

        __m128i c0 = make_ctr_block(J0, ++ctr);
        __m128i c1 = make_ctr_block(J0, ++ctr);
        __m128i c2 = make_ctr_block(J0, ++ctr);
        __m128i c3 = make_ctr_block(J0, ++ctr);

        __m128i rk0 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys));
        c0 = _mm_xor_si128(c0, rk0);
        c1 = _mm_xor_si128(c1, rk0);
        c2 = _mm_xor_si128(c2, rk0);
        c3 = _mm_xor_si128(c3, rk0);

        for (int i = 1; i < 14; i++) {
            __m128i rk = _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16));
            c0 = _mm_aesenc_si128(c0, rk);
            c1 = _mm_aesenc_si128(c1, rk);
            c2 = _mm_aesenc_si128(c2, rk);
            c3 = _mm_aesenc_si128(c3, rk);
        }

        __m128i rkLast = _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 224));
        c0 = _mm_aesenclast_si128(c0, rkLast);
        c1 = _mm_aesenclast_si128(c1, rkLast);
        c2 = _mm_aesenclast_si128(c2, rkLast);
        c3 = _mm_aesenclast_si128(c3, rkLast);

        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off),      _mm_xor_si128(c0, ct0));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 16), _mm_xor_si128(c1, ct1));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 32), _mm_xor_si128(c2, ct2));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 48), _mm_xor_si128(c3, ct3));

        off += 64;
    }

    while (off + 16 <= len) {
        __m128i ct_blk = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off));
        Y = ghash_update(Y, ct_blk, H_ref);

        __m128i c = make_ctr_block(J0, ++ctr);
        c = _mm_xor_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys)));
        for (int i = 1; i < 14; i++)
            c = _mm_aesenc_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16)));
        c = _mm_aesenclast_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 224)));

        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off), _mm_xor_si128(c, ct_blk));
        off += 16;
    }

    if (off < len) {
        alignas(16) uint8_t partial[16] = {};
        size_t rem = len - off;
        std::memcpy(partial, in + off, rem);
        Y = ghash_update(Y, _mm_loadu_si128(reinterpret_cast<const __m128i*>(partial)), H_ref);

        __m128i c = make_ctr_block(J0, ++ctr);
        c = _mm_xor_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys)));
        for (int i = 1; i < 14; i++)
            c = _mm_aesenc_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16)));
        c = _mm_aesenclast_si128(c, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 224)));

        alignas(16) uint8_t ks[16];
        _mm_storeu_si128(reinterpret_cast<__m128i*>(ks), c);
        for (size_t i = 0; i < rem; i++)
            out[off + i] = in[off + i] ^ ks[i];
    }

    // Finalize GHASH
    alignas(16) uint8_t lenblk[16] = {};
    uint64_t aad_bits = uint64_t(aad_len) * 8;
    uint64_t ct_bits  = uint64_t(len) * 8;
    lenblk[0] = uint8_t(aad_bits >> 56); lenblk[1] = uint8_t(aad_bits >> 48);
    lenblk[2] = uint8_t(aad_bits >> 40); lenblk[3] = uint8_t(aad_bits >> 32);
    lenblk[4] = uint8_t(aad_bits >> 24); lenblk[5] = uint8_t(aad_bits >> 16);
    lenblk[6] = uint8_t(aad_bits >> 8);  lenblk[7] = uint8_t(aad_bits);
    lenblk[8]  = uint8_t(ct_bits >> 56); lenblk[9]  = uint8_t(ct_bits >> 48);
    lenblk[10] = uint8_t(ct_bits >> 40); lenblk[11] = uint8_t(ct_bits >> 32);
    lenblk[12] = uint8_t(ct_bits >> 24); lenblk[13] = uint8_t(ct_bits >> 16);
    lenblk[14] = uint8_t(ct_bits >> 8);  lenblk[15] = uint8_t(ct_bits);

    Y = ghash_update(Y, _mm_loadu_si128(reinterpret_cast<const __m128i*>(lenblk)), H_ref);

    __m128i j0_block = _mm_loadu_si128(reinterpret_cast<const __m128i*>(J0));
    j0_block = _mm_xor_si128(j0_block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys)));
    for (int i = 1; i < 14; i++)
        j0_block = _mm_aesenc_si128(j0_block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + i * 16)));
    j0_block = _mm_aesenclast_si128(j0_block, _mm_loadu_si128(reinterpret_cast<const __m128i*>(round_keys + 224)));

    __m128i tag = _mm_xor_si128(j0_block, Y);
    _mm_storeu_si128(reinterpret_cast<__m128i*>(tag16), tag);
}

#endif // NETPLUS_X86

namespace netplus {
@@ -1235,6 +1516,26 @@ bool aes128::aes_gcm_decrypt(
    std::memcpy(J0, iv12, 12);
    J0[15] = 1;

#ifdef NETPLUS_X86
    if (g_has_hw_aes) {
        // Fused single pass: GHASH-of-ciphertext and CTR-decrypt share one
        // loop over the (already fully available) ciphertext instead of a
        // full GHASH pass followed by a full separate CTR pass — see
        // aesni_gcm_ctr_ghash_decrypt_128's comment.
        uint8_t expTag[16];
        aesni_gcm_ctr_ghash_decrypt_128(ct, pt, ct_len, J0, m_roundKeyBytes.data(),
                                         aad, aad_len, m_H, expTag);
        if (!ct_memeq(expTag, tag16, 16)) {
            // AEAD contract is all-or-nothing: the caller must never use
            // `pt` when this returns false. It was written speculatively
            // by the fused pass above, so wipe it as defense in depth.
            std::memset(pt, 0, ct_len);
            return false;
        }
        return true;
    }
#endif

    std::array<uint8_t,16> Sgh{};
    Sgh.fill(0);
    ghash(Sgh, aad, aad_len, ct, ct_len);
@@ -1249,12 +1550,6 @@ bool aes128::aes_gcm_decrypt(
    if (!ct_memeq(expTag, tag16, 16))
        return false;

#ifdef NETPLUS_X86
    if (g_has_hw_aes) {
        aesni_ctr_encrypt_128(ct, pt, ct_len, J0, m_roundKeyBytes.data());
    } else
#endif
    {
    uint8_t ctr[16];
    std::memcpy(ctr, J0, 16);

@@ -1269,7 +1564,6 @@ bool aes128::aes_gcm_decrypt(
            pt[off+i] = ct[off+i] ^ S[i];
        off += take;
    }
    }

    return true;
}
@@ -1792,6 +2086,21 @@ bool aes256::aes_gcm_decrypt(
    std::memcpy(J0, iv12, 12);
    J0[15] = 1;

#ifdef NETPLUS_X86
    if (g_has_hw_aes) {
        // See aes128::aes_gcm_decrypt / aesni_gcm_ctr_ghash_decrypt_256's
        // comment: fused GHASH+CTR-decrypt in one pass.
        uint8_t expTag[16];
        aesni_gcm_ctr_ghash_decrypt_256(ct, pt, ct_len, J0, m_roundKeyBytes.data(),
                                         aad, aad_len, m_H, expTag);
        if (!ct_memeq(expTag, tag16, 16)) {
            std::memset(pt, 0, ct_len);
            return false;
        }
        return true;
    }
#endif

    std::array<uint8_t,16> Sgh{};
    Sgh.fill(0);
    ghash(Sgh, aad, aad_len, ct, ct_len);
@@ -1806,12 +2115,6 @@ bool aes256::aes_gcm_decrypt(
    if (!ct_memeq(expTag, tag16, 16))
        return false;

#ifdef NETPLUS_X86
    if (g_has_hw_aes) {
        aesni_ctr_encrypt_256(ct, pt, ct_len, J0, m_roundKeyBytes.data());
    } else
#endif
    {
    uint8_t ctr[16];
    std::memcpy(ctr, J0, 16);

@@ -1826,7 +2129,6 @@ bool aes256::aes_gcm_decrypt(
            pt[off+i] = ct[off+i] ^ S[i];
        off += take;
    }
    }

    return true;
}