1135446Strhodes/* 2262706Serwin * Copyright (C) 2004, 2005, 2007, 2009, 2014 Internet Systems Consortium, Inc. ("ISC") 3135446Strhodes * Copyright (C) 2000, 2001 Internet Software Consortium. 4135446Strhodes * 5193149Sdougb * Permission to use, copy, modify, and/or distribute this software for any 6135446Strhodes * purpose with or without fee is hereby granted, provided that the above 7135446Strhodes * copyright notice and this permission notice appear in all copies. 8135446Strhodes * 9135446Strhodes * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10135446Strhodes * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11135446Strhodes * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12135446Strhodes * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13135446Strhodes * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14135446Strhodes * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15135446Strhodes * PERFORMANCE OF THIS SOFTWARE. 16135446Strhodes */ 17135446Strhodes 18234010Sdougb/* $Id: md5.c,v 1.16 2009/02/06 23:47:42 tbox Exp $ */ 19135446Strhodes 20170222Sdougb/*! \file 21135446Strhodes * This code implements the MD5 message-digest algorithm. 22135446Strhodes * The algorithm is due to Ron Rivest. This code was 23135446Strhodes * written by Colin Plumb in 1993, no copyright is claimed. 24135446Strhodes * This code is in the public domain; do with it what you wish. 25135446Strhodes * 26135446Strhodes * Equivalent code is available from RSA Data Security, Inc. 27135446Strhodes * This code has been tested against that, and is equivalent, 28135446Strhodes * except that you don't need to include two pages of legalese 29135446Strhodes * with every copy. 30135446Strhodes * 31135446Strhodes * To compute the message digest of a chunk of bytes, declare an 32135446Strhodes * MD5Context structure, pass it to MD5Init, call MD5Update as 33135446Strhodes * needed on buffers full of bytes, and then call MD5Final, which 34135446Strhodes * will fill a supplied 16-byte array with the digest. 35135446Strhodes */ 36135446Strhodes 37135446Strhodes#include "config.h" 38135446Strhodes 39135446Strhodes#include <isc/assertions.h> 40135446Strhodes#include <isc/md5.h> 41224092Sdougb#include <isc/platform.h> 42135446Strhodes#include <isc/string.h> 43135446Strhodes#include <isc/types.h> 44135446Strhodes#include <isc/util.h> 45135446Strhodes 46224092Sdougb#ifdef ISC_PLATFORM_OPENSSLHASH 47224092Sdougb 48224092Sdougbvoid 49224092Sdougbisc_md5_init(isc_md5_t *ctx) { 50224092Sdougb EVP_DigestInit(ctx, EVP_md5()); 51224092Sdougb} 52224092Sdougb 53224092Sdougbvoid 54224092Sdougbisc_md5_invalidate(isc_md5_t *ctx) { 55224092Sdougb EVP_MD_CTX_cleanup(ctx); 56224092Sdougb} 57224092Sdougb 58224092Sdougbvoid 59224092Sdougbisc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) { 60224092Sdougb EVP_DigestUpdate(ctx, (const void *) buf, (size_t) len); 61224092Sdougb} 62224092Sdougb 63224092Sdougbvoid 64224092Sdougbisc_md5_final(isc_md5_t *ctx, unsigned char *digest) { 65224092Sdougb EVP_DigestFinal(ctx, digest, NULL); 66224092Sdougb} 67224092Sdougb 68224092Sdougb#else 69224092Sdougb 70135446Strhodesstatic void 71135446StrhodesbyteSwap(isc_uint32_t *buf, unsigned words) 72135446Strhodes{ 73135446Strhodes unsigned char *p = (unsigned char *)buf; 74135446Strhodes 75135446Strhodes do { 76135446Strhodes *buf++ = (isc_uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 | 77135446Strhodes ((unsigned)p[1] << 8 | p[0]); 78135446Strhodes p += 4; 79135446Strhodes } while (--words); 80135446Strhodes} 81135446Strhodes 82170222Sdougb/*! 83135446Strhodes * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 84135446Strhodes * initialization constants. 85135446Strhodes */ 86135446Strhodesvoid 87135446Strhodesisc_md5_init(isc_md5_t *ctx) { 88135446Strhodes ctx->buf[0] = 0x67452301; 89135446Strhodes ctx->buf[1] = 0xefcdab89; 90135446Strhodes ctx->buf[2] = 0x98badcfe; 91135446Strhodes ctx->buf[3] = 0x10325476; 92135446Strhodes 93135446Strhodes ctx->bytes[0] = 0; 94135446Strhodes ctx->bytes[1] = 0; 95135446Strhodes} 96135446Strhodes 97135446Strhodesvoid 98135446Strhodesisc_md5_invalidate(isc_md5_t *ctx) { 99135446Strhodes memset(ctx, 0, sizeof(isc_md5_t)); 100135446Strhodes} 101135446Strhodes 102170222Sdougb/*@{*/ 103170222Sdougb/*! The four core functions - F1 is optimized somewhat */ 104135446Strhodes 105135446Strhodes/* #define F1(x, y, z) (x & y | ~x & z) */ 106135446Strhodes#define F1(x, y, z) (z ^ (x & (y ^ z))) 107135446Strhodes#define F2(x, y, z) F1(z, x, y) 108135446Strhodes#define F3(x, y, z) (x ^ y ^ z) 109135446Strhodes#define F4(x, y, z) (y ^ (x | ~z)) 110170222Sdougb/*@}*/ 111135446Strhodes 112170222Sdougb/*! This is the central step in the MD5 algorithm. */ 113135446Strhodes#define MD5STEP(f,w,x,y,z,in,s) \ 114135446Strhodes (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x) 115135446Strhodes 116170222Sdougb/*! 117135446Strhodes * The core of the MD5 algorithm, this alters an existing MD5 hash to 118135446Strhodes * reflect the addition of 16 longwords of new data. MD5Update blocks 119135446Strhodes * the data and converts bytes into longwords for this routine. 120135446Strhodes */ 121135446Strhodesstatic void 122135446Strhodestransform(isc_uint32_t buf[4], isc_uint32_t const in[16]) { 123135446Strhodes register isc_uint32_t a, b, c, d; 124135446Strhodes 125135446Strhodes a = buf[0]; 126135446Strhodes b = buf[1]; 127135446Strhodes c = buf[2]; 128135446Strhodes d = buf[3]; 129135446Strhodes 130135446Strhodes MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 131135446Strhodes MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 132135446Strhodes MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 133135446Strhodes MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 134135446Strhodes MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 135135446Strhodes MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 136135446Strhodes MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 137135446Strhodes MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 138135446Strhodes MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 139135446Strhodes MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 140135446Strhodes MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 141135446Strhodes MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 142135446Strhodes MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 143135446Strhodes MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 144135446Strhodes MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 145135446Strhodes MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 146135446Strhodes 147135446Strhodes MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 148135446Strhodes MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 149135446Strhodes MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 150135446Strhodes MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 151135446Strhodes MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 152135446Strhodes MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 153135446Strhodes MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 154135446Strhodes MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 155135446Strhodes MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 156135446Strhodes MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 157135446Strhodes MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 158135446Strhodes MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 159135446Strhodes MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 160135446Strhodes MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 161135446Strhodes MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 162135446Strhodes MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 163135446Strhodes 164135446Strhodes MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 165135446Strhodes MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 166135446Strhodes MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 167135446Strhodes MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 168135446Strhodes MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 169135446Strhodes MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 170135446Strhodes MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 171135446Strhodes MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 172135446Strhodes MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 173135446Strhodes MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 174135446Strhodes MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 175135446Strhodes MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 176135446Strhodes MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 177135446Strhodes MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 178135446Strhodes MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 179135446Strhodes MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 180135446Strhodes 181135446Strhodes MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 182135446Strhodes MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 183135446Strhodes MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 184135446Strhodes MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 185135446Strhodes MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 186135446Strhodes MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 187135446Strhodes MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 188135446Strhodes MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 189135446Strhodes MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 190135446Strhodes MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 191135446Strhodes MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 192135446Strhodes MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 193135446Strhodes MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 194135446Strhodes MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 195135446Strhodes MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 196135446Strhodes MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 197135446Strhodes 198135446Strhodes buf[0] += a; 199135446Strhodes buf[1] += b; 200135446Strhodes buf[2] += c; 201135446Strhodes buf[3] += d; 202135446Strhodes} 203135446Strhodes 204170222Sdougb/*! 205135446Strhodes * Update context to reflect the concatenation of another buffer full 206135446Strhodes * of bytes. 207135446Strhodes */ 208135446Strhodesvoid 209135446Strhodesisc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) { 210135446Strhodes isc_uint32_t t; 211135446Strhodes 212135446Strhodes /* Update byte count */ 213135446Strhodes 214135446Strhodes t = ctx->bytes[0]; 215135446Strhodes if ((ctx->bytes[0] = t + len) < t) 216135446Strhodes ctx->bytes[1]++; /* Carry from low to high */ 217135446Strhodes 218135446Strhodes t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */ 219135446Strhodes if (t > len) { 220262706Serwin memmove((unsigned char *)ctx->in + 64 - t, buf, len); 221135446Strhodes return; 222135446Strhodes } 223135446Strhodes /* First chunk is an odd size */ 224262706Serwin memmove((unsigned char *)ctx->in + 64 - t, buf, t); 225135446Strhodes byteSwap(ctx->in, 16); 226135446Strhodes transform(ctx->buf, ctx->in); 227135446Strhodes buf += t; 228135446Strhodes len -= t; 229135446Strhodes 230135446Strhodes /* Process data in 64-byte chunks */ 231135446Strhodes while (len >= 64) { 232262706Serwin memmove(ctx->in, buf, 64); 233135446Strhodes byteSwap(ctx->in, 16); 234135446Strhodes transform(ctx->buf, ctx->in); 235135446Strhodes buf += 64; 236135446Strhodes len -= 64; 237135446Strhodes } 238135446Strhodes 239135446Strhodes /* Handle any remaining bytes of data. */ 240262706Serwin memmove(ctx->in, buf, len); 241135446Strhodes} 242135446Strhodes 243170222Sdougb/*! 244135446Strhodes * Final wrapup - pad to 64-byte boundary with the bit pattern 245135446Strhodes * 1 0* (64-bit count of bits processed, MSB-first) 246135446Strhodes */ 247135446Strhodesvoid 248135446Strhodesisc_md5_final(isc_md5_t *ctx, unsigned char *digest) { 249135446Strhodes int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */ 250135446Strhodes unsigned char *p = (unsigned char *)ctx->in + count; 251135446Strhodes 252135446Strhodes /* Set the first char of padding to 0x80. There is always room. */ 253135446Strhodes *p++ = 0x80; 254135446Strhodes 255135446Strhodes /* Bytes of padding needed to make 56 bytes (-8..55) */ 256135446Strhodes count = 56 - 1 - count; 257135446Strhodes 258135446Strhodes if (count < 0) { /* Padding forces an extra block */ 259135446Strhodes memset(p, 0, count + 8); 260135446Strhodes byteSwap(ctx->in, 16); 261135446Strhodes transform(ctx->buf, ctx->in); 262135446Strhodes p = (unsigned char *)ctx->in; 263135446Strhodes count = 56; 264135446Strhodes } 265135446Strhodes memset(p, 0, count); 266135446Strhodes byteSwap(ctx->in, 14); 267135446Strhodes 268135446Strhodes /* Append length in bits and transform */ 269135446Strhodes ctx->in[14] = ctx->bytes[0] << 3; 270135446Strhodes ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29; 271135446Strhodes transform(ctx->buf, ctx->in); 272135446Strhodes 273135446Strhodes byteSwap(ctx->buf, 4); 274262706Serwin memmove(digest, ctx->buf, 16); 275135446Strhodes memset(ctx, 0, sizeof(isc_md5_t)); /* In case it's sensitive */ 276135446Strhodes} 277224092Sdougb#endif 278