Debian Codegraph @

ext/misc/sha1.c

raw
1 /*
2 ** 2017-01-27
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 SHA1 hashes.
14 ** Two SQL functions are implemented:
15 **
16 ** sha1(X)
17 ** sha1_query(Y)
18 **
19 ** The sha1(X) function computes the SHA1 hash of the input X, or NULL if
20 ** X is NULL.
21 **
22 ** The sha1_query(Y) function evalutes all queries in the SQL statements of Y
23 ** and returns a hash of their results.
24 */
25 #include "sqlite3ext.h"
26 SQLITE_EXTENSION_INIT1
27 #include <assert.h>
28 #include <string.h>
29 #include <stdarg.h>
30
31 /******************************************************************************
32 ** The Hash Engine
33 */
34 /* Context for the SHA1 hash */
35 typedef struct SHA1Context SHA1Context;
36 struct SHA1Context {
37 unsigned int state[5];
38 unsigned int count[2];
39 unsigned char buffer[64];
40 };
41
42 #define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r))
43 #define rol(x,k) SHA_ROT(x,k,32-(k))
44 #define ror(x,k) SHA_ROT(x,32-(k),k)
45
46 #define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \
47 |(rol(block[i],8)&0x00FF00FF))
48 #define blk0be(i) block[i]
49 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
50 ^block[(i+2)&15]^block[i&15],1))
51
52 /*
53 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
54 *
55 * Rl0() for little-endian and Rb0() for big-endian. Endianness is
56 * determined at run-time.
57 */
58 #define Rl0(v,w,x,y,z,i) \
59 z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2);
60 #define Rb0(v,w,x,y,z,i) \
61 z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2);
62 #define R1(v,w,x,y,z,i) \
63 z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2);
64 #define R2(v,w,x,y,z,i) \
65 z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2);
66 #define R3(v,w,x,y,z,i) \
67 z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2);
68 #define R4(v,w,x,y,z,i) \
69 z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2);
70
71 /*
72 * Hash a single 512-bit block. This is the core of the algorithm.
73 */
74 static void SHA1Transform(unsigned int state[5], const unsigned char buffer[64]){
75 unsigned int qq[5]; /* a, b, c, d, e; */
76 static int one = 1;
77 unsigned int block[16];
78 memcpy(block, buffer, 64);
79 memcpy(qq,state,5*sizeof(unsigned int));
80
81 #define a qq[0]
82 #define b qq[1]
83 #define c qq[2]
84 #define d qq[3]
85 #define e qq[4]
86
87 /* Copy p->state[] to working vars */
88 /*
89 a = state[0];
90 b = state[1];
91 c = state[2];
92 d = state[3];
93 e = state[4];
94 */
95
96 /* 4 rounds of 20 operations each. Loop unrolled. */
97 if( 1 == *(unsigned char*)&one ){
98 Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3);
99 Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7);
100 Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11);
101 Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15);
102 }else{
103 Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3);
104 Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7);
105 Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11);
106 Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15);
107 }
108 R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
109 R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
110 R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
111 R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
112 R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
113 R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
114 R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
115 R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
116 R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
117 R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
118 R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
119 R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
120 R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
121 R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
122 R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
123 R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
124
125 /* Add the working vars back into context.state[] */
126 state[0] += a;
127 state[1] += b;
128 state[2] += c;
129 state[3] += d;
130 state[4] += e;
131
132 #undef a
133 #undef b
134 #undef c
135 #undef d
136 #undef e
137 }
138
139
140 /* Initialize a SHA1 context */
141 static void hash_init(SHA1Context *p){
142 /* SHA1 initialization constants */
143 p->state[0] = 0x67452301;
144 p->state[1] = 0xEFCDAB89;
145 p->state[2] = 0x98BADCFE;
146 p->state[3] = 0x10325476;
147 p->state[4] = 0xC3D2E1F0;
148 p->count[0] = p->count[1] = 0;
149 }
150
151 /* Add new content to the SHA1 hash */
152 static void hash_step(
153 SHA1Context *p, /* Add content to this context */
154 const unsigned char *data, /* Data to be added */
155 unsigned int len /* Number of bytes in data */
156 ){
157 unsigned int i, j;
158
159 j = p->count[0];
160 if( (p->count[0] += len << 3) < j ){
161 p->count[1] += (len>>29)+1;
162 }
163 j = (j >> 3) & 63;
164 if( (j + len) > 63 ){
165 (void)memcpy(&p->buffer[j], data, (i = 64-j));
166 SHA1Transform(p->state, p->buffer);
167 for(; i + 63 < len; i += 64){
168 SHA1Transform(p->state, &data[i]);
169 }
170 j = 0;
171 }else{
172 i = 0;
173 }
174 (void)memcpy(&p->buffer[j], &data[i], len - i);
175 }
176
177 /* Compute a string using sqlite3_vsnprintf() and hash it */
178 static void hash_step_vformat(
179 SHA1Context *p, /* Add content to this context */
180 const char *zFormat,
181 ...
182 ){
183 va_list ap;
184 int n;
185 char zBuf[50];
186 va_start(ap, zFormat);
187 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
188 va_end(ap);
189 n = (int)strlen(zBuf);
190 hash_step(p, (unsigned char*)zBuf, n);
191 }
192
193
194 /* Add padding and compute the message digest. Render the
195 ** message digest as lower-case hexadecimal and put it into
196 ** zOut[]. zOut[] must be at least 41 bytes long. */
197 static void hash_finish(
198 SHA1Context *p, /* The SHA1 context to finish and render */
199 char *zOut, /* Store hex or binary hash here */
200 int bAsBinary /* 1 for binary hash, 0 for hex hash */
201 ){
202 unsigned int i;
203 unsigned char finalcount[8];
204 unsigned char digest[20];
205 static const char zEncode[] = "0123456789abcdef";
206
207 for (i = 0; i < 8; i++){
208 finalcount[i] = (unsigned char)((p->count[(i >= 4 ? 0 : 1)]
209 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
210 }
211 hash_step(p, (const unsigned char *)"\200", 1);
212 while ((p->count[0] & 504) != 448){
213 hash_step(p, (const unsigned char *)"\0", 1);
214 }
215 hash_step(p, finalcount, 8); /* Should cause a SHA1Transform() */
216 for (i = 0; i < 20; i++){
217 digest[i] = (unsigned char)((p->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
218 }
219 if( bAsBinary ){
220 memcpy(zOut, digest, 20);
221 }else{
222 for(i=0; i<20; i++){
223 zOut[i*2] = zEncode[(digest[i]>>4)&0xf];
224 zOut[i*2+1] = zEncode[digest[i] & 0xf];
225 }
226 zOut[i*2]= 0;
227 }
228 }
229 /* End of the hashing logic
230 *****************************************************************************/
231
232 /*
233 ** Two SQL functions: sha1(X) and sha1b(X).
234 **
235 ** sha1(X) returns a lower-case hexadecimal rendering of the SHA1 hash
236 ** of the argument X. If X is a BLOB, it is hashed as is. For all other
237 ** types of input, X is converted into a UTF-8 string and the string
238 ** is hashed without the trailing 0x00 terminator. The hash of a NULL
239 ** value is NULL.
240 **
241 ** sha1b(X) is the same except that it returns a 20-byte BLOB containing
242 ** the binary hash instead of a hexadecimal string.
243 */
244 static void sha1Func(
245 sqlite3_context *context,
246 int argc,
247 sqlite3_value **argv
248 ){
249 SHA1Context cx;
250 int eType = sqlite3_value_type(argv[0]);
251 int nByte = sqlite3_value_bytes(argv[0]);
252 const unsigned char *pData;
253 char zOut[44];
254
255 assert( argc==1 );
256 if( eType==SQLITE_NULL ) return;
257 hash_init(&cx);
258 if( eType==SQLITE_BLOB ){
259 pData = (const unsigned char*)sqlite3_value_blob(argv[0]);
260 }else{
261 pData = (const unsigned char*)sqlite3_value_text(argv[0]);
262 }
263 if( pData==0 ) return;
264 hash_step(&cx, pData, nByte);
265 if( sqlite3_user_data(context)!=0 ){
266 /* sha1b() - binary result */
267 hash_finish(&cx, zOut, 1);
268 sqlite3_result_blob(context, zOut, 20, SQLITE_TRANSIENT);
269 }else{
270 /* sha1() - hexadecimal text result */
271 hash_finish(&cx, zOut, 0);
272 sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT);
273 }
274 }
275
276 /*
277 ** Implementation of the sha1_query(SQL) function.
278 **
279 ** This function compiles and runs the SQL statement(s) given in the
280 ** argument. The results are hashed using SHA1 and that hash is returned.
281 **
282 ** The original SQL text is included as part of the hash.
283 **
284 ** The hash is not just a concatenation of the outputs. Each query
285 ** is delimited and each row and value within the query is delimited,
286 ** with all values being marked with their datatypes.
287 */
288 static void sha1QueryFunc(
289 sqlite3_context *context,
290 int argc,
291 sqlite3_value **argv
292 ){
293 sqlite3 *db = sqlite3_context_db_handle(context);
294 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
295 sqlite3_stmt *pStmt = 0;
296 int nCol; /* Number of columns in the result set */
297 int i; /* Loop counter */
298 int rc;
299 int n;
300 const char *z;
301 SHA1Context cx;
302 char zOut[44];
303
304 assert( argc==1 );
305 if( zSql==0 ) return;
306 hash_init(&cx);
307 while( zSql[0] ){
308 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
309 if( rc ){
310 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
311 zSql, sqlite3_errmsg(db));
312 sqlite3_finalize(pStmt);
313 sqlite3_result_error(context, zMsg, -1);
314 sqlite3_free(zMsg);
315 return;
316 }
317 if( !sqlite3_stmt_readonly(pStmt) ){
318 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
319 sqlite3_finalize(pStmt);
320 sqlite3_result_error(context, zMsg, -1);
321 sqlite3_free(zMsg);
322 return;
323 }
324 nCol = sqlite3_column_count(pStmt);
325 z = sqlite3_sql(pStmt);
326 if( z==0 ) z = "";
327 n = (int)strlen(z);
328 hash_step_vformat(&cx,"S%d:",n);
329 hash_step(&cx,(unsigned char*)z,n);
330
331 /* Compute a hash over the result of the query */
332 while( SQLITE_ROW==sqlite3_step(pStmt) ){
333 hash_step(&cx,(const unsigned char*)"R",1);
334 for(i=0; i<nCol; i++){
335 switch( sqlite3_column_type(pStmt,i) ){
336 case SQLITE_NULL: {
337 hash_step(&cx, (const unsigned char*)"N",1);
338 break;
339 }
340 case SQLITE_INTEGER: {
341 sqlite3_uint64 u;
342 int j;
343 unsigned char x[9];
344 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
345 memcpy(&u, &v, 8);
346 for(j=8; j>=1; j--){
347 x[j] = u & 0xff;
348 u >>= 8;
349 }
350 x[0] = 'I';
351 hash_step(&cx, x, 9);
352 break;
353 }
354 case SQLITE_FLOAT: {
355 sqlite3_uint64 u;
356 int j;
357 unsigned char x[9];
358 double r = sqlite3_column_double(pStmt,i);
359 memcpy(&u, &r, 8);
360 for(j=8; j>=1; j--){
361 x[j] = u & 0xff;
362 u >>= 8;
363 }
364 x[0] = 'F';
365 hash_step(&cx,x,9);
366 break;
367 }
368 case SQLITE_TEXT: {
369 int n2 = sqlite3_column_bytes(pStmt, i);
370 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
371 hash_step_vformat(&cx,"T%d:",n2);
372 hash_step(&cx, z2, n2);
373 break;
374 }
375 case SQLITE_BLOB: {
376 int n2 = sqlite3_column_bytes(pStmt, i);
377 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
378 hash_step_vformat(&cx,"B%d:",n2);
379 hash_step(&cx, z2, n2);
380 break;
381 }
382 }
383 }
384 }
385 sqlite3_finalize(pStmt);
386 }
387 hash_finish(&cx, zOut, 0);
388 sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT);
389 }
390
391
392 #ifdef _WIN32
393 __declspec(dllexport)
394 #endif
395 int sqlite3_sha_init(
396 sqlite3 *db,
397 char **pzErrMsg,
398 const sqlite3_api_routines *pApi
399 ){
400 int rc = SQLITE_OK;
401 static int one = 1;
402 SQLITE_EXTENSION_INIT2(pApi);
403 (void)pzErrMsg; /* Unused parameter */
404 rc = sqlite3_create_function(db, "sha1", 1,
405 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
406 0, sha1Func, 0, 0);
407 if( rc==SQLITE_OK ){
408 rc = sqlite3_create_function(db, "sha1b", 1,
409 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
410 (void*)&one, sha1Func, 0, 0);
411 }
412 if( rc==SQLITE_OK ){
413 rc = sqlite3_create_function(db, "sha1_query", 1,
414 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
415 sha1QueryFunc, 0, 0);
416 }
417 return rc;
418 }