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 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 hexadecimal hash here */ 200 ){ 201 unsigned int i; 202 unsigned char finalcount[8]; 203 unsigned char digest[20]; 204 static const char zEncode[] = "0123456789abcdef"; 205 206 for (i = 0; i < 8; i++){ 207 finalcount[i] = (unsigned char)((p->count[(i >= 4 ? 0 : 1)] 208 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ 209 } 210 hash_step(p, (const unsigned char *)"\200", 1); 211 while ((p->count[0] & 504) != 448){ 212 hash_step(p, (const unsigned char *)"\0", 1); 213 } 214 hash_step(p, finalcount, 8); /* Should cause a SHA1Transform() */ 215 for (i = 0; i < 20; i++){ 216 digest[i] = (unsigned char)((p->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); 217 } 218 for(i=0; i<20; i++){ 219 zOut[i*2] = zEncode[(digest[i]>>4)&0xf]; 220 zOut[i*2+1] = zEncode[digest[i] & 0xf]; 221 } 222 zOut[i*2]= 0; 223 } 224 /* End of the hashing logic 225 *****************************************************************************/ 226 227 /* 228 ** Implementation of the sha1(X) function. 229 ** 230 ** Return a lower-case hexadecimal rendering of the SHA1 hash of the 231 ** argument X. If X is a BLOB, it is hashed as is. For all other 232 ** types of input, X is converted into a UTF-8 string and the string 233 ** is hash without the trailing 0x00 terminator. The hash of a NULL 234 ** value is NULL. 235 */ 236 static void sha1Func( 237 sqlite3_context *context, 238 int argc, 239 sqlite3_value **argv 240 ){ 241 SHA1Context cx; 242 int eType = sqlite3_value_type(argv[0]); 243 int nByte = sqlite3_value_bytes(argv[0]); 244 char zOut[44]; 245 246 assert( argc==1 ); 247 if( eType==SQLITE_NULL ) return; 248 hash_init(&cx); 249 if( eType==SQLITE_BLOB ){ 250 hash_step(&cx, sqlite3_value_blob(argv[0]), nByte); 251 }else{ 252 hash_step(&cx, sqlite3_value_text(argv[0]), nByte); 253 } 254 hash_finish(&cx, zOut); 255 sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT); 256 } 257 258 /* 259 ** Implementation of the sha1_query(SQL) function. 260 ** 261 ** This function compiles and runs the SQL statement(s) given in the 262 ** argument. The results are hashed using SHA1 and that hash is returned. 263 ** 264 ** The original SQL text is included as part of the hash. 265 ** 266 ** The hash is not just a concatenation of the outputs. Each query 267 ** is delimited and each row and value within the query is delimited, 268 ** with all values being marked with their datatypes. 269 */ 270 static void sha1QueryFunc( 271 sqlite3_context *context, 272 int argc, 273 sqlite3_value **argv 274 ){ 275 sqlite3 *db = sqlite3_context_db_handle(context); 276 const char *zSql = (const char*)sqlite3_value_text(argv[0]); 277 sqlite3_stmt *pStmt = 0; 278 int nCol; /* Number of columns in the result set */ 279 int i; /* Loop counter */ 280 int rc; 281 int n; 282 const char *z; 283 SHA1Context cx; 284 char zOut[44]; 285 286 assert( argc==1 ); 287 if( zSql==0 ) return; 288 hash_init(&cx); 289 while( zSql[0] ){ 290 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); 291 if( rc ){ 292 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", 293 zSql, sqlite3_errmsg(db)); 294 sqlite3_finalize(pStmt); 295 sqlite3_result_error(context, zMsg, -1); 296 sqlite3_free(zMsg); 297 return; 298 } 299 if( !sqlite3_stmt_readonly(pStmt) ){ 300 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); 301 sqlite3_finalize(pStmt); 302 sqlite3_result_error(context, zMsg, -1); 303 sqlite3_free(zMsg); 304 return; 305 } 306 nCol = sqlite3_column_count(pStmt); 307 z = sqlite3_sql(pStmt); 308 n = (int)strlen(z); 309 hash_step_vformat(&cx,"S%d:",n); 310 hash_step(&cx,(unsigned char*)z,n); 311 312 /* Compute a hash over the result of the query */ 313 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 314 hash_step(&cx,(const unsigned char*)"R",1); 315 for(i=0; i<nCol; i++){ 316 switch( sqlite3_column_type(pStmt,i) ){ 317 case SQLITE_NULL: { 318 hash_step(&cx, (const unsigned char*)"N",1); 319 break; 320 } 321 case SQLITE_INTEGER: { 322 sqlite3_uint64 u; 323 int j; 324 unsigned char x[9]; 325 sqlite3_int64 v = sqlite3_column_int64(pStmt,i); 326 memcpy(&u, &v, 8); 327 for(j=8; j>=1; j--){ 328 x[j] = u & 0xff; 329 u >>= 8; 330 } 331 x[0] = 'I'; 332 hash_step(&cx, x, 9); 333 break; 334 } 335 case SQLITE_FLOAT: { 336 sqlite3_uint64 u; 337 int j; 338 unsigned char x[9]; 339 double r = sqlite3_column_double(pStmt,i); 340 memcpy(&u, &r, 8); 341 for(j=8; j>=1; j--){ 342 x[j] = u & 0xff; 343 u >>= 8; 344 } 345 x[0] = 'F'; 346 hash_step(&cx,x,9); 347 break; 348 } 349 case SQLITE_TEXT: { 350 int n2 = sqlite3_column_bytes(pStmt, i); 351 const unsigned char *z2 = sqlite3_column_text(pStmt, i); 352 hash_step_vformat(&cx,"T%d:",n2); 353 hash_step(&cx, z2, n2); 354 break; 355 } 356 case SQLITE_BLOB: { 357 int n2 = sqlite3_column_bytes(pStmt, i); 358 const unsigned char *z2 = sqlite3_column_blob(pStmt, i); 359 hash_step_vformat(&cx,"B%d:",n2); 360 hash_step(&cx, z2, n2); 361 break; 362 } 363 } 364 } 365 } 366 sqlite3_finalize(pStmt); 367 } 368 hash_finish(&cx, zOut); 369 sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT); 370 } 371 372 373 #ifdef _WIN32 374 __declspec(dllexport) 375 #endif 376 int sqlite3_sha_init( 377 sqlite3 *db, 378 char **pzErrMsg, 379 const sqlite3_api_routines *pApi 380 ){ 381 int rc = SQLITE_OK; 382 SQLITE_EXTENSION_INIT2(pApi); 383 (void)pzErrMsg; /* Unused parameter */ 384 rc = sqlite3_create_function(db, "sha1", 1, 385 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, 386 0, sha1Func, 0, 0); 387 if( rc==SQLITE_OK ){ 388 rc = sqlite3_create_function(db, "sha1_query", 1, 389 SQLITE_UTF8|SQLITE_DIRECTONLY, 0, 390 sha1QueryFunc, 0, 0); 391 } 392 return rc; 393 } 394