sshmd5.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "ssh.h"
  2. /*
  3. * MD5 implementation for PuTTY. Written directly from the spec by
  4. * Simon Tatham.
  5. */
  6. /* ----------------------------------------------------------------------
  7. * Core MD5 algorithm: processes 16-word blocks into a message digest.
  8. */
  9. #define F(x,y,z) ( ((x) & (y)) | ((~(x)) & (z)) )
  10. #define G(x,y,z) ( ((x) & (z)) | ((~(z)) & (y)) )
  11. #define H(x,y,z) ( (x) ^ (y) ^ (z) )
  12. #define I(x,y,z) ( (y) ^ ( (x) | ~(z) ) )
  13. #define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) )
  14. #define subround(f,w,x,y,z,k,s,ti) \
  15. w = x + rol(w + f(x,y,z) + block[k] + ti, s)
  16. static void MD5_Core_Init(MD5_Core_State * s)
  17. {
  18. s->h[0] = 0x67452301;
  19. s->h[1] = 0xefcdab89;
  20. s->h[2] = 0x98badcfe;
  21. s->h[3] = 0x10325476;
  22. }
  23. static void MD5_Block(MD5_Core_State * s, uint32 * block)
  24. {
  25. uint32 a, b, c, d;
  26. a = s->h[0];
  27. b = s->h[1];
  28. c = s->h[2];
  29. d = s->h[3];
  30. subround(F, a, b, c, d, 0, 7, 0xd76aa478);
  31. subround(F, d, a, b, c, 1, 12, 0xe8c7b756);
  32. subround(F, c, d, a, b, 2, 17, 0x242070db);
  33. subround(F, b, c, d, a, 3, 22, 0xc1bdceee);
  34. subround(F, a, b, c, d, 4, 7, 0xf57c0faf);
  35. subround(F, d, a, b, c, 5, 12, 0x4787c62a);
  36. subround(F, c, d, a, b, 6, 17, 0xa8304613);
  37. subround(F, b, c, d, a, 7, 22, 0xfd469501);
  38. subround(F, a, b, c, d, 8, 7, 0x698098d8);
  39. subround(F, d, a, b, c, 9, 12, 0x8b44f7af);
  40. subround(F, c, d, a, b, 10, 17, 0xffff5bb1);
  41. subround(F, b, c, d, a, 11, 22, 0x895cd7be);
  42. subround(F, a, b, c, d, 12, 7, 0x6b901122);
  43. subround(F, d, a, b, c, 13, 12, 0xfd987193);
  44. subround(F, c, d, a, b, 14, 17, 0xa679438e);
  45. subround(F, b, c, d, a, 15, 22, 0x49b40821);
  46. subround(G, a, b, c, d, 1, 5, 0xf61e2562);
  47. subround(G, d, a, b, c, 6, 9, 0xc040b340);
  48. subround(G, c, d, a, b, 11, 14, 0x265e5a51);
  49. subround(G, b, c, d, a, 0, 20, 0xe9b6c7aa);
  50. subround(G, a, b, c, d, 5, 5, 0xd62f105d);
  51. subround(G, d, a, b, c, 10, 9, 0x02441453);
  52. subround(G, c, d, a, b, 15, 14, 0xd8a1e681);
  53. subround(G, b, c, d, a, 4, 20, 0xe7d3fbc8);
  54. subround(G, a, b, c, d, 9, 5, 0x21e1cde6);
  55. subround(G, d, a, b, c, 14, 9, 0xc33707d6);
  56. subround(G, c, d, a, b, 3, 14, 0xf4d50d87);
  57. subround(G, b, c, d, a, 8, 20, 0x455a14ed);
  58. subround(G, a, b, c, d, 13, 5, 0xa9e3e905);
  59. subround(G, d, a, b, c, 2, 9, 0xfcefa3f8);
  60. subround(G, c, d, a, b, 7, 14, 0x676f02d9);
  61. subround(G, b, c, d, a, 12, 20, 0x8d2a4c8a);
  62. subround(H, a, b, c, d, 5, 4, 0xfffa3942);
  63. subround(H, d, a, b, c, 8, 11, 0x8771f681);
  64. subround(H, c, d, a, b, 11, 16, 0x6d9d6122);
  65. subround(H, b, c, d, a, 14, 23, 0xfde5380c);
  66. subround(H, a, b, c, d, 1, 4, 0xa4beea44);
  67. subround(H, d, a, b, c, 4, 11, 0x4bdecfa9);
  68. subround(H, c, d, a, b, 7, 16, 0xf6bb4b60);
  69. subround(H, b, c, d, a, 10, 23, 0xbebfbc70);
  70. subround(H, a, b, c, d, 13, 4, 0x289b7ec6);
  71. subround(H, d, a, b, c, 0, 11, 0xeaa127fa);
  72. subround(H, c, d, a, b, 3, 16, 0xd4ef3085);
  73. subround(H, b, c, d, a, 6, 23, 0x04881d05);
  74. subround(H, a, b, c, d, 9, 4, 0xd9d4d039);
  75. subround(H, d, a, b, c, 12, 11, 0xe6db99e5);
  76. subround(H, c, d, a, b, 15, 16, 0x1fa27cf8);
  77. subround(H, b, c, d, a, 2, 23, 0xc4ac5665);
  78. subround(I, a, b, c, d, 0, 6, 0xf4292244);
  79. subround(I, d, a, b, c, 7, 10, 0x432aff97);
  80. subround(I, c, d, a, b, 14, 15, 0xab9423a7);
  81. subround(I, b, c, d, a, 5, 21, 0xfc93a039);
  82. subround(I, a, b, c, d, 12, 6, 0x655b59c3);
  83. subround(I, d, a, b, c, 3, 10, 0x8f0ccc92);
  84. subround(I, c, d, a, b, 10, 15, 0xffeff47d);
  85. subround(I, b, c, d, a, 1, 21, 0x85845dd1);
  86. subround(I, a, b, c, d, 8, 6, 0x6fa87e4f);
  87. subround(I, d, a, b, c, 15, 10, 0xfe2ce6e0);
  88. subround(I, c, d, a, b, 6, 15, 0xa3014314);
  89. subround(I, b, c, d, a, 13, 21, 0x4e0811a1);
  90. subround(I, a, b, c, d, 4, 6, 0xf7537e82);
  91. subround(I, d, a, b, c, 11, 10, 0xbd3af235);
  92. subround(I, c, d, a, b, 2, 15, 0x2ad7d2bb);
  93. subround(I, b, c, d, a, 9, 21, 0xeb86d391);
  94. s->h[0] += a;
  95. s->h[1] += b;
  96. s->h[2] += c;
  97. s->h[3] += d;
  98. }
  99. /* ----------------------------------------------------------------------
  100. * Outer MD5 algorithm: take an arbitrary length byte string,
  101. * convert it into 16-word blocks with the prescribed padding at
  102. * the end, and pass those blocks to the core MD5 algorithm.
  103. */
  104. #define BLKSIZE 64
  105. void MD5Init(struct MD5Context *s)
  106. {
  107. MD5_Core_Init(&s->core);
  108. s->blkused = 0;
  109. s->lenhi = s->lenlo = 0;
  110. }
  111. void MD5Update(struct MD5Context *s, unsigned char const *p, unsigned len)
  112. {
  113. unsigned char *q = (unsigned char *) p;
  114. uint32 wordblock[16];
  115. uint32 lenw = len;
  116. int i;
  117. /*
  118. * Update the length field.
  119. */
  120. s->lenlo += lenw;
  121. s->lenhi += (s->lenlo < lenw);
  122. if (s->blkused + len < BLKSIZE) {
  123. /*
  124. * Trivial case: just add to the block.
  125. */
  126. memcpy(s->block + s->blkused, q, len);
  127. s->blkused += len;
  128. } else {
  129. /*
  130. * We must complete and process at least one block.
  131. */
  132. while (s->blkused + len >= BLKSIZE) {
  133. memcpy(s->block + s->blkused, q, BLKSIZE - s->blkused);
  134. q += BLKSIZE - s->blkused;
  135. len -= BLKSIZE - s->blkused;
  136. /* Now process the block. Gather bytes little-endian into words */
  137. for (i = 0; i < 16; i++) {
  138. wordblock[i] =
  139. (((uint32) s->block[i * 4 + 3]) << 24) |
  140. (((uint32) s->block[i * 4 + 2]) << 16) |
  141. (((uint32) s->block[i * 4 + 1]) << 8) |
  142. (((uint32) s->block[i * 4 + 0]) << 0);
  143. }
  144. MD5_Block(&s->core, wordblock);
  145. s->blkused = 0;
  146. }
  147. memcpy(s->block, q, len);
  148. s->blkused = len;
  149. }
  150. }
  151. void MD5Final(unsigned char output[16], struct MD5Context *s)
  152. {
  153. int i;
  154. unsigned pad;
  155. unsigned char c[64];
  156. uint32 lenhi, lenlo;
  157. if (s->blkused >= 56)
  158. pad = 56 + 64 - s->blkused;
  159. else
  160. pad = 56 - s->blkused;
  161. lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3));
  162. lenlo = (s->lenlo << 3);
  163. memset(c, 0, pad);
  164. c[0] = 0x80;
  165. MD5Update(s, c, pad);
  166. c[7] = (lenhi >> 24) & 0xFF;
  167. c[6] = (lenhi >> 16) & 0xFF;
  168. c[5] = (lenhi >> 8) & 0xFF;
  169. c[4] = (lenhi >> 0) & 0xFF;
  170. c[3] = (lenlo >> 24) & 0xFF;
  171. c[2] = (lenlo >> 16) & 0xFF;
  172. c[1] = (lenlo >> 8) & 0xFF;
  173. c[0] = (lenlo >> 0) & 0xFF;
  174. MD5Update(s, c, 8);
  175. for (i = 0; i < 4; i++) {
  176. output[4 * i + 3] = (s->core.h[i] >> 24) & 0xFF;
  177. output[4 * i + 2] = (s->core.h[i] >> 16) & 0xFF;
  178. output[4 * i + 1] = (s->core.h[i] >> 8) & 0xFF;
  179. output[4 * i + 0] = (s->core.h[i] >> 0) & 0xFF;
  180. }
  181. }
  182. void MD5Simple(void const *p, unsigned len, unsigned char output[16])
  183. {
  184. struct MD5Context s;
  185. MD5Init(&s);
  186. MD5Update(&s, (unsigned char const *)p, len);
  187. MD5Final(output, &s);
  188. smemclr(&s, sizeof(s));
  189. }
  190. /* ----------------------------------------------------------------------
  191. * The above is the MD5 algorithm itself. Now we implement the
  192. * HMAC wrapper on it.
  193. *
  194. * Some of these functions are exported directly, because they are
  195. * useful elsewhere (SOCKS5 CHAP authentication uses HMAC-MD5).
  196. */
  197. void *hmacmd5_make_context(void *cipher_ctx)
  198. {
  199. return snewn(3, struct MD5Context);
  200. }
  201. void hmacmd5_free_context(void *handle)
  202. {
  203. smemclr(handle, 3*sizeof(struct MD5Context));
  204. sfree(handle);
  205. }
  206. void hmacmd5_key(void *handle, void const *keyv, int len)
  207. {
  208. struct MD5Context *keys = (struct MD5Context *)handle;
  209. unsigned char foo[64];
  210. unsigned char const *key = (unsigned char const *)keyv;
  211. int i;
  212. memset(foo, 0x36, 64);
  213. for (i = 0; i < len && i < 64; i++)
  214. foo[i] ^= key[i];
  215. MD5Init(&keys[0]);
  216. MD5Update(&keys[0], foo, 64);
  217. memset(foo, 0x5C, 64);
  218. for (i = 0; i < len && i < 64; i++)
  219. foo[i] ^= key[i];
  220. MD5Init(&keys[1]);
  221. MD5Update(&keys[1], foo, 64);
  222. smemclr(foo, 64); /* burn the evidence */
  223. }
  224. static void hmacmd5_key_16(void *handle, unsigned char *key)
  225. {
  226. hmacmd5_key(handle, key, 16);
  227. }
  228. static void hmacmd5_start(void *handle)
  229. {
  230. struct MD5Context *keys = (struct MD5Context *)handle;
  231. keys[2] = keys[0]; /* structure copy */
  232. }
  233. static void hmacmd5_bytes(void *handle, unsigned char const *blk, int len)
  234. {
  235. struct MD5Context *keys = (struct MD5Context *)handle;
  236. MD5Update(&keys[2], blk, len);
  237. }
  238. static void hmacmd5_genresult(void *handle, unsigned char *hmac)
  239. {
  240. struct MD5Context *keys = (struct MD5Context *)handle;
  241. struct MD5Context s;
  242. unsigned char intermediate[16];
  243. s = keys[2]; /* structure copy */
  244. MD5Final(intermediate, &s);
  245. s = keys[1]; /* structure copy */
  246. MD5Update(&s, intermediate, 16);
  247. MD5Final(hmac, &s);
  248. }
  249. static int hmacmd5_verresult(void *handle, unsigned char const *hmac)
  250. {
  251. unsigned char correct[16];
  252. hmacmd5_genresult(handle, correct);
  253. return smemeq(correct, hmac, 16);
  254. }
  255. static void hmacmd5_do_hmac_internal(void *handle,
  256. unsigned char const *blk, int len,
  257. unsigned char const *blk2, int len2,
  258. unsigned char *hmac)
  259. {
  260. hmacmd5_start(handle);
  261. hmacmd5_bytes(handle, blk, len);
  262. if (blk2) hmacmd5_bytes(handle, blk2, len2);
  263. hmacmd5_genresult(handle, hmac);
  264. }
  265. void hmacmd5_do_hmac(void *handle, unsigned char const *blk, int len,
  266. unsigned char *hmac)
  267. {
  268. hmacmd5_do_hmac_internal(handle, blk, len, NULL, 0, hmac);
  269. }
  270. static void hmacmd5_do_hmac_ssh(void *handle, unsigned char const *blk, int len,
  271. unsigned long seq, unsigned char *hmac)
  272. {
  273. unsigned char seqbuf[16];
  274. PUT_32BIT_MSB_FIRST(seqbuf, seq);
  275. hmacmd5_do_hmac_internal(handle, seqbuf, 4, blk, len, hmac);
  276. }
  277. static void hmacmd5_generate(void *handle, unsigned char *blk, int len,
  278. unsigned long seq)
  279. {
  280. hmacmd5_do_hmac_ssh(handle, blk, len, seq, blk + len);
  281. }
  282. static int hmacmd5_verify(void *handle, unsigned char *blk, int len,
  283. unsigned long seq)
  284. {
  285. unsigned char correct[16];
  286. hmacmd5_do_hmac_ssh(handle, blk, len, seq, correct);
  287. return smemeq(correct, blk + len, 16);
  288. }
  289. const struct ssh_mac ssh_hmac_md5 = {
  290. hmacmd5_make_context, hmacmd5_free_context, hmacmd5_key_16,
  291. hmacmd5_generate, hmacmd5_verify,
  292. hmacmd5_start, hmacmd5_bytes, hmacmd5_genresult, hmacmd5_verresult,
  293. "hmac-md5", "[email protected]",
  294. 16, 16,
  295. "HMAC-MD5"
  296. };