sshsh256.c 22 KB

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