random.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. ** $Id: random.c,v 1.20 2007/08/21 13:51:23 drh Exp $
  19. */
  20. #include "sqliteInt.h"
  21. /*
  22. ** Get a single 8-bit random value from the RC4 PRNG. The Mutex
  23. ** must be held while executing this routine.
  24. **
  25. ** Why not just use a library random generator like lrand48() for this?
  26. ** Because the OP_NewRowid opcode in the VDBE depends on having a very
  27. ** good source of random numbers. The lrand48() library function may
  28. ** well be good enough. But maybe not. Or maybe lrand48() has some
  29. ** subtle problems on some systems that could cause problems. It is hard
  30. ** to know. To minimize the risk of problems due to bad lrand48()
  31. ** implementations, SQLite uses this random number generator based
  32. ** on RC4, which we know works very well.
  33. **
  34. ** (Later): Actually, OP_NewRowid does not depend on a good source of
  35. ** randomness any more. But we will leave this code in all the same.
  36. */
  37. static int randomByte(void){
  38. unsigned char t;
  39. /* All threads share a single random number generator.
  40. ** This structure is the current state of the generator.
  41. */
  42. static struct {
  43. unsigned char isInit; /* True if initialized */
  44. unsigned char i, j; /* State variables */
  45. unsigned char s[256]; /* State variables */
  46. } prng;
  47. /* Initialize the state of the random number generator once,
  48. ** the first time this routine is called. The seed value does
  49. ** not need to contain a lot of randomness since we are not
  50. ** trying to do secure encryption or anything like that...
  51. **
  52. ** Nothing in this file or anywhere else in SQLite does any kind of
  53. ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
  54. ** number generator) not as an encryption device.
  55. */
  56. if( !prng.isInit ){
  57. int i;
  58. char k[256];
  59. prng.j = 0;
  60. prng.i = 0;
  61. sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
  62. for(i=0; i<256; i++){
  63. prng.s[i] = i;
  64. }
  65. for(i=0; i<256; i++){
  66. prng.j += prng.s[i] + k[i];
  67. t = prng.s[prng.j];
  68. prng.s[prng.j] = prng.s[i];
  69. prng.s[i] = t;
  70. }
  71. prng.isInit = 1;
  72. }
  73. /* Generate and return single random byte
  74. */
  75. prng.i++;
  76. t = prng.s[prng.i];
  77. prng.j += t;
  78. prng.s[prng.i] = prng.s[prng.j];
  79. prng.s[prng.j] = t;
  80. t += prng.s[prng.i];
  81. return prng.s[t];
  82. }
  83. /*
  84. ** Return N random bytes.
  85. */
  86. void sqlite3Randomness(int N, void *pBuf){
  87. unsigned char *zBuf = pBuf;
  88. static sqlite3_mutex *mutex = 0;
  89. if( mutex==0 ){
  90. mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PRNG);
  91. }
  92. sqlite3_mutex_enter(mutex);
  93. while( N-- ){
  94. *(zBuf++) = randomByte();
  95. }
  96. sqlite3_mutex_leave(mutex);
  97. }