xref: /sqlite-3.40.0/ext/misc/shathree.c (revision 3b328522)
1 /*
2 ** 2017-03-08
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 ******************************************************************************
12 **
13 ** This SQLite extension implements a functions that compute SHA1 hashes.
14 ** Two SQL functions are implemented:
15 **
16 **     sha3(X,SIZE)
17 **     sha3_query(Y,SIZE)
18 **
19 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
20 ** X is NULL.
21 **
22 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
23 ** and returns a hash of their results.
24 **
25 ** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
26 ** is used.  If SIZE is included it must be one of the integers 224, 256,
27 ** 384, or 512, to determine SHA3 hash variant that is computed.
28 */
29 #include "sqlite3ext.h"
30 SQLITE_EXTENSION_INIT1
31 #include <assert.h>
32 #include <string.h>
33 #include <stdarg.h>
34 typedef sqlite3_uint64 u64;
35 
36 /******************************************************************************
37 ** The Hash Engine
38 */
39 /*
40 ** Macros to determine whether the machine is big or little endian,
41 ** and whether or not that determination is run-time or compile-time.
42 **
43 ** For best performance, an attempt is made to guess at the byte-order
44 ** using C-preprocessor macros.  If that is unsuccessful, or if
45 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
46 ** at run-time.
47 */
48 #ifndef SHA3_BYTEORDER
49 # if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
50      defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
51      defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
52      defined(__arm__)
53 #   define SHA3_BYTEORDER    1234
54 # elif defined(sparc)    || defined(__ppc__)
55 #   define SHA3_BYTEORDER    4321
56 # else
57 #   define SHA3_BYTEORDER 0
58 # endif
59 #endif
60 
61 
62 /*
63 ** State structure for a SHA3 hash in progress
64 */
65 typedef struct SHA3Context SHA3Context;
66 struct SHA3Context {
67   union {
68     u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
69     unsigned char x[1600];    /* ... or 1600 bytes */
70   } u;
71   unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
72   unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
73   unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
74 };
75 
76 /*
77 ** A single step of the Keccak mixing function for a 1600-bit state
78 */
79 static void KeccakF1600Step(SHA3Context *p){
80   int i;
81   u64 B0, B1, B2, B3, B4;
82   u64 C0, C1, C2, C3, C4;
83   u64 D0, D1, D2, D3, D4;
84   static const u64 RC[] = {
85     0x0000000000000001ULL,  0x0000000000008082ULL,
86     0x800000000000808aULL,  0x8000000080008000ULL,
87     0x000000000000808bULL,  0x0000000080000001ULL,
88     0x8000000080008081ULL,  0x8000000000008009ULL,
89     0x000000000000008aULL,  0x0000000000000088ULL,
90     0x0000000080008009ULL,  0x000000008000000aULL,
91     0x000000008000808bULL,  0x800000000000008bULL,
92     0x8000000000008089ULL,  0x8000000000008003ULL,
93     0x8000000000008002ULL,  0x8000000000000080ULL,
94     0x000000000000800aULL,  0x800000008000000aULL,
95     0x8000000080008081ULL,  0x8000000000008080ULL,
96     0x0000000080000001ULL,  0x8000000080008008ULL
97   };
98 # define A00 (p->u.s[0])
99 # define A01 (p->u.s[1])
100 # define A02 (p->u.s[2])
101 # define A03 (p->u.s[3])
102 # define A04 (p->u.s[4])
103 # define A10 (p->u.s[5])
104 # define A11 (p->u.s[6])
105 # define A12 (p->u.s[7])
106 # define A13 (p->u.s[8])
107 # define A14 (p->u.s[9])
108 # define A20 (p->u.s[10])
109 # define A21 (p->u.s[11])
110 # define A22 (p->u.s[12])
111 # define A23 (p->u.s[13])
112 # define A24 (p->u.s[14])
113 # define A30 (p->u.s[15])
114 # define A31 (p->u.s[16])
115 # define A32 (p->u.s[17])
116 # define A33 (p->u.s[18])
117 # define A34 (p->u.s[19])
118 # define A40 (p->u.s[20])
119 # define A41 (p->u.s[21])
120 # define A42 (p->u.s[22])
121 # define A43 (p->u.s[23])
122 # define A44 (p->u.s[24])
123 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
124 
125   for(i=0; i<24; i+=4){
126     C0 = A00^A10^A20^A30^A40;
127     C1 = A01^A11^A21^A31^A41;
128     C2 = A02^A12^A22^A32^A42;
129     C3 = A03^A13^A23^A33^A43;
130     C4 = A04^A14^A24^A34^A44;
131     D0 = C4^ROL64(C1, 1);
132     D1 = C0^ROL64(C2, 1);
133     D2 = C1^ROL64(C3, 1);
134     D3 = C2^ROL64(C4, 1);
135     D4 = C3^ROL64(C0, 1);
136 
137     B0 = (A00^D0);
138     B1 = ROL64((A11^D1), 44);
139     B2 = ROL64((A22^D2), 43);
140     B3 = ROL64((A33^D3), 21);
141     B4 = ROL64((A44^D4), 14);
142     A00 =   B0 ^((~B1)&  B2 );
143     A00 ^= RC[i];
144     A11 =   B1 ^((~B2)&  B3 );
145     A22 =   B2 ^((~B3)&  B4 );
146     A33 =   B3 ^((~B4)&  B0 );
147     A44 =   B4 ^((~B0)&  B1 );
148 
149     B2 = ROL64((A20^D0), 3);
150     B3 = ROL64((A31^D1), 45);
151     B4 = ROL64((A42^D2), 61);
152     B0 = ROL64((A03^D3), 28);
153     B1 = ROL64((A14^D4), 20);
154     A20 =   B0 ^((~B1)&  B2 );
155     A31 =   B1 ^((~B2)&  B3 );
156     A42 =   B2 ^((~B3)&  B4 );
157     A03 =   B3 ^((~B4)&  B0 );
158     A14 =   B4 ^((~B0)&  B1 );
159 
160     B4 = ROL64((A40^D0), 18);
161     B0 = ROL64((A01^D1), 1);
162     B1 = ROL64((A12^D2), 6);
163     B2 = ROL64((A23^D3), 25);
164     B3 = ROL64((A34^D4), 8);
165     A40 =   B0 ^((~B1)&  B2 );
166     A01 =   B1 ^((~B2)&  B3 );
167     A12 =   B2 ^((~B3)&  B4 );
168     A23 =   B3 ^((~B4)&  B0 );
169     A34 =   B4 ^((~B0)&  B1 );
170 
171     B1 = ROL64((A10^D0), 36);
172     B2 = ROL64((A21^D1), 10);
173     B3 = ROL64((A32^D2), 15);
174     B4 = ROL64((A43^D3), 56);
175     B0 = ROL64((A04^D4), 27);
176     A10 =   B0 ^((~B1)&  B2 );
177     A21 =   B1 ^((~B2)&  B3 );
178     A32 =   B2 ^((~B3)&  B4 );
179     A43 =   B3 ^((~B4)&  B0 );
180     A04 =   B4 ^((~B0)&  B1 );
181 
182     B3 = ROL64((A30^D0), 41);
183     B4 = ROL64((A41^D1), 2);
184     B0 = ROL64((A02^D2), 62);
185     B1 = ROL64((A13^D3), 55);
186     B2 = ROL64((A24^D4), 39);
187     A30 =   B0 ^((~B1)&  B2 );
188     A41 =   B1 ^((~B2)&  B3 );
189     A02 =   B2 ^((~B3)&  B4 );
190     A13 =   B3 ^((~B4)&  B0 );
191     A24 =   B4 ^((~B0)&  B1 );
192 
193     C0 = A00^A20^A40^A10^A30;
194     C1 = A11^A31^A01^A21^A41;
195     C2 = A22^A42^A12^A32^A02;
196     C3 = A33^A03^A23^A43^A13;
197     C4 = A44^A14^A34^A04^A24;
198     D0 = C4^ROL64(C1, 1);
199     D1 = C0^ROL64(C2, 1);
200     D2 = C1^ROL64(C3, 1);
201     D3 = C2^ROL64(C4, 1);
202     D4 = C3^ROL64(C0, 1);
203 
204     B0 = (A00^D0);
205     B1 = ROL64((A31^D1), 44);
206     B2 = ROL64((A12^D2), 43);
207     B3 = ROL64((A43^D3), 21);
208     B4 = ROL64((A24^D4), 14);
209     A00 =   B0 ^((~B1)&  B2 );
210     A00 ^= RC[i+1];
211     A31 =   B1 ^((~B2)&  B3 );
212     A12 =   B2 ^((~B3)&  B4 );
213     A43 =   B3 ^((~B4)&  B0 );
214     A24 =   B4 ^((~B0)&  B1 );
215 
216     B2 = ROL64((A40^D0), 3);
217     B3 = ROL64((A21^D1), 45);
218     B4 = ROL64((A02^D2), 61);
219     B0 = ROL64((A33^D3), 28);
220     B1 = ROL64((A14^D4), 20);
221     A40 =   B0 ^((~B1)&  B2 );
222     A21 =   B1 ^((~B2)&  B3 );
223     A02 =   B2 ^((~B3)&  B4 );
224     A33 =   B3 ^((~B4)&  B0 );
225     A14 =   B4 ^((~B0)&  B1 );
226 
227     B4 = ROL64((A30^D0), 18);
228     B0 = ROL64((A11^D1), 1);
229     B1 = ROL64((A42^D2), 6);
230     B2 = ROL64((A23^D3), 25);
231     B3 = ROL64((A04^D4), 8);
232     A30 =   B0 ^((~B1)&  B2 );
233     A11 =   B1 ^((~B2)&  B3 );
234     A42 =   B2 ^((~B3)&  B4 );
235     A23 =   B3 ^((~B4)&  B0 );
236     A04 =   B4 ^((~B0)&  B1 );
237 
238     B1 = ROL64((A20^D0), 36);
239     B2 = ROL64((A01^D1), 10);
240     B3 = ROL64((A32^D2), 15);
241     B4 = ROL64((A13^D3), 56);
242     B0 = ROL64((A44^D4), 27);
243     A20 =   B0 ^((~B1)&  B2 );
244     A01 =   B1 ^((~B2)&  B3 );
245     A32 =   B2 ^((~B3)&  B4 );
246     A13 =   B3 ^((~B4)&  B0 );
247     A44 =   B4 ^((~B0)&  B1 );
248 
249     B3 = ROL64((A10^D0), 41);
250     B4 = ROL64((A41^D1), 2);
251     B0 = ROL64((A22^D2), 62);
252     B1 = ROL64((A03^D3), 55);
253     B2 = ROL64((A34^D4), 39);
254     A10 =   B0 ^((~B1)&  B2 );
255     A41 =   B1 ^((~B2)&  B3 );
256     A22 =   B2 ^((~B3)&  B4 );
257     A03 =   B3 ^((~B4)&  B0 );
258     A34 =   B4 ^((~B0)&  B1 );
259 
260     C0 = A00^A40^A30^A20^A10;
261     C1 = A31^A21^A11^A01^A41;
262     C2 = A12^A02^A42^A32^A22;
263     C3 = A43^A33^A23^A13^A03;
264     C4 = A24^A14^A04^A44^A34;
265     D0 = C4^ROL64(C1, 1);
266     D1 = C0^ROL64(C2, 1);
267     D2 = C1^ROL64(C3, 1);
268     D3 = C2^ROL64(C4, 1);
269     D4 = C3^ROL64(C0, 1);
270 
271     B0 = (A00^D0);
272     B1 = ROL64((A21^D1), 44);
273     B2 = ROL64((A42^D2), 43);
274     B3 = ROL64((A13^D3), 21);
275     B4 = ROL64((A34^D4), 14);
276     A00 =   B0 ^((~B1)&  B2 );
277     A00 ^= RC[i+2];
278     A21 =   B1 ^((~B2)&  B3 );
279     A42 =   B2 ^((~B3)&  B4 );
280     A13 =   B3 ^((~B4)&  B0 );
281     A34 =   B4 ^((~B0)&  B1 );
282 
283     B2 = ROL64((A30^D0), 3);
284     B3 = ROL64((A01^D1), 45);
285     B4 = ROL64((A22^D2), 61);
286     B0 = ROL64((A43^D3), 28);
287     B1 = ROL64((A14^D4), 20);
288     A30 =   B0 ^((~B1)&  B2 );
289     A01 =   B1 ^((~B2)&  B3 );
290     A22 =   B2 ^((~B3)&  B4 );
291     A43 =   B3 ^((~B4)&  B0 );
292     A14 =   B4 ^((~B0)&  B1 );
293 
294     B4 = ROL64((A10^D0), 18);
295     B0 = ROL64((A31^D1), 1);
296     B1 = ROL64((A02^D2), 6);
297     B2 = ROL64((A23^D3), 25);
298     B3 = ROL64((A44^D4), 8);
299     A10 =   B0 ^((~B1)&  B2 );
300     A31 =   B1 ^((~B2)&  B3 );
301     A02 =   B2 ^((~B3)&  B4 );
302     A23 =   B3 ^((~B4)&  B0 );
303     A44 =   B4 ^((~B0)&  B1 );
304 
305     B1 = ROL64((A40^D0), 36);
306     B2 = ROL64((A11^D1), 10);
307     B3 = ROL64((A32^D2), 15);
308     B4 = ROL64((A03^D3), 56);
309     B0 = ROL64((A24^D4), 27);
310     A40 =   B0 ^((~B1)&  B2 );
311     A11 =   B1 ^((~B2)&  B3 );
312     A32 =   B2 ^((~B3)&  B4 );
313     A03 =   B3 ^((~B4)&  B0 );
314     A24 =   B4 ^((~B0)&  B1 );
315 
316     B3 = ROL64((A20^D0), 41);
317     B4 = ROL64((A41^D1), 2);
318     B0 = ROL64((A12^D2), 62);
319     B1 = ROL64((A33^D3), 55);
320     B2 = ROL64((A04^D4), 39);
321     A20 =   B0 ^((~B1)&  B2 );
322     A41 =   B1 ^((~B2)&  B3 );
323     A12 =   B2 ^((~B3)&  B4 );
324     A33 =   B3 ^((~B4)&  B0 );
325     A04 =   B4 ^((~B0)&  B1 );
326 
327     C0 = A00^A30^A10^A40^A20;
328     C1 = A21^A01^A31^A11^A41;
329     C2 = A42^A22^A02^A32^A12;
330     C3 = A13^A43^A23^A03^A33;
331     C4 = A34^A14^A44^A24^A04;
332     D0 = C4^ROL64(C1, 1);
333     D1 = C0^ROL64(C2, 1);
334     D2 = C1^ROL64(C3, 1);
335     D3 = C2^ROL64(C4, 1);
336     D4 = C3^ROL64(C0, 1);
337 
338     B0 = (A00^D0);
339     B1 = ROL64((A01^D1), 44);
340     B2 = ROL64((A02^D2), 43);
341     B3 = ROL64((A03^D3), 21);
342     B4 = ROL64((A04^D4), 14);
343     A00 =   B0 ^((~B1)&  B2 );
344     A00 ^= RC[i+3];
345     A01 =   B1 ^((~B2)&  B3 );
346     A02 =   B2 ^((~B3)&  B4 );
347     A03 =   B3 ^((~B4)&  B0 );
348     A04 =   B4 ^((~B0)&  B1 );
349 
350     B2 = ROL64((A10^D0), 3);
351     B3 = ROL64((A11^D1), 45);
352     B4 = ROL64((A12^D2), 61);
353     B0 = ROL64((A13^D3), 28);
354     B1 = ROL64((A14^D4), 20);
355     A10 =   B0 ^((~B1)&  B2 );
356     A11 =   B1 ^((~B2)&  B3 );
357     A12 =   B2 ^((~B3)&  B4 );
358     A13 =   B3 ^((~B4)&  B0 );
359     A14 =   B4 ^((~B0)&  B1 );
360 
361     B4 = ROL64((A20^D0), 18);
362     B0 = ROL64((A21^D1), 1);
363     B1 = ROL64((A22^D2), 6);
364     B2 = ROL64((A23^D3), 25);
365     B3 = ROL64((A24^D4), 8);
366     A20 =   B0 ^((~B1)&  B2 );
367     A21 =   B1 ^((~B2)&  B3 );
368     A22 =   B2 ^((~B3)&  B4 );
369     A23 =   B3 ^((~B4)&  B0 );
370     A24 =   B4 ^((~B0)&  B1 );
371 
372     B1 = ROL64((A30^D0), 36);
373     B2 = ROL64((A31^D1), 10);
374     B3 = ROL64((A32^D2), 15);
375     B4 = ROL64((A33^D3), 56);
376     B0 = ROL64((A34^D4), 27);
377     A30 =   B0 ^((~B1)&  B2 );
378     A31 =   B1 ^((~B2)&  B3 );
379     A32 =   B2 ^((~B3)&  B4 );
380     A33 =   B3 ^((~B4)&  B0 );
381     A34 =   B4 ^((~B0)&  B1 );
382 
383     B3 = ROL64((A40^D0), 41);
384     B4 = ROL64((A41^D1), 2);
385     B0 = ROL64((A42^D2), 62);
386     B1 = ROL64((A43^D3), 55);
387     B2 = ROL64((A44^D4), 39);
388     A40 =   B0 ^((~B1)&  B2 );
389     A41 =   B1 ^((~B2)&  B3 );
390     A42 =   B2 ^((~B3)&  B4 );
391     A43 =   B3 ^((~B4)&  B0 );
392     A44 =   B4 ^((~B0)&  B1 );
393   }
394 }
395 
396 /*
397 ** Initialize a new hash.  iSize determines the size of the hash
398 ** in bits and should be one of 224, 256, 384, or 512.  Or iSize
399 ** can be zero to use the default hash size of 256 bits.
400 */
401 static void SHA3Init(SHA3Context *p, int iSize){
402   memset(p, 0, sizeof(*p));
403   if( iSize>=128 && iSize<=512 ){
404     p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
405   }else{
406     p->nRate = (1600 - 2*256)/8;
407   }
408 #if SHA3_BYTEORDER==1234
409   /* Known to be little-endian at compile-time. No-op */
410 #elif SHA3_BYTEORDER==4321
411   p->ixMask = 7;  /* Big-endian */
412 #else
413   {
414     static unsigned int one = 1;
415     if( 1==*(unsigned char*)&one ){
416       /* Little endian.  No byte swapping. */
417       p->ixMask = 0;
418     }else{
419       /* Big endian.  Byte swap. */
420       p->ixMask = 7;
421     }
422   }
423 #endif
424 }
425 
426 /*
427 ** Make consecutive calls to the SHA3Update function to add new content
428 ** to the hash
429 */
430 static void SHA3Update(
431   SHA3Context *p,
432   const unsigned char *aData,
433   unsigned int nData
434 ){
435   unsigned int i = 0;
436 #if SHA3_BYTEORDER==1234
437   if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
438     for(; i+7<nData; i+=8){
439       p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
440       p->nLoaded += 8;
441       if( p->nLoaded>=p->nRate ){
442         KeccakF1600Step(p);
443         p->nLoaded = 0;
444       }
445     }
446   }
447 #endif
448   for(; i<nData; i++){
449 #if SHA3_BYTEORDER==1234
450     p->u.x[p->nLoaded] ^= aData[i];
451 #elif SHA3_BYTEORDER==4321
452     p->u.x[p->nLoaded^0x07] ^= aData[i];
453 #else
454     p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
455 #endif
456     p->nLoaded++;
457     if( p->nLoaded==p->nRate ){
458       KeccakF1600Step(p);
459       p->nLoaded = 0;
460     }
461   }
462 }
463 
464 /*
465 ** After all content has been added, invoke SHA3Final() to compute
466 ** the final hash.  The function returns a pointer to the binary
467 ** hash value.
468 */
469 static unsigned char *SHA3Final(SHA3Context *p){
470   unsigned int i;
471   if( p->nLoaded==p->nRate-1 ){
472     const unsigned char c1 = 0x86;
473     SHA3Update(p, &c1, 1);
474   }else{
475     const unsigned char c2 = 0x06;
476     const unsigned char c3 = 0x80;
477     SHA3Update(p, &c2, 1);
478     p->nLoaded = p->nRate - 1;
479     SHA3Update(p, &c3, 1);
480   }
481   for(i=0; i<p->nRate; i++){
482     p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
483   }
484   return &p->u.x[p->nRate];
485 }
486 /* End of the hashing logic
487 *****************************************************************************/
488 
489 /*
490 ** Implementation of the sha3(X,SIZE) function.
491 **
492 ** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
493 ** size is 256.  If X is a BLOB, it is hashed as is.
494 ** For all other non-NULL types of input, X is converted into a UTF-8 string
495 ** and the string is hashed without the trailing 0x00 terminator.  The hash
496 ** of a NULL value is NULL.
497 */
498 static void sha3Func(
499   sqlite3_context *context,
500   int argc,
501   sqlite3_value **argv
502 ){
503   SHA3Context cx;
504   int eType = sqlite3_value_type(argv[0]);
505   int nByte = sqlite3_value_bytes(argv[0]);
506   int iSize;
507   if( argc==1 ){
508     iSize = 256;
509   }else{
510     iSize = sqlite3_value_int(argv[1]);
511     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
512       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
513                                     "384 512", -1);
514       return;
515     }
516   }
517   if( eType==SQLITE_NULL ) return;
518   SHA3Init(&cx, iSize);
519   if( eType==SQLITE_BLOB ){
520     SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
521   }else{
522     SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
523   }
524   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
525 }
526 
527 /* Compute a string using sqlite3_vsnprintf() with a maximum length
528 ** of 50 bytes and add it to the hash.
529 */
530 static void hash_step_vformat(
531   SHA3Context *p,                 /* Add content to this context */
532   const char *zFormat,
533   ...
534 ){
535   va_list ap;
536   int n;
537   char zBuf[50];
538   va_start(ap, zFormat);
539   sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
540   va_end(ap);
541   n = (int)strlen(zBuf);
542   SHA3Update(p, (unsigned char*)zBuf, n);
543 }
544 
545 /*
546 ** Implementation of the sha3_query(SQL,SIZE) function.
547 **
548 ** This function compiles and runs the SQL statement(s) given in the
549 ** argument. The results are hashed using a SIZE-bit SHA3.  The default
550 ** size is 256.
551 **
552 ** The format of the byte stream that is hashed is summarized as follows:
553 **
554 **       S<n>:<sql>
555 **       R
556 **       N
557 **       I<int>
558 **       F<ieee-float>
559 **       B<size>:<bytes>
560 **       T<size>:<text>
561 **
562 ** <sql> is the original SQL text for each statement run and <n> is
563 ** the size of that text.  The SQL text is UTF-8.  A single R character
564 ** occurs before the start of each row.  N means a NULL value.
565 ** I mean an 8-byte little-endian integer <int>.  F is a floating point
566 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
567 ** B means blobs of <size> bytes.  T means text rendered as <size>
568 ** bytes of UTF-8.  The <n> and <size> values are expressed as an ASCII
569 ** text integers.
570 **
571 ** For each SQL statement in the X input, there is one S segment.  Each
572 ** S segment is followed by zero or more R segments, one for each row in the
573 ** result set.  After each R, there are one or more N, I, F, B, or T segments,
574 ** one for each column in the result set.  Segments are concatentated directly
575 ** with no delimiters of any kind.
576 */
577 static void sha3QueryFunc(
578   sqlite3_context *context,
579   int argc,
580   sqlite3_value **argv
581 ){
582   sqlite3 *db = sqlite3_context_db_handle(context);
583   const char *zSql = (const char*)sqlite3_value_text(argv[0]);
584   sqlite3_stmt *pStmt = 0;
585   int nCol;                   /* Number of columns in the result set */
586   int i;                      /* Loop counter */
587   int rc;
588   int n;
589   const char *z;
590   SHA3Context cx;
591   int iSize;
592 
593   if( argc==1 ){
594     iSize = 256;
595   }else{
596     iSize = sqlite3_value_int(argv[1]);
597     if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
598       sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
599                                     "384 512", -1);
600       return;
601     }
602   }
603   if( zSql==0 ) return;
604   SHA3Init(&cx, iSize);
605   while( zSql[0] ){
606     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
607     if( rc ){
608       char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
609                                    zSql, sqlite3_errmsg(db));
610       sqlite3_finalize(pStmt);
611       sqlite3_result_error(context, zMsg, -1);
612       sqlite3_free(zMsg);
613       return;
614     }
615     if( !sqlite3_stmt_readonly(pStmt) ){
616       char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
617       sqlite3_finalize(pStmt);
618       sqlite3_result_error(context, zMsg, -1);
619       sqlite3_free(zMsg);
620       return;
621     }
622     nCol = sqlite3_column_count(pStmt);
623     z = sqlite3_sql(pStmt);
624     n = (int)strlen(z);
625     hash_step_vformat(&cx,"S%d:",n);
626     SHA3Update(&cx,(unsigned char*)z,n);
627 
628     /* Compute a hash over the result of the query */
629     while( SQLITE_ROW==sqlite3_step(pStmt) ){
630       SHA3Update(&cx,(const unsigned char*)"R",1);
631       for(i=0; i<nCol; i++){
632         switch( sqlite3_column_type(pStmt,i) ){
633           case SQLITE_NULL: {
634             SHA3Update(&cx, (const unsigned char*)"N",1);
635             break;
636           }
637           case SQLITE_INTEGER: {
638             sqlite3_uint64 u;
639             int j;
640             unsigned char x[9];
641             sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
642             memcpy(&u, &v, 8);
643             for(j=8; j>=1; j--){
644               x[j] = u & 0xff;
645               u >>= 8;
646             }
647             x[0] = 'I';
648             SHA3Update(&cx, x, 9);
649             break;
650           }
651           case SQLITE_FLOAT: {
652             sqlite3_uint64 u;
653             int j;
654             unsigned char x[9];
655             double r = sqlite3_column_double(pStmt,i);
656             memcpy(&u, &r, 8);
657             for(j=8; j>=1; j--){
658               x[j] = u & 0xff;
659               u >>= 8;
660             }
661             x[0] = 'F';
662             SHA3Update(&cx,x,9);
663             break;
664           }
665           case SQLITE_TEXT: {
666             int n2 = sqlite3_column_bytes(pStmt, i);
667             const unsigned char *z2 = sqlite3_column_text(pStmt, i);
668             hash_step_vformat(&cx,"T%d:",n2);
669             SHA3Update(&cx, z2, n2);
670             break;
671           }
672           case SQLITE_BLOB: {
673             int n2 = sqlite3_column_bytes(pStmt, i);
674             const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
675             hash_step_vformat(&cx,"B%d:",n2);
676             SHA3Update(&cx, z2, n2);
677             break;
678           }
679         }
680       }
681     }
682     sqlite3_finalize(pStmt);
683   }
684   sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
685 }
686 
687 
688 #ifdef _WIN32
689 __declspec(dllexport)
690 #endif
691 int sqlite3_shathree_init(
692   sqlite3 *db,
693   char **pzErrMsg,
694   const sqlite3_api_routines *pApi
695 ){
696   int rc = SQLITE_OK;
697   SQLITE_EXTENSION_INIT2(pApi);
698   (void)pzErrMsg;  /* Unused parameter */
699   rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
700                                sha3Func, 0, 0);
701   if( rc==SQLITE_OK ){
702     rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
703                                  sha3Func, 0, 0);
704   }
705   if( rc==SQLITE_OK ){
706     rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
707                                  sha3QueryFunc, 0, 0);
708   }
709   if( rc==SQLITE_OK ){
710     rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
711                                  sha3QueryFunc, 0, 0);
712   }
713   return rc;
714 }
715