blob: 7afff50885a97edfd1f4f457fdf4f47d9457c115 [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/*
drhcf5ff122013-08-21 22:09:25 +000031** Return N random bytes.
drhae85dc82001-01-13 14:34:05 +000032*/
drhcf5ff122013-08-21 22:09:25 +000033void sqlite3_randomness(int N, void *pBuf){
drhbbd82df2004-02-11 09:46:30 +000034 unsigned char t;
drhcf5ff122013-08-21 22:09:25 +000035 unsigned char *zBuf = pBuf;
drhad75e982001-10-09 04:19:46 +000036
drh78f82d12008-09-02 00:52:52 +000037 /* 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
drhcf5ff122013-08-21 22:09:25 +000050#if SQLITE_THREADSAFE
51 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
52 sqlite3_mutex_enter(mutex);
53#endif
drh78f82d12008-09-02 00:52:52 +000054
drh90bfcda2001-09-23 19:46:51 +000055 /* Initialize the state of the random number generator once,
56 ** the first time this routine is called. The seed value does
57 ** not need to contain a lot of randomness since we are not
58 ** trying to do secure encryption or anything like that...
drhae85dc82001-01-13 14:34:05 +000059 **
60 ** Nothing in this file or anywhere else in SQLite does any kind of
61 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
62 ** number generator) not as an encryption device.
63 */
drh78f82d12008-09-02 00:52:52 +000064 if( !wsdPrng.isInit ){
drhae85dc82001-01-13 14:34:05 +000065 int i;
drhae85dc82001-01-13 14:34:05 +000066 char k[256];
drh78f82d12008-09-02 00:52:52 +000067 wsdPrng.j = 0;
68 wsdPrng.i = 0;
drhd677b3d2007-08-20 22:48:41 +000069 sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
drhae85dc82001-01-13 14:34:05 +000070 for(i=0; i<256; i++){
drhea678832008-12-10 19:26:22 +000071 wsdPrng.s[i] = (u8)i;
drhae85dc82001-01-13 14:34:05 +000072 }
73 for(i=0; i<256; i++){
drh78f82d12008-09-02 00:52:52 +000074 wsdPrng.j += wsdPrng.s[i] + k[i];
75 t = wsdPrng.s[wsdPrng.j];
76 wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
77 wsdPrng.s[i] = t;
drhae85dc82001-01-13 14:34:05 +000078 }
drh78f82d12008-09-02 00:52:52 +000079 wsdPrng.isInit = 1;
drhae85dc82001-01-13 14:34:05 +000080 }
81
drhbbd82df2004-02-11 09:46:30 +000082 while( N-- ){
drhcf5ff122013-08-21 22:09:25 +000083 wsdPrng.i++;
84 t = wsdPrng.s[wsdPrng.i];
85 wsdPrng.j += t;
86 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
87 wsdPrng.s[wsdPrng.j] = t;
88 t += wsdPrng.s[wsdPrng.i];
89 *(zBuf++) = wsdPrng.s[t];
drhae85dc82001-01-13 14:34:05 +000090 }
drh51fc3472007-08-21 13:51:23 +000091 sqlite3_mutex_leave(mutex);
drhae85dc82001-01-13 14:34:05 +000092}
drh93aed5a2008-01-16 17:46:38 +000093
drh3088d592008-03-21 16:45:47 +000094#ifndef SQLITE_OMIT_BUILTIN_TEST
drh93aed5a2008-01-16 17:46:38 +000095/*
96** For testing purposes, we sometimes want to preserve the state of
drh78f82d12008-09-02 00:52:52 +000097** PRNG and restore the PRNG to its saved state at a later time, or
98** to reset the PRNG to its initial state. These routines accomplish
99** those tasks.
100**
drh2fa18682008-03-19 14:15:34 +0000101** The sqlite3_test_control() interface calls these routines to
102** control the PRNG.
drh93aed5a2008-01-16 17:46:38 +0000103*/
drh1875f7a2008-12-08 18:19:17 +0000104static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
drh2fa18682008-03-19 14:15:34 +0000105void sqlite3PrngSaveState(void){
drh78f82d12008-09-02 00:52:52 +0000106 memcpy(
107 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
108 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
109 sizeof(sqlite3Prng)
110 );
drh93aed5a2008-01-16 17:46:38 +0000111}
drh2fa18682008-03-19 14:15:34 +0000112void sqlite3PrngRestoreState(void){
drh78f82d12008-09-02 00:52:52 +0000113 memcpy(
114 &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
115 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
116 sizeof(sqlite3Prng)
117 );
drh93aed5a2008-01-16 17:46:38 +0000118}
drh2fa18682008-03-19 14:15:34 +0000119void sqlite3PrngResetState(void){
drh78f82d12008-09-02 00:52:52 +0000120 GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
drh93aed5a2008-01-16 17:46:38 +0000121}
drh3088d592008-03-21 16:45:47 +0000122#endif /* SQLITE_OMIT_BUILTIN_TEST */