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. |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 19 | |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 20 | |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 21 | /* All threads share a single random number generator. |
| 22 | ** This structure is the current state of the generator. |
| 23 | */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 24 | static SQLITE_WSD struct sqlite3PrngType { |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 25 | unsigned char isInit; /* True if initialized */ |
| 26 | unsigned char i, j; /* State variables */ |
| 27 | unsigned char s[256]; /* State variables */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 28 | } sqlite3Prng; |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 29 | |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 30 | /* |
drh | cf5ff12 | 2013-08-21 22:09:25 +0000 | [diff] [blame] | 31 | ** Return N random bytes. |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 32 | */ |
drh | cf5ff12 | 2013-08-21 22:09:25 +0000 | [diff] [blame] | 33 | void sqlite3_randomness(int N, void *pBuf){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 34 | unsigned char t; |
drh | cf5ff12 | 2013-08-21 22:09:25 +0000 | [diff] [blame] | 35 | unsigned char *zBuf = pBuf; |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 36 | |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 37 | /* The "wsdPrng" macro will resolve to the pseudo-random number generator |
| 38 | ** state vector. If writable static data is unsupported on the target, |
| 39 | ** we have to locate the state vector at run-time. In the more common |
| 40 | ** case where writable static data is supported, wsdPrng can refer directly |
| 41 | ** to the "sqlite3Prng" state vector declared above. |
| 42 | */ |
| 43 | #ifdef SQLITE_OMIT_WSD |
| 44 | struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng); |
| 45 | # define wsdPrng p[0] |
| 46 | #else |
| 47 | # define wsdPrng sqlite3Prng |
| 48 | #endif |
| 49 | |
drh | cf5ff12 | 2013-08-21 22:09:25 +0000 | [diff] [blame] | 50 | #if SQLITE_THREADSAFE |
mistachkin | df9c093 | 2014-10-27 19:58:29 +0000 | [diff] [blame] | 51 | sqlite3_mutex *mutex; |
| 52 | #endif |
| 53 | |
| 54 | #ifndef SQLITE_OMIT_AUTOINIT |
| 55 | if( sqlite3_initialize() ) return; |
| 56 | #endif |
| 57 | |
| 58 | #if SQLITE_THREADSAFE |
| 59 | mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); |
drh | cf5ff12 | 2013-08-21 22:09:25 +0000 | [diff] [blame] | 60 | #endif |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 61 | |
drh | d61a18a | 2014-10-27 20:14:02 +0000 | [diff] [blame] | 62 | sqlite3_mutex_enter(mutex); |
drh | 5a5d120 | 2014-10-24 12:37:00 +0000 | [diff] [blame] | 63 | if( N<=0 || pBuf==0 ){ |
| 64 | wsdPrng.isInit = 0; |
| 65 | sqlite3_mutex_leave(mutex); |
| 66 | return; |
| 67 | } |
| 68 | |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 69 | /* Initialize the state of the random number generator once, |
| 70 | ** the first time this routine is called. The seed value does |
| 71 | ** not need to contain a lot of randomness since we are not |
| 72 | ** trying to do secure encryption or anything like that... |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 73 | ** |
| 74 | ** Nothing in this file or anywhere else in SQLite does any kind of |
| 75 | ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random |
| 76 | ** number generator) not as an encryption device. |
| 77 | */ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 78 | if( !wsdPrng.isInit ){ |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 79 | int i; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 80 | char k[256]; |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 81 | wsdPrng.j = 0; |
| 82 | wsdPrng.i = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 83 | sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k); |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 84 | for(i=0; i<256; i++){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 85 | wsdPrng.s[i] = (u8)i; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 86 | } |
| 87 | for(i=0; i<256; i++){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 88 | wsdPrng.j += wsdPrng.s[i] + k[i]; |
| 89 | t = wsdPrng.s[wsdPrng.j]; |
| 90 | wsdPrng.s[wsdPrng.j] = wsdPrng.s[i]; |
| 91 | wsdPrng.s[i] = t; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 92 | } |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 93 | wsdPrng.isInit = 1; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 94 | } |
| 95 | |
drh | fe98081 | 2014-01-01 14:00:13 +0000 | [diff] [blame] | 96 | assert( N>0 ); |
| 97 | do{ |
drh | cf5ff12 | 2013-08-21 22:09:25 +0000 | [diff] [blame] | 98 | wsdPrng.i++; |
| 99 | t = wsdPrng.s[wsdPrng.i]; |
| 100 | wsdPrng.j += t; |
| 101 | wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j]; |
| 102 | wsdPrng.s[wsdPrng.j] = t; |
| 103 | t += wsdPrng.s[wsdPrng.i]; |
| 104 | *(zBuf++) = wsdPrng.s[t]; |
drh | fe98081 | 2014-01-01 14:00:13 +0000 | [diff] [blame] | 105 | }while( --N ); |
drh | 51fc347 | 2007-08-21 13:51:23 +0000 | [diff] [blame] | 106 | sqlite3_mutex_leave(mutex); |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 107 | } |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 108 | |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 109 | #ifndef SQLITE_OMIT_BUILTIN_TEST |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 110 | /* |
| 111 | ** For testing purposes, we sometimes want to preserve the state of |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 112 | ** PRNG and restore the PRNG to its saved state at a later time, or |
| 113 | ** to reset the PRNG to its initial state. These routines accomplish |
| 114 | ** those tasks. |
| 115 | ** |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 116 | ** The sqlite3_test_control() interface calls these routines to |
| 117 | ** control the PRNG. |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 118 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 119 | static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng; |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 120 | void sqlite3PrngSaveState(void){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 121 | memcpy( |
| 122 | &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng), |
| 123 | &GLOBAL(struct sqlite3PrngType, sqlite3Prng), |
| 124 | sizeof(sqlite3Prng) |
| 125 | ); |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 126 | } |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 127 | void sqlite3PrngRestoreState(void){ |
drh | 78f82d1 | 2008-09-02 00:52:52 +0000 | [diff] [blame] | 128 | memcpy( |
| 129 | &GLOBAL(struct sqlite3PrngType, sqlite3Prng), |
| 130 | &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng), |
| 131 | sizeof(sqlite3Prng) |
| 132 | ); |
drh | 93aed5a | 2008-01-16 17:46:38 +0000 | [diff] [blame] | 133 | } |
drh | 3088d59 | 2008-03-21 16:45:47 +0000 | [diff] [blame] | 134 | #endif /* SQLITE_OMIT_BUILTIN_TEST */ |