Commit 11f353e1 authored by jan.koester's avatar jan.koester
Browse files

test

parent 77be4634
Loading
Loading
Loading
Loading
+0 −160
Changes for src/crypto/aes.cpp: 0 added lines, 160 removed lines.
Original line number Diff line number Diff line
@@ -125,166 +125,6 @@ static inline __m128i make_ctr_block(const uint8_t base[16], uint32_t counter) {
    return _mm_insert_epi32(blk, static_cast<int>(__builtin_bswap32(counter)), 3);
}

AESNI_FUNC
static void aesni_ctr_encrypt_128(
    const uint8_t* in, uint8_t* out, size_t len,
    const uint8_t J0[16],   // initial counter block (counter starts at J0's last 4 bytes + 1)
    const uint8_t* round_keys
) {
    // Extract initial counter value from J0
    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;

    // Process 4 blocks (64 bytes) at a time
    while (off + 64 <= len) {
        __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);

        // XOR with plaintext
        __m128i p0 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off));
        __m128i p1 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 16));
        __m128i p2 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 32));
        __m128i p3 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 48));

        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off),      _mm_xor_si128(c0, p0));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 16), _mm_xor_si128(c1, p1));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 32), _mm_xor_si128(c2, p2));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 48), _mm_xor_si128(c3, p3));

        off += 64;
    }

    // Handle remaining full blocks one at a time
    while (off + 16 <= len) {
        __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)));

        __m128i p = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off), _mm_xor_si128(c, p));
        off += 16;
    }

    // Handle final partial block
    if (off < len) {
        __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; off + i < len; i++)
            out[off + i] = in[off + i] ^ ks[i];
    }
}

AESNI_FUNC
static void aesni_ctr_encrypt_256(
    const uint8_t* in, uint8_t* out, size_t len,
    const uint8_t J0[16],
    const uint8_t* round_keys
) {
    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;

    // Process 4 blocks (64 bytes) at a time
    while (off + 64 <= len) {
        __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);

        __m128i p0 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off));
        __m128i p1 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 16));
        __m128i p2 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 32));
        __m128i p3 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off + 48));

        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off),      _mm_xor_si128(c0, p0));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 16), _mm_xor_si128(c1, p1));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 32), _mm_xor_si128(c2, p2));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off + 48), _mm_xor_si128(c3, p3));

        off += 64;
    }

    // Remaining full blocks
    while (off + 16 <= len) {
        __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)));

        __m128i p = _mm_loadu_si128(reinterpret_cast<const __m128i*>(in + off));
        _mm_storeu_si128(reinterpret_cast<__m128i*>(out + off), _mm_xor_si128(c, p));
        off += 16;
    }

    // Final partial block
    if (off < len) {
        __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; off + i < len; i++)
            out[off + i] = in[off + i] ^ ks[i];
    }
}

// ---- 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.
+4 −0
Changes for src/quic.cpp: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1705,6 +1705,9 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) {
    // the same thread already holds if ~quic() ever needed the lock too
    // (it deliberately doesn't — see its comment).
    {
        auto purge_now = std::chrono::steady_clock::now();
        if (purge_now - _last_registry_purge >= _registry_purge_interval) {
            _last_registry_purge = purge_now;
            std::vector<std::shared_ptr<quic>> to_destroy;
            std::unique_lock<std::shared_mutex> reg_lock(_registry_mutex);
            for (auto it = _child_connections.begin(); it != _child_connections.end(); ) {
@@ -1716,6 +1719,7 @@ void quic::accept(std::unique_ptr<socket>& csock, bool nonblock) {
                }
            }
        }
    }

    // QUIC accept is different from TCP - we receive Initial packets on the
    // listening socket and create new quic instances for each connection.
+13 −0
Changes for src/socket.h: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1429,6 +1429,19 @@ namespace netplus {
		std::chrono::steady_clock::time_point _last_pump_loss_scan{};
		static constexpr std::chrono::microseconds _pump_loss_scan_interval{50};

		// Throttle for accept()'s "purge stale (Closed) connections" scan
		// (listener-only state — a child never calls accept()). That scan
		// is O(_child_connections.size()) under the exclusive
		// _registry_mutex lock; at 100k+ concurrent connections, running it
		// unconditionally on every accept() call (i.e. every drain of the
		// listener socket, potentially every event-loop tick) both burns
		// CPU and holds pumpIncoming()'s shared_lock lookups off the
		// registry longer than needed. A closed connection sitting in the
		// map for up to one interval longer than before is harmless — nothing
		// reads _conn_state==Closed entries as "alive" in the meantime.
		std::chrono::steady_clock::time_point _last_registry_purge{};
		static constexpr std::chrono::milliseconds _registry_purge_interval{100};

		// Flow control batching: avoid sending duplicate MAX_DATA/MAX_STREAM_DATA
		bool _pending_max_data = false;
		uint64_t _pending_max_data_value = 0;