Fossil SCM

fossil-scm / src / md5.c
Blame History Raw 467 lines
1
/*
2
** The code is modified for use in fossil. The original header
3
** comment follows:
4
*/
5
/*
6
* This code implements the MD5 message-digest algorithm.
7
* The algorithm is due to Ron Rivest. This code was
8
* written by Colin Plumb in 1993, no copyright is claimed.
9
* This code is in the public domain; do with it what you wish.
10
*
11
* Equivalent code is available from RSA Data Security, Inc.
12
* This code has been tested against that, and is equivalent,
13
* except that you don't need to include two pages of legalese
14
* with every copy.
15
*
16
* To compute the message digest of a chunk of bytes, declare an
17
* MD5Context structure, pass it to MD5Init, call MD5Update as
18
* needed on buffers full of bytes, and then call MD5Final, which
19
* will fill a supplied 16-byte array with the digest.
20
*/
21
#include "config.h"
22
#include <string.h>
23
#include <stdio.h>
24
#include <sqlite3.h>
25
#include "md5.h"
26
27
#if 0 /* defined FOSSIL_ENABLE_SSL */
28
29
/* MD5 is deprecated in OpenSSL. So we have to fall back to our own
30
** implementation */
31
32
# include <openssl/md5.h>
33
# define MD5Context MD5_CTX
34
# define MD5Init MD5_Init
35
# define MD5Update MD5_Update
36
# define MD5Final MD5_Final
37
38
#else
39
40
/*
41
* If compiled on a machine that doesn't have a 32-bit integer,
42
* you just set "uint32" to the appropriate datatype for an
43
* unsigned 32-bit integer. For example:
44
*
45
* cc -Duint32='unsigned long' md5.c
46
*
47
*/
48
#ifndef uint32
49
# define uint32 unsigned int
50
#endif
51
52
struct Context {
53
int isInit;
54
uint32 buf[4];
55
uint32 bits[2];
56
unsigned char in[64];
57
};
58
typedef struct Context MD5Context;
59
60
#if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
61
defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
62
defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
63
defined(__arm__) || defined(_WIN32)
64
# define byteReverse(A,B)
65
#else
66
/*
67
* Convert an array of integers to little-endian.
68
* Note: this code is a no-op on little-endian machines.
69
*/
70
static void byteReverse (unsigned char *buf, unsigned longs){
71
uint32 t;
72
do {
73
t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 |
74
((unsigned)buf[1]<<8 | buf[0]);
75
*(uint32 *)buf = t;
76
buf += 4;
77
} while (--longs);
78
}
79
#endif
80
81
/* The four core functions - F1 is optimized somewhat */
82
83
/* #define F1(x, y, z) (x & y | ~x & z) */
84
#define F1(x, y, z) (z ^ (x & (y ^ z)))
85
#define F2(x, y, z) F1(z, x, y)
86
#define F3(x, y, z) (x ^ y ^ z)
87
#define F4(x, y, z) (y ^ (x | ~z))
88
89
/* This is the central step in the MD5 algorithm. */
90
#define MD5STEP(f, w, x, y, z, data, s) \
91
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
92
93
/*
94
* The core of the MD5 algorithm, this alters an existing MD5 hash to
95
* reflect the addition of 16 longwords of new data. MD5Update blocks
96
* the data and converts bytes into longwords for this routine.
97
*/
98
static void MD5Transform(uint32 buf[4], const uint32 in[16]){
99
register uint32 a, b, c, d;
100
101
a = buf[0];
102
b = buf[1];
103
c = buf[2];
104
d = buf[3];
105
106
MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7);
107
MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
108
MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
109
MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
110
MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7);
111
MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
112
MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
113
MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
114
MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7);
115
MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
116
MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
117
MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
118
MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7);
119
MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
120
MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
121
MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
122
123
MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5);
124
MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9);
125
MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
126
MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
127
MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5);
128
MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9);
129
MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
130
MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
131
MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5);
132
MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9);
133
MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
134
MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
135
MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5);
136
MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9);
137
MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
138
MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
139
140
MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4);
141
MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
142
MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
143
MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
144
MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4);
145
MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
146
MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
147
MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
148
MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4);
149
MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
150
MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
151
MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
152
MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4);
153
MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
154
MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
155
MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
156
157
MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6);
158
MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
159
MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
160
MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
161
MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6);
162
MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
163
MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
164
MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
165
MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6);
166
MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
167
MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
168
MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
169
MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6);
170
MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
171
MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
172
MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
173
174
buf[0] += a;
175
buf[1] += b;
176
buf[2] += c;
177
buf[3] += d;
178
}
179
180
/*
181
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
182
* initialization constants.
183
*/
184
static void MD5Init(MD5Context *ctx){
185
ctx->isInit = 1;
186
ctx->buf[0] = 0x67452301;
187
ctx->buf[1] = 0xefcdab89;
188
ctx->buf[2] = 0x98badcfe;
189
ctx->buf[3] = 0x10325476;
190
ctx->bits[0] = 0;
191
ctx->bits[1] = 0;
192
}
193
194
/*
195
* Update context to reflect the concatenation of another buffer full
196
* of bytes.
197
*/
198
static
199
void MD5Update(MD5Context *pCtx, const unsigned char *buf, unsigned int len){
200
struct Context *ctx = (struct Context *)pCtx;
201
uint32 t;
202
203
/* Update bitcount */
204
205
t = ctx->bits[0];
206
if ((ctx->bits[0] = t + ((uint32)len << 3)) < t)
207
ctx->bits[1]++; /* Carry from low to high */
208
ctx->bits[1] += len >> 29;
209
210
t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
211
212
/* Handle any leading odd-sized chunks */
213
214
if ( t ) {
215
unsigned char *p = (unsigned char *)ctx->in + t;
216
217
t = 64-t;
218
if (len < t) {
219
memcpy(p, buf, len);
220
return;
221
}
222
memcpy(p, buf, t);
223
byteReverse(ctx->in, 16);
224
MD5Transform(ctx->buf, (uint32 *)ctx->in);
225
buf += t;
226
len -= t;
227
}
228
229
/* Process data in 64-byte chunks */
230
231
while (len >= 64) {
232
memcpy(ctx->in, buf, 64);
233
byteReverse(ctx->in, 16);
234
MD5Transform(ctx->buf, (uint32 *)ctx->in);
235
buf += 64;
236
len -= 64;
237
}
238
239
/* Handle any remaining bytes of data. */
240
241
memcpy(ctx->in, buf, len);
242
}
243
244
/*
245
* Final wrapup - pad to 64-byte boundary with the bit pattern
246
* 1 0* (64-bit count of bits processed, MSB-first)
247
*/
248
static void MD5Final(unsigned char digest[16], MD5Context *pCtx){
249
struct Context *ctx = (struct Context *)pCtx;
250
unsigned count;
251
unsigned char *p;
252
253
/* Compute number of bytes mod 64 */
254
count = (ctx->bits[0] >> 3) & 0x3F;
255
256
/* Set the first char of padding to 0x80. This is safe since there is
257
always at least one byte free */
258
p = ctx->in + count;
259
*p++ = 0x80;
260
261
/* Bytes of padding needed to make 64 bytes */
262
count = 64 - 1 - count;
263
264
/* Pad out to 56 mod 64 */
265
if (count < 8) {
266
/* Two lots of padding: Pad the first block to 64 bytes */
267
memset(p, 0, count);
268
byteReverse(ctx->in, 16);
269
MD5Transform(ctx->buf, (uint32 *)ctx->in);
270
271
/* Now fill the next block with 56 bytes */
272
memset(ctx->in, 0, 56);
273
} else {
274
/* Pad block to 56 bytes */
275
memset(p, 0, count-8);
276
}
277
byteReverse(ctx->in, 14);
278
279
/* Append length in bits and transform */
280
memcpy(&ctx->in[14*sizeof(uint32)], ctx->bits, 2*sizeof(uint32));
281
282
MD5Transform(ctx->buf, (uint32 *)ctx->in);
283
byteReverse((unsigned char *)ctx->buf, 4);
284
memcpy(digest, ctx->buf, 16);
285
memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
286
}
287
#endif
288
289
/*
290
** Convert a digest into base-16. digest should be declared as
291
** "unsigned char digest[16]" in the calling function. The MD5
292
** digest is stored in the first 16 bytes. zBuf should
293
** be "char zBuf[33]".
294
*/
295
static void DigestToBase16(unsigned char *digest, char *zBuf){
296
static const char zEncode[] = "0123456789abcdef";
297
int i, j;
298
299
for(j=i=0; i<16; i++){
300
int a = digest[i];
301
zBuf[j++] = zEncode[(a>>4)&0xf];
302
zBuf[j++] = zEncode[a & 0xf];
303
}
304
zBuf[j] = 0;
305
}
306
307
/*
308
** The state of an incremental MD5 checksum computation. Only one
309
** such computation can be underway at a time, of course.
310
*/
311
static MD5Context incrCtx;
312
static int incrInit = 0;
313
314
/*
315
** Initialize the incremental MD5 checksum.
316
*/
317
void md5sum_init(void){
318
incrInit = 0;
319
}
320
321
/*
322
** Add more text to the incremental MD5 checksum.
323
*/
324
void md5sum_step_text(const char *zText, int nBytes){
325
if( !incrInit ){
326
MD5Init(&incrCtx);
327
incrInit = 1;
328
}
329
if( nBytes<=0 ){
330
if( nBytes==0 ) return;
331
nBytes = strlen(zText);
332
}
333
MD5Update(&incrCtx, (unsigned char*)zText, nBytes);
334
}
335
336
/*
337
** Add the content of a blob to the incremental MD5 checksum.
338
*/
339
void md5sum_step_blob(Blob *p){
340
md5sum_step_text(blob_buffer(p), blob_size(p));
341
}
342
343
/*
344
** For trouble-shooting only:
345
**
346
** Report the current state of the incremental checksum.
347
*/
348
const char *md5sum_current_state(void){
349
unsigned int cksum = 0;
350
unsigned int *pFirst, *pLast;
351
static char zResult[12];
352
353
pFirst = (unsigned int*)&incrCtx;
354
pLast = (unsigned int*)((&incrCtx)+1);
355
while( pFirst<pLast ){
356
cksum += *pFirst;
357
pFirst++;
358
}
359
sqlite3_snprintf(sizeof(zResult), zResult, "%08x", cksum);
360
return zResult;
361
}
362
363
/*
364
** Finish the incremental MD5 checksum. Store the result in blob pOut
365
** if pOut!=0. Also return a pointer to the result.
366
**
367
** This resets the incremental checksum preparing for the next round
368
** of computation. The return pointer points to a static buffer that
369
** is overwritten by subsequent calls to this function.
370
*/
371
char *md5sum_finish(Blob *pOut){
372
unsigned char zResult[16];
373
static char zOut[33];
374
md5sum_step_text(0,0);
375
MD5Final(zResult, &incrCtx);
376
incrInit = 0;
377
DigestToBase16(zResult, zOut);
378
if( pOut ){
379
blob_zero(pOut);
380
blob_append(pOut, zOut, 32);
381
}
382
return zOut;
383
}
384
385
386
/*
387
** Compute the MD5 checksum of a file on disk. Store the resulting
388
** checksum in the blob pCksum. pCksum is assumed to be initialized.
389
**
390
** Return the number of errors.
391
*/
392
int md5sum_file(const char *zFilename, Blob *pCksum){
393
FILE *in;
394
MD5Context ctx;
395
unsigned char zResult[16];
396
char zBuf[10240];
397
398
in = fossil_fopen(zFilename,"rb");
399
if( in==0 ){
400
return 1;
401
}
402
MD5Init(&ctx);
403
for(;;){
404
int n;
405
n = fread(zBuf, 1, sizeof(zBuf), in);
406
if( n<=0 ) break;
407
MD5Update(&ctx, (unsigned char*)zBuf, (unsigned)n);
408
}
409
fclose(in);
410
blob_zero(pCksum);
411
blob_resize(pCksum, 32);
412
MD5Final(zResult, &ctx);
413
DigestToBase16(zResult, blob_buffer(pCksum));
414
return 0;
415
}
416
417
/*
418
** Compute the MD5 checksum of a blob in memory. Store the resulting
419
** checksum in the blob pCksum. pCksum is assumed to be either
420
** uninitialized or the same blob as pIn.
421
**
422
** Return the number of errors.
423
*/
424
int md5sum_blob(const Blob *pIn, Blob *pCksum){
425
MD5Context ctx;
426
unsigned char zResult[16];
427
428
MD5Init(&ctx);
429
MD5Update(&ctx, (unsigned char*)blob_buffer(pIn), blob_size(pIn));
430
if( pIn==pCksum ){
431
blob_reset(pCksum);
432
}else{
433
blob_zero(pCksum);
434
}
435
blob_resize(pCksum, 32);
436
MD5Final(zResult, &ctx);
437
DigestToBase16(zResult, blob_buffer(pCksum));
438
return 0;
439
}
440
441
442
/*
443
** COMMAND: md5sum*
444
**
445
** Usage: %fossil md5sum FILES....
446
**
447
** Compute an MD5 checksum of all files named on the command-line.
448
** If a file is named "-" then content is read from standard input.
449
*/
450
void md5sum_test(void){
451
int i;
452
Blob in;
453
Blob cksum;
454
455
for(i=2; i<g.argc; i++){
456
blob_init(&cksum, "********** not found ***********", -1);
457
if( g.argv[i][0]=='-' && g.argv[i][1]==0 ){
458
blob_read_from_channel(&in, stdin, -1);
459
md5sum_blob(&in, &cksum);
460
}else{
461
md5sum_file(g.argv[i], &cksum);
462
}
463
fossil_print("%s %s\n", blob_str(&cksum), g.argv[i]);
464
blob_reset(&cksum);
465
}
466
}
467

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button