blob: 234ebdf658f436cc7c68f027c48b0e1b9ba34e98 [file] [log] [blame]
drhae85dc82001-01-13 14:34:05 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drhae85dc82001-01-13 14:34:05 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drhae85dc82001-01-13 14:34:05 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drhae85dc82001-01-13 14:34:05 +000010**
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.
drhae85dc82001-01-13 14:34:05 +000017*/
18#include "sqliteInt.h"
drhae85dc82001-01-13 14:34:05 +000019
drhaf9ff332002-01-16 21:00:27 +000020
drh93aed5a2008-01-16 17:46:38 +000021/* All threads share a single random number generator.
22** This structure is the current state of the generator.
23*/
drh78f82d12008-09-02 00:52:52 +000024static SQLITE_WSD struct sqlite3PrngType {
drh93aed5a2008-01-16 17:46:38 +000025 unsigned char isInit; /* True if initialized */
26 unsigned char i, j; /* State variables */
27 unsigned char s[256]; /* State variables */
drh1875f7a2008-12-08 18:19:17 +000028} sqlite3Prng;
drh93aed5a2008-01-16 17:46:38 +000029
drhae85dc82001-01-13 14:34:05 +000030/*
drhad75e982001-10-09 04:19:46 +000031** Get a single 8-bit random value from the RC4 PRNG. The Mutex
32** must be held while executing this routine.
drhaf9ff332002-01-16 21:00:27 +000033**
34** Why not just use a library random generator like lrand48() for this?
drhf0863fe2005-06-12 21:35:51 +000035** Because the OP_NewRowid opcode in the VDBE depends on having a very
drhaf9ff332002-01-16 21:00:27 +000036** good source of random numbers. The lrand48() library function may
37** well be good enough. But maybe not. Or maybe lrand48() has some
38** subtle problems on some systems that could cause problems. It is hard
39** to know. To minimize the risk of problems due to bad lrand48()
drhaaab5722002-02-19 13:39:21 +000040** implementations, SQLite uses this random number generator based
drhaf9ff332002-01-16 21:00:27 +000041** on RC4, which we know works very well.
drhf0863fe2005-06-12 21:35:51 +000042**
43** (Later): Actually, OP_NewRowid does not depend on a good source of
44** randomness any more. But we will leave this code in all the same.
drhae85dc82001-01-13 14:34:05 +000045*/
drhea678832008-12-10 19:26:22 +000046static u8 randomByte(void){
drhbbd82df2004-02-11 09:46:30 +000047 unsigned char t;
drhae85dc82001-01-13 14:34:05 +000048
drhad75e982001-10-09 04:19:46 +000049
drh78f82d12008-09-02 00:52:52 +000050 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
51 ** state vector. If writable static data is unsupported on the target,
52 ** we have to locate the state vector at run-time. In the more common
53 ** case where writable static data is supported, wsdPrng can refer directly
54 ** to the "sqlite3Prng" state vector declared above.
55 */
56#ifdef SQLITE_OMIT_WSD
57 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
58# define wsdPrng p[0]
59#else
60# define wsdPrng sqlite3Prng
61#endif
62
63
drh90bfcda2001-09-23 19:46:51 +000064 /* Initialize the state of the random number generator once,
65 ** the first time this routine is called. The seed value does
66 ** not need to contain a lot of randomness since we are not
67 ** trying to do secure encryption or anything like that...
drhae85dc82001-01-13 14:34:05 +000068 **
69 ** Nothing in this file or anywhere else in SQLite does any kind of
70 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
71 ** number generator) not as an encryption device.
72 */
drh78f82d12008-09-02 00:52:52 +000073 if( !wsdPrng.isInit ){
drhae85dc82001-01-13 14:34:05 +000074 int i;
drhae85dc82001-01-13 14:34:05 +000075 char k[256];
drh78f82d12008-09-02 00:52:52 +000076 wsdPrng.j = 0;
77 wsdPrng.i = 0;
drhd677b3d2007-08-20 22:48:41 +000078 sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
drhae85dc82001-01-13 14:34:05 +000079 for(i=0; i<256; i++){
drhea678832008-12-10 19:26:22 +000080 wsdPrng.s[i] = (u8)i;
drhae85dc82001-01-13 14:34:05 +000081 }
82 for(i=0; i<256; i++){
drh78f82d12008-09-02 00:52:52 +000083 wsdPrng.j += wsdPrng.s[i] + k[i];
84 t = wsdPrng.s[wsdPrng.j];
85 wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
86 wsdPrng.s[i] = t;
drhae85dc82001-01-13 14:34:05 +000087 }
drh78f82d12008-09-02 00:52:52 +000088 wsdPrng.isInit = 1;
drhae85dc82001-01-13 14:34:05 +000089 }
90
91 /* Generate and return single random byte
92 */
drh78f82d12008-09-02 00:52:52 +000093 wsdPrng.i++;
94 t = wsdPrng.s[wsdPrng.i];
95 wsdPrng.j += t;
96 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
97 wsdPrng.s[wsdPrng.j] = t;
98 t += wsdPrng.s[wsdPrng.i];
99 return wsdPrng.s[t];
drhad75e982001-10-09 04:19:46 +0000100}
101
102/*
drhbbd82df2004-02-11 09:46:30 +0000103** Return N random bytes.
drhad75e982001-10-09 04:19:46 +0000104*/
drh2fa18682008-03-19 14:15:34 +0000105void sqlite3_randomness(int N, void *pBuf){
drhbbd82df2004-02-11 09:46:30 +0000106 unsigned char *zBuf = pBuf;
drh18472fa2008-10-07 15:25:48 +0000107#if SQLITE_THREADSAFE
danielk197759f8c082008-06-18 17:09:10 +0000108 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
drh65bbf292008-06-19 01:03:17 +0000109#endif
drh51fc3472007-08-21 13:51:23 +0000110 sqlite3_mutex_enter(mutex);
drhbbd82df2004-02-11 09:46:30 +0000111 while( N-- ){
112 *(zBuf++) = randomByte();
drhae85dc82001-01-13 14:34:05 +0000113 }
drh51fc3472007-08-21 13:51:23 +0000114 sqlite3_mutex_leave(mutex);
drhae85dc82001-01-13 14:34:05 +0000115}
drh93aed5a2008-01-16 17:46:38 +0000116
drh3088d592008-03-21 16:45:47 +0000117#ifndef SQLITE_OMIT_BUILTIN_TEST
drh93aed5a2008-01-16 17:46:38 +0000118/*
119** For testing purposes, we sometimes want to preserve the state of
drh78f82d12008-09-02 00:52:52 +0000120** PRNG and restore the PRNG to its saved state at a later time, or
121** to reset the PRNG to its initial state. These routines accomplish
122** those tasks.
123**
drh2fa18682008-03-19 14:15:34 +0000124** The sqlite3_test_control() interface calls these routines to
125** control the PRNG.
drh93aed5a2008-01-16 17:46:38 +0000126*/
drh1875f7a2008-12-08 18:19:17 +0000127static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
drh2fa18682008-03-19 14:15:34 +0000128void sqlite3PrngSaveState(void){
drh78f82d12008-09-02 00:52:52 +0000129 memcpy(
130 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
131 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
132 sizeof(sqlite3Prng)
133 );
drh93aed5a2008-01-16 17:46:38 +0000134}
drh2fa18682008-03-19 14:15:34 +0000135void sqlite3PrngRestoreState(void){
drh78f82d12008-09-02 00:52:52 +0000136 memcpy(
137 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
138 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
139 sizeof(sqlite3Prng)
140 );
drh93aed5a2008-01-16 17:46:38 +0000141}
drh2fa18682008-03-19 14:15:34 +0000142void sqlite3PrngResetState(void){
drh78f82d12008-09-02 00:52:52 +0000143 GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
drh93aed5a2008-01-16 17:46:38 +0000144}
drh3088d592008-03-21 16:45:47 +0000145#endif /* SQLITE_OMIT_BUILTIN_TEST */