sshsh256.c 30 KB

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