2016-05-17 18:52:22 +00:00
|
|
|
/*
|
2017-07-18 13:39:21 +00:00
|
|
|
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
2001-01-08 10:59:26 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2001-01-08 10:59:26 +00:00
|
|
|
*/
|
2016-05-17 18:52:22 +00:00
|
|
|
|
2002-06-20 17:32:04 +00:00
|
|
|
#include "e_os.h"
|
2017-08-23 23:05:07 +00:00
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2001-01-08 10:59:26 +00:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include "rand_lcl.h"
|
2017-07-18 13:39:21 +00:00
|
|
|
#include <stdio.h>
|
2001-01-08 10:59:26 +00:00
|
|
|
|
2017-08-31 21:16:22 +00:00
|
|
|
#ifdef OPENSSL_RAND_SEED_GETRANDOM
|
|
|
|
# include <linux/random.h>
|
|
|
|
#endif
|
2001-01-08 10:59:26 +00:00
|
|
|
|
2017-08-31 21:16:22 +00:00
|
|
|
#if (defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)) && \
|
2017-07-18 13:39:21 +00:00
|
|
|
!defined(OPENSSL_RAND_SEED_NONE)
|
2017-08-31 21:16:22 +00:00
|
|
|
# error "UEFI and VXWorks only support seeding NONE"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
|
|
|
|
|| defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) \
|
|
|
|
|| defined(OPENSSL_SYS_UEFI))
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
# if defined(OPENSSL_SYS_VOS)
|
|
|
|
|
2017-07-18 13:39:21 +00:00
|
|
|
# ifndef OPENSSL_RAND_SEED_OS
|
|
|
|
# error "Unsupported seeding method configured; must be os"
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
|
|
|
|
# error "Unsupported HP-PA and IA32 at the same time."
|
|
|
|
# endif
|
|
|
|
# if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
|
|
|
|
# error "Must have one of HP-PA or IA32"
|
|
|
|
# endif
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* The following algorithm repeatedly samples the real-time clock (RTC) to
|
|
|
|
* generate a sequence of unpredictable data. The algorithm relies upon the
|
|
|
|
* uneven execution speed of the code (due to factors such as cache misses,
|
|
|
|
* interrupts, bus activity, and scheduling) and upon the rather large
|
|
|
|
* relative difference between the speed of the clock and the rate at which
|
2017-08-03 13:23:28 +00:00
|
|
|
* it can be read. If it is ported to an environment where execution speed
|
|
|
|
* is more constant or where the RTC ticks at a much slower rate, or the
|
|
|
|
* clock can be read with fewer instructions, it is likely that the results
|
|
|
|
* would be far more predictable. This should only be used for legacy
|
|
|
|
* platforms.
|
2015-01-22 03:40:55 +00:00
|
|
|
*
|
2017-08-31 21:16:22 +00:00
|
|
|
* As a precaution, we assume only 2 bits of entropy per byte.
|
2015-01-22 03:40:55 +00:00
|
|
|
*/
|
2017-08-31 21:16:22 +00:00
|
|
|
size_t RAND_POOL_acquire_entropy(RAND_POOL *pool)
|
2009-04-07 16:33:26 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
short int code;
|
|
|
|
gid_t curr_gid;
|
|
|
|
pid_t curr_pid;
|
|
|
|
uid_t curr_uid;
|
|
|
|
int i, k;
|
2017-08-31 21:16:22 +00:00
|
|
|
size_t bytes_needed;
|
2015-01-22 03:40:55 +00:00
|
|
|
struct timespec ts;
|
|
|
|
unsigned char v;
|
|
|
|
# ifdef OPENSSL_SYS_VOS_HPPA
|
|
|
|
long duration;
|
|
|
|
extern void s$sleep(long *_duration, short int *_code);
|
|
|
|
# else
|
|
|
|
long long duration;
|
|
|
|
extern void s$sleep2(long long *_duration, short int *_code);
|
2017-07-18 13:39:21 +00:00
|
|
|
# endif
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Seed with the gid, pid, and uid, to ensure *some* variation between
|
|
|
|
* different processes.
|
|
|
|
*/
|
|
|
|
curr_gid = getgid();
|
2017-08-31 21:16:22 +00:00
|
|
|
RAND_POOL_add(pool, &curr_gid, sizeof(curr_gid), 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
curr_pid = getpid();
|
2017-08-31 21:16:22 +00:00
|
|
|
RAND_POOL_add(pool, &curr_pid, sizeof(curr_pid), 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
curr_uid = getuid();
|
2017-08-31 21:16:22 +00:00
|
|
|
RAND_POOL_add(pool, &curr_uid, sizeof(curr_uid), 0);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-08-31 21:16:22 +00:00
|
|
|
bytes_needed = RAND_POOL_bytes_needed(pool, 2 /*entropy_per_byte*/);
|
|
|
|
|
|
|
|
for (i = 0; i < bytes_needed; i++) {
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* burn some cpu; hope for interrupts, cache collisions, bus
|
|
|
|
* interference, etc.
|
|
|
|
*/
|
|
|
|
for (k = 0; k < 99; k++)
|
|
|
|
ts.tv_nsec = random();
|
|
|
|
|
|
|
|
# ifdef OPENSSL_SYS_VOS_HPPA
|
|
|
|
/* sleep for 1/1024 of a second (976 us). */
|
|
|
|
duration = 1;
|
|
|
|
s$sleep(&duration, &code);
|
|
|
|
# else
|
|
|
|
/* sleep for 1/65536 of a second (15 us). */
|
|
|
|
duration = 1;
|
|
|
|
s$sleep2(&duration, &code);
|
2017-07-18 13:39:21 +00:00
|
|
|
# endif
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-07-18 13:39:21 +00:00
|
|
|
/* Get wall clock time, take 8 bits. */
|
2015-01-22 03:40:55 +00:00
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
2017-07-18 13:39:21 +00:00
|
|
|
v = (unsigned char)(ts.tv_nsec & 0xFF);
|
2017-08-31 21:16:22 +00:00
|
|
|
RAND_POOL_add(pool, arg, &v, sizeof(v) , 2);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2017-08-31 21:16:22 +00:00
|
|
|
return RAND_POOL_entropy_available(pool);
|
2009-04-07 16:33:26 +00:00
|
|
|
}
|
2017-07-18 13:39:21 +00:00
|
|
|
|
2017-06-14 17:53:01 +00:00
|
|
|
# else
|
2017-07-18 13:39:21 +00:00
|
|
|
|
|
|
|
# if defined(OPENSSL_RAND_SEED_EGD) && \
|
|
|
|
(defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
|
|
|
|
# error "Seeding uses EGD but EGD is turned off or no device given"
|
2015-01-22 03:40:55 +00:00
|
|
|
# endif
|
|
|
|
|
2017-07-18 13:39:21 +00:00
|
|
|
# if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
|
|
|
|
# error "Seeding uses urandom but DEVRANDOM is not configured"
|
|
|
|
# endif
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-07-18 13:39:21 +00:00
|
|
|
# if defined(OPENSSL_RAND_SEED_OS)
|
|
|
|
# if defined(DEVRANDOM)
|
|
|
|
# define OPENSSL_RAND_SEED_DEVRANDOM
|
|
|
|
# else
|
|
|
|
# error "OS seeding requires DEVRANDOM to be configured"
|
2015-01-22 03:40:55 +00:00
|
|
|
# endif
|
2017-07-18 13:39:21 +00:00
|
|
|
# endif
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-07-18 13:39:21 +00:00
|
|
|
# if defined(OPENSSL_RAND_SEED_LIBRANDOM)
|
|
|
|
# error "librandom not (yet) supported"
|
|
|
|
# endif
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-08-03 13:23:28 +00:00
|
|
|
/*
|
2017-08-31 21:16:22 +00:00
|
|
|
* Try the various seeding methods in turn, exit when successful.
|
|
|
|
*
|
|
|
|
* TODO(DRBG): If more than one entropy source is available, is it
|
|
|
|
* preferable to stop as soon as enough entropy has been collected
|
|
|
|
* (as favored by @rsalz) or should one rather be defensive and add
|
|
|
|
* more entropy than requested and/or from different sources?
|
|
|
|
*
|
|
|
|
* Currently, the user can select multiple entropy sources in the
|
|
|
|
* configure step, yet in practice only the first available source
|
|
|
|
* will be used. A more flexible solution has been requested, but
|
|
|
|
* currently it is not clear how this can be achieved without
|
|
|
|
* overengineering the problem. There are many parameters which
|
|
|
|
* could be taken into account when selecting the order and amount
|
|
|
|
* of input from the different entropy sources (trust, quality,
|
|
|
|
* possibility of blocking).
|
2017-08-03 13:23:28 +00:00
|
|
|
*/
|
2017-08-31 21:16:22 +00:00
|
|
|
size_t RAND_POOL_acquire_entropy(RAND_POOL *pool)
|
2017-07-18 13:39:21 +00:00
|
|
|
{
|
|
|
|
# ifdef OPENSSL_RAND_SEED_NONE
|
2017-08-31 21:16:22 +00:00
|
|
|
return RAND_POOL_entropy_available(pool);
|
2017-07-18 13:39:21 +00:00
|
|
|
# else
|
2017-08-31 21:16:22 +00:00
|
|
|
size_t bytes_needed;
|
|
|
|
size_t entropy_available = 0;
|
|
|
|
unsigned char *buffer;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-08-03 13:23:28 +00:00
|
|
|
# ifdef OPENSSL_RAND_SEED_GETRANDOM
|
2017-08-31 21:16:22 +00:00
|
|
|
bytes_needed = RAND_POOL_bytes_needed(pool, 8 /*entropy_per_byte*/);
|
|
|
|
buffer = RAND_POOL_add_begin(pool, bytes_needed);
|
|
|
|
if (buffer != NULL) {
|
|
|
|
size_t bytes = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-08-31 21:16:22 +00:00
|
|
|
if (getrandom(buffer, bytes_needed, 0) == (int)bytes_needed)
|
|
|
|
bytes = bytes_needed;
|
|
|
|
|
|
|
|
entropy_available = RAND_POOL_add_end(pool, bytes, 8 * bytes);
|
2017-08-03 13:23:28 +00:00
|
|
|
}
|
2017-08-31 21:16:22 +00:00
|
|
|
if (entropy_available > 0)
|
|
|
|
return entropy_available;
|
2015-01-22 03:40:55 +00:00
|
|
|
# endif
|
|
|
|
|
2017-08-03 13:23:28 +00:00
|
|
|
# if defined(OPENSSL_RAND_SEED_LIBRANDOM)
|
2017-07-18 13:39:21 +00:00
|
|
|
{
|
2017-08-03 13:23:28 +00:00
|
|
|
/* Not yet implemented. */
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2017-07-18 13:39:21 +00:00
|
|
|
# endif
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-07-18 13:39:21 +00:00
|
|
|
# ifdef OPENSSL_RAND_SEED_DEVRANDOM
|
2017-08-31 21:16:22 +00:00
|
|
|
bytes_needed = RAND_POOL_bytes_needed(pool, 8 /*entropy_per_byte*/);
|
|
|
|
if (bytes_needed > 0) {
|
2017-07-18 13:39:21 +00:00
|
|
|
static const char *paths[] = { DEVRANDOM, NULL };
|
|
|
|
FILE *fp;
|
|
|
|
int i;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-07-18 13:39:21 +00:00
|
|
|
for (i = 0; paths[i] != NULL; i++) {
|
|
|
|
if ((fp = fopen(paths[i], "rb")) == NULL)
|
|
|
|
continue;
|
|
|
|
setbuf(fp, NULL);
|
2017-08-31 21:16:22 +00:00
|
|
|
buffer = RAND_POOL_add_begin(pool, bytes_needed);
|
|
|
|
if (buffer != NULL) {
|
|
|
|
size_t bytes = 0;
|
|
|
|
if (fread(buffer, 1, bytes_needed, fp) == bytes_needed)
|
|
|
|
bytes = bytes_needed;
|
|
|
|
|
|
|
|
entropy_available = RAND_POOL_add_end(pool, bytes, 8 * bytes);
|
2017-07-18 13:39:21 +00:00
|
|
|
}
|
2017-08-03 13:23:28 +00:00
|
|
|
fclose(fp);
|
2017-08-31 21:16:22 +00:00
|
|
|
if (entropy_available > 0)
|
|
|
|
return entropy_available;
|
|
|
|
|
|
|
|
bytes_needed = RAND_POOL_bytes_needed(pool, 8 /*entropy_per_byte*/);
|
2017-07-18 13:39:21 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2017-07-18 13:39:21 +00:00
|
|
|
# endif
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-08-03 13:23:28 +00:00
|
|
|
# ifdef OPENSSL_RAND_SEED_RDTSC
|
2017-08-31 21:16:22 +00:00
|
|
|
entropy_available = rand_acquire_entropy_from_tsc(pool);
|
|
|
|
if (entropy_available > 0)
|
|
|
|
return entropy_available;
|
2017-08-03 13:23:28 +00:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef OPENSSL_RAND_SEED_RDCPU
|
2017-08-31 21:16:22 +00:00
|
|
|
entropy_available = rand_acquire_entropy_from_cpu(pool);
|
|
|
|
if (entropy_available > 0)
|
|
|
|
return entropy_available;
|
2017-08-03 13:23:28 +00:00
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef OPENSSL_RAND_SEED_EGD
|
2017-08-31 21:16:22 +00:00
|
|
|
bytes_needed = RAND_POOL_bytes_needed(pool, 8 /*entropy_per_byte*/);
|
|
|
|
if (bytes_needed > 0) {
|
2017-08-03 13:23:28 +00:00
|
|
|
static const char *paths[] = { DEVRANDOM_EGD, NULL };
|
|
|
|
int i;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-08-03 13:23:28 +00:00
|
|
|
for (i = 0; paths[i] != NULL; i++) {
|
2017-08-31 21:16:22 +00:00
|
|
|
buffer = RAND_POOL_add_begin(pool, bytes_needed);
|
|
|
|
if (buffer != NULL) {
|
|
|
|
size_t bytes = 0;
|
|
|
|
int num = RAND_query_egd_bytes(paths[i],
|
|
|
|
buffer, (int)bytes_needed);
|
|
|
|
if (num == (int)bytes_needed)
|
|
|
|
bytes = bytes_needed;
|
|
|
|
|
|
|
|
entropy_available = RAND_POOL_add_end(pool, bytes, 8 * bytes);
|
2017-08-03 13:23:28 +00:00
|
|
|
}
|
2017-08-31 21:16:22 +00:00
|
|
|
if (entropy_available > 0)
|
|
|
|
return entropy_available;
|
2017-07-18 13:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2017-08-31 21:16:22 +00:00
|
|
|
return RAND_POOL_entropy_available(pool);
|
2015-01-22 03:40:55 +00:00
|
|
|
# endif
|
2001-01-08 10:59:26 +00:00
|
|
|
}
|
2017-07-18 13:39:21 +00:00
|
|
|
# endif
|
2001-01-08 10:59:26 +00:00
|
|
|
|
2002-10-14 09:44:57 +00:00
|
|
|
#endif
|