random.c

Go to the documentation of this file.
00001 /*
00002 ** 2001 September 15
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 ** This file contains code to implement a pseudo-random number
00013 ** generator (PRNG) for SQLite.
00014 **
00015 ** Random numbers are used by some of the database backends in order
00016 ** to generate random integer keys for tables or random filenames.
00017 **
00018 ** $Id: random.c,v 1.27 2008/10/07 15:25:48 drh Exp $
00019 */
00020 #include "sqliteInt.h"
00021 
00022 
00023 /* All threads share a single random number generator.
00024 ** This structure is the current state of the generator.
00025 */
00026 static SQLITE_WSD struct sqlite3PrngType {
00027   unsigned char isInit;          /* True if initialized */
00028   unsigned char i, j;            /* State variables */
00029   unsigned char s[256];          /* State variables */
00030 } sqlite3Prng = { 0, };
00031 
00032 /*
00033 ** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
00034 ** must be held while executing this routine.
00035 **
00036 ** Why not just use a library random generator like lrand48() for this?
00037 ** Because the OP_NewRowid opcode in the VDBE depends on having a very
00038 ** good source of random numbers.  The lrand48() library function may
00039 ** well be good enough.  But maybe not.  Or maybe lrand48() has some
00040 ** subtle problems on some systems that could cause problems.  It is hard
00041 ** to know.  To minimize the risk of problems due to bad lrand48()
00042 ** implementations, SQLite uses this random number generator based
00043 ** on RC4, which we know works very well.
00044 **
00045 ** (Later):  Actually, OP_NewRowid does not depend on a good source of
00046 ** randomness any more.  But we will leave this code in all the same.
00047 */
00048 static int randomByte(void){
00049   unsigned char t;
00050 
00051 
00052   /* The "wsdPrng" macro will resolve to the pseudo-random number generator
00053   ** state vector.  If writable static data is unsupported on the target,
00054   ** we have to locate the state vector at run-time.  In the more common
00055   ** case where writable static data is supported, wsdPrng can refer directly
00056   ** to the "sqlite3Prng" state vector declared above.
00057   */
00058 #ifdef SQLITE_OMIT_WSD
00059   struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
00060 # define wsdPrng p[0]
00061 #else
00062 # define wsdPrng sqlite3Prng
00063 #endif
00064 
00065 
00066   /* Initialize the state of the random number generator once,
00067   ** the first time this routine is called.  The seed value does
00068   ** not need to contain a lot of randomness since we are not
00069   ** trying to do secure encryption or anything like that...
00070   **
00071   ** Nothing in this file or anywhere else in SQLite does any kind of
00072   ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
00073   ** number generator) not as an encryption device.
00074   */
00075   if( !wsdPrng.isInit ){
00076     int i;
00077     char k[256];
00078     wsdPrng.j = 0;
00079     wsdPrng.i = 0;
00080     sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
00081     for(i=0; i<256; i++){
00082       wsdPrng.s[i] = i;
00083     }
00084     for(i=0; i<256; i++){
00085       wsdPrng.j += wsdPrng.s[i] + k[i];
00086       t = wsdPrng.s[wsdPrng.j];
00087       wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
00088       wsdPrng.s[i] = t;
00089     }
00090     wsdPrng.isInit = 1;
00091   }
00092 
00093   /* Generate and return single random byte
00094   */
00095   wsdPrng.i++;
00096   t = wsdPrng.s[wsdPrng.i];
00097   wsdPrng.j += t;
00098   wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
00099   wsdPrng.s[wsdPrng.j] = t;
00100   t += wsdPrng.s[wsdPrng.i];
00101   return wsdPrng.s[t];
00102 }
00103 
00104 /*
00105 ** Return N random bytes.
00106 */
00107 void sqlite3_randomness(int N, void *pBuf){
00108   unsigned char *zBuf = pBuf;
00109 #if SQLITE_THREADSAFE
00110   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
00111 #endif
00112   sqlite3_mutex_enter(mutex);
00113   while( N-- ){
00114     *(zBuf++) = randomByte();
00115   }
00116   sqlite3_mutex_leave(mutex);
00117 }
00118 
00119 #ifndef SQLITE_OMIT_BUILTIN_TEST
00120 /*
00121 ** For testing purposes, we sometimes want to preserve the state of
00122 ** PRNG and restore the PRNG to its saved state at a later time, or
00123 ** to reset the PRNG to its initial state.  These routines accomplish
00124 ** those tasks.
00125 **
00126 ** The sqlite3_test_control() interface calls these routines to
00127 ** control the PRNG.
00128 */
00129 static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng = { 0, };
00130 void sqlite3PrngSaveState(void){
00131   memcpy(
00132     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
00133     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
00134     sizeof(sqlite3Prng)
00135   );
00136 }
00137 void sqlite3PrngRestoreState(void){
00138   memcpy(
00139     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
00140     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
00141     sizeof(sqlite3Prng)
00142   );
00143 }
00144 void sqlite3PrngResetState(void){
00145   GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
00146 }
00147 #endif /* SQLITE_OMIT_BUILTIN_TEST */

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:56 2011 by Doxygen 1.6.1