BeRTOS
|
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 "md5.h" 00019 #include <sec/util.h> 00020 #include <cpu/byteorder.h> 00021 #include <string.h> 00022 00023 static void MD5Transform(uint32_t buf[4], uint32_t in[16]); 00024 00025 static void byteReverse(uint32_t *buf, unsigned longs) 00026 { 00027 do { 00028 *buf = le32_to_cpu(*buf); 00029 ++buf; 00030 } while (--longs); 00031 } 00032 00033 /* 00034 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 00035 * initialization constants. 00036 */ 00037 static void MD5_begin(Hash *h) 00038 { 00039 MD5_Context *ctx = (MD5_Context *)h; 00040 00041 ctx->buf[0] = 0x67452301; 00042 ctx->buf[1] = 0xefcdab89; 00043 ctx->buf[2] = 0x98badcfe; 00044 ctx->buf[3] = 0x10325476; 00045 00046 ctx->bits = 0; 00047 } 00048 00049 /* 00050 * Update context to reflect the concatenation of another buffer full 00051 * of bytes. 00052 */ 00053 static void MD5_update(Hash *h, const void* vbuf, size_t len) 00054 { 00055 MD5_Context *ctx = (MD5_Context *)h; 00056 const char *buf = (const char *)vbuf; 00057 uint32_t t; 00058 00059 /* Update bitcount */ 00060 t = (ctx->bits >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 00061 ctx->bits += len*8; 00062 00063 /* Handle any leading odd-sized chunks */ 00064 00065 if (t) { 00066 uint8_t *p = (uint8_t*) ctx->in + t; 00067 00068 t = 64 - t; 00069 if (len < t) { 00070 memcpy(p, buf, len); 00071 return; 00072 } 00073 memcpy(p, buf, t); 00074 byteReverse((uint32_t*)ctx->in, 16); 00075 MD5Transform(ctx->buf, (uint32_t*)ctx->in); 00076 buf += t; 00077 len -= t; 00078 } 00079 /* Process data in 64-byte chunks */ 00080 00081 while (len >= 64) { 00082 memcpy(ctx->in, buf, 64); 00083 byteReverse((uint32_t*)ctx->in, 16); 00084 MD5Transform(ctx->buf, (uint32_t*)ctx->in); 00085 buf += 64; 00086 len -= 64; 00087 } 00088 00089 /* Handle any remaining bytes of data. */ 00090 memcpy(ctx->in, buf, len); 00091 } 00092 00093 /* 00094 * Final wrapup - pad to 64-byte boundary with the bit pattern 00095 * 1 0* (64-bit count of bits processed, MSB-first) 00096 */ 00097 static uint8_t* MD5_final(struct Hash *h) 00098 { 00099 MD5_Context *ctx = (MD5_Context *)h; 00100 unsigned count; 00101 unsigned char *p; 00102 00103 /* Compute number of bytes mod 64 */ 00104 count = (ctx->bits >> 3) & 0x3F; 00105 00106 /* Set the first char of padding to 0x80. This is safe since there is 00107 always at least one byte free */ 00108 p = ctx->in + count; 00109 *p++ = 0x80; 00110 00111 /* Bytes of padding needed to make 64 bytes */ 00112 count = 64 - 1 - count; 00113 00114 /* Pad out to 56 mod 64 */ 00115 if (count < 8) { 00116 /* Two lots of padding: Pad the first block to 64 bytes */ 00117 memset(p, 0, count); 00118 byteReverse((uint32_t*)ctx->in, 16); 00119 MD5Transform(ctx->buf, (uint32_t *) ctx->in); 00120 00121 /* Now fill the next block with 56 bytes */ 00122 memset(ctx->in, 0, 56); 00123 } else { 00124 /* Pad block to 56 bytes */ 00125 memset(p, 0, count - 8); 00126 } 00127 byteReverse((uint32_t*)ctx->in, 14); 00128 00129 /* Append length in bits and transform */ 00130 ((uint32_t*) ctx->in)[14] = (uint32_t)ctx->bits; 00131 ((uint32_t*) ctx->in)[15] = (uint32_t)(ctx->bits >> 32); 00132 00133 MD5Transform(ctx->buf, (uint32_t *) ctx->in); 00134 byteReverse((uint32_t*)ctx->buf, 4); 00135 00136 PURGE(ctx->in); 00137 PURGE(ctx->bits); 00138 00139 return (uint8_t *)ctx->buf; 00140 } 00141 00142 00143 /* The four core functions - F1 is optimized somewhat */ 00144 00145 /* #define F1(x, y, z) (x & y | ~x & z) */ 00146 #define F1(x, y, z) (z ^ (x & (y ^ z))) 00147 #define F2(x, y, z) F1(z, x, y) 00148 #define F3(x, y, z) (x ^ y ^ z) 00149 #define F4(x, y, z) (y ^ (x | ~z)) 00150 00151 /* This is the central step in the MD5 algorithm. */ 00152 #define MD5STEP(f, w, x, y, z, data, s) \ 00153 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 00154 00155 /* 00156 * The core of the MD5 algorithm, this alters an existing MD5 hash to 00157 * reflect the addition of 16 longwords of new data. MD5Update blocks 00158 * the data and converts bytes into longwords for this routine. 00159 */ 00160 static void MD5Transform(uint32_t buf[4], uint32_t in[16]) 00161 { 00162 register uint32_t a, b, c, d; 00163 00164 a = buf[0]; 00165 b = buf[1]; 00166 c = buf[2]; 00167 d = buf[3]; 00168 00169 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 00170 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 00171 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 00172 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 00173 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 00174 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 00175 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 00176 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 00177 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 00178 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 00179 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 00180 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 00181 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 00182 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 00183 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 00184 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 00185 00186 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 00187 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 00188 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 00189 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 00190 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 00191 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 00192 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 00193 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 00194 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 00195 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 00196 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 00197 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 00198 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 00199 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 00200 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 00201 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 00202 00203 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 00204 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 00205 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 00206 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 00207 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 00208 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 00209 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 00210 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 00211 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 00212 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 00213 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 00214 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 00215 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 00216 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 00217 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 00218 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 00219 00220 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 00221 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 00222 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 00223 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 00224 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 00225 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 00226 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 00227 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 00228 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 00229 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 00230 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 00231 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 00232 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 00233 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 00234 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 00235 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 00236 00237 buf[0] += a; 00238 buf[1] += b; 00239 buf[2] += c; 00240 buf[3] += d; 00241 } 00242 00243 /*******************************************************************/ 00244 00245 void MD5_init(MD5_Context *ctx) 00246 { 00247 ctx->h.begin = MD5_begin; 00248 ctx->h.update = MD5_update; 00249 ctx->h.final = MD5_final; 00250 ctx->h.digest_len = 16; 00251 ctx->h.block_len = 64; 00252 }