2001-09-23 12:12:44 +08:00
|
|
|
/*
|
|
|
|
* random.c
|
2005-07-10 11:55:28 +08:00
|
|
|
* Acquire randomness from system. For seeding RNG.
|
2001-09-23 12:12:44 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) 2001 Marko Kreen
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pgcrypto/random.c
|
2001-09-23 12:12:44 +08:00
|
|
|
*/
|
|
|
|
|
2005-07-11 23:07:59 +08:00
|
|
|
#include "postgres.h"
|
2001-09-23 12:12:44 +08:00
|
|
|
|
|
|
|
#include "px.h"
|
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
/* how many bytes to ask from system random provider */
|
|
|
|
#define RND_BYTES 32
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
/*
|
|
|
|
* Try to read from /dev/urandom or /dev/random on these OS'es.
|
|
|
|
*
|
|
|
|
* The list can be pretty liberal, as the device not existing
|
|
|
|
* is expected event.
|
|
|
|
*/
|
|
|
|
#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) \
|
|
|
|
|| defined(__NetBSD__) || defined(__DragonFly__) \
|
2005-07-19 01:09:01 +08:00
|
|
|
|| defined(__darwin__) || defined(__SOLARIS__) \
|
|
|
|
|| defined(__hpux) || defined(__HPUX__) \
|
|
|
|
|| defined(__CYGWIN__) || defined(_AIX)
|
2005-07-10 11:55:28 +08:00
|
|
|
|
|
|
|
#define TRY_DEV_RANDOM
|
2001-09-23 12:12:44 +08:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static int
|
|
|
|
safe_read(int fd, void *buf, size_t count)
|
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
int done = 0;
|
|
|
|
char *p = buf;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
while (count)
|
|
|
|
{
|
2001-09-23 12:12:44 +08:00
|
|
|
res = read(fd, p, count);
|
2001-10-25 13:50:21 +08:00
|
|
|
if (res <= 0)
|
|
|
|
{
|
2001-09-23 12:12:44 +08:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2005-03-21 13:19:55 +08:00
|
|
|
return PXE_DEV_READ_ERROR;
|
2001-09-23 12:12:44 +08:00
|
|
|
}
|
|
|
|
p += res;
|
|
|
|
done += res;
|
|
|
|
count -= res;
|
|
|
|
}
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
static uint8 *
|
|
|
|
try_dev_random(uint8 *dst)
|
2001-09-23 12:12:44 +08:00
|
|
|
{
|
2001-10-25 13:50:21 +08:00
|
|
|
int fd;
|
|
|
|
int res;
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2006-06-08 11:29:30 +08:00
|
|
|
fd = open("/dev/urandom", O_RDONLY, 0);
|
2001-09-23 12:12:44 +08:00
|
|
|
if (fd == -1)
|
2005-07-10 11:55:28 +08:00
|
|
|
{
|
2006-06-08 11:29:30 +08:00
|
|
|
fd = open("/dev/random", O_RDONLY, 0);
|
2005-07-10 11:55:28 +08:00
|
|
|
if (fd == -1)
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
res = safe_read(fd, dst, RND_BYTES);
|
2001-09-23 12:12:44 +08:00
|
|
|
close(fd);
|
2005-07-10 11:55:28 +08:00
|
|
|
if (res > 0)
|
|
|
|
dst += res;
|
|
|
|
return dst;
|
2001-09-23 12:12:44 +08:00
|
|
|
}
|
2005-07-10 11:55:28 +08:00
|
|
|
#endif
|
2005-03-21 13:22:14 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
/*
|
|
|
|
* Try to find randomness on Windows
|
|
|
|
*/
|
|
|
|
#ifdef WIN32
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
#define TRY_WIN32_GENRAND
|
|
|
|
#define TRY_WIN32_PERFC
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
#include <windows.h>
|
|
|
|
#include <wincrypt.h>
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
/*
|
|
|
|
* this function is from libtomcrypt
|
2005-10-15 10:49:52 +08:00
|
|
|
*
|
2005-07-10 11:55:28 +08:00
|
|
|
* try to use Microsoft crypto API
|
|
|
|
*/
|
2005-10-15 10:49:52 +08:00
|
|
|
static uint8 *
|
|
|
|
try_win32_genrand(uint8 *dst)
|
2005-03-21 13:22:14 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
int res;
|
|
|
|
HCRYPTPROV h = 0;
|
2005-07-10 11:55:28 +08:00
|
|
|
|
|
|
|
res = CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL,
|
2005-10-15 10:49:52 +08:00
|
|
|
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET));
|
2005-07-10 11:55:28 +08:00
|
|
|
if (!res)
|
|
|
|
res = CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL,
|
2005-10-15 10:49:52 +08:00
|
|
|
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET);
|
2005-07-10 11:55:28 +08:00
|
|
|
if (!res)
|
|
|
|
return dst;
|
2005-10-15 10:49:52 +08:00
|
|
|
|
2005-07-12 01:00:03 +08:00
|
|
|
res = CryptGenRandom(h, RND_BYTES, dst);
|
2005-07-10 11:55:28 +08:00
|
|
|
if (res == TRUE)
|
2005-07-12 03:06:46 +08:00
|
|
|
dst += RND_BYTES;
|
2005-07-10 11:55:28 +08:00
|
|
|
|
|
|
|
CryptReleaseContext(h, 0);
|
|
|
|
return dst;
|
2005-03-21 13:22:14 +08:00
|
|
|
}
|
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
static uint8 *
|
|
|
|
try_win32_perfc(uint8 *dst)
|
2005-07-10 11:55:28 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
int res;
|
2005-07-10 11:55:28 +08:00
|
|
|
LARGE_INTEGER time;
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
res = QueryPerformanceCounter(&time);
|
|
|
|
if (!res)
|
|
|
|
return dst;
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
memcpy(dst, &time, sizeof(time));
|
|
|
|
return dst + sizeof(time);
|
2005-03-21 13:22:14 +08:00
|
|
|
}
|
2005-10-15 10:49:52 +08:00
|
|
|
#endif /* WIN32 */
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2001-10-25 13:50:21 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
/*
|
|
|
|
* If we are not on Windows, then hopefully we are
|
|
|
|
* on a unix-like system. Use the usual suspects
|
|
|
|
* for randomness.
|
|
|
|
*/
|
|
|
|
#ifndef WIN32
|
2001-09-23 12:12:44 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
#define TRY_UNIXSTD
|
2001-10-28 14:26:15 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Everything here is predictible, only needs some patience.
|
|
|
|
*
|
|
|
|
* But there is a chance that the system-specific functions
|
|
|
|
* did not work. So keep faith and try to slow the attacker down.
|
|
|
|
*/
|
|
|
|
static uint8 *
|
|
|
|
try_unix_std(uint8 *dst)
|
2005-03-21 13:22:14 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
pid_t pid;
|
|
|
|
int x;
|
|
|
|
PX_MD *md;
|
2005-07-10 11:55:28 +08:00
|
|
|
struct timeval tv;
|
2005-10-15 10:49:52 +08:00
|
|
|
int res;
|
2005-07-10 11:55:28 +08:00
|
|
|
|
|
|
|
/* process id */
|
|
|
|
pid = getpid();
|
2005-10-15 10:49:52 +08:00
|
|
|
memcpy(dst, (uint8 *) &pid, sizeof(pid));
|
2005-07-10 11:55:28 +08:00
|
|
|
dst += sizeof(pid);
|
|
|
|
|
|
|
|
/* time */
|
|
|
|
gettimeofday(&tv, NULL);
|
2005-10-15 10:49:52 +08:00
|
|
|
memcpy(dst, (uint8 *) &tv, sizeof(tv));
|
2005-07-10 11:55:28 +08:00
|
|
|
dst += sizeof(tv);
|
|
|
|
|
|
|
|
/* pointless, but should not hurt */
|
|
|
|
x = random();
|
2005-10-15 10:49:52 +08:00
|
|
|
memcpy(dst, (uint8 *) &x, sizeof(x));
|
2005-07-10 11:55:28 +08:00
|
|
|
dst += sizeof(x);
|
|
|
|
|
|
|
|
/* let's be desperate */
|
|
|
|
res = px_find_digest("sha1", &md);
|
2005-10-15 10:49:52 +08:00
|
|
|
if (res >= 0)
|
|
|
|
{
|
|
|
|
uint8 *ptr;
|
|
|
|
uint8 stack[8192];
|
|
|
|
int alloc = 32 * 1024;
|
2005-07-10 11:55:28 +08:00
|
|
|
|
|
|
|
px_md_update(md, stack, sizeof(stack));
|
|
|
|
ptr = px_alloc(alloc);
|
|
|
|
px_md_update(md, ptr, alloc);
|
|
|
|
px_free(ptr);
|
|
|
|
|
|
|
|
px_md_finish(md, dst);
|
|
|
|
px_md_free(md);
|
|
|
|
|
|
|
|
dst += 20;
|
|
|
|
}
|
2005-03-21 13:22:14 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
#endif
|
2005-03-21 13:22:14 +08:00
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
/*
|
|
|
|
* try to extract some randomness for initial seeding
|
|
|
|
*
|
|
|
|
* dst should have room for 1024 bytes.
|
|
|
|
*/
|
2005-10-15 10:49:52 +08:00
|
|
|
unsigned
|
|
|
|
px_acquire_system_randomness(uint8 *dst)
|
2005-07-10 11:55:28 +08:00
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
uint8 *p = dst;
|
|
|
|
|
2005-07-10 11:55:28 +08:00
|
|
|
#ifdef TRY_DEV_RANDOM
|
|
|
|
p = try_dev_random(p);
|
|
|
|
#endif
|
|
|
|
#ifdef TRY_WIN32_GENRAND
|
|
|
|
p = try_win32_genrand(p);
|
|
|
|
#endif
|
|
|
|
#ifdef TRY_WIN32_PERFC
|
|
|
|
p = try_win32_perfc(p);
|
|
|
|
#endif
|
|
|
|
#ifdef TRY_UNIXSTD
|
|
|
|
p = try_unix_std(p);
|
|
|
|
#endif
|
|
|
|
return p - dst;
|
2005-03-21 13:22:14 +08:00
|
|
|
}
|