shathree.c 25 KB

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