From d72b357e8b6dae0bfa1513c38e27354a2b38a183 Mon Sep 17 00:00:00 2001 From: PrefectSol Date: Tue, 17 Nov 2026 23:59:36 +0500 Subject: [PATCH] fix payload masking for buffers not aligned to 4 bytes --- include/ws_client/MaskKey.hpp | 38 +++++++++++++++++++++++++++-------- tests/impl/MaskKey.cpp | 29 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/include/ws_client/MaskKey.hpp b/include/ws_client/MaskKey.hpp index 816cbb6..40eb971 100644 --- a/include/ws_client/MaskKey.hpp +++ b/include/ws_client/MaskKey.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -60,17 +61,37 @@ struct MaskKey * which is called on each aligned chunk (of size `align`) with a pointer to the chunk. * Unaligned leading and trailing bytes are processed naively byte by byte. */ + template + static size_t leading_bytes(const byte* data, size_t len) noexcept + { + auto ptr = reinterpret_cast(data); + size_t unaligned = (ptr & (align - 1)); + if (unaligned) + unaligned = align - unaligned; + return unaligned > len ? len : unaligned; + } + + /** + * Key for the aligned chunks: the chunks start at offset `leading_bytes`, + * so the key bytes must be rotated by that offset modulo 4. + */ + template + static uint32_t chunk_key(const byte* data, size_t len, uint32_t key) noexcept + { + const int shift = static_cast(8 * (leading_bytes(data, len) & (sizeof(uint32_t) - 1))); + if constexpr (std::endian::native == std::endian::little) + return std::rotr(key, shift); + else + return std::rotl(key, shift); + } + template static void mask_aligned(byte* data, size_t len, uint32_t key, const MaskFunc mask_func) { byte* key_bytes = reinterpret_cast(&key); // align data pointer to boundary - auto ptr = reinterpret_cast(data); - size_t unaligned = (ptr & (align - 1)); - if (unaligned) - unaligned = align - unaligned; - unaligned = unaligned > len ? len : unaligned; + const size_t unaligned = leading_bytes(data, len); // processing leading bytes naively size_t i = 0; @@ -92,7 +113,7 @@ struct MaskKey static void mask_fn(byte* data, size_t len, uint32_t key) { - const __m128i key_128 = _mm_set1_epi32(key); // 128-bit vector with key repeated + const __m128i key_128 = _mm_set1_epi32(static_cast(chunk_key<16>(data, len, key))); // 128-bit vector with key repeated auto mask_func = [&key_128](byte* data) { auto ptr = reinterpret_cast<__m128i*>(data); @@ -110,7 +131,7 @@ struct MaskKey static void mask_fn(byte* data, size_t len, uint32_t key) { // Duplicate the 32-bit key to all four parts of a 128-bit vector - const uint32x4_t key_128 = ::vdupq_n_u32(key); + const uint32x4_t key_128 = ::vdupq_n_u32(chunk_key<16>(data, len, key)); auto mask_func = [&key_128](byte* data) { @@ -132,7 +153,8 @@ struct MaskKey static void mask_fn(byte* data, size_t len, uint32_t key) { - uint64_t key_64 = (static_cast(key) << 32) | key; + const uint32_t chunk = chunk_key<8>(data, len, key); + uint64_t key_64 = (static_cast(chunk) << 32) | chunk; auto mask_func = [&key_64](byte* data) { *reinterpret_cast(data) ^= key_64; // diff --git a/tests/impl/MaskKey.cpp b/tests/impl/MaskKey.cpp index 7b3d11e..ac947fa 100644 --- a/tests/impl/MaskKey.cpp +++ b/tests/impl/MaskKey.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -71,3 +72,31 @@ TEST(MaskKey, mask_key_unaligned) key.mask(payload); EXPECT_TRUE(string((char*)payload.data(), payload.size()) == R"({"method":"subscribe","subscription":{"type":"user"})"); } + +TEST(MaskKey, matches_reference_for_all_offsets) +{ + alignas(64) byte data[256]; + alignas(64) byte expected[256]; + + const uint32_t key = 0xa5c3f00f; + byte key_bytes[4]; + std::memcpy(key_bytes, &key, sizeof(key)); + + for (size_t offset = 0; offset < 32; ++offset) + { + for (size_t size = 0; size <= 200; ++size) + { + for (size_t i = 0; i < size; ++i) + { + const byte value = static_cast((i * 31 + offset) & 0xff); + data[offset + i] = value; + expected[offset + i] = value ^ key_bytes[i & 3]; + } + + MaskKey(key).mask(span(data + offset, size)); + + ASSERT_EQ(std::memcmp(data + offset, expected + offset, size), 0) + << "offset = " << offset << ", size = " << size; + } + } +}