1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-13 07:41:39 +03:00

Replace PostmasterRandom() with a stronger way of generating randomness.

This adds a new routine, pg_strong_random() for generating random bytes,
for use in both frontend and backend. At the moment, it's only used in
the backend, but the upcoming SCRAM authentication patches need strong
random numbers in libpq as well.

pg_strong_random() is based on, and replaces, the existing implementation
in pgcrypto. It can acquire strong random numbers from a number of sources,
depending on what's available:
- OpenSSL RAND_bytes(), if built with OpenSSL
- On Windows, the native cryptographic functions are used
- /dev/urandom
- /dev/random

Original patch by Magnus Hagander, with further work by Michael Paquier
and me.

Discussion: <CAB7nPqRy3krN8quR9XujMVVHYtXJ0_60nqgVc6oUk8ygyVkZsA@mail.gmail.com>
This commit is contained in:
Heikki Linnakangas
2016-10-17 11:52:50 +03:00
parent 5dfc198146
commit 9e083fd468
9 changed files with 246 additions and 386 deletions

View File

@ -626,8 +626,6 @@ static time_t check_time = 0;
static void
system_reseed(void)
{
uint8 buf[1024];
int n;
time_t t;
int skip = 1;
@ -642,24 +640,34 @@ system_reseed(void)
else if (check_time == 0 ||
(t - check_time) > SYSTEM_RESEED_CHECK_TIME)
{
uint8 buf;
check_time = t;
/* roll dice */
px_get_random_bytes(buf, 1);
skip = buf[0] >= SYSTEM_RESEED_CHANCE;
px_get_random_bytes(&buf, 1);
skip = (buf >= SYSTEM_RESEED_CHANCE);
/* clear 1 byte */
px_memset(&buf, 0, sizeof(buf));
}
/* clear 1 byte */
px_memset(buf, 0, sizeof(buf));
if (!skip)
{
/*
* fortuna_add_entropy passes the input to SHA-256, so there's no
* point in giving it more than 256 bits of input to begin with.
*/
uint8 buf[32];
if (skip)
return;
if (!pg_strong_random(buf, sizeof(buf)))
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("could not acquire random data")));
fortuna_add_entropy(buf, sizeof(buf));
n = px_acquire_system_randomness(buf);
if (n > 0)
fortuna_add_entropy(buf, n);
seed_time = t;
px_memset(buf, 0, sizeof(buf));
seed_time = t;
px_memset(buf, 0, sizeof(buf));
}
}
int