sshsha.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * SHA-1 algorithm as described at
  3. *
  4. * http://csrc.nist.gov/cryptval/shs.html
  5. */
  6. #include "ssh.h"
  7. #include <assert.h>
  8. /*
  9. * Start by deciding whether we can support hardware SHA at all.
  10. */
  11. #define HW_SHA1_NONE 0
  12. #define HW_SHA1_NI 1
  13. #ifdef _FORCE_SHA_NI
  14. # define HW_SHA1 HW_SHA1_NI
  15. #elif defined(__clang__)
  16. # if __has_attribute(target) && __has_include(<wmmintrin.h>) && \
  17. (defined(__x86_64__) || defined(__i386))
  18. # define HW_SHA1 HW_SHA1_NI
  19. # endif
  20. #elif defined(__GNUC__)
  21. # if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)) && \
  22. (defined(__x86_64__) || defined(__i386))
  23. # define HW_SHA1 HW_SHA1_NI
  24. # endif
  25. #elif defined (_MSC_VER)
  26. # if (defined(_M_X64) || defined(_M_IX86)) && _MSC_FULL_VER >= 150030729
  27. # define HW_SHA1 HW_SHA1_NI
  28. # endif
  29. #endif
  30. #if defined _FORCE_SOFTWARE_SHA || !defined HW_SHA1
  31. # undef HW_SHA1
  32. # define HW_SHA1 HW_SHA1_NONE
  33. #endif
  34. /*
  35. * The actual query function that asks if hardware acceleration is
  36. * available.
  37. */
  38. static bool sha1_hw_available(void);
  39. /*
  40. * The top-level selection function, caching the results of
  41. * sha1_hw_available() so it only has to run once.
  42. */
  43. static bool sha1_hw_available_cached(void)
  44. {
  45. static bool initialised = false;
  46. static bool hw_available;
  47. if (!initialised) {
  48. hw_available = sha1_hw_available();
  49. initialised = true;
  50. }
  51. return hw_available;
  52. }
  53. static ssh_hash *sha1_select(const ssh_hashalg *alg)
  54. {
  55. const ssh_hashalg *real_alg =
  56. sha1_hw_available_cached() ? &ssh_sha1_hw : &ssh_sha1_sw;
  57. return ssh_hash_new(real_alg);
  58. }
  59. const ssh_hashalg ssh_sha1 = {
  60. sha1_select, NULL, NULL, NULL,
  61. 20, 64, "SHA-1",
  62. };
  63. /* ----------------------------------------------------------------------
  64. * Definitions likely to be helpful to multiple implementations.
  65. */
  66. static const uint32_t sha1_initial_state[] = {
  67. 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0,
  68. };
  69. #define SHA1_ROUNDS_PER_STAGE 20
  70. #define SHA1_STAGE0_CONSTANT 0x5a827999
  71. #define SHA1_STAGE1_CONSTANT 0x6ed9eba1
  72. #define SHA1_STAGE2_CONSTANT 0x8f1bbcdc
  73. #define SHA1_STAGE3_CONSTANT 0xca62c1d6
  74. #define SHA1_ROUNDS (4 * SHA1_ROUNDS_PER_STAGE)
  75. typedef struct sha1_block sha1_block;
  76. struct sha1_block {
  77. uint8_t block[64];
  78. size_t used;
  79. uint64_t len;
  80. };
  81. static inline void sha1_block_setup(sha1_block *blk)
  82. {
  83. blk->used = 0;
  84. blk->len = 0;
  85. }
  86. static inline bool sha1_block_write(
  87. sha1_block *blk, const void **vdata, size_t *len)
  88. {
  89. size_t blkleft = sizeof(blk->block) - blk->used;
  90. size_t chunk = *len < blkleft ? *len : blkleft;
  91. const uint8_t *p = *vdata;
  92. memcpy(blk->block + blk->used, p, chunk);
  93. *vdata = p + chunk;
  94. *len -= chunk;
  95. blk->used += chunk;
  96. blk->len += chunk;
  97. if (blk->used == sizeof(blk->block)) {
  98. blk->used = 0;
  99. return true;
  100. }
  101. return false;
  102. }
  103. static inline void sha1_block_pad(sha1_block *blk, BinarySink *bs)
  104. {
  105. uint64_t final_len = blk->len << 3;
  106. size_t pad = 1 + (63 & (55 - blk->used));
  107. put_byte(bs, 0x80);
  108. for (size_t i = 1; i < pad; i++)
  109. put_byte(bs, 0);
  110. put_uint64(bs, final_len);
  111. assert(blk->used == 0 && "Should have exactly hit a block boundary");
  112. }
  113. /* ----------------------------------------------------------------------
  114. * Software implementation of SHA-1.
  115. */
  116. static inline uint32_t rol(uint32_t x, unsigned y)
  117. {
  118. return (x << (31 & y)) | (x >> (31 & -y));
  119. }
  120. static inline uint32_t Ch(uint32_t ctrl, uint32_t if1, uint32_t if0)
  121. {
  122. return if0 ^ (ctrl & (if1 ^ if0));
  123. }
  124. static inline uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
  125. {
  126. return (x & y) | (z & (x | y));
  127. }
  128. static inline uint32_t Par(uint32_t x, uint32_t y, uint32_t z)
  129. {
  130. return (x ^ y ^ z);
  131. }
  132. static inline void sha1_sw_round(
  133. unsigned round_index, const uint32_t *schedule,
  134. uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e,
  135. uint32_t f, uint32_t constant)
  136. {
  137. *e = rol(*a, 5) + f + *e + schedule[round_index] + constant;
  138. *b = rol(*b, 30);
  139. }
  140. static void sha1_sw_block(uint32_t *core, const uint8_t *block)
  141. {
  142. uint32_t w[SHA1_ROUNDS];
  143. uint32_t a,b,c,d,e;
  144. for (size_t t = 0; t < 16; t++)
  145. w[t] = GET_32BIT_MSB_FIRST(block + 4*t);
  146. for (size_t t = 16; t < SHA1_ROUNDS; t++)
  147. w[t] = rol(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16], 1);
  148. a = core[0]; b = core[1]; c = core[2]; d = core[3];
  149. e = core[4];
  150. size_t t = 0;
  151. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  152. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Ch(b,c,d), SHA1_STAGE0_CONSTANT);
  153. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Ch(a,b,c), SHA1_STAGE0_CONSTANT);
  154. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Ch(e,a,b), SHA1_STAGE0_CONSTANT);
  155. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Ch(d,e,a), SHA1_STAGE0_CONSTANT);
  156. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Ch(c,d,e), SHA1_STAGE0_CONSTANT);
  157. }
  158. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  159. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Par(b,c,d), SHA1_STAGE1_CONSTANT);
  160. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Par(a,b,c), SHA1_STAGE1_CONSTANT);
  161. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Par(e,a,b), SHA1_STAGE1_CONSTANT);
  162. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Par(d,e,a), SHA1_STAGE1_CONSTANT);
  163. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Par(c,d,e), SHA1_STAGE1_CONSTANT);
  164. }
  165. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  166. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Maj(b,c,d), SHA1_STAGE2_CONSTANT);
  167. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Maj(a,b,c), SHA1_STAGE2_CONSTANT);
  168. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Maj(e,a,b), SHA1_STAGE2_CONSTANT);
  169. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Maj(d,e,a), SHA1_STAGE2_CONSTANT);
  170. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Maj(c,d,e), SHA1_STAGE2_CONSTANT);
  171. }
  172. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  173. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Par(b,c,d), SHA1_STAGE3_CONSTANT);
  174. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Par(a,b,c), SHA1_STAGE3_CONSTANT);
  175. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Par(e,a,b), SHA1_STAGE3_CONSTANT);
  176. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Par(d,e,a), SHA1_STAGE3_CONSTANT);
  177. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Par(c,d,e), SHA1_STAGE3_CONSTANT);
  178. }
  179. core[0] += a; core[1] += b; core[2] += c; core[3] += d; core[4] += e;
  180. smemclr(w, sizeof(w));
  181. }
  182. typedef struct sha1_sw {
  183. uint32_t core[5];
  184. sha1_block blk;
  185. BinarySink_IMPLEMENTATION;
  186. ssh_hash hash;
  187. } sha1_sw;
  188. static void sha1_sw_write(BinarySink *bs, const void *vp, size_t len);
  189. static ssh_hash *sha1_sw_new(const ssh_hashalg *alg)
  190. {
  191. sha1_sw *s = snew(sha1_sw);
  192. memcpy(s->core, sha1_initial_state, sizeof(s->core));
  193. sha1_block_setup(&s->blk);
  194. s->hash.vt = alg;
  195. BinarySink_INIT(s, sha1_sw_write);
  196. BinarySink_DELEGATE_INIT(&s->hash, s);
  197. return &s->hash;
  198. }
  199. static ssh_hash *sha1_sw_copy(ssh_hash *hash)
  200. {
  201. sha1_sw *s = container_of(hash, sha1_sw, hash);
  202. sha1_sw *copy = snew(sha1_sw);
  203. memcpy(copy, s, sizeof(*copy));
  204. BinarySink_COPIED(copy);
  205. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  206. return &copy->hash;
  207. }
  208. static void sha1_sw_free(ssh_hash *hash)
  209. {
  210. sha1_sw *s = container_of(hash, sha1_sw, hash);
  211. smemclr(s, sizeof(*s));
  212. sfree(s);
  213. }
  214. static void sha1_sw_write(BinarySink *bs, const void *vp, size_t len)
  215. {
  216. sha1_sw *s = BinarySink_DOWNCAST(bs, sha1_sw);
  217. while (len > 0)
  218. if (sha1_block_write(&s->blk, &vp, &len))
  219. sha1_sw_block(s->core, s->blk.block);
  220. }
  221. static void sha1_sw_final(ssh_hash *hash, uint8_t *digest)
  222. {
  223. sha1_sw *s = container_of(hash, sha1_sw, hash);
  224. sha1_block_pad(&s->blk, BinarySink_UPCAST(s));
  225. for (size_t i = 0; i < 5; i++)
  226. PUT_32BIT_MSB_FIRST(digest + 4*i, s->core[i]);
  227. sha1_sw_free(hash);
  228. }
  229. const ssh_hashalg ssh_sha1_sw = {
  230. sha1_sw_new, sha1_sw_copy, sha1_sw_final, sha1_sw_free,
  231. 20, 64, "SHA-1",
  232. };
  233. /* ----------------------------------------------------------------------
  234. * Hardware-accelerated implementation of SHA-1 using x86 SHA-NI.
  235. */
  236. #if HW_SHA1 == HW_SHA1_NI
  237. /*
  238. * Set target architecture for Clang and GCC
  239. */
  240. #if !defined(__clang__) && defined(__GNUC__)
  241. # pragma GCC target("sha")
  242. # pragma GCC target("sse4.1")
  243. #endif
  244. #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
  245. # define FUNC_ISA __attribute__ ((target("sse4.1,sha")))
  246. #else
  247. # define FUNC_ISA
  248. #endif
  249. #include <wmmintrin.h>
  250. #include <smmintrin.h>
  251. #include <immintrin.h>
  252. #if defined(__clang__) || defined(__GNUC__)
  253. #include <shaintrin.h>
  254. #endif
  255. #if defined(__clang__) || defined(__GNUC__)
  256. #include <cpuid.h>
  257. #define GET_CPU_ID_0(out) \
  258. __cpuid(0, (out)[0], (out)[1], (out)[2], (out)[3])
  259. #define GET_CPU_ID_7(out) \
  260. __cpuid_count(7, 0, (out)[0], (out)[1], (out)[2], (out)[3])
  261. #else
  262. #define GET_CPU_ID_0(out) __cpuid(out, 0)
  263. #define GET_CPU_ID_7(out) __cpuidex(out, 7, 0)
  264. #endif
  265. static bool sha1_hw_available(void)
  266. {
  267. unsigned int CPUInfo[4];
  268. GET_CPU_ID_0(CPUInfo);
  269. if (CPUInfo[0] < 7)
  270. return false;
  271. GET_CPU_ID_7(CPUInfo);
  272. return CPUInfo[1] & (1 << 29); /* Check SHA */
  273. }
  274. /* SHA1 implementation using new instructions
  275. The code is based on Jeffrey Walton's SHA1 implementation:
  276. https://github.com/noloader/SHA-Intrinsics
  277. */
  278. FUNC_ISA
  279. static inline void sha1_ni_block(__m128i *core, const uint8_t *p)
  280. {
  281. __m128i ABCD, E0, E1, MSG0, MSG1, MSG2, MSG3;
  282. const __m128i MASK = _mm_set_epi64x(
  283. 0x0001020304050607ULL, 0x08090a0b0c0d0e0fULL);
  284. const __m128i *block = (const __m128i *)p;
  285. /* Load initial values */
  286. ABCD = core[0];
  287. E0 = core[1];
  288. /* Rounds 0-3 */
  289. MSG0 = _mm_loadu_si128(block);
  290. MSG0 = _mm_shuffle_epi8(MSG0, MASK);
  291. E0 = _mm_add_epi32(E0, MSG0);
  292. E1 = ABCD;
  293. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  294. /* Rounds 4-7 */
  295. MSG1 = _mm_loadu_si128(block + 1);
  296. MSG1 = _mm_shuffle_epi8(MSG1, MASK);
  297. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  298. E0 = ABCD;
  299. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 0);
  300. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  301. /* Rounds 8-11 */
  302. MSG2 = _mm_loadu_si128(block + 2);
  303. MSG2 = _mm_shuffle_epi8(MSG2, MASK);
  304. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  305. E1 = ABCD;
  306. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  307. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  308. MSG0 = _mm_xor_si128(MSG0, MSG2);
  309. /* Rounds 12-15 */
  310. MSG3 = _mm_loadu_si128(block + 3);
  311. MSG3 = _mm_shuffle_epi8(MSG3, MASK);
  312. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  313. E0 = ABCD;
  314. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  315. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 0);
  316. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  317. MSG1 = _mm_xor_si128(MSG1, MSG3);
  318. /* Rounds 16-19 */
  319. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  320. E1 = ABCD;
  321. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  322. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  323. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  324. MSG2 = _mm_xor_si128(MSG2, MSG0);
  325. /* Rounds 20-23 */
  326. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  327. E0 = ABCD;
  328. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  329. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  330. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  331. MSG3 = _mm_xor_si128(MSG3, MSG1);
  332. /* Rounds 24-27 */
  333. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  334. E1 = ABCD;
  335. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  336. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 1);
  337. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  338. MSG0 = _mm_xor_si128(MSG0, MSG2);
  339. /* Rounds 28-31 */
  340. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  341. E0 = ABCD;
  342. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  343. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  344. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  345. MSG1 = _mm_xor_si128(MSG1, MSG3);
  346. /* Rounds 32-35 */
  347. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  348. E1 = ABCD;
  349. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  350. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 1);
  351. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  352. MSG2 = _mm_xor_si128(MSG2, MSG0);
  353. /* Rounds 36-39 */
  354. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  355. E0 = ABCD;
  356. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  357. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  358. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  359. MSG3 = _mm_xor_si128(MSG3, MSG1);
  360. /* Rounds 40-43 */
  361. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  362. E1 = ABCD;
  363. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  364. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  365. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  366. MSG0 = _mm_xor_si128(MSG0, MSG2);
  367. /* Rounds 44-47 */
  368. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  369. E0 = ABCD;
  370. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  371. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 2);
  372. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  373. MSG1 = _mm_xor_si128(MSG1, MSG3);
  374. /* Rounds 48-51 */
  375. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  376. E1 = ABCD;
  377. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  378. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  379. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  380. MSG2 = _mm_xor_si128(MSG2, MSG0);
  381. /* Rounds 52-55 */
  382. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  383. E0 = ABCD;
  384. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  385. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 2);
  386. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  387. MSG3 = _mm_xor_si128(MSG3, MSG1);
  388. /* Rounds 56-59 */
  389. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  390. E1 = ABCD;
  391. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  392. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  393. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  394. MSG0 = _mm_xor_si128(MSG0, MSG2);
  395. /* Rounds 60-63 */
  396. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  397. E0 = ABCD;
  398. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  399. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  400. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  401. MSG1 = _mm_xor_si128(MSG1, MSG3);
  402. /* Rounds 64-67 */
  403. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  404. E1 = ABCD;
  405. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  406. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 3);
  407. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  408. MSG2 = _mm_xor_si128(MSG2, MSG0);
  409. /* Rounds 68-71 */
  410. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  411. E0 = ABCD;
  412. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  413. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  414. MSG3 = _mm_xor_si128(MSG3, MSG1);
  415. /* Rounds 72-75 */
  416. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  417. E1 = ABCD;
  418. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  419. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 3);
  420. /* Rounds 76-79 */
  421. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  422. E0 = ABCD;
  423. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  424. /* Combine state */
  425. core[0] = _mm_add_epi32(ABCD, core[0]);
  426. core[1] = _mm_sha1nexte_epu32(E0, core[1]);
  427. }
  428. typedef struct sha1_ni {
  429. /*
  430. * core[0] stores the first four words of the SHA-1 state. core[1]
  431. * stores just the fifth word, in the vector lane at the highest
  432. * address.
  433. */
  434. __m128i core[2];
  435. sha1_block blk;
  436. void *pointer_to_free;
  437. BinarySink_IMPLEMENTATION;
  438. ssh_hash hash;
  439. } sha1_ni;
  440. static void sha1_ni_write(BinarySink *bs, const void *vp, size_t len);
  441. static sha1_ni *sha1_ni_alloc(void)
  442. {
  443. /*
  444. * The __m128i variables in the context structure need to be
  445. * 16-byte aligned, but not all malloc implementations that this
  446. * code has to work with will guarantee to return a 16-byte
  447. * aligned pointer. So we over-allocate, manually realign the
  448. * pointer ourselves, and store the original one inside the
  449. * context so we know how to free it later.
  450. */
  451. void *allocation = smalloc(sizeof(sha1_ni) + 15);
  452. uintptr_t alloc_address = (uintptr_t)allocation;
  453. uintptr_t aligned_address = (alloc_address + 15) & ~15;
  454. sha1_ni *s = (sha1_ni *)aligned_address;
  455. s->pointer_to_free = allocation;
  456. return s;
  457. }
  458. FUNC_ISA static ssh_hash *sha1_ni_new(const ssh_hashalg *alg)
  459. {
  460. if (!sha1_hw_available_cached())
  461. return NULL;
  462. sha1_ni *s = sha1_ni_alloc();
  463. /* Initialise the core vectors in their storage order */
  464. s->core[0] = _mm_set_epi64x(
  465. 0x67452301efcdab89ULL, 0x98badcfe10325476ULL);
  466. s->core[1] = _mm_set_epi32(0xc3d2e1f0, 0, 0, 0);
  467. sha1_block_setup(&s->blk);
  468. s->hash.vt = alg;
  469. BinarySink_INIT(s, sha1_ni_write);
  470. BinarySink_DELEGATE_INIT(&s->hash, s);
  471. return &s->hash;
  472. }
  473. static ssh_hash *sha1_ni_copy(ssh_hash *hash)
  474. {
  475. sha1_ni *s = container_of(hash, sha1_ni, hash);
  476. sha1_ni *copy = sha1_ni_alloc();
  477. void *ptf_save = copy->pointer_to_free;
  478. *copy = *s; /* structure copy */
  479. copy->pointer_to_free = ptf_save;
  480. BinarySink_COPIED(copy);
  481. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  482. return &copy->hash;
  483. }
  484. static void sha1_ni_free(ssh_hash *hash)
  485. {
  486. sha1_ni *s = container_of(hash, sha1_ni, hash);
  487. void *ptf = s->pointer_to_free;
  488. smemclr(s, sizeof(*s));
  489. sfree(ptf);
  490. }
  491. static void sha1_ni_write(BinarySink *bs, const void *vp, size_t len)
  492. {
  493. sha1_ni *s = BinarySink_DOWNCAST(bs, sha1_ni);
  494. while (len > 0)
  495. if (sha1_block_write(&s->blk, &vp, &len))
  496. sha1_ni_block(s->core, s->blk.block);
  497. }
  498. FUNC_ISA static void sha1_ni_final(ssh_hash *hash, uint8_t *digest)
  499. {
  500. sha1_ni *s = container_of(hash, sha1_ni, hash);
  501. sha1_block_pad(&s->blk, BinarySink_UPCAST(s));
  502. /* Rearrange the first vector into its output order */
  503. __m128i abcd = _mm_shuffle_epi32(s->core[0], 0x1B);
  504. /* Byte-swap it into the output endianness */
  505. const __m128i mask = _mm_setr_epi8(3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12);
  506. abcd = _mm_shuffle_epi8(abcd, mask);
  507. /* And store it */
  508. _mm_storeu_si128((__m128i *)digest, abcd);
  509. /* Finally, store the leftover word */
  510. uint32_t e = _mm_extract_epi32(s->core[1], 3);
  511. PUT_32BIT_MSB_FIRST(digest + 16, e);
  512. sha1_ni_free(hash);
  513. }
  514. const ssh_hashalg ssh_sha1_hw = {
  515. sha1_ni_new, sha1_ni_copy, sha1_ni_final, sha1_ni_free,
  516. 20, 64, "SHA-1",
  517. };
  518. /* ----------------------------------------------------------------------
  519. * Stub functions if we have no hardware-accelerated SHA-1. In this
  520. * case, sha1_hw_new returns NULL (though it should also never be
  521. * selected by sha1_select, so the only thing that should even be
  522. * _able_ to call it is testcrypt). As a result, the remaining vtable
  523. * functions should never be called at all.
  524. */
  525. #elif HW_SHA1 == HW_SHA1_NONE
  526. static bool sha1_hw_available(void)
  527. {
  528. return false;
  529. }
  530. static ssh_hash *sha1_stub_new(const ssh_hashalg *alg)
  531. {
  532. return NULL;
  533. }
  534. #define STUB_BODY { unreachable("Should never be called"); }
  535. static ssh_hash *sha1_stub_copy(ssh_hash *hash) STUB_BODY
  536. static void sha1_stub_free(ssh_hash *hash) STUB_BODY
  537. static void sha1_stub_final(ssh_hash *hash, uint8_t *digest) STUB_BODY
  538. const ssh_hashalg ssh_sha1_hw = {
  539. sha1_stub_new, sha1_stub_copy, sha1_stub_final, sha1_stub_free,
  540. 20, 64, "SHA-1",
  541. };
  542. #endif /* HW_SHA1 */