md5.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * $Id: md5.c,v 1.2 2008/03/24 20:59:12 mascarenhas Exp $
  3. * Hash function MD5
  4. * @author Marcela Ozorio Suarez, Roberto I.
  5. */
  6. #include <string.h>
  7. #include "md5.h"
  8. #define WORD 32
  9. #define MASK 0xFFFFFFFF
  10. #if __STDC_VERSION__ >= 199901L
  11. #include <stdint.h>
  12. typedef uint32_t WORD32;
  13. #else
  14. typedef unsigned int WORD32;
  15. #endif
  16. /**
  17. * md5 hash function.
  18. * @param message: aribtary string.
  19. * @param len: message length.
  20. * @param output: buffer to receive the hash value. Its size must be
  21. * (at least) HASHSIZE.
  22. */
  23. void md5 (const char *message, long len, char *output);
  24. /*
  25. ** Realiza a rotacao no sentido horario dos bits da variavel 'D' do tipo WORD32.
  26. ** Os bits sao deslocados de 'num' posicoes
  27. */
  28. #define rotate(D, num) (D<<num) | (D>>(WORD-num))
  29. /*Macros que definem operacoes relizadas pelo algoritmo md5 */
  30. #define F(x, y, z) (((x) & (y)) | ((~(x)) & (z)))
  31. #define G(x, y, z) (((x) & (z)) | ((y) & (~(z))))
  32. #define H(x, y, z) ((x) ^ (y) ^ (z))
  33. #define I(x, y, z) ((y) ^ ((x) | (~(z))))
  34. /*vetor de numeros utilizados pelo algoritmo md5 para embaralhar bits */
  35. static const WORD32 T[64]={
  36. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  37. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  38. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  39. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  40. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  41. 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
  42. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  43. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  44. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  45. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  46. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
  47. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  48. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  49. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  50. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  51. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
  52. };
  53. static void word32tobytes (const WORD32 *input, char *output) {
  54. int j = 0;
  55. while (j<4*4) {
  56. WORD32 v = *input++;
  57. output[j++] = (char)(v & 0xff); v >>= 8;
  58. output[j++] = (char)(v & 0xff); v >>= 8;
  59. output[j++] = (char)(v & 0xff); v >>= 8;
  60. output[j++] = (char)(v & 0xff);
  61. }
  62. }
  63. static void inic_digest(WORD32 *d) {
  64. d[0] = 0x67452301;
  65. d[1] = 0xEFCDAB89;
  66. d[2] = 0x98BADCFE;
  67. d[3] = 0x10325476;
  68. }
  69. /*funcao que implemeta os quatro passos principais do algoritmo MD5 */
  70. static void digest(const WORD32 *m, WORD32 *d) {
  71. int j;
  72. /*MD5 PASSO1 */
  73. for (j=0; j<4*4; j+=4) {
  74. d[0] = d[0]+ F(d[1], d[2], d[3])+ m[j] + T[j]; d[0]=rotate(d[0], 7);
  75. d[0]+=d[1];
  76. d[3] = d[3]+ F(d[0], d[1], d[2])+ m[(j)+1] + T[j+1]; d[3]=rotate(d[3], 12);
  77. d[3]+=d[0];
  78. d[2] = d[2]+ F(d[3], d[0], d[1])+ m[(j)+2] + T[j+2]; d[2]=rotate(d[2], 17);
  79. d[2]+=d[3];
  80. d[1] = d[1]+ F(d[2], d[3], d[0])+ m[(j)+3] + T[j+3]; d[1]=rotate(d[1], 22);
  81. d[1]+=d[2];
  82. }
  83. /*MD5 PASSO2 */
  84. for (j=0; j<4*4; j+=4) {
  85. d[0] = d[0]+ G(d[1], d[2], d[3])+ m[(5*j+1)&0x0f] + T[(j-1)+17];
  86. d[0] = rotate(d[0],5);
  87. d[0]+=d[1];
  88. d[3] = d[3]+ G(d[0], d[1], d[2])+ m[((5*(j+1)+1)&0x0f)] + T[(j+0)+17];
  89. d[3] = rotate(d[3], 9);
  90. d[3]+=d[0];
  91. d[2] = d[2]+ G(d[3], d[0], d[1])+ m[((5*(j+2)+1)&0x0f)] + T[(j+1)+17];
  92. d[2] = rotate(d[2], 14);
  93. d[2]+=d[3];
  94. d[1] = d[1]+ G(d[2], d[3], d[0])+ m[((5*(j+3)+1)&0x0f)] + T[(j+2)+17];
  95. d[1] = rotate(d[1], 20);
  96. d[1]+=d[2];
  97. }
  98. /*MD5 PASSO3 */
  99. for (j=0; j<4*4; j+=4) {
  100. d[0] = d[0]+ H(d[1], d[2], d[3])+ m[(3*j+5)&0x0f] + T[(j-1)+33];
  101. d[0] = rotate(d[0], 4);
  102. d[0]+=d[1];
  103. d[3] = d[3]+ H(d[0], d[1], d[2])+ m[(3*(j+1)+5)&0x0f] + T[(j+0)+33];
  104. d[3] = rotate(d[3], 11);
  105. d[3]+=d[0];
  106. d[2] = d[2]+ H(d[3], d[0], d[1])+ m[(3*(j+2)+5)&0x0f] + T[(j+1)+33];
  107. d[2] = rotate(d[2], 16);
  108. d[2]+=d[3];
  109. d[1] = d[1]+ H(d[2], d[3], d[0])+ m[(3*(j+3)+5)&0x0f] + T[(j+2)+33];
  110. d[1] = rotate(d[1], 23);
  111. d[1]+=d[2];
  112. }
  113. /*MD5 PASSO4 */
  114. for (j=0; j<4*4; j+=4) {
  115. d[0] = d[0]+ I(d[1], d[2], d[3])+ m[(7*j)&0x0f] + T[(j-1)+49];
  116. d[0] = rotate(d[0], 6);
  117. d[0]+=d[1];
  118. d[3] = d[3]+ I(d[0], d[1], d[2])+ m[(7*(j+1))&0x0f] + T[(j+0)+49];
  119. d[3] = rotate(d[3], 10);
  120. d[3]+=d[0];
  121. d[2] = d[2]+ I(d[3], d[0], d[1])+ m[(7*(j+2))&0x0f] + T[(j+1)+49];
  122. d[2] = rotate(d[2], 15);
  123. d[2]+=d[3];
  124. d[1] = d[1]+ I(d[2], d[3], d[0])+ m[(7*(j+3))&0x0f] + T[(j+2)+49];
  125. d[1] = rotate(d[1], 21);
  126. d[1]+=d[2];
  127. }
  128. }
  129. static void bytestoword32 (WORD32 *x, const char *pt) {
  130. int i;
  131. for (i=0; i<16; i++) {
  132. int j=i*4;
  133. x[i] = (((WORD32)(unsigned char)pt[j+3] << 8 |
  134. (WORD32)(unsigned char)pt[j+2]) << 8 |
  135. (WORD32)(unsigned char)pt[j+1]) << 8 |
  136. (WORD32)(unsigned char)pt[j];
  137. }
  138. }
  139. static void put_length(WORD32 *x, long len) {
  140. /* in bits! */
  141. x[14] = (WORD32)((len<<3) & MASK);
  142. x[15] = (WORD32)(len>>(32-3) & 0x7);
  143. }
  144. /*
  145. ** returned status:
  146. * 0 - normal message (full 64 bytes)
  147. * 1 - enough room for 0x80, but not for message length (two 4-byte words)
  148. * 2 - enough room for 0x80 plus message length (at least 9 bytes free)
  149. */
  150. static int converte (WORD32 *x, const char *pt, int num, int old_status) {
  151. int new_status = 0;
  152. char buff[64];
  153. if (num<64) {
  154. memcpy(buff, pt, num); /* to avoid changing original string */
  155. memset(buff+num, 0, 64-num);
  156. if (old_status == 0)
  157. buff[num] = '\200';
  158. new_status = 1;
  159. pt = buff;
  160. }
  161. bytestoword32(x, pt);
  162. if (num <= (64 - 9))
  163. new_status = 2;
  164. return new_status;
  165. }
  166. void md5 (const char *message, long len, char *output) {
  167. WORD32 d[4];
  168. int status = 0;
  169. long i = 0;
  170. inic_digest(d);
  171. while (status != 2) {
  172. WORD32 d_old[4];
  173. WORD32 wbuff[16];
  174. int numbytes = (len-i >= 64) ? 64 : len-i;
  175. /*salva os valores do vetor digest*/
  176. d_old[0]=d[0]; d_old[1]=d[1]; d_old[2]=d[2]; d_old[3]=d[3];
  177. status = converte(wbuff, message+i, numbytes, status);
  178. if (status == 2) put_length(wbuff, len);
  179. digest(wbuff, d);
  180. d[0]+=d_old[0]; d[1]+=d_old[1]; d[2]+=d_old[2]; d[3]+=d_old[3];
  181. i += numbytes;
  182. }
  183. word32tobytes(d, output);
  184. }