sshmd5.c 9.8 KB

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