From b6fcb7035d780aed38b4b33354cdecda80c44d26 Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Fri, 2 Sep 2016 17:38:42 +0300 Subject: [PATCH] rand: replace time() with time_stamp() as entropy source time_stamp() has bigger precision. --- src/rand.c | 10 ++++------ tests/test_rand.c | 3 +++ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/rand.c b/src/rand.c index fbdfce4..2b889ea 100644 --- a/src/rand.c +++ b/src/rand.c @@ -212,9 +212,7 @@ static int Hash_DRBG_Generate(Hash_DRBG_CTX *ctx, uint8_t *output, #define ENTROPY_ACCUMULATE(ptr, last, type, arg) \ do { \ type __arg = (type)(arg); \ - if ((char*)ptr + sizeof(__arg) < (char*)last && \ - __arg != (type)-1) \ - { \ + if ((char*)ptr + sizeof(__arg) < (char*)last) { \ *(type*)ptr = __arg; \ ptr = (void*)((char*)ptr + sizeof(__arg)); \ } \ @@ -228,8 +226,8 @@ static void xmpp_rand_reseed(xmpp_rand_t *rand) size_t len; /* entropy: - * 1. time(2) - * 2. clock(3) if != -1 + * 1. time_stamp() + * 2. clock(3) * 3. xmpp_rand_t address to make unique seed within one process * 4. counter to make unique seed within one context * 5. stack address @@ -240,7 +238,7 @@ static void xmpp_rand_reseed(xmpp_rand_t *rand) * XXX 6 and 7 are not implemented yet. */ - ENTROPY_ACCUMULATE(ptr, last, time_t, time(NULL)); + ENTROPY_ACCUMULATE(ptr, last, uint64_t, time_stamp()); ENTROPY_ACCUMULATE(ptr, last, clock_t, clock()); ENTROPY_ACCUMULATE(ptr, last, void *, rand); ENTROPY_ACCUMULATE(ptr, last, unsigned, ++rand->reseed_count); diff --git a/tests/test_rand.c b/tests/test_rand.c index 04ff29a..323301b 100644 --- a/tests/test_rand.c +++ b/tests/test_rand.c @@ -28,6 +28,9 @@ void xmpp_free(const xmpp_ctx_t * const ctx, void *p) { } int xmpp_snprintf (char *str, size_t count, const char *fmt, ...) { return 0; } +uint64_t time_stamp(void) { + return 0; +} static struct { const char *entropy_input;