Snapshot of upstream SQLite 3.26.0
[sqlcipher.git] / tool / mksourceid.c
blob282f5c44140c2d800a5b93be7eb0b59011270791
1 /*
2 ** Run this program with a single argument which is the name of the
3 ** Fossil "manifest" file for a project, and this program will emit on
4 ** standard output the "source id" for for the program.
5 **
6 ** (1) The "source id" is the date of check-in together with the
7 ** SHA3 hash of the manifest file.
8 **
9 ** (2) All individual file hashes in the manifest are verified. If any
10 ** source file has changed, the SHA3 hash ends with "modified".
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <ctype.h>
19 /* Portable 64-bit unsigned integers */
20 #if defined(_MSC_VER) || defined(__BORLANDC__)
21 typedef unsigned __int64 u64;
22 #else
23 typedef unsigned long long int u64;
24 #endif
28 ** Macros to determine whether the machine is big or little endian,
29 ** and whether or not that determination is run-time or compile-time.
31 ** For best performance, an attempt is made to guess at the byte-order
32 ** using C-preprocessor macros. If that is unsuccessful, or if
33 ** -DBYTEORDER=0 is set, then byte-order is determined
34 ** at run-time.
36 #ifndef BYTEORDER
37 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
38 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
39 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
40 defined(__arm__)
41 # define BYTEORDER 1234
42 # elif defined(sparc) || defined(__ppc__)
43 # define BYTEORDER 4321
44 # else
45 # define BYTEORDER 0
46 # endif
47 #endif
52 ** State structure for a SHA3 hash in progress
54 typedef struct SHA3Context SHA3Context;
55 struct SHA3Context {
56 union {
57 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
58 unsigned char x[1600]; /* ... or 1600 bytes */
59 } u;
60 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
61 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
62 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
66 ** A single step of the Keccak mixing function for a 1600-bit state
68 static void KeccakF1600Step(SHA3Context *p){
69 int i;
70 u64 B0, B1, B2, B3, B4;
71 u64 C0, C1, C2, C3, C4;
72 u64 D0, D1, D2, D3, D4;
73 static const u64 RC[] = {
74 0x0000000000000001ULL, 0x0000000000008082ULL,
75 0x800000000000808aULL, 0x8000000080008000ULL,
76 0x000000000000808bULL, 0x0000000080000001ULL,
77 0x8000000080008081ULL, 0x8000000000008009ULL,
78 0x000000000000008aULL, 0x0000000000000088ULL,
79 0x0000000080008009ULL, 0x000000008000000aULL,
80 0x000000008000808bULL, 0x800000000000008bULL,
81 0x8000000000008089ULL, 0x8000000000008003ULL,
82 0x8000000000008002ULL, 0x8000000000000080ULL,
83 0x000000000000800aULL, 0x800000008000000aULL,
84 0x8000000080008081ULL, 0x8000000000008080ULL,
85 0x0000000080000001ULL, 0x8000000080008008ULL
87 # define A00 (p->u.s[0])
88 # define A01 (p->u.s[1])
89 # define A02 (p->u.s[2])
90 # define A03 (p->u.s[3])
91 # define A04 (p->u.s[4])
92 # define A10 (p->u.s[5])
93 # define A11 (p->u.s[6])
94 # define A12 (p->u.s[7])
95 # define A13 (p->u.s[8])
96 # define A14 (p->u.s[9])
97 # define A20 (p->u.s[10])
98 # define A21 (p->u.s[11])
99 # define A22 (p->u.s[12])
100 # define A23 (p->u.s[13])
101 # define A24 (p->u.s[14])
102 # define A30 (p->u.s[15])
103 # define A31 (p->u.s[16])
104 # define A32 (p->u.s[17])
105 # define A33 (p->u.s[18])
106 # define A34 (p->u.s[19])
107 # define A40 (p->u.s[20])
108 # define A41 (p->u.s[21])
109 # define A42 (p->u.s[22])
110 # define A43 (p->u.s[23])
111 # define A44 (p->u.s[24])
112 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
114 for(i=0; i<24; i+=4){
115 C0 = A00^A10^A20^A30^A40;
116 C1 = A01^A11^A21^A31^A41;
117 C2 = A02^A12^A22^A32^A42;
118 C3 = A03^A13^A23^A33^A43;
119 C4 = A04^A14^A24^A34^A44;
120 D0 = C4^ROL64(C1, 1);
121 D1 = C0^ROL64(C2, 1);
122 D2 = C1^ROL64(C3, 1);
123 D3 = C2^ROL64(C4, 1);
124 D4 = C3^ROL64(C0, 1);
126 B0 = (A00^D0);
127 B1 = ROL64((A11^D1), 44);
128 B2 = ROL64((A22^D2), 43);
129 B3 = ROL64((A33^D3), 21);
130 B4 = ROL64((A44^D4), 14);
131 A00 = B0 ^((~B1)& B2 );
132 A00 ^= RC[i];
133 A11 = B1 ^((~B2)& B3 );
134 A22 = B2 ^((~B3)& B4 );
135 A33 = B3 ^((~B4)& B0 );
136 A44 = B4 ^((~B0)& B1 );
138 B2 = ROL64((A20^D0), 3);
139 B3 = ROL64((A31^D1), 45);
140 B4 = ROL64((A42^D2), 61);
141 B0 = ROL64((A03^D3), 28);
142 B1 = ROL64((A14^D4), 20);
143 A20 = B0 ^((~B1)& B2 );
144 A31 = B1 ^((~B2)& B3 );
145 A42 = B2 ^((~B3)& B4 );
146 A03 = B3 ^((~B4)& B0 );
147 A14 = B4 ^((~B0)& B1 );
149 B4 = ROL64((A40^D0), 18);
150 B0 = ROL64((A01^D1), 1);
151 B1 = ROL64((A12^D2), 6);
152 B2 = ROL64((A23^D3), 25);
153 B3 = ROL64((A34^D4), 8);
154 A40 = B0 ^((~B1)& B2 );
155 A01 = B1 ^((~B2)& B3 );
156 A12 = B2 ^((~B3)& B4 );
157 A23 = B3 ^((~B4)& B0 );
158 A34 = B4 ^((~B0)& B1 );
160 B1 = ROL64((A10^D0), 36);
161 B2 = ROL64((A21^D1), 10);
162 B3 = ROL64((A32^D2), 15);
163 B4 = ROL64((A43^D3), 56);
164 B0 = ROL64((A04^D4), 27);
165 A10 = B0 ^((~B1)& B2 );
166 A21 = B1 ^((~B2)& B3 );
167 A32 = B2 ^((~B3)& B4 );
168 A43 = B3 ^((~B4)& B0 );
169 A04 = B4 ^((~B0)& B1 );
171 B3 = ROL64((A30^D0), 41);
172 B4 = ROL64((A41^D1), 2);
173 B0 = ROL64((A02^D2), 62);
174 B1 = ROL64((A13^D3), 55);
175 B2 = ROL64((A24^D4), 39);
176 A30 = B0 ^((~B1)& B2 );
177 A41 = B1 ^((~B2)& B3 );
178 A02 = B2 ^((~B3)& B4 );
179 A13 = B3 ^((~B4)& B0 );
180 A24 = B4 ^((~B0)& B1 );
182 C0 = A00^A20^A40^A10^A30;
183 C1 = A11^A31^A01^A21^A41;
184 C2 = A22^A42^A12^A32^A02;
185 C3 = A33^A03^A23^A43^A13;
186 C4 = A44^A14^A34^A04^A24;
187 D0 = C4^ROL64(C1, 1);
188 D1 = C0^ROL64(C2, 1);
189 D2 = C1^ROL64(C3, 1);
190 D3 = C2^ROL64(C4, 1);
191 D4 = C3^ROL64(C0, 1);
193 B0 = (A00^D0);
194 B1 = ROL64((A31^D1), 44);
195 B2 = ROL64((A12^D2), 43);
196 B3 = ROL64((A43^D3), 21);
197 B4 = ROL64((A24^D4), 14);
198 A00 = B0 ^((~B1)& B2 );
199 A00 ^= RC[i+1];
200 A31 = B1 ^((~B2)& B3 );
201 A12 = B2 ^((~B3)& B4 );
202 A43 = B3 ^((~B4)& B0 );
203 A24 = B4 ^((~B0)& B1 );
205 B2 = ROL64((A40^D0), 3);
206 B3 = ROL64((A21^D1), 45);
207 B4 = ROL64((A02^D2), 61);
208 B0 = ROL64((A33^D3), 28);
209 B1 = ROL64((A14^D4), 20);
210 A40 = B0 ^((~B1)& B2 );
211 A21 = B1 ^((~B2)& B3 );
212 A02 = B2 ^((~B3)& B4 );
213 A33 = B3 ^((~B4)& B0 );
214 A14 = B4 ^((~B0)& B1 );
216 B4 = ROL64((A30^D0), 18);
217 B0 = ROL64((A11^D1), 1);
218 B1 = ROL64((A42^D2), 6);
219 B2 = ROL64((A23^D3), 25);
220 B3 = ROL64((A04^D4), 8);
221 A30 = B0 ^((~B1)& B2 );
222 A11 = B1 ^((~B2)& B3 );
223 A42 = B2 ^((~B3)& B4 );
224 A23 = B3 ^((~B4)& B0 );
225 A04 = B4 ^((~B0)& B1 );
227 B1 = ROL64((A20^D0), 36);
228 B2 = ROL64((A01^D1), 10);
229 B3 = ROL64((A32^D2), 15);
230 B4 = ROL64((A13^D3), 56);
231 B0 = ROL64((A44^D4), 27);
232 A20 = B0 ^((~B1)& B2 );
233 A01 = B1 ^((~B2)& B3 );
234 A32 = B2 ^((~B3)& B4 );
235 A13 = B3 ^((~B4)& B0 );
236 A44 = B4 ^((~B0)& B1 );
238 B3 = ROL64((A10^D0), 41);
239 B4 = ROL64((A41^D1), 2);
240 B0 = ROL64((A22^D2), 62);
241 B1 = ROL64((A03^D3), 55);
242 B2 = ROL64((A34^D4), 39);
243 A10 = B0 ^((~B1)& B2 );
244 A41 = B1 ^((~B2)& B3 );
245 A22 = B2 ^((~B3)& B4 );
246 A03 = B3 ^((~B4)& B0 );
247 A34 = B4 ^((~B0)& B1 );
249 C0 = A00^A40^A30^A20^A10;
250 C1 = A31^A21^A11^A01^A41;
251 C2 = A12^A02^A42^A32^A22;
252 C3 = A43^A33^A23^A13^A03;
253 C4 = A24^A14^A04^A44^A34;
254 D0 = C4^ROL64(C1, 1);
255 D1 = C0^ROL64(C2, 1);
256 D2 = C1^ROL64(C3, 1);
257 D3 = C2^ROL64(C4, 1);
258 D4 = C3^ROL64(C0, 1);
260 B0 = (A00^D0);
261 B1 = ROL64((A21^D1), 44);
262 B2 = ROL64((A42^D2), 43);
263 B3 = ROL64((A13^D3), 21);
264 B4 = ROL64((A34^D4), 14);
265 A00 = B0 ^((~B1)& B2 );
266 A00 ^= RC[i+2];
267 A21 = B1 ^((~B2)& B3 );
268 A42 = B2 ^((~B3)& B4 );
269 A13 = B3 ^((~B4)& B0 );
270 A34 = B4 ^((~B0)& B1 );
272 B2 = ROL64((A30^D0), 3);
273 B3 = ROL64((A01^D1), 45);
274 B4 = ROL64((A22^D2), 61);
275 B0 = ROL64((A43^D3), 28);
276 B1 = ROL64((A14^D4), 20);
277 A30 = B0 ^((~B1)& B2 );
278 A01 = B1 ^((~B2)& B3 );
279 A22 = B2 ^((~B3)& B4 );
280 A43 = B3 ^((~B4)& B0 );
281 A14 = B4 ^((~B0)& B1 );
283 B4 = ROL64((A10^D0), 18);
284 B0 = ROL64((A31^D1), 1);
285 B1 = ROL64((A02^D2), 6);
286 B2 = ROL64((A23^D3), 25);
287 B3 = ROL64((A44^D4), 8);
288 A10 = B0 ^((~B1)& B2 );
289 A31 = B1 ^((~B2)& B3 );
290 A02 = B2 ^((~B3)& B4 );
291 A23 = B3 ^((~B4)& B0 );
292 A44 = B4 ^((~B0)& B1 );
294 B1 = ROL64((A40^D0), 36);
295 B2 = ROL64((A11^D1), 10);
296 B3 = ROL64((A32^D2), 15);
297 B4 = ROL64((A03^D3), 56);
298 B0 = ROL64((A24^D4), 27);
299 A40 = B0 ^((~B1)& B2 );
300 A11 = B1 ^((~B2)& B3 );
301 A32 = B2 ^((~B3)& B4 );
302 A03 = B3 ^((~B4)& B0 );
303 A24 = B4 ^((~B0)& B1 );
305 B3 = ROL64((A20^D0), 41);
306 B4 = ROL64((A41^D1), 2);
307 B0 = ROL64((A12^D2), 62);
308 B1 = ROL64((A33^D3), 55);
309 B2 = ROL64((A04^D4), 39);
310 A20 = B0 ^((~B1)& B2 );
311 A41 = B1 ^((~B2)& B3 );
312 A12 = B2 ^((~B3)& B4 );
313 A33 = B3 ^((~B4)& B0 );
314 A04 = B4 ^((~B0)& B1 );
316 C0 = A00^A30^A10^A40^A20;
317 C1 = A21^A01^A31^A11^A41;
318 C2 = A42^A22^A02^A32^A12;
319 C3 = A13^A43^A23^A03^A33;
320 C4 = A34^A14^A44^A24^A04;
321 D0 = C4^ROL64(C1, 1);
322 D1 = C0^ROL64(C2, 1);
323 D2 = C1^ROL64(C3, 1);
324 D3 = C2^ROL64(C4, 1);
325 D4 = C3^ROL64(C0, 1);
327 B0 = (A00^D0);
328 B1 = ROL64((A01^D1), 44);
329 B2 = ROL64((A02^D2), 43);
330 B3 = ROL64((A03^D3), 21);
331 B4 = ROL64((A04^D4), 14);
332 A00 = B0 ^((~B1)& B2 );
333 A00 ^= RC[i+3];
334 A01 = B1 ^((~B2)& B3 );
335 A02 = B2 ^((~B3)& B4 );
336 A03 = B3 ^((~B4)& B0 );
337 A04 = B4 ^((~B0)& B1 );
339 B2 = ROL64((A10^D0), 3);
340 B3 = ROL64((A11^D1), 45);
341 B4 = ROL64((A12^D2), 61);
342 B0 = ROL64((A13^D3), 28);
343 B1 = ROL64((A14^D4), 20);
344 A10 = B0 ^((~B1)& B2 );
345 A11 = B1 ^((~B2)& B3 );
346 A12 = B2 ^((~B3)& B4 );
347 A13 = B3 ^((~B4)& B0 );
348 A14 = B4 ^((~B0)& B1 );
350 B4 = ROL64((A20^D0), 18);
351 B0 = ROL64((A21^D1), 1);
352 B1 = ROL64((A22^D2), 6);
353 B2 = ROL64((A23^D3), 25);
354 B3 = ROL64((A24^D4), 8);
355 A20 = B0 ^((~B1)& B2 );
356 A21 = B1 ^((~B2)& B3 );
357 A22 = B2 ^((~B3)& B4 );
358 A23 = B3 ^((~B4)& B0 );
359 A24 = B4 ^((~B0)& B1 );
361 B1 = ROL64((A30^D0), 36);
362 B2 = ROL64((A31^D1), 10);
363 B3 = ROL64((A32^D2), 15);
364 B4 = ROL64((A33^D3), 56);
365 B0 = ROL64((A34^D4), 27);
366 A30 = B0 ^((~B1)& B2 );
367 A31 = B1 ^((~B2)& B3 );
368 A32 = B2 ^((~B3)& B4 );
369 A33 = B3 ^((~B4)& B0 );
370 A34 = B4 ^((~B0)& B1 );
372 B3 = ROL64((A40^D0), 41);
373 B4 = ROL64((A41^D1), 2);
374 B0 = ROL64((A42^D2), 62);
375 B1 = ROL64((A43^D3), 55);
376 B2 = ROL64((A44^D4), 39);
377 A40 = B0 ^((~B1)& B2 );
378 A41 = B1 ^((~B2)& B3 );
379 A42 = B2 ^((~B3)& B4 );
380 A43 = B3 ^((~B4)& B0 );
381 A44 = B4 ^((~B0)& B1 );
386 ** Initialize a new hash. iSize determines the size of the hash
387 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
388 ** can be zero to use the default hash size of 256 bits.
390 static void SHA3Init(SHA3Context *p, int iSize){
391 memset(p, 0, sizeof(*p));
392 if( iSize>=128 && iSize<=512 ){
393 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
394 }else{
395 p->nRate = (1600 - 2*256)/8;
397 #if BYTEORDER==1234
398 /* Known to be little-endian at compile-time. No-op */
399 #elif BYTEORDER==4321
400 p->ixMask = 7; /* Big-endian */
401 #else
403 static unsigned int one = 1;
404 if( 1==*(unsigned char*)&one ){
405 /* Little endian. No byte swapping. */
406 p->ixMask = 0;
407 }else{
408 /* Big endian. Byte swap. */
409 p->ixMask = 7;
412 #endif
416 ** Make consecutive calls to the SHA3Update function to add new content
417 ** to the hash
419 static void SHA3Update(
420 SHA3Context *p,
421 const unsigned char *aData,
422 unsigned int nData
424 unsigned int i = 0;
425 #if BYTEORDER==1234
426 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
427 for(; i+7<nData; i+=8){
428 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
429 p->nLoaded += 8;
430 if( p->nLoaded>=p->nRate ){
431 KeccakF1600Step(p);
432 p->nLoaded = 0;
436 #endif
437 for(; i<nData; i++){
438 #if BYTEORDER==1234
439 p->u.x[p->nLoaded] ^= aData[i];
440 #elif BYTEORDER==4321
441 p->u.x[p->nLoaded^0x07] ^= aData[i];
442 #else
443 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
444 #endif
445 p->nLoaded++;
446 if( p->nLoaded==p->nRate ){
447 KeccakF1600Step(p);
448 p->nLoaded = 0;
454 ** After all content has been added, invoke SHA3Final() to compute
455 ** the final hash. The function returns a pointer to the binary
456 ** hash value.
458 static unsigned char *SHA3Final(SHA3Context *p){
459 unsigned int i;
460 if( p->nLoaded==p->nRate-1 ){
461 const unsigned char c1 = 0x86;
462 SHA3Update(p, &c1, 1);
463 }else{
464 const unsigned char c2 = 0x06;
465 const unsigned char c3 = 0x80;
466 SHA3Update(p, &c2, 1);
467 p->nLoaded = p->nRate - 1;
468 SHA3Update(p, &c3, 1);
470 for(i=0; i<p->nRate; i++){
471 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
473 return &p->u.x[p->nRate];
477 ** Convert a digest into base-16. digest should be declared as
478 ** "unsigned char digest[20]" in the calling function. The SHA3
479 ** digest is stored in the first 20 bytes. zBuf should
480 ** be "char zBuf[41]".
482 static void DigestToBase16(unsigned char *digest, char *zBuf, int nByte){
483 static const char zEncode[] = "0123456789abcdef";
484 int ix;
486 for(ix=0; ix<nByte; ix++){
487 *zBuf++ = zEncode[(*digest>>4)&0xf];
488 *zBuf++ = zEncode[*digest++ & 0xf];
490 *zBuf = '\0';
495 ** Compute the SHA3 checksum of a file on disk. Store the resulting
496 ** checksum in the blob pCksum. pCksum is assumed to be initialized.
498 ** Return the number of errors.
500 static int sha3sum_file(const char *zFilename, int iSize, char *pCksum){
501 FILE *in;
502 SHA3Context ctx;
503 char zBuf[10240];
505 in = fopen(zFilename,"rb");
506 if( in==0 ){
507 return 1;
509 SHA3Init(&ctx, iSize);
510 for(;;){
511 int n = (int)fread(zBuf, 1, sizeof(zBuf), in);
512 if( n<=0 ) break;
513 SHA3Update(&ctx, (unsigned char*)zBuf, (unsigned)n);
515 fclose(in);
516 DigestToBase16(SHA3Final(&ctx), pCksum, iSize/8);
517 return 0;
521 ** The SHA1 implementation below is adapted from:
523 ** $NetBSD: sha1.c,v 1.6 2009/11/06 20:31:18 joerg Exp $
524 ** $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $
526 ** SHA-1 in C
527 ** By Steve Reid <steve@edmweb.com>
528 ** 100% Public Domain
530 typedef struct SHA1Context SHA1Context;
531 struct SHA1Context {
532 unsigned int state[5];
533 unsigned int count[2];
534 unsigned char buffer[64];
538 * blk0() and blk() perform the initial expand.
539 * I got the idea of expanding during the round function from SSLeay
541 * blk0le() for little-endian and blk0be() for big-endian.
543 #if __GNUC__ && (defined(__i386__) || defined(__x86_64__))
545 * GCC by itself only generates left rotates. Use right rotates if
546 * possible to be kinder to dinky implementations with iterative rotate
547 * instructions.
549 #define SHA_ROT(op, x, k) \
550 ({ unsigned int y; asm(op " %1,%0" : "=r" (y) : "I" (k), "0" (x)); y; })
551 #define rol(x,k) SHA_ROT("roll", x, k)
552 #define ror(x,k) SHA_ROT("rorl", x, k)
554 #else
555 /* Generic C equivalent */
556 #define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r))
557 #define rol(x,k) SHA_ROT(x,k,32-(k))
558 #define ror(x,k) SHA_ROT(x,32-(k),k)
559 #endif
565 #define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \
566 |(rol(block[i],8)&0x00FF00FF))
567 #define blk0be(i) block[i]
568 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
569 ^block[(i+2)&15]^block[i&15],1))
572 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
574 * Rl0() for little-endian and Rb0() for big-endian. Endianness is
575 * determined at run-time.
577 #define Rl0(v,w,x,y,z,i) \
578 z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2);
579 #define Rb0(v,w,x,y,z,i) \
580 z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2);
581 #define R1(v,w,x,y,z,i) \
582 z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2);
583 #define R2(v,w,x,y,z,i) \
584 z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2);
585 #define R3(v,w,x,y,z,i) \
586 z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2);
587 #define R4(v,w,x,y,z,i) \
588 z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2);
591 * Hash a single 512-bit block. This is the core of the algorithm.
593 #define a qq[0]
594 #define b qq[1]
595 #define c qq[2]
596 #define d qq[3]
597 #define e qq[4]
599 static void SHA1Transform(
600 unsigned int state[5],
601 const unsigned char buffer[64]
603 unsigned int qq[5]; /* a, b, c, d, e; */
604 static int one = 1;
605 unsigned int block[16];
606 memcpy(block, buffer, 64);
607 memcpy(qq,state,5*sizeof(unsigned int));
609 /* Copy context->state[] to working vars */
611 a = state[0];
612 b = state[1];
613 c = state[2];
614 d = state[3];
615 e = state[4];
618 /* 4 rounds of 20 operations each. Loop unrolled. */
619 if( 1 == *(unsigned char*)&one ){
620 Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3);
621 Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7);
622 Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11);
623 Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15);
624 }else{
625 Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3);
626 Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7);
627 Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11);
628 Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15);
630 R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
631 R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
632 R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
633 R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
634 R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
635 R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
636 R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
637 R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
638 R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
639 R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
640 R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
641 R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
642 R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
643 R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
644 R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
645 R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
647 /* Add the working vars back into context.state[] */
648 state[0] += a;
649 state[1] += b;
650 state[2] += c;
651 state[3] += d;
652 state[4] += e;
657 * SHA1Init - Initialize new context
659 static void SHA1Init(SHA1Context *context){
660 /* SHA1 initialization constants */
661 context->state[0] = 0x67452301;
662 context->state[1] = 0xEFCDAB89;
663 context->state[2] = 0x98BADCFE;
664 context->state[3] = 0x10325476;
665 context->state[4] = 0xC3D2E1F0;
666 context->count[0] = context->count[1] = 0;
671 * Run your data through this.
673 static void SHA1Update(
674 SHA1Context *context,
675 const unsigned char *data,
676 unsigned int len
678 unsigned int i, j;
680 j = context->count[0];
681 if ((context->count[0] += len << 3) < j)
682 context->count[1] += (len>>29)+1;
683 j = (j >> 3) & 63;
684 if ((j + len) > 63) {
685 (void)memcpy(&context->buffer[j], data, (i = 64-j));
686 SHA1Transform(context->state, context->buffer);
687 for ( ; i + 63 < len; i += 64)
688 SHA1Transform(context->state, &data[i]);
689 j = 0;
690 } else {
691 i = 0;
693 (void)memcpy(&context->buffer[j], &data[i], len - i);
698 * Add padding and return the message digest.
700 static void SHA1Final(unsigned char *digest, SHA1Context *context){
701 unsigned int i;
702 unsigned char finalcount[8];
704 for (i = 0; i < 8; i++) {
705 finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
706 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
708 SHA1Update(context, (const unsigned char *)"\200", 1);
709 while ((context->count[0] & 504) != 448)
710 SHA1Update(context, (const unsigned char *)"\0", 1);
711 SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
713 if (digest) {
714 for (i = 0; i < 20; i++)
715 digest[i] = (unsigned char)
716 ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
722 ** Compute the SHA1 checksum of a file on disk. Store the resulting
723 ** checksum in the blob pCksum. pCksum is assumed to be initialized.
725 ** Return the number of errors.
727 static int sha1sum_file(const char *zFilename, char *pCksum){
728 FILE *in;
729 SHA1Context ctx;
730 unsigned char zResult[20];
731 char zBuf[10240];
733 in = fopen(zFilename,"rb");
734 if( in==0 ){
735 return 1;
737 SHA1Init(&ctx);
738 for(;;){
739 int n = (int)fread(zBuf, 1, sizeof(zBuf), in);
740 if( n<=0 ) break;
741 SHA1Update(&ctx, (unsigned char*)zBuf, (unsigned)n);
743 fclose(in);
744 SHA1Final(zResult, &ctx);
745 DigestToBase16(zResult, pCksum, 20);
746 return 0;
750 ** Print a usage comment and quit.
752 static void usage(const char *argv0){
753 fprintf(stderr,
754 "Usage: %s manifest\n"
755 "Options:\n"
756 " -v Diagnostic output\n"
757 , argv0);
758 exit(1);
762 ** Find the first whitespace character in a string. Set that whitespace
763 ** to a \000 terminator and return a pointer to the next character.
765 static char *nextToken(char *z){
766 while( *z && !isspace(*z) ) z++;
767 if( *z==0 ) return z;
768 *z = 0;
769 return &z[1];
773 int main(int argc, char **argv){
774 const char *zManifest = 0;
775 int i;
776 int bVerbose = 0;
777 FILE *in;
778 int allValid = 1;
779 int rc;
780 SHA3Context ctx;
781 char zDate[50];
782 char zHash[100];
783 char zLine[20000];
785 for(i=1; i<argc; i++){
786 const char *z = argv[i];
787 if( z[0]=='-' ){
788 if( z[1]=='-' ) z++;
789 if( strcmp(z, "-v")==0 ){
790 bVerbose = 1;
791 }else
793 fprintf(stderr, "unknown option \"%s\"", argv[i]);
794 exit(1);
796 }else if( zManifest!=0 ){
797 usage(argv[0]);
798 }else{
799 zManifest = z;
802 if( zManifest==0 ) usage(argv[0]);
803 zDate[0] = 0;
804 in = fopen(zManifest, "rb");
805 if( in==0 ){
806 fprintf(stderr, "cannot open \"%s\" for reading\n", zManifest);
807 exit(1);
809 SHA3Init(&ctx, 256);
810 while( fgets(zLine, sizeof(zLine), in) ){
811 if( strncmp(zLine,"# Remove this line", 18)!=0 ){
812 SHA3Update(&ctx, (unsigned char*)zLine, (unsigned)strlen(zLine));
814 if( strncmp(zLine, "D 20", 4)==0 ){
815 memcpy(zDate, &zLine[2], 10);
816 zDate[10] = ' ';
817 memcpy(&zDate[11], &zLine[13], 8);
818 zDate[19] = 0;
819 continue;
821 if( strncmp(zLine, "F ", 2)==0 ){
822 char *zFilename = &zLine[2];
823 char *zMHash = nextToken(zFilename);
824 nextToken(zMHash);
825 if( strlen(zMHash)==40 ){
826 rc = sha1sum_file(zFilename, zHash);
827 }else{
828 rc = sha3sum_file(zFilename, 256, zHash);
830 if( rc ){
831 allValid = 0;
832 if( bVerbose ){
833 printf("hash failed: %s\n", zFilename);
835 }else if( strcmp(zHash, zMHash)!=0 ){
836 allValid = 0;
837 if( bVerbose ){
838 printf("wrong hash: %s\n", zFilename);
839 printf("... expected: %s\n", zMHash);
840 printf("... got: %s\n", zHash);
845 fclose(in);
846 DigestToBase16(SHA3Final(&ctx), zHash, 256/8);
847 if( !allValid ){
848 printf("%s %.60salt1\n", zDate, zHash);
849 }else{
850 printf("%s %s\n", zDate, zHash);
852 return 0;