sshsh256.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 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 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(const ssh2_macalg *alg, ssh_cipher *cipher)
  222. {
  223. struct hmacsha256 *ctx = snew(struct hmacsha256);
  224. ctx->mac.vt = alg;
  225. BinarySink_DELEGATE_INIT(&ctx->mac, &ctx->sha[2]);
  226. return &ctx->mac;
  227. }
  228. static void hmacsha256_free(ssh2_mac *mac)
  229. {
  230. struct hmacsha256 *ctx = container_of(mac, struct hmacsha256, mac);
  231. smemclr(ctx, sizeof(*ctx));
  232. sfree(ctx);
  233. }
  234. static void sha256_key_internal(struct hmacsha256 *ctx,
  235. const unsigned char *key, int len)
  236. {
  237. unsigned char foo[64];
  238. int i;
  239. memset(foo, 0x36, 64);
  240. for (i = 0; i < len && i < 64; i++)
  241. foo[i] ^= key[i];
  242. SHA256_Init(&ctx->sha[0]);
  243. put_data(&ctx->sha[0], foo, 64);
  244. memset(foo, 0x5C, 64);
  245. for (i = 0; i < len && i < 64; i++)
  246. foo[i] ^= key[i];
  247. SHA256_Init(&ctx->sha[1]);
  248. put_data(&ctx->sha[1], foo, 64);
  249. smemclr(foo, 64); /* burn the evidence */
  250. }
  251. static void hmacsha256_key(ssh2_mac *mac, ptrlen key)
  252. {
  253. struct hmacsha256 *ctx = container_of(mac, struct hmacsha256, mac);
  254. sha256_key_internal(ctx, key.ptr, key.len);
  255. }
  256. static void hmacsha256_start(ssh2_mac *mac)
  257. {
  258. struct hmacsha256 *ctx = container_of(mac, struct hmacsha256, mac);
  259. ctx->sha[2] = ctx->sha[0]; /* structure copy */
  260. BinarySink_COPIED(&ctx->sha[2]);
  261. }
  262. static void hmacsha256_genresult(ssh2_mac *mac, unsigned char *hmac)
  263. {
  264. struct hmacsha256 *ctx = container_of(mac, struct hmacsha256, mac);
  265. SHA256_State s;
  266. unsigned char intermediate[32];
  267. s = ctx->sha[2]; /* structure copy */
  268. BinarySink_COPIED(&s);
  269. SHA256_Final(&s, intermediate);
  270. s = ctx->sha[1]; /* structure copy */
  271. BinarySink_COPIED(&s);
  272. put_data(&s, intermediate, 32);
  273. SHA256_Final(&s, hmac);
  274. }
  275. const ssh2_macalg ssh_hmac_sha256 = {
  276. hmacsha256_new, hmacsha256_free, hmacsha256_key,
  277. hmacsha256_start, hmacsha256_genresult,
  278. "hmac-sha2-256", "[email protected]",
  279. 32, 32,
  280. "HMAC-SHA-256"
  281. };
  282. #endif // !WINSCP_VS
  283. #ifdef COMPILER_SUPPORTS_SHA_NI
  284. #if defined _MSC_VER && defined _M_AMD64
  285. # include <intrin.h>
  286. #endif
  287. /*
  288. * Set target architecture for Clang and GCC
  289. */
  290. #if !defined(__clang__) && defined(__GNUC__)
  291. # pragma GCC target("sha")
  292. # pragma GCC target("sse4.1")
  293. #endif
  294. #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 5))
  295. # define FUNC_ISA __attribute__ ((target("sse4.1,sha")))
  296. #else
  297. # define FUNC_ISA
  298. #endif
  299. #include <wmmintrin.h>
  300. #include <smmintrin.h>
  301. #include <immintrin.h>
  302. #if defined(__clang__) || defined(__GNUC__)
  303. #include <shaintrin.h>
  304. #endif
  305. /* SHA256 implementation using new instructions
  306. The code is based on Jeffrey Walton's SHA256 implementation:
  307. https://github.com/noloader/SHA-Intrinsics
  308. */
  309. FUNC_ISA
  310. static void SHA256_ni_(SHA256_State * s, const unsigned char *q, int len) {
  311. if (s->blkused && s->blkused+len < BLKSIZE) {
  312. /*
  313. * Trivial case: just add to the block.
  314. */
  315. memcpy(s->block + s->blkused, q, len);
  316. s->blkused += len;
  317. } else {
  318. __m128i STATE0, STATE1;
  319. __m128i MSG, TMP;
  320. __m128i MSG0, MSG1, MSG2, MSG3;
  321. __m128i ABEF_SAVE, CDGH_SAVE;
  322. const __m128i MASK = _mm_set_epi64x(0x0c0d0e0f08090a0bULL, 0x0405060700010203ULL);
  323. /* Load initial values */
  324. TMP = _mm_loadu_si128((const __m128i*) &s->h[0]);
  325. STATE1 = _mm_loadu_si128((const __m128i*) &s->h[4]);
  326. TMP = _mm_shuffle_epi32(TMP, 0xB1); /* CDAB */
  327. STATE1 = _mm_shuffle_epi32(STATE1, 0x1B); /* EFGH */
  328. STATE0 = _mm_alignr_epi8(TMP, STATE1, 8); /* ABEF */
  329. STATE1 = _mm_blend_epi16(STATE1, TMP, 0xF0); /* CDGH */
  330. /*
  331. * We must complete and process at least one block.
  332. */
  333. while (s->blkused + len >= BLKSIZE) {
  334. memcpy(s->block + s->blkused, q, BLKSIZE - s->blkused);
  335. q += BLKSIZE - s->blkused;
  336. len -= BLKSIZE - s->blkused;
  337. /* Save current state */
  338. ABEF_SAVE = STATE0;
  339. CDGH_SAVE = STATE1;
  340. /* Rounds 0-3 */
  341. MSG = _mm_loadu_si128((const __m128i*) (s->block + 0));
  342. MSG0 = _mm_shuffle_epi8(MSG, MASK);
  343. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(0xE9B5DBA5B5C0FBCFULL, 0x71374491428A2F98ULL));
  344. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  345. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  346. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  347. /* Rounds 4-7 */
  348. MSG1 = _mm_loadu_si128((const __m128i*) (s->block + 16));
  349. MSG1 = _mm_shuffle_epi8(MSG1, MASK);
  350. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(0xAB1C5ED5923F82A4ULL, 0x59F111F13956C25BULL));
  351. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  352. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  353. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  354. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  355. /* Rounds 8-11 */
  356. MSG2 = _mm_loadu_si128((const __m128i*) (s->block + 32));
  357. MSG2 = _mm_shuffle_epi8(MSG2, MASK);
  358. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(0x550C7DC3243185BEULL, 0x12835B01D807AA98ULL));
  359. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  360. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  361. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  362. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  363. /* Rounds 12-15 */
  364. MSG3 = _mm_loadu_si128((const __m128i*) (s->block + 48));
  365. MSG3 = _mm_shuffle_epi8(MSG3, MASK);
  366. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(0xC19BF1749BDC06A7ULL, 0x80DEB1FE72BE5D74ULL));
  367. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  368. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  369. MSG0 = _mm_add_epi32(MSG0, TMP);
  370. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  371. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  372. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  373. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  374. /* Rounds 16-19 */
  375. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(0x240CA1CC0FC19DC6ULL, 0xEFBE4786E49B69C1ULL));
  376. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  377. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  378. MSG1 = _mm_add_epi32(MSG1, TMP);
  379. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  380. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  381. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  382. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  383. /* Rounds 20-23 */
  384. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(0x76F988DA5CB0A9DCULL, 0x4A7484AA2DE92C6FULL));
  385. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  386. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  387. MSG2 = _mm_add_epi32(MSG2, TMP);
  388. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  389. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  390. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  391. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  392. /* Rounds 24-27 */
  393. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(0xBF597FC7B00327C8ULL, 0xA831C66D983E5152ULL));
  394. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  395. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  396. MSG3 = _mm_add_epi32(MSG3, TMP);
  397. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  398. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  399. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  400. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  401. /* Rounds 28-31 */
  402. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(0x1429296706CA6351ULL, 0xD5A79147C6E00BF3ULL));
  403. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  404. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  405. MSG0 = _mm_add_epi32(MSG0, TMP);
  406. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  407. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  408. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  409. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  410. /* Rounds 32-35 */
  411. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(0x53380D134D2C6DFCULL, 0x2E1B213827B70A85ULL));
  412. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  413. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  414. MSG1 = _mm_add_epi32(MSG1, TMP);
  415. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  416. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  417. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  418. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  419. /* Rounds 36-39 */
  420. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(0x92722C8581C2C92EULL, 0x766A0ABB650A7354ULL));
  421. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  422. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  423. MSG2 = _mm_add_epi32(MSG2, TMP);
  424. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  425. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  426. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  427. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  428. /* Rounds 40-43 */
  429. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(0xC76C51A3C24B8B70ULL, 0xA81A664BA2BFE8A1ULL));
  430. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  431. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  432. MSG3 = _mm_add_epi32(MSG3, TMP);
  433. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  434. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  435. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  436. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  437. /* Rounds 44-47 */
  438. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(0x106AA070F40E3585ULL, 0xD6990624D192E819ULL));
  439. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  440. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  441. MSG0 = _mm_add_epi32(MSG0, TMP);
  442. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  443. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  444. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  445. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  446. /* Rounds 48-51 */
  447. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(0x34B0BCB52748774CULL, 0x1E376C0819A4C116ULL));
  448. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  449. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  450. MSG1 = _mm_add_epi32(MSG1, TMP);
  451. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  452. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  453. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  454. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  455. /* Rounds 52-55 */
  456. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(0x682E6FF35B9CCA4FULL, 0x4ED8AA4A391C0CB3ULL));
  457. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  458. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  459. MSG2 = _mm_add_epi32(MSG2, TMP);
  460. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  461. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  462. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  463. /* Rounds 56-59 */
  464. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(0x8CC7020884C87814ULL, 0x78A5636F748F82EEULL));
  465. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  466. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  467. MSG3 = _mm_add_epi32(MSG3, TMP);
  468. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  469. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  470. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  471. /* Rounds 60-63 */
  472. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(0xC67178F2BEF9A3F7ULL, 0xA4506CEB90BEFFFAULL));
  473. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  474. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  475. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  476. /* Combine state */
  477. STATE0 = _mm_add_epi32(STATE0, ABEF_SAVE);
  478. STATE1 = _mm_add_epi32(STATE1, CDGH_SAVE);
  479. s->blkused = 0;
  480. }
  481. TMP = _mm_shuffle_epi32(STATE0, 0x1B); /* FEBA */
  482. STATE1 = _mm_shuffle_epi32(STATE1, 0xB1); /* DCHG */
  483. STATE0 = _mm_blend_epi16(TMP, STATE1, 0xF0); /* DCBA */
  484. STATE1 = _mm_alignr_epi8(STATE1, TMP, 8); /* ABEF */
  485. /* Save state */
  486. _mm_storeu_si128((__m128i*) &s->h[0], STATE0);
  487. _mm_storeu_si128((__m128i*) &s->h[4], STATE1);
  488. memcpy(s->block, q, len);
  489. s->blkused = len;
  490. }
  491. }
  492. /*
  493. * Workaround LLVM bug https://bugs.llvm.org/show_bug.cgi?id=34980
  494. */
  495. static void SHA256_ni(SHA256_State * s, const unsigned char *q, int len)
  496. {
  497. SHA256_ni_(s, q, len);
  498. }
  499. #else /* COMPILER_SUPPORTS_AES_NI */
  500. static void SHA256_ni(SHA256_State * s, const unsigned char *q, int len)
  501. {
  502. unreachable("SHA256_ni not compiled in");
  503. }
  504. #endif /* COMPILER_SUPPORTS_AES_NI */