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