random.c 3.7 KB

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