2 // Copyright (c) 2016-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)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_WEBSOCKET_DETAIL_MASK_HPP
11 #define BOOST_BEAST_WEBSOCKET_DETAIL_MASK_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/asio/buffer.hpp>
19 #include <type_traits>
26 // Pseudo-random source of mask keys
28 template<class Generator>
35 typename Generator::result_type;
40 operator()() noexcept;
46 template<class Generator>
47 maskgen_t<Generator>::maskgen_t()
52 template<class Generator>
54 maskgen_t<Generator>::operator()() noexcept ->
66 std::random_device rng;
68 std::array<std::uint32_t, 32> e;
71 // VFALCO This constructor causes
72 // address sanitizer to fail, no idea why.
73 std::seed_seq ss(e.begin(), e.end());
80 // VFALCO NOTE This generator has 5KB of state!
81 //using maskgen = maskgen_t<std::mt19937>;
82 using maskgen = maskgen_t<std::minstd_rand>;
84 //------------------------------------------------------------------------------
87 std::conditional<sizeof(void*) == 8,
88 std::uint64_t, std::uint32_t>::type;
92 prepare_key(std::uint32_t& prepared, std::uint32_t key)
99 prepare_key(std::uint64_t& prepared, std::uint32_t key)
102 (static_cast<std::uint64_t>(key) << 32) | key;
107 typename std::enable_if<std::is_integral<T>::value, T>::type
108 ror(T t, unsigned n = 1)
110 auto constexpr bits =
111 static_cast<unsigned>(
112 sizeof(T) * CHAR_BIT);
114 return static_cast<T>((t << (bits - n)) | (
115 static_cast<typename std::make_unsigned<T>::type>(t) >> n));
120 template<class = void>
123 boost::asio::mutable_buffer const& b,
127 auto p = reinterpret_cast<std::uint8_t*>(b.data());
130 // Bring p to 4-byte alignment
131 auto const i = reinterpret_cast<
132 std::uintptr_t>(p) & (sizeof(key)-1);
135 case 1: p[2] ^= static_cast<std::uint8_t>(key >> 16); BOOST_BEAST_FALLTHROUGH;
136 case 2: p[1] ^= static_cast<std::uint8_t>(key >> 8); BOOST_BEAST_FALLTHROUGH;
137 case 3: p[0] ^= static_cast<std::uint8_t>(key);
139 auto const d = static_cast<unsigned>(sizeof(key) - i);
143 BOOST_BEAST_FALLTHROUGH;
150 // Mask 4 bytes at a time
151 for(auto i = n / sizeof(key); i; --i)
154 std::uint32_t*>(p) ^= key;
162 case 3: p[2] ^= static_cast<std::uint8_t>(key >> 16); BOOST_BEAST_FALLTHROUGH;
163 case 2: p[1] ^= static_cast<std::uint8_t>(key >> 8); BOOST_BEAST_FALLTHROUGH;
164 case 1: p[0] ^= static_cast<std::uint8_t>(key);
165 key = ror(key, static_cast<unsigned>(8*n));
166 BOOST_BEAST_FALLTHROUGH;
174 template<class = void>
177 boost::asio::mutable_buffer const& b,
181 auto p = reinterpret_cast<std::uint8_t*>(b.data());
184 // Bring p to 8-byte alignment
185 auto const i = reinterpret_cast<
186 std::uintptr_t>(p) & (sizeof(key)-1);
189 case 1: p[6] ^= static_cast<std::uint8_t>(key >> 48);
190 case 2: p[5] ^= static_cast<std::uint8_t>(key >> 40);
191 case 3: p[4] ^= static_cast<std::uint8_t>(key >> 32);
192 case 4: p[3] ^= static_cast<std::uint8_t>(key >> 24);
193 case 5: p[2] ^= static_cast<std::uint8_t>(key >> 16);
194 case 6: p[1] ^= static_cast<std::uint8_t>(key >> 8);
195 case 7: p[0] ^= static_cast<std::uint8_t>(key);
197 auto const d = static_cast<
198 unsigned>(sizeof(key) - i);
208 // Mask 8 bytes at a time
209 for(auto i = n / sizeof(key); i; --i)
212 std::uint64_t*>(p) ^= key;
220 case 7: p[6] ^= static_cast<std::uint8_t>(key >> 48);
221 case 6: p[5] ^= static_cast<std::uint8_t>(key >> 40);
222 case 5: p[4] ^= static_cast<std::uint8_t>(key >> 32);
223 case 4: p[3] ^= static_cast<std::uint8_t>(key >> 24);
224 case 3: p[2] ^= static_cast<std::uint8_t>(key >> 16);
225 case 2: p[1] ^= static_cast<std::uint8_t>(key >> 8);
226 case 1: p[0] ^= static_cast<std::uint8_t>(key);
227 key = ror(key, static_cast<unsigned>(8*n));
236 boost::asio::mutable_buffer const& b,
239 mask_inplace_fast(b, key);
245 boost::asio::mutable_buffer const& b,
248 mask_inplace_fast(b, key);
251 // Apply mask in place
253 template<class MutableBuffers, class KeyType>
256 MutableBuffers const& bs, KeyType& key)
258 for(boost::asio::mutable_buffer b : bs)
259 mask_inplace(b, key);