Back to index

citadel  8.12
md5.c
Go to the documentation of this file.
00001 /*
00002  * This code implements the MD5 message-digest algorithm.
00003  * The algorithm is due to Ron Rivest.  This code was
00004  * written by Colin Plumb in 1993, no copyright is claimed.
00005  * This code is in the public domain; do with it what you wish.
00006  *
00007  * Equivalent code is available from RSA Data Security, Inc.
00008  * This code has been tested against that, and is equivalent,
00009  * except that you don't need to include two pages of legalese
00010  * with every copy.
00011  *
00012  * To compute the message digest of a chunk of bytes, declare an
00013  * MD5Context structure, pass it to MD5Init, call MD5Update as
00014  * needed on buffers full of bytes, and then call MD5Final, which
00015  * will fill a supplied 16-byte array with the digest.
00016  */
00017 
00018 #include <stdio.h>
00019 #include <sys/types.h>
00020 #include <ctype.h>
00021 #include <string.h>         /* for memcpy() */
00022 #include "md5.h"
00023 
00024 #ifndef HIGHFIRST
00025 #define byteReverse(buf, len)      /* Nothing */
00026 #else
00027 void byteReverse(unsigned char *buf, unsigned longs);
00028 
00029 #ifndef ASM_MD5
00030 /*
00031  * Note: this code is harmless on little-endian machines.
00032  */
00033 void byteReverse(unsigned char *buf, unsigned longs)
00034 {
00035     cit_uint32_t t;
00036     do {
00037        t = (cit_uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
00038            ((unsigned) buf[1] << 8 | buf[0]);
00039        *(cit_uint32_t *) buf = t;
00040        buf += 4;
00041     } while (--longs);
00042 }
00043 #endif
00044 #endif
00045 
00046 
00047 
00048 /*
00049  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
00050  * initialization constants.
00051  */
00052 void MD5Init(struct MD5Context *ctx)
00053 {
00054     ctx->buf[0] = 0x67452301;
00055     ctx->buf[1] = 0xefcdab89;
00056     ctx->buf[2] = 0x98badcfe;
00057     ctx->buf[3] = 0x10325476;
00058 
00059     ctx->bits[0] = 0;
00060     ctx->bits[1] = 0;
00061 }
00062 
00063 /*
00064  * Update context to reflect the concatenation of another buffer full
00065  * of bytes.
00066  */
00067 void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
00068 {
00069     cit_uint32_t t;
00070 
00071     /* Update bitcount */
00072 
00073     t = ctx->bits[0];
00074     if ((ctx->bits[0] = t + ((cit_uint32_t) len << 3)) < t)
00075        ctx->bits[1]++;             /* Carry from low to high */
00076     ctx->bits[1] += len >> 29;
00077 
00078     t = (t >> 3) & 0x3f;    /* Bytes already in shsInfo->data */
00079 
00080     /* Handle any leading odd-sized chunks */
00081 
00082     if (t) {
00083        unsigned char *p = (unsigned char *) ctx->in + t;
00084 
00085        t = 64 - t;
00086        if (len < t) {
00087            memcpy(p, buf, len);
00088            return;
00089        }
00090        memcpy(p, buf, t);
00091        byteReverse(ctx->in, 16);
00092        MD5Transform(ctx->buf, ctx->in);
00093        buf += t;
00094        len -= t;
00095     }
00096     /* Process data in 64-byte chunks */
00097 
00098     while (len >= 64) {
00099        memcpy(ctx->in, buf, 64);
00100        byteReverse(ctx->in, 16);
00101        MD5Transform(ctx->buf, ctx->in);
00102        buf += 64;
00103        len -= 64;
00104     }
00105 
00106     /* Handle any remaining bytes of data. */
00107 
00108     memcpy(ctx->in, buf, len);
00109 }
00110 
00111 /*
00112  * Final wrapup - pad to 64-byte boundary with the bit pattern 
00113  * 1 0* (64-bit count of bits processed, MSB-first)
00114  */
00115 void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
00116 {
00117     unsigned count;
00118     unsigned char *p;
00119 
00120     /* Compute number of bytes mod 64 */
00121     count = (ctx->bits[0] >> 3) & 0x3F;
00122 
00123     /* Set the first char of padding to 0x80.  This is safe since there is
00124        always at least one byte free */
00125     p = ((unsigned char*)ctx->in) + count;
00126     *p++ = 0x80;
00127 
00128     /* Bytes of padding needed to make 64 bytes */
00129     count = 64 - 1 - count;
00130 
00131     /* Pad out to 56 mod 64 */
00132     if (count < 8) {
00133        /* Two lots of padding:  Pad the first block to 64 bytes */
00134        memset(p, 0, count);
00135        byteReverse(ctx->in, 16);
00136        MD5Transform(ctx->buf, ctx->in);
00137 
00138        /* Now fill the next block with 56 bytes */
00139        memset(ctx->in, 0, 56);
00140     } else {
00141        /* Pad block to 56 bytes */
00142        memset(p, 0, count - 8);
00143     }
00144     byteReverse(ctx->in, 14);
00145 
00146     /* Append length in bits and transform */
00147     ((cit_uint32_t *) ctx->in)[14] = ctx->bits[0];
00148     ((cit_uint32_t *) ctx->in)[15] = ctx->bits[1];
00149 
00150     MD5Transform(ctx->buf, (cit_uint32_t *) ctx->in);
00151     byteReverse((unsigned char *) ctx->buf, 4);
00152     memcpy(digest, ctx->buf, 16);
00153     memset(ctx, 0, sizeof(ctx));   /* In case it's sensitive */
00154 }
00155 
00156 #ifndef ASM_MD5
00157 
00158 /* The four core functions - F1 is optimized somewhat */
00159 
00160 /* #define F1(x, y, z) (x & y | ~x & z) */
00161 #define F1(x, y, z) (z ^ (x & (y ^ z)))
00162 #define F2(x, y, z) F1(z, x, y)
00163 #define F3(x, y, z) (x ^ y ^ z)
00164 #define F4(x, y, z) (y ^ (x | ~z))
00165 
00166 /* This is the central step in the MD5 algorithm. */
00167 #ifdef __PUREC__
00168 #define MD5STEP(f, w, x, y, z, data, s) \
00169        ( w += f /*(x, y, z)*/ + data,  w = w<<s | w>>(32-s),  w += x )
00170 #else
00171 #define MD5STEP(f, w, x, y, z, data, s) \
00172        ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
00173 #endif
00174 
00175 /*
00176  * The core of the MD5 algorithm, this alters an existing MD5 hash to
00177  * reflect the addition of 16 longwords of new data.  MD5Update blocks
00178  * the data and converts bytes into longwords for this routine.
00179  */
00180 void MD5Transform(cit_uint32_t buf[4], cit_uint32_t const in[16])
00181 {
00182     register cit_uint32_t a, b, c, d;
00183 
00184     a = buf[0];
00185     b = buf[1];
00186     c = buf[2];
00187     d = buf[3];
00188 
00189 #ifdef __PUREC__     /* PureC Weirdness... (GG) */
00190     MD5STEP(F1(b,c,d), a, b, c, d, in[0] + 0xd76aa478L, 7);
00191     MD5STEP(F1(a,b,c), d, a, b, c, in[1] + 0xe8c7b756L, 12);
00192     MD5STEP(F1(d,a,b), c, d, a, b, in[2] + 0x242070dbL, 17);
00193     MD5STEP(F1(c,d,a), b, c, d, a, in[3] + 0xc1bdceeeL, 22);
00194     MD5STEP(F1(b,c,d), a, b, c, d, in[4] + 0xf57c0fafL, 7);
00195     MD5STEP(F1(a,b,c), d, a, b, c, in[5] + 0x4787c62aL, 12);
00196     MD5STEP(F1(d,a,b), c, d, a, b, in[6] + 0xa8304613L, 17);
00197     MD5STEP(F1(c,d,a), b, c, d, a, in[7] + 0xfd469501L, 22);
00198     MD5STEP(F1(b,c,d), a, b, c, d, in[8] + 0x698098d8L, 7);
00199     MD5STEP(F1(a,b,c), d, a, b, c, in[9] + 0x8b44f7afL, 12);
00200     MD5STEP(F1(d,a,b), c, d, a, b, in[10] + 0xffff5bb1L, 17);
00201     MD5STEP(F1(c,d,a), b, c, d, a, in[11] + 0x895cd7beL, 22);
00202     MD5STEP(F1(b,c,d), a, b, c, d, in[12] + 0x6b901122L, 7);
00203     MD5STEP(F1(a,b,c), d, a, b, c, in[13] + 0xfd987193L, 12);
00204     MD5STEP(F1(d,a,b), c, d, a, b, in[14] + 0xa679438eL, 17);
00205     MD5STEP(F1(c,d,a), b, c, d, a, in[15] + 0x49b40821L, 22);
00206 
00207     MD5STEP(F2(b,c,d), a, b, c, d, in[1] + 0xf61e2562L, 5);
00208     MD5STEP(F2(a,b,c), d, a, b, c, in[6] + 0xc040b340L, 9);
00209     MD5STEP(F2(d,a,b), c, d, a, b, in[11] + 0x265e5a51L, 14);
00210     MD5STEP(F2(c,d,a), b, c, d, a, in[0] + 0xe9b6c7aaL, 20);
00211     MD5STEP(F2(b,c,d), a, b, c, d, in[5] + 0xd62f105dL, 5);
00212     MD5STEP(F2(a,b,c), d, a, b, c, in[10] + 0x02441453L, 9);
00213     MD5STEP(F2(d,a,b), c, d, a, b, in[15] + 0xd8a1e681L, 14);
00214     MD5STEP(F2(c,d,a), b, c, d, a, in[4] + 0xe7d3fbc8L, 20);
00215     MD5STEP(F2(b,c,d), a, b, c, d, in[9] + 0x21e1cde6L, 5);
00216     MD5STEP(F2(a,b,c), d, a, b, c, in[14] + 0xc33707d6L, 9);
00217     MD5STEP(F2(d,a,b), c, d, a, b, in[3] + 0xf4d50d87L, 14);
00218     MD5STEP(F2(c,d,a), b, c, d, a, in[8] + 0x455a14edL, 20);
00219     MD5STEP(F2(b,c,d), a, b, c, d, in[13] + 0xa9e3e905L, 5);
00220     MD5STEP(F2(a,b,c), d, a, b, c, in[2] + 0xfcefa3f8L, 9);
00221     MD5STEP(F2(d,a,b), c, d, a, b, in[7] + 0x676f02d9L, 14);
00222     MD5STEP(F2(c,d,a), b, c, d, a, in[12] + 0x8d2a4c8aL, 20);
00223 
00224     MD5STEP(F3(b,c,d), a, b, c, d, in[5] + 0xfffa3942L, 4);
00225     MD5STEP(F3(a,b,c), d, a, b, c, in[8] + 0x8771f681L, 11);
00226     MD5STEP(F3(d,a,b), c, d, a, b, in[11] + 0x6d9d6122L, 16);
00227     MD5STEP(F3(c,d,a), b, c, d, a, in[14] + 0xfde5380cL, 23);
00228     MD5STEP(F3(b,c,d), a, b, c, d, in[1] + 0xa4beea44L, 4);
00229     MD5STEP(F3(a,b,c), d, a, b, c, in[4] + 0x4bdecfa9L, 11);
00230     MD5STEP(F3(d,a,b), c, d, a, b, in[7] + 0xf6bb4b60L, 16);
00231     MD5STEP(F3(c,d,a), b, c, d, a, in[10] + 0xbebfbc70L, 23);
00232     MD5STEP(F3(b,c,d), a, b, c, d, in[13] + 0x289b7ec6L, 4);
00233     MD5STEP(F3(a,b,c), d, a, b, c, in[0] + 0xeaa127faL, 11);
00234     MD5STEP(F3(d,a,b), c, d, a, b, in[3] + 0xd4ef3085L, 16);
00235     MD5STEP(F3(c,d,a), b, c, d, a, in[6] + 0x04881d05L, 23);
00236     MD5STEP(F3(b,c,d), a, b, c, d, in[9] + 0xd9d4d039L, 4);
00237     MD5STEP(F3(a,b,c), d, a, b, c, in[12] + 0xe6db99e5L, 11);
00238     MD5STEP(F3(d,a,b), c, d, a, b, in[15] + 0x1fa27cf8L, 16);
00239     MD5STEP(F3(c,d,a), b, c, d, a, in[2] + 0xc4ac5665L, 23);
00240 
00241     MD5STEP(F4(b,c,d), a, b, c, d, in[0] + 0xf4292244L, 6);
00242     MD5STEP(F4(a,b,c), d, a, b, c, in[7] + 0x432aff97L, 10);
00243     MD5STEP(F4(d,a,b), c, d, a, b, in[14] + 0xab9423a7L, 15);
00244     MD5STEP(F4(c,d,a), b, c, d, a, in[5] + 0xfc93a039L, 21);
00245     MD5STEP(F4(b,c,d), a, b, c, d, in[12] + 0x655b59c3L, 6);
00246     MD5STEP(F4(a,b,c), d, a, b, c, in[3] + 0x8f0ccc92L, 10);
00247     MD5STEP(F4(d,a,b), c, d, a, b, in[10] + 0xffeff47dL, 15);
00248     MD5STEP(F4(c,d,a), b, c, d, a, in[1] + 0x85845dd1L, 21);
00249     MD5STEP(F4(b,c,d), a, b, c, d, in[8] + 0x6fa87e4fL, 6);
00250     MD5STEP(F4(a,b,c), d, a, b, c, in[15] + 0xfe2ce6e0L, 10);
00251     MD5STEP(F4(d,a,b), c, d, a, b, in[6] + 0xa3014314L, 15);
00252     MD5STEP(F4(c,d,a), b, c, d, a, in[13] + 0x4e0811a1L, 21);
00253     MD5STEP(F4(b,c,d), a, b, c, d, in[4] + 0xf7537e82L, 6);
00254     MD5STEP(F4(a,b,c), d, a, b, c, in[11] + 0xbd3af235L, 10);
00255     MD5STEP(F4(d,a,b), c, d, a, b, in[2] + 0x2ad7d2bbL, 15);
00256     MD5STEP(F4(c,d,a), b, c, d, a, in[9] + 0xeb86d391L, 21);
00257 #else
00258     MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
00259     MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
00260     MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
00261     MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
00262     MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
00263     MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
00264     MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
00265     MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
00266     MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
00267     MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
00268     MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
00269     MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
00270     MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
00271     MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
00272     MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
00273     MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
00274 
00275     MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
00276     MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
00277     MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
00278     MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
00279     MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
00280     MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
00281     MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
00282     MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
00283     MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
00284     MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
00285     MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
00286     MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
00287     MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
00288     MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
00289     MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
00290     MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
00291 
00292     MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
00293     MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
00294     MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
00295     MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
00296     MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
00297     MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
00298     MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
00299     MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
00300     MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
00301     MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
00302     MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
00303     MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
00304     MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
00305     MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
00306     MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
00307     MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
00308 
00309     MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
00310     MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
00311     MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
00312     MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
00313     MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
00314     MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
00315     MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
00316     MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
00317     MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
00318     MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
00319     MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
00320     MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
00321     MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
00322     MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
00323     MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
00324     MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
00325 #endif
00326 
00327     buf[0] += a;
00328     buf[1] += b;
00329     buf[2] += c;
00330     buf[3] += d;
00331 }
00332 
00333 /*
00334  * This part was added by Brian Costello <btx@calyx.net>
00335  * For citadel's APOP auth - makes a lower case (as per APOP RFC)
00336  * md5 string
00337  */
00338 
00339 char *make_apop_string(char *realpass, char *nonce, char *buffer, size_t n)
00340 {
00341    struct MD5Context ctx;
00342    u_char rawdigest[MD5_DIGEST_LEN];
00343    int i;
00344    
00345    MD5Init(&ctx);
00346    MD5Update(&ctx, (u_char*)nonce, strlen(nonce));
00347    MD5Update(&ctx, (u_char*)realpass, strlen(realpass));
00348    MD5Final(rawdigest, &ctx);
00349    for (i=0; i<MD5_DIGEST_LEN; i++)
00350    {
00351       snprintf(&buffer[i*2], n - i*2, "%02X", (unsigned char) (rawdigest[i] & 0xff));
00352       buffer[i*2] = tolower(buffer[i*2]);
00353       buffer[(i*2)+1] = tolower(buffer[(i*2)+1]);
00354    }
00355    return buffer;
00356 }
00357 
00358 
00359 #endif