File indexing completed on 2025-02-23 04:27:36

0001 /* Parts Copyright 2004 David Hammerton <crazney@crazney.net>
0002  * Parts found in the public domain with no copyright claim.
0003  *
0004  * see rfc1321
0005  *
0006  * Permission is hereby granted, free of charge, to any person
0007  * obtaining a copy of this software and associated documentation
0008  * files (the "Software"), to deal in the Software without
0009  * restriction, including without limitation the rights to use,
0010  * copy, modify, merge, publish, distribute, sublicense, and/or
0011  * sell copies of the Software, and to permit persons to whom the
0012  * Software is furnished to do so, subject to the following conditions:
0013  *
0014  * The above copyright notice and this permission notice shall be
0015  * included in all copies or substantial portions of the Software.
0016  *
0017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0018  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0019  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0020  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0021  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0022  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0023  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0024  * OTHER DEALINGS IN THE SOFTWARE.
0025  *
0026  */
0027 
0028 #include <string.h>
0029 #include "md5.h"
0030 
0031 /*
0032 * This code implements the MD5 message-digest algorithm.
0033 * The algorithm is due to Ron Rivest.  This code was
0034 * written by Colin Plumb in 1993, no copyright is claimed.
0035 * This code is in the public domain; do with it what you wish.
0036 *
0037 * Equivalent code is available from RSA Data Security, Inc.
0038 * This code has been tested against that, and is equivalent,
0039 * except that you don't need to include two pages of legalese
0040 * with every copy.
0041 *
0042 * To compute the message digest of a chunk of bytes, declare an MD5Context
0043 * structure, pass it to OpenDaap_MD5Init, call OpenDaap_MD5Update as needed
0044 * on buffers full of bytes, and then call OpenDaap_MD5Final, which will fill
0045 * a supplied 16-byte array with the digest.
0046 */
0047 static void MD5Transform(uint32_t buf[4], uint32_t const in[16], int apple_ver);
0048 /* for some reason we still have to reverse bytes on bigendian machines
0049  * I don't really know why... but otherwise it fails..
0050  * Any MD5 gurus out there know why???
0051  */
0052 #if 0 /*ndef WORDS_BIGENDIAN was: HIGHFIRST */
0053 #define byteReverse(buf, len)     /* Nothing */
0054 #else
0055 static void byteReverse(unsigned char *buf, unsigned longs);
0056 
0057 #ifndef ASM_MD5
0058 /*
0059 * Note: this code is harmless on little-endian machines.
0060 */
0061 static void byteReverse(unsigned char *buf, unsigned longs)
0062 {
0063      uint32_t t;
0064      do {
0065           t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
0066                ((unsigned) buf[1] << 8 | buf[0]);
0067           *(uint32_t *) buf = t;
0068           buf += 4;
0069      } while (--longs);
0070 }
0071 #endif
0072 #endif
0073 
0074 
0075 void OpenDaap_MD5Init(MD5_CTX *ctx, int apple_ver)
0076 {
0077     memset(ctx, 0, sizeof(*ctx));
0078     ctx->buf[0] = 0x67452301;
0079     ctx->buf[1] = 0xefcdab89;
0080     ctx->buf[2] = 0x98badcfe;
0081     ctx->buf[3] = 0x10325476;
0082 
0083     ctx->bits[0] = 0;
0084     ctx->bits[1] = 0;
0085 
0086     ctx->apple_ver = apple_ver;
0087 }
0088 
0089 void OpenDaap_MD5Update(MD5_CTX *ctx, unsigned char const *buf, unsigned int len)
0090 {
0091     uint32_t t;
0092 
0093     /* Update bitcount */
0094 
0095     t = ctx->bits[0];
0096     if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t)
0097         ctx->bits[1]++;          /* Carry from low to high */
0098     ctx->bits[1] += len >> 29;
0099 
0100     t = (t >> 3) & 0x3f;     /* Bytes already in shsInfo->data */
0101 
0102     /* Handle any leading odd-sized chunks */
0103 
0104     if (t) {
0105         unsigned char *p = (unsigned char *) ctx->in + t;
0106 
0107         t = 64 - t;
0108         if (len < t) {
0109             memcpy(p, buf, len);
0110             return;
0111         }
0112         memcpy(p, buf, t);
0113         byteReverse(ctx->in, 16);
0114         MD5Transform(ctx->buf, (uint32_t *) ctx->in, ctx->apple_ver);
0115         buf += t;
0116         len -= t;
0117     }
0118     /* Process data in 64-byte chunks */
0119 
0120     while (len >= 64) {
0121         memcpy(ctx->in, buf, 64);
0122         byteReverse(ctx->in, 16);
0123         MD5Transform(ctx->buf, (uint32_t *) ctx->in, ctx->apple_ver);
0124         buf += 64;
0125         len -= 64;
0126     }
0127 
0128     /* Handle any remaining bytes of data. */
0129 
0130     memcpy(ctx->in, buf, len);
0131 
0132 }
0133 
0134 void OpenDaap_MD5Final(MD5_CTX *ctx, unsigned char digest[16])
0135 {
0136     unsigned count;
0137     unsigned char *p;
0138 
0139     /* Compute number of bytes mod 64 */
0140     count = (ctx->bits[0] >> 3) & 0x3F;
0141 
0142     /* Set the first char of padding to 0x80.  This is safe since there is
0143     always at least one byte free */
0144     p = ctx->in + count;
0145     *p++ = 0x80;
0146 
0147     /* Bytes of padding needed to make 64 bytes */
0148     count = 64 - 1 - count;
0149 
0150     /* Pad out to 56 mod 64 */
0151     if (count < 8) {
0152         /* Two lots of padding:  Pad the first block to 64 bytes */
0153         memset(p, 0, count);
0154         byteReverse(ctx->in, 16);
0155         MD5Transform(ctx->buf, (uint32_t *) ctx->in, ctx->apple_ver);
0156 
0157         /* Now fill the next block with 56 bytes */
0158         memset(ctx->in, 0, 56);
0159     } else {
0160         /* Pad block to 56 bytes */
0161         memset(p, 0, count - 8);
0162     }
0163     byteReverse(ctx->in, 14);
0164 
0165     /* Append length in bits and transform */
0166     ((uint32_t *) ctx->in)[14] = ctx->bits[0];
0167     ((uint32_t *) ctx->in)[15] = ctx->bits[1];
0168 
0169     MD5Transform(ctx->buf, (uint32_t *) ctx->in, ctx->apple_ver);
0170     byteReverse((unsigned char *) ctx->buf, 4);
0171     memcpy(digest, ctx->buf, 16);
0172     memset(ctx, 0, sizeof(*ctx));     /* In case it's sensitive */
0173 }
0174 
0175 #ifndef ASM_MD5
0176 
0177 /* The four core functions - F1 is optimized somewhat */
0178 
0179 /* #define F1(x, y, z) (x & y | ~x & z) */
0180 #define F1(x, y, z) (z ^ (x & (y ^ z)))
0181 #define F2(x, y, z) F1(z, x, y)
0182 #define F3(x, y, z) (x ^ y ^ z)
0183 #define F4(x, y, z) (y ^ (x | ~z))
0184 
0185 /* This is the central step in the MD5 algorithm. */
0186 #define MD5STEP(f, w, x, y, z, data, s) \
0187 ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
0188 
0189 /*
0190 * The core of the MD5 algorithm, this alters an existing MD5 hash to reflect
0191 * the addition of 16 longwords of new data.  OpenDaap_MD5Update blocks the
0192 * data and converts bytes into longwords for this routine.
0193 */
0194 static void MD5Transform(uint32_t buf[4], uint32_t const in[16], int apple_ver)
0195 {
0196     uint32_t a, b, c, d;
0197 
0198     a = buf[0];
0199     b = buf[1];
0200     c = buf[2];
0201     d = buf[3];
0202 
0203     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
0204     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
0205     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
0206     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
0207     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
0208     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
0209     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
0210     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
0211     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
0212     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
0213     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
0214     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
0215     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
0216     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
0217     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
0218     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
0219 
0220     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
0221     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
0222     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
0223     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
0224     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
0225     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
0226     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
0227     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
0228     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
0229     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
0230     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
0231 
0232     if (apple_ver == 1)
0233     {
0234         MD5STEP(F2, b, c, d, a, in[8] + 0x445a14ed, 20);
0235     }
0236     else
0237     {
0238         MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
0239     }
0240     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
0241     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
0242     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
0243     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
0244 
0245     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
0246     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
0247     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
0248     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
0249     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
0250     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
0251     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
0252     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
0253     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
0254     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
0255     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
0256     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
0257     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
0258     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
0259     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
0260     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
0261 
0262     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
0263     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
0264     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
0265     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
0266     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
0267     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
0268     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
0269     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
0270     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
0271     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
0272     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
0273     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
0274     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
0275     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
0276     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
0277     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
0278 
0279     buf[0] += a;
0280     buf[1] += b;
0281     buf[2] += c;
0282     buf[3] += d;
0283 }
0284 
0285 #endif
0286 
0287 
0288