md5.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  2. */
  3. /* Function names changed to avoid namespace collisions: Rob Siemborski */
  4. /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  5. rights reserved.
  6. License to copy and use this software is granted provided that it
  7. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  8. Algorithm" in all material mentioning or referencing this software
  9. or this function.
  10. License is also granted to make and use derivative works provided
  11. that such works are identified as "derived from the RSA Data
  12. Security, Inc. MD5 Message-Digest Algorithm" in all material
  13. mentioning or referencing the derived work.
  14. RSA Data Security, Inc. makes no representations concerning either
  15. the merchantability of this software or the suitability of this
  16. software for any particular purpose. It is provided "as is"
  17. without express or implied warranty of any kind.
  18. These notices must be retained in any copies of any part of this
  19. documentation and/or software.
  20. */
  21. #include <config.h>
  22. #include "md5global.h"
  23. #include "md5.h"
  24. #include "hmac-md5.h"
  25. #ifndef WIN32
  26. # include <arpa/inet.h>
  27. #endif
  28. /* Constants for MD5Transform routine.
  29. */
  30. #define S11 7
  31. #define S12 12
  32. #define S13 17
  33. #define S14 22
  34. #define S21 5
  35. #define S22 9
  36. #define S23 14
  37. #define S24 20
  38. #define S31 4
  39. #define S32 11
  40. #define S33 16
  41. #define S34 23
  42. #define S41 6
  43. #define S42 10
  44. #define S43 15
  45. #define S44 21
  46. static void MD5Transform PROTO_LIST ((SASL_UINT4 [4], const unsigned char [64]));
  47. static void Encode PROTO_LIST
  48. ((unsigned char *, SASL_UINT4 *, unsigned int));
  49. static void Decode PROTO_LIST
  50. ((SASL_UINT4 *, const unsigned char *, unsigned int));
  51. static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  52. static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
  53. static unsigned char PADDING[64] = {
  54. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  56. };
  57. /* F, G, H and I are basic MD5 functions.
  58. */
  59. #ifdef I
  60. /* This might be defined via NANA */
  61. #undef I
  62. #endif
  63. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  64. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  65. #define H(x, y, z) ((x) ^ (y) ^ (z))
  66. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  67. /* ROTATE_LEFT rotates x left n bits.
  68. */
  69. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  70. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  71. Rotation is separate from addition to prevent recomputation.
  72. */
  73. #define FF(a, b, c, d, x, s, ac) { (a) += F ((b), (c), (d)) + (x) + (SASL_UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  74. #define GG(a, b, c, d, x, s, ac) { (a) += G ((b), (c), (d)) + (x) + (SASL_UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  75. #define HH(a, b, c, d, x, s, ac) { (a) += H ((b), (c), (d)) + (x) + (SASL_UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  76. #define II(a, b, c, d, x, s, ac) { (a) += I ((b), (c), (d)) + (x) + (SASL_UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); }
  77. /* MD5 initialization. Begins an MD5 operation, writing a new context.
  78. */
  79. void _sasl_MD5Init (context)
  80. MD5_CTX *context; /* context */
  81. {
  82. context->count[0] = context->count[1] = 0;
  83. /* Load magic initialization constants. */
  84. context->state[0] = 0x67452301;
  85. context->state[1] = 0xefcdab89;
  86. context->state[2] = 0x98badcfe;
  87. context->state[3] = 0x10325476;
  88. }
  89. /* MD5 block update operation. Continues an MD5 message-digest
  90. operation, processing another message block, and updating the context.
  91. */
  92. void _sasl_MD5Update (context, input, inputLen)
  93. MD5_CTX *context; /* context */
  94. const unsigned char *input; /* input block */
  95. unsigned int inputLen; /* length of input block */
  96. {
  97. unsigned int i, index, partLen;
  98. /* Compute number of bytes mod 64 */
  99. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  100. /* Update number of bits */
  101. if ((context->count[0] += ((SASL_UINT4)inputLen << 3))
  102. < ((SASL_UINT4)inputLen << 3))
  103. context->count[1]++;
  104. context->count[1] += ((SASL_UINT4)inputLen >> 29);
  105. partLen = 64 - index;
  106. /* Transform as many times as possible.
  107. */
  108. if (inputLen >= partLen) {
  109. MD5_memcpy
  110. ((POINTER)&context->buffer[index], (POINTER)input, partLen); MD5Transform
  111. (context->state, context->buffer);
  112. for (i = partLen; i + 63 < inputLen; i += 64)
  113. MD5Transform (context->state, &input[i]);
  114. index = 0;
  115. }
  116. else
  117. i = 0;
  118. /* Buffer remaining input */
  119. MD5_memcpy
  120. ((POINTER)&context->buffer[index], (POINTER)&input[i],
  121. inputLen-i);
  122. }
  123. /* MD5 finalization. Ends an MD5 message-digest operation, writing the
  124. the message digest and zeroizing the context.
  125. */
  126. void _sasl_MD5Final (digest, context)
  127. unsigned char digest[16]; /* message digest */
  128. MD5_CTX *context; /* context */
  129. {
  130. unsigned char bits[8];
  131. unsigned int index, padLen;
  132. /* Save number of bits */
  133. Encode (bits, context->count, 8);
  134. /* Pad out to 56 mod 64. */
  135. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  136. padLen = (index < 56) ? (56 - index) : (120 - index);
  137. _sasl_MD5Update (context, PADDING, padLen);
  138. /* Append length (before padding) */
  139. _sasl_MD5Update (context, bits, 8);
  140. /* Store state in digest */
  141. Encode (digest, context->state, 16);
  142. /* Zeroize sensitive information. */
  143. MD5_memset ((POINTER)context, 0, sizeof (*context));
  144. }
  145. /* MD5 basic transformation. Transforms state based on block. */
  146. static void MD5Transform (state, block)
  147. SASL_UINT4 state[4];
  148. const unsigned char block[64];
  149. {
  150. SASL_UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  151. Decode (x, block, 64);
  152. /* Round 1 */
  153. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  154. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  155. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  156. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  157. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  158. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  159. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  160. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  161. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  162. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  163. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  164. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  165. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  166. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  167. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  168. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  169. /* Round 2 */
  170. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  171. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  172. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  173. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  174. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  175. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  176. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  177. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  178. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  179. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  180. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  181. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  182. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  183. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  184. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  185. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  186. /* Round 3 */
  187. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  188. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  189. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  190. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  191. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  192. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  193. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  194. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  195. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  196. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  197. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  198. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  199. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  200. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  201. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  202. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  203. /* Round 4 */
  204. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  205. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  206. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  207. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  208. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  209. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  210. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  211. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  212. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  213. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  214. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  215. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  216. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  217. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  218. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  219. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  220. state[0] += a;
  221. state[1] += b;
  222. state[2] += c;
  223. state[3] += d;
  224. /* Zeroize sensitive information.
  225. */
  226. MD5_memset ((POINTER)x, 0, sizeof (x));
  227. }
  228. /* Encodes input (SASL_UINT4) into output (unsigned char). Assumes len is
  229. a multiple of 4.
  230. */
  231. static void Encode (output, input, len)
  232. unsigned char *output;
  233. SASL_UINT4 *input;
  234. unsigned int len;
  235. {
  236. unsigned int i, j;
  237. for (i = 0, j = 0; j < len; i++, j += 4) {
  238. output[j] = (unsigned char)(input[i] & 0xff);
  239. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  240. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  241. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  242. }
  243. }
  244. /* Decodes input (unsigned char) into output (SASL_UINT4). Assumes len is
  245. a multiple of 4.
  246. */
  247. static void Decode (output, input, len)
  248. SASL_UINT4 *output;
  249. const unsigned char *input;
  250. unsigned int len;
  251. {
  252. unsigned int i, j;
  253. for (i = 0, j = 0; j < len; i++, j += 4)
  254. output[i] = ((SASL_UINT4)input[j]) | (((SASL_UINT4)input[j+1]) << 8) | (((SASL_UINT4)input[j+2]) << 16)
  255. | (((SASL_UINT4)input[j+3]) << 24);
  256. }
  257. /* Note: Replace "for loop" with standard memcpy if possible.
  258. */
  259. static void MD5_memcpy (output, input, len)
  260. POINTER output;
  261. POINTER input;
  262. unsigned int len;
  263. {
  264. unsigned int i;
  265. for (i = 0; i < len; i++)
  266. output[i] = input[i];
  267. }
  268. /* Note: Replace "for loop" with standard memset if possible.
  269. */
  270. static void MD5_memset (output, value, len)
  271. POINTER output;
  272. int value;
  273. unsigned int len;
  274. {
  275. unsigned int i;
  276. for (i = 0; i < len; i++)
  277. ((char *)output)[i] = (char)value;
  278. }
  279. void _sasl_hmac_md5_init(HMAC_MD5_CTX *hmac,
  280. const unsigned char *key,
  281. int key_len)
  282. {
  283. unsigned char k_ipad[65]; /* inner padding -
  284. * key XORd with ipad
  285. */
  286. unsigned char k_opad[65]; /* outer padding -
  287. * key XORd with opad
  288. */
  289. unsigned char tk[16];
  290. int i;
  291. /* if key is longer than 64 bytes reset it to key=MD5(key) */
  292. if (key_len > 64) {
  293. MD5_CTX tctx;
  294. _sasl_MD5Init(&tctx);
  295. _sasl_MD5Update(&tctx, key, key_len);
  296. _sasl_MD5Final(tk, &tctx);
  297. key = tk;
  298. key_len = 16;
  299. }
  300. /*
  301. * the HMAC_MD5 transform looks like:
  302. *
  303. * MD5(K XOR opad, MD5(K XOR ipad, text))
  304. *
  305. * where K is an n byte key
  306. * ipad is the byte 0x36 repeated 64 times
  307. * opad is the byte 0x5c repeated 64 times
  308. * and text is the data being protected
  309. */
  310. /* start out by storing key in pads */
  311. MD5_memset((POINTER)k_ipad, '\0', sizeof k_ipad);
  312. MD5_memset((POINTER)k_opad, '\0', sizeof k_opad);
  313. MD5_memcpy( k_ipad, (POINTER)key, key_len);
  314. MD5_memcpy( k_opad, (POINTER)key, key_len);
  315. /* XOR key with ipad and opad values */
  316. for (i=0; i<64; i++) {
  317. k_ipad[i] ^= 0x36;
  318. k_opad[i] ^= 0x5c;
  319. }
  320. _sasl_MD5Init(&hmac->ictx); /* init inner context */
  321. _sasl_MD5Update(&hmac->ictx, k_ipad, 64); /* apply inner pad */
  322. _sasl_MD5Init(&hmac->octx); /* init outer context */
  323. _sasl_MD5Update(&hmac->octx, k_opad, 64); /* apply outer pad */
  324. /* scrub the pads and key context (if used) */
  325. MD5_memset((POINTER)&k_ipad, 0, sizeof(k_ipad));
  326. MD5_memset((POINTER)&k_opad, 0, sizeof(k_opad));
  327. MD5_memset((POINTER)&tk, 0, sizeof(tk));
  328. /* and we're done. */
  329. }
  330. /* The precalc and import routines here rely on the fact that we pad
  331. * the key out to 64 bytes and use that to initialize the md5
  332. * contexts, and that updating an md5 context with 64 bytes of data
  333. * leaves nothing left over; all of the interesting state is contained
  334. * in the state field, and none of it is left over in the count and
  335. * buffer fields. So all we have to do is save the state field; we
  336. * can zero the others when we reload it. Which is why the decision
  337. * was made to pad the key out to 64 bytes in the first place. */
  338. void _sasl_hmac_md5_precalc(HMAC_MD5_STATE *state,
  339. const unsigned char *key,
  340. int key_len)
  341. {
  342. HMAC_MD5_CTX hmac;
  343. unsigned lupe;
  344. _sasl_hmac_md5_init(&hmac, key, key_len);
  345. for (lupe = 0; lupe < 4; lupe++) {
  346. state->istate[lupe] = htonl(hmac.ictx.state[lupe]);
  347. state->ostate[lupe] = htonl(hmac.octx.state[lupe]);
  348. }
  349. MD5_memset((POINTER)&hmac, 0, sizeof(hmac));
  350. }
  351. void _sasl_hmac_md5_import(HMAC_MD5_CTX *hmac,
  352. HMAC_MD5_STATE *state)
  353. {
  354. unsigned lupe;
  355. MD5_memset((POINTER)hmac, 0, sizeof(HMAC_MD5_CTX));
  356. for (lupe = 0; lupe < 4; lupe++) {
  357. hmac->ictx.state[lupe] = ntohl(state->istate[lupe]);
  358. hmac->octx.state[lupe] = ntohl(state->ostate[lupe]);
  359. }
  360. /* Init the counts to account for our having applied
  361. * 64 bytes of key; this works out to 0x200 (64 << 3; see
  362. * MD5Update above...) */
  363. hmac->ictx.count[0] = hmac->octx.count[0] = 0x200;
  364. }
  365. void _sasl_hmac_md5_final(unsigned char digest[HMAC_MD5_SIZE],
  366. HMAC_MD5_CTX *hmac)
  367. {
  368. _sasl_MD5Final(digest, &hmac->ictx); /* Finalize inner md5 */
  369. _sasl_MD5Update(&hmac->octx, digest, 16); /* Update outer ctx */
  370. _sasl_MD5Final(digest, &hmac->octx); /* Finalize outer md5 */
  371. }
  372. void _sasl_hmac_md5(text, text_len, key, key_len, digest)
  373. const unsigned char* text; /* pointer to data stream */
  374. int text_len; /* length of data stream */
  375. const unsigned char* key; /* pointer to authentication key */
  376. int key_len; /* length of authentication key */
  377. unsigned char *digest; /* caller digest to be filled in */
  378. {
  379. MD5_CTX context;
  380. unsigned char k_ipad[65]; /* inner padding -
  381. * key XORd with ipad
  382. */
  383. unsigned char k_opad[65]; /* outer padding -
  384. * key XORd with opad
  385. */
  386. unsigned char tk[16];
  387. int i;
  388. /* if key is longer than 64 bytes reset it to key=MD5(key) */
  389. if (key_len > 64) {
  390. MD5_CTX tctx;
  391. _sasl_MD5Init(&tctx);
  392. _sasl_MD5Update(&tctx, key, key_len);
  393. _sasl_MD5Final(tk, &tctx);
  394. key = tk;
  395. key_len = 16;
  396. }
  397. /*
  398. * the HMAC_MD5 transform looks like:
  399. *
  400. * MD5(K XOR opad, MD5(K XOR ipad, text))
  401. *
  402. * where K is an n byte key
  403. * ipad is the byte 0x36 repeated 64 times
  404. * opad is the byte 0x5c repeated 64 times
  405. * and text is the data being protected
  406. */
  407. /* start out by storing key in pads */
  408. MD5_memset(k_ipad, '\0', sizeof k_ipad);
  409. MD5_memset(k_opad, '\0', sizeof k_opad);
  410. MD5_memcpy( k_ipad, (POINTER)key, key_len);
  411. MD5_memcpy( k_opad, (POINTER)key, key_len);
  412. /* XOR key with ipad and opad values */
  413. for (i=0; i<64; i++) {
  414. k_ipad[i] ^= 0x36;
  415. k_opad[i] ^= 0x5c;
  416. }
  417. /*
  418. * perform inner MD5
  419. */
  420. _sasl_MD5Init(&context); /* init context for 1st
  421. * pass */
  422. _sasl_MD5Update(&context, k_ipad, 64); /* start with inner pad */
  423. _sasl_MD5Update(&context, text, text_len); /* then text of datagram */
  424. _sasl_MD5Final(digest, &context); /* finish up 1st pass */
  425. /*
  426. * perform outer MD5
  427. */
  428. _sasl_MD5Init(&context); /* init context for 2nd
  429. * pass */
  430. _sasl_MD5Update(&context, k_opad, 64); /* start with outer pad */
  431. _sasl_MD5Update(&context, digest, 16); /* then results of 1st
  432. * hash */
  433. _sasl_MD5Final(digest, &context); /* finish up 2nd pass */
  434. }