md5block.c 4.9 KB

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