sshsha.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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. #define HW_SHA1_NEON 2
  14. #ifdef _FORCE_SHA_NI
  15. # define HW_SHA1 HW_SHA1_NI
  16. #elif defined(__clang__)
  17. # if __has_attribute(target) && __has_include(<wmmintrin.h>) && \
  18. (defined(__x86_64__) || defined(__i386))
  19. # define HW_SHA1 HW_SHA1_NI
  20. # endif
  21. #elif defined(__GNUC__)
  22. # if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) && \
  23. (defined(__x86_64__) || defined(__i386))
  24. # define HW_SHA1 HW_SHA1_NI
  25. # endif
  26. #elif defined (_MSC_VER)
  27. # if (defined(_M_X64) || defined(_M_IX86)) && _MSC_FULL_VER >= 150030729
  28. # define HW_SHA1 HW_SHA1_NI
  29. # endif
  30. #endif
  31. #ifdef _FORCE_SHA_NEON
  32. # define HW_SHA1 HW_SHA1_NEON
  33. #elif defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  34. /* Arm can potentially support both endiannesses, but this code
  35. * hasn't been tested on anything but little. If anyone wants to
  36. * run big-endian, they'll need to fix it first. */
  37. #elif defined __ARM_FEATURE_CRYPTO
  38. /* If the Arm crypto extension is available already, we can
  39. * support NEON SHA without having to enable anything by hand */
  40. # define HW_SHA1 HW_SHA1_NEON
  41. #elif defined(__clang__)
  42. # if __has_attribute(target) && __has_include(<arm_neon.h>) && \
  43. (defined(__aarch64__))
  44. /* clang can enable the crypto extension in AArch64 using
  45. * __attribute__((target)) */
  46. # define HW_SHA1 HW_SHA1_NEON
  47. # define USE_CLANG_ATTR_TARGET_AARCH64
  48. # endif
  49. #elif defined _MSC_VER
  50. /* Visual Studio supports the crypto extension when targeting
  51. * AArch64, but as of VS2017, the AArch32 header doesn't quite
  52. * manage it (declaring the shae/shad intrinsics without a round
  53. * key operand). */
  54. # if defined _M_ARM64
  55. # define HW_SHA1 HW_SHA1_NEON
  56. # if defined _M_ARM64
  57. # define USE_ARM64_NEON_H /* unusual header name in this case */
  58. # endif
  59. # endif
  60. #endif
  61. #if defined _FORCE_SOFTWARE_SHA || !defined HW_SHA1
  62. # undef HW_SHA1
  63. # define HW_SHA1 HW_SHA1_NONE
  64. #endif
  65. /*
  66. * The actual query function that asks if hardware acceleration is
  67. * available.
  68. */
  69. static bool sha1_hw_available(void);
  70. /*
  71. * The top-level selection function, caching the results of
  72. * sha1_hw_available() so it only has to run once.
  73. */
  74. static bool sha1_hw_available_cached(void)
  75. {
  76. static bool initialised = false;
  77. static bool hw_available;
  78. if (!initialised) {
  79. hw_available = sha1_hw_available();
  80. initialised = true;
  81. }
  82. return hw_available;
  83. }
  84. static ssh_hash *sha1_select(const ssh_hashalg *alg)
  85. {
  86. const ssh_hashalg *real_alg =
  87. sha1_hw_available_cached() ? &ssh_sha1_hw : &ssh_sha1_sw;
  88. return ssh_hash_new(real_alg);
  89. }
  90. const ssh_hashalg ssh_sha1 = {
  91. .new = sha1_select,
  92. .hlen = 20,
  93. .blocklen = 64,
  94. HASHALG_NAMES_ANNOTATED("SHA-1", "dummy selector vtable"),
  95. };
  96. /* ----------------------------------------------------------------------
  97. * Definitions likely to be helpful to multiple implementations.
  98. */
  99. static const uint32_t sha1_initial_state[] = {
  100. 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0,
  101. };
  102. #define SHA1_ROUNDS_PER_STAGE 20
  103. #define SHA1_STAGE0_CONSTANT 0x5a827999
  104. #define SHA1_STAGE1_CONSTANT 0x6ed9eba1
  105. #define SHA1_STAGE2_CONSTANT 0x8f1bbcdc
  106. #define SHA1_STAGE3_CONSTANT 0xca62c1d6
  107. #define SHA1_ROUNDS (4 * SHA1_ROUNDS_PER_STAGE)
  108. typedef struct sha1_block sha1_block;
  109. struct sha1_block {
  110. uint8_t block[64];
  111. size_t used;
  112. uint64_t len;
  113. };
  114. static inline void sha1_block_setup(sha1_block *blk)
  115. {
  116. blk->used = 0;
  117. blk->len = 0;
  118. }
  119. static inline bool sha1_block_write(
  120. sha1_block *blk, const void **vdata, size_t *len)
  121. {
  122. size_t blkleft = sizeof(blk->block) - blk->used;
  123. size_t chunk = *len < blkleft ? *len : blkleft;
  124. const uint8_t *p = *vdata;
  125. memcpy(blk->block + blk->used, p, chunk);
  126. *vdata = p + chunk;
  127. *len -= chunk;
  128. blk->used += chunk;
  129. blk->len += chunk;
  130. if (blk->used == sizeof(blk->block)) {
  131. blk->used = 0;
  132. return true;
  133. }
  134. return false;
  135. }
  136. static inline void sha1_block_pad(sha1_block *blk, BinarySink *bs)
  137. {
  138. uint64_t final_len = blk->len << 3;
  139. size_t pad = 1 + (63 & (55 - blk->used));
  140. put_byte(bs, 0x80);
  141. for (size_t i = 1; i < pad; i++)
  142. put_byte(bs, 0);
  143. put_uint64(bs, final_len);
  144. assert(blk->used == 0 && "Should have exactly hit a block boundary");
  145. }
  146. /* ----------------------------------------------------------------------
  147. * Software implementation of SHA-1.
  148. */
  149. static inline uint32_t rol(uint32_t x, unsigned y)
  150. {
  151. return (x << (31 & y)) | (x >> (31 & -y));
  152. }
  153. static inline uint32_t Ch(uint32_t ctrl, uint32_t if1, uint32_t if0)
  154. {
  155. return if0 ^ (ctrl & (if1 ^ if0));
  156. }
  157. static inline uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
  158. {
  159. return (x & y) | (z & (x | y));
  160. }
  161. static inline uint32_t Par(uint32_t x, uint32_t y, uint32_t z)
  162. {
  163. return (x ^ y ^ z);
  164. }
  165. static inline void sha1_sw_round(
  166. unsigned round_index, const uint32_t *schedule,
  167. uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d, uint32_t *e,
  168. uint32_t f, uint32_t constant)
  169. {
  170. *e = rol(*a, 5) + f + *e + schedule[round_index] + constant;
  171. *b = rol(*b, 30);
  172. }
  173. static void sha1_sw_block(uint32_t *core, const uint8_t *block)
  174. {
  175. uint32_t w[SHA1_ROUNDS];
  176. uint32_t a,b,c,d,e;
  177. for (size_t t = 0; t < 16; t++)
  178. w[t] = GET_32BIT_MSB_FIRST(block + 4*t);
  179. for (size_t t = 16; t < SHA1_ROUNDS; t++)
  180. w[t] = rol(w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16], 1);
  181. a = core[0]; b = core[1]; c = core[2]; d = core[3];
  182. e = core[4];
  183. size_t t = 0;
  184. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  185. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Ch(b,c,d), SHA1_STAGE0_CONSTANT);
  186. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Ch(a,b,c), SHA1_STAGE0_CONSTANT);
  187. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Ch(e,a,b), SHA1_STAGE0_CONSTANT);
  188. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Ch(d,e,a), SHA1_STAGE0_CONSTANT);
  189. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Ch(c,d,e), SHA1_STAGE0_CONSTANT);
  190. }
  191. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  192. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Par(b,c,d), SHA1_STAGE1_CONSTANT);
  193. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Par(a,b,c), SHA1_STAGE1_CONSTANT);
  194. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Par(e,a,b), SHA1_STAGE1_CONSTANT);
  195. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Par(d,e,a), SHA1_STAGE1_CONSTANT);
  196. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Par(c,d,e), SHA1_STAGE1_CONSTANT);
  197. }
  198. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  199. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Maj(b,c,d), SHA1_STAGE2_CONSTANT);
  200. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Maj(a,b,c), SHA1_STAGE2_CONSTANT);
  201. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Maj(e,a,b), SHA1_STAGE2_CONSTANT);
  202. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Maj(d,e,a), SHA1_STAGE2_CONSTANT);
  203. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Maj(c,d,e), SHA1_STAGE2_CONSTANT);
  204. }
  205. for (size_t u = 0; u < SHA1_ROUNDS_PER_STAGE/5; u++) {
  206. sha1_sw_round(t++,w, &a,&b,&c,&d,&e, Par(b,c,d), SHA1_STAGE3_CONSTANT);
  207. sha1_sw_round(t++,w, &e,&a,&b,&c,&d, Par(a,b,c), SHA1_STAGE3_CONSTANT);
  208. sha1_sw_round(t++,w, &d,&e,&a,&b,&c, Par(e,a,b), SHA1_STAGE3_CONSTANT);
  209. sha1_sw_round(t++,w, &c,&d,&e,&a,&b, Par(d,e,a), SHA1_STAGE3_CONSTANT);
  210. sha1_sw_round(t++,w, &b,&c,&d,&e,&a, Par(c,d,e), SHA1_STAGE3_CONSTANT);
  211. }
  212. core[0] += a; core[1] += b; core[2] += c; core[3] += d; core[4] += e;
  213. smemclr(w, sizeof(w));
  214. }
  215. typedef struct sha1_sw {
  216. uint32_t core[5];
  217. sha1_block blk;
  218. BinarySink_IMPLEMENTATION;
  219. ssh_hash hash;
  220. } sha1_sw;
  221. static void sha1_sw_write(BinarySink *bs, const void *vp, size_t len);
  222. static ssh_hash *sha1_sw_new(const ssh_hashalg *alg)
  223. {
  224. sha1_sw *s = snew(sha1_sw);
  225. s->hash.vt = alg;
  226. BinarySink_INIT(s, sha1_sw_write);
  227. BinarySink_DELEGATE_INIT(&s->hash, s);
  228. return &s->hash;
  229. }
  230. static void sha1_sw_reset(ssh_hash *hash)
  231. {
  232. sha1_sw *s = container_of(hash, sha1_sw, hash);
  233. memcpy(s->core, sha1_initial_state, sizeof(s->core));
  234. sha1_block_setup(&s->blk);
  235. }
  236. static void sha1_sw_copyfrom(ssh_hash *hcopy, ssh_hash *horig)
  237. {
  238. sha1_sw *copy = container_of(hcopy, sha1_sw, hash);
  239. sha1_sw *orig = container_of(horig, sha1_sw, hash);
  240. memcpy(copy, orig, sizeof(*copy));
  241. BinarySink_COPIED(copy);
  242. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  243. }
  244. static void sha1_sw_free(ssh_hash *hash)
  245. {
  246. sha1_sw *s = container_of(hash, sha1_sw, hash);
  247. smemclr(s, sizeof(*s));
  248. sfree(s);
  249. }
  250. static void sha1_sw_write(BinarySink *bs, const void *vp, size_t len)
  251. {
  252. sha1_sw *s = BinarySink_DOWNCAST(bs, sha1_sw);
  253. while (len > 0)
  254. if (sha1_block_write(&s->blk, &vp, &len))
  255. sha1_sw_block(s->core, s->blk.block);
  256. }
  257. static void sha1_sw_digest(ssh_hash *hash, uint8_t *digest)
  258. {
  259. sha1_sw *s = container_of(hash, sha1_sw, hash);
  260. sha1_block_pad(&s->blk, BinarySink_UPCAST(s));
  261. for (size_t i = 0; i < 5; i++)
  262. PUT_32BIT_MSB_FIRST(digest + 4*i, s->core[i]);
  263. }
  264. const ssh_hashalg ssh_sha1_sw = {
  265. .new = sha1_sw_new,
  266. .reset = sha1_sw_reset,
  267. .copyfrom = sha1_sw_copyfrom,
  268. .digest = sha1_sw_digest,
  269. .free = sha1_sw_free,
  270. .hlen = 20,
  271. .blocklen = 64,
  272. HASHALG_NAMES_ANNOTATED("SHA-1", "unaccelerated"),
  273. };
  274. /* ----------------------------------------------------------------------
  275. * Hardware-accelerated implementation of SHA-1 using x86 SHA-NI.
  276. */
  277. #if HW_SHA1 == HW_SHA1_NI
  278. /*
  279. * Set target architecture for Clang and GCC
  280. */
  281. #if defined(__clang__) || defined(__GNUC__)
  282. # define FUNC_ISA __attribute__ ((target("sse4.1,sha")))
  283. #if !defined(__clang__)
  284. # pragma GCC target("sha")
  285. # pragma GCC target("sse4.1")
  286. #endif
  287. #else
  288. # define FUNC_ISA
  289. #endif
  290. #include <wmmintrin.h>
  291. #include <smmintrin.h>
  292. #include <immintrin.h>
  293. #if defined(__clang__) || defined(__GNUC__)
  294. #include <shaintrin.h>
  295. #endif
  296. #if defined(__clang__) || defined(__GNUC__)
  297. #include <cpuid.h>
  298. #define GET_CPU_ID_0(out) \
  299. __cpuid(0, (out)[0], (out)[1], (out)[2], (out)[3])
  300. #define GET_CPU_ID_7(out) \
  301. __cpuid_count(7, 0, (out)[0], (out)[1], (out)[2], (out)[3])
  302. #else
  303. #define GET_CPU_ID_0(out) __cpuid(out, 0)
  304. #define GET_CPU_ID_7(out) __cpuidex(out, 7, 0)
  305. #endif
  306. static bool sha1_hw_available(void)
  307. {
  308. unsigned int CPUInfo[4];
  309. GET_CPU_ID_0(CPUInfo);
  310. if (CPUInfo[0] < 7)
  311. return false;
  312. GET_CPU_ID_7(CPUInfo);
  313. return CPUInfo[1] & (1 << 29); /* Check SHA */
  314. }
  315. /* SHA1 implementation using new instructions
  316. The code is based on Jeffrey Walton's SHA1 implementation:
  317. https://github.com/noloader/SHA-Intrinsics
  318. */
  319. FUNC_ISA
  320. static inline void sha1_ni_block(__m128i *core, const uint8_t *p)
  321. {
  322. __m128i ABCD, E0, E1, MSG0, MSG1, MSG2, MSG3;
  323. const __m128i MASK = _mm_set_epi64x(
  324. 0x0001020304050607ULL, 0x08090a0b0c0d0e0fULL);
  325. const __m128i *block = (const __m128i *)p;
  326. /* Load initial values */
  327. ABCD = core[0];
  328. E0 = core[1];
  329. /* Rounds 0-3 */
  330. MSG0 = _mm_loadu_si128(block);
  331. MSG0 = _mm_shuffle_epi8(MSG0, MASK);
  332. E0 = _mm_add_epi32(E0, MSG0);
  333. E1 = ABCD;
  334. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  335. /* Rounds 4-7 */
  336. MSG1 = _mm_loadu_si128(block + 1);
  337. MSG1 = _mm_shuffle_epi8(MSG1, MASK);
  338. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  339. E0 = ABCD;
  340. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 0);
  341. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  342. /* Rounds 8-11 */
  343. MSG2 = _mm_loadu_si128(block + 2);
  344. MSG2 = _mm_shuffle_epi8(MSG2, MASK);
  345. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  346. E1 = ABCD;
  347. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  348. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  349. MSG0 = _mm_xor_si128(MSG0, MSG2);
  350. /* Rounds 12-15 */
  351. MSG3 = _mm_loadu_si128(block + 3);
  352. MSG3 = _mm_shuffle_epi8(MSG3, MASK);
  353. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  354. E0 = ABCD;
  355. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  356. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 0);
  357. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  358. MSG1 = _mm_xor_si128(MSG1, MSG3);
  359. /* Rounds 16-19 */
  360. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  361. E1 = ABCD;
  362. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  363. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  364. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  365. MSG2 = _mm_xor_si128(MSG2, MSG0);
  366. /* Rounds 20-23 */
  367. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  368. E0 = ABCD;
  369. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  370. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  371. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  372. MSG3 = _mm_xor_si128(MSG3, MSG1);
  373. /* Rounds 24-27 */
  374. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  375. E1 = ABCD;
  376. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  377. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 1);
  378. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  379. MSG0 = _mm_xor_si128(MSG0, MSG2);
  380. /* Rounds 28-31 */
  381. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  382. E0 = ABCD;
  383. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  384. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  385. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  386. MSG1 = _mm_xor_si128(MSG1, MSG3);
  387. /* Rounds 32-35 */
  388. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  389. E1 = ABCD;
  390. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  391. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 1);
  392. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  393. MSG2 = _mm_xor_si128(MSG2, MSG0);
  394. /* Rounds 36-39 */
  395. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  396. E0 = ABCD;
  397. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  398. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  399. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  400. MSG3 = _mm_xor_si128(MSG3, MSG1);
  401. /* Rounds 40-43 */
  402. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  403. E1 = ABCD;
  404. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  405. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  406. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  407. MSG0 = _mm_xor_si128(MSG0, MSG2);
  408. /* Rounds 44-47 */
  409. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  410. E0 = ABCD;
  411. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  412. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 2);
  413. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  414. MSG1 = _mm_xor_si128(MSG1, MSG3);
  415. /* Rounds 48-51 */
  416. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  417. E1 = ABCD;
  418. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  419. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  420. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  421. MSG2 = _mm_xor_si128(MSG2, MSG0);
  422. /* Rounds 52-55 */
  423. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  424. E0 = ABCD;
  425. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  426. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 2);
  427. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  428. MSG3 = _mm_xor_si128(MSG3, MSG1);
  429. /* Rounds 56-59 */
  430. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  431. E1 = ABCD;
  432. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  433. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  434. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  435. MSG0 = _mm_xor_si128(MSG0, MSG2);
  436. /* Rounds 60-63 */
  437. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  438. E0 = ABCD;
  439. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  440. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  441. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  442. MSG1 = _mm_xor_si128(MSG1, MSG3);
  443. /* Rounds 64-67 */
  444. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  445. E1 = ABCD;
  446. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  447. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 3);
  448. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  449. MSG2 = _mm_xor_si128(MSG2, MSG0);
  450. /* Rounds 68-71 */
  451. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  452. E0 = ABCD;
  453. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  454. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  455. MSG3 = _mm_xor_si128(MSG3, MSG1);
  456. /* Rounds 72-75 */
  457. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  458. E1 = ABCD;
  459. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  460. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 3);
  461. /* Rounds 76-79 */
  462. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  463. E0 = ABCD;
  464. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  465. /* Combine state */
  466. core[0] = _mm_add_epi32(ABCD, core[0]);
  467. core[1] = _mm_sha1nexte_epu32(E0, core[1]);
  468. }
  469. typedef struct sha1_ni {
  470. /*
  471. * core[0] stores the first four words of the SHA-1 state. core[1]
  472. * stores just the fifth word, in the vector lane at the highest
  473. * address.
  474. */
  475. __m128i core[2];
  476. sha1_block blk;
  477. void *pointer_to_free;
  478. BinarySink_IMPLEMENTATION;
  479. ssh_hash hash;
  480. } sha1_ni;
  481. static void sha1_ni_write(BinarySink *bs, const void *vp, size_t len);
  482. static sha1_ni *sha1_ni_alloc(void)
  483. {
  484. /*
  485. * The __m128i variables in the context structure need to be
  486. * 16-byte aligned, but not all malloc implementations that this
  487. * code has to work with will guarantee to return a 16-byte
  488. * aligned pointer. So we over-allocate, manually realign the
  489. * pointer ourselves, and store the original one inside the
  490. * context so we know how to free it later.
  491. */
  492. void *allocation = smalloc(sizeof(sha1_ni) + 15);
  493. uintptr_t alloc_address = (uintptr_t)allocation;
  494. uintptr_t aligned_address = (alloc_address + 15) & ~15;
  495. sha1_ni *s = (sha1_ni *)aligned_address;
  496. s->pointer_to_free = allocation;
  497. return s;
  498. }
  499. static ssh_hash *sha1_ni_new(const ssh_hashalg *alg)
  500. {
  501. if (!sha1_hw_available_cached())
  502. return NULL;
  503. sha1_ni *s = sha1_ni_alloc();
  504. s->hash.vt = alg;
  505. BinarySink_INIT(s, sha1_ni_write);
  506. BinarySink_DELEGATE_INIT(&s->hash, s);
  507. return &s->hash;
  508. }
  509. FUNC_ISA static void sha1_ni_reset(ssh_hash *hash)
  510. {
  511. sha1_ni *s = container_of(hash, sha1_ni, hash);
  512. /* Initialise the core vectors in their storage order */
  513. s->core[0] = _mm_set_epi64x(
  514. 0x67452301efcdab89ULL, 0x98badcfe10325476ULL);
  515. s->core[1] = _mm_set_epi32(0xc3d2e1f0, 0, 0, 0);
  516. sha1_block_setup(&s->blk);
  517. }
  518. static void sha1_ni_copyfrom(ssh_hash *hcopy, ssh_hash *horig)
  519. {
  520. sha1_ni *copy = container_of(hcopy, sha1_ni, hash);
  521. sha1_ni *orig = container_of(horig, sha1_ni, hash);
  522. void *ptf_save = copy->pointer_to_free;
  523. *copy = *orig; /* structure copy */
  524. copy->pointer_to_free = ptf_save;
  525. BinarySink_COPIED(copy);
  526. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  527. }
  528. static void sha1_ni_free(ssh_hash *hash)
  529. {
  530. sha1_ni *s = container_of(hash, sha1_ni, hash);
  531. void *ptf = s->pointer_to_free;
  532. smemclr(s, sizeof(*s));
  533. sfree(ptf);
  534. }
  535. static void sha1_ni_write(BinarySink *bs, const void *vp, size_t len)
  536. {
  537. sha1_ni *s = BinarySink_DOWNCAST(bs, sha1_ni);
  538. while (len > 0)
  539. if (sha1_block_write(&s->blk, &vp, &len))
  540. sha1_ni_block(s->core, s->blk.block);
  541. }
  542. FUNC_ISA static void sha1_ni_digest(ssh_hash *hash, uint8_t *digest)
  543. {
  544. sha1_ni *s = container_of(hash, sha1_ni, hash);
  545. sha1_block_pad(&s->blk, BinarySink_UPCAST(s));
  546. /* Rearrange the first vector into its output order */
  547. __m128i abcd = _mm_shuffle_epi32(s->core[0], 0x1B);
  548. /* Byte-swap it into the output endianness */
  549. const __m128i mask = _mm_setr_epi8(3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12);
  550. abcd = _mm_shuffle_epi8(abcd, mask);
  551. /* And store it */
  552. _mm_storeu_si128((__m128i *)digest, abcd);
  553. /* Finally, store the leftover word */
  554. uint32_t e = _mm_extract_epi32(s->core[1], 3);
  555. PUT_32BIT_MSB_FIRST(digest + 16, e);
  556. }
  557. const ssh_hashalg ssh_sha1_hw = {
  558. .new = sha1_ni_new,
  559. .reset = sha1_ni_reset,
  560. .copyfrom = sha1_ni_copyfrom,
  561. .digest = sha1_ni_digest,
  562. .free = sha1_ni_free,
  563. .hlen = 20,
  564. .blocklen = 64,
  565. HASHALG_NAMES_ANNOTATED("SHA-1", "SHA-NI accelerated"),
  566. };
  567. /* ----------------------------------------------------------------------
  568. * Hardware-accelerated implementation of SHA-1 using Arm NEON.
  569. */
  570. #elif HW_SHA1 == HW_SHA1_NEON
  571. /*
  572. * Manually set the target architecture, if we decided above that we
  573. * need to.
  574. */
  575. #ifdef USE_CLANG_ATTR_TARGET_AARCH64
  576. /*
  577. * A spot of cheating: redefine some ACLE feature macros before
  578. * including arm_neon.h. Otherwise we won't get the SHA intrinsics
  579. * defined by that header, because it will be looking at the settings
  580. * for the whole translation unit rather than the ones we're going to
  581. * put on some particular functions using __attribute__((target)).
  582. */
  583. #define __ARM_NEON 1
  584. #define __ARM_FEATURE_CRYPTO 1
  585. #define __ARM_FEATURE_SHA2 1
  586. #define FUNC_ISA __attribute__ ((target("neon,crypto")))
  587. #endif /* USE_CLANG_ATTR_TARGET_AARCH64 */
  588. #ifndef FUNC_ISA
  589. #define FUNC_ISA
  590. #endif
  591. #ifdef USE_ARM64_NEON_H
  592. #include <arm64_neon.h>
  593. #else
  594. #include <arm_neon.h>
  595. #endif
  596. static bool sha1_hw_available(void)
  597. {
  598. /*
  599. * For Arm, we delegate to a per-platform detection function (see
  600. * explanation in sshaes.c).
  601. */
  602. return platform_sha1_hw_available();
  603. }
  604. typedef struct sha1_neon_core sha1_neon_core;
  605. struct sha1_neon_core {
  606. uint32x4_t abcd;
  607. uint32_t e;
  608. };
  609. FUNC_ISA
  610. static inline uint32x4_t sha1_neon_load_input(const uint8_t *p)
  611. {
  612. return vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(p)));
  613. }
  614. FUNC_ISA
  615. static inline uint32x4_t sha1_neon_schedule_update(
  616. uint32x4_t m4, uint32x4_t m3, uint32x4_t m2, uint32x4_t m1)
  617. {
  618. return vsha1su1q_u32(vsha1su0q_u32(m4, m3, m2), m1);
  619. }
  620. /*
  621. * SHA-1 has three different kinds of round, differing in whether they
  622. * use the Ch, Maj or Par functions defined above. Each one uses a
  623. * separate NEON instruction, so we define three inline functions for
  624. * the different round types using this macro.
  625. *
  626. * The two batches of Par-type rounds also use a different constant,
  627. * but that's passed in as an operand, so we don't need a fourth
  628. * inline function just for that.
  629. */
  630. #define SHA1_NEON_ROUND_FN(type) \
  631. FUNC_ISA static inline sha1_neon_core sha1_neon_round4_##type( \
  632. sha1_neon_core old, uint32x4_t sched, uint32x4_t constant) \
  633. { \
  634. sha1_neon_core new; \
  635. uint32x4_t round_input = vaddq_u32(sched, constant); \
  636. new.abcd = vsha1##type##q_u32(old.abcd, old.e, round_input); \
  637. new.e = vsha1h_u32(vget_lane_u32(vget_low_u32(old.abcd), 0)); \
  638. return new; \
  639. }
  640. SHA1_NEON_ROUND_FN(c)
  641. SHA1_NEON_ROUND_FN(p)
  642. SHA1_NEON_ROUND_FN(m)
  643. FUNC_ISA
  644. static inline void sha1_neon_block(sha1_neon_core *core, const uint8_t *p)
  645. {
  646. uint32x4_t constant, s0, s1, s2, s3;
  647. sha1_neon_core cr = *core;
  648. constant = vdupq_n_u32(SHA1_STAGE0_CONSTANT);
  649. s0 = sha1_neon_load_input(p);
  650. cr = sha1_neon_round4_c(cr, s0, constant);
  651. s1 = sha1_neon_load_input(p + 16);
  652. cr = sha1_neon_round4_c(cr, s1, constant);
  653. s2 = sha1_neon_load_input(p + 32);
  654. cr = sha1_neon_round4_c(cr, s2, constant);
  655. s3 = sha1_neon_load_input(p + 48);
  656. cr = sha1_neon_round4_c(cr, s3, constant);
  657. s0 = sha1_neon_schedule_update(s0, s1, s2, s3);
  658. cr = sha1_neon_round4_c(cr, s0, constant);
  659. constant = vdupq_n_u32(SHA1_STAGE1_CONSTANT);
  660. s1 = sha1_neon_schedule_update(s1, s2, s3, s0);
  661. cr = sha1_neon_round4_p(cr, s1, constant);
  662. s2 = sha1_neon_schedule_update(s2, s3, s0, s1);
  663. cr = sha1_neon_round4_p(cr, s2, constant);
  664. s3 = sha1_neon_schedule_update(s3, s0, s1, s2);
  665. cr = sha1_neon_round4_p(cr, s3, constant);
  666. s0 = sha1_neon_schedule_update(s0, s1, s2, s3);
  667. cr = sha1_neon_round4_p(cr, s0, constant);
  668. s1 = sha1_neon_schedule_update(s1, s2, s3, s0);
  669. cr = sha1_neon_round4_p(cr, s1, constant);
  670. constant = vdupq_n_u32(SHA1_STAGE2_CONSTANT);
  671. s2 = sha1_neon_schedule_update(s2, s3, s0, s1);
  672. cr = sha1_neon_round4_m(cr, s2, constant);
  673. s3 = sha1_neon_schedule_update(s3, s0, s1, s2);
  674. cr = sha1_neon_round4_m(cr, s3, constant);
  675. s0 = sha1_neon_schedule_update(s0, s1, s2, s3);
  676. cr = sha1_neon_round4_m(cr, s0, constant);
  677. s1 = sha1_neon_schedule_update(s1, s2, s3, s0);
  678. cr = sha1_neon_round4_m(cr, s1, constant);
  679. s2 = sha1_neon_schedule_update(s2, s3, s0, s1);
  680. cr = sha1_neon_round4_m(cr, s2, constant);
  681. constant = vdupq_n_u32(SHA1_STAGE3_CONSTANT);
  682. s3 = sha1_neon_schedule_update(s3, s0, s1, s2);
  683. cr = sha1_neon_round4_p(cr, s3, constant);
  684. s0 = sha1_neon_schedule_update(s0, s1, s2, s3);
  685. cr = sha1_neon_round4_p(cr, s0, constant);
  686. s1 = sha1_neon_schedule_update(s1, s2, s3, s0);
  687. cr = sha1_neon_round4_p(cr, s1, constant);
  688. s2 = sha1_neon_schedule_update(s2, s3, s0, s1);
  689. cr = sha1_neon_round4_p(cr, s2, constant);
  690. s3 = sha1_neon_schedule_update(s3, s0, s1, s2);
  691. cr = sha1_neon_round4_p(cr, s3, constant);
  692. core->abcd = vaddq_u32(core->abcd, cr.abcd);
  693. core->e += cr.e;
  694. }
  695. typedef struct sha1_neon {
  696. sha1_neon_core core;
  697. sha1_block blk;
  698. BinarySink_IMPLEMENTATION;
  699. ssh_hash hash;
  700. } sha1_neon;
  701. static void sha1_neon_write(BinarySink *bs, const void *vp, size_t len);
  702. static ssh_hash *sha1_neon_new(const ssh_hashalg *alg)
  703. {
  704. if (!sha1_hw_available_cached())
  705. return NULL;
  706. sha1_neon *s = snew(sha1_neon);
  707. s->hash.vt = alg;
  708. BinarySink_INIT(s, sha1_neon_write);
  709. BinarySink_DELEGATE_INIT(&s->hash, s);
  710. return &s->hash;
  711. }
  712. static void sha1_neon_reset(ssh_hash *hash)
  713. {
  714. sha1_neon *s = container_of(hash, sha1_neon, hash);
  715. s->core.abcd = vld1q_u32(sha1_initial_state);
  716. s->core.e = sha1_initial_state[4];
  717. sha1_block_setup(&s->blk);
  718. }
  719. static void sha1_neon_copyfrom(ssh_hash *hcopy, ssh_hash *horig)
  720. {
  721. sha1_neon *copy = container_of(hcopy, sha1_neon, hash);
  722. sha1_neon *orig = container_of(horig, sha1_neon, hash);
  723. *copy = *orig; /* structure copy */
  724. BinarySink_COPIED(copy);
  725. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  726. }
  727. static void sha1_neon_free(ssh_hash *hash)
  728. {
  729. sha1_neon *s = container_of(hash, sha1_neon, hash);
  730. smemclr(s, sizeof(*s));
  731. sfree(s);
  732. }
  733. static void sha1_neon_write(BinarySink *bs, const void *vp, size_t len)
  734. {
  735. sha1_neon *s = BinarySink_DOWNCAST(bs, sha1_neon);
  736. while (len > 0)
  737. if (sha1_block_write(&s->blk, &vp, &len))
  738. sha1_neon_block(&s->core, s->blk.block);
  739. }
  740. static void sha1_neon_digest(ssh_hash *hash, uint8_t *digest)
  741. {
  742. sha1_neon *s = container_of(hash, sha1_neon, hash);
  743. sha1_block_pad(&s->blk, BinarySink_UPCAST(s));
  744. vst1q_u8(digest, vrev32q_u8(vreinterpretq_u8_u32(s->core.abcd)));
  745. PUT_32BIT_MSB_FIRST(digest + 16, s->core.e);
  746. }
  747. const ssh_hashalg ssh_sha1_hw = {
  748. .new = sha1_neon_new,
  749. .reset = sha1_neon_reset,
  750. .copyfrom = sha1_neon_copyfrom,
  751. .digest = sha1_neon_digest,
  752. .free = sha1_neon_free,
  753. .hlen = 20,
  754. .blocklen = 64,
  755. HASHALG_NAMES_ANNOTATED("SHA-1", "NEON accelerated"),
  756. };
  757. /* ----------------------------------------------------------------------
  758. * Stub functions if we have no hardware-accelerated SHA-1. In this
  759. * case, sha1_hw_new returns NULL (though it should also never be
  760. * selected by sha1_select, so the only thing that should even be
  761. * _able_ to call it is testcrypt). As a result, the remaining vtable
  762. * functions should never be called at all.
  763. */
  764. #elif HW_SHA1 == HW_SHA1_NONE
  765. static bool sha1_hw_available(void)
  766. {
  767. return false;
  768. }
  769. static ssh_hash *sha1_stub_new(const ssh_hashalg *alg)
  770. {
  771. return NULL;
  772. }
  773. #define STUB_BODY { unreachable("Should never be called"); }
  774. static void sha1_stub_reset(ssh_hash *hash) STUB_BODY
  775. static void sha1_stub_copyfrom(ssh_hash *hash, ssh_hash *orig) STUB_BODY
  776. static void sha1_stub_free(ssh_hash *hash) STUB_BODY
  777. static void sha1_stub_digest(ssh_hash *hash, uint8_t *digest) STUB_BODY
  778. const ssh_hashalg ssh_sha1_hw = {
  779. .new = sha1_stub_new,
  780. .reset = sha1_stub_reset,
  781. .copyfrom = sha1_stub_copyfrom,
  782. .digest = sha1_stub_digest,
  783. .free = sha1_stub_free,
  784. .hlen = 20,
  785. .blocklen = 64,
  786. HASHALG_NAMES_ANNOTATED("SHA-1", "!NONEXISTENT ACCELERATED VERSION!"),
  787. };
  788. #endif /* HW_SHA1 */