sshmd5.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. typedef struct {
  8. uint32_t h[4];
  9. } MD5_Core_State;
  10. struct MD5Context {
  11. MD5_Core_State core;
  12. unsigned char block[64];
  13. int blkused;
  14. uint64_t len;
  15. BinarySink_IMPLEMENTATION;
  16. };
  17. /* ----------------------------------------------------------------------
  18. * Core MD5 algorithm: processes 16-word blocks into a message digest.
  19. */
  20. #define F(x,y,z) ( ((x) & (y)) | ((~(x)) & (z)) )
  21. #define G(x,y,z) ( ((x) & (z)) | ((~(z)) & (y)) )
  22. #define H(x,y,z) ( (x) ^ (y) ^ (z) )
  23. #define I(x,y,z) ( (y) ^ ( (x) | ~(z) ) )
  24. #define rol(x,y) ( ((x) << (y)) | (((uint32_t)x) >> (32-y)) )
  25. #define subround(f,w,x,y,z,k,s,ti) \
  26. w = x + rol(w + f(x,y,z) + block[k] + ti, s)
  27. static void MD5_Core_Init(MD5_Core_State * s)
  28. {
  29. s->h[0] = 0x67452301;
  30. s->h[1] = 0xefcdab89;
  31. s->h[2] = 0x98badcfe;
  32. s->h[3] = 0x10325476;
  33. }
  34. static void MD5_Block(MD5_Core_State *s, uint32_t *block)
  35. {
  36. uint32_t a, b, c, d;
  37. a = s->h[0];
  38. b = s->h[1];
  39. c = s->h[2];
  40. d = s->h[3];
  41. subround(F, a, b, c, d, 0, 7, 0xd76aa478);
  42. subround(F, d, a, b, c, 1, 12, 0xe8c7b756);
  43. subround(F, c, d, a, b, 2, 17, 0x242070db);
  44. subround(F, b, c, d, a, 3, 22, 0xc1bdceee);
  45. subround(F, a, b, c, d, 4, 7, 0xf57c0faf);
  46. subround(F, d, a, b, c, 5, 12, 0x4787c62a);
  47. subround(F, c, d, a, b, 6, 17, 0xa8304613);
  48. subround(F, b, c, d, a, 7, 22, 0xfd469501);
  49. subround(F, a, b, c, d, 8, 7, 0x698098d8);
  50. subround(F, d, a, b, c, 9, 12, 0x8b44f7af);
  51. subround(F, c, d, a, b, 10, 17, 0xffff5bb1);
  52. subround(F, b, c, d, a, 11, 22, 0x895cd7be);
  53. subround(F, a, b, c, d, 12, 7, 0x6b901122);
  54. subround(F, d, a, b, c, 13, 12, 0xfd987193);
  55. subround(F, c, d, a, b, 14, 17, 0xa679438e);
  56. subround(F, b, c, d, a, 15, 22, 0x49b40821);
  57. subround(G, a, b, c, d, 1, 5, 0xf61e2562);
  58. subround(G, d, a, b, c, 6, 9, 0xc040b340);
  59. subround(G, c, d, a, b, 11, 14, 0x265e5a51);
  60. subround(G, b, c, d, a, 0, 20, 0xe9b6c7aa);
  61. subround(G, a, b, c, d, 5, 5, 0xd62f105d);
  62. subround(G, d, a, b, c, 10, 9, 0x02441453);
  63. subround(G, c, d, a, b, 15, 14, 0xd8a1e681);
  64. subround(G, b, c, d, a, 4, 20, 0xe7d3fbc8);
  65. subround(G, a, b, c, d, 9, 5, 0x21e1cde6);
  66. subround(G, d, a, b, c, 14, 9, 0xc33707d6);
  67. subround(G, c, d, a, b, 3, 14, 0xf4d50d87);
  68. subround(G, b, c, d, a, 8, 20, 0x455a14ed);
  69. subround(G, a, b, c, d, 13, 5, 0xa9e3e905);
  70. subround(G, d, a, b, c, 2, 9, 0xfcefa3f8);
  71. subround(G, c, d, a, b, 7, 14, 0x676f02d9);
  72. subround(G, b, c, d, a, 12, 20, 0x8d2a4c8a);
  73. subround(H, a, b, c, d, 5, 4, 0xfffa3942);
  74. subround(H, d, a, b, c, 8, 11, 0x8771f681);
  75. subround(H, c, d, a, b, 11, 16, 0x6d9d6122);
  76. subround(H, b, c, d, a, 14, 23, 0xfde5380c);
  77. subround(H, a, b, c, d, 1, 4, 0xa4beea44);
  78. subround(H, d, a, b, c, 4, 11, 0x4bdecfa9);
  79. subround(H, c, d, a, b, 7, 16, 0xf6bb4b60);
  80. subround(H, b, c, d, a, 10, 23, 0xbebfbc70);
  81. subround(H, a, b, c, d, 13, 4, 0x289b7ec6);
  82. subround(H, d, a, b, c, 0, 11, 0xeaa127fa);
  83. subround(H, c, d, a, b, 3, 16, 0xd4ef3085);
  84. subround(H, b, c, d, a, 6, 23, 0x04881d05);
  85. subround(H, a, b, c, d, 9, 4, 0xd9d4d039);
  86. subround(H, d, a, b, c, 12, 11, 0xe6db99e5);
  87. subround(H, c, d, a, b, 15, 16, 0x1fa27cf8);
  88. subround(H, b, c, d, a, 2, 23, 0xc4ac5665);
  89. subround(I, a, b, c, d, 0, 6, 0xf4292244);
  90. subround(I, d, a, b, c, 7, 10, 0x432aff97);
  91. subround(I, c, d, a, b, 14, 15, 0xab9423a7);
  92. subround(I, b, c, d, a, 5, 21, 0xfc93a039);
  93. subround(I, a, b, c, d, 12, 6, 0x655b59c3);
  94. subround(I, d, a, b, c, 3, 10, 0x8f0ccc92);
  95. subround(I, c, d, a, b, 10, 15, 0xffeff47d);
  96. subround(I, b, c, d, a, 1, 21, 0x85845dd1);
  97. subround(I, a, b, c, d, 8, 6, 0x6fa87e4f);
  98. subround(I, d, a, b, c, 15, 10, 0xfe2ce6e0);
  99. subround(I, c, d, a, b, 6, 15, 0xa3014314);
  100. subround(I, b, c, d, a, 13, 21, 0x4e0811a1);
  101. subround(I, a, b, c, d, 4, 6, 0xf7537e82);
  102. subround(I, d, a, b, c, 11, 10, 0xbd3af235);
  103. subround(I, c, d, a, b, 2, 15, 0x2ad7d2bb);
  104. subround(I, b, c, d, a, 9, 21, 0xeb86d391);
  105. s->h[0] += a;
  106. s->h[1] += b;
  107. s->h[2] += c;
  108. s->h[3] += d;
  109. }
  110. /* ----------------------------------------------------------------------
  111. * Outer MD5 algorithm: take an arbitrary length byte string,
  112. * convert it into 16-word blocks with the prescribed padding at
  113. * the end, and pass those blocks to the core MD5 algorithm.
  114. */
  115. #define BLKSIZE 64
  116. static void MD5_BinarySink_write(BinarySink *bs, const void *data, size_t len);
  117. void MD5Init(struct MD5Context *s)
  118. {
  119. MD5_Core_Init(&s->core);
  120. s->blkused = 0;
  121. s->len = 0;
  122. BinarySink_INIT(s, MD5_BinarySink_write);
  123. }
  124. static void MD5_BinarySink_write(BinarySink *bs, const void *data, size_t len)
  125. {
  126. struct MD5Context *s = BinarySink_DOWNCAST(bs, struct MD5Context);
  127. const unsigned char *q = (const unsigned char *)data;
  128. uint32_t wordblock[16];
  129. uint32_t lenw = len;
  130. int i;
  131. assert(lenw == len);
  132. /*
  133. * Update the length field.
  134. */
  135. s->len += lenw;
  136. if (s->blkused + len < BLKSIZE) {
  137. /*
  138. * Trivial case: just add to the block.
  139. */
  140. memcpy(s->block + s->blkused, q, len);
  141. s->blkused += len;
  142. } else {
  143. /*
  144. * We must complete and process at least one block.
  145. */
  146. while (s->blkused + len >= BLKSIZE) {
  147. memcpy(s->block + s->blkused, q, BLKSIZE - s->blkused);
  148. q += BLKSIZE - s->blkused;
  149. len -= BLKSIZE - s->blkused;
  150. /* Now process the block. Gather bytes little-endian into words */
  151. for (i = 0; i < 16; i++) {
  152. wordblock[i] =
  153. (((uint32_t) s->block[i * 4 + 3]) << 24) |
  154. (((uint32_t) s->block[i * 4 + 2]) << 16) |
  155. (((uint32_t) s->block[i * 4 + 1]) << 8) |
  156. (((uint32_t) s->block[i * 4 + 0]) << 0);
  157. }
  158. MD5_Block(&s->core, wordblock);
  159. s->blkused = 0;
  160. }
  161. #ifdef MPEXT
  162. if (len > 0)
  163. #endif
  164. memcpy(s->block, q, len);
  165. s->blkused = len;
  166. }
  167. }
  168. void MD5Final(unsigned char output[16], struct MD5Context *s)
  169. {
  170. int i;
  171. unsigned pad;
  172. unsigned char c[64];
  173. uint64_t len;
  174. if (s->blkused >= 56)
  175. pad = 56 + 64 - s->blkused;
  176. else
  177. pad = 56 - s->blkused;
  178. len = (s->len << 3);
  179. memset(c, 0, pad);
  180. c[0] = 0x80;
  181. put_data(s, c, pad);
  182. PUT_64BIT_LSB_FIRST(c, len);
  183. put_data(s, c, 8);
  184. for (i = 0; i < 4; i++) {
  185. output[4 * i + 3] = (s->core.h[i] >> 24) & 0xFF;
  186. output[4 * i + 2] = (s->core.h[i] >> 16) & 0xFF;
  187. output[4 * i + 1] = (s->core.h[i] >> 8) & 0xFF;
  188. output[4 * i + 0] = (s->core.h[i] >> 0) & 0xFF;
  189. }
  190. }
  191. void MD5Simple(void const *p, unsigned len, unsigned char output[16])
  192. {
  193. struct MD5Context s;
  194. MD5Init(&s);
  195. put_data(&s, (unsigned char const *)p, len);
  196. MD5Final(output, &s);
  197. smemclr(&s, sizeof(s));
  198. }
  199. /* ----------------------------------------------------------------------
  200. * Thin abstraction for things where hashes are pluggable.
  201. */
  202. struct md5_hash {
  203. struct MD5Context state;
  204. ssh_hash hash;
  205. };
  206. static ssh_hash *md5_new(const ssh_hashalg *alg)
  207. {
  208. struct md5_hash *h = snew(struct md5_hash);
  209. MD5Init(&h->state);
  210. h->hash.vt = alg;
  211. BinarySink_DELEGATE_INIT(&h->hash, &h->state);
  212. return &h->hash;
  213. }
  214. static ssh_hash *md5_copy(ssh_hash *hashold)
  215. {
  216. struct md5_hash *hold, *hnew;
  217. ssh_hash *hashnew = md5_new(hashold->vt);
  218. hold = container_of(hashold, struct md5_hash, hash);
  219. hnew = container_of(hashnew, struct md5_hash, hash);
  220. hnew->state = hold->state;
  221. BinarySink_COPIED(&hnew->state);
  222. return hashnew;
  223. }
  224. static void md5_free(ssh_hash *hash)
  225. {
  226. struct md5_hash *h = container_of(hash, struct md5_hash, hash);
  227. smemclr(h, sizeof(*h));
  228. sfree(h);
  229. }
  230. static void md5_final(ssh_hash *hash, unsigned char *output)
  231. {
  232. struct md5_hash *h = container_of(hash, struct md5_hash, hash);
  233. MD5Final(output, &h->state);
  234. md5_free(hash);
  235. }
  236. const ssh_hashalg ssh_md5 = {
  237. md5_new, md5_copy, md5_final, md5_free, 16, 64, HASHALG_NAMES_BARE("MD5"),
  238. };