23 #ifndef ICL_CORE_CRYPT_SHA2_IMPL_HPP_INCLUDED 24 #define ICL_CORE_CRYPT_SHA2_IMPL_HPP_INCLUDED 33 #define TEMPLATEM template <typename T, T t_h0, T t_h1, T t_h2, T t_h3, T t_h4, T t_h5, T t_h6, T t_h7, size_t t_len> 34 #define CLASSM Sha2Impl<T, t_h0, t_h1, t_h2, t_h3, t_h4, t_h5, t_h6, t_h7, t_len> 61 for (
size_t i = 0; i < t_len; ++i)
63 ss << std::hex << std::setw(8) << std::setfill(
'0') << m_digest[i];
68 #define bswaparr(buf, T, i) ((static_cast<T>(buf[(i)*4+3]) ) | \ 69 (static_cast<T>(buf[(i)*4+2]) << 8) | \ 70 (static_cast<T>(buf[(i)*4+1]) << 16) | \ 71 (static_cast<T>(buf[(i)*4+0]) << 24)) 73 #define bswaparr64(buf, T, i) ((static_cast<T>(buf[(i)*8+7]) ) | \ 74 (static_cast<T>(buf[(i)*8+6]) << 8) | \ 75 (static_cast<T>(buf[(i)*8+5]) << 16) | \ 76 (static_cast<T>(buf[(i)*8+4]) << 24) | \ 77 (static_cast<T>(buf[(i)*8+3]) << 32) | \ 78 (static_cast<T>(buf[(i)*8+2]) << 40) | \ 79 (static_cast<T>(buf[(i)*8+1]) << 48) | \ 80 (static_cast<T>(buf[(i)*8+0]) << 56)) 83 #define Ch(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) 84 #define Maj(x, y, z) (((x) & (y)) | ((z) & ((x) ^ (y)))) 86 #define Rotr(x, n, nbits) (((x) >> (n)) | ((x) << ((nbits)-(n)))) 88 #define Shr(x, n) ((x) >> (n)) 93 #define Sigma0_256(x) (Rotr((x), 2, 32) ^ Rotr((x), 13, 32) ^ Rotr((x), 22, 32)) 94 #define Sigma1_256(x) (Rotr((x), 6, 32) ^ Rotr((x), 11, 32) ^ Rotr((x), 25, 32)) 95 #define sigma0_256(x) (Rotr((x), 7, 32) ^ Rotr((x), 18, 32) ^ Shr((x), 3)) 96 #define sigma1_256(x) (Rotr((x), 17, 32) ^ Rotr((x), 19, 32) ^ Shr((x), 10)) 103 0x428a2f98ul, 0x71374491ul, 0xb5c0fbcful, 0xe9b5dba5ul, 0x3956c25bul, 0x59f111f1ul, 0x923f82a4ul, 0xab1c5ed5ul,
104 0xd807aa98ul, 0x12835b01ul, 0x243185beul, 0x550c7dc3ul, 0x72be5d74ul, 0x80deb1feul, 0x9bdc06a7ul, 0xc19bf174ul,
105 0xe49b69c1ul, 0xefbe4786ul, 0x0fc19dc6ul, 0x240ca1ccul, 0x2de92c6ful, 0x4a7484aaul, 0x5cb0a9dcul, 0x76f988daul,
106 0x983e5152ul, 0xa831c66dul, 0xb00327c8ul, 0xbf597fc7ul, 0xc6e00bf3ul, 0xd5a79147ul, 0x06ca6351ul, 0x14292967ul,
107 0x27b70a85ul, 0x2e1b2138ul, 0x4d2c6dfcul, 0x53380d13ul, 0x650a7354ul, 0x766a0abbul, 0x81c2c92eul, 0x92722c85ul,
108 0xa2bfe8a1ul, 0xa81a664bul, 0xc24b8b70ul, 0xc76c51a3ul, 0xd192e819ul, 0xd6990624ul, 0xf40e3585ul, 0x106aa070ul,
109 0x19a4c116ul, 0x1e376c08ul, 0x2748774cul, 0x34b0bcb5ul, 0x391c0cb3ul, 0x4ed8aa4aul, 0x5b9cca4ful, 0x682e6ff3ul,
110 0x748f82eeul, 0x78a5636ful, 0x84c87814ul, 0x8cc70208ul, 0x90befffaul, 0xa4506cebul, 0xbef9a3f7ul, 0xc67178f2ul
113 #define round_0_15(a, b, c, d, e, f, g, h, K, W, t) \ 114 T1 = wv(h) + Sigma1_256(wv(e)) + Ch(wv(e), wv(f), wv(g)) + K[t] + W[t&15]; \ 115 T2 = Sigma0_256(wv(a)) + Maj(wv(a), wv(b), wv(c)); \ 119 #define round_16_63(a, b, c, d, e, f, g, h, K, W, t) \ 120 W[(t)&15] += sigma1_256(W[(t+14)&15]) + W[(t+9)&15] + sigma0_256(W[(t+1)&15]); \ 121 T1 = wv(h) + Sigma1_256(wv(e)) + Ch(wv(e), wv(f), wv(g)) + K[t] + W[t&15]; \ 122 T2 = Sigma0_256(wv(a)) + Maj(wv(a), wv(b), wv(c)); \ 127 void CLASSM::processBuffer()
150 round_0_15(0,1,2,3,4,5,6,7, k256, w, 0);
151 round_0_15(7,0,1,2,3,4,5,6, k256, w, 1);
152 round_0_15(6,7,0,1,2,3,4,5, k256, w, 2);
153 round_0_15(5,6,7,0,1,2,3,4, k256, w, 3);
154 round_0_15(4,5,6,7,0,1,2,3, k256, w, 4);
155 round_0_15(3,4,5,6,7,0,1,2, k256, w, 5);
156 round_0_15(2,3,4,5,6,7,0,1, k256, w, 6);
157 round_0_15(1,2,3,4,5,6,7,0, k256, w, 7);
159 round_0_15(0,1,2,3,4,5,6,7, k256, w, 8);
160 round_0_15(7,0,1,2,3,4,5,6, k256, w, 9);
161 round_0_15(6,7,0,1,2,3,4,5, k256, w, 10);
162 round_0_15(5,6,7,0,1,2,3,4, k256, w, 11);
163 round_0_15(4,5,6,7,0,1,2,3, k256, w, 12);
164 round_0_15(3,4,5,6,7,0,1,2, k256, w, 13);
165 round_0_15(2,3,4,5,6,7,0,1, k256, w, 14);
166 round_0_15(1,2,3,4,5,6,7,0, k256, w, 15);
168 round_16_63(0,1,2,3,4,5,6,7, k256, w, 16);
169 round_16_63(7,0,1,2,3,4,5,6, k256, w, 17);
170 round_16_63(6,7,0,1,2,3,4,5, k256, w, 18);
171 round_16_63(5,6,7,0,1,2,3,4, k256, w, 19);
172 round_16_63(4,5,6,7,0,1,2,3, k256, w, 20);
173 round_16_63(3,4,5,6,7,0,1,2, k256, w, 21);
174 round_16_63(2,3,4,5,6,7,0,1, k256, w, 22);
175 round_16_63(1,2,3,4,5,6,7,0, k256, w, 23);
177 round_16_63(0,1,2,3,4,5,6,7, k256, w, 24);
178 round_16_63(7,0,1,2,3,4,5,6, k256, w, 25);
179 round_16_63(6,7,0,1,2,3,4,5, k256, w, 26);
180 round_16_63(5,6,7,0,1,2,3,4, k256, w, 27);
181 round_16_63(4,5,6,7,0,1,2,3, k256, w, 28);
182 round_16_63(3,4,5,6,7,0,1,2, k256, w, 29);
183 round_16_63(2,3,4,5,6,7,0,1, k256, w, 30);
184 round_16_63(1,2,3,4,5,6,7,0, k256, w, 31);
186 round_16_63(0,1,2,3,4,5,6,7, k256, w, 32);
187 round_16_63(7,0,1,2,3,4,5,6, k256, w, 33);
188 round_16_63(6,7,0,1,2,3,4,5, k256, w, 34);
189 round_16_63(5,6,7,0,1,2,3,4, k256, w, 35);
190 round_16_63(4,5,6,7,0,1,2,3, k256, w, 36);
191 round_16_63(3,4,5,6,7,0,1,2, k256, w, 37);
192 round_16_63(2,3,4,5,6,7,0,1, k256, w, 38);
193 round_16_63(1,2,3,4,5,6,7,0, k256, w, 39);
195 round_16_63(0,1,2,3,4,5,6,7, k256, w, 40);
196 round_16_63(7,0,1,2,3,4,5,6, k256, w, 41);
197 round_16_63(6,7,0,1,2,3,4,5, k256, w, 42);
198 round_16_63(5,6,7,0,1,2,3,4, k256, w, 43);
199 round_16_63(4,5,6,7,0,1,2,3, k256, w, 44);
200 round_16_63(3,4,5,6,7,0,1,2, k256, w, 45);
201 round_16_63(2,3,4,5,6,7,0,1, k256, w, 46);
202 round_16_63(1,2,3,4,5,6,7,0, k256, w, 47);
204 round_16_63(0,1,2,3,4,5,6,7, k256, w, 48);
205 round_16_63(7,0,1,2,3,4,5,6, k256, w, 49);
206 round_16_63(6,7,0,1,2,3,4,5, k256, w, 50);
207 round_16_63(5,6,7,0,1,2,3,4, k256, w, 51);
208 round_16_63(4,5,6,7,0,1,2,3, k256, w, 52);
209 round_16_63(3,4,5,6,7,0,1,2, k256, w, 53);
210 round_16_63(2,3,4,5,6,7,0,1, k256, w, 54);
211 round_16_63(1,2,3,4,5,6,7,0, k256, w, 55);
213 round_16_63(0,1,2,3,4,5,6,7, k256, w, 56);
214 round_16_63(7,0,1,2,3,4,5,6, k256, w, 57);
215 round_16_63(6,7,0,1,2,3,4,5, k256, w, 58);
216 round_16_63(5,6,7,0,1,2,3,4, k256, w, 59);
217 round_16_63(4,5,6,7,0,1,2,3, k256, w, 60);
218 round_16_63(3,4,5,6,7,0,1,2, k256, w, 61);
219 round_16_63(2,3,4,5,6,7,0,1, k256, w, 62);
220 round_16_63(1,2,3,4,5,6,7,0, k256, w, 63);
243 0x428a2f98d728ae22ull, 0x7137449123ef65cdull, 0xb5c0fbcfec4d3b2full, 0xe9b5dba58189dbbcull,
244 0x3956c25bf348b538ull, 0x59f111f1b605d019ull, 0x923f82a4af194f9bull, 0xab1c5ed5da6d8118ull,
245 0xd807aa98a3030242ull, 0x12835b0145706fbeull, 0x243185be4ee4b28cull, 0x550c7dc3d5ffb4e2ull,
246 0x72be5d74f27b896full, 0x80deb1fe3b1696b1ull, 0x9bdc06a725c71235ull, 0xc19bf174cf692694ull,
247 0xe49b69c19ef14ad2ull, 0xefbe4786384f25e3ull, 0x0fc19dc68b8cd5b5ull, 0x240ca1cc77ac9c65ull,
248 0x2de92c6f592b0275ull, 0x4a7484aa6ea6e483ull, 0x5cb0a9dcbd41fbd4ull, 0x76f988da831153b5ull,
249 0x983e5152ee66dfabull, 0xa831c66d2db43210ull, 0xb00327c898fb213full, 0xbf597fc7beef0ee4ull,
250 0xc6e00bf33da88fc2ull, 0xd5a79147930aa725ull, 0x06ca6351e003826full, 0x142929670a0e6e70ull,
251 0x27b70a8546d22ffcull, 0x2e1b21385c26c926ull, 0x4d2c6dfc5ac42aedull, 0x53380d139d95b3dfull,
252 0x650a73548baf63deull, 0x766a0abb3c77b2a8ull, 0x81c2c92e47edaee6ull, 0x92722c851482353bull,
253 0xa2bfe8a14cf10364ull, 0xa81a664bbc423001ull, 0xc24b8b70d0f89791ull, 0xc76c51a30654be30ull,
254 0xd192e819d6ef5218ull, 0xd69906245565a910ull, 0xf40e35855771202aull, 0x106aa07032bbd1b8ull,
255 0x19a4c116b8d2d0c8ull, 0x1e376c085141ab53ull, 0x2748774cdf8eeb99ull, 0x34b0bcb5e19b48a8ull,
256 0x391c0cb3c5c95a63ull, 0x4ed8aa4ae3418acbull, 0x5b9cca4f7763e373ull, 0x682e6ff3d6b2b8a3ull,
257 0x748f82ee5defb2fcull, 0x78a5636f43172f60ull, 0x84c87814a1f0ab72ull, 0x8cc702081a6439ecull,
258 0x90befffa23631e28ull, 0xa4506cebde82bde9ull, 0xbef9a3f7b2c67915ull, 0xc67178f2e372532bull,
259 0xca273eceea26619cull, 0xd186b8c721c0c207ull, 0xeada7dd6cde0eb1eull, 0xf57d4f7fee6ed178ull,
260 0x06f067aa72176fbaull, 0x0a637dc5a2c898a6ull, 0x113f9804bef90daeull, 0x1b710b35131c471bull,
261 0x28db77f523047d84ull, 0x32caab7b40c72493ull, 0x3c9ebe0a15c9bebcull, 0x431d67c49c100d4cull,
262 0x4cc5d4becb3e42b6ull, 0x597f299cfc657e2aull, 0x5fcb6fab3ad6faecull, 0x6c44198c4a475817ull
265 #define TEMPLATEM template <uint64_t t_h0, uint64_t t_h1, uint64_t t_h2, uint64_t t_h3, uint64_t t_h4, uint64_t t_h5, uint64_t t_h6, uint64_t t_h7, size_t t_len> 266 #define CLASSM Sha2Impl<uint64_t, t_h0, t_h1, t_h2, t_h3, t_h4, t_h5, t_h6, t_h7, t_len> 268 #define Sigma0_512(x) (Rotr((x), 28, 64) ^ Rotr((x), 34, 64) ^ Rotr((x), 39, 64)) 269 #define Sigma1_512(x) (Rotr((x), 14, 64) ^ Rotr((x), 18, 64) ^ Rotr((x), 41, 64)) 270 #define sigma0_512(x) (Rotr((x), 1, 64) ^ Rotr((x), 8, 64) ^ Shr((x), 7)) 271 #define sigma1_512(x) (Rotr((x), 19, 64) ^ Rotr((x), 61, 64) ^ Shr((x), 6)) 297 std::stringstream ss;
298 for (
size_t i = 0; i < t_len; ++i)
300 ss << std::hex << std::setw(16) << std::setfill(
'0') << m_digest[i];
305 #define round_0_15(a, b, c, d, e, f, g, h, K, W, t) \ 306 T1 = wv(h) + Sigma1_512(wv(e)) + Ch(wv(e), wv(f), wv(g)) + K[t] + W[t&15]; \ 307 T2 = Sigma0_512(wv(a)) + Maj(wv(a), wv(b), wv(c)); \ 311 #define round_16_79(a, b, c, d, e, f, g, h, K, W, t) \ 312 W[(t)&15] += sigma1_512(W[(t+14)&15]) + W[(t+9)&15] + sigma0_512(W[(t+1)&15]); \ 313 T1 = wv(h) + Sigma1_512(wv(e)) + Ch(wv(e), wv(f), wv(g)) + K[t] + W[t&15]; \ 314 T2 = Sigma0_512(wv(a)) + Maj(wv(a), wv(b), wv(c)); \ 319 void CLASSM::processBuffer()
342 round_0_15(0,1,2,3,4,5,6,7, k512, w, 0);
343 round_0_15(7,0,1,2,3,4,5,6, k512, w, 1);
344 round_0_15(6,7,0,1,2,3,4,5, k512, w, 2);
345 round_0_15(5,6,7,0,1,2,3,4, k512, w, 3);
346 round_0_15(4,5,6,7,0,1,2,3, k512, w, 4);
347 round_0_15(3,4,5,6,7,0,1,2, k512, w, 5);
348 round_0_15(2,3,4,5,6,7,0,1, k512, w, 6);
349 round_0_15(1,2,3,4,5,6,7,0, k512, w, 7);
351 round_0_15(0,1,2,3,4,5,6,7, k512, w, 8);
352 round_0_15(7,0,1,2,3,4,5,6, k512, w, 9);
353 round_0_15(6,7,0,1,2,3,4,5, k512, w, 10);
354 round_0_15(5,6,7,0,1,2,3,4, k512, w, 11);
355 round_0_15(4,5,6,7,0,1,2,3, k512, w, 12);
356 round_0_15(3,4,5,6,7,0,1,2, k512, w, 13);
357 round_0_15(2,3,4,5,6,7,0,1, k512, w, 14);
358 round_0_15(1,2,3,4,5,6,7,0, k512, w, 15);
360 round_16_79(0,1,2,3,4,5,6,7, k512, w, 16);
361 round_16_79(7,0,1,2,3,4,5,6, k512, w, 17);
362 round_16_79(6,7,0,1,2,3,4,5, k512, w, 18);
363 round_16_79(5,6,7,0,1,2,3,4, k512, w, 19);
364 round_16_79(4,5,6,7,0,1,2,3, k512, w, 20);
365 round_16_79(3,4,5,6,7,0,1,2, k512, w, 21);
366 round_16_79(2,3,4,5,6,7,0,1, k512, w, 22);
367 round_16_79(1,2,3,4,5,6,7,0, k512, w, 23);
369 round_16_79(0,1,2,3,4,5,6,7, k512, w, 24);
370 round_16_79(7,0,1,2,3,4,5,6, k512, w, 25);
371 round_16_79(6,7,0,1,2,3,4,5, k512, w, 26);
372 round_16_79(5,6,7,0,1,2,3,4, k512, w, 27);
373 round_16_79(4,5,6,7,0,1,2,3, k512, w, 28);
374 round_16_79(3,4,5,6,7,0,1,2, k512, w, 29);
375 round_16_79(2,3,4,5,6,7,0,1, k512, w, 30);
376 round_16_79(1,2,3,4,5,6,7,0, k512, w, 31);
378 round_16_79(0,1,2,3,4,5,6,7, k512, w, 32);
379 round_16_79(7,0,1,2,3,4,5,6, k512, w, 33);
380 round_16_79(6,7,0,1,2,3,4,5, k512, w, 34);
381 round_16_79(5,6,7,0,1,2,3,4, k512, w, 35);
382 round_16_79(4,5,6,7,0,1,2,3, k512, w, 36);
383 round_16_79(3,4,5,6,7,0,1,2, k512, w, 37);
384 round_16_79(2,3,4,5,6,7,0,1, k512, w, 38);
385 round_16_79(1,2,3,4,5,6,7,0, k512, w, 39);
387 round_16_79(0,1,2,3,4,5,6,7, k512, w, 40);
388 round_16_79(7,0,1,2,3,4,5,6, k512, w, 41);
389 round_16_79(6,7,0,1,2,3,4,5, k512, w, 42);
390 round_16_79(5,6,7,0,1,2,3,4, k512, w, 43);
391 round_16_79(4,5,6,7,0,1,2,3, k512, w, 44);
392 round_16_79(3,4,5,6,7,0,1,2, k512, w, 45);
393 round_16_79(2,3,4,5,6,7,0,1, k512, w, 46);
394 round_16_79(1,2,3,4,5,6,7,0, k512, w, 47);
396 round_16_79(0,1,2,3,4,5,6,7, k512, w, 48);
397 round_16_79(7,0,1,2,3,4,5,6, k512, w, 49);
398 round_16_79(6,7,0,1,2,3,4,5, k512, w, 50);
399 round_16_79(5,6,7,0,1,2,3,4, k512, w, 51);
400 round_16_79(4,5,6,7,0,1,2,3, k512, w, 52);
401 round_16_79(3,4,5,6,7,0,1,2, k512, w, 53);
402 round_16_79(2,3,4,5,6,7,0,1, k512, w, 54);
403 round_16_79(1,2,3,4,5,6,7,0, k512, w, 55);
405 round_16_79(0,1,2,3,4,5,6,7, k512, w, 56);
406 round_16_79(7,0,1,2,3,4,5,6, k512, w, 57);
407 round_16_79(6,7,0,1,2,3,4,5, k512, w, 58);
408 round_16_79(5,6,7,0,1,2,3,4, k512, w, 59);
409 round_16_79(4,5,6,7,0,1,2,3, k512, w, 60);
410 round_16_79(3,4,5,6,7,0,1,2, k512, w, 61);
411 round_16_79(2,3,4,5,6,7,0,1, k512, w, 62);
412 round_16_79(1,2,3,4,5,6,7,0, k512, w, 63);
414 round_16_79(0,1,2,3,4,5,6,7, k512, w, 64);
415 round_16_79(7,0,1,2,3,4,5,6, k512, w, 65);
416 round_16_79(6,7,0,1,2,3,4,5, k512, w, 66);
417 round_16_79(5,6,7,0,1,2,3,4, k512, w, 67);
418 round_16_79(4,5,6,7,0,1,2,3, k512, w, 68);
419 round_16_79(3,4,5,6,7,0,1,2, k512, w, 69);
420 round_16_79(2,3,4,5,6,7,0,1, k512, w, 70);
421 round_16_79(1,2,3,4,5,6,7,0, k512, w, 71);
423 round_16_79(0,1,2,3,4,5,6,7, k512, w, 72);
424 round_16_79(7,0,1,2,3,4,5,6, k512, w, 73);
425 round_16_79(6,7,0,1,2,3,4,5, k512, w, 74);
426 round_16_79(5,6,7,0,1,2,3,4, k512, w, 75);
427 round_16_79(4,5,6,7,0,1,2,3, k512, w, 76);
428 round_16_79(3,4,5,6,7,0,1,2, k512, w, 77);
429 round_16_79(2,3,4,5,6,7,0,1, k512, w, 78);
430 round_16_79(1,2,3,4,5,6,7,0, k512, w, 79);
#define round_0_15(a, b, c, d, e, f, g, h, K, W, t)
static const uint32_t k256[64]
#define round_16_63(a, b, c, d, e, f, g, h, K, W, t)
#define bswaparr(buf, T, i)
#define bswaparr64(buf, T, i)
unsigned __int64 uint64_t
#define round_16_79(a, b, c, d, e, f, g, h, K, W, t)
static const uint64_t k512[80]