1*eabc0478Schristos /* $NetBSD: md5.c,v 1.2 2024/08/18 20:47:14 christos Exp $ */ 2897be3a4Schristos 3897be3a4Schristos /* 4897be3a4Schristos * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC") 5897be3a4Schristos * Copyright (C) 2000, 2001 Internet Software Consortium. 6897be3a4Schristos * 7897be3a4Schristos * Permission to use, copy, modify, and/or distribute this software for any 8897be3a4Schristos * purpose with or without fee is hereby granted, provided that the above 9897be3a4Schristos * copyright notice and this permission notice appear in all copies. 10897be3a4Schristos * 11897be3a4Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12897be3a4Schristos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13897be3a4Schristos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14897be3a4Schristos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15897be3a4Schristos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16897be3a4Schristos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17897be3a4Schristos * PERFORMANCE OF THIS SOFTWARE. 18897be3a4Schristos */ 19897be3a4Schristos 20897be3a4Schristos /* Id: md5.c,v 1.16 2009/02/06 23:47:42 tbox Exp */ 21897be3a4Schristos 22897be3a4Schristos /*! \file 23897be3a4Schristos * This code implements the MD5 message-digest algorithm. 24897be3a4Schristos * The algorithm is due to Ron Rivest. This code was 25897be3a4Schristos * written by Colin Plumb in 1993, no copyright is claimed. 26897be3a4Schristos * This code is in the public domain; do with it what you wish. 27897be3a4Schristos * 28897be3a4Schristos * Equivalent code is available from RSA Data Security, Inc. 29897be3a4Schristos * This code has been tested against that, and is equivalent, 30897be3a4Schristos * except that you don't need to include two pages of legalese 31897be3a4Schristos * with every copy. 32897be3a4Schristos * 33897be3a4Schristos * To compute the message digest of a chunk of bytes, declare an 34897be3a4Schristos * MD5Context structure, pass it to MD5Init, call MD5Update as 35897be3a4Schristos * needed on buffers full of bytes, and then call MD5Final, which 36897be3a4Schristos * will fill a supplied 16-byte array with the digest. 37897be3a4Schristos */ 38897be3a4Schristos 39897be3a4Schristos #include "config.h" 40897be3a4Schristos 41897be3a4Schristos #include <isc/assertions.h> 42897be3a4Schristos #include <isc/md5.h> 43897be3a4Schristos #include <isc/platform.h> 44897be3a4Schristos #include <isc/string.h> 45897be3a4Schristos #include <isc/types.h> 46897be3a4Schristos #include <isc/util.h> 47897be3a4Schristos 48897be3a4Schristos #ifdef ISC_PLATFORM_OPENSSLHASH 49897be3a4Schristos 50897be3a4Schristos void 51897be3a4Schristos isc_md5_init(isc_md5_t *ctx) { 52897be3a4Schristos EVP_DigestInit(ctx, EVP_md5()); 53897be3a4Schristos } 54897be3a4Schristos 55897be3a4Schristos void 56897be3a4Schristos isc_md5_invalidate(isc_md5_t *ctx) { 57897be3a4Schristos EVP_MD_CTX_cleanup(ctx); 58897be3a4Schristos } 59897be3a4Schristos 60897be3a4Schristos void 61897be3a4Schristos isc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) { 62897be3a4Schristos EVP_DigestUpdate(ctx, (const void *) buf, (size_t) len); 63897be3a4Schristos } 64897be3a4Schristos 65897be3a4Schristos void 66897be3a4Schristos isc_md5_final(isc_md5_t *ctx, unsigned char *digest) { 67897be3a4Schristos EVP_DigestFinal(ctx, digest, NULL); 68897be3a4Schristos } 69897be3a4Schristos 70897be3a4Schristos #else 71897be3a4Schristos 72897be3a4Schristos static void 73897be3a4Schristos byteSwap(isc_uint32_t *buf, unsigned words) 74897be3a4Schristos { 75897be3a4Schristos unsigned char *p = (unsigned char *)buf; 76897be3a4Schristos 77897be3a4Schristos do { 78897be3a4Schristos *buf++ = (isc_uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 | 79897be3a4Schristos ((unsigned)p[1] << 8 | p[0]); 80897be3a4Schristos p += 4; 81897be3a4Schristos } while (--words); 82897be3a4Schristos } 83897be3a4Schristos 84897be3a4Schristos /*! 85897be3a4Schristos * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 86897be3a4Schristos * initialization constants. 87897be3a4Schristos */ 88897be3a4Schristos void 89897be3a4Schristos isc_md5_init(isc_md5_t *ctx) { 90897be3a4Schristos ctx->buf[0] = 0x67452301; 91897be3a4Schristos ctx->buf[1] = 0xefcdab89; 92897be3a4Schristos ctx->buf[2] = 0x98badcfe; 93897be3a4Schristos ctx->buf[3] = 0x10325476; 94897be3a4Schristos 95897be3a4Schristos ctx->bytes[0] = 0; 96897be3a4Schristos ctx->bytes[1] = 0; 97897be3a4Schristos } 98897be3a4Schristos 99897be3a4Schristos void 100897be3a4Schristos isc_md5_invalidate(isc_md5_t *ctx) { 101897be3a4Schristos memset(ctx, 0, sizeof(isc_md5_t)); 102897be3a4Schristos } 103897be3a4Schristos 104897be3a4Schristos /*@{*/ 105897be3a4Schristos /*! The four core functions - F1 is optimized somewhat */ 106897be3a4Schristos 107897be3a4Schristos /* #define F1(x, y, z) (x & y | ~x & z) */ 108897be3a4Schristos #define F1(x, y, z) (z ^ (x & (y ^ z))) 109897be3a4Schristos #define F2(x, y, z) F1(z, x, y) 110897be3a4Schristos #define F3(x, y, z) (x ^ y ^ z) 111897be3a4Schristos #define F4(x, y, z) (y ^ (x | ~z)) 112897be3a4Schristos /*@}*/ 113897be3a4Schristos 114897be3a4Schristos /*! This is the central step in the MD5 algorithm. */ 115897be3a4Schristos #define MD5STEP(f,w,x,y,z,in,s) \ 116897be3a4Schristos (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x) 117897be3a4Schristos 118897be3a4Schristos /*! 119897be3a4Schristos * The core of the MD5 algorithm, this alters an existing MD5 hash to 120897be3a4Schristos * reflect the addition of 16 longwords of new data. MD5Update blocks 121897be3a4Schristos * the data and converts bytes into longwords for this routine. 122897be3a4Schristos */ 123897be3a4Schristos static void 124897be3a4Schristos transform(isc_uint32_t buf[4], isc_uint32_t const in[16]) { 125897be3a4Schristos register isc_uint32_t a, b, c, d; 126897be3a4Schristos 127897be3a4Schristos a = buf[0]; 128897be3a4Schristos b = buf[1]; 129897be3a4Schristos c = buf[2]; 130897be3a4Schristos d = buf[3]; 131897be3a4Schristos 132897be3a4Schristos MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 133897be3a4Schristos MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 134897be3a4Schristos MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 135897be3a4Schristos MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 136897be3a4Schristos MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 137897be3a4Schristos MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 138897be3a4Schristos MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 139897be3a4Schristos MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 140897be3a4Schristos MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 141897be3a4Schristos MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 142897be3a4Schristos MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 143897be3a4Schristos MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 144897be3a4Schristos MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 145897be3a4Schristos MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 146897be3a4Schristos MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 147897be3a4Schristos MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 148897be3a4Schristos 149897be3a4Schristos MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 150897be3a4Schristos MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 151897be3a4Schristos MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 152897be3a4Schristos MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 153897be3a4Schristos MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 154897be3a4Schristos MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 155897be3a4Schristos MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 156897be3a4Schristos MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 157897be3a4Schristos MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 158897be3a4Schristos MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 159897be3a4Schristos MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 160897be3a4Schristos MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 161897be3a4Schristos MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 162897be3a4Schristos MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 163897be3a4Schristos MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 164897be3a4Schristos MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 165897be3a4Schristos 166897be3a4Schristos MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 167897be3a4Schristos MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 168897be3a4Schristos MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 169897be3a4Schristos MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 170897be3a4Schristos MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 171897be3a4Schristos MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 172897be3a4Schristos MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 173897be3a4Schristos MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 174897be3a4Schristos MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 175897be3a4Schristos MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 176897be3a4Schristos MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 177897be3a4Schristos MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 178897be3a4Schristos MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 179897be3a4Schristos MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 180897be3a4Schristos MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 181897be3a4Schristos MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 182897be3a4Schristos 183897be3a4Schristos MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 184897be3a4Schristos MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 185897be3a4Schristos MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 186897be3a4Schristos MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 187897be3a4Schristos MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 188897be3a4Schristos MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 189897be3a4Schristos MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 190897be3a4Schristos MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 191897be3a4Schristos MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 192897be3a4Schristos MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 193897be3a4Schristos MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 194897be3a4Schristos MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 195897be3a4Schristos MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 196897be3a4Schristos MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 197897be3a4Schristos MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 198897be3a4Schristos MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 199897be3a4Schristos 200897be3a4Schristos buf[0] += a; 201897be3a4Schristos buf[1] += b; 202897be3a4Schristos buf[2] += c; 203897be3a4Schristos buf[3] += d; 204897be3a4Schristos } 205897be3a4Schristos 206897be3a4Schristos /*! 207897be3a4Schristos * Update context to reflect the concatenation of another buffer full 208897be3a4Schristos * of bytes. 209897be3a4Schristos */ 210897be3a4Schristos void 211897be3a4Schristos isc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) { 212897be3a4Schristos isc_uint32_t t; 213897be3a4Schristos 214897be3a4Schristos /* Update byte count */ 215897be3a4Schristos 216897be3a4Schristos t = ctx->bytes[0]; 217897be3a4Schristos if ((ctx->bytes[0] = t + len) < t) 218897be3a4Schristos ctx->bytes[1]++; /* Carry from low to high */ 219897be3a4Schristos 220897be3a4Schristos t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */ 221897be3a4Schristos if (t > len) { 222897be3a4Schristos memcpy((unsigned char *)ctx->in + 64 - t, buf, len); 223897be3a4Schristos return; 224897be3a4Schristos } 225897be3a4Schristos /* First chunk is an odd size */ 226897be3a4Schristos memcpy((unsigned char *)ctx->in + 64 - t, buf, t); 227897be3a4Schristos byteSwap(ctx->in, 16); 228897be3a4Schristos transform(ctx->buf, ctx->in); 229897be3a4Schristos buf += t; 230897be3a4Schristos len -= t; 231897be3a4Schristos 232897be3a4Schristos /* Process data in 64-byte chunks */ 233897be3a4Schristos while (len >= 64) { 234897be3a4Schristos memcpy(ctx->in, buf, 64); 235897be3a4Schristos byteSwap(ctx->in, 16); 236897be3a4Schristos transform(ctx->buf, ctx->in); 237897be3a4Schristos buf += 64; 238897be3a4Schristos len -= 64; 239897be3a4Schristos } 240897be3a4Schristos 241897be3a4Schristos /* Handle any remaining bytes of data. */ 242897be3a4Schristos memcpy(ctx->in, buf, len); 243897be3a4Schristos } 244897be3a4Schristos 245897be3a4Schristos /*! 246897be3a4Schristos * Final wrapup - pad to 64-byte boundary with the bit pattern 247897be3a4Schristos * 1 0* (64-bit count of bits processed, MSB-first) 248897be3a4Schristos */ 249897be3a4Schristos void 250897be3a4Schristos isc_md5_final(isc_md5_t *ctx, unsigned char *digest) { 251897be3a4Schristos int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */ 252897be3a4Schristos unsigned char *p = (unsigned char *)ctx->in + count; 253897be3a4Schristos 254897be3a4Schristos /* Set the first char of padding to 0x80. There is always room. */ 255897be3a4Schristos *p++ = 0x80; 256897be3a4Schristos 257897be3a4Schristos /* Bytes of padding needed to make 56 bytes (-8..55) */ 258897be3a4Schristos count = 56 - 1 - count; 259897be3a4Schristos 260897be3a4Schristos if (count < 0) { /* Padding forces an extra block */ 261897be3a4Schristos memset(p, 0, count + 8); 262897be3a4Schristos byteSwap(ctx->in, 16); 263897be3a4Schristos transform(ctx->buf, ctx->in); 264897be3a4Schristos p = (unsigned char *)ctx->in; 265897be3a4Schristos count = 56; 266897be3a4Schristos } 267897be3a4Schristos memset(p, 0, count); 268897be3a4Schristos byteSwap(ctx->in, 14); 269897be3a4Schristos 270897be3a4Schristos /* Append length in bits and transform */ 271897be3a4Schristos ctx->in[14] = ctx->bytes[0] << 3; 272897be3a4Schristos ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29; 273897be3a4Schristos transform(ctx->buf, ctx->in); 274897be3a4Schristos 275897be3a4Schristos byteSwap(ctx->buf, 4); 276897be3a4Schristos memcpy(digest, ctx->buf, 16); 277897be3a4Schristos memset(ctx, 0, sizeof(isc_md5_t)); /* In case it's sensitive */ 278897be3a4Schristos } 279897be3a4Schristos #endif 280