10 #ifndef BOOST_BEAST_DETAIL_SHA1_HPP 11 #define BOOST_BEAST_DETAIL_SHA1_HPP 33 static std::size_t constexpr BLOCK_INTS = 16;
34 static std::size_t constexpr BLOCK_BYTES = 64;
35 static std::size_t constexpr DIGEST_BYTES = 20;
39 rol(std::uint32_t value, std::size_t bits)
41 return (value << bits) | (value >> (32 - bits));
46 blk(std::uint32_t block[BLOCK_INTS], std::size_t i)
49 block[(i+13)&15] ^ block[(i+8)&15] ^
50 block[(i+2)&15] ^ block[i], 1);
55 R0(std::uint32_t block[BLOCK_INTS], std::uint32_t v,
56 std::uint32_t &w, std::uint32_t x, std::uint32_t y,
57 std::uint32_t &z, std::size_t i)
59 z += ((w&(x^y))^y) + block[i] + 0x5a827999 +
rol(v, 5);
66 R1(std::uint32_t block[BLOCK_INTS], std::uint32_t v,
67 std::uint32_t &w, std::uint32_t x, std::uint32_t y,
68 std::uint32_t &z, std::size_t i)
70 block[i] =
blk(block, i);
71 z += ((w&(x^y))^y) + block[i] + 0x5a827999 +
rol(v, 5);
77 R2(std::uint32_t block[BLOCK_INTS], std::uint32_t v,
78 std::uint32_t &w, std::uint32_t x, std::uint32_t y,
79 std::uint32_t &z, std::size_t i)
81 block[i] =
blk(block, i);
82 z += (w^x^y) + block[i] + 0x6ed9eba1 +
rol(v, 5);
88 R3(std::uint32_t block[BLOCK_INTS], std::uint32_t v,
89 std::uint32_t &w, std::uint32_t x, std::uint32_t y,
90 std::uint32_t &z, std::size_t i)
92 block[i] =
blk(block, i);
93 z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc +
rol(v, 5);
99 R4(std::uint32_t block[BLOCK_INTS], std::uint32_t v,
100 std::uint32_t &w, std::uint32_t x, std::uint32_t y,
101 std::uint32_t &z, std::size_t i)
103 block[i] =
blk(block, i);
104 z += (w^x^y) + block[i] + 0xca62c1d6 +
rol(v, 5);
111 std::uint32_t block[BLOCK_INTS])
113 for(std::size_t i = 0; i < BLOCK_INTS; i++)
115 (static_cast<std::uint32_t>(p[4*i+3])) |
116 (
static_cast<std::uint32_t
>(p[4*i+2]))<< 8 |
117 (
static_cast<std::uint32_t
>(p[4*i+1]))<<16 |
118 (
static_cast<std::uint32_t
>(p[4*i+0]))<<24;
121 template<
class =
void>
124 std::uint32_t digest[], std::uint32_t block[BLOCK_INTS])
126 std::uint32_t a = digest[0];
127 std::uint32_t b = digest[1];
128 std::uint32_t c = digest[2];
129 std::uint32_t d = digest[3];
130 std::uint32_t e = digest[4];
132 R0(block, a, b, c, d, e, 0);
133 R0(block, e, a, b, c, d, 1);
134 R0(block, d, e, a, b, c, 2);
135 R0(block, c, d, e, a, b, 3);
136 R0(block, b, c, d, e, a, 4);
137 R0(block, a, b, c, d, e, 5);
138 R0(block, e, a, b, c, d, 6);
139 R0(block, d, e, a, b, c, 7);
140 R0(block, c, d, e, a, b, 8);
141 R0(block, b, c, d, e, a, 9);
142 R0(block, a, b, c, d, e, 10);
143 R0(block, e, a, b, c, d, 11);
144 R0(block, d, e, a, b, c, 12);
145 R0(block, c, d, e, a, b, 13);
146 R0(block, b, c, d, e, a, 14);
147 R0(block, a, b, c, d, e, 15);
148 R1(block, e, a, b, c, d, 0);
149 R1(block, d, e, a, b, c, 1);
150 R1(block, c, d, e, a, b, 2);
151 R1(block, b, c, d, e, a, 3);
152 R2(block, a, b, c, d, e, 4);
153 R2(block, e, a, b, c, d, 5);
154 R2(block, d, e, a, b, c, 6);
155 R2(block, c, d, e, a, b, 7);
156 R2(block, b, c, d, e, a, 8);
157 R2(block, a, b, c, d, e, 9);
158 R2(block, e, a, b, c, d, 10);
159 R2(block, d, e, a, b, c, 11);
160 R2(block, c, d, e, a, b, 12);
161 R2(block, b, c, d, e, a, 13);
162 R2(block, a, b, c, d, e, 14);
163 R2(block, e, a, b, c, d, 15);
164 R2(block, d, e, a, b, c, 0);
165 R2(block, c, d, e, a, b, 1);
166 R2(block, b, c, d, e, a, 2);
167 R2(block, a, b, c, d, e, 3);
168 R2(block, e, a, b, c, d, 4);
169 R2(block, d, e, a, b, c, 5);
170 R2(block, c, d, e, a, b, 6);
171 R2(block, b, c, d, e, a, 7);
172 R3(block, a, b, c, d, e, 8);
173 R3(block, e, a, b, c, d, 9);
174 R3(block, d, e, a, b, c, 10);
175 R3(block, c, d, e, a, b, 11);
176 R3(block, b, c, d, e, a, 12);
177 R3(block, a, b, c, d, e, 13);
178 R3(block, e, a, b, c, d, 14);
179 R3(block, d, e, a, b, c, 15);
180 R3(block, c, d, e, a, b, 0);
181 R3(block, b, c, d, e, a, 1);
182 R3(block, a, b, c, d, e, 2);
183 R3(block, e, a, b, c, d, 3);
184 R3(block, d, e, a, b, c, 4);
185 R3(block, c, d, e, a, b, 5);
186 R3(block, b, c, d, e, a, 6);
187 R3(block, a, b, c, d, e, 7);
188 R3(block, e, a, b, c, d, 8);
189 R3(block, d, e, a, b, c, 9);
190 R3(block, c, d, e, a, b, 10);
191 R3(block, b, c, d, e, a, 11);
192 R4(block, a, b, c, d, e, 12);
193 R4(block, e, a, b, c, d, 13);
194 R4(block, d, e, a, b, c, 14);
195 R4(block, c, d, e, a, b, 15);
196 R4(block, b, c, d, e, a, 0);
197 R4(block, a, b, c, d, e, 1);
198 R4(block, e, a, b, c, d, 2);
199 R4(block, d, e, a, b, c, 3);
200 R4(block, c, d, e, a, b, 4);
201 R4(block, b, c, d, e, a, 5);
202 R4(block, a, b, c, d, e, 6);
203 R4(block, e, a, b, c, d, 7);
204 R4(block, d, e, a, b, c, 8);
205 R4(block, c, d, e, a, b, 9);
206 R4(block, b, c, d, e, a, 10);
207 R4(block, a, b, c, d, e, 11);
208 R4(block, e, a, b, c, d, 12);
209 R4(block, d, e, a, b, c, 13);
210 R4(block, c, d, e, a, b, 14);
211 R4(block, b, c, d, e, a, 15);
224 static unsigned int constexpr block_size = sha1::BLOCK_BYTES;
225 static unsigned int constexpr digest_size = 20;
229 std::uint32_t digest[5];
230 std::uint8_t buf[block_size];
233 template<
class =
void>
239 ctx.digest[0] = 0x67452301;
240 ctx.digest[1] = 0xefcdab89;
241 ctx.digest[2] = 0x98badcfe;
242 ctx.digest[3] = 0x10325476;
243 ctx.digest[4] = 0xc3d2e1f0;
246 template<
class =
void>
249 void const* message, std::size_t size) noexcept
251 auto p =
reinterpret_cast< 252 std::uint8_t const*
>(message);
255 auto const n = (std::min)(
256 size,
sizeof(ctx.buf) - ctx.buflen);
257 std::memcpy(ctx.buf + ctx.buflen, p, n);
264 std::uint32_t block[sha1::BLOCK_INTS];
271 template<
class =
void>
275 using sha1::BLOCK_INTS;
276 using sha1::BLOCK_BYTES;
278 std::uint64_t total_bits =
279 (ctx.blocks*64 + ctx.buflen) * 8;
281 ctx.buf[ctx.buflen++] = 0x80;
282 auto const buflen = ctx.buflen;
283 while(ctx.buflen < 64)
284 ctx.buf[ctx.buflen++] = 0x00;
285 std::uint32_t block[BLOCK_INTS];
287 if(buflen > BLOCK_BYTES - 8)
290 for(
size_t i = 0; i < BLOCK_INTS - 2; i++)
295 block[BLOCK_INTS - 1] = total_bits & 0xffffffff;
296 block[BLOCK_INTS - 2] = (total_bits >> 32);
298 for(std::size_t i = 0; i < sha1::DIGEST_BYTES/4; i++)
301 reinterpret_cast<std::uint8_t*
>(digest) + 4 * i;
302 d[3] = ctx.digest[i] & 0xff;
303 d[2] = (ctx.digest[i] >> 8) & 0xff;
304 d[1] = (ctx.digest[i] >> 16) & 0xff;
305 d[0] = (ctx.digest[i] >> 24) & 0xff;
void init(sha1_context &ctx) noexcept
Definition: sha1.hpp:235
Definition: async_result.hpp:20
void R1(std::uint32_t block[BLOCK_INTS], std::uint32_t v, std::uint32_t &w, std::uint32_t x, std::uint32_t y, std::uint32_t &z, std::size_t i)
Definition: sha1.hpp:66
std::uint32_t blk(std::uint32_t block[BLOCK_INTS], std::size_t i)
Definition: sha1.hpp:46
void R2(std::uint32_t block[BLOCK_INTS], std::uint32_t v, std::uint32_t &w, std::uint32_t x, std::uint32_t y, std::uint32_t &z, std::size_t i)
Definition: sha1.hpp:77
void finish(sha1_context &ctx, void *digest) noexcept
Definition: sha1.hpp:273
std::size_t buflen
Definition: sha1.hpp:227
void update(sha1_context &ctx, void const *message, std::size_t size) noexcept
Definition: sha1.hpp:248
std::size_t blocks
Definition: sha1.hpp:228
void R3(std::uint32_t block[BLOCK_INTS], std::uint32_t v, std::uint32_t &w, std::uint32_t x, std::uint32_t y, std::uint32_t &z, std::size_t i)
Definition: sha1.hpp:88
std::uint32_t rol(std::uint32_t value, std::size_t bits)
Definition: sha1.hpp:39
void R4(std::uint32_t block[BLOCK_INTS], std::uint32_t v, std::uint32_t &w, std::uint32_t x, std::uint32_t y, std::uint32_t &z, std::size_t i)
Definition: sha1.hpp:99
void make_block(std::uint8_t const *p, std::uint32_t block[BLOCK_INTS])
Definition: sha1.hpp:110
void transform(std::uint32_t digest[], std::uint32_t block[BLOCK_INTS])
Definition: sha1.hpp:123
void R0(std::uint32_t block[BLOCK_INTS], std::uint32_t v, std::uint32_t &w, std::uint32_t x, std::uint32_t y, std::uint32_t &z, std::size_t i)
Definition: sha1.hpp:55