Debian Codegraph @

ext/misc/shathree.c

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