md5.c revision 22bed621ef87bc8b6c1fea599b02c4b38dd6bf48
7d98a1783f222964bcde7d56dab77b822706204dBob Halley * Copyright (C) 2000, 2001, 2004, 2005, 2007, 2009, 2014-2016 Internet Systems Consortium, Inc. ("ISC")
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * This Source Code Form is subject to the terms of the Mozilla Public
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * License, v. 2.0. If a copy of the MPL was not distributed with this
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * file, You can obtain one at http://mozilla.org/MPL/2.0/.
15a44745412679c30a6d022733925af70a38b715David Lawrence/* $Id: md5.c,v 1.16 2009/02/06 23:47:42 tbox Exp $ */
15a44745412679c30a6d022733925af70a38b715David Lawrence * This code implements the MD5 message-digest algorithm.
15a44745412679c30a6d022733925af70a38b715David Lawrence * The algorithm is due to Ron Rivest. This code was
15a44745412679c30a6d022733925af70a38b715David Lawrence * written by Colin Plumb in 1993, no copyright is claimed.
15a44745412679c30a6d022733925af70a38b715David Lawrence * This code is in the public domain; do with it what you wish.
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * Equivalent code is available from RSA Data Security, Inc.
8f7cae3d7b0c122c3b17e8409bbb80005433acd2Brian Wellington * This code has been tested against that, and is equivalent,
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * except that you don't need to include two pages of legalese
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * with every copy.
8f7cae3d7b0c122c3b17e8409bbb80005433acd2Brian Wellington * To compute the message digest of a chunk of bytes, declare an
8f7cae3d7b0c122c3b17e8409bbb80005433acd2Brian Wellington * MD5Context structure, pass it to MD5Init, call MD5Update as
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * needed on buffers full of bytes, and then call MD5Final, which
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * will fill a supplied 16-byte array with the digest.
8327c62a49a2487d29a37acbed6b602e629fc0eeAndreas Gustafsson#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
8327c62a49a2487d29a37acbed6b602e629fc0eeAndreas Gustafsson#define EVP_MD_CTX_free(ptr) EVP_MD_CTX_cleanup(ptr)
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley RUNTIME_CHECK(EVP_DigestInit(ctx->ctx, EVP_md5()) == 1);
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halleyisc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
edcd1247ad7e81bb8b430e610d9718f64c70f05dDavid Lawrence (const void *) buf,
c90f5e8d1edbd5c277f2ee320167a12a30ba7c7bMichael Graffisc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
8582a1e113c13886ccbd1b534d6c240315767be6Bob Halley RUNTIME_CHECK(EVP_DigestFinal(ctx->ctx, digest, NULL) == 1);
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson CK_MECHANISM mech = { CKM_MD5, NULL, 0 };
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
e02c696ea586f8dcc7c6145cc0f143f887960cd4Andreas Gustafsson PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence (void) pkcs_C_DigestFinal(ctx->session, garbage, &len);
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafssonisc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafssonisc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson (ctx->session, (CK_BYTE_PTR) digest, &len));
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas GustafssonbyteSwap(isc_uint32_t *buf, unsigned words)
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson unsigned char *p = (unsigned char *)buf;
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson *buf++ = (isc_uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
7193a1762e428cfba06907e51fa9e4bce3b5569aAndreas Gustafsson * initialization constants.
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson/*! The four core functions - F1 is optimized somewhat */
5542df09597c479be604da0ece8271cbc6fd9c4aDavid Lawrence/* #define F1(x, y, z) (x & y | ~x & z) */
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley#define F1(x, y, z) (z ^ (x & (y ^ z)))
11d435aa4cf77e035445978f7e3776a3589715fdAndreas Gustafsson#define F3(x, y, z) (x ^ y ^ z)
5542df09597c479be604da0ece8271cbc6fd9c4aDavid Lawrence#define F4(x, y, z) (y ^ (x | ~z))
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley/*! This is the central step in the MD5 algorithm. */
a0f6cda5fd9f2fcc4154bb63628f849b639a40caAndreas Gustafsson (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * The core of the MD5 algorithm, this alters an existing MD5 hash to
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * reflect the addition of 16 longwords of new data. MD5Update blocks
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley * the data and converts bytes into longwords for this routine.
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halleytransform(isc_uint32_t buf[4], isc_uint32_t const in[16]) {
1687985cdfc3a4c330c5bdb02c131835f8756e3cBob Halley register isc_uint32_t a, b, c, d;
a = buf[0];
buf[0] += a;
isc_uint32_t t;
if (t > len) {
buf += t;
len -= t;
#ifdef WIN32