sshsha.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. /* ----------------------------------------------------------------------
  9. * Core SHA algorithm: processes 16-word blocks into a message digest.
  10. */
  11. #define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) )
  12. static void SHA_Core_Init(uint32 h[5])
  13. {
  14. h[0] = 0x67452301;
  15. h[1] = 0xefcdab89;
  16. h[2] = 0x98badcfe;
  17. h[3] = 0x10325476;
  18. h[4] = 0xc3d2e1f0;
  19. }
  20. void SHATransform(word32 * digest, word32 * block)
  21. {
  22. word32 w[80];
  23. word32 a, b, c, d, e;
  24. int t;
  25. #ifdef RANDOM_DIAGNOSTICS
  26. {
  27. extern int random_diagnostics;
  28. if (random_diagnostics) {
  29. int i;
  30. printf("SHATransform:");
  31. for (i = 0; i < 5; i++)
  32. printf(" %08x", digest[i]);
  33. printf(" +");
  34. for (i = 0; i < 16; i++)
  35. printf(" %08x", block[i]);
  36. }
  37. }
  38. #endif
  39. for (t = 0; t < 16; t++)
  40. w[t] = block[t];
  41. for (t = 16; t < 80; t++) {
  42. word32 tmp = w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16];
  43. w[t] = rol(tmp, 1);
  44. }
  45. a = digest[0];
  46. b = digest[1];
  47. c = digest[2];
  48. d = digest[3];
  49. e = digest[4];
  50. for (t = 0; t < 20; t++) {
  51. word32 tmp =
  52. rol(a, 5) + ((b & c) | (d & ~b)) + e + w[t] + 0x5a827999;
  53. e = d;
  54. d = c;
  55. c = rol(b, 30);
  56. b = a;
  57. a = tmp;
  58. }
  59. for (t = 20; t < 40; t++) {
  60. word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0x6ed9eba1;
  61. e = d;
  62. d = c;
  63. c = rol(b, 30);
  64. b = a;
  65. a = tmp;
  66. }
  67. for (t = 40; t < 60; t++) {
  68. word32 tmp = rol(a,
  69. 5) + ((b & c) | (b & d) | (c & d)) + e + w[t] +
  70. 0x8f1bbcdc;
  71. e = d;
  72. d = c;
  73. c = rol(b, 30);
  74. b = a;
  75. a = tmp;
  76. }
  77. for (t = 60; t < 80; t++) {
  78. word32 tmp = rol(a, 5) + (b ^ c ^ d) + e + w[t] + 0xca62c1d6;
  79. e = d;
  80. d = c;
  81. c = rol(b, 30);
  82. b = a;
  83. a = tmp;
  84. }
  85. digest[0] += a;
  86. digest[1] += b;
  87. digest[2] += c;
  88. digest[3] += d;
  89. digest[4] += e;
  90. #ifdef RANDOM_DIAGNOSTICS
  91. {
  92. extern int random_diagnostics;
  93. if (random_diagnostics) {
  94. int i;
  95. printf(" =");
  96. for (i = 0; i < 5; i++)
  97. printf(" %08x", digest[i]);
  98. printf("\n");
  99. }
  100. }
  101. #endif
  102. }
  103. /* ----------------------------------------------------------------------
  104. * Outer SHA algorithm: take an arbitrary length byte string,
  105. * convert it into 16-word blocks with the prescribed padding at
  106. * the end, and pass those blocks to the core SHA algorithm.
  107. */
  108. void SHA_Init(SHA_State * s)
  109. {
  110. SHA_Core_Init(s->h);
  111. s->blkused = 0;
  112. s->lenhi = s->lenlo = 0;
  113. }
  114. void SHA_Bytes(SHA_State * s, const void *p, int len)
  115. {
  116. const unsigned char *q = (const unsigned char *) p;
  117. uint32 wordblock[16];
  118. uint32 lenw = len;
  119. int i;
  120. /*
  121. * Update the length field.
  122. */
  123. s->lenlo += lenw;
  124. s->lenhi += (s->lenlo < lenw);
  125. if (s->blkused && s->blkused + len < 64) {
  126. /*
  127. * Trivial case: just add to the block.
  128. */
  129. memcpy(s->block + s->blkused, q, len);
  130. s->blkused += len;
  131. } else {
  132. /*
  133. * We must complete and process at least one block.
  134. */
  135. while (s->blkused + len >= 64) {
  136. memcpy(s->block + s->blkused, q, 64 - s->blkused);
  137. q += 64 - s->blkused;
  138. len -= 64 - s->blkused;
  139. /* Now process the block. Gather bytes big-endian into words */
  140. for (i = 0; i < 16; i++) {
  141. wordblock[i] =
  142. (((uint32) s->block[i * 4 + 0]) << 24) |
  143. (((uint32) s->block[i * 4 + 1]) << 16) |
  144. (((uint32) s->block[i * 4 + 2]) << 8) |
  145. (((uint32) s->block[i * 4 + 3]) << 0);
  146. }
  147. SHATransform(s->h, wordblock);
  148. s->blkused = 0;
  149. }
  150. #ifdef MPEXT
  151. if (len > 0)
  152. #endif
  153. memcpy(s->block, q, len);
  154. s->blkused = len;
  155. }
  156. }
  157. void SHA_Final(SHA_State * s, unsigned char *output)
  158. {
  159. int i;
  160. int pad;
  161. unsigned char c[64];
  162. uint32 lenhi, lenlo;
  163. if (s->blkused >= 56)
  164. pad = 56 + 64 - s->blkused;
  165. else
  166. pad = 56 - s->blkused;
  167. lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3));
  168. lenlo = (s->lenlo << 3);
  169. memset(c, 0, pad);
  170. c[0] = 0x80;
  171. SHA_Bytes(s, &c, pad);
  172. c[0] = (lenhi >> 24) & 0xFF;
  173. c[1] = (lenhi >> 16) & 0xFF;
  174. c[2] = (lenhi >> 8) & 0xFF;
  175. c[3] = (lenhi >> 0) & 0xFF;
  176. c[4] = (lenlo >> 24) & 0xFF;
  177. c[5] = (lenlo >> 16) & 0xFF;
  178. c[6] = (lenlo >> 8) & 0xFF;
  179. c[7] = (lenlo >> 0) & 0xFF;
  180. SHA_Bytes(s, &c, 8);
  181. for (i = 0; i < 5; i++) {
  182. output[i * 4] = (s->h[i] >> 24) & 0xFF;
  183. output[i * 4 + 1] = (s->h[i] >> 16) & 0xFF;
  184. output[i * 4 + 2] = (s->h[i] >> 8) & 0xFF;
  185. output[i * 4 + 3] = (s->h[i]) & 0xFF;
  186. }
  187. }
  188. void SHA_Simple(const void *p, int len, unsigned char *output)
  189. {
  190. SHA_State s;
  191. SHA_Init(&s);
  192. SHA_Bytes(&s, p, len);
  193. SHA_Final(&s, output);
  194. smemclr(&s, sizeof(s));
  195. }
  196. /*
  197. * Thin abstraction for things where hashes are pluggable.
  198. */
  199. static void *sha1_init(void)
  200. {
  201. SHA_State *s;
  202. s = snew(SHA_State);
  203. SHA_Init(s);
  204. return s;
  205. }
  206. static void *sha1_copy(const void *vold)
  207. {
  208. const SHA_State *old = (const SHA_State *)vold;
  209. SHA_State *s;
  210. s = snew(SHA_State);
  211. *s = *old;
  212. return s;
  213. }
  214. static void sha1_free(void *handle)
  215. {
  216. SHA_State *s = handle;
  217. smemclr(s, sizeof(*s));
  218. sfree(s);
  219. }
  220. static void sha1_bytes(void *handle, const void *p, int len)
  221. {
  222. SHA_State *s = handle;
  223. SHA_Bytes(s, p, len);
  224. }
  225. static void sha1_final(void *handle, unsigned char *output)
  226. {
  227. SHA_State *s = handle;
  228. SHA_Final(s, output);
  229. sha1_free(s);
  230. }
  231. const struct ssh_hash ssh_sha1 = {
  232. sha1_init, sha1_copy, sha1_bytes, sha1_final, sha1_free, 20, "SHA-1"
  233. };
  234. /* ----------------------------------------------------------------------
  235. * The above is the SHA-1 algorithm itself. Now we implement the
  236. * HMAC wrapper on it.
  237. */
  238. static void *sha1_make_context(void *cipher_ctx)
  239. {
  240. return snewn(3, SHA_State);
  241. }
  242. static void sha1_free_context(void *handle)
  243. {
  244. smemclr(handle, 3 * sizeof(SHA_State));
  245. sfree(handle);
  246. }
  247. static void sha1_key_internal(void *handle, unsigned char *key, int len)
  248. {
  249. SHA_State *keys = (SHA_State *)handle;
  250. unsigned char foo[64];
  251. int i;
  252. memset(foo, 0x36, 64);
  253. for (i = 0; i < len && i < 64; i++)
  254. foo[i] ^= key[i];
  255. SHA_Init(&keys[0]);
  256. SHA_Bytes(&keys[0], foo, 64);
  257. memset(foo, 0x5C, 64);
  258. for (i = 0; i < len && i < 64; i++)
  259. foo[i] ^= key[i];
  260. SHA_Init(&keys[1]);
  261. SHA_Bytes(&keys[1], foo, 64);
  262. smemclr(foo, 64); /* burn the evidence */
  263. }
  264. static void sha1_key(void *handle, unsigned char *key)
  265. {
  266. sha1_key_internal(handle, key, 20);
  267. }
  268. static void sha1_key_buggy(void *handle, unsigned char *key)
  269. {
  270. sha1_key_internal(handle, key, 16);
  271. }
  272. static void hmacsha1_start(void *handle)
  273. {
  274. SHA_State *keys = (SHA_State *)handle;
  275. keys[2] = keys[0]; /* structure copy */
  276. }
  277. static void hmacsha1_bytes(void *handle, unsigned char const *blk, int len)
  278. {
  279. SHA_State *keys = (SHA_State *)handle;
  280. SHA_Bytes(&keys[2], (void *)blk, len);
  281. }
  282. static void hmacsha1_genresult(void *handle, unsigned char *hmac)
  283. {
  284. SHA_State *keys = (SHA_State *)handle;
  285. SHA_State s;
  286. unsigned char intermediate[20];
  287. s = keys[2]; /* structure copy */
  288. SHA_Final(&s, intermediate);
  289. s = keys[1]; /* structure copy */
  290. SHA_Bytes(&s, intermediate, 20);
  291. SHA_Final(&s, hmac);
  292. }
  293. static void sha1_do_hmac(void *handle, unsigned char *blk, int len,
  294. unsigned long seq, unsigned char *hmac)
  295. {
  296. unsigned char seqbuf[4];
  297. PUT_32BIT_MSB_FIRST(seqbuf, seq);
  298. hmacsha1_start(handle);
  299. hmacsha1_bytes(handle, seqbuf, 4);
  300. hmacsha1_bytes(handle, blk, len);
  301. hmacsha1_genresult(handle, hmac);
  302. }
  303. static void sha1_generate(void *handle, unsigned char *blk, int len,
  304. unsigned long seq)
  305. {
  306. sha1_do_hmac(handle, blk, len, seq, blk + len);
  307. }
  308. static int hmacsha1_verresult(void *handle, unsigned char const *hmac)
  309. {
  310. unsigned char correct[20];
  311. hmacsha1_genresult(handle, correct);
  312. return smemeq(correct, hmac, 20);
  313. }
  314. static int sha1_verify(void *handle, unsigned char *blk, int len,
  315. unsigned long seq)
  316. {
  317. unsigned char correct[20];
  318. sha1_do_hmac(handle, blk, len, seq, correct);
  319. return smemeq(correct, blk + len, 20);
  320. }
  321. static void hmacsha1_96_genresult(void *handle, unsigned char *hmac)
  322. {
  323. unsigned char full[20];
  324. hmacsha1_genresult(handle, full);
  325. memcpy(hmac, full, 12);
  326. }
  327. static void sha1_96_generate(void *handle, unsigned char *blk, int len,
  328. unsigned long seq)
  329. {
  330. unsigned char full[20];
  331. sha1_do_hmac(handle, blk, len, seq, full);
  332. memcpy(blk + len, full, 12);
  333. }
  334. static int hmacsha1_96_verresult(void *handle, unsigned char const *hmac)
  335. {
  336. unsigned char correct[20];
  337. hmacsha1_genresult(handle, correct);
  338. return smemeq(correct, hmac, 12);
  339. }
  340. static int sha1_96_verify(void *handle, unsigned char *blk, int len,
  341. unsigned long seq)
  342. {
  343. unsigned char correct[20];
  344. sha1_do_hmac(handle, blk, len, seq, correct);
  345. return smemeq(correct, blk + len, 12);
  346. }
  347. void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
  348. unsigned char *output) {
  349. SHA_State states[2];
  350. unsigned char intermediate[20];
  351. sha1_key_internal(states, key, keylen);
  352. SHA_Bytes(&states[0], data, datalen);
  353. SHA_Final(&states[0], intermediate);
  354. SHA_Bytes(&states[1], intermediate, 20);
  355. SHA_Final(&states[1], output);
  356. }
  357. const struct ssh_mac ssh_hmac_sha1 = {
  358. sha1_make_context, sha1_free_context, sha1_key,
  359. sha1_generate, sha1_verify,
  360. hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
  361. "hmac-sha1", "[email protected]",
  362. 20, 20,
  363. "HMAC-SHA1"
  364. };
  365. const struct ssh_mac ssh_hmac_sha1_96 = {
  366. sha1_make_context, sha1_free_context, sha1_key,
  367. sha1_96_generate, sha1_96_verify,
  368. hmacsha1_start, hmacsha1_bytes,
  369. hmacsha1_96_genresult, hmacsha1_96_verresult,
  370. "hmac-sha1-96", "[email protected]",
  371. 12, 20,
  372. "HMAC-SHA1-96"
  373. };
  374. const struct ssh_mac ssh_hmac_sha1_buggy = {
  375. sha1_make_context, sha1_free_context, sha1_key_buggy,
  376. sha1_generate, sha1_verify,
  377. hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
  378. "hmac-sha1", NULL,
  379. 20, 16,
  380. "bug-compatible HMAC-SHA1"
  381. };
  382. const struct ssh_mac ssh_hmac_sha1_96_buggy = {
  383. sha1_make_context, sha1_free_context, sha1_key_buggy,
  384. sha1_96_generate, sha1_96_verify,
  385. hmacsha1_start, hmacsha1_bytes,
  386. hmacsha1_96_genresult, hmacsha1_96_verresult,
  387. "hmac-sha1-96", NULL,
  388. 12, 16,
  389. "bug-compatible HMAC-SHA1-96"
  390. };
  391. #ifdef MPEXT
  392. #include "puttyexp.h"
  393. void call_sha1_key_internal(void * handle, unsigned char * key, int len)
  394. {
  395. sha1_key_internal(handle, key, len);
  396. }
  397. #endif