From: drh Date: Mon, 15 Feb 2016 20:41:56 +0000 (+0000) Subject: Improved entropy gathering for the implementation of winRandomness(). X-Git-Tag: version-3.12.0~189 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8eb3790a8d228d363836c1fcbe790325c747436f;p=thirdparty%2Fsqlite.git Improved entropy gathering for the implementation of winRandomness(). FossilOrigin-Name: fea3705e7750d276a8c8300b7a82dfae2d5979c0 --- diff --git a/manifest b/manifest index 4dbb251d56..223eb2086c 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sworking\s-q\sand\s--help\soptions\sto\stestfixture. -D 2016-02-15T19:38:17.026 +C Improved\sentropy\sgathering\sfor\sthe\simplementation\sof\swinRandomness(). +D 2016-02-15T20:41:56.528 F Makefile.in 4e90dc1521879022aa9479268a4cd141d1771142 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 30f075dc4f27a07abb76088946b2944178d85347 @@ -333,7 +333,7 @@ F src/os.h 3e57a24e2794a94d3cf2342c6d9a884888cd96bf F src/os_common.h b2f4707a603e36811d9b1a13278bffd757857b85 F src/os_setup.h c9d4553b5aaa6f73391448b265b89bed0b890faa F src/os_unix.c 3a6f20736dfb8a0949cdd66553fdf59f6604be35 -F src/os_win.c 6cce4c3aab5fb5cc6fabf999d785cfccb66ff9d4 +F src/os_win.c ae1f287a5a47e6cc08c0ac8dd0cfa2c67358f73a F src/os_win.h eb7a47aa17b26b77eb97e4823f20a00b8bda12ca F src/pager.c 6812f3803951774b56abded396171e1c12b0b003 F src/pager.h f3eb324a3ff2408b28bab7e81c1c55c13720f865 @@ -1427,7 +1427,7 @@ F tool/vdbe_profile.tcl 246d0da094856d72d2c12efec03250d71639d19f F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh a98af506df552f3b3c0d904f94e4cdc4e1a6d598 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 153f01a821e9844d0a2e418eb83c60f6c7695c8d -R b996ecd577900723d8e8c822f6ba0fa2 +P 404494e52b6385671ccde1c83b7b868986645536 +R 65a12199f7ba9145cf2ed73477d5bc07 U drh -Z 1c21e4bc8961adde657fcb0992c7a61d +Z f5568a99b7bf2a6087045b1c5b2d107b diff --git a/manifest.uuid b/manifest.uuid index 33f1beae3e..85665bc05f 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -404494e52b6385671ccde1c83b7b868986645536 \ No newline at end of file +fea3705e7750d276a8c8300b7a82dfae2d5979c0 \ No newline at end of file diff --git a/src/os_win.c b/src/os_win.c index caa6dea5db..9824ffb1c7 100644 --- a/src/os_win.c +++ b/src/os_win.c @@ -5390,65 +5390,82 @@ static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ #define winDlClose 0 #endif +/* State information for the randomness gatherer. */ +typedef struct EntropyGatherer EntropyGatherer; +struct EntropyGatherer { + unsigned char *a; /* Gather entropy into this buffer */ + int na; /* Size of a[] in bytes */ + int i; /* XOR next input into a[i] */ + int nXor; /* Number of XOR operations done */ +}; + +#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) +/* Mix sz bytes of entropy into p. */ +static void xorMemory(EntropyGatherer *p, unsigned char *x, int sz){ + int j, k; + for(j=0, k=p->i; ja[k++] ^= x[j]; + if( k>=p->na ) k = 0; + } + p->i = k; + p->nXor += sz; +} +#endif /* ** Write up to nBuf bytes of randomness into zBuf. */ static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ - int n = 0; - UNUSED_PARAMETER(pVfs); #if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) - n = nBuf; + UNUSED_PARAMETER(pVfs); memset(zBuf, 0, nBuf); + return nBuf; #else - if( sizeof(SYSTEMTIME)<=nBuf-n ){ + EntropyGatherer e; + UNUSED_PARAMETER(pVfs); + memset(zBuf, 0, nBuf); + e.a = (unsigned char*)zBuf; + e.na = nBuf; + e.nXor = 0; + e.i = 0; + { SYSTEMTIME x; osGetSystemTime(&x); - memcpy(&zBuf[n], &x, sizeof(x)); - n += sizeof(x); + xorMemory(&e, (unsigned char*)&x, sizeof(x)); } - if( sizeof(DWORD)<=nBuf-n ){ + { DWORD pid = osGetCurrentProcessId(); - memcpy(&zBuf[n], &pid, sizeof(pid)); - n += sizeof(pid); + xorMemory(&e, (unsigned char*)&pid, sizeof(pid)); } #if SQLITE_OS_WINRT - if( sizeof(ULONGLONG)<=nBuf-n ){ + { ULONGLONG cnt = osGetTickCount64(); - memcpy(&zBuf[n], &cnt, sizeof(cnt)); - n += sizeof(cnt); + xorMemory(&e, (unsigned char*)&cnt, sizeof(cnt)); } #else - if( sizeof(DWORD)<=nBuf-n ){ + { DWORD cnt = osGetTickCount(); - memcpy(&zBuf[n], &cnt, sizeof(cnt)); - n += sizeof(cnt); + xorMemory(&e, (unsigned char*)&cnt, sizeof(cnt)); } #endif - if( sizeof(LARGE_INTEGER)<=nBuf-n ){ + { LARGE_INTEGER i; osQueryPerformanceCounter(&i); - memcpy(&zBuf[n], &i, sizeof(i)); - n += sizeof(i); + xorMemory(&e, (unsigned char*)&i, sizeof(i)); } #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID - if( sizeof(UUID)<=nBuf-n ){ + { UUID id; memset(&id, 0, sizeof(UUID)); osUuidCreate(&id); - memcpy(&zBuf[n], &id, sizeof(UUID)); - n += sizeof(UUID); - } - if( sizeof(UUID)<=nBuf-n ){ - UUID id; + xorMemory(&e, (unsigned char*)&id, sizeof(UUID)); memset(&id, 0, sizeof(UUID)); osUuidCreateSequential(&id); - memcpy(&zBuf[n], &id, sizeof(UUID)); - n += sizeof(UUID); + xorMemory(&e, (unsigned char*)&id, sizeof(UUID)); } #endif + return e.nXor>nBuf ? nBuf : e.nXor; #endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */ - return n; }