/ Check-in [fc663799]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add the ".sha3sum" command to the CLI - used to compute a cryptographic hash of the content of a database file or of individual tables with in that file.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: fc663799075a22b0a61a6a114116bb2d1b96d4ab
User & Date: drh 2017-03-08 16:10:34
Context
2017-03-08
17:56
In the ".sha3sum" command, if there is a LIKE pattern, show the hashes for each table separately. Without a LIKE pattern, show a single hash over the entire database. check-in: 30f87883 user: drh tags: trunk
16:10
Add the ".sha3sum" command to the CLI - used to compute a cryptographic hash of the content of a database file or of individual tables with in that file. check-in: fc663799 user: drh tags: trunk
15:03
Improved comments explaining the sha3_query() format. Fix a performance #ifdef error in the sha3 implementation. check-in: 54ef7abd user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/shell.c.

   669    669       }else{
   670    670         upr = mid-1;
   671    671       }
   672    672     }
   673    673     return 0;
   674    674   }
   675    675   
          676  +/******************************************************************************
          677  +** SHA3 hash implementation copied from ../ext/misc/shathree.c
          678  +*/
          679  +typedef sqlite3_uint64 u64;
          680  +/*
          681  +** Macros to determine whether the machine is big or little endian,
          682  +** and whether or not that determination is run-time or compile-time.
          683  +**
          684  +** For best performance, an attempt is made to guess at the byte-order
          685  +** using C-preprocessor macros.  If that is unsuccessful, or if
          686  +** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
          687  +** at run-time.
          688  +*/
          689  +#ifndef SHA3_BYTEORDER
          690  +# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
          691  +     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
          692  +     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
          693  +     defined(__arm__)
          694  +#   define SHA3_BYTEORDER    1234
          695  +# elif defined(sparc)    || defined(__ppc__)
          696  +#   define SHA3_BYTEORDER    4321
          697  +# else
          698  +#   define SHA3_BYTEORDER 0
          699  +# endif
          700  +#endif
          701  +
          702  +
          703  +/*
          704  +** State structure for a SHA3 hash in progress
          705  +*/
          706  +typedef struct SHA3Context SHA3Context;
          707  +struct SHA3Context {
          708  +  union {
          709  +    u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
          710  +    unsigned char x[1600];    /* ... or 1600 bytes */
          711  +  } u;
          712  +  unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
          713  +  unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
          714  +  unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
          715  +};
          716  +
          717  +/*
          718  +** A single step of the Keccak mixing function for a 1600-bit state
          719  +*/
          720  +static void KeccakF1600Step(SHA3Context *p){
          721  +  int i;
          722  +  u64 B0, B1, B2, B3, B4;
          723  +  u64 C0, C1, C2, C3, C4;
          724  +  u64 D0, D1, D2, D3, D4;
          725  +  static const u64 RC[] = {
          726  +    0x0000000000000001ULL,  0x0000000000008082ULL,
          727  +    0x800000000000808aULL,  0x8000000080008000ULL,
          728  +    0x000000000000808bULL,  0x0000000080000001ULL,
          729  +    0x8000000080008081ULL,  0x8000000000008009ULL,
          730  +    0x000000000000008aULL,  0x0000000000000088ULL,
          731  +    0x0000000080008009ULL,  0x000000008000000aULL,
          732  +    0x000000008000808bULL,  0x800000000000008bULL,
          733  +    0x8000000000008089ULL,  0x8000000000008003ULL,
          734  +    0x8000000000008002ULL,  0x8000000000000080ULL,
          735  +    0x000000000000800aULL,  0x800000008000000aULL,
          736  +    0x8000000080008081ULL,  0x8000000000008080ULL,
          737  +    0x0000000080000001ULL,  0x8000000080008008ULL
          738  +  };
          739  +# define A00 (p->u.s[0])
          740  +# define A01 (p->u.s[1])
          741  +# define A02 (p->u.s[2])
          742  +# define A03 (p->u.s[3])
          743  +# define A04 (p->u.s[4])
          744  +# define A10 (p->u.s[5])
          745  +# define A11 (p->u.s[6])
          746  +# define A12 (p->u.s[7])
          747  +# define A13 (p->u.s[8])
          748  +# define A14 (p->u.s[9])
          749  +# define A20 (p->u.s[10])
          750  +# define A21 (p->u.s[11])
          751  +# define A22 (p->u.s[12])
          752  +# define A23 (p->u.s[13])
          753  +# define A24 (p->u.s[14])
          754  +# define A30 (p->u.s[15])
          755  +# define A31 (p->u.s[16])
          756  +# define A32 (p->u.s[17])
          757  +# define A33 (p->u.s[18])
          758  +# define A34 (p->u.s[19])
          759  +# define A40 (p->u.s[20])
          760  +# define A41 (p->u.s[21])
          761  +# define A42 (p->u.s[22])
          762  +# define A43 (p->u.s[23])
          763  +# define A44 (p->u.s[24])
          764  +# define ROL64(a,x) ((a<<x)|(a>>(64-x)))
          765  +
          766  +  for(i=0; i<24; i+=4){
          767  +    C0 = A00^A10^A20^A30^A40;
          768  +    C1 = A01^A11^A21^A31^A41;
          769  +    C2 = A02^A12^A22^A32^A42;
          770  +    C3 = A03^A13^A23^A33^A43;
          771  +    C4 = A04^A14^A24^A34^A44;
          772  +    D0 = C4^ROL64(C1, 1);
          773  +    D1 = C0^ROL64(C2, 1);
          774  +    D2 = C1^ROL64(C3, 1);
          775  +    D3 = C2^ROL64(C4, 1);
          776  +    D4 = C3^ROL64(C0, 1);
          777  +
          778  +    B0 = (A00^D0);
          779  +    B1 = ROL64((A11^D1), 44);
          780  +    B2 = ROL64((A22^D2), 43);
          781  +    B3 = ROL64((A33^D3), 21);
          782  +    B4 = ROL64((A44^D4), 14);
          783  +    A00 =   B0 ^((~B1)&  B2 );
          784  +    A00 ^= RC[i];
          785  +    A11 =   B1 ^((~B2)&  B3 );
          786  +    A22 =   B2 ^((~B3)&  B4 );
          787  +    A33 =   B3 ^((~B4)&  B0 );
          788  +    A44 =   B4 ^((~B0)&  B1 );
          789  +
          790  +    B2 = ROL64((A20^D0), 3);
          791  +    B3 = ROL64((A31^D1), 45);
          792  +    B4 = ROL64((A42^D2), 61);
          793  +    B0 = ROL64((A03^D3), 28);
          794  +    B1 = ROL64((A14^D4), 20);
          795  +    A20 =   B0 ^((~B1)&  B2 );
          796  +    A31 =   B1 ^((~B2)&  B3 );
          797  +    A42 =   B2 ^((~B3)&  B4 );
          798  +    A03 =   B3 ^((~B4)&  B0 );
          799  +    A14 =   B4 ^((~B0)&  B1 );
          800  +
          801  +    B4 = ROL64((A40^D0), 18);
          802  +    B0 = ROL64((A01^D1), 1);
          803  +    B1 = ROL64((A12^D2), 6);
          804  +    B2 = ROL64((A23^D3), 25);
          805  +    B3 = ROL64((A34^D4), 8);
          806  +    A40 =   B0 ^((~B1)&  B2 );
          807  +    A01 =   B1 ^((~B2)&  B3 );
          808  +    A12 =   B2 ^((~B3)&  B4 );
          809  +    A23 =   B3 ^((~B4)&  B0 );
          810  +    A34 =   B4 ^((~B0)&  B1 );
          811  +
          812  +    B1 = ROL64((A10^D0), 36);
          813  +    B2 = ROL64((A21^D1), 10);
          814  +    B3 = ROL64((A32^D2), 15);
          815  +    B4 = ROL64((A43^D3), 56);
          816  +    B0 = ROL64((A04^D4), 27);
          817  +    A10 =   B0 ^((~B1)&  B2 );
          818  +    A21 =   B1 ^((~B2)&  B3 );
          819  +    A32 =   B2 ^((~B3)&  B4 );
          820  +    A43 =   B3 ^((~B4)&  B0 );
          821  +    A04 =   B4 ^((~B0)&  B1 );
          822  +
          823  +    B3 = ROL64((A30^D0), 41);
          824  +    B4 = ROL64((A41^D1), 2);
          825  +    B0 = ROL64((A02^D2), 62);
          826  +    B1 = ROL64((A13^D3), 55);
          827  +    B2 = ROL64((A24^D4), 39);
          828  +    A30 =   B0 ^((~B1)&  B2 );
          829  +    A41 =   B1 ^((~B2)&  B3 );
          830  +    A02 =   B2 ^((~B3)&  B4 );
          831  +    A13 =   B3 ^((~B4)&  B0 );
          832  +    A24 =   B4 ^((~B0)&  B1 );
          833  +
          834  +    C0 = A00^A20^A40^A10^A30;
          835  +    C1 = A11^A31^A01^A21^A41;
          836  +    C2 = A22^A42^A12^A32^A02;
          837  +    C3 = A33^A03^A23^A43^A13;
          838  +    C4 = A44^A14^A34^A04^A24;
          839  +    D0 = C4^ROL64(C1, 1);
          840  +    D1 = C0^ROL64(C2, 1);
          841  +    D2 = C1^ROL64(C3, 1);
          842  +    D3 = C2^ROL64(C4, 1);
          843  +    D4 = C3^ROL64(C0, 1);
          844  +
          845  +    B0 = (A00^D0);
          846  +    B1 = ROL64((A31^D1), 44);
          847  +    B2 = ROL64((A12^D2), 43);
          848  +    B3 = ROL64((A43^D3), 21);
          849  +    B4 = ROL64((A24^D4), 14);
          850  +    A00 =   B0 ^((~B1)&  B2 );
          851  +    A00 ^= RC[i+1];
          852  +    A31 =   B1 ^((~B2)&  B3 );
          853  +    A12 =   B2 ^((~B3)&  B4 );
          854  +    A43 =   B3 ^((~B4)&  B0 );
          855  +    A24 =   B4 ^((~B0)&  B1 );
          856  +
          857  +    B2 = ROL64((A40^D0), 3);
          858  +    B3 = ROL64((A21^D1), 45);
          859  +    B4 = ROL64((A02^D2), 61);
          860  +    B0 = ROL64((A33^D3), 28);
          861  +    B1 = ROL64((A14^D4), 20);
          862  +    A40 =   B0 ^((~B1)&  B2 );
          863  +    A21 =   B1 ^((~B2)&  B3 );
          864  +    A02 =   B2 ^((~B3)&  B4 );
          865  +    A33 =   B3 ^((~B4)&  B0 );
          866  +    A14 =   B4 ^((~B0)&  B1 );
          867  +
          868  +    B4 = ROL64((A30^D0), 18);
          869  +    B0 = ROL64((A11^D1), 1);
          870  +    B1 = ROL64((A42^D2), 6);
          871  +    B2 = ROL64((A23^D3), 25);
          872  +    B3 = ROL64((A04^D4), 8);
          873  +    A30 =   B0 ^((~B1)&  B2 );
          874  +    A11 =   B1 ^((~B2)&  B3 );
          875  +    A42 =   B2 ^((~B3)&  B4 );
          876  +    A23 =   B3 ^((~B4)&  B0 );
          877  +    A04 =   B4 ^((~B0)&  B1 );
          878  +
          879  +    B1 = ROL64((A20^D0), 36);
          880  +    B2 = ROL64((A01^D1), 10);
          881  +    B3 = ROL64((A32^D2), 15);
          882  +    B4 = ROL64((A13^D3), 56);
          883  +    B0 = ROL64((A44^D4), 27);
          884  +    A20 =   B0 ^((~B1)&  B2 );
          885  +    A01 =   B1 ^((~B2)&  B3 );
          886  +    A32 =   B2 ^((~B3)&  B4 );
          887  +    A13 =   B3 ^((~B4)&  B0 );
          888  +    A44 =   B4 ^((~B0)&  B1 );
          889  +
          890  +    B3 = ROL64((A10^D0), 41);
          891  +    B4 = ROL64((A41^D1), 2);
          892  +    B0 = ROL64((A22^D2), 62);
          893  +    B1 = ROL64((A03^D3), 55);
          894  +    B2 = ROL64((A34^D4), 39);
          895  +    A10 =   B0 ^((~B1)&  B2 );
          896  +    A41 =   B1 ^((~B2)&  B3 );
          897  +    A22 =   B2 ^((~B3)&  B4 );
          898  +    A03 =   B3 ^((~B4)&  B0 );
          899  +    A34 =   B4 ^((~B0)&  B1 );
          900  +
          901  +    C0 = A00^A40^A30^A20^A10;
          902  +    C1 = A31^A21^A11^A01^A41;
          903  +    C2 = A12^A02^A42^A32^A22;
          904  +    C3 = A43^A33^A23^A13^A03;
          905  +    C4 = A24^A14^A04^A44^A34;
          906  +    D0 = C4^ROL64(C1, 1);
          907  +    D1 = C0^ROL64(C2, 1);
          908  +    D2 = C1^ROL64(C3, 1);
          909  +    D3 = C2^ROL64(C4, 1);
          910  +    D4 = C3^ROL64(C0, 1);
          911  +
          912  +    B0 = (A00^D0);
          913  +    B1 = ROL64((A21^D1), 44);
          914  +    B2 = ROL64((A42^D2), 43);
          915  +    B3 = ROL64((A13^D3), 21);
          916  +    B4 = ROL64((A34^D4), 14);
          917  +    A00 =   B0 ^((~B1)&  B2 );
          918  +    A00 ^= RC[i+2];
          919  +    A21 =   B1 ^((~B2)&  B3 );
          920  +    A42 =   B2 ^((~B3)&  B4 );
          921  +    A13 =   B3 ^((~B4)&  B0 );
          922  +    A34 =   B4 ^((~B0)&  B1 );
          923  +
          924  +    B2 = ROL64((A30^D0), 3);
          925  +    B3 = ROL64((A01^D1), 45);
          926  +    B4 = ROL64((A22^D2), 61);
          927  +    B0 = ROL64((A43^D3), 28);
          928  +    B1 = ROL64((A14^D4), 20);
          929  +    A30 =   B0 ^((~B1)&  B2 );
          930  +    A01 =   B1 ^((~B2)&  B3 );
          931  +    A22 =   B2 ^((~B3)&  B4 );
          932  +    A43 =   B3 ^((~B4)&  B0 );
          933  +    A14 =   B4 ^((~B0)&  B1 );
          934  +
          935  +    B4 = ROL64((A10^D0), 18);
          936  +    B0 = ROL64((A31^D1), 1);
          937  +    B1 = ROL64((A02^D2), 6);
          938  +    B2 = ROL64((A23^D3), 25);
          939  +    B3 = ROL64((A44^D4), 8);
          940  +    A10 =   B0 ^((~B1)&  B2 );
          941  +    A31 =   B1 ^((~B2)&  B3 );
          942  +    A02 =   B2 ^((~B3)&  B4 );
          943  +    A23 =   B3 ^((~B4)&  B0 );
          944  +    A44 =   B4 ^((~B0)&  B1 );
          945  +
          946  +    B1 = ROL64((A40^D0), 36);
          947  +    B2 = ROL64((A11^D1), 10);
          948  +    B3 = ROL64((A32^D2), 15);
          949  +    B4 = ROL64((A03^D3), 56);
          950  +    B0 = ROL64((A24^D4), 27);
          951  +    A40 =   B0 ^((~B1)&  B2 );
          952  +    A11 =   B1 ^((~B2)&  B3 );
          953  +    A32 =   B2 ^((~B3)&  B4 );
          954  +    A03 =   B3 ^((~B4)&  B0 );
          955  +    A24 =   B4 ^((~B0)&  B1 );
          956  +
          957  +    B3 = ROL64((A20^D0), 41);
          958  +    B4 = ROL64((A41^D1), 2);
          959  +    B0 = ROL64((A12^D2), 62);
          960  +    B1 = ROL64((A33^D3), 55);
          961  +    B2 = ROL64((A04^D4), 39);
          962  +    A20 =   B0 ^((~B1)&  B2 );
          963  +    A41 =   B1 ^((~B2)&  B3 );
          964  +    A12 =   B2 ^((~B3)&  B4 );
          965  +    A33 =   B3 ^((~B4)&  B0 );
          966  +    A04 =   B4 ^((~B0)&  B1 );
          967  +
          968  +    C0 = A00^A30^A10^A40^A20;
          969  +    C1 = A21^A01^A31^A11^A41;
          970  +    C2 = A42^A22^A02^A32^A12;
          971  +    C3 = A13^A43^A23^A03^A33;
          972  +    C4 = A34^A14^A44^A24^A04;
          973  +    D0 = C4^ROL64(C1, 1);
          974  +    D1 = C0^ROL64(C2, 1);
          975  +    D2 = C1^ROL64(C3, 1);
          976  +    D3 = C2^ROL64(C4, 1);
          977  +    D4 = C3^ROL64(C0, 1);
          978  +
          979  +    B0 = (A00^D0);
          980  +    B1 = ROL64((A01^D1), 44);
          981  +    B2 = ROL64((A02^D2), 43);
          982  +    B3 = ROL64((A03^D3), 21);
          983  +    B4 = ROL64((A04^D4), 14);
          984  +    A00 =   B0 ^((~B1)&  B2 );
          985  +    A00 ^= RC[i+3];
          986  +    A01 =   B1 ^((~B2)&  B3 );
          987  +    A02 =   B2 ^((~B3)&  B4 );
          988  +    A03 =   B3 ^((~B4)&  B0 );
          989  +    A04 =   B4 ^((~B0)&  B1 );
          990  +
          991  +    B2 = ROL64((A10^D0), 3);
          992  +    B3 = ROL64((A11^D1), 45);
          993  +    B4 = ROL64((A12^D2), 61);
          994  +    B0 = ROL64((A13^D3), 28);
          995  +    B1 = ROL64((A14^D4), 20);
          996  +    A10 =   B0 ^((~B1)&  B2 );
          997  +    A11 =   B1 ^((~B2)&  B3 );
          998  +    A12 =   B2 ^((~B3)&  B4 );
          999  +    A13 =   B3 ^((~B4)&  B0 );
         1000  +    A14 =   B4 ^((~B0)&  B1 );
         1001  +
         1002  +    B4 = ROL64((A20^D0), 18);
         1003  +    B0 = ROL64((A21^D1), 1);
         1004  +    B1 = ROL64((A22^D2), 6);
         1005  +    B2 = ROL64((A23^D3), 25);
         1006  +    B3 = ROL64((A24^D4), 8);
         1007  +    A20 =   B0 ^((~B1)&  B2 );
         1008  +    A21 =   B1 ^((~B2)&  B3 );
         1009  +    A22 =   B2 ^((~B3)&  B4 );
         1010  +    A23 =   B3 ^((~B4)&  B0 );
         1011  +    A24 =   B4 ^((~B0)&  B1 );
         1012  +
         1013  +    B1 = ROL64((A30^D0), 36);
         1014  +    B2 = ROL64((A31^D1), 10);
         1015  +    B3 = ROL64((A32^D2), 15);
         1016  +    B4 = ROL64((A33^D3), 56);
         1017  +    B0 = ROL64((A34^D4), 27);
         1018  +    A30 =   B0 ^((~B1)&  B2 );
         1019  +    A31 =   B1 ^((~B2)&  B3 );
         1020  +    A32 =   B2 ^((~B3)&  B4 );
         1021  +    A33 =   B3 ^((~B4)&  B0 );
         1022  +    A34 =   B4 ^((~B0)&  B1 );
         1023  +
         1024  +    B3 = ROL64((A40^D0), 41);
         1025  +    B4 = ROL64((A41^D1), 2);
         1026  +    B0 = ROL64((A42^D2), 62);
         1027  +    B1 = ROL64((A43^D3), 55);
         1028  +    B2 = ROL64((A44^D4), 39);
         1029  +    A40 =   B0 ^((~B1)&  B2 );
         1030  +    A41 =   B1 ^((~B2)&  B3 );
         1031  +    A42 =   B2 ^((~B3)&  B4 );
         1032  +    A43 =   B3 ^((~B4)&  B0 );
         1033  +    A44 =   B4 ^((~B0)&  B1 );
         1034  +  }
         1035  +}
         1036  +
         1037  +/*
         1038  +** Initialize a new hash.  iSize determines the size of the hash
         1039  +** in bits and should be one of 224, 256, 384, or 512.  Or iSize
         1040  +** can be zero to use the default hash size of 256 bits.
         1041  +*/
         1042  +static void SHA3Init(SHA3Context *p, int iSize){
         1043  +  memset(p, 0, sizeof(*p));
         1044  +  if( iSize>=128 && iSize<=512 ){
         1045  +    p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
         1046  +  }else{
         1047  +    p->nRate = (1600 - 2*256)/8;
         1048  +  }
         1049  +#if SHA3_BYTEORDER==1234
         1050  +  /* Known to be little-endian at compile-time. No-op */
         1051  +#elif SHA3_BYTEORDER==4321
         1052  +  p->ixMask = 7;  /* Big-endian */
         1053  +#else
         1054  +  {
         1055  +    static unsigned int one = 1;
         1056  +    if( 1==*(unsigned char*)&one ){
         1057  +      /* Little endian.  No byte swapping. */
         1058  +      p->ixMask = 0;
         1059  +    }else{
         1060  +      /* Big endian.  Byte swap. */
         1061  +      p->ixMask = 7;
         1062  +    }
         1063  +  }
         1064  +#endif
         1065  +}
         1066  +
         1067  +/*
         1068  +** Make consecutive calls to the SHA3Update function to add new content
         1069  +** to the hash
         1070  +*/
         1071  +static void SHA3Update(
         1072  +  SHA3Context *p,
         1073  +  const unsigned char *aData,
         1074  +  unsigned int nData
         1075  +){
         1076  +  unsigned int i = 0;
         1077  +#if SHA3_BYTEORDER==1234
         1078  +  if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
         1079  +    for(; i+7<nData; i+=8){
         1080  +      p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
         1081  +      p->nLoaded += 8;
         1082  +      if( p->nLoaded>=p->nRate ){
         1083  +        KeccakF1600Step(p);
         1084  +        p->nLoaded = 0;
         1085  +      }
         1086  +    }
         1087  +  }
         1088  +#endif
         1089  +  for(; i<nData; i++){
         1090  +#if SHA3_BYTEORDER==1234
         1091  +    p->u.x[p->nLoaded] ^= aData[i];
         1092  +#elif SHA3_BYTEORDER==4321
         1093  +    p->u.x[p->nLoaded^0x07] ^= aData[i];
         1094  +#else
         1095  +    p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
         1096  +#endif
         1097  +    p->nLoaded++;
         1098  +    if( p->nLoaded==p->nRate ){
         1099  +      KeccakF1600Step(p);
         1100  +      p->nLoaded = 0;
         1101  +    }
         1102  +  }
         1103  +}
         1104  +
         1105  +/*
         1106  +** After all content has been added, invoke SHA3Final() to compute
         1107  +** the final hash.  The function returns a pointer to the binary
         1108  +** hash value.
         1109  +*/
         1110  +static unsigned char *SHA3Final(SHA3Context *p){
         1111  +  unsigned int i;
         1112  +  if( p->nLoaded==p->nRate-1 ){
         1113  +    const unsigned char c1 = 0x86;
         1114  +    SHA3Update(p, &c1, 1);
         1115  +  }else{
         1116  +    const unsigned char c2 = 0x06;
         1117  +    const unsigned char c3 = 0x80;
         1118  +    SHA3Update(p, &c2, 1);
         1119  +    p->nLoaded = p->nRate - 1;
         1120  +    SHA3Update(p, &c3, 1);
         1121  +  }
         1122  +  for(i=0; i<p->nRate; i++){
         1123  +    p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
         1124  +  }
         1125  +  return &p->u.x[p->nRate];
         1126  +}
         1127  +
         1128  +/*
         1129  +** Implementation of the sha3(X,SIZE) function.
         1130  +**
         1131  +** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
         1132  +** size is 256.  If X is a BLOB, it is hashed as is.  
         1133  +** For all other non-NULL types of input, X is converted into a UTF-8 string
         1134  +** and the string is hashed without the trailing 0x00 terminator.  The hash
         1135  +** of a NULL value is NULL.
         1136  +*/
         1137  +static void sha3Func(
         1138  +  sqlite3_context *context,
         1139  +  int argc,
         1140  +  sqlite3_value **argv
         1141  +){
         1142  +  SHA3Context cx;
         1143  +  int eType = sqlite3_value_type(argv[0]);
         1144  +  int nByte = sqlite3_value_bytes(argv[0]);
         1145  +  int iSize;
         1146  +  if( argc==1 ){
         1147  +    iSize = 256;
         1148  +  }else{
         1149  +    iSize = sqlite3_value_int(argv[1]);
         1150  +    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
         1151  +      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
         1152  +                                    "384 512", -1);
         1153  +      return;
         1154  +    }
         1155  +  }
         1156  +  if( eType==SQLITE_NULL ) return;
         1157  +  SHA3Init(&cx, iSize);
         1158  +  if( eType==SQLITE_BLOB ){
         1159  +    SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
         1160  +  }else{
         1161  +    SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
         1162  +  }
         1163  +  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
         1164  +}
         1165  +
         1166  +/* Compute a string using sqlite3_vsnprintf() with a maximum length
         1167  +** of 50 bytes and add it to the hash.
         1168  +*/
         1169  +static void hash_step_vformat(
         1170  +  SHA3Context *p,                 /* Add content to this context */
         1171  +  const char *zFormat,
         1172  +  ...
         1173  +){
         1174  +  va_list ap;
         1175  +  int n;
         1176  +  char zBuf[50];
         1177  +  va_start(ap, zFormat);
         1178  +  sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
         1179  +  va_end(ap);
         1180  +  n = (int)strlen(zBuf);
         1181  +  SHA3Update(p, (unsigned char*)zBuf, n);
         1182  +}
         1183  +
         1184  +/*
         1185  +** Implementation of the sha3_query(SQL,SIZE) function.
         1186  +**
         1187  +** This function compiles and runs the SQL statement(s) given in the
         1188  +** argument. The results are hashed using a SIZE-bit SHA3.  The default
         1189  +** size is 256.
         1190  +**
         1191  +** The format of the byte stream that is hashed is summarized as follows:
         1192  +**
         1193  +**       S<n>:<sql>
         1194  +**       R
         1195  +**       N
         1196  +**       I<int>
         1197  +**       F<ieee-float>
         1198  +**       B<size>:<bytes>
         1199  +**       T<size>:<text>
         1200  +**
         1201  +** <sql> is the original SQL text for each statement run and <n> is
         1202  +** the size of that text.  The SQL text is UTF-8.  A single R character
         1203  +** occurs before the start of each row.  N means a NULL value.
         1204  +** I mean an 8-byte little-endian integer <int>.  F is a floating point
         1205  +** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
         1206  +** B means blobs of <size> bytes.  T means text rendered as <size>
         1207  +** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
         1208  +** text integers.
         1209  +**
         1210  +** For each SQL statement in the X input, there is one S segment.  Each
         1211  +** S segment is followed by zero or more R segments, one for each row in the
         1212  +** result set.  After each R, there are one or more N, I, F, B, or T segments,
         1213  +** one for each column in the result set.  Segments are concatentated directly
         1214  +** with no delimiters of any kind.
         1215  +*/
         1216  +static void sha3QueryFunc(
         1217  +  sqlite3_context *context,
         1218  +  int argc,
         1219  +  sqlite3_value **argv
         1220  +){
         1221  +  sqlite3 *db = sqlite3_context_db_handle(context);
         1222  +  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
         1223  +  sqlite3_stmt *pStmt = 0;
         1224  +  int nCol;                   /* Number of columns in the result set */
         1225  +  int i;                      /* Loop counter */
         1226  +  int rc;
         1227  +  int n;
         1228  +  const char *z;
         1229  +  SHA3Context cx;
         1230  +  int iSize;
         1231  +
         1232  +  if( argc==1 ){
         1233  +    iSize = 256;
         1234  +  }else{
         1235  +    iSize = sqlite3_value_int(argv[1]);
         1236  +    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
         1237  +      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
         1238  +                                    "384 512", -1);
         1239  +      return;
         1240  +    }
         1241  +  }
         1242  +  if( zSql==0 ) return;
         1243  +  SHA3Init(&cx, iSize);
         1244  +  while( zSql[0] ){
         1245  +    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
         1246  +    if( rc ){
         1247  +      char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
         1248  +                                   zSql, sqlite3_errmsg(db));
         1249  +      sqlite3_finalize(pStmt);
         1250  +      sqlite3_result_error(context, zMsg, -1);
         1251  +      sqlite3_free(zMsg);
         1252  +      return;
         1253  +    }
         1254  +    if( !sqlite3_stmt_readonly(pStmt) ){
         1255  +      char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
         1256  +      sqlite3_finalize(pStmt);
         1257  +      sqlite3_result_error(context, zMsg, -1);
         1258  +      sqlite3_free(zMsg);
         1259  +      return;
         1260  +    }
         1261  +    nCol = sqlite3_column_count(pStmt);
         1262  +    z = sqlite3_sql(pStmt);
         1263  +    n = (int)strlen(z);
         1264  +    hash_step_vformat(&cx,"S%d:",n);
         1265  +    SHA3Update(&cx,(unsigned char*)z,n);
         1266  +
         1267  +    /* Compute a hash over the result of the query */
         1268  +    while( SQLITE_ROW==sqlite3_step(pStmt) ){
         1269  +      SHA3Update(&cx,(const unsigned char*)"R",1);
         1270  +      for(i=0; i<nCol; i++){
         1271  +        switch( sqlite3_column_type(pStmt,i) ){
         1272  +          case SQLITE_NULL: {
         1273  +            SHA3Update(&cx, (const unsigned char*)"N",1);
         1274  +            break;
         1275  +          }
         1276  +          case SQLITE_INTEGER: {
         1277  +            sqlite3_uint64 u;
         1278  +            int j;
         1279  +            unsigned char x[9];
         1280  +            sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
         1281  +            memcpy(&u, &v, 8);
         1282  +            for(j=8; j>=1; j--){
         1283  +              x[j] = u & 0xff;
         1284  +              u >>= 8;
         1285  +            }
         1286  +            x[0] = 'I';
         1287  +            SHA3Update(&cx, x, 9);
         1288  +            break;
         1289  +          }
         1290  +          case SQLITE_FLOAT: {
         1291  +            sqlite3_uint64 u;
         1292  +            int j;
         1293  +            unsigned char x[9];
         1294  +            double r = sqlite3_column_double(pStmt,i);
         1295  +            memcpy(&u, &r, 8);
         1296  +            for(j=8; j>=1; j--){
         1297  +              x[j] = u & 0xff;
         1298  +              u >>= 8;
         1299  +            }
         1300  +            x[0] = 'F';
         1301  +            SHA3Update(&cx,x,9);
         1302  +            break;
         1303  +          }
         1304  +          case SQLITE_TEXT: {
         1305  +            int n2 = sqlite3_column_bytes(pStmt, i);
         1306  +            const unsigned char *z2 = sqlite3_column_text(pStmt, i);
         1307  +            hash_step_vformat(&cx,"T%d:",n2);
         1308  +            SHA3Update(&cx, z2, n2);
         1309  +            break;
         1310  +          }
         1311  +          case SQLITE_BLOB: {
         1312  +            int n2 = sqlite3_column_bytes(pStmt, i);
         1313  +            const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
         1314  +            hash_step_vformat(&cx,"B%d:",n2);
         1315  +            SHA3Update(&cx, z2, n2);
         1316  +            break;
         1317  +          }
         1318  +        }
         1319  +      }
         1320  +    }
         1321  +    sqlite3_finalize(pStmt);
         1322  +  }
         1323  +  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
         1324  +}
         1325  +/* End of SHA3 hashing logic copy/pasted from ../ext/misc/shathree.c
         1326  +********************************************************************************/
         1327  +
   676   1328   #if defined(SQLITE_ENABLE_SESSION)
   677   1329   /*
   678   1330   ** State information for a single open session
   679   1331   */
   680   1332   typedef struct OpenSession OpenSession;
   681   1333   struct OpenSession {
   682   1334     char *zName;             /* Symbolic name for this session */
................................................................................
  2398   3050     ".schema ?PATTERN?      Show the CREATE statements matching PATTERN\n"
  2399   3051     "                          Add --indent for pretty-printing\n"
  2400   3052     ".separator COL ?ROW?   Change the column separator and optionally the row\n"
  2401   3053     "                         separator for both the output mode and .import\n"
  2402   3054   #if defined(SQLITE_ENABLE_SESSION)
  2403   3055     ".session CMD ...       Create or control sessions\n"
  2404   3056   #endif
         3057  +  ".sha3sum ?OPTIONS...?  Compute a SHA3 hash of database content\n"
  2405   3058     ".shell CMD ARGS...     Run CMD ARGS... in a system shell\n"
  2406   3059     ".show                  Show the current values for various settings\n"
  2407   3060     ".stats ?on|off?        Show stats or turn stats on or off\n"
  2408   3061     ".system CMD ARGS...    Run CMD ARGS... in a system shell\n"
  2409   3062     ".tables ?TABLE?        List names of tables\n"
  2410   3063     "                         If TABLE specified, only list tables matching\n"
  2411   3064     "                         LIKE pattern TABLE.\n"
................................................................................
  2601   3254   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  2602   3255       sqlite3_enable_load_extension(p->db, 1);
  2603   3256   #endif
  2604   3257       sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0,
  2605   3258                               readfileFunc, 0, 0);
  2606   3259       sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0,
  2607   3260                               writefileFunc, 0, 0);
         3261  +    sqlite3_create_function(p->db, "sha3", 1, SQLITE_UTF8, 0,
         3262  +                            sha3Func, 0, 0);
         3263  +    sqlite3_create_function(p->db, "sha3", 2, SQLITE_UTF8, 0,
         3264  +                            sha3Func, 0, 0);
         3265  +    sqlite3_create_function(p->db, "sha3_query", 1, SQLITE_UTF8, 0,
         3266  +                            sha3QueryFunc, 0, 0);
         3267  +    sqlite3_create_function(p->db, "sha3_query", 2, SQLITE_UTF8, 0,
         3268  +                            sha3QueryFunc, 0, 0);
  2608   3269     }
  2609   3270   }
  2610   3271   
  2611   3272   /*
  2612   3273   ** Do C-language style dequoting.
  2613   3274   **
  2614   3275   **    \a    -> alarm
................................................................................
  5031   5692                          "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
  5032   5693       }
  5033   5694       if( nArg>=3 ){
  5034   5695         sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
  5035   5696                          "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
  5036   5697       }
  5037   5698     }else
         5699  +
         5700  +  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
         5701  +    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
         5702  +    int i;                   /* Loop counter */
         5703  +    int bSchema = 0;         /* Also hash the schema */
         5704  +    int iSize = 224;         /* Hash algorithm to use */
         5705  +    int bDebug = 0;          /* Only show the query that would have run */
         5706  +    sqlite3_stmt *pStmt;     /* For querying tables names */
         5707  +    char *zSql;              /* SQL to be run */
         5708  +    ShellText sQuery;        /* Set of queries used to read all content */
         5709  +    for(i=1; i<nArg; i++){
         5710  +      const char *z = azArg[i];
         5711  +      if( z[0]=='-' ){
         5712  +        z++;
         5713  +        if( z[0]=='-' ) z++;
         5714  +        if( strcmp(z,"schema")==0 ){
         5715  +          bSchema = 1;
         5716  +        }else
         5717  +        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 
         5718  +         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 
         5719  +        ){
         5720  +          iSize = atoi(&z[5]);
         5721  +        }else
         5722  +        if( strcmp(z,"debug")==0 ){
         5723  +          bDebug = 1;
         5724  +        }else
         5725  +        {
         5726  +          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
         5727  +                      azArg[0], azArg[i]);
         5728  +          raw_printf(stderr, "Should be one of: --schema"
         5729  +                             " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n");
         5730  +          rc = 1;
         5731  +          goto meta_command_exit;
         5732  +        }
         5733  +      }else if( zLike ){
         5734  +        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
         5735  +        rc = 1;
         5736  +        goto meta_command_exit;
         5737  +      }else{
         5738  +        zLike = z;
         5739  +        if( sqlite3_strlike("sqlite3_%", zLike, 0)==0 ) bSchema = 1;
         5740  +      }
         5741  +    }
         5742  +    if( bSchema ){
         5743  +      zSql = "SELECT lower(name) FROM sqlite_master"
         5744  +             " WHERE type='table' AND coalesce(rootpage,0)>1"
         5745  +             " UNION ALL SELECT 'sqlite_master'"
         5746  +             " ORDER BY 1 collate nocase";
         5747  +    }else{
         5748  +      zSql = "SELECT lower(name) FROM sqlite_master"
         5749  +             " WHERE type='table' AND coalesce(rootpage,0)>1"
         5750  +             " AND name NOT LIKE 'sqlite_%'"
         5751  +             " ORDER BY 1 collate nocase";
         5752  +    }
         5753  +    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
         5754  +    initText(&sQuery);
         5755  +    while( SQLITE_ROW==sqlite3_step(pStmt) ){
         5756  +      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
         5757  +      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
         5758  +      if( strncmp(zTab, "sqlite_",7)!=0 ){
         5759  +        appendText(&sQuery,"SELECT * FROM ", 0);
         5760  +        appendText(&sQuery,zTab,'"');
         5761  +        appendText(&sQuery," NOT INDEXED;", 0);
         5762  +      }else if( strcmp(zTab, "sqlite_master")==0 ){
         5763  +        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
         5764  +                           " ORDER BY name;", 0);
         5765  +      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
         5766  +        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
         5767  +                           " ORDER BY name;", 0);
         5768  +      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
         5769  +        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
         5770  +                           " ORDER BY tbl,idx;", 0);
         5771  +      }else if( strcmp(zTab, "sqlite_stat3")==0
         5772  +             || strcmp(zTab, "sqlite_stat4")==0 ){
         5773  +        appendText(&sQuery, "SELECT * FROM ", 0);
         5774  +        appendText(&sQuery, zTab, 0);
         5775  +        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
         5776  +      }
         5777  +    }
         5778  +    sqlite3_finalize(pStmt);
         5779  +    zSql = sqlite3_mprintf("SELECT lower(hex(sha3_query(%Q,%d))) AS hash;",
         5780  +                           sQuery.z, iSize);
         5781  +    freeText(&sQuery);
         5782  +    if( bDebug ){
         5783  +      utf8_printf(p->out, "%s\n", zSql);
         5784  +    }else{
         5785  +      shell_exec(p->db, zSql, shell_callback, p, 0);
         5786  +    }
         5787  +    sqlite3_free(zSql);
         5788  +  }else
  5038   5789   
  5039   5790     if( c=='s'
  5040   5791      && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
  5041   5792     ){
  5042   5793       char *zCmd;
  5043   5794       int i, x;
  5044   5795       if( nArg<2 ){