123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- #include "includes.h"
- #include "buffer.h"
- #include "dbutil.h"
- #include "bignum.h"
- #include "dbrandom.h"
- #include "runopts.h"
- static uint32_t counter = 0;
- #define MAX_COUNTER (1<<30)
- static unsigned char hashpool[SHA256_HASH_SIZE] = {0};
- static int donerandinit = 0;
- #define INIT_SEED_SIZE 32
- static int
- process_file(hash_state *hs, const char *filename,
- unsigned int wantlen, int prngd) {
- int readfd = -1;
- unsigned int readcount;
- int ret = DROPBEAR_FAILURE;
- if (prngd) {
- #if DROPBEAR_USE_PRNGD
- readfd = connect_unix(filename);
- #endif
- } else {
- readfd = open(filename, O_RDONLY);
- }
- if (readfd < 0) {
- goto out;
- }
- readcount = 0;
- while (wantlen == 0 || readcount < wantlen) {
- int readlen, wantread;
- unsigned char readbuf[4096];
- if (wantlen == 0) {
- wantread = sizeof(readbuf);
- } else {
- wantread = MIN(sizeof(readbuf), wantlen-readcount);
- }
- #if DROPBEAR_USE_PRNGD
- if (prngd) {
- char egdcmd[2];
- egdcmd[0] = 0x02;
- egdcmd[1] = (unsigned char)wantread;
- if (write(readfd, egdcmd, 2) < 0) {
- dropbear_exit("Can't send command to egd");
- }
- }
- #endif
- readlen = read(readfd, readbuf, wantread);
- if (readlen <= 0) {
- if (readlen < 0 && errno == EINTR) {
- continue;
- }
- if (readlen == 0 && wantlen == 0) {
-
- break;
- }
- goto out;
- }
- sha256_process(hs, readbuf, readlen);
- readcount += readlen;
- }
- ret = DROPBEAR_SUCCESS;
- out:
- close(readfd);
- return ret;
- }
- void addrandom(const unsigned char * buf, unsigned int len)
- {
- hash_state hs;
- #if DROPBEAR_FUZZ
- if (fuzz.fuzzing) {
- return;
- }
- #endif
-
- sha256_init(&hs);
-
- sha256_process(&hs, (void*)hashpool, sizeof(hashpool));
-
- sha256_process(&hs, buf, len);
- sha256_done(&hs, hashpool);
- }
- static void write_urandom()
- {
- #if DROPBEAR_FUZZ
- if (fuzz.fuzzing) {
- return;
- }
- #endif
- #if !DROPBEAR_USE_PRNGD
-
- unsigned char buf[INIT_SEED_SIZE];
- FILE *f = fopen(DROPBEAR_URANDOM_DEV, "w");
- if (!f) {
- return;
- }
- genrandom(buf, sizeof(buf));
- fwrite(buf, sizeof(buf), 1, f);
- fclose(f);
- #endif
- }
- #if DROPBEAR_FUZZ
- void fuzz_seed(const unsigned char* dat, unsigned int len) {
- hash_state hs;
- sha256_init(&hs);
- sha256_process(&hs, "fuzzfuzzfuzz", strlen("fuzzfuzzfuzz"));
- sha256_process(&hs, dat, len);
- sha256_done(&hs, hashpool);
- counter = 0;
- donerandinit = 1;
- }
- #endif
- #ifdef HAVE_GETRANDOM
- static int process_getrandom(hash_state *hs) {
- char buf[INIT_SEED_SIZE];
- ssize_t ret;
-
- ret = getrandom(buf, sizeof(buf), GRND_NONBLOCK);
- if (ret == -1) {
- if (errno == ENOSYS) {
-
- return DROPBEAR_FAILURE;
- }
-
- TRACE(("first getrandom() failed: %d %s", errno, strerror(errno)))
- if (errno == EAGAIN) {
- dropbear_log(LOG_WARNING, "Waiting for kernel randomness to be initialised...");
- }
- }
-
- while (ret != sizeof(buf)) {
- ret = getrandom(buf, sizeof(buf), 0);
- if (ret == sizeof(buf)) {
-
- break;
- }
- if (ret == -1 && errno == EINTR) {
-
- continue;
- }
- if (ret >= 0) {
- TRACE(("Short read %zd from getrandom() shouldn't happen", ret))
-
- continue;
- }
-
- TRACE(("2nd getrandom() failed: %d %s", errno, strerror(errno)))
- break;
- }
- if (ret == sizeof(buf)) {
-
- sha256_process(hs, (void*)buf, sizeof(buf));
- return DROPBEAR_SUCCESS;
- }
- return DROPBEAR_FAILURE;
- }
- #endif
- void seedrandom() {
- hash_state hs;
- pid_t pid;
- struct timeval tv;
- clock_t clockval;
- int urandom_seeded = 0;
- #if DROPBEAR_FUZZ
- if (fuzz.fuzzing) {
- return;
- }
- #endif
-
- sha256_init(&hs);
-
- sha256_process(&hs, (void*)hashpool, sizeof(hashpool));
- #ifdef HAVE_GETRANDOM
- if (process_getrandom(&hs) == DROPBEAR_SUCCESS) {
- urandom_seeded = 1;
- }
- #endif
- if (!urandom_seeded) {
- #if DROPBEAR_USE_PRNGD
- if (process_file(&hs, DROPBEAR_PRNGD_SOCKET, INIT_SEED_SIZE, 1)
- != DROPBEAR_SUCCESS) {
- dropbear_exit("Failure reading random device %s",
- DROPBEAR_PRNGD_SOCKET);
- urandom_seeded = 1;
- }
- #else
-
- if (process_file(&hs, DROPBEAR_URANDOM_DEV, INIT_SEED_SIZE, 0)
- != DROPBEAR_SUCCESS) {
- dropbear_exit("Failure reading random device %s",
- DROPBEAR_URANDOM_DEV);
- urandom_seeded = 1;
- }
- #endif
- }
-
- #ifdef __linux__
-
- process_file(&hs, "/proc/timer_list", 0, 0);
-
- process_file(&hs, "/proc/interrupts", 0, 0);
- process_file(&hs, "/proc/loadavg", 0, 0);
- process_file(&hs, "/proc/sys/kernel/random/entropy_avail", 0, 0);
-
- process_file(&hs, "/proc/net/netstat", 4096, 0);
- process_file(&hs, "/proc/net/dev", 4096, 0);
- process_file(&hs, "/proc/net/tcp", 4096, 0);
-
- process_file(&hs, "/proc/net/rt_cache", 4096, 0);
- process_file(&hs, "/proc/vmstat", 0, 0);
- #endif
- pid = getpid();
- sha256_process(&hs, (void*)&pid, sizeof(pid));
-
- memset(&tv, 0x0, sizeof(tv));
- gettimeofday(&tv, NULL);
- sha256_process(&hs, (void*)&tv, sizeof(tv));
- clockval = clock();
- sha256_process(&hs, (void*)&clockval, sizeof(clockval));
-
- sha256_done(&hs, hashpool);
- counter = 0;
- donerandinit = 1;
-
- write_urandom();
- }
- void genrandom(unsigned char* buf, unsigned int len) {
- hash_state hs;
- unsigned char hash[SHA256_HASH_SIZE];
- unsigned int copylen;
- if (!donerandinit) {
- dropbear_exit("seedrandom not done");
- }
- while (len > 0) {
- sha256_init(&hs);
- sha256_process(&hs, (void*)hashpool, sizeof(hashpool));
- sha256_process(&hs, (void*)&counter, sizeof(counter));
- sha256_done(&hs, hash);
- counter++;
- if (counter > MAX_COUNTER) {
- seedrandom();
- }
- copylen = MIN(len, SHA256_HASH_SIZE);
- memcpy(buf, hash, copylen);
- len -= copylen;
- buf += copylen;
- }
- m_burn(hash, sizeof(hash));
- }
- void gen_random_mpint(mp_int *max, mp_int *rand) {
- unsigned char *randbuf = NULL;
- unsigned int len = 0;
- const unsigned char masks[] = {0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f};
- const int size_bits = mp_count_bits(max);
- len = size_bits / 8;
- if ((size_bits % 8) != 0) {
- len += 1;
- }
- randbuf = (unsigned char*)m_malloc(len);
- do {
- genrandom(randbuf, len);
-
- randbuf[0] &= masks[size_bits % 8];
- bytes_to_mp(rand, randbuf, len);
-
- } while (!(mp_cmp(rand, max) == MP_LT && mp_cmp_d(rand, 0) == MP_GT));
- m_burn(randbuf, len);
- m_free(randbuf);
- }
|