xref: /illumos-gate/usr/src/lib/libsqlite/src/md5.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate ** SQLite uses this code for testing only.  It is not a part of
37c478bd9Sstevel@tonic-gate ** the SQLite library.  This file implements two new TCL commands
47c478bd9Sstevel@tonic-gate ** "md5" and "md5file" that compute md5 checksums on arbitrary text
57c478bd9Sstevel@tonic-gate ** and on complete files.  These commands are used by the "testfixture"
67c478bd9Sstevel@tonic-gate ** program to help verify the correct operation of the SQLite library.
77c478bd9Sstevel@tonic-gate **
87c478bd9Sstevel@tonic-gate ** The original use of these TCL commands was to test the ROLLBACK
97c478bd9Sstevel@tonic-gate ** feature of SQLite.  First compute the MD5-checksum of the database.
107c478bd9Sstevel@tonic-gate ** Then make some changes but rollback the changes rather than commit
117c478bd9Sstevel@tonic-gate ** them.  Compute a second MD5-checksum of the file and verify that the
127c478bd9Sstevel@tonic-gate ** two checksums are the same.  Such is the original use of this code.
137c478bd9Sstevel@tonic-gate ** New uses may have been added since this comment was written.
147c478bd9Sstevel@tonic-gate */
157c478bd9Sstevel@tonic-gate /*
167c478bd9Sstevel@tonic-gate  * This code implements the MD5 message-digest algorithm.
177c478bd9Sstevel@tonic-gate  * The algorithm is due to Ron Rivest.  This code was
187c478bd9Sstevel@tonic-gate  * written by Colin Plumb in 1993, no copyright is claimed.
197c478bd9Sstevel@tonic-gate  * This code is in the public domain; do with it what you wish.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * Equivalent code is available from RSA Data Security, Inc.
227c478bd9Sstevel@tonic-gate  * This code has been tested against that, and is equivalent,
237c478bd9Sstevel@tonic-gate  * except that you don't need to include two pages of legalese
247c478bd9Sstevel@tonic-gate  * with every copy.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * To compute the message digest of a chunk of bytes, declare an
277c478bd9Sstevel@tonic-gate  * MD5Context structure, pass it to MD5Init, call MD5Update as
287c478bd9Sstevel@tonic-gate  * needed on buffers full of bytes, and then call MD5Final, which
297c478bd9Sstevel@tonic-gate  * will fill a supplied 16-byte array with the digest.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate #include <tcl.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include "sqlite.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * If compiled on a machine that doesn't have a 32-bit integer,
377c478bd9Sstevel@tonic-gate  * you just set "uint32" to the appropriate datatype for an
387c478bd9Sstevel@tonic-gate  * unsigned 32-bit integer.  For example:
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  *       cc -Duint32='unsigned long' md5.c
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate #ifndef uint32
447c478bd9Sstevel@tonic-gate #  define uint32 unsigned int
457c478bd9Sstevel@tonic-gate #endif
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate struct Context {
487c478bd9Sstevel@tonic-gate   uint32 buf[4];
497c478bd9Sstevel@tonic-gate   uint32 bits[2];
507c478bd9Sstevel@tonic-gate   unsigned char in[64];
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate typedef char MD5Context[88];
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * Note: this code is harmless on little-endian machines.
567c478bd9Sstevel@tonic-gate  */
byteReverse(unsigned char * buf,unsigned longs)577c478bd9Sstevel@tonic-gate static void byteReverse (unsigned char *buf, unsigned longs){
587c478bd9Sstevel@tonic-gate         uint32 t;
597c478bd9Sstevel@tonic-gate         do {
607c478bd9Sstevel@tonic-gate                 t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 |
617c478bd9Sstevel@tonic-gate                             ((unsigned)buf[1]<<8 | buf[0]);
627c478bd9Sstevel@tonic-gate                 *(uint32 *)buf = t;
637c478bd9Sstevel@tonic-gate                 buf += 4;
647c478bd9Sstevel@tonic-gate         } while (--longs);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate /* The four core functions - F1 is optimized somewhat */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* #define F1(x, y, z) (x & y | ~x & z) */
697c478bd9Sstevel@tonic-gate #define F1(x, y, z) (z ^ (x & (y ^ z)))
707c478bd9Sstevel@tonic-gate #define F2(x, y, z) F1(z, x, y)
717c478bd9Sstevel@tonic-gate #define F3(x, y, z) (x ^ y ^ z)
727c478bd9Sstevel@tonic-gate #define F4(x, y, z) (y ^ (x | ~z))
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* This is the central step in the MD5 algorithm. */
757c478bd9Sstevel@tonic-gate #define MD5STEP(f, w, x, y, z, data, s) \
767c478bd9Sstevel@tonic-gate         ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * The core of the MD5 algorithm, this alters an existing MD5 hash to
807c478bd9Sstevel@tonic-gate  * reflect the addition of 16 longwords of new data.  MD5Update blocks
817c478bd9Sstevel@tonic-gate  * the data and converts bytes into longwords for this routine.
827c478bd9Sstevel@tonic-gate  */
MD5Transform(uint32 buf[4],const uint32 in[16])837c478bd9Sstevel@tonic-gate static void MD5Transform(uint32 buf[4], const uint32 in[16]){
847c478bd9Sstevel@tonic-gate         register uint32 a, b, c, d;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate         a = buf[0];
877c478bd9Sstevel@tonic-gate         b = buf[1];
887c478bd9Sstevel@tonic-gate         c = buf[2];
897c478bd9Sstevel@tonic-gate         d = buf[3];
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate         MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478,  7);
927c478bd9Sstevel@tonic-gate         MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
937c478bd9Sstevel@tonic-gate         MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
947c478bd9Sstevel@tonic-gate         MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
957c478bd9Sstevel@tonic-gate         MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf,  7);
967c478bd9Sstevel@tonic-gate         MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
977c478bd9Sstevel@tonic-gate         MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
987c478bd9Sstevel@tonic-gate         MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
997c478bd9Sstevel@tonic-gate         MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8,  7);
1007c478bd9Sstevel@tonic-gate         MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
1017c478bd9Sstevel@tonic-gate         MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
1027c478bd9Sstevel@tonic-gate         MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
1037c478bd9Sstevel@tonic-gate         MD5STEP(F1, a, b, c, d, in[12]+0x6b901122,  7);
1047c478bd9Sstevel@tonic-gate         MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
1057c478bd9Sstevel@tonic-gate         MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
1067c478bd9Sstevel@tonic-gate         MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate         MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562,  5);
1097c478bd9Sstevel@tonic-gate         MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340,  9);
1107c478bd9Sstevel@tonic-gate         MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
1117c478bd9Sstevel@tonic-gate         MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
1127c478bd9Sstevel@tonic-gate         MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d,  5);
1137c478bd9Sstevel@tonic-gate         MD5STEP(F2, d, a, b, c, in[10]+0x02441453,  9);
1147c478bd9Sstevel@tonic-gate         MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
1157c478bd9Sstevel@tonic-gate         MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
1167c478bd9Sstevel@tonic-gate         MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6,  5);
1177c478bd9Sstevel@tonic-gate         MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6,  9);
1187c478bd9Sstevel@tonic-gate         MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
1197c478bd9Sstevel@tonic-gate         MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
1207c478bd9Sstevel@tonic-gate         MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905,  5);
1217c478bd9Sstevel@tonic-gate         MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8,  9);
1227c478bd9Sstevel@tonic-gate         MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
1237c478bd9Sstevel@tonic-gate         MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate         MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942,  4);
1267c478bd9Sstevel@tonic-gate         MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
1277c478bd9Sstevel@tonic-gate         MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
1287c478bd9Sstevel@tonic-gate         MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
1297c478bd9Sstevel@tonic-gate         MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44,  4);
1307c478bd9Sstevel@tonic-gate         MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
1317c478bd9Sstevel@tonic-gate         MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
1327c478bd9Sstevel@tonic-gate         MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
1337c478bd9Sstevel@tonic-gate         MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6,  4);
1347c478bd9Sstevel@tonic-gate         MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
1357c478bd9Sstevel@tonic-gate         MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
1367c478bd9Sstevel@tonic-gate         MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
1377c478bd9Sstevel@tonic-gate         MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039,  4);
1387c478bd9Sstevel@tonic-gate         MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
1397c478bd9Sstevel@tonic-gate         MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
1407c478bd9Sstevel@tonic-gate         MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate         MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244,  6);
1437c478bd9Sstevel@tonic-gate         MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
1447c478bd9Sstevel@tonic-gate         MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
1457c478bd9Sstevel@tonic-gate         MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
1467c478bd9Sstevel@tonic-gate         MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3,  6);
1477c478bd9Sstevel@tonic-gate         MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
1487c478bd9Sstevel@tonic-gate         MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
1497c478bd9Sstevel@tonic-gate         MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
1507c478bd9Sstevel@tonic-gate         MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f,  6);
1517c478bd9Sstevel@tonic-gate         MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
1527c478bd9Sstevel@tonic-gate         MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
1537c478bd9Sstevel@tonic-gate         MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
1547c478bd9Sstevel@tonic-gate         MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82,  6);
1557c478bd9Sstevel@tonic-gate         MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
1567c478bd9Sstevel@tonic-gate         MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
1577c478bd9Sstevel@tonic-gate         MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate         buf[0] += a;
1607c478bd9Sstevel@tonic-gate         buf[1] += b;
1617c478bd9Sstevel@tonic-gate         buf[2] += c;
1627c478bd9Sstevel@tonic-gate         buf[3] += d;
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
1677c478bd9Sstevel@tonic-gate  * initialization constants.
1687c478bd9Sstevel@tonic-gate  */
MD5Init(MD5Context * pCtx)1697c478bd9Sstevel@tonic-gate static void MD5Init(MD5Context *pCtx){
1707c478bd9Sstevel@tonic-gate         struct Context *ctx = (struct Context *)pCtx;
1717c478bd9Sstevel@tonic-gate         ctx->buf[0] = 0x67452301;
1727c478bd9Sstevel@tonic-gate         ctx->buf[1] = 0xefcdab89;
1737c478bd9Sstevel@tonic-gate         ctx->buf[2] = 0x98badcfe;
1747c478bd9Sstevel@tonic-gate         ctx->buf[3] = 0x10325476;
1757c478bd9Sstevel@tonic-gate         ctx->bits[0] = 0;
1767c478bd9Sstevel@tonic-gate         ctx->bits[1] = 0;
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * Update context to reflect the concatenation of another buffer full
1817c478bd9Sstevel@tonic-gate  * of bytes.
1827c478bd9Sstevel@tonic-gate  */
183*1da57d55SToomas Soome static
MD5Update(MD5Context * pCtx,const unsigned char * buf,unsigned int len)1847c478bd9Sstevel@tonic-gate void MD5Update(MD5Context *pCtx, const unsigned char *buf, unsigned int len){
1857c478bd9Sstevel@tonic-gate         struct Context *ctx = (struct Context *)pCtx;
1867c478bd9Sstevel@tonic-gate         uint32 t;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate         /* Update bitcount */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate         t = ctx->bits[0];
1917c478bd9Sstevel@tonic-gate         if ((ctx->bits[0] = t + ((uint32)len << 3)) < t)
1927c478bd9Sstevel@tonic-gate                 ctx->bits[1]++; /* Carry from low to high */
1937c478bd9Sstevel@tonic-gate         ctx->bits[1] += len >> 29;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate         t = (t >> 3) & 0x3f;    /* Bytes already in shsInfo->data */
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate         /* Handle any leading odd-sized chunks */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate         if ( t ) {
2007c478bd9Sstevel@tonic-gate                 unsigned char *p = (unsigned char *)ctx->in + t;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate                 t = 64-t;
2037c478bd9Sstevel@tonic-gate                 if (len < t) {
2047c478bd9Sstevel@tonic-gate                         memcpy(p, buf, len);
2057c478bd9Sstevel@tonic-gate                         return;
2067c478bd9Sstevel@tonic-gate                 }
2077c478bd9Sstevel@tonic-gate                 memcpy(p, buf, t);
2087c478bd9Sstevel@tonic-gate                 byteReverse(ctx->in, 16);
2097c478bd9Sstevel@tonic-gate                 MD5Transform(ctx->buf, (uint32 *)ctx->in);
2107c478bd9Sstevel@tonic-gate                 buf += t;
2117c478bd9Sstevel@tonic-gate                 len -= t;
2127c478bd9Sstevel@tonic-gate         }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate         /* Process data in 64-byte chunks */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate         while (len >= 64) {
2177c478bd9Sstevel@tonic-gate                 memcpy(ctx->in, buf, 64);
2187c478bd9Sstevel@tonic-gate                 byteReverse(ctx->in, 16);
2197c478bd9Sstevel@tonic-gate                 MD5Transform(ctx->buf, (uint32 *)ctx->in);
2207c478bd9Sstevel@tonic-gate                 buf += 64;
2217c478bd9Sstevel@tonic-gate                 len -= 64;
2227c478bd9Sstevel@tonic-gate         }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate         /* Handle any remaining bytes of data. */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate         memcpy(ctx->in, buf, len);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
230*1da57d55SToomas Soome  * Final wrapup - pad to 64-byte boundary with the bit pattern
2317c478bd9Sstevel@tonic-gate  * 1 0* (64-bit count of bits processed, MSB-first)
2327c478bd9Sstevel@tonic-gate  */
MD5Final(unsigned char digest[16],MD5Context * pCtx)2337c478bd9Sstevel@tonic-gate static void MD5Final(unsigned char digest[16], MD5Context *pCtx){
2347c478bd9Sstevel@tonic-gate         struct Context *ctx = (struct Context *)pCtx;
2357c478bd9Sstevel@tonic-gate         unsigned count;
2367c478bd9Sstevel@tonic-gate         unsigned char *p;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate         /* Compute number of bytes mod 64 */
2397c478bd9Sstevel@tonic-gate         count = (ctx->bits[0] >> 3) & 0x3F;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate         /* Set the first char of padding to 0x80.  This is safe since there is
2427c478bd9Sstevel@tonic-gate            always at least one byte free */
2437c478bd9Sstevel@tonic-gate         p = ctx->in + count;
2447c478bd9Sstevel@tonic-gate         *p++ = 0x80;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate         /* Bytes of padding needed to make 64 bytes */
2477c478bd9Sstevel@tonic-gate         count = 64 - 1 - count;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate         /* Pad out to 56 mod 64 */
2507c478bd9Sstevel@tonic-gate         if (count < 8) {
2517c478bd9Sstevel@tonic-gate                 /* Two lots of padding:  Pad the first block to 64 bytes */
2527c478bd9Sstevel@tonic-gate                 memset(p, 0, count);
2537c478bd9Sstevel@tonic-gate                 byteReverse(ctx->in, 16);
2547c478bd9Sstevel@tonic-gate                 MD5Transform(ctx->buf, (uint32 *)ctx->in);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate                 /* Now fill the next block with 56 bytes */
2577c478bd9Sstevel@tonic-gate                 memset(ctx->in, 0, 56);
2587c478bd9Sstevel@tonic-gate         } else {
2597c478bd9Sstevel@tonic-gate                 /* Pad block to 56 bytes */
2607c478bd9Sstevel@tonic-gate                 memset(p, 0, count-8);
2617c478bd9Sstevel@tonic-gate         }
2627c478bd9Sstevel@tonic-gate         byteReverse(ctx->in, 14);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate         /* Append length in bits and transform */
2657c478bd9Sstevel@tonic-gate         ((uint32 *)ctx->in)[ 14 ] = ctx->bits[0];
2667c478bd9Sstevel@tonic-gate         ((uint32 *)ctx->in)[ 15 ] = ctx->bits[1];
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate         MD5Transform(ctx->buf, (uint32 *)ctx->in);
2697c478bd9Sstevel@tonic-gate         byteReverse((unsigned char *)ctx->buf, 4);
2707c478bd9Sstevel@tonic-gate         memcpy(digest, ctx->buf, 16);
2717c478bd9Sstevel@tonic-gate         memset(ctx, 0, sizeof(ctx));    /* In case it's sensitive */
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate ** Convert a digest into base-16.  digest should be declared as
2767c478bd9Sstevel@tonic-gate ** "unsigned char digest[16]" in the calling function.  The MD5
2777c478bd9Sstevel@tonic-gate ** digest is stored in the first 16 bytes.  zBuf should
2787c478bd9Sstevel@tonic-gate ** be "char zBuf[33]".
2797c478bd9Sstevel@tonic-gate */
DigestToBase16(unsigned char * digest,char * zBuf)2807c478bd9Sstevel@tonic-gate static void DigestToBase16(unsigned char *digest, char *zBuf){
2817c478bd9Sstevel@tonic-gate   static char const zEncode[] = "0123456789abcdef";
2827c478bd9Sstevel@tonic-gate   int i, j;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate   for(j=i=0; i<16; i++){
2857c478bd9Sstevel@tonic-gate     int a = digest[i];
2867c478bd9Sstevel@tonic-gate     zBuf[j++] = zEncode[(a>>4)&0xf];
2877c478bd9Sstevel@tonic-gate     zBuf[j++] = zEncode[a & 0xf];
2887c478bd9Sstevel@tonic-gate   }
2897c478bd9Sstevel@tonic-gate   zBuf[j] = 0;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate ** A TCL command for md5.  The argument is the text to be hashed.  The
294*1da57d55SToomas Soome ** Result is the hash in base64.
2957c478bd9Sstevel@tonic-gate */
md5_cmd(void * cd,Tcl_Interp * interp,int argc,const char ** argv)2967c478bd9Sstevel@tonic-gate static int md5_cmd(void*cd, Tcl_Interp *interp, int argc, const char **argv){
2977c478bd9Sstevel@tonic-gate   MD5Context ctx;
2987c478bd9Sstevel@tonic-gate   unsigned char digest[16];
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate   if( argc!=2 ){
301*1da57d55SToomas Soome     Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
3027c478bd9Sstevel@tonic-gate         " TEXT\"", 0);
3037c478bd9Sstevel@tonic-gate     return TCL_ERROR;
3047c478bd9Sstevel@tonic-gate   }
3057c478bd9Sstevel@tonic-gate   MD5Init(&ctx);
3067c478bd9Sstevel@tonic-gate   MD5Update(&ctx, (unsigned char*)argv[1], (unsigned)strlen(argv[1]));
3077c478bd9Sstevel@tonic-gate   MD5Final(digest, &ctx);
3087c478bd9Sstevel@tonic-gate   DigestToBase16(digest, interp->result);
3097c478bd9Sstevel@tonic-gate   return TCL_OK;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate ** A TCL command to take the md5 hash of a file.  The argument is the
3147c478bd9Sstevel@tonic-gate ** name of the file.
3157c478bd9Sstevel@tonic-gate */
md5file_cmd(void * cd,Tcl_Interp * interp,int argc,const char ** argv)3167c478bd9Sstevel@tonic-gate static int md5file_cmd(void*cd, Tcl_Interp*interp, int argc, const char **argv){
3177c478bd9Sstevel@tonic-gate   FILE *in;
3187c478bd9Sstevel@tonic-gate   MD5Context ctx;
3197c478bd9Sstevel@tonic-gate   unsigned char digest[16];
3207c478bd9Sstevel@tonic-gate   char zBuf[10240];
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate   if( argc!=2 ){
323*1da57d55SToomas Soome     Tcl_AppendResult(interp,"wrong # args: should be \"", argv[0],
3247c478bd9Sstevel@tonic-gate         " FILENAME\"", 0);
3257c478bd9Sstevel@tonic-gate     return TCL_ERROR;
3267c478bd9Sstevel@tonic-gate   }
3277c478bd9Sstevel@tonic-gate   in = fopen(argv[1],"rb");
3287c478bd9Sstevel@tonic-gate   if( in==0 ){
329*1da57d55SToomas Soome     Tcl_AppendResult(interp,"unable to open file \"", argv[1],
3307c478bd9Sstevel@tonic-gate          "\" for reading", 0);
3317c478bd9Sstevel@tonic-gate     return TCL_ERROR;
3327c478bd9Sstevel@tonic-gate   }
3337c478bd9Sstevel@tonic-gate   MD5Init(&ctx);
3347c478bd9Sstevel@tonic-gate   for(;;){
3357c478bd9Sstevel@tonic-gate     int n;
3367c478bd9Sstevel@tonic-gate     n = fread(zBuf, 1, sizeof(zBuf), in);
3377c478bd9Sstevel@tonic-gate     if( n<=0 ) break;
3387c478bd9Sstevel@tonic-gate     MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n);
3397c478bd9Sstevel@tonic-gate   }
3407c478bd9Sstevel@tonic-gate   fclose(in);
3417c478bd9Sstevel@tonic-gate   MD5Final(digest, &ctx);
3427c478bd9Sstevel@tonic-gate   DigestToBase16(digest, interp->result);
3437c478bd9Sstevel@tonic-gate   return TCL_OK;
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate ** Register the two TCL commands above with the TCL interpreter.
3487c478bd9Sstevel@tonic-gate */
Md5_Init(Tcl_Interp * interp)3497c478bd9Sstevel@tonic-gate int Md5_Init(Tcl_Interp *interp){
3507c478bd9Sstevel@tonic-gate   Tcl_CreateCommand(interp, "md5", (Tcl_CmdProc*)md5_cmd, 0, 0);
3517c478bd9Sstevel@tonic-gate   Tcl_CreateCommand(interp, "md5file", (Tcl_CmdProc*)md5file_cmd, 0, 0);
3527c478bd9Sstevel@tonic-gate   return TCL_OK;
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate ** During testing, the special md5sum() aggregate function is available.
3577c478bd9Sstevel@tonic-gate ** inside SQLite.  The following routines implement that function.
3587c478bd9Sstevel@tonic-gate */
md5step(sqlite_func * context,int argc,const char ** argv)3597c478bd9Sstevel@tonic-gate static void md5step(sqlite_func *context, int argc, const char **argv){
3607c478bd9Sstevel@tonic-gate   MD5Context *p;
3617c478bd9Sstevel@tonic-gate   int i;
3627c478bd9Sstevel@tonic-gate   if( argc<1 ) return;
3637c478bd9Sstevel@tonic-gate   p = sqlite_aggregate_context(context, sizeof(*p));
3647c478bd9Sstevel@tonic-gate   if( p==0 ) return;
3657c478bd9Sstevel@tonic-gate   if( sqlite_aggregate_count(context)==1 ){
3667c478bd9Sstevel@tonic-gate     MD5Init(p);
3677c478bd9Sstevel@tonic-gate   }
3687c478bd9Sstevel@tonic-gate   for(i=0; i<argc; i++){
3697c478bd9Sstevel@tonic-gate     if( argv[i] ){
3707c478bd9Sstevel@tonic-gate       MD5Update(p, (unsigned char*)argv[i], strlen(argv[i]));
3717c478bd9Sstevel@tonic-gate     }
3727c478bd9Sstevel@tonic-gate   }
3737c478bd9Sstevel@tonic-gate }
md5finalize(sqlite_func * context)3747c478bd9Sstevel@tonic-gate static void md5finalize(sqlite_func *context){
3757c478bd9Sstevel@tonic-gate   MD5Context *p;
3767c478bd9Sstevel@tonic-gate   unsigned char digest[16];
3777c478bd9Sstevel@tonic-gate   char zBuf[33];
3787c478bd9Sstevel@tonic-gate   p = sqlite_aggregate_context(context, sizeof(*p));
3797c478bd9Sstevel@tonic-gate   MD5Final(digest,p);
3807c478bd9Sstevel@tonic-gate   DigestToBase16(digest, zBuf);
3817c478bd9Sstevel@tonic-gate   sqlite_set_result_string(context, zBuf, strlen(zBuf));
3827c478bd9Sstevel@tonic-gate }
Md5_Register(sqlite * db)3837c478bd9Sstevel@tonic-gate void Md5_Register(sqlite *db){
3847c478bd9Sstevel@tonic-gate   sqlite_create_aggregate(db, "md5sum", -1, md5step, md5finalize, 0);
3857c478bd9Sstevel@tonic-gate }
386