1 /* 2 ** SQLite uses this code for testing only. It is not a part of 3 ** the SQLite library. This file implements two new TCL commands 4 ** "md5" and "md5file" that compute md5 checksums on arbitrary text 5 ** and on complete files. These commands are used by the "testfixture" 6 ** program to help verify the correct operation of the SQLite library. 7 ** 8 ** The original use of these TCL commands was to test the ROLLBACK 9 ** feature of SQLite. First compute the MD5-checksum of the database. 10 ** Then make some changes but rollback the changes rather than commit 11 ** them. Compute a second MD5-checksum of the file and verify that the 12 ** two checksums are the same. Such is the original use of this code. 13 ** New uses may have been added since this comment was written. 14 */ 15 /* 16 * This code implements the MD5 message-digest algorithm. 17 * The algorithm is due to Ron Rivest. This code was 18 * written by Colin Plumb in 1993, no copyright is claimed. 19 * This code is in the public domain; do with it what you wish. 20 * 21 * Equivalent code is available from RSA Data Security, Inc. 22 * This code has been tested against that, and is equivalent, 23 * except that you don't need to include two pages of legalese 24 * with every copy. 25 * 26 * To compute the message digest of a chunk of bytes, declare an 27 * MD5Context structure, pass it to MD5Init, call MD5Update as 28 * needed on buffers full of bytes, and then call MD5Final, which 29 * will fill a supplied 16-byte array with the digest. 30 */ 31 #include <tcl.h> 32 #include <string.h> 33 #include "sqlite3.h" 34 35 /* 36 * If compiled on a machine that doesn't have a 32-bit integer, 37 * you just set "uint32" to the appropriate datatype for an 38 * unsigned 32-bit integer. For example: 39 * 40 * cc -Duint32='unsigned long' md5.c 41 * 42 */ 43 #ifndef uint32 44 # define uint32 unsigned int 45 #endif 46 47 struct Context { 48 int isInit; 49 uint32 buf[4]; 50 uint32 bits[2]; 51 unsigned char in[64]; 52 }; 53 typedef struct Context MD5Context; 54 55 /* 56 * Note: this code is harmless on little-endian machines. 57 */ 58 static void byteReverse (unsigned char *buf, unsigned longs){ 59 uint32 t; 60 do { 61 t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | 62 ((unsigned)buf[1]<<8 | buf[0]); 63 *(uint32 *)buf = t; 64 buf += 4; 65 } while (--longs); 66 } 67 /* The four core functions - F1 is optimized somewhat */ 68 69 /* #define F1(x, y, z) (x & y | ~x & z) */ 70 #define F1(x, y, z) (z ^ (x & (y ^ z))) 71 #define F2(x, y, z) F1(z, x, y) 72 #define F3(x, y, z) (x ^ y ^ z) 73 #define F4(x, y, z) (y ^ (x | ~z)) 74 75 /* This is the central step in the MD5 algorithm. */ 76 #define MD5STEP(f, w, x, y, z, data, s) \ 77 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 78 79 /* 80 * The core of the MD5 algorithm, this alters an existing MD5 hash to 81 * reflect the addition of 16 longwords of new data. MD5Update blocks 82 * the data and converts bytes into longwords for this routine. 83 */ 84 static void MD5Transform(uint32 buf[4], const uint32 in[16]){ 85 register uint32 a, b, c, d; 86 87 a = buf[0]; 88 b = buf[1]; 89 c = buf[2]; 90 d = buf[3]; 91 92 MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); 93 MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); 94 MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); 95 MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); 96 MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); 97 MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); 98 MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); 99 MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); 100 MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); 101 MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); 102 MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); 103 MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); 104 MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); 105 MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); 106 MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); 107 MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); 108 109 MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); 110 MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); 111 MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); 112 MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); 113 MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); 114 MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); 115 MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); 116 MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); 117 MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); 118 MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); 119 MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); 120 MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); 121 MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); 122 MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); 123 MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); 124 MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); 125 126 MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); 127 MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); 128 MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); 129 MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); 130 MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); 131 MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); 132 MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); 133 MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); 134 MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); 135 MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); 136 MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); 137 MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); 138 MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); 139 MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); 140 MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); 141 MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); 142 143 MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); 144 MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); 145 MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); 146 MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); 147 MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); 148 MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); 149 MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); 150 MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); 151 MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); 152 MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); 153 MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); 154 MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); 155 MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); 156 MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); 157 MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); 158 MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); 159 160 buf[0] += a; 161 buf[1] += b; 162 buf[2] += c; 163 buf[3] += d; 164 } 165 166 /* 167 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 168 * initialization constants. 169 */ 170 static void MD5Init(MD5Context *ctx){ 171 ctx->isInit = 1; 172 ctx->buf[0] = 0x67452301; 173 ctx->buf[1] = 0xefcdab89; 174 ctx->buf[2] = 0x98badcfe; 175 ctx->buf[3] = 0x10325476; 176 ctx->bits[0] = 0; 177 ctx->bits[1] = 0; 178 } 179 180 /* 181 * Update context to reflect the concatenation of another buffer full 182 * of bytes. 183 */ 184 static 185 void MD5Update(MD5Context *pCtx, const unsigned char *buf, unsigned int len){ 186 struct Context *ctx = (struct Context *)pCtx; 187 uint32 t; 188 189 /* Update bitcount */ 190 191 t = ctx->bits[0]; 192 if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) 193 ctx->bits[1]++; /* Carry from low to high */ 194 ctx->bits[1] += len >> 29; 195 196 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 197 198 /* Handle any leading odd-sized chunks */ 199 200 if ( t ) { 201 unsigned char *p = (unsigned char *)ctx->in + t; 202 203 t = 64-t; 204 if (len < t) { 205 memcpy(p, buf, len); 206 return; 207 } 208 memcpy(p, buf, t); 209 byteReverse(ctx->in, 16); 210 MD5Transform(ctx->buf, (uint32 *)ctx->in); 211 buf += t; 212 len -= t; 213 } 214 215 /* Process data in 64-byte chunks */ 216 217 while (len >= 64) { 218 memcpy(ctx->in, buf, 64); 219 byteReverse(ctx->in, 16); 220 MD5Transform(ctx->buf, (uint32 *)ctx->in); 221 buf += 64; 222 len -= 64; 223 } 224 225 /* Handle any remaining bytes of data. */ 226 227 memcpy(ctx->in, buf, len); 228 } 229 230 /* 231 * Final wrapup - pad to 64-byte boundary with the bit pattern 232 * 1 0* (64-bit count of bits processed, MSB-first) 233 */ 234 static void MD5Final(unsigned char digest[16], MD5Context *pCtx){ 235 struct Context *ctx = (struct Context *)pCtx; 236 unsigned count; 237 unsigned char *p; 238 239 /* Compute number of bytes mod 64 */ 240 count = (ctx->bits[0] >> 3) & 0x3F; 241 242 /* Set the first char of padding to 0x80. This is safe since there is 243 always at least one byte free */ 244 p = ctx->in + count; 245 *p++ = 0x80; 246 247 /* Bytes of padding needed to make 64 bytes */ 248 count = 64 - 1 - count; 249 250 /* Pad out to 56 mod 64 */ 251 if (count < 8) { 252 /* Two lots of padding: Pad the first block to 64 bytes */ 253 memset(p, 0, count); 254 byteReverse(ctx->in, 16); 255 MD5Transform(ctx->buf, (uint32 *)ctx->in); 256 257 /* Now fill the next block with 56 bytes */ 258 memset(ctx->in, 0, 56); 259 } else { 260 /* Pad block to 56 bytes */ 261 memset(p, 0, count-8); 262 } 263 byteReverse(ctx->in, 14); 264 265 /* Append length in bits and transform */ 266 ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0]; 267 ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1]; 268 269 MD5Transform(ctx->buf, (uint32 *)ctx->in); 270 byteReverse((unsigned char *)ctx->buf, 4); 271 memcpy(digest, ctx->buf, 16); 272 memset(ctx, 0, sizeof(ctx)); /* In case it is sensitive */ 273 } 274 275 /* 276 ** Convert a digest into base-16. digest should be declared as 277 ** "unsigned char digest[16]" in the calling function. The MD5 278 ** digest is stored in the first 16 bytes. zBuf should 279 ** be "char zBuf[33]". 280 */ 281 static void DigestToBase16(unsigned char *digest, char *zBuf){ 282 static char const zEncode[] = "0123456789abcdef"; 283 int i, j; 284 285 for(j=i=0; i<16; i++){ 286 int a = digest[i]; 287 zBuf[j++] = zEncode[(a>>4)&0xf]; 288 zBuf[j++] = zEncode[a & 0xf]; 289 } 290 zBuf[j] = 0; 291 } 292 293 /* 294 ** A TCL command for md5. The argument is the text to be hashed. The 295 ** Result is the hash in base64. 296 */ 297 static int md5_cmd(void*cd, Tcl_Interp *interp, int argc, const char **argv){ 298 MD5Context ctx; 299 unsigned char digest[16]; 300 301 if( argc!=2 ){ 302 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 303 " TEXT\"", 0); 304 return TCL_ERROR; 305 } 306 MD5Init(&ctx); 307 MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1])); 308 MD5Final(digest, &ctx); 309 DigestToBase16(digest, interp->result); 310 return TCL_OK; 311 } 312 313 /* 314 ** A TCL command to take the md5 hash of a file. The argument is the 315 ** name of the file. 316 */ 317 static int md5file_cmd(void*cd, Tcl_Interp*interp, int argc, const char **argv){ 318 FILE *in; 319 MD5Context ctx; 320 unsigned char digest[16]; 321 char zBuf[10240]; 322 323 if( argc!=2 ){ 324 Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0], 325 " FILENAME\"", 0); 326 return TCL_ERROR; 327 } 328 in = fopen(argv[1],"rb"); 329 if( in==0 ){ 330 Tcl_AppendResult(interp,"unable to open file \"", argv[1], 331 "\" for reading", 0); 332 return TCL_ERROR; 333 } 334 MD5Init(&ctx); 335 for(;;){ 336 int n; 337 n = fread(zBuf, 1, sizeof(zBuf), in); 338 if( n<=0 ) break; 339 MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n); 340 } 341 fclose(in); 342 MD5Final(digest, &ctx); 343 DigestToBase16(digest, interp->result); 344 return TCL_OK; 345 } 346 347 /* 348 ** Register the two TCL commands above with the TCL interpreter. 349 */ 350 int Md5_Init(Tcl_Interp *interp){ 351 Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, 0, 0); 352 Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, 0, 0); 353 return TCL_OK; 354 } 355 356 /* 357 ** During testing, the special md5sum() aggregate function is available. 358 ** inside SQLite. The following routines implement that function. 359 */ 360 static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ 361 MD5Context *p; 362 int i; 363 if( argc<1 ) return; 364 p = sqlite3_aggregate_context(context, sizeof(*p)); 365 if( p==0 ) return; 366 if( !p->isInit ){ 367 MD5Init(p); 368 } 369 for(i=0; i<argc; i++){ 370 const char *zData = (char*)sqlite3_value_text(argv[i]); 371 if( zData ){ 372 MD5Update(p, (unsigned char*)zData, strlen(zData)); 373 } 374 } 375 } 376 static void md5finalize(sqlite3_context *context){ 377 MD5Context *p; 378 unsigned char digest[16]; 379 char zBuf[33]; 380 p = sqlite3_aggregate_context(context, sizeof(*p)); 381 MD5Final(digest,p); 382 DigestToBase16(digest, zBuf); 383 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 384 } 385 int Md5_Register(sqlite3 *db){ 386 int rc = sqlite3_create_function(db, "md5sum", -1, SQLITE_UTF8, 0, 0, 387 md5step, md5finalize); 388 sqlite3_overload_function(db, "md5sum", -1); /* To exercise this API */ 389 return rc; 390 } 391