sshsha.c 19 KB

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