sshsh256.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * SHA-256 algorithm as described at
  3. *
  4. * http://csrc.nist.gov/cryptval/shs.html
  5. */
  6. #include "ssh.h"
  7. #include <assert.h>
  8. /*
  9. * Start by deciding whether we can support hardware SHA at all.
  10. */
  11. #define HW_SHA256_NONE 0
  12. #define HW_SHA256_NI 1
  13. #define HW_SHA256_NEON 2
  14. #ifdef _FORCE_SHA_NI
  15. # define HW_SHA256 HW_SHA256_NI
  16. #elif defined(__clang__)
  17. # if __has_attribute(target) && __has_include(<wmmintrin.h>) && \
  18. (defined(__x86_64__) || defined(__i386))
  19. # define HW_SHA256 HW_SHA256_NI
  20. # endif
  21. #elif defined(__GNUC__)
  22. # if (__GNUC__ >= 5) && (defined(__x86_64__) || defined(__i386))
  23. # define HW_SHA256 HW_SHA256_NI
  24. # endif
  25. #elif defined (_MSC_VER)
  26. # if (defined(_M_X64) || defined(_M_IX86)) && _MSC_FULL_VER >= 150030729
  27. # define HW_SHA256 HW_SHA256_NI
  28. # endif
  29. #endif
  30. #ifdef _FORCE_SHA_NEON
  31. # define HW_SHA256 HW_SHA256_NEON
  32. #elif defined __BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  33. /* Arm can potentially support both endiannesses, but this code
  34. * hasn't been tested on anything but little. If anyone wants to
  35. * run big-endian, they'll need to fix it first. */
  36. #elif defined __ARM_FEATURE_CRYPTO
  37. /* If the Arm crypto extension is available already, we can
  38. * support NEON SHA without having to enable anything by hand */
  39. # define HW_SHA256 HW_SHA256_NEON
  40. #elif defined(__clang__)
  41. # if __has_attribute(target) && __has_include(<arm_neon.h>) && \
  42. (defined(__aarch64__))
  43. /* clang can enable the crypto extension in AArch64 using
  44. * __attribute__((target)) */
  45. # define HW_SHA256 HW_SHA256_NEON
  46. # define USE_CLANG_ATTR_TARGET_AARCH64
  47. # endif
  48. #elif defined _MSC_VER
  49. /* Visual Studio supports the crypto extension when targeting
  50. * AArch64, but as of VS2017, the AArch32 header doesn't quite
  51. * manage it (declaring the shae/shad intrinsics without a round
  52. * key operand). */
  53. # if defined _M_ARM64
  54. # define HW_SHA256 HW_SHA256_NEON
  55. # if defined _M_ARM64
  56. # define USE_ARM64_NEON_H /* unusual header name in this case */
  57. # endif
  58. # endif
  59. #endif
  60. #if defined _FORCE_SOFTWARE_SHA || !defined HW_SHA256
  61. # undef HW_SHA256
  62. # define HW_SHA256 HW_SHA256_NONE
  63. #endif
  64. /*
  65. * The actual query function that asks if hardware acceleration is
  66. * available.
  67. */
  68. static bool sha256_hw_available(void);
  69. /*
  70. * The top-level selection function, caching the results of
  71. * sha256_hw_available() so it only has to run once.
  72. */
  73. static bool sha256_hw_available_cached(void)
  74. {
  75. static bool initialised = false;
  76. static bool hw_available;
  77. if (!initialised) {
  78. hw_available = sha256_hw_available();
  79. initialised = true;
  80. }
  81. return hw_available;
  82. }
  83. static ssh_hash *sha256_select(const ssh_hashalg *alg)
  84. {
  85. const ssh_hashalg *real_alg =
  86. sha256_hw_available_cached() ? &ssh_sha256_hw : &ssh_sha256_sw;
  87. return ssh_hash_new(real_alg);
  88. }
  89. const ssh_hashalg ssh_sha256 = {
  90. sha256_select, NULL, NULL, NULL,
  91. 32, 64, HASHALG_NAMES_ANNOTATED("SHA-256", "dummy selector vtable"),
  92. };
  93. /* ----------------------------------------------------------------------
  94. * Definitions likely to be helpful to multiple implementations.
  95. */
  96. static const uint32_t sha256_initial_state[] = {
  97. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  98. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
  99. };
  100. static const uint32_t sha256_round_constants[] = {
  101. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  102. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  103. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  104. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  105. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  106. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  107. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  108. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  109. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  110. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  111. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  112. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  113. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  114. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  115. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  116. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  117. };
  118. #define SHA256_ROUNDS 64
  119. typedef struct sha256_block sha256_block;
  120. struct sha256_block {
  121. uint8_t block[64];
  122. size_t used;
  123. uint64_t len;
  124. };
  125. static inline void sha256_block_setup(sha256_block *blk)
  126. {
  127. blk->used = 0;
  128. blk->len = 0;
  129. }
  130. static inline bool sha256_block_write(
  131. sha256_block *blk, const void **vdata, size_t *len)
  132. {
  133. size_t blkleft = sizeof(blk->block) - blk->used;
  134. size_t chunk = *len < blkleft ? *len : blkleft;
  135. const uint8_t *p = *vdata;
  136. memcpy(blk->block + blk->used, p, chunk);
  137. *vdata = p + chunk;
  138. *len -= chunk;
  139. blk->used += chunk;
  140. blk->len += chunk;
  141. if (blk->used == sizeof(blk->block)) {
  142. blk->used = 0;
  143. return true;
  144. }
  145. return false;
  146. }
  147. static inline void sha256_block_pad(sha256_block *blk, BinarySink *bs)
  148. {
  149. uint64_t final_len = blk->len << 3;
  150. size_t pad = 1 + (63 & (55 - blk->used));
  151. put_byte(bs, 0x80);
  152. for (size_t i = 1; i < pad; i++)
  153. put_byte(bs, 0);
  154. put_uint64(bs, final_len);
  155. assert(blk->used == 0 && "Should have exactly hit a block boundary");
  156. }
  157. /* ----------------------------------------------------------------------
  158. * Software implementation of SHA-256.
  159. */
  160. static inline uint32_t ror(uint32_t x, unsigned y)
  161. {
  162. return (x << (31 & -y)) | (x >> (31 & y));
  163. }
  164. static inline uint32_t Ch(uint32_t ctrl, uint32_t if1, uint32_t if0)
  165. {
  166. return if0 ^ (ctrl & (if1 ^ if0));
  167. }
  168. static inline uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
  169. {
  170. return (x & y) | (z & (x | y));
  171. }
  172. static inline uint32_t Sigma_0(uint32_t x)
  173. {
  174. return ror(x,2) ^ ror(x,13) ^ ror(x,22);
  175. }
  176. static inline uint32_t Sigma_1(uint32_t x)
  177. {
  178. return ror(x,6) ^ ror(x,11) ^ ror(x,25);
  179. }
  180. static inline uint32_t sigma_0(uint32_t x)
  181. {
  182. return ror(x,7) ^ ror(x,18) ^ (x >> 3);
  183. }
  184. static inline uint32_t sigma_1(uint32_t x)
  185. {
  186. return ror(x,17) ^ ror(x,19) ^ (x >> 10);
  187. }
  188. static inline void sha256_sw_round(
  189. unsigned round_index, const uint32_t *schedule,
  190. uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d,
  191. uint32_t *e, uint32_t *f, uint32_t *g, uint32_t *h)
  192. {
  193. uint32_t t1 = *h + Sigma_1(*e) + Ch(*e,*f,*g) +
  194. sha256_round_constants[round_index] + schedule[round_index];
  195. uint32_t t2 = Sigma_0(*a) + Maj(*a,*b,*c);
  196. *d += t1;
  197. *h = t1 + t2;
  198. }
  199. static void sha256_sw_block(uint32_t *core, const uint8_t *block)
  200. {
  201. uint32_t w[SHA256_ROUNDS];
  202. uint32_t a,b,c,d,e,f,g,h;
  203. for (size_t t = 0; t < 16; t++)
  204. w[t] = GET_32BIT_MSB_FIRST(block + 4*t);
  205. for (size_t t = 16; t < SHA256_ROUNDS; t++)
  206. w[t] = sigma_1(w[t-2]) + w[t-7] + sigma_0(w[t-15]) + w[t-16];
  207. a = core[0]; b = core[1]; c = core[2]; d = core[3];
  208. e = core[4]; f = core[5]; g = core[6]; h = core[7];
  209. for (size_t t = 0; t < SHA256_ROUNDS; t += 8) {
  210. sha256_sw_round(t+0, w, &a,&b,&c,&d,&e,&f,&g,&h);
  211. sha256_sw_round(t+1, w, &h,&a,&b,&c,&d,&e,&f,&g);
  212. sha256_sw_round(t+2, w, &g,&h,&a,&b,&c,&d,&e,&f);
  213. sha256_sw_round(t+3, w, &f,&g,&h,&a,&b,&c,&d,&e);
  214. sha256_sw_round(t+4, w, &e,&f,&g,&h,&a,&b,&c,&d);
  215. sha256_sw_round(t+5, w, &d,&e,&f,&g,&h,&a,&b,&c);
  216. sha256_sw_round(t+6, w, &c,&d,&e,&f,&g,&h,&a,&b);
  217. sha256_sw_round(t+7, w, &b,&c,&d,&e,&f,&g,&h,&a);
  218. }
  219. core[0] += a; core[1] += b; core[2] += c; core[3] += d;
  220. core[4] += e; core[5] += f; core[6] += g; core[7] += h;
  221. smemclr(w, sizeof(w));
  222. }
  223. typedef struct sha256_sw {
  224. uint32_t core[8];
  225. sha256_block blk;
  226. BinarySink_IMPLEMENTATION;
  227. ssh_hash hash;
  228. } sha256_sw;
  229. static void sha256_sw_write(BinarySink *bs, const void *vp, size_t len);
  230. static ssh_hash *sha256_sw_new(const ssh_hashalg *alg)
  231. {
  232. sha256_sw *s = snew(sha256_sw);
  233. memcpy(s->core, sha256_initial_state, sizeof(s->core));
  234. sha256_block_setup(&s->blk);
  235. s->hash.vt = alg;
  236. BinarySink_INIT(s, sha256_sw_write);
  237. BinarySink_DELEGATE_INIT(&s->hash, s);
  238. return &s->hash;
  239. }
  240. static ssh_hash *sha256_sw_copy(ssh_hash *hash)
  241. {
  242. sha256_sw *s = container_of(hash, sha256_sw, hash);
  243. sha256_sw *copy = snew(sha256_sw);
  244. memcpy(copy, s, sizeof(*copy));
  245. BinarySink_COPIED(copy);
  246. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  247. return &copy->hash;
  248. }
  249. static void sha256_sw_free(ssh_hash *hash)
  250. {
  251. sha256_sw *s = container_of(hash, sha256_sw, hash);
  252. smemclr(s, sizeof(*s));
  253. sfree(s);
  254. }
  255. static void sha256_sw_write(BinarySink *bs, const void *vp, size_t len)
  256. {
  257. sha256_sw *s = BinarySink_DOWNCAST(bs, sha256_sw);
  258. while (len > 0)
  259. if (sha256_block_write(&s->blk, &vp, &len))
  260. sha256_sw_block(s->core, s->blk.block);
  261. }
  262. static void sha256_sw_final(ssh_hash *hash, uint8_t *digest)
  263. {
  264. sha256_sw *s = container_of(hash, sha256_sw, hash);
  265. sha256_block_pad(&s->blk, BinarySink_UPCAST(s));
  266. for (size_t i = 0; i < 8; i++)
  267. PUT_32BIT_MSB_FIRST(digest + 4*i, s->core[i]);
  268. sha256_sw_free(hash);
  269. }
  270. const ssh_hashalg ssh_sha256_sw = {
  271. sha256_sw_new, sha256_sw_copy, sha256_sw_final, sha256_sw_free,
  272. 32, 64, HASHALG_NAMES_ANNOTATED("SHA-256", "unaccelerated"),
  273. };
  274. /* ----------------------------------------------------------------------
  275. * Hardware-accelerated implementation of SHA-256 using x86 SHA-NI.
  276. */
  277. #if HW_SHA256 == HW_SHA256_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 sha256_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. /* SHA256 implementation using new instructions
  316. The code is based on Jeffrey Walton's SHA256 implementation:
  317. https://github.com/noloader/SHA-Intrinsics
  318. */
  319. FUNC_ISA
  320. static inline void sha256_ni_block(__m128i *core, const uint8_t *p)
  321. {
  322. __m128i STATE0, STATE1;
  323. __m128i MSG, TMP;
  324. __m128i MSG0, MSG1, MSG2, MSG3;
  325. const __m128i *block = (const __m128i *)p;
  326. const __m128i MASK = _mm_set_epi64x(
  327. 0x0c0d0e0f08090a0bULL, 0x0405060700010203ULL);
  328. /* Load initial values */
  329. STATE0 = core[0];
  330. STATE1 = core[1];
  331. /* Rounds 0-3 */
  332. MSG = _mm_loadu_si128(block);
  333. MSG0 = _mm_shuffle_epi8(MSG, MASK);
  334. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(
  335. 0xE9B5DBA5B5C0FBCFULL, 0x71374491428A2F98ULL));
  336. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  337. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  338. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  339. /* Rounds 4-7 */
  340. MSG1 = _mm_loadu_si128(block + 1);
  341. MSG1 = _mm_shuffle_epi8(MSG1, MASK);
  342. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(
  343. 0xAB1C5ED5923F82A4ULL, 0x59F111F13956C25BULL));
  344. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  345. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  346. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  347. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  348. /* Rounds 8-11 */
  349. MSG2 = _mm_loadu_si128(block + 2);
  350. MSG2 = _mm_shuffle_epi8(MSG2, MASK);
  351. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(
  352. 0x550C7DC3243185BEULL, 0x12835B01D807AA98ULL));
  353. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  354. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  355. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  356. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  357. /* Rounds 12-15 */
  358. MSG3 = _mm_loadu_si128(block + 3);
  359. MSG3 = _mm_shuffle_epi8(MSG3, MASK);
  360. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(
  361. 0xC19BF1749BDC06A7ULL, 0x80DEB1FE72BE5D74ULL));
  362. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  363. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  364. MSG0 = _mm_add_epi32(MSG0, TMP);
  365. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  366. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  367. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  368. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  369. /* Rounds 16-19 */
  370. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(
  371. 0x240CA1CC0FC19DC6ULL, 0xEFBE4786E49B69C1ULL));
  372. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  373. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  374. MSG1 = _mm_add_epi32(MSG1, TMP);
  375. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  376. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  377. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  378. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  379. /* Rounds 20-23 */
  380. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(
  381. 0x76F988DA5CB0A9DCULL, 0x4A7484AA2DE92C6FULL));
  382. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  383. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  384. MSG2 = _mm_add_epi32(MSG2, TMP);
  385. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  386. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  387. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  388. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  389. /* Rounds 24-27 */
  390. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(
  391. 0xBF597FC7B00327C8ULL, 0xA831C66D983E5152ULL));
  392. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  393. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  394. MSG3 = _mm_add_epi32(MSG3, TMP);
  395. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  396. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  397. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  398. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  399. /* Rounds 28-31 */
  400. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(
  401. 0x1429296706CA6351ULL, 0xD5A79147C6E00BF3ULL));
  402. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  403. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  404. MSG0 = _mm_add_epi32(MSG0, TMP);
  405. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  406. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  407. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  408. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  409. /* Rounds 32-35 */
  410. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(
  411. 0x53380D134D2C6DFCULL, 0x2E1B213827B70A85ULL));
  412. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  413. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  414. MSG1 = _mm_add_epi32(MSG1, TMP);
  415. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  416. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  417. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  418. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  419. /* Rounds 36-39 */
  420. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(
  421. 0x92722C8581C2C92EULL, 0x766A0ABB650A7354ULL));
  422. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  423. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  424. MSG2 = _mm_add_epi32(MSG2, TMP);
  425. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  426. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  427. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  428. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  429. /* Rounds 40-43 */
  430. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(
  431. 0xC76C51A3C24B8B70ULL, 0xA81A664BA2BFE8A1ULL));
  432. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  433. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  434. MSG3 = _mm_add_epi32(MSG3, TMP);
  435. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  436. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  437. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  438. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  439. /* Rounds 44-47 */
  440. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(
  441. 0x106AA070F40E3585ULL, 0xD6990624D192E819ULL));
  442. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  443. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  444. MSG0 = _mm_add_epi32(MSG0, TMP);
  445. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  446. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  447. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  448. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  449. /* Rounds 48-51 */
  450. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(
  451. 0x34B0BCB52748774CULL, 0x1E376C0819A4C116ULL));
  452. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  453. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  454. MSG1 = _mm_add_epi32(MSG1, TMP);
  455. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  456. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  457. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  458. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  459. /* Rounds 52-55 */
  460. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(
  461. 0x682E6FF35B9CCA4FULL, 0x4ED8AA4A391C0CB3ULL));
  462. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  463. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  464. MSG2 = _mm_add_epi32(MSG2, TMP);
  465. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  466. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  467. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  468. /* Rounds 56-59 */
  469. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(
  470. 0x8CC7020884C87814ULL, 0x78A5636F748F82EEULL));
  471. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  472. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  473. MSG3 = _mm_add_epi32(MSG3, TMP);
  474. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  475. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  476. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  477. /* Rounds 60-63 */
  478. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(
  479. 0xC67178F2BEF9A3F7ULL, 0xA4506CEB90BEFFFAULL));
  480. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  481. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  482. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  483. /* Combine state */
  484. core[0] = _mm_add_epi32(STATE0, core[0]);
  485. core[1] = _mm_add_epi32(STATE1, core[1]);
  486. }
  487. typedef struct sha256_ni {
  488. /*
  489. * These two vectors store the 8 words of the SHA-256 state, but
  490. * not in the same order they appear in the spec: the first word
  491. * holds A,B,E,F and the second word C,D,G,H.
  492. */
  493. __m128i core[2];
  494. sha256_block blk;
  495. void *pointer_to_free;
  496. BinarySink_IMPLEMENTATION;
  497. ssh_hash hash;
  498. } sha256_ni;
  499. static void sha256_ni_write(BinarySink *bs, const void *vp, size_t len);
  500. static sha256_ni *sha256_ni_alloc(void)
  501. {
  502. /*
  503. * The __m128i variables in the context structure need to be
  504. * 16-byte aligned, but not all malloc implementations that this
  505. * code has to work with will guarantee to return a 16-byte
  506. * aligned pointer. So we over-allocate, manually realign the
  507. * pointer ourselves, and store the original one inside the
  508. * context so we know how to free it later.
  509. */
  510. void *allocation = smalloc(sizeof(sha256_ni) + 15);
  511. uintptr_t alloc_address = (uintptr_t)allocation;
  512. uintptr_t aligned_address = (alloc_address + 15) & ~15;
  513. sha256_ni *s = (sha256_ni *)aligned_address;
  514. s->pointer_to_free = allocation;
  515. return s;
  516. }
  517. FUNC_ISA static ssh_hash *sha256_ni_new(const ssh_hashalg *alg)
  518. {
  519. if (!sha256_hw_available_cached())
  520. return NULL;
  521. sha256_ni *s = sha256_ni_alloc();
  522. /* Initialise the core vectors in their storage order */
  523. s->core[0] = _mm_set_epi64x(
  524. 0x6a09e667bb67ae85ULL, 0x510e527f9b05688cULL);
  525. s->core[1] = _mm_set_epi64x(
  526. 0x3c6ef372a54ff53aULL, 0x1f83d9ab5be0cd19ULL);
  527. sha256_block_setup(&s->blk);
  528. s->hash.vt = alg;
  529. BinarySink_INIT(s, sha256_ni_write);
  530. BinarySink_DELEGATE_INIT(&s->hash, s);
  531. return &s->hash;
  532. }
  533. static ssh_hash *sha256_ni_copy(ssh_hash *hash)
  534. {
  535. sha256_ni *s = container_of(hash, sha256_ni, hash);
  536. sha256_ni *copy = sha256_ni_alloc();
  537. void *ptf_save = copy->pointer_to_free;
  538. *copy = *s; /* structure copy */
  539. copy->pointer_to_free = ptf_save;
  540. BinarySink_COPIED(copy);
  541. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  542. return &copy->hash;
  543. }
  544. static void sha256_ni_free(ssh_hash *hash)
  545. {
  546. sha256_ni *s = container_of(hash, sha256_ni, hash);
  547. void *ptf = s->pointer_to_free;
  548. smemclr(s, sizeof(*s));
  549. sfree(ptf);
  550. }
  551. static void sha256_ni_write(BinarySink *bs, const void *vp, size_t len)
  552. {
  553. sha256_ni *s = BinarySink_DOWNCAST(bs, sha256_ni);
  554. while (len > 0)
  555. if (sha256_block_write(&s->blk, &vp, &len))
  556. sha256_ni_block(s->core, s->blk.block);
  557. }
  558. FUNC_ISA static void sha256_ni_final(ssh_hash *hash, uint8_t *digest)
  559. {
  560. sha256_ni *s = container_of(hash, sha256_ni, hash);
  561. sha256_block_pad(&s->blk, BinarySink_UPCAST(s));
  562. /* Rearrange the words into the output order */
  563. __m128i feba = _mm_shuffle_epi32(s->core[0], 0x1B);
  564. __m128i dchg = _mm_shuffle_epi32(s->core[1], 0xB1);
  565. __m128i dcba = _mm_blend_epi16(feba, dchg, 0xF0);
  566. __m128i hgfe = _mm_alignr_epi8(dchg, feba, 8);
  567. /* Byte-swap them into the output endianness */
  568. const __m128i mask = _mm_setr_epi8(3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12);
  569. dcba = _mm_shuffle_epi8(dcba, mask);
  570. hgfe = _mm_shuffle_epi8(hgfe, mask);
  571. /* And store them */
  572. __m128i *output = (__m128i *)digest;
  573. _mm_storeu_si128(output, dcba);
  574. _mm_storeu_si128(output+1, hgfe);
  575. sha256_ni_free(hash);
  576. }
  577. const ssh_hashalg ssh_sha256_hw = {
  578. sha256_ni_new, sha256_ni_copy, sha256_ni_final, sha256_ni_free,
  579. 32, 64, HASHALG_NAMES_ANNOTATED("SHA-256", "SHA-NI accelerated"),
  580. };
  581. /* ----------------------------------------------------------------------
  582. * Hardware-accelerated implementation of SHA-256 using Arm NEON.
  583. */
  584. #elif HW_SHA256 == HW_SHA256_NEON
  585. /*
  586. * Manually set the target architecture, if we decided above that we
  587. * need to.
  588. */
  589. #ifdef USE_CLANG_ATTR_TARGET_AARCH64
  590. /*
  591. * A spot of cheating: redefine some ACLE feature macros before
  592. * including arm_neon.h. Otherwise we won't get the SHA intrinsics
  593. * defined by that header, because it will be looking at the settings
  594. * for the whole translation unit rather than the ones we're going to
  595. * put on some particular functions using __attribute__((target)).
  596. */
  597. #define __ARM_NEON 1
  598. #define __ARM_FEATURE_CRYPTO 1
  599. #define FUNC_ISA __attribute__ ((target("neon,crypto")))
  600. #endif /* USE_CLANG_ATTR_TARGET_AARCH64 */
  601. #ifndef FUNC_ISA
  602. #define FUNC_ISA
  603. #endif
  604. #ifdef USE_ARM64_NEON_H
  605. #include <arm64_neon.h>
  606. #else
  607. #include <arm_neon.h>
  608. #endif
  609. static bool sha256_hw_available(void)
  610. {
  611. /*
  612. * For Arm, we delegate to a per-platform detection function (see
  613. * explanation in sshaes.c).
  614. */
  615. return platform_sha256_hw_available();
  616. }
  617. typedef struct sha256_neon_core sha256_neon_core;
  618. struct sha256_neon_core {
  619. uint32x4_t abcd, efgh;
  620. };
  621. FUNC_ISA
  622. static inline uint32x4_t sha256_neon_load_input(const uint8_t *p)
  623. {
  624. return vreinterpretq_u32_u8(vrev32q_u8(vld1q_u8(p)));
  625. }
  626. FUNC_ISA
  627. static inline uint32x4_t sha256_neon_schedule_update(
  628. uint32x4_t m4, uint32x4_t m3, uint32x4_t m2, uint32x4_t m1)
  629. {
  630. return vsha256su1q_u32(vsha256su0q_u32(m4, m3), m2, m1);
  631. }
  632. FUNC_ISA
  633. static inline sha256_neon_core sha256_neon_round4(
  634. sha256_neon_core old, uint32x4_t sched, unsigned round)
  635. {
  636. sha256_neon_core new;
  637. uint32x4_t round_input = vaddq_u32(
  638. sched, vld1q_u32(sha256_round_constants + round));
  639. new.abcd = vsha256hq_u32 (old.abcd, old.efgh, round_input);
  640. new.efgh = vsha256h2q_u32(old.efgh, old.abcd, round_input);
  641. return new;
  642. }
  643. FUNC_ISA
  644. static inline void sha256_neon_block(sha256_neon_core *core, const uint8_t *p)
  645. {
  646. uint32x4_t s0, s1, s2, s3;
  647. sha256_neon_core cr = *core;
  648. s0 = sha256_neon_load_input(p);
  649. cr = sha256_neon_round4(cr, s0, 0);
  650. s1 = sha256_neon_load_input(p+16);
  651. cr = sha256_neon_round4(cr, s1, 4);
  652. s2 = sha256_neon_load_input(p+32);
  653. cr = sha256_neon_round4(cr, s2, 8);
  654. s3 = sha256_neon_load_input(p+48);
  655. cr = sha256_neon_round4(cr, s3, 12);
  656. s0 = sha256_neon_schedule_update(s0, s1, s2, s3);
  657. cr = sha256_neon_round4(cr, s0, 16);
  658. s1 = sha256_neon_schedule_update(s1, s2, s3, s0);
  659. cr = sha256_neon_round4(cr, s1, 20);
  660. s2 = sha256_neon_schedule_update(s2, s3, s0, s1);
  661. cr = sha256_neon_round4(cr, s2, 24);
  662. s3 = sha256_neon_schedule_update(s3, s0, s1, s2);
  663. cr = sha256_neon_round4(cr, s3, 28);
  664. s0 = sha256_neon_schedule_update(s0, s1, s2, s3);
  665. cr = sha256_neon_round4(cr, s0, 32);
  666. s1 = sha256_neon_schedule_update(s1, s2, s3, s0);
  667. cr = sha256_neon_round4(cr, s1, 36);
  668. s2 = sha256_neon_schedule_update(s2, s3, s0, s1);
  669. cr = sha256_neon_round4(cr, s2, 40);
  670. s3 = sha256_neon_schedule_update(s3, s0, s1, s2);
  671. cr = sha256_neon_round4(cr, s3, 44);
  672. s0 = sha256_neon_schedule_update(s0, s1, s2, s3);
  673. cr = sha256_neon_round4(cr, s0, 48);
  674. s1 = sha256_neon_schedule_update(s1, s2, s3, s0);
  675. cr = sha256_neon_round4(cr, s1, 52);
  676. s2 = sha256_neon_schedule_update(s2, s3, s0, s1);
  677. cr = sha256_neon_round4(cr, s2, 56);
  678. s3 = sha256_neon_schedule_update(s3, s0, s1, s2);
  679. cr = sha256_neon_round4(cr, s3, 60);
  680. core->abcd = vaddq_u32(core->abcd, cr.abcd);
  681. core->efgh = vaddq_u32(core->efgh, cr.efgh);
  682. }
  683. typedef struct sha256_neon {
  684. sha256_neon_core core;
  685. sha256_block blk;
  686. BinarySink_IMPLEMENTATION;
  687. ssh_hash hash;
  688. } sha256_neon;
  689. static void sha256_neon_write(BinarySink *bs, const void *vp, size_t len);
  690. static ssh_hash *sha256_neon_new(const ssh_hashalg *alg)
  691. {
  692. if (!sha256_hw_available_cached())
  693. return NULL;
  694. sha256_neon *s = snew(sha256_neon);
  695. s->core.abcd = vld1q_u32(sha256_initial_state);
  696. s->core.efgh = vld1q_u32(sha256_initial_state + 4);
  697. sha256_block_setup(&s->blk);
  698. s->hash.vt = alg;
  699. BinarySink_INIT(s, sha256_neon_write);
  700. BinarySink_DELEGATE_INIT(&s->hash, s);
  701. return &s->hash;
  702. }
  703. static ssh_hash *sha256_neon_copy(ssh_hash *hash)
  704. {
  705. sha256_neon *s = container_of(hash, sha256_neon, hash);
  706. sha256_neon *copy = snew(sha256_neon);
  707. *copy = *s; /* structure copy */
  708. BinarySink_COPIED(copy);
  709. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  710. return &copy->hash;
  711. }
  712. static void sha256_neon_free(ssh_hash *hash)
  713. {
  714. sha256_neon *s = container_of(hash, sha256_neon, hash);
  715. smemclr(s, sizeof(*s));
  716. sfree(s);
  717. }
  718. static void sha256_neon_write(BinarySink *bs, const void *vp, size_t len)
  719. {
  720. sha256_neon *s = BinarySink_DOWNCAST(bs, sha256_neon);
  721. while (len > 0)
  722. if (sha256_block_write(&s->blk, &vp, &len))
  723. sha256_neon_block(&s->core, s->blk.block);
  724. }
  725. static void sha256_neon_final(ssh_hash *hash, uint8_t *digest)
  726. {
  727. sha256_neon *s = container_of(hash, sha256_neon, hash);
  728. sha256_block_pad(&s->blk, BinarySink_UPCAST(s));
  729. vst1q_u8(digest, vrev32q_u8(vreinterpretq_u8_u32(s->core.abcd)));
  730. vst1q_u8(digest + 16, vrev32q_u8(vreinterpretq_u8_u32(s->core.efgh)));
  731. sha256_neon_free(hash);
  732. }
  733. const ssh_hashalg ssh_sha256_hw = {
  734. sha256_neon_new, sha256_neon_copy, sha256_neon_final, sha256_neon_free,
  735. 32, 64, HASHALG_NAMES_ANNOTATED("SHA-256", "NEON accelerated"),
  736. };
  737. /* ----------------------------------------------------------------------
  738. * Stub functions if we have no hardware-accelerated SHA-256. In this
  739. * case, sha256_hw_new returns NULL (though it should also never be
  740. * selected by sha256_select, so the only thing that should even be
  741. * _able_ to call it is testcrypt). As a result, the remaining vtable
  742. * functions should never be called at all.
  743. */
  744. #elif HW_SHA256 == HW_SHA256_NONE
  745. static bool sha256_hw_available(void)
  746. {
  747. return false;
  748. }
  749. static ssh_hash *sha256_stub_new(const ssh_hashalg *alg)
  750. {
  751. return NULL;
  752. }
  753. #define STUB_BODY { unreachable("Should never be called"); }
  754. static ssh_hash *sha256_stub_copy(ssh_hash *hash) STUB_BODY
  755. static void sha256_stub_free(ssh_hash *hash) STUB_BODY
  756. static void sha256_stub_final(ssh_hash *hash, uint8_t *digest) STUB_BODY
  757. const ssh_hashalg ssh_sha256_hw = {
  758. sha256_stub_new, sha256_stub_copy, sha256_stub_final, sha256_stub_free,
  759. 32, 64, HASHALG_NAMES_ANNOTATED(
  760. "SHA-256", "!NONEXISTENT ACCELERATED VERSION!"),
  761. };
  762. #endif /* HW_SHA256 */