xref: /illumos-gate/usr/src/lib/libsqlite/src/random.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1*c5c4113dSnw141292 /*
2*c5c4113dSnw141292 ** 2001 September 15
3*c5c4113dSnw141292 **
4*c5c4113dSnw141292 ** The author disclaims copyright to this source code.  In place of
5*c5c4113dSnw141292 ** a legal notice, here is a blessing:
6*c5c4113dSnw141292 **
7*c5c4113dSnw141292 **    May you do good and not evil.
8*c5c4113dSnw141292 **    May you find forgiveness for yourself and forgive others.
9*c5c4113dSnw141292 **    May you share freely, never taking more than you give.
10*c5c4113dSnw141292 **
11*c5c4113dSnw141292 *************************************************************************
12*c5c4113dSnw141292 ** This file contains code to implement a pseudo-random number
13*c5c4113dSnw141292 ** generator (PRNG) for SQLite.
14*c5c4113dSnw141292 **
15*c5c4113dSnw141292 ** Random numbers are used by some of the database backends in order
16*c5c4113dSnw141292 ** to generate random integer keys for tables or random filenames.
17*c5c4113dSnw141292 **
18*c5c4113dSnw141292 ** $Id: random.c,v 1.11 2004/02/11 09:46:33 drh Exp $
19*c5c4113dSnw141292 */
20*c5c4113dSnw141292 #include "sqliteInt.h"
21*c5c4113dSnw141292 #include "os.h"
22*c5c4113dSnw141292 
23*c5c4113dSnw141292 
24*c5c4113dSnw141292 /*
25*c5c4113dSnw141292 ** Get a single 8-bit random value from the RC4 PRNG.  The Mutex
26*c5c4113dSnw141292 ** must be held while executing this routine.
27*c5c4113dSnw141292 **
28*c5c4113dSnw141292 ** Why not just use a library random generator like lrand48() for this?
29*c5c4113dSnw141292 ** Because the OP_NewRecno opcode in the VDBE depends on having a very
30*c5c4113dSnw141292 ** good source of random numbers.  The lrand48() library function may
31*c5c4113dSnw141292 ** well be good enough.  But maybe not.  Or maybe lrand48() has some
32*c5c4113dSnw141292 ** subtle problems on some systems that could cause problems.  It is hard
33*c5c4113dSnw141292 ** to know.  To minimize the risk of problems due to bad lrand48()
34*c5c4113dSnw141292 ** implementations, SQLite uses this random number generator based
35*c5c4113dSnw141292 ** on RC4, which we know works very well.
36*c5c4113dSnw141292 */
randomByte()37*c5c4113dSnw141292 static int randomByte(){
38*c5c4113dSnw141292   unsigned char t;
39*c5c4113dSnw141292 
40*c5c4113dSnw141292   /* All threads share a single random number generator.
41*c5c4113dSnw141292   ** This structure is the current state of the generator.
42*c5c4113dSnw141292   */
43*c5c4113dSnw141292   static struct {
44*c5c4113dSnw141292     unsigned char isInit;          /* True if initialized */
45*c5c4113dSnw141292     unsigned char i, j;            /* State variables */
46*c5c4113dSnw141292     unsigned char s[256];          /* State variables */
47*c5c4113dSnw141292   } prng;
48*c5c4113dSnw141292 
49*c5c4113dSnw141292   /* Initialize the state of the random number generator once,
50*c5c4113dSnw141292   ** the first time this routine is called.  The seed value does
51*c5c4113dSnw141292   ** not need to contain a lot of randomness since we are not
52*c5c4113dSnw141292   ** trying to do secure encryption or anything like that...
53*c5c4113dSnw141292   **
54*c5c4113dSnw141292   ** Nothing in this file or anywhere else in SQLite does any kind of
55*c5c4113dSnw141292   ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
56*c5c4113dSnw141292   ** number generator) not as an encryption device.
57*c5c4113dSnw141292   */
58*c5c4113dSnw141292   if( !prng.isInit ){
59*c5c4113dSnw141292     int i;
60*c5c4113dSnw141292     char k[256];
61*c5c4113dSnw141292     prng.j = 0;
62*c5c4113dSnw141292     prng.i = 0;
63*c5c4113dSnw141292     sqliteOsRandomSeed(k);
64*c5c4113dSnw141292     for(i=0; i<256; i++){
65*c5c4113dSnw141292       prng.s[i] = i;
66*c5c4113dSnw141292     }
67*c5c4113dSnw141292     for(i=0; i<256; i++){
68*c5c4113dSnw141292       prng.j += prng.s[i] + k[i];
69*c5c4113dSnw141292       t = prng.s[prng.j];
70*c5c4113dSnw141292       prng.s[prng.j] = prng.s[i];
71*c5c4113dSnw141292       prng.s[i] = t;
72*c5c4113dSnw141292     }
73*c5c4113dSnw141292     prng.isInit = 1;
74*c5c4113dSnw141292   }
75*c5c4113dSnw141292 
76*c5c4113dSnw141292   /* Generate and return single random byte
77*c5c4113dSnw141292   */
78*c5c4113dSnw141292   prng.i++;
79*c5c4113dSnw141292   t = prng.s[prng.i];
80*c5c4113dSnw141292   prng.j += t;
81*c5c4113dSnw141292   prng.s[prng.i] = prng.s[prng.j];
82*c5c4113dSnw141292   prng.s[prng.j] = t;
83*c5c4113dSnw141292   t += prng.s[prng.i];
84*c5c4113dSnw141292   return prng.s[t];
85*c5c4113dSnw141292 }
86*c5c4113dSnw141292 
87*c5c4113dSnw141292 /*
88*c5c4113dSnw141292 ** Return N random bytes.
89*c5c4113dSnw141292 */
sqliteRandomness(int N,void * pBuf)90*c5c4113dSnw141292 void sqliteRandomness(int N, void *pBuf){
91*c5c4113dSnw141292   unsigned char *zBuf = pBuf;
92*c5c4113dSnw141292   sqliteOsEnterMutex();
93*c5c4113dSnw141292   while( N-- ){
94*c5c4113dSnw141292     *(zBuf++) = randomByte();
95*c5c4113dSnw141292   }
96*c5c4113dSnw141292   sqliteOsLeaveMutex();
97*c5c4113dSnw141292 }
98