md5.c revision 76af83c9adb772f7b045c62cf8b411165bfaa5ef
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Copyright (C) 2000, 2001, 2004, 2005, 2007, 2009, 2014-2016 Internet Systems Consortium, Inc. ("ISC")
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews/* $Id: md5.c,v 1.16 2009/02/06 23:47:42 tbox Exp $ */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * This code implements the MD5 message-digest algorithm.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * The algorithm is due to Ron Rivest. This code was
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * written by Colin Plumb in 1993, no copyright is claimed.
75c622f53bdda9d2f69f05e06eaf7be01fc09a33Evan Hunt * This code is in the public domain; do with it what you wish.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Equivalent code is available from RSA Data Security, Inc.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * This code has been tested against that, and is equivalent,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * except that you don't need to include two pages of legalese
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * with every copy.
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * To compute the message digest of a chunk of bytes, declare an
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * MD5Context structure, pass it to MD5Init, call MD5Update as
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * needed on buffers full of bytes, and then call MD5Final, which
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * will fill a supplied 16-byte array with the digest.
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#define EVP_MD_CTX_free(ptr) EVP_MD_CTX_cleanup(ptr)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RUNTIME_CHECK(EVP_DigestInit(ctx->ctx, EVP_md5()) == 1);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsisc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver (const void *) buf,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RUNTIME_CHECK(EVP_DigestFinal(ctx->ctx, digest, NULL) == 1);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver (void) pkcs_C_DigestFinal(ctx->session, garbage, &len);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver (ctx->session, (CK_BYTE_PTR) digest, &len));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver unsigned char *p = (unsigned char *)buf;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver *buf++ = (isc_uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * initialization constants.
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver/*! The four core functions - F1 is optimized somewhat */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver/* #define F1(x, y, z) (x & y | ~x & z) */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#define F1(x, y, z) (z ^ (x & (y ^ z)))
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#define F3(x, y, z) (x ^ y ^ z)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#define F4(x, y, z) (y ^ (x | ~z))
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver/*! This is the central step in the MD5 algorithm. */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * The core of the MD5 algorithm, this alters an existing MD5 hash to
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * reflect the addition of 16 longwords of new data. MD5Update blocks
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * the data and converts bytes into longwords for this routine.
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryvertransform(isc_uint32_t buf[4], isc_uint32_t const in[16]) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver register isc_uint32_t a, b, c, d;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
7b4b6f361b2fb2291c2019b377a9c0c8e80cfd6bMark Andrews MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * Update context to reflect the concatenation of another buffer full
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsisc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews /* Update byte count */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver ctx->bytes[1]++; /* Carry from low to high */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews memmove((unsigned char *)ctx->in + 64 - t, buf, len);
76db58eb818dc4839fa816df6a1a1ecb2c7a6bd0Evan Hunt /* First chunk is an odd size */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver memmove((unsigned char *)ctx->in + 64 - t, buf, t);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver /* Process data in 64-byte chunks */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver /* Handle any remaining bytes of data. */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * Final wrapup - pad to 64-byte boundary with the bit pattern
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver * 1 0* (64-bit count of bits processed, MSB-first)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver unsigned char *p = (unsigned char *)ctx->in + count;
475b1ed9cced1f92ce34bc2e59b3065dae48f366Mark Andrews /* Set the first char of padding to 0x80. There is always room. */
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver /* Bytes of padding needed to make 56 bytes (-8..55) */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (count < 0) { /* Padding forces an extra block */
#ifdef WIN32