sshsh256.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * SHA-256 algorithm as described at
  3. *
  4. * http://csrc.nist.gov/cryptval/shs.html
  5. */
  6. #include "ssh.h"
  7. #include <assert.h>
  8. /* ----------------------------------------------------------------------
  9. * Core SHA256 algorithm: processes 16-word blocks into a message digest.
  10. */
  11. #define ror(x,y) ( ((x) << (32-y)) | (((uint32)(x)) >> (y)) )
  12. #define shr(x,y) ( (((uint32)(x)) >> (y)) )
  13. #define Ch(x,y,z) ( ((x) & (y)) ^ (~(x) & (z)) )
  14. #define Maj(x,y,z) ( ((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)) )
  15. #define bigsigma0(x) ( ror((x),2) ^ ror((x),13) ^ ror((x),22) )
  16. #define bigsigma1(x) ( ror((x),6) ^ ror((x),11) ^ ror((x),25) )
  17. #define smallsigma0(x) ( ror((x),7) ^ ror((x),18) ^ shr((x),3) )
  18. #define smallsigma1(x) ( ror((x),17) ^ ror((x),19) ^ shr((x),10) )
  19. static void SHA256_sw(SHA256_State *s, const unsigned char *q, int len);
  20. static void SHA256_ni(SHA256_State * s, const unsigned char *q, int len);
  21. void SHA256_Core_Init(SHA256_State *s) {
  22. s->h[0] = 0x6a09e667;
  23. s->h[1] = 0xbb67ae85;
  24. s->h[2] = 0x3c6ef372;
  25. s->h[3] = 0xa54ff53a;
  26. s->h[4] = 0x510e527f;
  27. s->h[5] = 0x9b05688c;
  28. s->h[6] = 0x1f83d9ab;
  29. s->h[7] = 0x5be0cd19;
  30. }
  31. void SHA256_Block(SHA256_State *s, uint32 *block) {
  32. uint32 w[80];
  33. uint32 a,b,c,d,e,f,g,h;
  34. static const int k[] = {
  35. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  36. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  37. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  38. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  39. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  40. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  41. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  42. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  43. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  44. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  45. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  46. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  47. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  48. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  49. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  50. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  51. };
  52. int t;
  53. for (t = 0; t < 16; t++)
  54. w[t] = block[t];
  55. for (t = 16; t < 64; t++)
  56. w[t] = smallsigma1(w[t-2]) + w[t-7] + smallsigma0(w[t-15]) + w[t-16];
  57. a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3];
  58. e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7];
  59. for (t = 0; t < 64; t+=8) {
  60. uint32 t1, t2;
  61. #define ROUND(j,a,b,c,d,e,f,g,h) \
  62. t1 = h + bigsigma1(e) + Ch(e,f,g) + k[j] + w[j]; \
  63. t2 = bigsigma0(a) + Maj(a,b,c); \
  64. d = d + t1; h = t1 + t2;
  65. ROUND(t+0, a,b,c,d,e,f,g,h);
  66. ROUND(t+1, h,a,b,c,d,e,f,g);
  67. ROUND(t+2, g,h,a,b,c,d,e,f);
  68. ROUND(t+3, f,g,h,a,b,c,d,e);
  69. ROUND(t+4, e,f,g,h,a,b,c,d);
  70. ROUND(t+5, d,e,f,g,h,a,b,c);
  71. ROUND(t+6, c,d,e,f,g,h,a,b);
  72. ROUND(t+7, b,c,d,e,f,g,h,a);
  73. }
  74. s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d;
  75. s->h[4] += e; s->h[5] += f; s->h[6] += g; s->h[7] += h;
  76. }
  77. /* ----------------------------------------------------------------------
  78. * Outer SHA256 algorithm: take an arbitrary length byte string,
  79. * convert it into 16-word blocks with the prescribed padding at
  80. * the end, and pass those blocks to the core SHA256 algorithm.
  81. */
  82. #define BLKSIZE 64
  83. static void SHA256_BinarySink_write(BinarySink *bs,
  84. const void *p, size_t len);
  85. void SHA256_Init(SHA256_State *s) {
  86. SHA256_Core_Init(s);
  87. s->blkused = 0;
  88. s->lenhi = s->lenlo = 0;
  89. if (supports_sha_ni())
  90. s->sha256 = &SHA256_ni;
  91. else
  92. s->sha256 = &SHA256_sw;
  93. BinarySink_INIT(s, SHA256_BinarySink_write);
  94. }
  95. static void SHA256_BinarySink_write(BinarySink *bs,
  96. const void *p, size_t len)
  97. {
  98. struct SHA256_State *s = BinarySink_DOWNCAST(bs, struct SHA256_State);
  99. unsigned char *q = (unsigned char *)p;
  100. uint32 lenw = len;
  101. assert(len == lenw);
  102. /*
  103. * Update the length field.
  104. */
  105. s->lenlo += lenw;
  106. s->lenhi += (s->lenlo < lenw);
  107. (*(s->sha256))(s, q, len);
  108. }
  109. static void SHA256_sw(SHA256_State *s, const unsigned char *q, int len) {
  110. uint32 wordblock[16];
  111. int i;
  112. if (s->blkused && s->blkused+len < BLKSIZE) {
  113. /*
  114. * Trivial case: just add to the block.
  115. */
  116. memcpy(s->block + s->blkused, q, len);
  117. s->blkused += len;
  118. } else {
  119. /*
  120. * We must complete and process at least one block.
  121. */
  122. while (s->blkused + len >= BLKSIZE) {
  123. memcpy(s->block + s->blkused, q, BLKSIZE - s->blkused);
  124. q += BLKSIZE - s->blkused;
  125. len -= BLKSIZE - s->blkused;
  126. /* Now process the block. Gather bytes big-endian into words */
  127. for (i = 0; i < 16; i++) {
  128. wordblock[i] =
  129. ( ((uint32)s->block[i*4+0]) << 24 ) |
  130. ( ((uint32)s->block[i*4+1]) << 16 ) |
  131. ( ((uint32)s->block[i*4+2]) << 8 ) |
  132. ( ((uint32)s->block[i*4+3]) << 0 );
  133. }
  134. SHA256_Block(s, wordblock);
  135. s->blkused = 0;
  136. }
  137. memcpy(s->block, q, len);
  138. s->blkused = len;
  139. }
  140. }
  141. void SHA256_Final(SHA256_State *s, unsigned char *digest) {
  142. int i;
  143. int pad;
  144. unsigned char c[64];
  145. uint32 lenhi, lenlo;
  146. if (s->blkused >= 56)
  147. pad = 56 + 64 - s->blkused;
  148. else
  149. pad = 56 - s->blkused;
  150. lenhi = (s->lenhi << 3) | (s->lenlo >> (32-3));
  151. lenlo = (s->lenlo << 3);
  152. memset(c, 0, pad);
  153. c[0] = 0x80;
  154. put_data(s, &c, pad);
  155. put_uint32(s, lenhi);
  156. put_uint32(s, lenlo);
  157. for (i = 0; i < 8; i++) {
  158. digest[i*4+0] = (s->h[i] >> 24) & 0xFF;
  159. digest[i*4+1] = (s->h[i] >> 16) & 0xFF;
  160. digest[i*4+2] = (s->h[i] >> 8) & 0xFF;
  161. digest[i*4+3] = (s->h[i] >> 0) & 0xFF;
  162. }
  163. }
  164. void SHA256_Simple(const void *p, int len, unsigned char *output) {
  165. SHA256_State s;
  166. SHA256_Init(&s);
  167. put_data(&s, p, len);
  168. SHA256_Final(&s, output);
  169. smemclr(&s, sizeof(s));
  170. }
  171. /*
  172. * Thin abstraction for things where hashes are pluggable.
  173. */
  174. struct sha256_hash {
  175. SHA256_State state;
  176. ssh_hash hash;
  177. };
  178. static ssh_hash *sha256_new(const struct ssh_hashalg *alg)
  179. {
  180. struct sha256_hash *h = snew(struct sha256_hash);
  181. SHA256_Init(&h->state);
  182. h->hash.vt = alg;
  183. BinarySink_DELEGATE_INIT(&h->hash, &h->state);
  184. return &h->hash;
  185. }
  186. static ssh_hash *sha256_copy(ssh_hash *hashold)
  187. {
  188. struct sha256_hash *hold, *hnew;
  189. ssh_hash *hashnew = sha256_new(hashold->vt);
  190. hold = container_of(hashold, struct sha256_hash, hash);
  191. hnew = container_of(hashnew, struct sha256_hash, hash);
  192. hnew->state = hold->state;
  193. BinarySink_COPIED(&hnew->state);
  194. return hashnew;
  195. }
  196. static void sha256_free(ssh_hash *hash)
  197. {
  198. struct sha256_hash *h = container_of(hash, struct sha256_hash, hash);
  199. smemclr(h, sizeof(*h));
  200. sfree(h);
  201. }
  202. static void sha256_final(ssh_hash *hash, unsigned char *output)
  203. {
  204. struct sha256_hash *h = container_of(hash, struct sha256_hash, hash);
  205. SHA256_Final(&h->state, output);
  206. sha256_free(hash);
  207. }
  208. const struct ssh_hashalg ssh_sha256 = {
  209. sha256_new, sha256_copy, sha256_final, sha256_free, 32, "SHA-256"
  210. };
  211. /* ----------------------------------------------------------------------
  212. * The above is the SHA-256 algorithm itself. Now we implement the
  213. * HMAC wrapper on it.
  214. */
  215. struct hmacsha256 {
  216. SHA256_State sha[3];
  217. ssh2_mac mac;
  218. };
  219. static ssh2_mac *hmacsha256_new(
  220. const struct ssh2_macalg *alg, ssh2_cipher *cipher)
  221. {
  222. struct hmacsha256 *ctx = snew(struct hmacsha256);
  223. ctx->mac.vt = alg;
  224. BinarySink_DELEGATE_INIT(&ctx->mac, &ctx->sha[2]);
  225. return &ctx->mac;
  226. }
  227. static void hmacsha256_free(ssh2_mac *mac)
  228. {
  229. struct hmacsha256 *ctx = container_of(mac, struct hmacsha256, mac);
  230. smemclr(ctx, sizeof(*ctx));
  231. sfree(ctx);
  232. }
  233. static void sha256_key_internal(struct hmacsha256 *ctx,
  234. const unsigned char *key, int len)
  235. {
  236. unsigned char foo[64];
  237. int i;
  238. memset(foo, 0x36, 64);
  239. for (i = 0; i < len && i < 64; i++)
  240. foo[i] ^= key[i];
  241. SHA256_Init(&ctx->sha[0]);
  242. put_data(&ctx->sha[0], foo, 64);
  243. memset(foo, 0x5C, 64);
  244. for (i = 0; i < len && i < 64; i++)
  245. foo[i] ^= key[i];
  246. SHA256_Init(&ctx->sha[1]);
  247. put_data(&ctx->sha[1], foo, 64);
  248. smemclr(foo, 64); /* burn the evidence */
  249. }
  250. static void hmacsha256_key(ssh2_mac *mac, const void *key)
  251. {
  252. struct hmacsha256 *ctx = container_of(mac, struct hmacsha256, mac);
  253. sha256_key_internal(ctx, key, ctx->mac.vt->keylen);
  254. }
  255. static void hmacsha256_start(ssh2_mac *mac)
  256. {
  257. struct hmacsha256 *ctx = container_of(mac, struct hmacsha256, mac);
  258. ctx->sha[2] = ctx->sha[0]; /* structure copy */
  259. BinarySink_COPIED(&ctx->sha[2]);
  260. }
  261. static void hmacsha256_genresult(ssh2_mac *mac, unsigned char *hmac)
  262. {
  263. struct hmacsha256 *ctx = container_of(mac, struct hmacsha256, mac);
  264. SHA256_State s;
  265. unsigned char intermediate[32];
  266. s = ctx->sha[2]; /* structure copy */
  267. BinarySink_COPIED(&s);
  268. SHA256_Final(&s, intermediate);
  269. s = ctx->sha[1]; /* structure copy */
  270. BinarySink_COPIED(&s);
  271. put_data(&s, intermediate, 32);
  272. SHA256_Final(&s, hmac);
  273. }
  274. const struct ssh2_macalg ssh_hmac_sha256 = {
  275. hmacsha256_new, hmacsha256_free, hmacsha256_key,
  276. hmacsha256_start, hmacsha256_genresult,
  277. "hmac-sha2-256", "[email protected]",
  278. 32, 32,
  279. "HMAC-SHA-256"
  280. };
  281. #ifdef TEST
  282. #include <stdio.h>
  283. #include <stdlib.h>
  284. #include <assert.h>
  285. int main(void) {
  286. unsigned char digest[32];
  287. int i, j, errors;
  288. struct {
  289. const char *teststring;
  290. unsigned char digest[32];
  291. } tests[] = {
  292. { "abc", {
  293. 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
  294. 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  295. 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
  296. 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad,
  297. } },
  298. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", {
  299. 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
  300. 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
  301. 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
  302. 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1,
  303. } },
  304. };
  305. errors = 0;
  306. for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
  307. SHA256_Simple(tests[i].teststring,
  308. strlen(tests[i].teststring), digest);
  309. for (j = 0; j < 32; j++) {
  310. if (digest[j] != tests[i].digest[j]) {
  311. fprintf(stderr,
  312. "\"%s\" digest byte %d should be 0x%02x, is 0x%02x\n",
  313. tests[i].teststring, j, tests[i].digest[j], digest[j]);
  314. errors++;
  315. }
  316. }
  317. }
  318. printf("%d errors\n", errors);
  319. return 0;
  320. }
  321. #endif
  322. #ifdef COMPILER_SUPPORTS_SHA_NI
  323. #if defined _MSC_VER && defined _M_AMD64
  324. # include <intrin.h>
  325. #endif
  326. /*
  327. * Set target architecture for Clang and GCC
  328. */
  329. #if !defined(__clang__) && defined(__GNUC__)
  330. # pragma GCC target("sha")
  331. # pragma GCC target("sse4.1")
  332. #endif
  333. #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 5))
  334. # define FUNC_ISA __attribute__ ((target("sse4.1,sha")))
  335. #else
  336. # define FUNC_ISA
  337. #endif
  338. #include <wmmintrin.h>
  339. #include <smmintrin.h>
  340. #include <immintrin.h>
  341. #if defined(__clang__) || defined(__GNUC__)
  342. #include <shaintrin.h>
  343. #endif
  344. /* SHA256 implementation using new instructions
  345. The code is based on Jeffrey Walton's SHA256 implementation:
  346. https://github.com/noloader/SHA-Intrinsics
  347. */
  348. FUNC_ISA
  349. static void SHA256_ni_(SHA256_State * s, const unsigned char *q, int len) {
  350. if (s->blkused && s->blkused+len < BLKSIZE) {
  351. /*
  352. * Trivial case: just add to the block.
  353. */
  354. memcpy(s->block + s->blkused, q, len);
  355. s->blkused += len;
  356. } else {
  357. __m128i STATE0, STATE1;
  358. __m128i MSG, TMP;
  359. __m128i MSG0, MSG1, MSG2, MSG3;
  360. __m128i ABEF_SAVE, CDGH_SAVE;
  361. const __m128i MASK = _mm_set_epi64x(0x0c0d0e0f08090a0bULL, 0x0405060700010203ULL);
  362. /* Load initial values */
  363. TMP = _mm_loadu_si128((const __m128i*) &s->h[0]);
  364. STATE1 = _mm_loadu_si128((const __m128i*) &s->h[4]);
  365. TMP = _mm_shuffle_epi32(TMP, 0xB1); /* CDAB */
  366. STATE1 = _mm_shuffle_epi32(STATE1, 0x1B); /* EFGH */
  367. STATE0 = _mm_alignr_epi8(TMP, STATE1, 8); /* ABEF */
  368. STATE1 = _mm_blend_epi16(STATE1, TMP, 0xF0); /* CDGH */
  369. /*
  370. * We must complete and process at least one block.
  371. */
  372. while (s->blkused + len >= BLKSIZE) {
  373. memcpy(s->block + s->blkused, q, BLKSIZE - s->blkused);
  374. q += BLKSIZE - s->blkused;
  375. len -= BLKSIZE - s->blkused;
  376. /* Save current state */
  377. ABEF_SAVE = STATE0;
  378. CDGH_SAVE = STATE1;
  379. /* Rounds 0-3 */
  380. MSG = _mm_loadu_si128((const __m128i*) (s->block + 0));
  381. MSG0 = _mm_shuffle_epi8(MSG, MASK);
  382. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(0xE9B5DBA5B5C0FBCFULL, 0x71374491428A2F98ULL));
  383. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  384. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  385. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  386. /* Rounds 4-7 */
  387. MSG1 = _mm_loadu_si128((const __m128i*) (s->block + 16));
  388. MSG1 = _mm_shuffle_epi8(MSG1, MASK);
  389. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(0xAB1C5ED5923F82A4ULL, 0x59F111F13956C25BULL));
  390. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  391. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  392. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  393. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  394. /* Rounds 8-11 */
  395. MSG2 = _mm_loadu_si128((const __m128i*) (s->block + 32));
  396. MSG2 = _mm_shuffle_epi8(MSG2, MASK);
  397. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(0x550C7DC3243185BEULL, 0x12835B01D807AA98ULL));
  398. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  399. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  400. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  401. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  402. /* Rounds 12-15 */
  403. MSG3 = _mm_loadu_si128((const __m128i*) (s->block + 48));
  404. MSG3 = _mm_shuffle_epi8(MSG3, MASK);
  405. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(0xC19BF1749BDC06A7ULL, 0x80DEB1FE72BE5D74ULL));
  406. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  407. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  408. MSG0 = _mm_add_epi32(MSG0, TMP);
  409. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  410. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  411. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  412. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  413. /* Rounds 16-19 */
  414. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(0x240CA1CC0FC19DC6ULL, 0xEFBE4786E49B69C1ULL));
  415. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  416. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  417. MSG1 = _mm_add_epi32(MSG1, TMP);
  418. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  419. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  420. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  421. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  422. /* Rounds 20-23 */
  423. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(0x76F988DA5CB0A9DCULL, 0x4A7484AA2DE92C6FULL));
  424. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  425. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  426. MSG2 = _mm_add_epi32(MSG2, TMP);
  427. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  428. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  429. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  430. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  431. /* Rounds 24-27 */
  432. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(0xBF597FC7B00327C8ULL, 0xA831C66D983E5152ULL));
  433. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  434. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  435. MSG3 = _mm_add_epi32(MSG3, TMP);
  436. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  437. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  438. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  439. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  440. /* Rounds 28-31 */
  441. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(0x1429296706CA6351ULL, 0xD5A79147C6E00BF3ULL));
  442. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  443. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  444. MSG0 = _mm_add_epi32(MSG0, TMP);
  445. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  446. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  447. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  448. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  449. /* Rounds 32-35 */
  450. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(0x53380D134D2C6DFCULL, 0x2E1B213827B70A85ULL));
  451. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  452. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  453. MSG1 = _mm_add_epi32(MSG1, TMP);
  454. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  455. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  456. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  457. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  458. /* Rounds 36-39 */
  459. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(0x92722C8581C2C92EULL, 0x766A0ABB650A7354ULL));
  460. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  461. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  462. MSG2 = _mm_add_epi32(MSG2, TMP);
  463. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  464. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  465. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  466. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  467. /* Rounds 40-43 */
  468. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(0xC76C51A3C24B8B70ULL, 0xA81A664BA2BFE8A1ULL));
  469. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  470. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  471. MSG3 = _mm_add_epi32(MSG3, TMP);
  472. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  473. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  474. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  475. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  476. /* Rounds 44-47 */
  477. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(0x106AA070F40E3585ULL, 0xD6990624D192E819ULL));
  478. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  479. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  480. MSG0 = _mm_add_epi32(MSG0, TMP);
  481. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  482. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  483. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  484. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  485. /* Rounds 48-51 */
  486. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(0x34B0BCB52748774CULL, 0x1E376C0819A4C116ULL));
  487. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  488. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  489. MSG1 = _mm_add_epi32(MSG1, TMP);
  490. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  491. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  492. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  493. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  494. /* Rounds 52-55 */
  495. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(0x682E6FF35B9CCA4FULL, 0x4ED8AA4A391C0CB3ULL));
  496. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  497. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  498. MSG2 = _mm_add_epi32(MSG2, TMP);
  499. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  500. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  501. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  502. /* Rounds 56-59 */
  503. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(0x8CC7020884C87814ULL, 0x78A5636F748F82EEULL));
  504. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  505. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  506. MSG3 = _mm_add_epi32(MSG3, TMP);
  507. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  508. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  509. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  510. /* Rounds 60-63 */
  511. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(0xC67178F2BEF9A3F7ULL, 0xA4506CEB90BEFFFAULL));
  512. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  513. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  514. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  515. /* Combine state */
  516. STATE0 = _mm_add_epi32(STATE0, ABEF_SAVE);
  517. STATE1 = _mm_add_epi32(STATE1, CDGH_SAVE);
  518. s->blkused = 0;
  519. }
  520. TMP = _mm_shuffle_epi32(STATE0, 0x1B); /* FEBA */
  521. STATE1 = _mm_shuffle_epi32(STATE1, 0xB1); /* DCHG */
  522. STATE0 = _mm_blend_epi16(TMP, STATE1, 0xF0); /* DCBA */
  523. STATE1 = _mm_alignr_epi8(STATE1, TMP, 8); /* ABEF */
  524. /* Save state */
  525. _mm_storeu_si128((__m128i*) &s->h[0], STATE0);
  526. _mm_storeu_si128((__m128i*) &s->h[4], STATE1);
  527. memcpy(s->block, q, len);
  528. s->blkused = len;
  529. }
  530. }
  531. /*
  532. * Workaround LLVM bug https://bugs.llvm.org/show_bug.cgi?id=34980
  533. */
  534. static void SHA256_ni(SHA256_State * s, const unsigned char *q, int len)
  535. {
  536. SHA256_ni_(s, q, len);
  537. }
  538. #else /* COMPILER_SUPPORTS_AES_NI */
  539. static void SHA256_ni(SHA256_State * s, const unsigned char *q, int len)
  540. {
  541. assert(0);
  542. }
  543. #endif /* COMPILER_SUPPORTS_AES_NI */