src-verify.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. ** This utility program reads the "manifest" and "manifest.uuid" files
  3. ** in a Fossil-generated source tree (where the repository has the
  4. ** "manifest" setting turned on - this is true for SQLite and Fossil itself)
  5. ** and verifies that the source code files are complete and unaltered by
  6. ** checking the SHA1 and SHA3 hashes of the source files contained in the
  7. ** "manifest" file.
  8. **
  9. ** On success it prints: "OK $HASH" where $HASH is the SHA3-256 hash of
  10. ** the check-in for the source tree. If it finds any discrepencies, it
  11. ** prints "Derived from $HASH with changes to:" followed by a list of files
  12. ** which have been altered.
  13. **
  14. ** USAGE:
  15. **
  16. ** src-verify [-x] [-v] $(ROOT)
  17. **
  18. ** Where ROOT is the root of the source tree - the directory that contains
  19. ** the "manifest" and "manifest.uuid" files. Add the "-v" option for
  20. ** some debugging output. With the -x option, the output is in a format
  21. ** that is intended to be read by a script rather by a human. The -x output
  22. ** format always has the SHA3 hash of the source check-in on the first line
  23. ** and lists files that have changed on subsequent lines.
  24. **
  25. ** Additional debugging options:
  26. **
  27. ** src-verify --sha1 FILE ...
  28. ** src-verify --sha3 FILE ...
  29. **
  30. ** Compute the SHA1 or SHA3-256 hashes for all of the FILEs named
  31. **
  32. ** COMPILING:
  33. **
  34. ** This utility is self-contained. It uses only the standard library.
  35. ** There are no other dependencies. Just compile it and run it.
  36. **
  37. ** LIMITATIONS:
  38. **
  39. ** * This utility assumes that the check-in hash uses SHA3-256.
  40. ** It is ok for individual file hashes to be SHA1, but the
  41. ** check-in itself must use a SHA3-256 hash.
  42. */
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46. #if !defined(_WIN32)
  47. # include <unistd.h>
  48. #else
  49. # include <io.h>
  50. # ifndef R_OK
  51. # define R_OK 04
  52. # endif
  53. # ifndef access
  54. # define access(f,m) _access((f),(m))
  55. # endif
  56. #endif
  57. typedef unsigned long long int u64;
  58. /*
  59. ** The SHA1 implementation below is adapted from:
  60. **
  61. ** $NetBSD: sha1.c,v 1.6 2009/11/06 20:31:18 joerg Exp $
  62. ** $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $
  63. **
  64. ** SHA-1 in C
  65. ** By Steve Reid <steve@edmweb.com>
  66. ** 100% Public Domain
  67. */
  68. typedef struct SHA1Context SHA1Context;
  69. struct SHA1Context {
  70. unsigned int state[5];
  71. unsigned int count[2];
  72. unsigned char buffer[64];
  73. };
  74. /*
  75. * blk0() and blk() perform the initial expand.
  76. * I got the idea of expanding during the round function from SSLeay
  77. *
  78. * blk0le() for little-endian and blk0be() for big-endian.
  79. */
  80. #define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r))
  81. #define rol(x,k) SHA_ROT(x,k,32-(k))
  82. #define ror(x,k) SHA_ROT(x,32-(k),k)
  83. #define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \
  84. |(rol(block[i],8)&0x00FF00FF))
  85. #define blk0be(i) block[i]
  86. #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
  87. ^block[(i+2)&15]^block[i&15],1))
  88. /*
  89. * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
  90. *
  91. * Rl0() for little-endian and Rb0() for big-endian. Endianness is
  92. * determined at run-time.
  93. */
  94. #define Rl0(v,w,x,y,z,i) \
  95. z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2);
  96. #define Rb0(v,w,x,y,z,i) \
  97. z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2);
  98. #define R1(v,w,x,y,z,i) \
  99. z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2);
  100. #define R2(v,w,x,y,z,i) \
  101. z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2);
  102. #define R3(v,w,x,y,z,i) \
  103. z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2);
  104. #define R4(v,w,x,y,z,i) \
  105. z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2);
  106. /*
  107. * Hash a single 512-bit block. This is the core of the algorithm.
  108. */
  109. #define a qq[0]
  110. #define b qq[1]
  111. #define c qq[2]
  112. #define d qq[3]
  113. #define e qq[4]
  114. void SHA1Transform(unsigned int state[5], const unsigned char buffer[64])
  115. {
  116. unsigned int qq[5]; /* a, b, c, d, e; */
  117. static int one = 1;
  118. unsigned int block[16];
  119. memcpy(block, buffer, 64);
  120. memcpy(qq,state,5*sizeof(unsigned int));
  121. /* Copy context->state[] to working vars */
  122. /*
  123. a = state[0];
  124. b = state[1];
  125. c = state[2];
  126. d = state[3];
  127. e = state[4];
  128. */
  129. /* 4 rounds of 20 operations each. Loop unrolled. */
  130. if( 1 == *(unsigned char*)&one ){
  131. 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);
  132. 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);
  133. 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);
  134. 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);
  135. }else{
  136. 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);
  137. 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);
  138. 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);
  139. 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);
  140. }
  141. 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);
  142. 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);
  143. 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);
  144. 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);
  145. 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);
  146. 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);
  147. 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);
  148. 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);
  149. 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);
  150. 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);
  151. 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);
  152. 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);
  153. 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);
  154. 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);
  155. 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);
  156. 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);
  157. /* Add the working vars back into context.state[] */
  158. state[0] += a;
  159. state[1] += b;
  160. state[2] += c;
  161. state[3] += d;
  162. state[4] += e;
  163. }
  164. /*
  165. * SHA1Init - Initialize new context
  166. */
  167. static void SHA1Init(SHA1Context *context){
  168. /* SHA1 initialization constants */
  169. context->state[0] = 0x67452301;
  170. context->state[1] = 0xEFCDAB89;
  171. context->state[2] = 0x98BADCFE;
  172. context->state[3] = 0x10325476;
  173. context->state[4] = 0xC3D2E1F0;
  174. context->count[0] = context->count[1] = 0;
  175. }
  176. /*
  177. * Run your data through this.
  178. */
  179. static void SHA1Update(
  180. SHA1Context *context,
  181. const unsigned char *data,
  182. unsigned int len
  183. ){
  184. unsigned int i, j;
  185. j = context->count[0];
  186. if ((context->count[0] += len << 3) < j)
  187. context->count[1] += (len>>29)+1;
  188. j = (j >> 3) & 63;
  189. if ((j + len) > 63) {
  190. (void)memcpy(&context->buffer[j], data, (i = 64-j));
  191. SHA1Transform(context->state, context->buffer);
  192. for ( ; i + 63 < len; i += 64)
  193. SHA1Transform(context->state, &data[i]);
  194. j = 0;
  195. } else {
  196. i = 0;
  197. }
  198. (void)memcpy(&context->buffer[j], &data[i], len - i);
  199. }
  200. /*
  201. * Add padding and return the message digest.
  202. */
  203. static void SHA1Final(unsigned char *digest, SHA1Context *context){
  204. unsigned int i;
  205. unsigned char finalcount[8];
  206. for (i = 0; i < 8; i++) {
  207. finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
  208. >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
  209. }
  210. SHA1Update(context, (const unsigned char *)"\200", 1);
  211. while ((context->count[0] & 504) != 448)
  212. SHA1Update(context, (const unsigned char *)"\0", 1);
  213. SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
  214. if (digest) {
  215. for (i = 0; i < 20; i++)
  216. digest[i] = (unsigned char)
  217. ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
  218. }
  219. }
  220. /*
  221. ** Macros to determine whether the machine is big or little endian,
  222. ** and whether or not that determination is run-time or compile-time.
  223. **
  224. ** For best performance, an attempt is made to guess at the byte-order
  225. ** using C-preprocessor macros. If that is unsuccessful, or if
  226. ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
  227. ** at run-time.
  228. */
  229. #ifndef SHA3_BYTEORDER
  230. # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
  231. defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
  232. defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
  233. defined(__arm__)
  234. # define SHA3_BYTEORDER 1234
  235. # elif defined(sparc) || defined(__ppc__)
  236. # define SHA3_BYTEORDER 4321
  237. # else
  238. # define SHA3_BYTEORDER 0
  239. # endif
  240. #endif
  241. /*
  242. ** State structure for a SHA3 hash in progress
  243. */
  244. typedef struct SHA3Context SHA3Context;
  245. struct SHA3Context {
  246. union {
  247. u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
  248. unsigned char x[1600]; /* ... or 1600 bytes */
  249. } u;
  250. unsigned nRate; /* Bytes of input accepted per Keccak iteration */
  251. unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
  252. unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
  253. };
  254. /*
  255. ** A single step of the Keccak mixing function for a 1600-bit state
  256. */
  257. static void KeccakF1600Step(SHA3Context *p){
  258. int i;
  259. u64 B0, B1, B2, B3, B4;
  260. u64 C0, C1, C2, C3, C4;
  261. u64 D0, D1, D2, D3, D4;
  262. static const u64 RC[] = {
  263. 0x0000000000000001ULL, 0x0000000000008082ULL,
  264. 0x800000000000808aULL, 0x8000000080008000ULL,
  265. 0x000000000000808bULL, 0x0000000080000001ULL,
  266. 0x8000000080008081ULL, 0x8000000000008009ULL,
  267. 0x000000000000008aULL, 0x0000000000000088ULL,
  268. 0x0000000080008009ULL, 0x000000008000000aULL,
  269. 0x000000008000808bULL, 0x800000000000008bULL,
  270. 0x8000000000008089ULL, 0x8000000000008003ULL,
  271. 0x8000000000008002ULL, 0x8000000000000080ULL,
  272. 0x000000000000800aULL, 0x800000008000000aULL,
  273. 0x8000000080008081ULL, 0x8000000000008080ULL,
  274. 0x0000000080000001ULL, 0x8000000080008008ULL
  275. };
  276. # define A00 (p->u.s[0])
  277. # define A01 (p->u.s[1])
  278. # define A02 (p->u.s[2])
  279. # define A03 (p->u.s[3])
  280. # define A04 (p->u.s[4])
  281. # define A10 (p->u.s[5])
  282. # define A11 (p->u.s[6])
  283. # define A12 (p->u.s[7])
  284. # define A13 (p->u.s[8])
  285. # define A14 (p->u.s[9])
  286. # define A20 (p->u.s[10])
  287. # define A21 (p->u.s[11])
  288. # define A22 (p->u.s[12])
  289. # define A23 (p->u.s[13])
  290. # define A24 (p->u.s[14])
  291. # define A30 (p->u.s[15])
  292. # define A31 (p->u.s[16])
  293. # define A32 (p->u.s[17])
  294. # define A33 (p->u.s[18])
  295. # define A34 (p->u.s[19])
  296. # define A40 (p->u.s[20])
  297. # define A41 (p->u.s[21])
  298. # define A42 (p->u.s[22])
  299. # define A43 (p->u.s[23])
  300. # define A44 (p->u.s[24])
  301. # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
  302. for(i=0; i<24; i+=4){
  303. C0 = A00^A10^A20^A30^A40;
  304. C1 = A01^A11^A21^A31^A41;
  305. C2 = A02^A12^A22^A32^A42;
  306. C3 = A03^A13^A23^A33^A43;
  307. C4 = A04^A14^A24^A34^A44;
  308. D0 = C4^ROL64(C1, 1);
  309. D1 = C0^ROL64(C2, 1);
  310. D2 = C1^ROL64(C3, 1);
  311. D3 = C2^ROL64(C4, 1);
  312. D4 = C3^ROL64(C0, 1);
  313. B0 = (A00^D0);
  314. B1 = ROL64((A11^D1), 44);
  315. B2 = ROL64((A22^D2), 43);
  316. B3 = ROL64((A33^D3), 21);
  317. B4 = ROL64((A44^D4), 14);
  318. A00 = B0 ^((~B1)& B2 );
  319. A00 ^= RC[i];
  320. A11 = B1 ^((~B2)& B3 );
  321. A22 = B2 ^((~B3)& B4 );
  322. A33 = B3 ^((~B4)& B0 );
  323. A44 = B4 ^((~B0)& B1 );
  324. B2 = ROL64((A20^D0), 3);
  325. B3 = ROL64((A31^D1), 45);
  326. B4 = ROL64((A42^D2), 61);
  327. B0 = ROL64((A03^D3), 28);
  328. B1 = ROL64((A14^D4), 20);
  329. A20 = B0 ^((~B1)& B2 );
  330. A31 = B1 ^((~B2)& B3 );
  331. A42 = B2 ^((~B3)& B4 );
  332. A03 = B3 ^((~B4)& B0 );
  333. A14 = B4 ^((~B0)& B1 );
  334. B4 = ROL64((A40^D0), 18);
  335. B0 = ROL64((A01^D1), 1);
  336. B1 = ROL64((A12^D2), 6);
  337. B2 = ROL64((A23^D3), 25);
  338. B3 = ROL64((A34^D4), 8);
  339. A40 = B0 ^((~B1)& B2 );
  340. A01 = B1 ^((~B2)& B3 );
  341. A12 = B2 ^((~B3)& B4 );
  342. A23 = B3 ^((~B4)& B0 );
  343. A34 = B4 ^((~B0)& B1 );
  344. B1 = ROL64((A10^D0), 36);
  345. B2 = ROL64((A21^D1), 10);
  346. B3 = ROL64((A32^D2), 15);
  347. B4 = ROL64((A43^D3), 56);
  348. B0 = ROL64((A04^D4), 27);
  349. A10 = B0 ^((~B1)& B2 );
  350. A21 = B1 ^((~B2)& B3 );
  351. A32 = B2 ^((~B3)& B4 );
  352. A43 = B3 ^((~B4)& B0 );
  353. A04 = B4 ^((~B0)& B1 );
  354. B3 = ROL64((A30^D0), 41);
  355. B4 = ROL64((A41^D1), 2);
  356. B0 = ROL64((A02^D2), 62);
  357. B1 = ROL64((A13^D3), 55);
  358. B2 = ROL64((A24^D4), 39);
  359. A30 = B0 ^((~B1)& B2 );
  360. A41 = B1 ^((~B2)& B3 );
  361. A02 = B2 ^((~B3)& B4 );
  362. A13 = B3 ^((~B4)& B0 );
  363. A24 = B4 ^((~B0)& B1 );
  364. C0 = A00^A20^A40^A10^A30;
  365. C1 = A11^A31^A01^A21^A41;
  366. C2 = A22^A42^A12^A32^A02;
  367. C3 = A33^A03^A23^A43^A13;
  368. C4 = A44^A14^A34^A04^A24;
  369. D0 = C4^ROL64(C1, 1);
  370. D1 = C0^ROL64(C2, 1);
  371. D2 = C1^ROL64(C3, 1);
  372. D3 = C2^ROL64(C4, 1);
  373. D4 = C3^ROL64(C0, 1);
  374. B0 = (A00^D0);
  375. B1 = ROL64((A31^D1), 44);
  376. B2 = ROL64((A12^D2), 43);
  377. B3 = ROL64((A43^D3), 21);
  378. B4 = ROL64((A24^D4), 14);
  379. A00 = B0 ^((~B1)& B2 );
  380. A00 ^= RC[i+1];
  381. A31 = B1 ^((~B2)& B3 );
  382. A12 = B2 ^((~B3)& B4 );
  383. A43 = B3 ^((~B4)& B0 );
  384. A24 = B4 ^((~B0)& B1 );
  385. B2 = ROL64((A40^D0), 3);
  386. B3 = ROL64((A21^D1), 45);
  387. B4 = ROL64((A02^D2), 61);
  388. B0 = ROL64((A33^D3), 28);
  389. B1 = ROL64((A14^D4), 20);
  390. A40 = B0 ^((~B1)& B2 );
  391. A21 = B1 ^((~B2)& B3 );
  392. A02 = B2 ^((~B3)& B4 );
  393. A33 = B3 ^((~B4)& B0 );
  394. A14 = B4 ^((~B0)& B1 );
  395. B4 = ROL64((A30^D0), 18);
  396. B0 = ROL64((A11^D1), 1);
  397. B1 = ROL64((A42^D2), 6);
  398. B2 = ROL64((A23^D3), 25);
  399. B3 = ROL64((A04^D4), 8);
  400. A30 = B0 ^((~B1)& B2 );
  401. A11 = B1 ^((~B2)& B3 );
  402. A42 = B2 ^((~B3)& B4 );
  403. A23 = B3 ^((~B4)& B0 );
  404. A04 = B4 ^((~B0)& B1 );
  405. B1 = ROL64((A20^D0), 36);
  406. B2 = ROL64((A01^D1), 10);
  407. B3 = ROL64((A32^D2), 15);
  408. B4 = ROL64((A13^D3), 56);
  409. B0 = ROL64((A44^D4), 27);
  410. A20 = B0 ^((~B1)& B2 );
  411. A01 = B1 ^((~B2)& B3 );
  412. A32 = B2 ^((~B3)& B4 );
  413. A13 = B3 ^((~B4)& B0 );
  414. A44 = B4 ^((~B0)& B1 );
  415. B3 = ROL64((A10^D0), 41);
  416. B4 = ROL64((A41^D1), 2);
  417. B0 = ROL64((A22^D2), 62);
  418. B1 = ROL64((A03^D3), 55);
  419. B2 = ROL64((A34^D4), 39);
  420. A10 = B0 ^((~B1)& B2 );
  421. A41 = B1 ^((~B2)& B3 );
  422. A22 = B2 ^((~B3)& B4 );
  423. A03 = B3 ^((~B4)& B0 );
  424. A34 = B4 ^((~B0)& B1 );
  425. C0 = A00^A40^A30^A20^A10;
  426. C1 = A31^A21^A11^A01^A41;
  427. C2 = A12^A02^A42^A32^A22;
  428. C3 = A43^A33^A23^A13^A03;
  429. C4 = A24^A14^A04^A44^A34;
  430. D0 = C4^ROL64(C1, 1);
  431. D1 = C0^ROL64(C2, 1);
  432. D2 = C1^ROL64(C3, 1);
  433. D3 = C2^ROL64(C4, 1);
  434. D4 = C3^ROL64(C0, 1);
  435. B0 = (A00^D0);
  436. B1 = ROL64((A21^D1), 44);
  437. B2 = ROL64((A42^D2), 43);
  438. B3 = ROL64((A13^D3), 21);
  439. B4 = ROL64((A34^D4), 14);
  440. A00 = B0 ^((~B1)& B2 );
  441. A00 ^= RC[i+2];
  442. A21 = B1 ^((~B2)& B3 );
  443. A42 = B2 ^((~B3)& B4 );
  444. A13 = B3 ^((~B4)& B0 );
  445. A34 = B4 ^((~B0)& B1 );
  446. B2 = ROL64((A30^D0), 3);
  447. B3 = ROL64((A01^D1), 45);
  448. B4 = ROL64((A22^D2), 61);
  449. B0 = ROL64((A43^D3), 28);
  450. B1 = ROL64((A14^D4), 20);
  451. A30 = B0 ^((~B1)& B2 );
  452. A01 = B1 ^((~B2)& B3 );
  453. A22 = B2 ^((~B3)& B4 );
  454. A43 = B3 ^((~B4)& B0 );
  455. A14 = B4 ^((~B0)& B1 );
  456. B4 = ROL64((A10^D0), 18);
  457. B0 = ROL64((A31^D1), 1);
  458. B1 = ROL64((A02^D2), 6);
  459. B2 = ROL64((A23^D3), 25);
  460. B3 = ROL64((A44^D4), 8);
  461. A10 = B0 ^((~B1)& B2 );
  462. A31 = B1 ^((~B2)& B3 );
  463. A02 = B2 ^((~B3)& B4 );
  464. A23 = B3 ^((~B4)& B0 );
  465. A44 = B4 ^((~B0)& B1 );
  466. B1 = ROL64((A40^D0), 36);
  467. B2 = ROL64((A11^D1), 10);
  468. B3 = ROL64((A32^D2), 15);
  469. B4 = ROL64((A03^D3), 56);
  470. B0 = ROL64((A24^D4), 27);
  471. A40 = B0 ^((~B1)& B2 );
  472. A11 = B1 ^((~B2)& B3 );
  473. A32 = B2 ^((~B3)& B4 );
  474. A03 = B3 ^((~B4)& B0 );
  475. A24 = B4 ^((~B0)& B1 );
  476. B3 = ROL64((A20^D0), 41);
  477. B4 = ROL64((A41^D1), 2);
  478. B0 = ROL64((A12^D2), 62);
  479. B1 = ROL64((A33^D3), 55);
  480. B2 = ROL64((A04^D4), 39);
  481. A20 = B0 ^((~B1)& B2 );
  482. A41 = B1 ^((~B2)& B3 );
  483. A12 = B2 ^((~B3)& B4 );
  484. A33 = B3 ^((~B4)& B0 );
  485. A04 = B4 ^((~B0)& B1 );
  486. C0 = A00^A30^A10^A40^A20;
  487. C1 = A21^A01^A31^A11^A41;
  488. C2 = A42^A22^A02^A32^A12;
  489. C3 = A13^A43^A23^A03^A33;
  490. C4 = A34^A14^A44^A24^A04;
  491. D0 = C4^ROL64(C1, 1);
  492. D1 = C0^ROL64(C2, 1);
  493. D2 = C1^ROL64(C3, 1);
  494. D3 = C2^ROL64(C4, 1);
  495. D4 = C3^ROL64(C0, 1);
  496. B0 = (A00^D0);
  497. B1 = ROL64((A01^D1), 44);
  498. B2 = ROL64((A02^D2), 43);
  499. B3 = ROL64((A03^D3), 21);
  500. B4 = ROL64((A04^D4), 14);
  501. A00 = B0 ^((~B1)& B2 );
  502. A00 ^= RC[i+3];
  503. A01 = B1 ^((~B2)& B3 );
  504. A02 = B2 ^((~B3)& B4 );
  505. A03 = B3 ^((~B4)& B0 );
  506. A04 = B4 ^((~B0)& B1 );
  507. B2 = ROL64((A10^D0), 3);
  508. B3 = ROL64((A11^D1), 45);
  509. B4 = ROL64((A12^D2), 61);
  510. B0 = ROL64((A13^D3), 28);
  511. B1 = ROL64((A14^D4), 20);
  512. A10 = B0 ^((~B1)& B2 );
  513. A11 = B1 ^((~B2)& B3 );
  514. A12 = B2 ^((~B3)& B4 );
  515. A13 = B3 ^((~B4)& B0 );
  516. A14 = B4 ^((~B0)& B1 );
  517. B4 = ROL64((A20^D0), 18);
  518. B0 = ROL64((A21^D1), 1);
  519. B1 = ROL64((A22^D2), 6);
  520. B2 = ROL64((A23^D3), 25);
  521. B3 = ROL64((A24^D4), 8);
  522. A20 = B0 ^((~B1)& B2 );
  523. A21 = B1 ^((~B2)& B3 );
  524. A22 = B2 ^((~B3)& B4 );
  525. A23 = B3 ^((~B4)& B0 );
  526. A24 = B4 ^((~B0)& B1 );
  527. B1 = ROL64((A30^D0), 36);
  528. B2 = ROL64((A31^D1), 10);
  529. B3 = ROL64((A32^D2), 15);
  530. B4 = ROL64((A33^D3), 56);
  531. B0 = ROL64((A34^D4), 27);
  532. A30 = B0 ^((~B1)& B2 );
  533. A31 = B1 ^((~B2)& B3 );
  534. A32 = B2 ^((~B3)& B4 );
  535. A33 = B3 ^((~B4)& B0 );
  536. A34 = B4 ^((~B0)& B1 );
  537. B3 = ROL64((A40^D0), 41);
  538. B4 = ROL64((A41^D1), 2);
  539. B0 = ROL64((A42^D2), 62);
  540. B1 = ROL64((A43^D3), 55);
  541. B2 = ROL64((A44^D4), 39);
  542. A40 = B0 ^((~B1)& B2 );
  543. A41 = B1 ^((~B2)& B3 );
  544. A42 = B2 ^((~B3)& B4 );
  545. A43 = B3 ^((~B4)& B0 );
  546. A44 = B4 ^((~B0)& B1 );
  547. }
  548. }
  549. /*
  550. ** Initialize a new hash. iSize determines the size of the hash
  551. ** in bits and should be one of 224, 256, 384, or 512. Or iSize
  552. ** can be zero to use the default hash size of 256 bits.
  553. */
  554. static void SHA3Init(SHA3Context *p, int iSize){
  555. memset(p, 0, sizeof(*p));
  556. if( iSize>=128 && iSize<=512 ){
  557. p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
  558. }else{
  559. p->nRate = (1600 - 2*256)/8;
  560. }
  561. #if SHA3_BYTEORDER==1234
  562. /* Known to be little-endian at compile-time. No-op */
  563. #elif SHA3_BYTEORDER==4321
  564. p->ixMask = 7; /* Big-endian */
  565. #else
  566. {
  567. static unsigned int one = 1;
  568. if( 1==*(unsigned char*)&one ){
  569. /* Little endian. No byte swapping. */
  570. p->ixMask = 0;
  571. }else{
  572. /* Big endian. Byte swap. */
  573. p->ixMask = 7;
  574. }
  575. }
  576. #endif
  577. }
  578. /*
  579. ** Make consecutive calls to the SHA3Update function to add new content
  580. ** to the hash
  581. */
  582. static void SHA3Update(
  583. SHA3Context *p,
  584. const unsigned char *aData,
  585. unsigned int nData
  586. ){
  587. unsigned int i = 0;
  588. #if SHA3_BYTEORDER==1234
  589. if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
  590. for(; i+7<nData; i+=8){
  591. p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
  592. p->nLoaded += 8;
  593. if( p->nLoaded>=p->nRate ){
  594. KeccakF1600Step(p);
  595. p->nLoaded = 0;
  596. }
  597. }
  598. }
  599. #endif
  600. for(; i<nData; i++){
  601. #if SHA3_BYTEORDER==1234
  602. p->u.x[p->nLoaded] ^= aData[i];
  603. #elif SHA3_BYTEORDER==4321
  604. p->u.x[p->nLoaded^0x07] ^= aData[i];
  605. #else
  606. p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
  607. #endif
  608. p->nLoaded++;
  609. if( p->nLoaded==p->nRate ){
  610. KeccakF1600Step(p);
  611. p->nLoaded = 0;
  612. }
  613. }
  614. }
  615. /*
  616. ** After all content has been added, invoke SHA3Final() to compute
  617. ** the final hash. The function returns a pointer to the binary
  618. ** hash value.
  619. */
  620. static unsigned char *SHA3Final(SHA3Context *p){
  621. unsigned int i;
  622. if( p->nLoaded==p->nRate-1 ){
  623. const unsigned char c1 = 0x86;
  624. SHA3Update(p, &c1, 1);
  625. }else{
  626. const unsigned char c2 = 0x06;
  627. const unsigned char c3 = 0x80;
  628. SHA3Update(p, &c2, 1);
  629. p->nLoaded = p->nRate - 1;
  630. SHA3Update(p, &c3, 1);
  631. }
  632. for(i=0; i<p->nRate; i++){
  633. p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
  634. }
  635. return &p->u.x[p->nRate];
  636. }
  637. /*
  638. ** Convert a digest into base-16.
  639. */
  640. static void DigestToBase16(unsigned char *digest, char *zBuf, int nByte){
  641. static const char zEncode[] = "0123456789abcdef";
  642. int ix;
  643. for(ix=0; ix<nByte; ix++){
  644. *zBuf++ = zEncode[(*digest>>4)&0xf];
  645. *zBuf++ = zEncode[*digest++ & 0xf];
  646. }
  647. *zBuf = '\0';
  648. }
  649. /*
  650. ** Compute the SHA3-256 checksum of a file on disk. Store the resulting
  651. ** checksum in the zCksum.
  652. **
  653. ** Return the number of errors.
  654. */
  655. void sha3sum_file(const char *zFilename, char *zCksum){
  656. FILE *in;
  657. SHA3Context ctx;
  658. char zBuf[10240];
  659. in = fopen(zFilename,"rb");
  660. if( in==0 ){
  661. zCksum[0] = 0;
  662. return;
  663. }
  664. SHA3Init(&ctx, 256);
  665. for(;;){
  666. size_t n;
  667. n = fread(zBuf, 1, sizeof(zBuf), in);
  668. if( n<=0 ) break;
  669. SHA3Update(&ctx, (unsigned char*)zBuf, (unsigned)n);
  670. }
  671. fclose(in);
  672. DigestToBase16(SHA3Final(&ctx), zCksum, 32);
  673. }
  674. /*
  675. ** Compute the SHA1 checksum of a file on disk. Store the resulting
  676. ** checksum in the zCksum.
  677. **
  678. ** Return the number of errors.
  679. */
  680. void sha1sum_file(const char *zFilename, char *zCksum){
  681. FILE *in;
  682. SHA1Context ctx;
  683. unsigned char zResult[20];
  684. char zBuf[10240];
  685. in = fopen(zFilename,"rb");
  686. if( in==0 ){
  687. zCksum[0] = 0;
  688. return;
  689. }
  690. SHA1Init(&ctx);
  691. for(;;){
  692. size_t n;
  693. n = fread(zBuf, 1, sizeof(zBuf), in);
  694. if( n<=0 ) break;
  695. SHA1Update(&ctx, (unsigned char*)zBuf, (unsigned)n);
  696. }
  697. fclose(in);
  698. SHA1Final(zResult, &ctx);
  699. DigestToBase16(zResult, zCksum, 20);
  700. }
  701. /*
  702. ** Decode a fossilized string in-place.
  703. */
  704. void defossilize(char *z){
  705. int i, j, cc;
  706. char *zSlash = strchr(z, '\\');
  707. if( zSlash==0 ) return;
  708. i = zSlash - z;
  709. for(j=i; (cc=z[i])!=0; i++){
  710. if( cc=='\\' && z[i+1] ){
  711. i++;
  712. switch( z[i] ){
  713. case 'n': cc = '\n'; break;
  714. case 's': cc = ' '; break;
  715. case 't': cc = '\t'; break;
  716. case 'r': cc = '\r'; break;
  717. case 'v': cc = '\v'; break;
  718. case 'f': cc = '\f'; break;
  719. case '0': cc = 0; break;
  720. case '\\': cc = '\\'; break;
  721. default: cc = z[i]; break;
  722. }
  723. }
  724. z[j++] = cc;
  725. }
  726. if( z[j] ) z[j] = 0;
  727. }
  728. /*
  729. ** Report that a single file is incorrect.
  730. */
  731. static void errorMsg(int *pnErr, const char *zVers, const char *zFile){
  732. if( *pnErr==0 ){
  733. printf("Derived from %.25s with changes to:\n", zVers);
  734. }
  735. printf(" %s\n", zFile);
  736. (*pnErr)++;
  737. }
  738. static void errorMsgNH(int *pnErr, const char *zVers, const char *zFile){
  739. if( *pnErr==0 ){
  740. printf("%s\n", zVers);
  741. }
  742. printf("%s\n", zFile);
  743. (*pnErr)++;
  744. }
  745. int main(int argc, char **argv){
  746. int i, j;
  747. int nDir;
  748. FILE *in;
  749. int bDebug = 0;
  750. int bNonHuman = 0;
  751. int bSeenManifestErr = 0;
  752. int nErr = 0;
  753. SHA3Context ctx3;
  754. const char *zDir = 0;
  755. void (*xErr)(int*,const char*,const char*);
  756. char zHash[100];
  757. char zCk[100];
  758. char zVers[100];
  759. char zLine[40000];
  760. char zFile[40000];
  761. xErr = errorMsg;
  762. for(i=1; i<argc; i++){
  763. const char *z = argv[i];
  764. if( z[0]!='-' ){
  765. if( zDir!=0 ){
  766. fprintf(stderr, "bad argument: %s\n", z);
  767. return 1;
  768. }
  769. zDir = z;
  770. continue;
  771. }
  772. if( z[1]=='-' && z[2]!=0 ) z++;
  773. if( strcmp(argv[1],"-sha1")==0 ){
  774. /* For testing purposes, if the first argument is --sha1, then simply
  775. ** compute and print the SHA1 checksum of all subsequent arguments. */
  776. for(i++; i<argc; i++){
  777. sha1sum_file(argv[i], zHash);
  778. printf("%s %s\n", zHash, argv[i]);
  779. }
  780. return 0;
  781. }
  782. if( strcmp(argv[1], "-sha3")==0 ){
  783. /* For testing purposes, if the first argument is --sha3, then simply
  784. ** compute and print the SHA3-256 checksum of all subsequent arguments. */
  785. for(i++; i<argc; i++){
  786. sha3sum_file(argv[i], zHash);
  787. printf("%s %s\n", zHash, argv[i]);
  788. }
  789. return 0;
  790. }
  791. if( strcmp(z,"-v")==0 ){
  792. bDebug = 1;
  793. continue;
  794. }
  795. if( strcmp(z,"-x")==0 ){
  796. bNonHuman = 1;
  797. xErr = errorMsgNH;
  798. continue;
  799. }
  800. fprintf(stderr, "Usage: %s DIRECTORY\n"
  801. " or: %s --sha1 FILE ...\n"
  802. " or: %s --sha3 FILE ...\n",
  803. argv[0], argv[0], argv[0]);
  804. return 1;
  805. }
  806. if( strlen(zDir)>1000 ){
  807. fprintf(stderr, "Directory argument too big: [%s]\n", zDir);
  808. return 1;
  809. }
  810. nDir = (int)strlen(zDir);
  811. if( nDir<0 ){
  812. fprintf(stderr, "Directory argument too short.\n");
  813. return 1;
  814. }
  815. memcpy(zFile, zDir, nDir);
  816. if( zFile[nDir-1]!='/' ){
  817. zFile[nDir++] = '/';
  818. }
  819. memcpy(&zFile[nDir], "manifest", 9);
  820. if( bDebug ){
  821. printf("manifest file: [%s]\n", zFile);
  822. }
  823. in = fopen(zFile, "rb");
  824. if( in==0 ){
  825. fprintf(stderr, "missing manifest: \"%s\"\n", zFile);
  826. return 1;
  827. }
  828. SHA3Init(&ctx3, 256);
  829. while( fgets(zLine, sizeof(zLine), in) ){
  830. if( zLine[0]=='#' ) break;
  831. SHA3Update(&ctx3, (unsigned char*)zLine, (int)strlen(zLine));
  832. }
  833. DigestToBase16(SHA3Final(&ctx3), zVers, 32);
  834. rewind(in);
  835. while( fgets(zLine, sizeof(zLine), in) ){
  836. if( zLine[0]!='F' ) continue;
  837. if( zLine[1]!=' ' ) continue;
  838. for(i=2, j=nDir; zLine[i]!=0 && zLine[i]!=' '; i++, j++){
  839. if( j<sizeof(zFile) ) zFile[j] = zLine[i];
  840. }
  841. if( j<sizeof(zFile) ) zFile[j] = 0;
  842. zFile[sizeof(zFile)-1] = 0;
  843. defossilize(&zFile[nDir]);
  844. if( zLine[i]!=' ' ){
  845. bSeenManifestErr = 1;
  846. continue;
  847. }
  848. for(i++, j=0; zLine[i]>='0' && zLine[i]<='f'; i++, j++){
  849. if( j<sizeof(zHash) ) zHash[j] = zLine[i];
  850. }
  851. if( j<sizeof(zHash) ) zHash[j] = 0;
  852. zHash[sizeof(zHash)-1] = 0;
  853. if( bDebug ){
  854. printf("%s %s\n", zFile, zHash);
  855. }
  856. if( access(zFile, R_OK)!=0 ){
  857. xErr(&nErr, zVers, &zFile[nDir]);
  858. continue;
  859. }
  860. if( strlen(zHash)==40 ){
  861. sha1sum_file(zFile, zCk);
  862. if( strcmp(zHash, zCk)!=0 ){
  863. xErr(&nErr, zVers, &zFile[nDir]);
  864. }
  865. }else if( strlen(zHash)==64 ){
  866. sha3sum_file(zFile, zCk);
  867. if( strcmp(zHash, zCk)!=0 ){
  868. xErr(&nErr, zVers, &zFile[nDir]);
  869. }
  870. }else{
  871. bSeenManifestErr = 1;
  872. xErr(&nErr, zVers, &zFile[nDir]);
  873. }
  874. }
  875. fclose(in);
  876. in = 0;
  877. if( bSeenManifestErr ) xErr(&nErr, zVers, "manifest");
  878. memcpy(&zFile[nDir], "manifest.uuid", 14);
  879. if( access(zFile, R_OK)!=0
  880. || (in = fopen(zFile,"rb"))==0
  881. || fgets(zLine, sizeof(zLine), in)==0
  882. || strlen(zLine)!=65
  883. || zLine[64]!='\n'
  884. || memcmp(zLine, zVers, 64)!=0
  885. ){
  886. xErr(&nErr, zVers, &zFile[nDir]);
  887. }
  888. if( in ) fclose(in);
  889. if( bNonHuman ){
  890. if( nErr ) return 0;
  891. printf("%s\n", zVers);
  892. }else{
  893. if( nErr ) return nErr;
  894. printf("OK %.25s\n", zVers);
  895. }
  896. return 0;
  897. }