From 51e8f079574389d5b76296198de3df4a7815d299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Sat, 20 Jan 2007 20:49:28 +0000 Subject: [PATCH] Implement RAND_load_file and RAND_write_file. Select UNIX random device if available, otherwise use Fortuna. git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@20001 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/des/rand.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 6 deletions(-) diff --git a/lib/des/rand.c b/lib/des/rand.c index 97d9911da..18dc914f6 100644 --- a/lib/des/rand.c +++ b/lib/des/rand.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006 Kungliga Tekniska Högskolan + * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. * @@ -44,43 +44,64 @@ RCSID("$Id$"); #include -extern RAND_METHOD hc_rand_fortuna_method; -extern RAND_METHOD hc_rand_unix_method; -static const RAND_METHOD *selected_meth = &hc_rand_fortuna_method; +#ifndef O_BINARY +#define O_BINARY 0 +#endif + + +const static RAND_METHOD *selected_meth = NULL; + +static void +init_method(void) +{ + if (selected_meth != NULL) + return; + + if (_hc_rand_unix_status() == 1) + selected_meth = &hc_rand_unix_method; + else + selected_meth = &hc_rand_fortuna_method; +} void RAND_seed(const void *indata, size_t size) { + init_method(); (*selected_meth->seed)(indata, size); } int RAND_bytes(void *outdata, size_t size) { + init_method(); return (*selected_meth->bytes)(outdata, size); } void RAND_cleanup(void) { + init_method(); (*selected_meth->cleanup)(); } void RAND_add(const void *indata, size_t size, double entropi) { + init_method(); (*selected_meth->add)(indata, size, entropi); } int RAND_pseudo_bytes(void *outdata, size_t size) { + init_method(); return (*selected_meth->pseudorand)(outdata, size); } int RAND_status(void) { + init_method(); return (*selected_meth->status)(); } @@ -103,16 +124,59 @@ RAND_set_rand_engine(ENGINE *engine) return 1; } +#define RAND_FILE_SIZE 1024 + int RAND_load_file(const char *filename, size_t size) { - return 1; + unsigned char buf[128]; + size_t len; + ssize_t slen; + int fd; + + fd = open(filename, O_RDONLY | O_BINARY, 0600); + if (fd < 0) + return 0; + + len = 0; + while(len < size) { + slen = read(fd, buf, sizeof(buf)); + if (slen <= 0) + break; + RAND_seed(buf, slen); + len += slen; + } + close(fd); + + return len ? 1 : 0; } int RAND_write_file(const char *filename) { - return 1; + unsigned char buf[128]; + size_t len; + int res, fd; + + fd = open(filename, O_WRONLY | O_CREAT | O_BINARY | O_TRUNC, 0600); + if (fd < 0) + return 0; + + len = 0; + while(len < RAND_FILE_SIZE) { + res = RAND_bytes(buf, sizeof(buf)); + if (res != 1) + break; + if (write(fd, buf, sizeof(buf)) != sizeof(buf)) { + res = 0; + break; + } + len += sizeof(buf); + } + + close(fd); + + return res; } int