sshmd5.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. void MD5_Core_Init(MD5_Core_State *s) {
  17. s->h[0] = 0x67452301;
  18. s->h[1] = 0xefcdab89;
  19. s->h[2] = 0x98badcfe;
  20. s->h[3] = 0x10325476;
  21. }
  22. void MD5_Block(MD5_Core_State *s, uint32 *block) {
  23. uint32 a,b,c,d;
  24. a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3];
  25. subround(F, a, b, c, d, 0, 7, 0xd76aa478);
  26. subround(F, d, a, b, c, 1, 12, 0xe8c7b756);
  27. subround(F, c, d, a, b, 2, 17, 0x242070db);
  28. subround(F, b, c, d, a, 3, 22, 0xc1bdceee);
  29. subround(F, a, b, c, d, 4, 7, 0xf57c0faf);
  30. subround(F, d, a, b, c, 5, 12, 0x4787c62a);
  31. subround(F, c, d, a, b, 6, 17, 0xa8304613);
  32. subround(F, b, c, d, a, 7, 22, 0xfd469501);
  33. subround(F, a, b, c, d, 8, 7, 0x698098d8);
  34. subround(F, d, a, b, c, 9, 12, 0x8b44f7af);
  35. subround(F, c, d, a, b, 10, 17, 0xffff5bb1);
  36. subround(F, b, c, d, a, 11, 22, 0x895cd7be);
  37. subround(F, a, b, c, d, 12, 7, 0x6b901122);
  38. subround(F, d, a, b, c, 13, 12, 0xfd987193);
  39. subround(F, c, d, a, b, 14, 17, 0xa679438e);
  40. subround(F, b, c, d, a, 15, 22, 0x49b40821);
  41. subround(G, a, b, c, d, 1, 5, 0xf61e2562);
  42. subround(G, d, a, b, c, 6, 9, 0xc040b340);
  43. subround(G, c, d, a, b, 11, 14, 0x265e5a51);
  44. subround(G, b, c, d, a, 0, 20, 0xe9b6c7aa);
  45. subround(G, a, b, c, d, 5, 5, 0xd62f105d);
  46. subround(G, d, a, b, c, 10, 9, 0x02441453);
  47. subround(G, c, d, a, b, 15, 14, 0xd8a1e681);
  48. subround(G, b, c, d, a, 4, 20, 0xe7d3fbc8);
  49. subround(G, a, b, c, d, 9, 5, 0x21e1cde6);
  50. subround(G, d, a, b, c, 14, 9, 0xc33707d6);
  51. subround(G, c, d, a, b, 3, 14, 0xf4d50d87);
  52. subround(G, b, c, d, a, 8, 20, 0x455a14ed);
  53. subround(G, a, b, c, d, 13, 5, 0xa9e3e905);
  54. subround(G, d, a, b, c, 2, 9, 0xfcefa3f8);
  55. subround(G, c, d, a, b, 7, 14, 0x676f02d9);
  56. subround(G, b, c, d, a, 12, 20, 0x8d2a4c8a);
  57. subround(H, a, b, c, d, 5, 4, 0xfffa3942);
  58. subround(H, d, a, b, c, 8, 11, 0x8771f681);
  59. subround(H, c, d, a, b, 11, 16, 0x6d9d6122);
  60. subround(H, b, c, d, a, 14, 23, 0xfde5380c);
  61. subround(H, a, b, c, d, 1, 4, 0xa4beea44);
  62. subround(H, d, a, b, c, 4, 11, 0x4bdecfa9);
  63. subround(H, c, d, a, b, 7, 16, 0xf6bb4b60);
  64. subround(H, b, c, d, a, 10, 23, 0xbebfbc70);
  65. subround(H, a, b, c, d, 13, 4, 0x289b7ec6);
  66. subround(H, d, a, b, c, 0, 11, 0xeaa127fa);
  67. subround(H, c, d, a, b, 3, 16, 0xd4ef3085);
  68. subround(H, b, c, d, a, 6, 23, 0x04881d05);
  69. subround(H, a, b, c, d, 9, 4, 0xd9d4d039);
  70. subround(H, d, a, b, c, 12, 11, 0xe6db99e5);
  71. subround(H, c, d, a, b, 15, 16, 0x1fa27cf8);
  72. subround(H, b, c, d, a, 2, 23, 0xc4ac5665);
  73. subround(I, a, b, c, d, 0, 6, 0xf4292244);
  74. subround(I, d, a, b, c, 7, 10, 0x432aff97);
  75. subround(I, c, d, a, b, 14, 15, 0xab9423a7);
  76. subround(I, b, c, d, a, 5, 21, 0xfc93a039);
  77. subround(I, a, b, c, d, 12, 6, 0x655b59c3);
  78. subround(I, d, a, b, c, 3, 10, 0x8f0ccc92);
  79. subround(I, c, d, a, b, 10, 15, 0xffeff47d);
  80. subround(I, b, c, d, a, 1, 21, 0x85845dd1);
  81. subround(I, a, b, c, d, 8, 6, 0x6fa87e4f);
  82. subround(I, d, a, b, c, 15, 10, 0xfe2ce6e0);
  83. subround(I, c, d, a, b, 6, 15, 0xa3014314);
  84. subround(I, b, c, d, a, 13, 21, 0x4e0811a1);
  85. subround(I, a, b, c, d, 4, 6, 0xf7537e82);
  86. subround(I, d, a, b, c, 11, 10, 0xbd3af235);
  87. subround(I, c, d, a, b, 2, 15, 0x2ad7d2bb);
  88. subround(I, b, c, d, a, 9, 21, 0xeb86d391);
  89. s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d;
  90. }
  91. /* ----------------------------------------------------------------------
  92. * Outer MD5 algorithm: take an arbitrary length byte string,
  93. * convert it into 16-word blocks with the prescribed padding at
  94. * the end, and pass those blocks to the core MD5 algorithm.
  95. */
  96. #define BLKSIZE 64
  97. void MD5Init(struct MD5Context *s) {
  98. MD5_Core_Init(&s->core);
  99. s->blkused = 0;
  100. s->lenhi = s->lenlo = 0;
  101. }
  102. void MD5Update(struct MD5Context *s, unsigned char const *p,
  103. unsigned len) {
  104. unsigned char *q = (unsigned char *)p;
  105. uint32 wordblock[16];
  106. uint32 lenw = len;
  107. int i;
  108. /*
  109. * Update the length field.
  110. */
  111. s->lenlo += lenw;
  112. s->lenhi += (s->lenlo < lenw);
  113. if (s->blkused+len < BLKSIZE) {
  114. /*
  115. * Trivial case: just add to the block.
  116. */
  117. memcpy(s->block + s->blkused, q, len);
  118. s->blkused += len;
  119. } else {
  120. /*
  121. * We must complete and process at least one block.
  122. */
  123. while (s->blkused + len >= BLKSIZE) {
  124. memcpy(s->block + s->blkused, q, BLKSIZE - s->blkused);
  125. q += BLKSIZE - s->blkused;
  126. len -= BLKSIZE - s->blkused;
  127. /* Now process the block. Gather bytes little-endian into words */
  128. for (i = 0; i < 16; i++) {
  129. wordblock[i] =
  130. ( ((uint32)s->block[i*4+3]) << 24 ) |
  131. ( ((uint32)s->block[i*4+2]) << 16 ) |
  132. ( ((uint32)s->block[i*4+1]) << 8 ) |
  133. ( ((uint32)s->block[i*4+0]) << 0 );
  134. }
  135. MD5_Block(&s->core, wordblock);
  136. s->blkused = 0;
  137. }
  138. memcpy(s->block, q, len);
  139. s->blkused = len;
  140. }
  141. }
  142. void MD5Final(unsigned char output[16], struct MD5Context *s) {
  143. int i;
  144. unsigned pad;
  145. unsigned char c[64];
  146. uint32 lenhi, lenlo;
  147. if (s->blkused >= 56)
  148. pad = 56 + 64 - s->blkused;
  149. else
  150. pad = 56 - s->blkused;
  151. lenhi = (s->lenhi << 3) | (s->lenlo >> (32-3));
  152. lenlo = (s->lenlo << 3);
  153. memset(c, 0, pad);
  154. c[0] = 0x80;
  155. MD5Update(s, c, pad);
  156. c[7] = (lenhi >> 24) & 0xFF;
  157. c[6] = (lenhi >> 16) & 0xFF;
  158. c[5] = (lenhi >> 8) & 0xFF;
  159. c[4] = (lenhi >> 0) & 0xFF;
  160. c[3] = (lenlo >> 24) & 0xFF;
  161. c[2] = (lenlo >> 16) & 0xFF;
  162. c[1] = (lenlo >> 8) & 0xFF;
  163. c[0] = (lenlo >> 0) & 0xFF;
  164. MD5Update(s, c, 8);
  165. for (i = 0; i < 4; i++) {
  166. output[4*i+3] = (s->core.h[i] >> 24) & 0xFF;
  167. output[4*i+2] = (s->core.h[i] >> 16) & 0xFF;
  168. output[4*i+1] = (s->core.h[i] >> 8) & 0xFF;
  169. output[4*i+0] = (s->core.h[i] >> 0) & 0xFF;
  170. }
  171. }