drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code to implement a pseudo-random number |
| 13 | ** generator (PRNG) for SQLite. |
| 14 | ** |
| 15 | ** Random numbers are used by some of the database backends in order |
| 16 | ** to generate random integer keys for tables or random filenames. |
| 17 | ** |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 18 | ** $Id: random.c,v 1.27 2008/10/07 15:25:48 drh Exp $ |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 19 | */ |
| 20 | #include "sqliteInt.h" |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 21 | |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 22 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 23 | /* All threads share a single random number generator. |
| 24 | ** This structure is the current state of the generator. |
| 25 | */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 26 | static SQLITE_WSD struct sqlite3PrngType { |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 27 | unsigned char isInit; /* True if initialized */ |
| 28 | unsigned char i, j; /* State variables */ |
| 29 | unsigned char s[256]; /* State variables */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 30 | } sqlite3Prng = { 0, }; |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 31 | |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 32 | /* |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 33 | ** Get a single 8-bit random value from the RC4 PRNG. The Mutex |
| 34 | ** must be held while executing this routine. |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 35 | ** |
| 36 | ** Why not just use a library random generator like lrand48() for this? |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 37 | ** Because the OP_NewRowid opcode in the VDBE depends on having a very |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 38 | ** good source of random numbers. The lrand48() library function may |
| 39 | ** well be good enough. But maybe not. Or maybe lrand48() has some |
| 40 | ** subtle problems on some systems that could cause problems. It is hard |
| 41 | ** to know. To minimize the risk of problems due to bad lrand48() |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 42 | ** implementations, SQLite uses this random number generator based |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 43 | ** on RC4, which we know works very well. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 44 | ** |
| 45 | ** (Later): Actually, OP_NewRowid does not depend on a good source of |
| 46 | ** randomness any more. But we will leave this code in all the same. |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 47 | */ |
drh | aedd892 | 2007-01-05 14:38:54 +0000 | [diff] [blame] | 48 | static int randomByte(void){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 49 | unsigned char t; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 50 | |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 51 | |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 52 | /* The "wsdPrng" macro will resolve to the pseudo-random number generator |
| 53 | ** state vector. If writable static data is unsupported on the target, |
| 54 | ** we have to locate the state vector at run-time. In the more common |
| 55 | ** case where writable static data is supported, wsdPrng can refer directly |
| 56 | ** to the "sqlite3Prng" state vector declared above. |
| 57 | */ |
| 58 | #ifdef SQLITE_OMIT_WSD |
| 59 | struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng); |
| 60 | # define wsdPrng p[0] |
| 61 | #else |
| 62 | # define wsdPrng sqlite3Prng |
| 63 | #endif |
| 64 | |
| 65 | |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 66 | /* Initialize the state of the random number generator once, |
| 67 | ** the first time this routine is called. The seed value does |
| 68 | ** not need to contain a lot of randomness since we are not |
| 69 | ** trying to do secure encryption or anything like that... |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 70 | ** |
| 71 | ** Nothing in this file or anywhere else in SQLite does any kind of |
| 72 | ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random |
| 73 | ** number generator) not as an encryption device. |
| 74 | */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 75 | if( !wsdPrng.isInit ){ |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 76 | int i; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 77 | char k[256]; |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 78 | wsdPrng.j = 0; |
| 79 | wsdPrng.i = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 80 | sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k); |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 81 | for(i=0; i<256; i++){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 82 | wsdPrng.s[i] = i; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 83 | } |
| 84 | for(i=0; i<256; i++){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 85 | wsdPrng.j += wsdPrng.s[i] + k[i]; |
| 86 | t = wsdPrng.s[wsdPrng.j]; |
| 87 | wsdPrng.s[wsdPrng.j] = wsdPrng.s[i]; |
| 88 | wsdPrng.s[i] = t; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 89 | } |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 90 | wsdPrng.isInit = 1; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* Generate and return single random byte |
| 94 | */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 95 | wsdPrng.i++; |
| 96 | t = wsdPrng.s[wsdPrng.i]; |
| 97 | wsdPrng.j += t; |
| 98 | wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j]; |
| 99 | wsdPrng.s[wsdPrng.j] = t; |
| 100 | t += wsdPrng.s[wsdPrng.i]; |
| 101 | return wsdPrng.s[t]; |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 105 | ** Return N random bytes. |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 106 | */ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 107 | void sqlite3_randomness(int N, void *pBuf){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 108 | unsigned char *zBuf = pBuf; |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 109 | #if SQLITE_THREADSAFE |
danielk1977 | 59f8c08 | 2008-06-18 17:09:10 +0000 | [diff] [blame] | 110 | sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); |
drh | 65bbf29 | 2008-06-19 01:03:17 +0000 | [diff] [blame] | 111 | #endif |
drh | 51fc347 | 2007-08-21 13:51:23 +0000 | [diff] [blame] | 112 | sqlite3_mutex_enter(mutex); |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 113 | while( N-- ){ |
| 114 | *(zBuf++) = randomByte(); |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 115 | } |
drh | 51fc347 | 2007-08-21 13:51:23 +0000 | [diff] [blame] | 116 | sqlite3_mutex_leave(mutex); |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 117 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 118 | |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 119 | #ifndef SQLITE_OMIT_BUILTIN_TEST |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 120 | /* |
| 121 | ** For testing purposes, we sometimes want to preserve the state of |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 122 | ** PRNG and restore the PRNG to its saved state at a later time, or |
| 123 | ** to reset the PRNG to its initial state. These routines accomplish |
| 124 | ** those tasks. |
| 125 | ** |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 126 | ** The sqlite3_test_control() interface calls these routines to |
| 127 | ** control the PRNG. |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 128 | */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 129 | static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng = { 0, }; |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 130 | void sqlite3PrngSaveState(void){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 131 | memcpy( |
| 132 | &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng), |
| 133 | &GLOBAL(struct sqlite3PrngType, sqlite3Prng), |
| 134 | sizeof(sqlite3Prng) |
| 135 | ); |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 136 | } |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 137 | void sqlite3PrngRestoreState(void){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 138 | memcpy( |
| 139 | &GLOBAL(struct sqlite3PrngType, sqlite3Prng), |
| 140 | &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng), |
| 141 | sizeof(sqlite3Prng) |
| 142 | ); |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 143 | } |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 144 | void sqlite3PrngResetState(void){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 145 | GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0; |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 146 | } |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 147 | #endif /* SQLITE_OMIT_BUILTIN_TEST */ |