sshsha.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * SHA1 hash algorithm. Used in SSH-2 as a MAC, and the transform is
  3. * also used as a `stirring' function for the PuTTY random number
  4. * pool. Implemented directly from the specification by Simon
  5. * Tatham.
  6. */
  7. #include "ssh.h"
  8. #include <assert.h>
  9. /* ----------------------------------------------------------------------
  10. * Core SHA algorithm: processes 16-word blocks into a message digest.
  11. */
  12. #define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) )
  13. static void sha1_sw(SHA_State * s, const unsigned char *q, int len);
  14. static void sha1_ni(SHA_State * s, const unsigned char *q, int len);
  15. static void SHA_Core_Init(uint32 h[5])
  16. {
  17. h[0] = 0x67452301;
  18. h[1] = 0xefcdab89;
  19. h[2] = 0x98badcfe;
  20. h[3] = 0x10325476;
  21. h[4] = 0xc3d2e1f0;
  22. }
  23. void SHATransform(word32 * digest, word32 * block)
  24. {
  25. word32 w[80];
  26. word32 a, b, c, d, e;
  27. int t;
  28. #ifdef RANDOM_DIAGNOSTICS
  29. {
  30. extern int random_diagnostics;
  31. if (random_diagnostics) {
  32. int i;
  33. printf("SHATransform:");
  34. for (i = 0; i < 5; i++)
  35. printf(" %08x", digest[i]);
  36. printf(" +");
  37. for (i = 0; i < 16; i++)
  38. printf(" %08x", block[i]);
  39. }
  40. }
  41. #endif
  42. for (t = 0; t < 16; t++)
  43. w[t] = block[t];
  44. for (t = 16; t < 80; t++) {
  45. word32 tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
  46. w[t] = rol(tmp, 1);
  47. }
  48. a = digest[0];
  49. b = digest[1];
  50. c = digest[2];
  51. d = digest[3];
  52. e = digest[4];
  53. for (t = 0; t < 20; t++) {
  54. word32 tmp =
  55. rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999;
  56. e = d;
  57. d = c;
  58. c = rol(b, 30);
  59. b = a;
  60. a = tmp;
  61. }
  62. for (t = 20; t < 40; t++) {
  63. word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1;
  64. e = d;
  65. d = c;
  66. c = rol(b, 30);
  67. b = a;
  68. a = tmp;
  69. }
  70. for (t = 40; t < 60; t++) {
  71. word32 tmp = rol(a,
  72. 5) + ((b & c) | (b & d) | (c & d)) + e + w[t] +
  73. 0x8f1bbcdc;
  74. e = d;
  75. d = c;
  76. c = rol(b, 30);
  77. b = a;
  78. a = tmp;
  79. }
  80. for (t = 60; t < 80; t++) {
  81. word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6;
  82. e = d;
  83. d = c;
  84. c = rol(b, 30);
  85. b = a;
  86. a = tmp;
  87. }
  88. digest[0] += a;
  89. digest[1] += b;
  90. digest[2] += c;
  91. digest[3] += d;
  92. digest[4] += e;
  93. #ifdef RANDOM_DIAGNOSTICS
  94. {
  95. extern int random_diagnostics;
  96. if (random_diagnostics) {
  97. int i;
  98. printf(" =");
  99. for (i = 0; i < 5; i++)
  100. printf(" %08x", digest[i]);
  101. printf("\n");
  102. }
  103. }
  104. #endif
  105. }
  106. /* ----------------------------------------------------------------------
  107. * Outer SHA algorithm: take an arbitrary length byte string,
  108. * convert it into 16-word blocks with the prescribed padding at
  109. * the end, and pass those blocks to the core SHA algorithm.
  110. */
  111. static void SHA_BinarySink_write(BinarySink *bs, const void *p, size_t len);
  112. void SHA_Init(SHA_State * s)
  113. {
  114. SHA_Core_Init(s->h);
  115. s->blkused = 0;
  116. s->lenhi = s->lenlo = 0;
  117. if (supports_sha_ni())
  118. s->sha1 = &sha1_ni;
  119. else
  120. s->sha1 = &sha1_sw;
  121. BinarySink_INIT(s, SHA_BinarySink_write);
  122. }
  123. static void SHA_BinarySink_write(BinarySink *bs, const void *p, size_t len)
  124. {
  125. struct SHA_State *s = BinarySink_DOWNCAST(bs, struct SHA_State);
  126. const unsigned char *q = (const unsigned char *) p;
  127. uint32 lenw = len;
  128. assert(lenw == len);
  129. /*
  130. * Update the length field.
  131. */
  132. s->lenlo += lenw;
  133. s->lenhi += (s->lenlo < lenw);
  134. (*(s->sha1))(s, q, len);
  135. }
  136. static void sha1_sw(SHA_State * s, const unsigned char *q, int len)
  137. {
  138. uint32 wordblock[16];
  139. int i;
  140. if (s->blkused && s->blkused + len < 64) {
  141. /*
  142. * Trivial case: just add to the block.
  143. */
  144. memcpy(s->block + s->blkused, q, len);
  145. s->blkused += len;
  146. } else {
  147. /*
  148. * We must complete and process at least one block.
  149. */
  150. while (s->blkused + len >= 64) {
  151. memcpy(s->block + s->blkused, q, 64 - s->blkused);
  152. q += 64 - s->blkused;
  153. len -= 64 - s->blkused;
  154. /* Now process the block. Gather bytes big-endian into words */
  155. for (i = 0; i < 16; i++) {
  156. wordblock[i] =
  157. (((uint32) s->block[i * 4 + 0]) << 24) |
  158. (((uint32) s->block[i * 4 + 1]) << 16) |
  159. (((uint32) s->block[i * 4 + 2]) << 8) |
  160. (((uint32) s->block[i * 4 + 3]) << 0);
  161. }
  162. SHATransform(s->h, wordblock);
  163. s->blkused = 0;
  164. }
  165. memcpy(s->block, q, len);
  166. s->blkused = len;
  167. }
  168. }
  169. void SHA_Final(SHA_State * s, unsigned char *output)
  170. {
  171. int i;
  172. int pad;
  173. unsigned char c[64];
  174. uint32 lenhi, lenlo;
  175. if (s->blkused >= 56)
  176. pad = 56 + 64 - s->blkused;
  177. else
  178. pad = 56 - s->blkused;
  179. lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3));
  180. lenlo = (s->lenlo << 3);
  181. memset(c, 0, pad);
  182. c[0] = 0x80;
  183. put_data(s, &c, pad);
  184. put_uint32(s, lenhi);
  185. put_uint32(s, lenlo);
  186. for (i = 0; i < 5; i++) {
  187. output[i * 4] = (s->h[i] >> 24) & 0xFF;
  188. output[i * 4 + 1] = (s->h[i] >> 16) & 0xFF;
  189. output[i * 4 + 2] = (s->h[i] >> 8) & 0xFF;
  190. output[i * 4 + 3] = (s->h[i]) & 0xFF;
  191. }
  192. }
  193. void SHA_Simple(const void *p, int len, unsigned char *output)
  194. {
  195. SHA_State s;
  196. SHA_Init(&s);
  197. put_data(&s, p, len);
  198. SHA_Final(&s, output);
  199. smemclr(&s, sizeof(s));
  200. }
  201. /*
  202. * Thin abstraction for things where hashes are pluggable.
  203. */
  204. struct sha1_hash {
  205. SHA_State state;
  206. ssh_hash hash;
  207. };
  208. static ssh_hash *sha1_new(const struct ssh_hashalg *alg)
  209. {
  210. struct sha1_hash *h = snew(struct sha1_hash);
  211. SHA_Init(&h->state);
  212. h->hash.vt = alg;
  213. BinarySink_DELEGATE_INIT(&h->hash, &h->state);
  214. return &h->hash;
  215. }
  216. static ssh_hash *sha1_copy(ssh_hash *hashold)
  217. {
  218. struct sha1_hash *hold, *hnew;
  219. ssh_hash *hashnew = sha1_new(hashold->vt);
  220. hold = container_of(hashold, struct sha1_hash, hash);
  221. hnew = container_of(hashnew, struct sha1_hash, hash);
  222. hnew->state = hold->state;
  223. BinarySink_COPIED(&hnew->state);
  224. return hashnew;
  225. }
  226. static void sha1_free(ssh_hash *hash)
  227. {
  228. struct sha1_hash *h = container_of(hash, struct sha1_hash, hash);
  229. smemclr(h, sizeof(*h));
  230. sfree(h);
  231. }
  232. static void sha1_final(ssh_hash *hash, unsigned char *output)
  233. {
  234. struct sha1_hash *h = container_of(hash, struct sha1_hash, hash);
  235. SHA_Final(&h->state, output);
  236. sha1_free(hash);
  237. }
  238. const struct ssh_hashalg ssh_sha1 = {
  239. sha1_new, sha1_copy, sha1_final, sha1_free, 20, "SHA-1"
  240. };
  241. /* ----------------------------------------------------------------------
  242. * The above is the SHA-1 algorithm itself. Now we implement the
  243. * HMAC wrapper on it.
  244. */
  245. struct hmacsha1 {
  246. SHA_State sha[3];
  247. ssh2_mac mac;
  248. };
  249. static ssh2_mac *hmacsha1_new(
  250. const struct ssh2_macalg *alg, ssh2_cipher *cipher)
  251. {
  252. struct hmacsha1 *ctx = snew(struct hmacsha1);
  253. ctx->mac.vt = alg;
  254. BinarySink_DELEGATE_INIT(&ctx->mac, &ctx->sha[2]);
  255. return &ctx->mac;
  256. }
  257. static void hmacsha1_free(ssh2_mac *mac)
  258. {
  259. struct hmacsha1 *ctx = container_of(mac, struct hmacsha1, mac);
  260. smemclr(ctx, sizeof(*ctx));
  261. sfree(ctx);
  262. }
  263. static void sha1_key_internal(SHA_State *keys,
  264. const unsigned char *key, int len)
  265. {
  266. unsigned char foo[64];
  267. int i;
  268. memset(foo, 0x36, 64);
  269. for (i = 0; i < len && i < 64; i++)
  270. foo[i] ^= key[i];
  271. SHA_Init(&keys[0]);
  272. put_data(&keys[0], foo, 64);
  273. memset(foo, 0x5C, 64);
  274. for (i = 0; i < len && i < 64; i++)
  275. foo[i] ^= key[i];
  276. SHA_Init(&keys[1]);
  277. put_data(&keys[1], foo, 64);
  278. smemclr(foo, 64); /* burn the evidence */
  279. }
  280. static void hmacsha1_key(ssh2_mac *mac, const void *key)
  281. {
  282. struct hmacsha1 *ctx = container_of(mac, struct hmacsha1, mac);
  283. /* Reading the key length out of the ssh2_macalg structure means
  284. * this same method can be used for the _buggy variants which use
  285. * a shorter key */
  286. sha1_key_internal(ctx->sha, key, ctx->mac.vt->keylen);
  287. }
  288. static void hmacsha1_start(ssh2_mac *mac)
  289. {
  290. struct hmacsha1 *ctx = container_of(mac, struct hmacsha1, mac);
  291. ctx->sha[2] = ctx->sha[0]; /* structure copy */
  292. BinarySink_COPIED(&ctx->sha[2]);
  293. }
  294. static void hmacsha1_genresult(ssh2_mac *mac, unsigned char *hmac)
  295. {
  296. struct hmacsha1 *ctx = container_of(mac, struct hmacsha1, mac);
  297. SHA_State s;
  298. unsigned char intermediate[20];
  299. s = ctx->sha[2]; /* structure copy */
  300. BinarySink_COPIED(&s);
  301. SHA_Final(&s, intermediate);
  302. s = ctx->sha[1]; /* structure copy */
  303. BinarySink_COPIED(&s);
  304. put_data(&s, intermediate, 20);
  305. SHA_Final(&s, intermediate);
  306. memcpy(hmac, intermediate, ctx->mac.vt->len);
  307. smemclr(intermediate, sizeof(intermediate));
  308. }
  309. void hmac_sha1_simple(const void *key, int keylen,
  310. const void *data, int datalen,
  311. unsigned char *output) {
  312. SHA_State states[2];
  313. unsigned char intermediate[20];
  314. sha1_key_internal(states, key, keylen);
  315. put_data(&states[0], data, datalen);
  316. SHA_Final(&states[0], intermediate);
  317. put_data(&states[1], intermediate, 20);
  318. SHA_Final(&states[1], output);
  319. }
  320. const struct ssh2_macalg ssh_hmac_sha1 = {
  321. hmacsha1_new, hmacsha1_free, hmacsha1_key,
  322. hmacsha1_start, hmacsha1_genresult,
  323. "hmac-sha1", "[email protected]",
  324. 20, 20,
  325. "HMAC-SHA1"
  326. };
  327. const struct ssh2_macalg ssh_hmac_sha1_96 = {
  328. hmacsha1_new, hmacsha1_free, hmacsha1_key,
  329. hmacsha1_start, hmacsha1_genresult,
  330. "hmac-sha1-96", "[email protected]",
  331. 12, 20,
  332. "HMAC-SHA1-96"
  333. };
  334. const struct ssh2_macalg ssh_hmac_sha1_buggy = {
  335. hmacsha1_new, hmacsha1_free, hmacsha1_key,
  336. hmacsha1_start, hmacsha1_genresult,
  337. "hmac-sha1", NULL,
  338. 20, 16,
  339. "bug-compatible HMAC-SHA1"
  340. };
  341. const struct ssh2_macalg ssh_hmac_sha1_96_buggy = {
  342. hmacsha1_new, hmacsha1_free, hmacsha1_key,
  343. hmacsha1_start, hmacsha1_genresult,
  344. "hmac-sha1-96", NULL,
  345. 12, 16,
  346. "bug-compatible HMAC-SHA1-96"
  347. };
  348. #ifdef COMPILER_SUPPORTS_SHA_NI
  349. #if defined _MSC_VER && defined _M_AMD64
  350. # include <intrin.h>
  351. #endif
  352. /*
  353. * Set target architecture for Clang and GCC
  354. */
  355. #if !defined(__clang__) && defined(__GNUC__)
  356. # pragma GCC target("sha")
  357. # pragma GCC target("sse4.1")
  358. #endif
  359. #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 5))
  360. # define FUNC_ISA __attribute__ ((target("sse4.1,sha")))
  361. #else
  362. # define FUNC_ISA
  363. #endif
  364. #include <wmmintrin.h>
  365. #include <smmintrin.h>
  366. #include <immintrin.h>
  367. #if defined(__clang__) || defined(__GNUC__)
  368. #include <shaintrin.h>
  369. #endif
  370. /*
  371. * Determinators of CPU type
  372. */
  373. #if defined(__clang__) || defined(__GNUC__)
  374. #include <cpuid.h>
  375. int supports_sha_ni(void)
  376. {
  377. unsigned int CPUInfo[4];
  378. __cpuid(0, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
  379. if (CPUInfo[0] < 7)
  380. return 0;
  381. __cpuid_count(7, 0, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
  382. return CPUInfo[1] & (1 << 29); /* SHA */
  383. }
  384. #else /* defined(__clang__) || defined(__GNUC__) */
  385. int supports_sha_ni(void)
  386. {
  387. unsigned int CPUInfo[4];
  388. __cpuid(CPUInfo, 0);
  389. if (CPUInfo[0] < 7)
  390. return 0;
  391. __cpuidex(CPUInfo, 7, 0);
  392. return CPUInfo[1] & (1 << 29); /* Check SHA */
  393. }
  394. #endif /* defined(__clang__) || defined(__GNUC__) */
  395. /* SHA1 implementation using new instructions
  396. The code is based on Jeffrey Walton's SHA1 implementation:
  397. https://github.com/noloader/SHA-Intrinsics
  398. */
  399. FUNC_ISA
  400. static void sha1_ni_(SHA_State * s, const unsigned char *q, int len)
  401. {
  402. if (s->blkused && s->blkused + len < 64) {
  403. /*
  404. * Trivial case: just add to the block.
  405. */
  406. memcpy(s->block + s->blkused, q, len);
  407. s->blkused += len;
  408. } else {
  409. __m128i ABCD, ABCD_SAVE, E0, E0_SAVE, E1;
  410. const __m128i MASK = _mm_set_epi64x(0x0001020304050607ULL, 0x08090a0b0c0d0e0fULL);
  411. ABCD = _mm_loadu_si128((const __m128i*) s->h);
  412. E0 = _mm_set_epi32(s->h[4], 0, 0, 0);
  413. ABCD = _mm_shuffle_epi32(ABCD, 0x1B);
  414. /*
  415. * We must complete and process at least one block.
  416. */
  417. while (s->blkused + len >= 64)
  418. {
  419. __m128i MSG0, MSG1, MSG2, MSG3;
  420. memcpy(s->block + s->blkused, q, 64 - s->blkused);
  421. q += 64 - s->blkused;
  422. len -= 64 - s->blkused;
  423. /* Save current state */
  424. ABCD_SAVE = ABCD;
  425. E0_SAVE = E0;
  426. /* Rounds 0-3 */
  427. MSG0 = _mm_loadu_si128((const __m128i*)(s->block + 0));
  428. MSG0 = _mm_shuffle_epi8(MSG0, MASK);
  429. E0 = _mm_add_epi32(E0, MSG0);
  430. E1 = ABCD;
  431. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  432. /* Rounds 4-7 */
  433. MSG1 = _mm_loadu_si128((const __m128i*)(s->block + 16));
  434. MSG1 = _mm_shuffle_epi8(MSG1, MASK);
  435. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  436. E0 = ABCD;
  437. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 0);
  438. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  439. /* Rounds 8-11 */
  440. MSG2 = _mm_loadu_si128((const __m128i*)(s->block + 32));
  441. MSG2 = _mm_shuffle_epi8(MSG2, MASK);
  442. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  443. E1 = ABCD;
  444. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  445. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  446. MSG0 = _mm_xor_si128(MSG0, MSG2);
  447. /* Rounds 12-15 */
  448. MSG3 = _mm_loadu_si128((const __m128i*)(s->block + 48));
  449. MSG3 = _mm_shuffle_epi8(MSG3, MASK);
  450. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  451. E0 = ABCD;
  452. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  453. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 0);
  454. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  455. MSG1 = _mm_xor_si128(MSG1, MSG3);
  456. /* Rounds 16-19 */
  457. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  458. E1 = ABCD;
  459. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  460. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  461. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  462. MSG2 = _mm_xor_si128(MSG2, MSG0);
  463. /* Rounds 20-23 */
  464. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  465. E0 = ABCD;
  466. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  467. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  468. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  469. MSG3 = _mm_xor_si128(MSG3, MSG1);
  470. /* Rounds 24-27 */
  471. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  472. E1 = ABCD;
  473. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  474. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 1);
  475. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  476. MSG0 = _mm_xor_si128(MSG0, MSG2);
  477. /* Rounds 28-31 */
  478. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  479. E0 = ABCD;
  480. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  481. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  482. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  483. MSG1 = _mm_xor_si128(MSG1, MSG3);
  484. /* Rounds 32-35 */
  485. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  486. E1 = ABCD;
  487. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  488. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 1);
  489. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  490. MSG2 = _mm_xor_si128(MSG2, MSG0);
  491. /* Rounds 36-39 */
  492. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  493. E0 = ABCD;
  494. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  495. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  496. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  497. MSG3 = _mm_xor_si128(MSG3, MSG1);
  498. /* Rounds 40-43 */
  499. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  500. E1 = ABCD;
  501. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  502. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  503. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  504. MSG0 = _mm_xor_si128(MSG0, MSG2);
  505. /* Rounds 44-47 */
  506. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  507. E0 = ABCD;
  508. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  509. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 2);
  510. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  511. MSG1 = _mm_xor_si128(MSG1, MSG3);
  512. /* Rounds 48-51 */
  513. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  514. E1 = ABCD;
  515. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  516. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  517. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  518. MSG2 = _mm_xor_si128(MSG2, MSG0);
  519. /* Rounds 52-55 */
  520. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  521. E0 = ABCD;
  522. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  523. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 2);
  524. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  525. MSG3 = _mm_xor_si128(MSG3, MSG1);
  526. /* Rounds 56-59 */
  527. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  528. E1 = ABCD;
  529. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  530. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  531. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  532. MSG0 = _mm_xor_si128(MSG0, MSG2);
  533. /* Rounds 60-63 */
  534. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  535. E0 = ABCD;
  536. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  537. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  538. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  539. MSG1 = _mm_xor_si128(MSG1, MSG3);
  540. /* Rounds 64-67 */
  541. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  542. E1 = ABCD;
  543. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  544. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 3);
  545. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  546. MSG2 = _mm_xor_si128(MSG2, MSG0);
  547. /* Rounds 68-71 */
  548. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  549. E0 = ABCD;
  550. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  551. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  552. MSG3 = _mm_xor_si128(MSG3, MSG1);
  553. /* Rounds 72-75 */
  554. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  555. E1 = ABCD;
  556. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  557. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 3);
  558. /* Rounds 76-79 */
  559. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  560. E0 = ABCD;
  561. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  562. /* Combine state */
  563. E0 = _mm_sha1nexte_epu32(E0, E0_SAVE);
  564. ABCD = _mm_add_epi32(ABCD, ABCD_SAVE);
  565. s->blkused = 0;
  566. }
  567. ABCD = _mm_shuffle_epi32(ABCD, 0x1B);
  568. /* Save state */
  569. _mm_storeu_si128((__m128i*) s->h, ABCD);
  570. s->h[4] = _mm_extract_epi32(E0, 3);
  571. memcpy(s->block, q, len);
  572. s->blkused = len;
  573. }
  574. }
  575. /*
  576. * Workaround LLVM bug https://bugs.llvm.org/show_bug.cgi?id=34980
  577. */
  578. static void sha1_ni(SHA_State * s, const unsigned char *q, int len)
  579. {
  580. sha1_ni_(s, q, len);
  581. }
  582. #else /* COMPILER_SUPPORTS_AES_NI */
  583. static void sha1_ni(SHA_State * s, const unsigned char *q, int len)
  584. {
  585. assert(0);
  586. }
  587. int supports_sha_ni(void)
  588. {
  589. return 0;
  590. }
  591. #endif /* COMPILER_SUPPORTS_AES_NI */