sshsha.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. memcpy(s->block, q, len);
  151. s->blkused = len;
  152. }
  153. }
  154. void SHA_Final(SHA_State * s, unsigned char *output)
  155. {
  156. int i;
  157. int pad;
  158. unsigned char c[64];
  159. uint32 lenhi, lenlo;
  160. if (s->blkused >= 56)
  161. pad = 56 + 64 - s->blkused;
  162. else
  163. pad = 56 - s->blkused;
  164. lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3));
  165. lenlo = (s->lenlo << 3);
  166. memset(c, 0, pad);
  167. c[0] = 0x80;
  168. SHA_Bytes(s, &c, pad);
  169. c[0] = (lenhi >> 24) & 0xFF;
  170. c[1] = (lenhi >> 16) & 0xFF;
  171. c[2] = (lenhi >> 8) & 0xFF;
  172. c[3] = (lenhi >> 0) & 0xFF;
  173. c[4] = (lenlo >> 24) & 0xFF;
  174. c[5] = (lenlo >> 16) & 0xFF;
  175. c[6] = (lenlo >> 8) & 0xFF;
  176. c[7] = (lenlo >> 0) & 0xFF;
  177. SHA_Bytes(s, &c, 8);
  178. for (i = 0; i < 5; i++) {
  179. output[i * 4] = (s->h[i] >> 24) & 0xFF;
  180. output[i * 4 + 1] = (s->h[i] >> 16) & 0xFF;
  181. output[i * 4 + 2] = (s->h[i] >> 8) & 0xFF;
  182. output[i * 4 + 3] = (s->h[i]) & 0xFF;
  183. }
  184. }
  185. void SHA_Simple(const void *p, int len, unsigned char *output)
  186. {
  187. SHA_State s;
  188. SHA_Init(&s);
  189. SHA_Bytes(&s, p, len);
  190. SHA_Final(&s, output);
  191. smemclr(&s, sizeof(s));
  192. }
  193. /*
  194. * Thin abstraction for things where hashes are pluggable.
  195. */
  196. static void *sha1_init(void)
  197. {
  198. SHA_State *s;
  199. s = snew(SHA_State);
  200. SHA_Init(s);
  201. return s;
  202. }
  203. static void *sha1_copy(const void *vold)
  204. {
  205. const SHA_State *old = (const SHA_State *)vold;
  206. SHA_State *s;
  207. s = snew(SHA_State);
  208. *s = *old;
  209. return s;
  210. }
  211. static void sha1_free(void *handle)
  212. {
  213. SHA_State *s = handle;
  214. smemclr(s, sizeof(*s));
  215. sfree(s);
  216. }
  217. static void sha1_bytes(void *handle, const void *p, int len)
  218. {
  219. SHA_State *s = handle;
  220. SHA_Bytes(s, p, len);
  221. }
  222. static void sha1_final(void *handle, unsigned char *output)
  223. {
  224. SHA_State *s = handle;
  225. SHA_Final(s, output);
  226. sha1_free(s);
  227. }
  228. const struct ssh_hash ssh_sha1 = {
  229. sha1_init, sha1_copy, sha1_bytes, sha1_final, sha1_free, 20, "SHA-1"
  230. };
  231. /* ----------------------------------------------------------------------
  232. * The above is the SHA-1 algorithm itself. Now we implement the
  233. * HMAC wrapper on it.
  234. */
  235. static void *sha1_make_context(void *cipher_ctx)
  236. {
  237. return snewn(3, SHA_State);
  238. }
  239. static void sha1_free_context(void *handle)
  240. {
  241. smemclr(handle, 3 * sizeof(SHA_State));
  242. sfree(handle);
  243. }
  244. static void sha1_key_internal(void *handle, unsigned char *key, int len)
  245. {
  246. SHA_State *keys = (SHA_State *)handle;
  247. unsigned char foo[64];
  248. int i;
  249. memset(foo, 0x36, 64);
  250. for (i = 0; i < len && i < 64; i++)
  251. foo[i] ^= key[i];
  252. SHA_Init(&keys[0]);
  253. SHA_Bytes(&keys[0], foo, 64);
  254. memset(foo, 0x5C, 64);
  255. for (i = 0; i < len && i < 64; i++)
  256. foo[i] ^= key[i];
  257. SHA_Init(&keys[1]);
  258. SHA_Bytes(&keys[1], foo, 64);
  259. smemclr(foo, 64); /* burn the evidence */
  260. }
  261. static void sha1_key(void *handle, unsigned char *key)
  262. {
  263. sha1_key_internal(handle, key, 20);
  264. }
  265. static void sha1_key_buggy(void *handle, unsigned char *key)
  266. {
  267. sha1_key_internal(handle, key, 16);
  268. }
  269. static void hmacsha1_start(void *handle)
  270. {
  271. SHA_State *keys = (SHA_State *)handle;
  272. keys[2] = keys[0]; /* structure copy */
  273. }
  274. static void hmacsha1_bytes(void *handle, unsigned char const *blk, int len)
  275. {
  276. SHA_State *keys = (SHA_State *)handle;
  277. SHA_Bytes(&keys[2], (void *)blk, len);
  278. }
  279. static void hmacsha1_genresult(void *handle, unsigned char *hmac)
  280. {
  281. SHA_State *keys = (SHA_State *)handle;
  282. SHA_State s;
  283. unsigned char intermediate[20];
  284. s = keys[2]; /* structure copy */
  285. SHA_Final(&s, intermediate);
  286. s = keys[1]; /* structure copy */
  287. SHA_Bytes(&s, intermediate, 20);
  288. SHA_Final(&s, hmac);
  289. }
  290. static void sha1_do_hmac(void *handle, unsigned char *blk, int len,
  291. unsigned long seq, unsigned char *hmac)
  292. {
  293. unsigned char seqbuf[4];
  294. PUT_32BIT_MSB_FIRST(seqbuf, seq);
  295. hmacsha1_start(handle);
  296. hmacsha1_bytes(handle, seqbuf, 4);
  297. hmacsha1_bytes(handle, blk, len);
  298. hmacsha1_genresult(handle, hmac);
  299. }
  300. static void sha1_generate(void *handle, unsigned char *blk, int len,
  301. unsigned long seq)
  302. {
  303. sha1_do_hmac(handle, blk, len, seq, blk + len);
  304. }
  305. static int hmacsha1_verresult(void *handle, unsigned char const *hmac)
  306. {
  307. unsigned char correct[20];
  308. hmacsha1_genresult(handle, correct);
  309. return smemeq(correct, hmac, 20);
  310. }
  311. static int sha1_verify(void *handle, unsigned char *blk, int len,
  312. unsigned long seq)
  313. {
  314. unsigned char correct[20];
  315. sha1_do_hmac(handle, blk, len, seq, correct);
  316. return smemeq(correct, blk + len, 20);
  317. }
  318. static void hmacsha1_96_genresult(void *handle, unsigned char *hmac)
  319. {
  320. unsigned char full[20];
  321. hmacsha1_genresult(handle, full);
  322. memcpy(hmac, full, 12);
  323. }
  324. static void sha1_96_generate(void *handle, unsigned char *blk, int len,
  325. unsigned long seq)
  326. {
  327. unsigned char full[20];
  328. sha1_do_hmac(handle, blk, len, seq, full);
  329. memcpy(blk + len, full, 12);
  330. }
  331. static int hmacsha1_96_verresult(void *handle, unsigned char const *hmac)
  332. {
  333. unsigned char correct[20];
  334. hmacsha1_genresult(handle, correct);
  335. return smemeq(correct, hmac, 12);
  336. }
  337. static int sha1_96_verify(void *handle, unsigned char *blk, int len,
  338. unsigned long seq)
  339. {
  340. unsigned char correct[20];
  341. sha1_do_hmac(handle, blk, len, seq, correct);
  342. return smemeq(correct, blk + len, 12);
  343. }
  344. void hmac_sha1_simple(void *key, int keylen, void *data, int datalen,
  345. unsigned char *output) {
  346. SHA_State states[2];
  347. unsigned char intermediate[20];
  348. sha1_key_internal(states, key, keylen);
  349. SHA_Bytes(&states[0], data, datalen);
  350. SHA_Final(&states[0], intermediate);
  351. SHA_Bytes(&states[1], intermediate, 20);
  352. SHA_Final(&states[1], output);
  353. }
  354. const struct ssh_mac ssh_hmac_sha1 = {
  355. sha1_make_context, sha1_free_context, sha1_key,
  356. sha1_generate, sha1_verify,
  357. hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
  358. "hmac-sha1", "hmac-sha1-etm@openssh.com",
  359. 20, 20,
  360. "HMAC-SHA1"
  361. };
  362. const struct ssh_mac ssh_hmac_sha1_96 = {
  363. sha1_make_context, sha1_free_context, sha1_key,
  364. sha1_96_generate, sha1_96_verify,
  365. hmacsha1_start, hmacsha1_bytes,
  366. hmacsha1_96_genresult, hmacsha1_96_verresult,
  367. "hmac-sha1-96", "hmac-sha1-96-etm@openssh.com",
  368. 12, 20,
  369. "HMAC-SHA1-96"
  370. };
  371. const struct ssh_mac ssh_hmac_sha1_buggy = {
  372. sha1_make_context, sha1_free_context, sha1_key_buggy,
  373. sha1_generate, sha1_verify,
  374. hmacsha1_start, hmacsha1_bytes, hmacsha1_genresult, hmacsha1_verresult,
  375. "hmac-sha1", NULL,
  376. 20, 16,
  377. "bug-compatible HMAC-SHA1"
  378. };
  379. const struct ssh_mac ssh_hmac_sha1_96_buggy = {
  380. sha1_make_context, sha1_free_context, sha1_key_buggy,
  381. sha1_96_generate, sha1_96_verify,
  382. hmacsha1_start, hmacsha1_bytes,
  383. hmacsha1_96_genresult, hmacsha1_96_verresult,
  384. "hmac-sha1-96", NULL,
  385. 12, 16,
  386. "bug-compatible HMAC-SHA1-96"
  387. };