/contrib/bind9/lib/isc/md5.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 277 lines · 178 code · 37 blank · 62 comment · 5 complexity · f245b8adc41c3f8539c055d62dbee012 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2000, 2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: md5.c,v 1.16 2009/02/06 23:47:42 tbox Exp $ */
  18. /*! \file
  19. * This code implements the MD5 message-digest algorithm.
  20. * The algorithm is due to Ron Rivest. This code was
  21. * written by Colin Plumb in 1993, no copyright is claimed.
  22. * This code is in the public domain; do with it what you wish.
  23. *
  24. * Equivalent code is available from RSA Data Security, Inc.
  25. * This code has been tested against that, and is equivalent,
  26. * except that you don't need to include two pages of legalese
  27. * with every copy.
  28. *
  29. * To compute the message digest of a chunk of bytes, declare an
  30. * MD5Context structure, pass it to MD5Init, call MD5Update as
  31. * needed on buffers full of bytes, and then call MD5Final, which
  32. * will fill a supplied 16-byte array with the digest.
  33. */
  34. #include "config.h"
  35. #include <isc/assertions.h>
  36. #include <isc/md5.h>
  37. #include <isc/platform.h>
  38. #include <isc/string.h>
  39. #include <isc/types.h>
  40. #include <isc/util.h>
  41. #ifdef ISC_PLATFORM_OPENSSLHASH
  42. void
  43. isc_md5_init(isc_md5_t *ctx) {
  44. EVP_DigestInit(ctx, EVP_md5());
  45. }
  46. void
  47. isc_md5_invalidate(isc_md5_t *ctx) {
  48. EVP_MD_CTX_cleanup(ctx);
  49. }
  50. void
  51. isc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
  52. EVP_DigestUpdate(ctx, (const void *) buf, (size_t) len);
  53. }
  54. void
  55. isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
  56. EVP_DigestFinal(ctx, digest, NULL);
  57. }
  58. #else
  59. static void
  60. byteSwap(isc_uint32_t *buf, unsigned words)
  61. {
  62. unsigned char *p = (unsigned char *)buf;
  63. do {
  64. *buf++ = (isc_uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
  65. ((unsigned)p[1] << 8 | p[0]);
  66. p += 4;
  67. } while (--words);
  68. }
  69. /*!
  70. * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
  71. * initialization constants.
  72. */
  73. void
  74. isc_md5_init(isc_md5_t *ctx) {
  75. ctx->buf[0] = 0x67452301;
  76. ctx->buf[1] = 0xefcdab89;
  77. ctx->buf[2] = 0x98badcfe;
  78. ctx->buf[3] = 0x10325476;
  79. ctx->bytes[0] = 0;
  80. ctx->bytes[1] = 0;
  81. }
  82. void
  83. isc_md5_invalidate(isc_md5_t *ctx) {
  84. memset(ctx, 0, sizeof(isc_md5_t));
  85. }
  86. /*@{*/
  87. /*! The four core functions - F1 is optimized somewhat */
  88. /* #define F1(x, y, z) (x & y | ~x & z) */
  89. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  90. #define F2(x, y, z) F1(z, x, y)
  91. #define F3(x, y, z) (x ^ y ^ z)
  92. #define F4(x, y, z) (y ^ (x | ~z))
  93. /*@}*/
  94. /*! This is the central step in the MD5 algorithm. */
  95. #define MD5STEP(f,w,x,y,z,in,s) \
  96. (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
  97. /*!
  98. * The core of the MD5 algorithm, this alters an existing MD5 hash to
  99. * reflect the addition of 16 longwords of new data. MD5Update blocks
  100. * the data and converts bytes into longwords for this routine.
  101. */
  102. static void
  103. transform(isc_uint32_t buf[4], isc_uint32_t const in[16]) {
  104. register isc_uint32_t a, b, c, d;
  105. a = buf[0];
  106. b = buf[1];
  107. c = buf[2];
  108. d = buf[3];
  109. MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
  110. MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
  111. MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
  112. MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
  113. MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
  114. MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
  115. MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
  116. MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
  117. MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
  118. MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
  119. MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
  120. MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
  121. MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
  122. MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
  123. MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
  124. MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
  125. MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
  126. MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
  127. MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
  128. MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
  129. MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
  130. MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
  131. MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
  132. MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
  133. MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
  134. MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
  135. MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
  136. MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
  137. MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
  138. MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
  139. MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
  140. MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
  141. MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
  142. MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
  143. MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
  144. MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
  145. MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
  146. MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
  147. MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
  148. MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
  149. MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
  150. MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
  151. MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
  152. MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
  153. MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
  154. MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
  155. MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
  156. MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
  157. MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
  158. MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
  159. MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
  160. MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
  161. MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
  162. MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
  163. MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
  164. MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
  165. MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
  166. MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
  167. MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
  168. MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
  169. MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
  170. MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
  171. MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
  172. MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
  173. buf[0] += a;
  174. buf[1] += b;
  175. buf[2] += c;
  176. buf[3] += d;
  177. }
  178. /*!
  179. * Update context to reflect the concatenation of another buffer full
  180. * of bytes.
  181. */
  182. void
  183. isc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
  184. isc_uint32_t t;
  185. /* Update byte count */
  186. t = ctx->bytes[0];
  187. if ((ctx->bytes[0] = t + len) < t)
  188. ctx->bytes[1]++; /* Carry from low to high */
  189. t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
  190. if (t > len) {
  191. memcpy((unsigned char *)ctx->in + 64 - t, buf, len);
  192. return;
  193. }
  194. /* First chunk is an odd size */
  195. memcpy((unsigned char *)ctx->in + 64 - t, buf, t);
  196. byteSwap(ctx->in, 16);
  197. transform(ctx->buf, ctx->in);
  198. buf += t;
  199. len -= t;
  200. /* Process data in 64-byte chunks */
  201. while (len >= 64) {
  202. memcpy(ctx->in, buf, 64);
  203. byteSwap(ctx->in, 16);
  204. transform(ctx->buf, ctx->in);
  205. buf += 64;
  206. len -= 64;
  207. }
  208. /* Handle any remaining bytes of data. */
  209. memcpy(ctx->in, buf, len);
  210. }
  211. /*!
  212. * Final wrapup - pad to 64-byte boundary with the bit pattern
  213. * 1 0* (64-bit count of bits processed, MSB-first)
  214. */
  215. void
  216. isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
  217. int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
  218. unsigned char *p = (unsigned char *)ctx->in + count;
  219. /* Set the first char of padding to 0x80. There is always room. */
  220. *p++ = 0x80;
  221. /* Bytes of padding needed to make 56 bytes (-8..55) */
  222. count = 56 - 1 - count;
  223. if (count < 0) { /* Padding forces an extra block */
  224. memset(p, 0, count + 8);
  225. byteSwap(ctx->in, 16);
  226. transform(ctx->buf, ctx->in);
  227. p = (unsigned char *)ctx->in;
  228. count = 56;
  229. }
  230. memset(p, 0, count);
  231. byteSwap(ctx->in, 14);
  232. /* Append length in bits and transform */
  233. ctx->in[14] = ctx->bytes[0] << 3;
  234. ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
  235. transform(ctx->buf, ctx->in);
  236. byteSwap(ctx->buf, 4);
  237. memcpy(digest, ctx->buf, 16);
  238. memset(ctx, 0, sizeof(isc_md5_t)); /* In case it's sensitive */
  239. }
  240. #endif