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