PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_strong_random.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
Include dependency graph for pg_strong_random.c:

Go to the source code of this file.

Functions

bool pg_strong_random (void *buf, size_t len)
 

Function Documentation

bool pg_strong_random ( void *  buf,
size_t  len 
)

Definition at line 100 of file pg_strong_random.c.

References NULL.

Referenced by init_sess_key(), mp_px_rand(), pad_eme_pkcs1_v15(), pg_frontend_random(), pg_random_bytes(), and RandomCancelKey().

101 {
102  /*
103  * When built with OpenSSL, use OpenSSL's RAND_bytes function.
104  */
105 #if defined(USE_OPENSSL_RANDOM)
106  if (RAND_bytes(buf, len) == 1)
107  return true;
108  return false;
109 
110  /*
111  * Windows has CryptoAPI for strong cryptographic numbers.
112  */
113 #elif defined(USE_WIN32_RANDOM)
114  if (hProvider == 0)
115  {
116  if (!CryptAcquireContext(&hProvider,
117  NULL,
118  MS_DEF_PROV,
119  PROV_RSA_FULL,
120  CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
121  {
122  /*
123  * On failure, set back to 0 in case the value was for some reason
124  * modified.
125  */
126  hProvider = 0;
127  }
128  }
129  /* Re-check in case we just retrieved the provider */
130  if (hProvider != 0)
131  {
132  if (CryptGenRandom(hProvider, len, buf))
133  return true;
134  }
135  return false;
136 
137  /*
138  * Read /dev/urandom ourselves.
139  */
140 #elif defined(USE_DEV_URANDOM)
141  if (random_from_file("/dev/urandom", buf, len))
142  return true;
143  return false;
144 
145 #else
146  /* The autoconf script should not have allowed this */
147 #error no source of random numbers configured
148 #endif
149 }
static char * buf
Definition: pg_test_fsync.c:66
#define NULL
Definition: c.h:229