sshsha.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * SHA1 hash algorithm. Used in SSH-2 as a MAC, and the transform is
  3. * also used as a `stirring' function for the PuTTY random number
  4. * pool. Implemented directly from the specification by Simon
  5. * Tatham.
  6. */
  7. #include "ssh.h"
  8. #include <assert.h>
  9. typedef struct SHA_State {
  10. uint32_t h[5];
  11. unsigned char block[64];
  12. int blkused;
  13. uint64_t len;
  14. void (*sha1)(struct SHA_State * s, const unsigned char *p, int len);
  15. BinarySink_IMPLEMENTATION;
  16. } SHA_State;
  17. /* ----------------------------------------------------------------------
  18. * Core SHA algorithm: processes 16-word blocks into a message digest.
  19. */
  20. #define rol(x,y) ( ((x) << (y)) | (((uint32_t)x) >> (32-y)) )
  21. static void sha1_sw(SHA_State * s, const unsigned char *q, int len);
  22. static void sha1_ni(SHA_State * s, const unsigned char *q, int len);
  23. static void SHA_Core_Init(uint32_t h[5])
  24. {
  25. h[0] = 0x67452301;
  26. h[1] = 0xefcdab89;
  27. h[2] = 0x98badcfe;
  28. h[3] = 0x10325476;
  29. h[4] = 0xc3d2e1f0;
  30. }
  31. void SHATransform(uint32_t * digest, uint32_t * block)
  32. {
  33. uint32_t w[80];
  34. uint32_t a, b, c, d, e;
  35. int t;
  36. #ifdef RANDOM_DIAGNOSTICS
  37. {
  38. extern int random_diagnostics;
  39. if (random_diagnostics) {
  40. int i;
  41. printf("SHATransform:");
  42. for (i = 0; i < 5; i++)
  43. printf(" %08x", digest[i]);
  44. printf(" +");
  45. for (i = 0; i < 16; i++)
  46. printf(" %08x", block[i]);
  47. }
  48. }
  49. #endif
  50. for (t = 0; t < 16; t++)
  51. w[t] = block[t];
  52. for (t = 16; t < 80; t++) {
  53. uint32_t tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
  54. w[t] = rol(tmp, 1);
  55. }
  56. a = digest[0];
  57. b = digest[1];
  58. c = digest[2];
  59. d = digest[3];
  60. e = digest[4];
  61. for (t = 0; t < 20; t++) {
  62. uint32_t tmp =
  63. rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999;
  64. e = d;
  65. d = c;
  66. c = rol(b, 30);
  67. b = a;
  68. a = tmp;
  69. }
  70. for (t = 20; t < 40; t++) {
  71. uint32_t tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1;
  72. e = d;
  73. d = c;
  74. c = rol(b, 30);
  75. b = a;
  76. a = tmp;
  77. }
  78. for (t = 40; t < 60; t++) {
  79. uint32_t tmp = rol(a,
  80. 5) + ((b & c) | (b & d) | (c & d)) + e + w[t] +
  81. 0x8f1bbcdc;
  82. e = d;
  83. d = c;
  84. c = rol(b, 30);
  85. b = a;
  86. a = tmp;
  87. }
  88. for (t = 60; t < 80; t++) {
  89. uint32_t tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6;
  90. e = d;
  91. d = c;
  92. c = rol(b, 30);
  93. b = a;
  94. a = tmp;
  95. }
  96. digest[0] += a;
  97. digest[1] += b;
  98. digest[2] += c;
  99. digest[3] += d;
  100. digest[4] += e;
  101. #ifdef RANDOM_DIAGNOSTICS
  102. {
  103. extern int random_diagnostics;
  104. if (random_diagnostics) {
  105. int i;
  106. printf(" =");
  107. for (i = 0; i < 5; i++)
  108. printf(" %08x", digest[i]);
  109. printf("\n");
  110. }
  111. }
  112. #endif
  113. }
  114. /* ----------------------------------------------------------------------
  115. * Outer SHA algorithm: take an arbitrary length byte string,
  116. * convert it into 16-word blocks with the prescribed padding at
  117. * the end, and pass those blocks to the core SHA algorithm.
  118. */
  119. static void SHA_BinarySink_write(BinarySink *bs, const void *p, size_t len);
  120. void SHA_Init(SHA_State * s)
  121. {
  122. SHA_Core_Init(s->h);
  123. s->blkused = 0;
  124. s->len = 0;
  125. if (supports_sha_ni())
  126. s->sha1 = &sha1_ni;
  127. else
  128. s->sha1 = &sha1_sw;
  129. BinarySink_INIT(s, SHA_BinarySink_write);
  130. }
  131. static void SHA_BinarySink_write(BinarySink *bs, const void *p, size_t len)
  132. {
  133. struct SHA_State *s = BinarySink_DOWNCAST(bs, struct SHA_State);
  134. const unsigned char *q = (const unsigned char *) p;
  135. /*
  136. * Update the length field.
  137. */
  138. s->len += len;
  139. (*(s->sha1))(s, q, len);
  140. }
  141. static void sha1_sw(SHA_State * s, const unsigned char *q, int len)
  142. {
  143. uint32_t wordblock[16];
  144. int i;
  145. if (s->blkused && s->blkused + len < 64) {
  146. /*
  147. * Trivial case: just add to the block.
  148. */
  149. memcpy(s->block + s->blkused, q, len);
  150. s->blkused += len;
  151. } else {
  152. /*
  153. * We must complete and process at least one block.
  154. */
  155. while (s->blkused + len >= 64) {
  156. memcpy(s->block + s->blkused, q, 64 - s->blkused);
  157. q += 64 - s->blkused;
  158. len -= 64 - s->blkused;
  159. /* Now process the block. Gather bytes big-endian into words */
  160. for (i = 0; i < 16; i++) {
  161. wordblock[i] =
  162. (((uint32_t) s->block[i * 4 + 0]) << 24) |
  163. (((uint32_t) s->block[i * 4 + 1]) << 16) |
  164. (((uint32_t) s->block[i * 4 + 2]) << 8) |
  165. (((uint32_t) s->block[i * 4 + 3]) << 0);
  166. }
  167. SHATransform(s->h, wordblock);
  168. s->blkused = 0;
  169. }
  170. memcpy(s->block, q, len);
  171. s->blkused = len;
  172. }
  173. }
  174. void SHA_Final(SHA_State * s, unsigned char *output)
  175. {
  176. int i;
  177. int pad;
  178. unsigned char c[64];
  179. uint64_t len;
  180. if (s->blkused >= 56)
  181. pad = 56 + 64 - s->blkused;
  182. else
  183. pad = 56 - s->blkused;
  184. len = (s->len << 3);
  185. memset(c, 0, pad);
  186. c[0] = 0x80;
  187. put_data(s, &c, pad);
  188. put_uint64(s, len);
  189. for (i = 0; i < 5; i++) {
  190. output[i * 4] = (s->h[i] >> 24) & 0xFF;
  191. output[i * 4 + 1] = (s->h[i] >> 16) & 0xFF;
  192. output[i * 4 + 2] = (s->h[i] >> 8) & 0xFF;
  193. output[i * 4 + 3] = (s->h[i]) & 0xFF;
  194. }
  195. }
  196. void SHA_Simple(const void *p, int len, unsigned char *output)
  197. {
  198. SHA_State s;
  199. SHA_Init(&s);
  200. put_data(&s, p, len);
  201. SHA_Final(&s, output);
  202. smemclr(&s, sizeof(s));
  203. }
  204. /*
  205. * Thin abstraction for things where hashes are pluggable.
  206. */
  207. struct sha1_hash {
  208. SHA_State state;
  209. ssh_hash hash;
  210. };
  211. static ssh_hash *sha1_new(const ssh_hashalg *alg)
  212. {
  213. struct sha1_hash *h = snew(struct sha1_hash);
  214. SHA_Init(&h->state);
  215. h->hash.vt = alg;
  216. BinarySink_DELEGATE_INIT(&h->hash, &h->state);
  217. return &h->hash;
  218. }
  219. static ssh_hash *sha1_copy(ssh_hash *hashold)
  220. {
  221. struct sha1_hash *hold, *hnew;
  222. ssh_hash *hashnew = sha1_new(hashold->vt);
  223. hold = container_of(hashold, struct sha1_hash, hash);
  224. hnew = container_of(hashnew, struct sha1_hash, hash);
  225. hnew->state = hold->state;
  226. BinarySink_COPIED(&hnew->state);
  227. return hashnew;
  228. }
  229. static void sha1_free(ssh_hash *hash)
  230. {
  231. struct sha1_hash *h = container_of(hash, struct sha1_hash, hash);
  232. smemclr(h, sizeof(*h));
  233. sfree(h);
  234. }
  235. static void sha1_final(ssh_hash *hash, unsigned char *output)
  236. {
  237. struct sha1_hash *h = container_of(hash, struct sha1_hash, hash);
  238. SHA_Final(&h->state, output);
  239. sha1_free(hash);
  240. }
  241. const ssh_hashalg ssh_sha1 = {
  242. sha1_new, sha1_copy, sha1_final, sha1_free, 20, 64, "SHA-1"
  243. };
  244. #ifdef COMPILER_SUPPORTS_SHA_NI
  245. #if defined _MSC_VER && defined _M_AMD64
  246. # include <intrin.h>
  247. #endif
  248. /*
  249. * Set target architecture for Clang and GCC
  250. */
  251. #if !defined(__clang__) && defined(__GNUC__)
  252. # pragma GCC target("sha")
  253. # pragma GCC target("sse4.1")
  254. #endif
  255. #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 5))
  256. # define FUNC_ISA __attribute__ ((target("sse4.1,sha")))
  257. #else
  258. # define FUNC_ISA
  259. #endif
  260. #include <wmmintrin.h>
  261. #include <smmintrin.h>
  262. #include <immintrin.h>
  263. #if defined(__clang__) || defined(__GNUC__)
  264. #include <shaintrin.h>
  265. #endif
  266. /*
  267. * Determinators of CPU type
  268. */
  269. #if defined(__clang__) || defined(__GNUC__)
  270. #include <cpuid.h>
  271. bool supports_sha_ni(void)
  272. {
  273. unsigned int CPUInfo[4];
  274. __cpuid(0, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
  275. if (CPUInfo[0] < 7)
  276. return false;
  277. __cpuid_count(7, 0, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
  278. return CPUInfo[1] & (1 << 29); /* SHA */
  279. }
  280. #else /* defined(__clang__) || defined(__GNUC__) */
  281. bool supports_sha_ni(void)
  282. {
  283. unsigned int CPUInfo[4];
  284. __cpuid(CPUInfo, 0);
  285. if (CPUInfo[0] < 7)
  286. return false;
  287. __cpuidex(CPUInfo, 7, 0);
  288. return CPUInfo[1] & (1 << 29); /* Check SHA */
  289. }
  290. #endif /* defined(__clang__) || defined(__GNUC__) */
  291. /* SHA1 implementation using new instructions
  292. The code is based on Jeffrey Walton's SHA1 implementation:
  293. https://github.com/noloader/SHA-Intrinsics
  294. */
  295. FUNC_ISA
  296. static void sha1_ni_(SHA_State * s, const unsigned char *q, int len)
  297. {
  298. if (s->blkused && s->blkused + len < 64) {
  299. /*
  300. * Trivial case: just add to the block.
  301. */
  302. memcpy(s->block + s->blkused, q, len);
  303. s->blkused += len;
  304. } else {
  305. __m128i ABCD, ABCD_SAVE, E0, E0_SAVE, E1;
  306. const __m128i MASK = _mm_set_epi64x(0x0001020304050607ULL, 0x08090a0b0c0d0e0fULL);
  307. ABCD = _mm_loadu_si128((const __m128i*) s->h);
  308. E0 = _mm_set_epi32(s->h[4], 0, 0, 0);
  309. ABCD = _mm_shuffle_epi32(ABCD, 0x1B);
  310. /*
  311. * We must complete and process at least one block.
  312. */
  313. while (s->blkused + len >= 64)
  314. {
  315. __m128i MSG0, MSG1, MSG2, MSG3;
  316. memcpy(s->block + s->blkused, q, 64 - s->blkused);
  317. q += 64 - s->blkused;
  318. len -= 64 - s->blkused;
  319. /* Save current state */
  320. ABCD_SAVE = ABCD;
  321. E0_SAVE = E0;
  322. /* Rounds 0-3 */
  323. MSG0 = _mm_loadu_si128((const __m128i*)(s->block + 0));
  324. MSG0 = _mm_shuffle_epi8(MSG0, MASK);
  325. E0 = _mm_add_epi32(E0, MSG0);
  326. E1 = ABCD;
  327. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  328. /* Rounds 4-7 */
  329. MSG1 = _mm_loadu_si128((const __m128i*)(s->block + 16));
  330. MSG1 = _mm_shuffle_epi8(MSG1, MASK);
  331. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  332. E0 = ABCD;
  333. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 0);
  334. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  335. /* Rounds 8-11 */
  336. MSG2 = _mm_loadu_si128((const __m128i*)(s->block + 32));
  337. MSG2 = _mm_shuffle_epi8(MSG2, MASK);
  338. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  339. E1 = ABCD;
  340. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  341. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  342. MSG0 = _mm_xor_si128(MSG0, MSG2);
  343. /* Rounds 12-15 */
  344. MSG3 = _mm_loadu_si128((const __m128i*)(s->block + 48));
  345. MSG3 = _mm_shuffle_epi8(MSG3, MASK);
  346. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  347. E0 = ABCD;
  348. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  349. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 0);
  350. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  351. MSG1 = _mm_xor_si128(MSG1, MSG3);
  352. /* Rounds 16-19 */
  353. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  354. E1 = ABCD;
  355. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  356. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 0);
  357. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  358. MSG2 = _mm_xor_si128(MSG2, MSG0);
  359. /* Rounds 20-23 */
  360. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  361. E0 = ABCD;
  362. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  363. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  364. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  365. MSG3 = _mm_xor_si128(MSG3, MSG1);
  366. /* Rounds 24-27 */
  367. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  368. E1 = ABCD;
  369. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  370. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 1);
  371. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  372. MSG0 = _mm_xor_si128(MSG0, MSG2);
  373. /* Rounds 28-31 */
  374. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  375. E0 = ABCD;
  376. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  377. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  378. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  379. MSG1 = _mm_xor_si128(MSG1, MSG3);
  380. /* Rounds 32-35 */
  381. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  382. E1 = ABCD;
  383. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  384. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 1);
  385. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  386. MSG2 = _mm_xor_si128(MSG2, MSG0);
  387. /* Rounds 36-39 */
  388. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  389. E0 = ABCD;
  390. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  391. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 1);
  392. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  393. MSG3 = _mm_xor_si128(MSG3, MSG1);
  394. /* Rounds 40-43 */
  395. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  396. E1 = ABCD;
  397. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  398. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  399. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  400. MSG0 = _mm_xor_si128(MSG0, MSG2);
  401. /* Rounds 44-47 */
  402. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  403. E0 = ABCD;
  404. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  405. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 2);
  406. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  407. MSG1 = _mm_xor_si128(MSG1, MSG3);
  408. /* Rounds 48-51 */
  409. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  410. E1 = ABCD;
  411. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  412. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  413. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  414. MSG2 = _mm_xor_si128(MSG2, MSG0);
  415. /* Rounds 52-55 */
  416. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  417. E0 = ABCD;
  418. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  419. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 2);
  420. MSG0 = _mm_sha1msg1_epu32(MSG0, MSG1);
  421. MSG3 = _mm_xor_si128(MSG3, MSG1);
  422. /* Rounds 56-59 */
  423. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  424. E1 = ABCD;
  425. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  426. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 2);
  427. MSG1 = _mm_sha1msg1_epu32(MSG1, MSG2);
  428. MSG0 = _mm_xor_si128(MSG0, MSG2);
  429. /* Rounds 60-63 */
  430. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  431. E0 = ABCD;
  432. MSG0 = _mm_sha1msg2_epu32(MSG0, MSG3);
  433. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  434. MSG2 = _mm_sha1msg1_epu32(MSG2, MSG3);
  435. MSG1 = _mm_xor_si128(MSG1, MSG3);
  436. /* Rounds 64-67 */
  437. E0 = _mm_sha1nexte_epu32(E0, MSG0);
  438. E1 = ABCD;
  439. MSG1 = _mm_sha1msg2_epu32(MSG1, MSG0);
  440. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 3);
  441. MSG3 = _mm_sha1msg1_epu32(MSG3, MSG0);
  442. MSG2 = _mm_xor_si128(MSG2, MSG0);
  443. /* Rounds 68-71 */
  444. E1 = _mm_sha1nexte_epu32(E1, MSG1);
  445. E0 = ABCD;
  446. MSG2 = _mm_sha1msg2_epu32(MSG2, MSG1);
  447. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  448. MSG3 = _mm_xor_si128(MSG3, MSG1);
  449. /* Rounds 72-75 */
  450. E0 = _mm_sha1nexte_epu32(E0, MSG2);
  451. E1 = ABCD;
  452. MSG3 = _mm_sha1msg2_epu32(MSG3, MSG2);
  453. ABCD = _mm_sha1rnds4_epu32(ABCD, E0, 3);
  454. /* Rounds 76-79 */
  455. E1 = _mm_sha1nexte_epu32(E1, MSG3);
  456. E0 = ABCD;
  457. ABCD = _mm_sha1rnds4_epu32(ABCD, E1, 3);
  458. /* Combine state */
  459. E0 = _mm_sha1nexte_epu32(E0, E0_SAVE);
  460. ABCD = _mm_add_epi32(ABCD, ABCD_SAVE);
  461. s->blkused = 0;
  462. }
  463. ABCD = _mm_shuffle_epi32(ABCD, 0x1B);
  464. /* Save state */
  465. _mm_storeu_si128((__m128i*) s->h, ABCD);
  466. s->h[4] = _mm_extract_epi32(E0, 3);
  467. memcpy(s->block, q, len);
  468. s->blkused = len;
  469. }
  470. }
  471. /*
  472. * Workaround LLVM bug https://bugs.llvm.org/show_bug.cgi?id=34980
  473. */
  474. static void sha1_ni(SHA_State * s, const unsigned char *q, int len)
  475. {
  476. sha1_ni_(s, q, len);
  477. }
  478. #else /* COMPILER_SUPPORTS_AES_NI */
  479. static void sha1_ni(SHA_State * s, const unsigned char *q, int len)
  480. {
  481. unreachable("sha1_ni not compiled in");
  482. }
  483. bool supports_sha_ni(void)
  484. {
  485. return false;
  486. }
  487. #endif /* COMPILER_SUPPORTS_AES_NI */