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 | 51fc347 | 2007-08-21 13:51:23 +0000 | [diff] [blame^] | 18 | ** $Id: random.c,v 1.20 2007/08/21 13:51:23 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 | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 23 | /* |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 24 | ** Get a single 8-bit random value from the RC4 PRNG. The Mutex |
| 25 | ** must be held while executing this routine. |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 26 | ** |
| 27 | ** Why not just use a library random generator like lrand48() for this? |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 28 | ** Because the OP_NewRowid opcode in the VDBE depends on having a very |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 29 | ** good source of random numbers. The lrand48() library function may |
| 30 | ** well be good enough. But maybe not. Or maybe lrand48() has some |
| 31 | ** subtle problems on some systems that could cause problems. It is hard |
| 32 | ** to know. To minimize the risk of problems due to bad lrand48() |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 33 | ** implementations, SQLite uses this random number generator based |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 34 | ** on RC4, which we know works very well. |
drh | f0863fe | 2005-06-12 21:35:51 +0000 | [diff] [blame] | 35 | ** |
| 36 | ** (Later): Actually, OP_NewRowid does not depend on a good source of |
| 37 | ** randomness any more. But we will leave this code in all the same. |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 38 | */ |
drh | aedd892 | 2007-01-05 14:38:54 +0000 | [diff] [blame] | 39 | static int randomByte(void){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 40 | unsigned char t; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 41 | |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 42 | /* All threads share a single random number generator. |
| 43 | ** This structure is the current state of the generator. |
| 44 | */ |
| 45 | static struct { |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 46 | unsigned char isInit; /* True if initialized */ |
| 47 | unsigned char i, j; /* State variables */ |
| 48 | unsigned char s[256]; /* State variables */ |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 49 | } prng; |
| 50 | |
drh | 90bfcda | 2001-09-23 19:46:51 +0000 | [diff] [blame] | 51 | /* Initialize the state of the random number generator once, |
| 52 | ** the first time this routine is called. The seed value does |
| 53 | ** not need to contain a lot of randomness since we are not |
| 54 | ** trying to do secure encryption or anything like that... |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 55 | ** |
| 56 | ** Nothing in this file or anywhere else in SQLite does any kind of |
| 57 | ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random |
| 58 | ** number generator) not as an encryption device. |
| 59 | */ |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 60 | if( !prng.isInit ){ |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 61 | int i; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 62 | char k[256]; |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 63 | prng.j = 0; |
| 64 | prng.i = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 65 | sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k); |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 66 | for(i=0; i<256; i++){ |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 67 | prng.s[i] = i; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 68 | } |
| 69 | for(i=0; i<256; i++){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 70 | prng.j += prng.s[i] + k[i]; |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 71 | t = prng.s[prng.j]; |
| 72 | prng.s[prng.j] = prng.s[i]; |
| 73 | prng.s[i] = t; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 74 | } |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 75 | prng.isInit = 1; |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* Generate and return single random byte |
| 79 | */ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 80 | prng.i++; |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 81 | t = prng.s[prng.i]; |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 82 | prng.j += t; |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 83 | prng.s[prng.i] = prng.s[prng.j]; |
| 84 | prng.s[prng.j] = t; |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 85 | t += prng.s[prng.i]; |
| 86 | return prng.s[t]; |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 90 | ** Return N random bytes. |
drh | ad75e98 | 2001-10-09 04:19:46 +0000 | [diff] [blame] | 91 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 92 | void sqlite3Randomness(int N, void *pBuf){ |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 93 | unsigned char *zBuf = pBuf; |
drh | 51fc347 | 2007-08-21 13:51:23 +0000 | [diff] [blame^] | 94 | static sqlite3_mutex *mutex = 0; |
| 95 | if( mutex==0 ){ |
| 96 | mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG); |
| 97 | } |
| 98 | sqlite3_mutex_enter(mutex); |
drh | bbd82df | 2004-02-11 09:46:30 +0000 | [diff] [blame] | 99 | while( N-- ){ |
| 100 | *(zBuf++) = randomByte(); |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 101 | } |
drh | 51fc347 | 2007-08-21 13:51:23 +0000 | [diff] [blame^] | 102 | sqlite3_mutex_leave(mutex); |
drh | ae85dc8 | 2001-01-13 14:34:05 +0000 | [diff] [blame] | 103 | } |