2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BEAST_WEBSOCKET_DETAIL_MASK_HPP
9 #define BEAST_WEBSOCKET_DETAIL_MASK_HPP
11 #include <boost/asio/buffer.hpp>
16 #include <type_traits>
22 // Pseudo-random source of mask keys
24 template<class Generator>
31 typename Generator::result_type;
36 operator()() noexcept;
42 template<class Generator>
43 maskgen_t<Generator>::maskgen_t()
48 template<class Generator>
50 maskgen_t<Generator>::operator()() noexcept ->
62 std::random_device rng;
64 std::array<std::uint32_t, 32> e;
67 // VFALCO This constructor causes
68 // address sanitizer to fail, no idea why.
69 std::seed_seq ss(e.begin(), e.end());
76 // VFALCO NOTE This generator has 5KB of state!
77 //using maskgen = maskgen_t<std::mt19937>;
78 using maskgen = maskgen_t<std::minstd_rand>;
80 //------------------------------------------------------------------------------
83 std::conditional<sizeof(void*) == 8,
84 std::uint64_t, std::uint32_t>::type;
88 prepare_key(std::uint32_t& prepared, std::uint32_t key)
95 prepare_key(std::uint64_t& prepared, std::uint32_t key)
98 (static_cast<std::uint64_t>(key) << 32) | key;
103 typename std::enable_if<std::is_integral<T>::value, T>::type
104 ror(T t, unsigned n = 1)
106 auto constexpr bits =
107 static_cast<unsigned>(
108 sizeof(T) * CHAR_BIT);
110 return static_cast<T>((t << (bits - n)) | (
111 static_cast<typename std::make_unsigned<T>::type>(t) >> n));
116 template<class = void>
119 boost::asio::mutable_buffer const& b,
122 using boost::asio::buffer_cast;
123 using boost::asio::buffer_size;
124 auto n = buffer_size(b);
125 auto p = buffer_cast<std::uint8_t*>(b);
128 // Bring p to 4-byte alignment
129 auto const i = reinterpret_cast<
130 std::uintptr_t>(p) & (sizeof(key)-1);
133 case 1: p[2] ^= static_cast<std::uint8_t>(key >> 16);
134 case 2: p[1] ^= static_cast<std::uint8_t>(key >> 8);
135 case 3: p[0] ^= static_cast<std::uint8_t>(key);
137 auto const d = static_cast<
138 unsigned>(sizeof(key) - i);
148 // Mask 4 bytes at a time
149 for(auto i = n / sizeof(key); i; --i)
152 std::uint32_t*>(p) ^= key;
160 case 3: p[2] ^= static_cast<std::uint8_t>(key >> 16);
161 case 2: p[1] ^= static_cast<std::uint8_t>(key >> 8);
162 case 1: p[0] ^= static_cast<std::uint8_t>(key);
163 key = ror(key, static_cast<unsigned>(8*n));
171 template<class = void>
174 boost::asio::mutable_buffer const& b,
177 using boost::asio::buffer_cast;
178 using boost::asio::buffer_size;
179 auto n = buffer_size(b);
180 auto p = buffer_cast<std::uint8_t*>(b);
183 // Bring p to 8-byte alignment
184 auto const i = reinterpret_cast<
185 std::uintptr_t>(p) & (sizeof(key)-1);
188 case 1: p[6] ^= static_cast<std::uint8_t>(key >> 48);
189 case 2: p[5] ^= static_cast<std::uint8_t>(key >> 40);
190 case 3: p[4] ^= static_cast<std::uint8_t>(key >> 32);
191 case 4: p[3] ^= static_cast<std::uint8_t>(key >> 24);
192 case 5: p[2] ^= static_cast<std::uint8_t>(key >> 16);
193 case 6: p[1] ^= static_cast<std::uint8_t>(key >> 8);
194 case 7: p[0] ^= static_cast<std::uint8_t>(key);
196 auto const d = static_cast<
197 unsigned>(sizeof(key) - i);
207 // Mask 8 bytes at a time
208 for(auto i = n / sizeof(key); i; --i)
211 std::uint64_t*>(p) ^= key;
219 case 7: p[6] ^= static_cast<std::uint8_t>(key >> 48);
220 case 6: p[5] ^= static_cast<std::uint8_t>(key >> 40);
221 case 5: p[4] ^= static_cast<std::uint8_t>(key >> 32);
222 case 4: p[3] ^= static_cast<std::uint8_t>(key >> 24);
223 case 3: p[2] ^= static_cast<std::uint8_t>(key >> 16);
224 case 2: p[1] ^= static_cast<std::uint8_t>(key >> 8);
225 case 1: p[0] ^= static_cast<std::uint8_t>(key);
226 key = ror(key, static_cast<unsigned>(8*n));
235 boost::asio::mutable_buffer const& b,
238 mask_inplace_fast(b, key);
244 boost::asio::mutable_buffer const& b,
247 mask_inplace_fast(b, key);
250 // Apply mask in place
252 template<class MutableBuffers, class KeyType>
255 MutableBuffers const& bs, KeyType& key)
257 for(auto const& b : bs)
258 mask_inplace(b, key);