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