sshsh256.c 29 KB

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