md5-block.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* These are the four functions used in the four steps of the MD5 algorithm
  2. and defined in the RFC 1321. The first function is a little bit optimized
  3. (as found in Colin Plumbs public domain implementation). */
  4. /* #define FF(b, c, d) ((b & c) | (~b & d)) */
  5. #define FF(b, c, d) (d ^ (b & (c ^ d)))
  6. #define FG(b, c, d) FF (d, b, c)
  7. #define FH(b, c, d) (b ^ c ^ d)
  8. #define FI(b, c, d) (c ^ (b | ~d))
  9. /* Process LEN bytes of BUFFER, accumulating context into CTX.
  10. It is assumed that LEN % 64 == 0. */
  11. void
  12. __md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
  13. {
  14. md5_uint32 correct_words[16];
  15. const md5_uint32 *words = buffer;
  16. size_t nwords = len / sizeof (md5_uint32);
  17. const md5_uint32 *endp = words + nwords;
  18. md5_uint32 A = ctx->A;
  19. md5_uint32 B = ctx->B;
  20. md5_uint32 C = ctx->C;
  21. md5_uint32 D = ctx->D;
  22. md5_uint32 lolen = len;
  23. /* First increment the byte count. RFC 1321 specifies the possible
  24. length of the file up to 2^64 bits. Here we only compute the
  25. number of bytes. Do a double word increment. */
  26. ctx->total[0] += lolen;
  27. ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
  28. /* Process all bytes in the buffer with 64 bytes in each round of
  29. the loop. */
  30. while (words < endp)
  31. {
  32. md5_uint32 *cwp = correct_words;
  33. md5_uint32 A_save = A;
  34. md5_uint32 B_save = B;
  35. md5_uint32 C_save = C;
  36. md5_uint32 D_save = D;
  37. /* First round: using the given function, the context and a constant
  38. the next context is computed. Because the algorithms processing
  39. unit is a 32-bit word and it is determined to work on words in
  40. little endian byte order we perhaps have to change the byte order
  41. before the computation. To reduce the work for the next steps
  42. we store the swapped words in the array CORRECT_WORDS. */
  43. #define OP(a, b, c, d, s, T) \
  44. do \
  45. { \
  46. a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
  47. ++words; \
  48. CYCLIC (a, s); \
  49. a += b; \
  50. } \
  51. while (0)
  52. /* It is unfortunate that C does not provide an operator for
  53. cyclic rotation. Hope the C compiler is smart enough. */
  54. #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
  55. /* Before we start, one word to the strange constants.
  56. They are defined in RFC 1321 as
  57. T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
  58. */
  59. /* Round 1. */
  60. OP (A, B, C, D, 7, 0xd76aa478);
  61. OP (D, A, B, C, 12, 0xe8c7b756);
  62. OP (C, D, A, B, 17, 0x242070db);
  63. OP (B, C, D, A, 22, 0xc1bdceee);
  64. OP (A, B, C, D, 7, 0xf57c0faf);
  65. OP (D, A, B, C, 12, 0x4787c62a);
  66. OP (C, D, A, B, 17, 0xa8304613);
  67. OP (B, C, D, A, 22, 0xfd469501);
  68. OP (A, B, C, D, 7, 0x698098d8);
  69. OP (D, A, B, C, 12, 0x8b44f7af);
  70. OP (C, D, A, B, 17, 0xffff5bb1);
  71. OP (B, C, D, A, 22, 0x895cd7be);
  72. OP (A, B, C, D, 7, 0x6b901122);
  73. OP (D, A, B, C, 12, 0xfd987193);
  74. OP (C, D, A, B, 17, 0xa679438e);
  75. OP (B, C, D, A, 22, 0x49b40821);
  76. /* For the second to fourth round we have the possibly swapped words
  77. in CORRECT_WORDS. Redefine the macro to take an additional first
  78. argument specifying the function to use. */
  79. #undef OP
  80. #define OP(f, a, b, c, d, k, s, T) \
  81. do \
  82. { \
  83. a += f (b, c, d) + correct_words[k] + T; \
  84. CYCLIC (a, s); \
  85. a += b; \
  86. } \
  87. while (0)
  88. /* Round 2. */
  89. OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
  90. OP (FG, D, A, B, C, 6, 9, 0xc040b340);
  91. OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
  92. OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
  93. OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
  94. OP (FG, D, A, B, C, 10, 9, 0x02441453);
  95. OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
  96. OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
  97. OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
  98. OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
  99. OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
  100. OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
  101. OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
  102. OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
  103. OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
  104. OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
  105. /* Round 3. */
  106. OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
  107. OP (FH, D, A, B, C, 8, 11, 0x8771f681);
  108. OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
  109. OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
  110. OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
  111. OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
  112. OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
  113. OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
  114. OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
  115. OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
  116. OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
  117. OP (FH, B, C, D, A, 6, 23, 0x04881d05);
  118. OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
  119. OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
  120. OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
  121. OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
  122. /* Round 4. */
  123. OP (FI, A, B, C, D, 0, 6, 0xf4292244);
  124. OP (FI, D, A, B, C, 7, 10, 0x432aff97);
  125. OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
  126. OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
  127. OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
  128. OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
  129. OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
  130. OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
  131. OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
  132. OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
  133. OP (FI, C, D, A, B, 6, 15, 0xa3014314);
  134. OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
  135. OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
  136. OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
  137. OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
  138. OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
  139. /* Add the starting values of the context. */
  140. A += A_save;
  141. B += B_save;
  142. C += C_save;
  143. D += D_save;
  144. }
  145. /* Put checksum in context given as argument. */
  146. ctx->A = A;
  147. ctx->B = B;
  148. ctx->C = C;
  149. ctx->D = D;
  150. }