md5block.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "os.h"
  2. #include <libsec.h>
  3. /*
  4. * rfc1321 requires that I include this. The code is new. The constants
  5. * all come from the rfc (hence the copyright). We trade a table for the
  6. * macros in rfc. The total size is a lot less. -- presotto
  7. *
  8. * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  9. * rights reserved.
  10. *
  11. * License to copy and use this software is granted provided that it
  12. * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  13. * Algorithm" in all material mentioning or referencing this software
  14. * or this function.
  15. *
  16. * License is also granted to make and use derivative works provided
  17. * that such works are identified as "derived from the RSA Data
  18. * Security, Inc. MD5 Message-Digest Algorithm" in all material
  19. * mentioning or referencing the derived work.
  20. *
  21. * RSA Data Security, Inc. makes no representations concerning either
  22. * the merchantability of this software or the suitability of this
  23. * software forany particular purpose. It is provided "as is"
  24. * without express or implied warranty of any kind.
  25. * These notices must be retained in any copies of any part of this
  26. * documentation and/or software.
  27. */
  28. /*
  29. * Rotate ammounts used in the algorithm
  30. */
  31. enum
  32. {
  33. S11= 7,
  34. S12= 12,
  35. S13= 17,
  36. S14= 22,
  37. S21= 5,
  38. S22= 9,
  39. S23= 14,
  40. S24= 20,
  41. S31= 4,
  42. S32= 11,
  43. S33= 16,
  44. S34= 23,
  45. S41= 6,
  46. S42= 10,
  47. S43= 15,
  48. S44= 21,
  49. };
  50. static u32int md5tab[] =
  51. {
  52. /* round 1 */
  53. /*[0]*/ 0xd76aa478,
  54. 0xe8c7b756,
  55. 0x242070db,
  56. 0xc1bdceee,
  57. 0xf57c0faf,
  58. 0x4787c62a,
  59. 0xa8304613,
  60. 0xfd469501,
  61. 0x698098d8,
  62. 0x8b44f7af,
  63. 0xffff5bb1,
  64. 0x895cd7be,
  65. 0x6b901122,
  66. 0xfd987193,
  67. 0xa679438e,
  68. 0x49b40821,
  69. /* round 2 */
  70. /*[16]*/0xf61e2562,
  71. 0xc040b340,
  72. 0x265e5a51,
  73. 0xe9b6c7aa,
  74. 0xd62f105d,
  75. 0x2441453,
  76. 0xd8a1e681,
  77. 0xe7d3fbc8,
  78. 0x21e1cde6,
  79. 0xc33707d6,
  80. 0xf4d50d87,
  81. 0x455a14ed,
  82. 0xa9e3e905,
  83. 0xfcefa3f8,
  84. 0x676f02d9,
  85. 0x8d2a4c8a,
  86. /* round 3 */
  87. /*[32]*/0xfffa3942,
  88. 0x8771f681,
  89. 0x6d9d6122,
  90. 0xfde5380c,
  91. 0xa4beea44,
  92. 0x4bdecfa9,
  93. 0xf6bb4b60,
  94. 0xbebfbc70,
  95. 0x289b7ec6,
  96. 0xeaa127fa,
  97. 0xd4ef3085,
  98. 0x4881d05,
  99. 0xd9d4d039,
  100. 0xe6db99e5,
  101. 0x1fa27cf8,
  102. 0xc4ac5665,
  103. /* round 4 */
  104. /*[48]*/0xf4292244,
  105. 0x432aff97,
  106. 0xab9423a7,
  107. 0xfc93a039,
  108. 0x655b59c3,
  109. 0x8f0ccc92,
  110. 0xffeff47d,
  111. 0x85845dd1,
  112. 0x6fa87e4f,
  113. 0xfe2ce6e0,
  114. 0xa3014314,
  115. 0x4e0811a1,
  116. 0xf7537e82,
  117. 0xbd3af235,
  118. 0x2ad7d2bb,
  119. 0xeb86d391,
  120. };
  121. static void decode(u32int*, uchar*, ulong);
  122. extern void _md5block(uchar *p, ulong len, u32int *s);
  123. void
  124. _md5block(uchar *p, ulong len, u32int *s)
  125. {
  126. u32int a, b, c, d, sh;
  127. u32int *t;
  128. uchar *end;
  129. u32int x[16];
  130. for(end = p+len; p < end; p += 64){
  131. a = s[0];
  132. b = s[1];
  133. c = s[2];
  134. d = s[3];
  135. decode(x, p, 64);
  136. t = md5tab;
  137. sh = 0;
  138. for(; sh != 16; t += 4){
  139. a += ((c ^ d) & b) ^ d;
  140. a += x[sh] + t[0];
  141. a = (a << S11) | (a >> (32 - S11));
  142. a += b;
  143. d += ((b ^ c) & a) ^ c;
  144. d += x[sh + 1] + t[1];
  145. d = (d << S12) | (d >> (32 - S12));
  146. d += a;
  147. c += ((a ^ b) & d) ^ b;
  148. c += x[sh + 2] + t[2];
  149. c = (c << S13) | (c >> (32 - S13));
  150. c += d;
  151. b += ((d ^ a) & c) ^ a;
  152. b += x[sh + 3] + t[3];
  153. b = (b << S14) | (b >> (32 - S14));
  154. b += c;
  155. sh += 4;
  156. }
  157. sh = 1;
  158. for(; sh != 1+20*4; t += 4){
  159. a += ((b ^ c) & d) ^ c;
  160. a += x[sh & 0xf] + t[0];
  161. a = (a << S21) | (a >> (32 - S21));
  162. a += b;
  163. d += ((a ^ b) & c) ^ b;
  164. d += x[(sh + 5) & 0xf] + t[1];
  165. d = (d << S22) | (d >> (32 - S22));
  166. d += a;
  167. c += ((d ^ a) & b) ^ a;
  168. c += x[(sh + 10) & 0xf] + t[2];
  169. c = (c << S23) | (c >> (32 - S23));
  170. c += d;
  171. b += ((c ^ d) & a) ^ d;
  172. b += x[(sh + 15) & 0xf] + t[3];
  173. b = (b << S24) | (b >> (32 - S24));
  174. b += c;
  175. sh += 20;
  176. }
  177. sh = 5;
  178. for(; sh != 5+12*4; t += 4){
  179. a += b ^ c ^ d;
  180. a += x[sh & 0xf] + t[0];
  181. a = (a << S31) | (a >> (32 - S31));
  182. a += b;
  183. d += a ^ b ^ c;
  184. d += x[(sh + 3) & 0xf] + t[1];
  185. d = (d << S32) | (d >> (32 - S32));
  186. d += a;
  187. c += d ^ a ^ b;
  188. c += x[(sh + 6) & 0xf] + t[2];
  189. c = (c << S33) | (c >> (32 - S33));
  190. c += d;
  191. b += c ^ d ^ a;
  192. b += x[(sh + 9) & 0xf] + t[3];
  193. b = (b << S34) | (b >> (32 - S34));
  194. b += c;
  195. sh += 12;
  196. }
  197. sh = 0;
  198. for(; sh != 28*4; t += 4){
  199. a += c ^ (b | ~d);
  200. a += x[sh & 0xf] + t[0];
  201. a = (a << S41) | (a >> (32 - S41));
  202. a += b;
  203. d += b ^ (a | ~c);
  204. d += x[(sh + 7) & 0xf] + t[1];
  205. d = (d << S42) | (d >> (32 - S42));
  206. d += a;
  207. c += a ^ (d | ~b);
  208. c += x[(sh + 14) & 0xf] + t[2];
  209. c = (c << S43) | (c >> (32 - S43));
  210. c += d;
  211. b += d ^ (c | ~a);
  212. b += x[(sh + 21) & 0xf] + t[3];
  213. b = (b << S44) | (b >> (32 - S44));
  214. b += c;
  215. sh += 28;
  216. }
  217. s[0] += a;
  218. s[1] += b;
  219. s[2] += c;
  220. s[3] += d;
  221. }
  222. }
  223. /*
  224. * decodes input (uchar) into output (u32int). Assumes len is
  225. * a multiple of 4.
  226. */
  227. static void
  228. decode(u32int *output, uchar *input, ulong len)
  229. {
  230. uchar *e;
  231. for(e = input+len; input < e; input += 4)
  232. *output++ = input[0] | (input[1] << 8) |
  233. (input[2] << 16) | (input[3] << 24);
  234. }