su_md5.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #define compile \
  2. { gcc -o su_md5 -O2 -g -Wall -DTEST -I. su_md5.c } ; exit 0
  3. /* -*- c-style: java -*- */
  4. /*
  5. * This file is part of the Sofia-SIP package
  6. *
  7. * Copyright (C) 2005 Nokia Corporation.
  8. *
  9. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public License
  13. * as published by the Free Software Foundation; either version 2.1 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  24. * 02110-1301 USA
  25. *
  26. */
  27. /*
  28. * This code implements the MD5 message-digest algorithm. The algorithm is
  29. * due to Ron Rivest. This code was initially written by Colin Plumb in
  30. * 1993, no copyright is claimed. This code is in the public domain; do with
  31. * it what you wish.
  32. *
  33. * Equivalent code is available from RSA Data Security, Inc. This code has
  34. * been tested against that, and is equivalent, except that you don't need
  35. * to include two pages of legalese with every copy.
  36. */
  37. /** @ingroup su_md5
  38. *
  39. * @CFILE su_md5.c MD5 Implementation
  40. *
  41. * To compute the message digest of a chunk of bytes, declare an su_md5_t
  42. * context structure, pass it to su_md5_init(), call su_md5_update() as
  43. * needed on buffers full of bytes, and then call su_md5_digest(), which
  44. * will fill a supplied 16-byte array with the current digest.
  45. *
  46. * @note
  47. * This code was modified in 1997 by Jim Kingdon of Cyclic Software to
  48. * not require an integer type which is exactly 32 bits. This work
  49. * draws on the changes for the same purpose by Tatu Ylonen
  50. * <ylo@cs.hut.fi> as part of SSH, but since I didn't actually use
  51. * that code, there is no copyright issue. I hereby disclaim
  52. * copyright in any changes I have made; this code remains in the
  53. * public domain.
  54. *
  55. * @note Regarding su_* namespace: this avoids potential conflicts
  56. * with libraries such as some versions of Kerberos. No particular
  57. * need to worry about whether the system supplies an MD5 library, as
  58. * this file is only about 3k of object code.
  59. *
  60. */
  61. #include <string.h> /* for memcpy() and memset() */
  62. #include "sofia-sip/su_md5.h"
  63. static void su_md5_transform(uint32_t buf[4], const unsigned char inraw[64]);
  64. /* Little-endian byte-swapping routines. Note that these do not depend on
  65. the size of datatypes such as cvs_uint32, nor do they require us to
  66. detect the endianness of the machine we are running on. It is possible
  67. they should be macros for speed, but I would be surprised if they were a
  68. performance bottleneck for MD5. These are inlined by any sane compiler,
  69. anyways. */
  70. static uint32_t getu32(const unsigned char *addr)
  71. {
  72. return (((((unsigned long)addr[3] << 8) | addr[2]) << 8)
  73. | addr[1]) << 8 | addr[0];
  74. }
  75. static void putu32(uint32_t data, unsigned char *addr)
  76. {
  77. addr[0] = (unsigned char)data;
  78. addr[1] = (unsigned char)(data >> 8);
  79. addr[2] = (unsigned char)(data >> 16);
  80. addr[3] = (unsigned char)(data >> 24);
  81. }
  82. /** Initialize MD5 context.
  83. *
  84. * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
  85. * initialization constants.
  86. *
  87. * @param ctx Pointer to context structure.
  88. */
  89. void
  90. su_md5_init(su_md5_t *ctx)
  91. {
  92. ctx->buf[0] = 0x67452301;
  93. ctx->buf[1] = 0xefcdab89;
  94. ctx->buf[2] = 0x98badcfe;
  95. ctx->buf[3] = 0x10325476;
  96. ctx->bits[0] = 0;
  97. ctx->bits[1] = 0;
  98. }
  99. /** Clear MD5 context.
  100. *
  101. * The function su_md5_deinit() clears MD5 context.
  102. *
  103. * @param context Pointer to MD5 context structure.
  104. */
  105. void su_md5_deinit(su_md5_t *context)
  106. {
  107. memset(context, 0, sizeof *context);
  108. }
  109. /** Update MD5 context.
  110. *
  111. * Update context to reflect the concatenation of another buffer full
  112. * of bytes.
  113. *
  114. * @param ctx Pointer to context structure
  115. * @param b Pointer to data
  116. * @param len Length of @a b as bytes
  117. */
  118. void
  119. su_md5_update(su_md5_t *ctx,
  120. void const *b,
  121. usize_t len)
  122. {
  123. unsigned char const *buf = (unsigned char const *)b;
  124. uint32_t t;
  125. /* Update bitcount */
  126. t = ctx->bits[0];
  127. if ((ctx->bits[0] = (t + ((uint32_t)len << 3)) & 0xffffffff) < t)
  128. ctx->bits[1]++; /* Carry from low to high */
  129. ctx->bits[1] += (uint32_t)(len >> 29);
  130. t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
  131. /* Handle any leading odd-sized chunks */
  132. if ( t ) {
  133. unsigned char *p = ctx->in + t;
  134. t = 64 - t;
  135. if (len < t) {
  136. memcpy(p, buf, len);
  137. return;
  138. }
  139. memcpy(p, buf, t);
  140. su_md5_transform (ctx->buf, ctx->in);
  141. buf += t;
  142. len -= t;
  143. }
  144. /* Process data in 64-byte chunks */
  145. while (len >= 64) {
  146. su_md5_transform(ctx->buf, buf);
  147. buf += 64;
  148. len -= 64;
  149. }
  150. /* Handle any remaining bytes of data. */
  151. memcpy(ctx->in, buf, len);
  152. }
  153. /** Copy memory, fix case to lower. */
  154. static
  155. void mem_i_cpy(unsigned char *d, unsigned char const *s, size_t len)
  156. {
  157. size_t i;
  158. for (i = 0; i < len; i++)
  159. if (s[i] >= 'A' && s[i] <= 'Z')
  160. d[i] = s[i] + ('a' - 'A');
  161. else
  162. d[i] = s[i];
  163. }
  164. /**Update MD5 context.
  165. *
  166. * The function su_md5_iupdate() updates context to reflect the
  167. * concatenation of another buffer full of case-independent characters.
  168. *
  169. * @param ctx Pointer to context structure
  170. * @param b Pointer to data
  171. * @param len Length of @a b as bytes
  172. */
  173. void
  174. su_md5_iupdate(su_md5_t *ctx,
  175. void const *b,
  176. usize_t len)
  177. {
  178. unsigned char const *buf = (unsigned char const *)b;
  179. uint32_t t;
  180. /* Update bitcount */
  181. t = ctx->bits[0];
  182. if ((ctx->bits[0] = (t + ((uint32_t)len << 3)) & 0xffffffff) < t)
  183. ctx->bits[1]++; /* Carry from low to high */
  184. ctx->bits[1] += (uint32_t)(len >> 29);
  185. t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
  186. /* Handle any leading odd-sized chunks */
  187. if ( t ) {
  188. unsigned char *p = ctx->in + t;
  189. t = sizeof(ctx->in) - t;
  190. if (len < t) {
  191. mem_i_cpy(p, buf, len);
  192. return;
  193. }
  194. mem_i_cpy(p, buf, t);
  195. su_md5_transform (ctx->buf, ctx->in);
  196. buf += t;
  197. len -= t;
  198. }
  199. /* Process data in 64-byte chunks */
  200. while (len >= sizeof(ctx->in)) {
  201. mem_i_cpy(ctx->in, buf, sizeof(ctx->in));
  202. su_md5_transform(ctx->buf, ctx->in);
  203. buf += sizeof(ctx->in);
  204. len -= sizeof(ctx->in);
  205. }
  206. /* Handle any remaining bytes of data. */
  207. mem_i_cpy(ctx->in, buf, len);
  208. }
  209. /** Update MD5 context with contents of string.
  210. *
  211. * The function su_md5_strupdate() updates context to reflect the
  212. * concatenation of NUL-terminated string.
  213. *
  214. * @param ctx Pointer to context structure
  215. * @param s Pointer to string
  216. */
  217. void su_md5_strupdate(su_md5_t *ctx, char const *s)
  218. {
  219. if (s)
  220. su_md5_update(ctx, s, strlen(s));
  221. }
  222. /** Update MD5 context with contents of string, including final NUL.
  223. *
  224. * The function su_md5_str0update() updates context to reflect the
  225. * concatenation of NUL-terminated string, including the final NUL.
  226. *
  227. * @param ctx Pointer to context structure
  228. * @param s Pointer to string
  229. */
  230. void su_md5_str0update(su_md5_t *ctx, char const *s)
  231. {
  232. if (!s)
  233. s = "";
  234. su_md5_update(ctx, s, strlen(s) + 1);
  235. }
  236. /** Update MD5 context with contents of case-independent string.
  237. *
  238. * The function su_md5_striupdate() updates context to reflect the
  239. * concatenation of NUL-terminated string.
  240. *
  241. * @param ctx Pointer to context structure
  242. * @param s Pointer to string
  243. */
  244. void su_md5_striupdate(su_md5_t *ctx, char const *s)
  245. {
  246. if (s)
  247. su_md5_iupdate(ctx, s, strlen(s));
  248. }
  249. /** Update MD5 context with contents of case-independent string, including
  250. * final NUL.
  251. *
  252. * The function su_md5_stri0update() updates context to reflect the
  253. * concatenation of NUL-terminated string, including the final NUL.
  254. *
  255. * @param ctx Pointer to context structure
  256. * @param s Pointer to string
  257. */
  258. void su_md5_stri0update(su_md5_t *ctx, char const *s)
  259. {
  260. if (!s)
  261. s = "";
  262. su_md5_iupdate(ctx, s, strlen(s) + 1);
  263. }
  264. /** Generate digest.
  265. *
  266. * Final wrapup. Pad message to 64-byte boundary with the bit pattern 1 0*
  267. * (64-bit count of bits processed, MSB-first), then concatenate message
  268. * with its length (measured in bits) as 64-byte big-endian integer.
  269. *
  270. * @param context Pointer to context structure
  271. * @param digest Digest array to be filled
  272. */
  273. void
  274. su_md5_digest(su_md5_t const *context, uint8_t digest[16])
  275. {
  276. unsigned count;
  277. unsigned char *p;
  278. su_md5_t ctx[1];
  279. ctx[0] = context[0];
  280. /* Compute number of bytes mod 64 */
  281. count = (ctx->bits[0] >> 3) & 0x3F;
  282. /* Set the first char of padding to 0x80. This is safe since there is
  283. always at least one byte free */
  284. p = ctx->in + count;
  285. *p++ = 0x80;
  286. /* Bytes of padding needed to make 64 bytes */
  287. count = 64 - 1 - count;
  288. /* Pad out to 56 mod 64 */
  289. if (count < 8) {
  290. /* Two lots of padding: Pad the first block to 64 bytes */
  291. memset(p, 0, count);
  292. su_md5_transform (ctx->buf, ctx->in);
  293. /* Now fill the next block with 56 bytes */
  294. memset(ctx->in, 0, 56);
  295. } else {
  296. /* Pad block to 56 bytes */
  297. memset(p, 0, count-8);
  298. }
  299. /* Append length in bits and transform */
  300. putu32(ctx->bits[0], ctx->in + 56);
  301. putu32(ctx->bits[1], ctx->in + 60);
  302. su_md5_transform(ctx->buf, ctx->in);
  303. putu32(ctx->buf[0], digest);
  304. putu32(ctx->buf[1], digest + 4);
  305. putu32(ctx->buf[2], digest + 8);
  306. putu32(ctx->buf[3], digest + 12);
  307. memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
  308. }
  309. void su_md5_hexdigest(su_md5_t const *ctx,
  310. char digest[2 * SU_MD5_DIGEST_SIZE + 1])
  311. {
  312. uint8_t b, bin[SU_MD5_DIGEST_SIZE];
  313. short i, j;
  314. su_md5_digest(ctx, bin);
  315. for (i = j = 0; i < 16; i++) {
  316. b = (bin[i] >> 4) & 15;
  317. digest[j++] = b + (b > 9 ? 'a' - 10 : '0');
  318. b = bin[i] & 15;
  319. digest[j++] = b + (b > 9 ? 'a' - 10 : '0');
  320. }
  321. digest[j] = '\0';
  322. }
  323. #ifndef ASM_MD5
  324. /* The four core functions - F1 is optimized somewhat */
  325. /* #define F1(x, y, z) (x & y | ~x & z) */
  326. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  327. #define F2(x, y, z) F1(z, x, y)
  328. #define F3(x, y, z) (x ^ y ^ z)
  329. #define F4(x, y, z) (y ^ (x | ~z))
  330. /* This is the central step in the MD5 algorithm. */
  331. #define MD5STEP(f, w, x, y, z, data, s) \
  332. ( w += f(x, y, z) + data, w &= 0xffffffff, w = w<<s | w>>(32-s), w += x )
  333. /** @internal
  334. *
  335. * Add 64 bytes of data to hash.
  336. *
  337. * The core of the MD5 algorithm, this alters an existing MD5 hash to
  338. * reflect the addition of 16 longwords of new data. MD5Update blocks
  339. * the data and converts bytes into longwords for this routine.
  340. */
  341. static void
  342. su_md5_transform(uint32_t buf[4], const unsigned char inraw[64])
  343. {
  344. register uint32_t a, b, c, d;
  345. uint32_t in[16];
  346. int i;
  347. for (i = 0; i < 16; ++i)
  348. in[i] = getu32 (inraw + 4 * i);
  349. a = buf[0];
  350. b = buf[1];
  351. c = buf[2];
  352. d = buf[3];
  353. MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7);
  354. MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
  355. MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
  356. MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
  357. MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7);
  358. MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
  359. MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
  360. MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
  361. MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7);
  362. MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
  363. MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
  364. MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
  365. MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7);
  366. MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
  367. MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
  368. MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
  369. MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5);
  370. MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9);
  371. MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
  372. MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
  373. MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5);
  374. MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9);
  375. MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
  376. MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
  377. MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5);
  378. MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9);
  379. MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
  380. MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
  381. MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5);
  382. MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9);
  383. MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
  384. MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
  385. MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4);
  386. MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
  387. MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
  388. MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
  389. MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4);
  390. MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
  391. MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
  392. MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
  393. MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4);
  394. MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
  395. MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
  396. MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
  397. MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4);
  398. MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
  399. MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
  400. MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
  401. MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6);
  402. MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
  403. MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
  404. MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
  405. MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6);
  406. MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
  407. MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
  408. MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
  409. MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6);
  410. MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
  411. MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
  412. MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
  413. MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6);
  414. MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
  415. MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
  416. MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
  417. buf[0] += a;
  418. buf[1] += b;
  419. buf[2] += c;
  420. buf[3] += d;
  421. }
  422. #endif