sshsh256.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. #ifdef _FORCE_SHA_NI
  14. # define HW_SHA256 HW_SHA256_NI
  15. #elif defined(__clang__)
  16. # if __has_attribute(target) && __has_include(<wmmintrin.h>) && \
  17. (defined(__x86_64__) || defined(__i386))
  18. # define HW_SHA256 HW_SHA256_NI
  19. # endif
  20. #elif defined(__GNUC__)
  21. # if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)) && \
  22. (defined(__x86_64__) || defined(__i386))
  23. # define HW_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. // Should be working (when set to HW_SHA256_NI), but we do not have a HW to test this on
  31. #undef HW_SHA256
  32. #if defined _FORCE_SOFTWARE_SHA || !defined HW_SHA256
  33. # undef HW_SHA256
  34. # define HW_SHA256 HW_SHA256_NONE
  35. #endif
  36. #ifndef WINSCP_VS
  37. /*
  38. * The actual query function that asks if hardware acceleration is
  39. * available.
  40. */
  41. bool sha256_hw_available(void);
  42. /*
  43. * The top-level selection function, caching the results of
  44. * sha256_hw_available() so it only has to run once.
  45. */
  46. /*WINSCP static*/ bool sha256_hw_available_cached(void)
  47. {
  48. static bool initialised = false;
  49. static bool hw_available;
  50. if (!initialised) {
  51. hw_available = sha256_hw_available();
  52. initialised = true;
  53. }
  54. return hw_available;
  55. }
  56. static ssh_hash *sha256_select(const ssh_hashalg *alg)
  57. {
  58. const ssh_hashalg *real_alg =
  59. sha256_hw_available_cached() ? &ssh_sha256_hw : &ssh_sha256_sw;
  60. return ssh_hash_new(real_alg);
  61. }
  62. const ssh_hashalg ssh_sha256 = {
  63. sha256_select, NULL, NULL, NULL,
  64. 32, 64, "SHA-256",
  65. };
  66. #else
  67. bool sha256_hw_available_cached(void);
  68. #endif
  69. /* ----------------------------------------------------------------------
  70. * Definitions likely to be helpful to multiple implementations.
  71. */
  72. static const uint32_t sha256_initial_state[] = {
  73. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
  74. 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
  75. };
  76. static const uint32_t sha256_round_constants[] = {
  77. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  78. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  79. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  80. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  81. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  82. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  83. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  84. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  85. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  86. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  87. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  88. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  89. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  90. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  91. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  92. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  93. };
  94. #define SHA256_ROUNDS 64
  95. typedef struct sha256_block sha256_block;
  96. struct sha256_block {
  97. uint8_t block[64];
  98. size_t used;
  99. uint64_t len;
  100. };
  101. static inline void sha256_block_setup(sha256_block *blk)
  102. {
  103. blk->used = 0;
  104. blk->len = 0;
  105. }
  106. #ifdef WINSCP_VS
  107. /*WINSCP static inline*/ bool sha256_block_write(
  108. sha256_block *blk, const void **vdata, size_t *len)
  109. {
  110. size_t blkleft = sizeof(blk->block) - blk->used;
  111. size_t chunk = *len < blkleft ? *len : blkleft;
  112. const uint8_t *p = *vdata;
  113. memcpy(blk->block + blk->used, p, chunk);
  114. *vdata = p + chunk;
  115. *len -= chunk;
  116. blk->used += chunk;
  117. blk->len += chunk;
  118. if (blk->used == sizeof(blk->block)) {
  119. blk->used = 0;
  120. return true;
  121. }
  122. return false;
  123. }
  124. /*WINSCP static inline*/ void sha256_block_pad(sha256_block *blk, BinarySink *bs)
  125. {
  126. uint64_t final_len = blk->len << 3;
  127. size_t pad = 1 + (63 & (55 - blk->used));
  128. put_byte(bs, 0x80);
  129. for (size_t i = 1; i < pad; i++)
  130. put_byte(bs, 0);
  131. put_uint64(bs, final_len);
  132. assert(blk->used == 0 && "Should have exactly hit a block boundary");
  133. }
  134. /* ----------------------------------------------------------------------
  135. * Software implementation of SHA-256.
  136. */
  137. static inline uint32_t ror(uint32_t x, unsigned y)
  138. {
  139. return (x << (31 & /*WINSCP*/(uint32_t)(-(int32_t)y))) | (x >> (31 & y));
  140. }
  141. static inline uint32_t Ch(uint32_t ctrl, uint32_t if1, uint32_t if0)
  142. {
  143. return if0 ^ (ctrl & (if1 ^ if0));
  144. }
  145. static inline uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
  146. {
  147. return (x & y) | (z & (x | y));
  148. }
  149. static inline uint32_t Sigma_0(uint32_t x)
  150. {
  151. return ror(x,2) ^ ror(x,13) ^ ror(x,22);
  152. }
  153. static inline uint32_t Sigma_1(uint32_t x)
  154. {
  155. return ror(x,6) ^ ror(x,11) ^ ror(x,25);
  156. }
  157. static inline uint32_t sigma_0(uint32_t x)
  158. {
  159. return ror(x,7) ^ ror(x,18) ^ (x >> 3);
  160. }
  161. static inline uint32_t sigma_1(uint32_t x)
  162. {
  163. return ror(x,17) ^ ror(x,19) ^ (x >> 10);
  164. }
  165. static inline void sha256_sw_round(
  166. unsigned round_index, const uint32_t *schedule,
  167. uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d,
  168. uint32_t *e, uint32_t *f, uint32_t *g, uint32_t *h)
  169. {
  170. uint32_t t1 = *h + Sigma_1(*e) + Ch(*e,*f,*g) +
  171. sha256_round_constants[round_index] + schedule[round_index];
  172. uint32_t t2 = Sigma_0(*a) + Maj(*a,*b,*c);
  173. *d += t1;
  174. *h = t1 + t2;
  175. }
  176. /*WINSCP static*/ void sha256_sw_block(uint32_t *core, const uint8_t *block)
  177. {
  178. uint32_t w[SHA256_ROUNDS];
  179. uint32_t a,b,c,d,e,f,g,h;
  180. for (size_t t = 0; t < 16; t++)
  181. w[t] = GET_32BIT_MSB_FIRST(block + 4*t);
  182. for (size_t t = 16; t < SHA256_ROUNDS; t++)
  183. w[t] = sigma_1(w[t-2]) + w[t-7] + sigma_0(w[t-15]) + w[t-16];
  184. a = core[0]; b = core[1]; c = core[2]; d = core[3];
  185. e = core[4]; f = core[5]; g = core[6]; h = core[7];
  186. for (size_t t = 0; t < SHA256_ROUNDS; t += 8) {
  187. sha256_sw_round(t+0, w, &a,&b,&c,&d,&e,&f,&g,&h);
  188. sha256_sw_round(t+1, w, &h,&a,&b,&c,&d,&e,&f,&g);
  189. sha256_sw_round(t+2, w, &g,&h,&a,&b,&c,&d,&e,&f);
  190. sha256_sw_round(t+3, w, &f,&g,&h,&a,&b,&c,&d,&e);
  191. sha256_sw_round(t+4, w, &e,&f,&g,&h,&a,&b,&c,&d);
  192. sha256_sw_round(t+5, w, &d,&e,&f,&g,&h,&a,&b,&c);
  193. sha256_sw_round(t+6, w, &c,&d,&e,&f,&g,&h,&a,&b);
  194. sha256_sw_round(t+7, w, &b,&c,&d,&e,&f,&g,&h,&a);
  195. }
  196. core[0] += a; core[1] += b; core[2] += c; core[3] += d;
  197. core[4] += e; core[5] += f; core[6] += g; core[7] += h;
  198. smemclr(w, sizeof(w));
  199. }
  200. #endif // WINSCP_VS
  201. #ifndef WINSCP_VS
  202. bool sha256_block_write(
  203. sha256_block *blk, const void **vdata, size_t *len);
  204. void sha256_sw_block(uint32_t *core, const uint8_t *block);
  205. void sha256_block_pad(sha256_block *blk, BinarySink *bs);
  206. typedef struct sha256_sw {
  207. uint32_t core[8];
  208. sha256_block blk;
  209. BinarySink_IMPLEMENTATION;
  210. ssh_hash hash;
  211. } sha256_sw;
  212. static void sha256_sw_write(BinarySink *bs, const void *vp, size_t len);
  213. static ssh_hash *sha256_sw_new(const ssh_hashalg *alg)
  214. {
  215. sha256_sw *s = snew(sha256_sw);
  216. memcpy(s->core, sha256_initial_state, sizeof(s->core));
  217. sha256_block_setup(&s->blk);
  218. s->hash.vt = alg;
  219. BinarySink_INIT(s, sha256_sw_write);
  220. BinarySink_DELEGATE_INIT(&s->hash, s);
  221. return &s->hash;
  222. }
  223. static ssh_hash *sha256_sw_copy(ssh_hash *hash)
  224. {
  225. sha256_sw *s = container_of(hash, sha256_sw, hash);
  226. sha256_sw *copy = snew(sha256_sw);
  227. memcpy(copy, s, sizeof(*copy));
  228. BinarySink_COPIED(copy);
  229. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  230. return &copy->hash;
  231. }
  232. static void sha256_sw_free(ssh_hash *hash)
  233. {
  234. sha256_sw *s = container_of(hash, sha256_sw, hash);
  235. smemclr(s, sizeof(*s));
  236. sfree(s);
  237. }
  238. static void sha256_sw_write(BinarySink *bs, const void *vp, size_t len)
  239. {
  240. sha256_sw *s = BinarySink_DOWNCAST(bs, sha256_sw);
  241. while (len > 0)
  242. if (sha256_block_write(&s->blk, &vp, &len))
  243. sha256_sw_block(s->core, s->blk.block);
  244. }
  245. static void sha256_sw_final(ssh_hash *hash, uint8_t *digest)
  246. {
  247. sha256_sw *s = container_of(hash, sha256_sw, hash);
  248. sha256_block_pad(&s->blk, BinarySink_UPCAST(s));
  249. { // WINSCP
  250. size_t i; // WINSCP
  251. for (i = 0; i < 8; i++)
  252. PUT_32BIT_MSB_FIRST(digest + 4*i, s->core[i]);
  253. sha256_sw_free(hash);
  254. } // WINSCP
  255. }
  256. const ssh_hashalg ssh_sha256_sw = {
  257. sha256_sw_new, sha256_sw_copy, sha256_sw_final, sha256_sw_free,
  258. 32, 64, "SHA-256",
  259. };
  260. #endif // !WINSCP_VS
  261. /* ----------------------------------------------------------------------
  262. * Hardware-accelerated implementation of SHA-256 using x86 SHA-NI.
  263. */
  264. #if HW_SHA256 == HW_SHA256_NI
  265. #ifdef WINSCP_VS
  266. /*
  267. * Set target architecture for Clang and GCC
  268. */
  269. #if !defined(__clang__) && defined(__GNUC__)
  270. # pragma GCC target("sha")
  271. # pragma GCC target("sse4.1")
  272. #endif
  273. #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)))
  274. # define FUNC_ISA __attribute__ ((target("sse4.1,sha")))
  275. #else
  276. # define FUNC_ISA
  277. #endif
  278. #include <wmmintrin.h>
  279. #include <smmintrin.h>
  280. #include <immintrin.h>
  281. #if defined(__clang__) || defined(__GNUC__)
  282. #include <shaintrin.h>
  283. #endif
  284. #if defined(__clang__) || defined(__GNUC__)
  285. #include <cpuid.h>
  286. #define GET_CPU_ID_0(out) \
  287. __cpuid(0, (out)[0], (out)[1], (out)[2], (out)[3])
  288. #define GET_CPU_ID_7(out) \
  289. __cpuid_count(7, 0, (out)[0], (out)[1], (out)[2], (out)[3])
  290. #else
  291. #define GET_CPU_ID_0(out) __cpuid(out, 0)
  292. #define GET_CPU_ID_7(out) __cpuidex(out, 7, 0)
  293. #endif
  294. /*WINSCP static*/ bool sha256_hw_available(void)
  295. {
  296. unsigned int CPUInfo[4];
  297. GET_CPU_ID_0(CPUInfo);
  298. if (CPUInfo[0] < 7)
  299. return false;
  300. GET_CPU_ID_7(CPUInfo);
  301. return CPUInfo[1] & (1 << 29); /* Check SHA */
  302. }
  303. /* SHA256 implementation using new instructions
  304. The code is based on Jeffrey Walton's SHA256 implementation:
  305. https://github.com/noloader/SHA-Intrinsics
  306. */
  307. FUNC_ISA
  308. static inline void sha256_ni_block(__m128i *core, const uint8_t *p)
  309. {
  310. __m128i STATE0, STATE1;
  311. __m128i MSG, TMP;
  312. __m128i MSG0, MSG1, MSG2, MSG3;
  313. const __m128i *block = (const __m128i *)p;
  314. const __m128i MASK = _mm_set_epi64x(
  315. 0x0c0d0e0f08090a0bULL, 0x0405060700010203ULL);
  316. /* Load initial values */
  317. STATE0 = core[0];
  318. STATE1 = core[1];
  319. /* Rounds 0-3 */
  320. MSG = _mm_loadu_si128(block);
  321. MSG0 = _mm_shuffle_epi8(MSG, MASK);
  322. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(
  323. 0xE9B5DBA5B5C0FBCFULL, 0x71374491428A2F98ULL));
  324. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  325. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  326. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  327. /* Rounds 4-7 */
  328. MSG1 = _mm_loadu_si128(block + 1);
  329. MSG1 = _mm_shuffle_epi8(MSG1, MASK);
  330. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(
  331. 0xAB1C5ED5923F82A4ULL, 0x59F111F13956C25BULL));
  332. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  333. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  334. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  335. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  336. /* Rounds 8-11 */
  337. MSG2 = _mm_loadu_si128(block + 2);
  338. MSG2 = _mm_shuffle_epi8(MSG2, MASK);
  339. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(
  340. 0x550C7DC3243185BEULL, 0x12835B01D807AA98ULL));
  341. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  342. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  343. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  344. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  345. /* Rounds 12-15 */
  346. MSG3 = _mm_loadu_si128(block + 3);
  347. MSG3 = _mm_shuffle_epi8(MSG3, MASK);
  348. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(
  349. 0xC19BF1749BDC06A7ULL, 0x80DEB1FE72BE5D74ULL));
  350. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  351. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  352. MSG0 = _mm_add_epi32(MSG0, TMP);
  353. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  354. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  355. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  356. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  357. /* Rounds 16-19 */
  358. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(
  359. 0x240CA1CC0FC19DC6ULL, 0xEFBE4786E49B69C1ULL));
  360. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  361. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  362. MSG1 = _mm_add_epi32(MSG1, TMP);
  363. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  364. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  365. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  366. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  367. /* Rounds 20-23 */
  368. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(
  369. 0x76F988DA5CB0A9DCULL, 0x4A7484AA2DE92C6FULL));
  370. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  371. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  372. MSG2 = _mm_add_epi32(MSG2, TMP);
  373. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  374. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  375. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  376. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  377. /* Rounds 24-27 */
  378. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(
  379. 0xBF597FC7B00327C8ULL, 0xA831C66D983E5152ULL));
  380. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  381. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  382. MSG3 = _mm_add_epi32(MSG3, TMP);
  383. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  384. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  385. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  386. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  387. /* Rounds 28-31 */
  388. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(
  389. 0x1429296706CA6351ULL, 0xD5A79147C6E00BF3ULL));
  390. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  391. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  392. MSG0 = _mm_add_epi32(MSG0, TMP);
  393. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  394. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  395. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  396. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  397. /* Rounds 32-35 */
  398. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(
  399. 0x53380D134D2C6DFCULL, 0x2E1B213827B70A85ULL));
  400. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  401. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  402. MSG1 = _mm_add_epi32(MSG1, TMP);
  403. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  404. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  405. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  406. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  407. /* Rounds 36-39 */
  408. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(
  409. 0x92722C8581C2C92EULL, 0x766A0ABB650A7354ULL));
  410. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  411. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  412. MSG2 = _mm_add_epi32(MSG2, TMP);
  413. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  414. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  415. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  416. MSG0 = _mm_sha256msg1_epu32(MSG0, MSG1);
  417. /* Rounds 40-43 */
  418. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(
  419. 0xC76C51A3C24B8B70ULL, 0xA81A664BA2BFE8A1ULL));
  420. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  421. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  422. MSG3 = _mm_add_epi32(MSG3, TMP);
  423. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  424. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  425. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  426. MSG1 = _mm_sha256msg1_epu32(MSG1, MSG2);
  427. /* Rounds 44-47 */
  428. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(
  429. 0x106AA070F40E3585ULL, 0xD6990624D192E819ULL));
  430. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  431. TMP = _mm_alignr_epi8(MSG3, MSG2, 4);
  432. MSG0 = _mm_add_epi32(MSG0, TMP);
  433. MSG0 = _mm_sha256msg2_epu32(MSG0, MSG3);
  434. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  435. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  436. MSG2 = _mm_sha256msg1_epu32(MSG2, MSG3);
  437. /* Rounds 48-51 */
  438. MSG = _mm_add_epi32(MSG0, _mm_set_epi64x(
  439. 0x34B0BCB52748774CULL, 0x1E376C0819A4C116ULL));
  440. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  441. TMP = _mm_alignr_epi8(MSG0, MSG3, 4);
  442. MSG1 = _mm_add_epi32(MSG1, TMP);
  443. MSG1 = _mm_sha256msg2_epu32(MSG1, MSG0);
  444. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  445. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  446. MSG3 = _mm_sha256msg1_epu32(MSG3, MSG0);
  447. /* Rounds 52-55 */
  448. MSG = _mm_add_epi32(MSG1, _mm_set_epi64x(
  449. 0x682E6FF35B9CCA4FULL, 0x4ED8AA4A391C0CB3ULL));
  450. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  451. TMP = _mm_alignr_epi8(MSG1, MSG0, 4);
  452. MSG2 = _mm_add_epi32(MSG2, TMP);
  453. MSG2 = _mm_sha256msg2_epu32(MSG2, MSG1);
  454. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  455. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  456. /* Rounds 56-59 */
  457. MSG = _mm_add_epi32(MSG2, _mm_set_epi64x(
  458. 0x8CC7020884C87814ULL, 0x78A5636F748F82EEULL));
  459. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  460. TMP = _mm_alignr_epi8(MSG2, MSG1, 4);
  461. MSG3 = _mm_add_epi32(MSG3, TMP);
  462. MSG3 = _mm_sha256msg2_epu32(MSG3, MSG2);
  463. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  464. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  465. /* Rounds 60-63 */
  466. MSG = _mm_add_epi32(MSG3, _mm_set_epi64x(
  467. 0xC67178F2BEF9A3F7ULL, 0xA4506CEB90BEFFFAULL));
  468. STATE1 = _mm_sha256rnds2_epu32(STATE1, STATE0, MSG);
  469. MSG = _mm_shuffle_epi32(MSG, 0x0E);
  470. STATE0 = _mm_sha256rnds2_epu32(STATE0, STATE1, MSG);
  471. /* Combine state */
  472. core[0] = _mm_add_epi32(STATE0, core[0]);
  473. core[1] = _mm_add_epi32(STATE1, core[1]);
  474. }
  475. typedef struct sha256_ni {
  476. /*
  477. * These two vectors store the 8 words of the SHA-256 state, but
  478. * not in the same order they appear in the spec: the first word
  479. * holds A,B,E,F and the second word C,D,G,H.
  480. */
  481. __m128i core[2];
  482. sha256_block blk;
  483. void *pointer_to_free;
  484. BinarySink_IMPLEMENTATION;
  485. ssh_hash hash;
  486. } sha256_ni;
  487. static void sha256_ni_write(BinarySink *bs, const void *vp, size_t len);
  488. static sha256_ni *sha256_ni_alloc(void)
  489. {
  490. /*
  491. * The __m128i variables in the context structure need to be
  492. * 16-byte aligned, but not all malloc implementations that this
  493. * code has to work with will guarantee to return a 16-byte
  494. * aligned pointer. So we over-allocate, manually realign the
  495. * pointer ourselves, and store the original one inside the
  496. * context so we know how to free it later.
  497. */
  498. void *allocation = smalloc(sizeof(sha256_ni) + 15);
  499. uintptr_t alloc_address = (uintptr_t)allocation;
  500. uintptr_t aligned_address = (alloc_address + 15) & ~15;
  501. sha256_ni *s = (sha256_ni *)aligned_address;
  502. s->pointer_to_free = allocation;
  503. return s;
  504. }
  505. FUNC_ISA /*WINSCP static*/ ssh_hash *sha256_ni_new(const ssh_hashalg *alg)
  506. {
  507. if (!sha256_hw_available_cached())
  508. return NULL;
  509. sha256_ni *s = sha256_ni_alloc();
  510. /* Initialise the core vectors in their storage order */
  511. s->core[0] = _mm_set_epi64x(
  512. 0x6a09e667bb67ae85ULL, 0x510e527f9b05688cULL);
  513. s->core[1] = _mm_set_epi64x(
  514. 0x3c6ef372a54ff53aULL, 0x1f83d9ab5be0cd19ULL);
  515. sha256_block_setup(&s->blk);
  516. s->hash.vt = alg;
  517. BinarySink_INIT(s, sha256_ni_write);
  518. BinarySink_DELEGATE_INIT(&s->hash, s);
  519. return &s->hash;
  520. }
  521. /*WINSCP static*/ ssh_hash *sha256_ni_copy(ssh_hash *hash)
  522. {
  523. sha256_ni *s = container_of(hash, sha256_ni, hash);
  524. sha256_ni *copy = sha256_ni_alloc();
  525. void *ptf_save = copy->pointer_to_free;
  526. *copy = *s; /* structure copy */
  527. copy->pointer_to_free = ptf_save;
  528. BinarySink_COPIED(copy);
  529. BinarySink_DELEGATE_INIT(&copy->hash, copy);
  530. return &copy->hash;
  531. }
  532. /*WINSCP static*/ void sha256_ni_free(ssh_hash *hash)
  533. {
  534. sha256_ni *s = container_of(hash, sha256_ni, hash);
  535. void *ptf = s->pointer_to_free;
  536. smemclr(s, sizeof(*s));
  537. sfree(ptf);
  538. }
  539. static void sha256_ni_write(BinarySink *bs, const void *vp, size_t len)
  540. {
  541. sha256_ni *s = BinarySink_DOWNCAST(bs, sha256_ni);
  542. while (len > 0)
  543. if (sha256_block_write(&s->blk, &vp, &len))
  544. sha256_ni_block(s->core, s->blk.block);
  545. }
  546. FUNC_ISA /*WINSCP static*/ void sha256_ni_final(ssh_hash *hash, uint8_t *digest)
  547. {
  548. sha256_ni *s = container_of(hash, sha256_ni, hash);
  549. sha256_block_pad(&s->blk, BinarySink_UPCAST(s));
  550. /* Rearrange the words into the output order */
  551. __m128i feba = _mm_shuffle_epi32(s->core[0], 0x1B);
  552. __m128i dchg = _mm_shuffle_epi32(s->core[1], 0xB1);
  553. __m128i dcba = _mm_blend_epi16(feba, dchg, 0xF0);
  554. __m128i hgfe = _mm_alignr_epi8(dchg, feba, 8);
  555. /* Byte-swap them into the output endianness */
  556. const __m128i mask = _mm_setr_epi8(3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12);
  557. dcba = _mm_shuffle_epi8(dcba, mask);
  558. hgfe = _mm_shuffle_epi8(hgfe, mask);
  559. /* And store them */
  560. __m128i *output = (__m128i *)digest;
  561. _mm_storeu_si128(output, dcba);
  562. _mm_storeu_si128(output+1, hgfe);
  563. sha256_ni_free(hash);
  564. }
  565. #endif // WINSCP_VS
  566. #ifndef WINSCP_VS
  567. ssh_hash *sha256_ni_new(const ssh_hashalg *alg);
  568. ssh_hash *sha256_ni_copy(ssh_hash *hash);
  569. void sha256_ni_final(ssh_hash *hash, uint8_t *digest);
  570. void sha256_ni_free(ssh_hash *hash);
  571. const ssh_hashalg ssh_sha256_hw = {
  572. sha256_ni_new, sha256_ni_copy, sha256_ni_final, sha256_ni_free,
  573. 32, 64, "SHA-256",
  574. };
  575. #endif
  576. /* ----------------------------------------------------------------------
  577. * Stub functions if we have no hardware-accelerated SHA-256. In this
  578. * case, sha256_hw_new returns NULL (though it should also never be
  579. * selected by sha256_select, so the only thing that should even be
  580. * _able_ to call it is testcrypt). As a result, the remaining vtable
  581. * functions should never be called at all.
  582. */
  583. #elif HW_SHA256 == HW_SHA256_NONE
  584. static bool sha256_hw_available(void)
  585. {
  586. return false;
  587. }
  588. static ssh_hash *sha256_stub_new(const ssh_hashalg *alg)
  589. {
  590. return NULL;
  591. }
  592. #define STUB_BODY { unreachable("Should never be called"); }
  593. static ssh_hash *sha256_stub_copy(ssh_hash *hash) { STUB_BODY; return NULL; }
  594. static void sha256_stub_free(ssh_hash *hash) STUB_BODY
  595. static void sha256_stub_final(ssh_hash *hash, uint8_t *digest) STUB_BODY
  596. const ssh_hashalg ssh_sha256_hw = {
  597. sha256_stub_new, sha256_stub_copy, sha256_stub_final, sha256_stub_free,
  598. 32, 64, "SHA-256",
  599. };
  600. #endif /* HW_SHA256 */