Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions include/ws_client/MaskKey.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <bit>
#include <cstdint>
#include <cstddef>
#include <span>
Expand Down Expand Up @@ -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 <size_t align>
static size_t leading_bytes(const byte* data, size_t len) noexcept
{
auto ptr = reinterpret_cast<uintptr_t>(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 <size_t align>
static uint32_t chunk_key(const byte* data, size_t len, uint32_t key) noexcept
{
const int shift = static_cast<int>(8 * (leading_bytes<align>(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 <size_t align, typename MaskFunc>
static void mask_aligned(byte* data, size_t len, uint32_t key, const MaskFunc mask_func)
{
byte* key_bytes = reinterpret_cast<byte*>(&key);

// align data pointer to boundary
auto ptr = reinterpret_cast<uintptr_t>(data);
size_t unaligned = (ptr & (align - 1));
if (unaligned)
unaligned = align - unaligned;
unaligned = unaligned > len ? len : unaligned;
const size_t unaligned = leading_bytes<align>(data, len);

// processing leading bytes naively
size_t i = 0;
Expand All @@ -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<int>(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);
Expand All @@ -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)
{
Expand All @@ -132,7 +153,8 @@ struct MaskKey

static void mask_fn(byte* data, size_t len, uint32_t key)
{
uint64_t key_64 = (static_cast<uint64_t>(key) << 32) | key;
const uint32_t chunk = chunk_key<8>(data, len, key);
uint64_t key_64 = (static_cast<uint64_t>(chunk) << 32) | chunk;
auto mask_func = [&key_64](byte* data)
{
*reinterpret_cast<uint64_t*>(data) ^= key_64; //
Expand Down
29 changes: 29 additions & 0 deletions tests/impl/MaskKey.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gtest/gtest.h>

#include <cstring>
#include <string>
#include <iostream>

Expand Down Expand Up @@ -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<byte>((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;
}
}
}