]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
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) | |
6 | // | |
7 | ||
8 | #ifndef BEAST_WEBSOCKET_DETAIL_MASK_HPP | |
9 | #define BEAST_WEBSOCKET_DETAIL_MASK_HPP | |
10 | ||
11 | #include <boost/asio/buffer.hpp> | |
12 | #include <array> | |
13 | #include <climits> | |
14 | #include <cstdint> | |
15 | #include <random> | |
16 | #include <type_traits> | |
17 | ||
18 | namespace beast { | |
19 | namespace websocket { | |
20 | namespace detail { | |
21 | ||
22 | // Pseudo-random source of mask keys | |
23 | // | |
24 | template<class Generator> | |
25 | class maskgen_t | |
26 | { | |
27 | Generator g_; | |
28 | ||
29 | public: | |
30 | using result_type = | |
31 | typename Generator::result_type; | |
32 | ||
33 | maskgen_t(); | |
34 | ||
35 | result_type | |
36 | operator()() noexcept; | |
37 | ||
38 | void | |
39 | rekey(); | |
40 | }; | |
41 | ||
42 | template<class Generator> | |
43 | maskgen_t<Generator>::maskgen_t() | |
44 | { | |
45 | rekey(); | |
46 | } | |
47 | ||
48 | template<class Generator> | |
49 | auto | |
50 | maskgen_t<Generator>::operator()() noexcept -> | |
51 | result_type | |
52 | { | |
53 | for(;;) | |
54 | if(auto key = g_()) | |
55 | return key; | |
56 | } | |
57 | ||
58 | template<class _> | |
59 | void | |
60 | maskgen_t<_>::rekey() | |
61 | { | |
62 | std::random_device rng; | |
63 | #if 0 | |
64 | std::array<std::uint32_t, 32> e; | |
65 | for(auto& i : e) | |
66 | i = rng(); | |
67 | // VFALCO This constructor causes | |
68 | // address sanitizer to fail, no idea why. | |
69 | std::seed_seq ss(e.begin(), e.end()); | |
70 | g_.seed(ss); | |
71 | #else | |
72 | g_.seed(rng()); | |
73 | #endif | |
74 | } | |
75 | ||
76 | // VFALCO NOTE This generator has 5KB of state! | |
77 | //using maskgen = maskgen_t<std::mt19937>; | |
78 | using maskgen = maskgen_t<std::minstd_rand>; | |
79 | ||
80 | //------------------------------------------------------------------------------ | |
81 | ||
82 | using prepared_key = | |
83 | std::conditional<sizeof(void*) == 8, | |
84 | std::uint64_t, std::uint32_t>::type; | |
85 | ||
86 | inline | |
87 | void | |
88 | prepare_key(std::uint32_t& prepared, std::uint32_t key) | |
89 | { | |
90 | prepared = key; | |
91 | } | |
92 | ||
93 | inline | |
94 | void | |
95 | prepare_key(std::uint64_t& prepared, std::uint32_t key) | |
96 | { | |
97 | prepared = | |
98 | (static_cast<std::uint64_t>(key) << 32) | key; | |
99 | } | |
100 | ||
101 | template<class T> | |
102 | inline | |
103 | typename std::enable_if<std::is_integral<T>::value, T>::type | |
104 | ror(T t, unsigned n = 1) | |
105 | { | |
106 | auto constexpr bits = | |
107 | static_cast<unsigned>( | |
108 | sizeof(T) * CHAR_BIT); | |
109 | n &= bits-1; | |
110 | return static_cast<T>((t << (bits - n)) | ( | |
111 | static_cast<typename std::make_unsigned<T>::type>(t) >> n)); | |
112 | } | |
113 | ||
114 | // 32-bit optimized | |
115 | // | |
116 | template<class = void> | |
117 | void | |
118 | mask_inplace_fast( | |
119 | boost::asio::mutable_buffer const& b, | |
120 | std::uint32_t& key) | |
121 | { | |
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); | |
126 | if(n >= sizeof(key)) | |
127 | { | |
128 | // Bring p to 4-byte alignment | |
129 | auto const i = reinterpret_cast< | |
130 | std::uintptr_t>(p) & (sizeof(key)-1); | |
131 | switch(i) | |
132 | { | |
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); | |
136 | { | |
137 | auto const d = static_cast< | |
138 | unsigned>(sizeof(key) - i); | |
139 | key = ror(key, 8*d); | |
140 | n -= d; | |
141 | p += d; | |
142 | } | |
143 | default: | |
144 | break; | |
145 | } | |
146 | } | |
147 | ||
148 | // Mask 4 bytes at a time | |
149 | for(auto i = n / sizeof(key); i; --i) | |
150 | { | |
151 | *reinterpret_cast< | |
152 | std::uint32_t*>(p) ^= key; | |
153 | p += sizeof(key); | |
154 | } | |
155 | ||
156 | // Leftovers | |
157 | n &= sizeof(key)-1; | |
158 | switch(n) | |
159 | { | |
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)); | |
164 | default: | |
165 | break; | |
166 | } | |
167 | } | |
168 | ||
169 | // 64-bit optimized | |
170 | // | |
171 | template<class = void> | |
172 | void | |
173 | mask_inplace_fast( | |
174 | boost::asio::mutable_buffer const& b, | |
175 | std::uint64_t& key) | |
176 | { | |
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); | |
181 | if(n >= sizeof(key)) | |
182 | { | |
183 | // Bring p to 8-byte alignment | |
184 | auto const i = reinterpret_cast< | |
185 | std::uintptr_t>(p) & (sizeof(key)-1); | |
186 | switch(i) | |
187 | { | |
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); | |
195 | { | |
196 | auto const d = static_cast< | |
197 | unsigned>(sizeof(key) - i); | |
198 | key = ror(key, 8*d); | |
199 | n -= d; | |
200 | p += d; | |
201 | } | |
202 | default: | |
203 | break; | |
204 | } | |
205 | } | |
206 | ||
207 | // Mask 8 bytes at a time | |
208 | for(auto i = n / sizeof(key); i; --i) | |
209 | { | |
210 | *reinterpret_cast< | |
211 | std::uint64_t*>(p) ^= key; | |
212 | p += sizeof(key); | |
213 | } | |
214 | ||
215 | // Leftovers | |
216 | n &= sizeof(key)-1; | |
217 | switch(n) | |
218 | { | |
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)); | |
227 | default: | |
228 | break; | |
229 | } | |
230 | } | |
231 | ||
232 | inline | |
233 | void | |
234 | mask_inplace( | |
235 | boost::asio::mutable_buffer const& b, | |
236 | std::uint32_t& key) | |
237 | { | |
238 | mask_inplace_fast(b, key); | |
239 | } | |
240 | ||
241 | inline | |
242 | void | |
243 | mask_inplace( | |
244 | boost::asio::mutable_buffer const& b, | |
245 | std::uint64_t& key) | |
246 | { | |
247 | mask_inplace_fast(b, key); | |
248 | } | |
249 | ||
250 | // Apply mask in place | |
251 | // | |
252 | template<class MutableBuffers, class KeyType> | |
253 | void | |
254 | mask_inplace( | |
255 | MutableBuffers const& bs, KeyType& key) | |
256 | { | |
257 | for(auto const& b : bs) | |
258 | mask_inplace(b, key); | |
259 | } | |
260 | ||
261 | } // detail | |
262 | } // websocket | |
263 | } // beast | |
264 | ||
265 | #endif |