md5block.c 5.4 KB

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