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