From 7cad2aea777893dbacb8ae00aeeff81b319cb7d1 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 29 Mar 2002 16:34:20 +0000 Subject: [PATCH] Added a library module for generation of cryptographically secure random numbers. --- lib/Makefile | 2 +- lib/lib.h | 4 ++++ lib/randomkey.c | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 lib/randomkey.c diff --git a/lib/Makefile b/lib/Makefile index 9cf5c53b..2ccb3d0a 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -7,7 +7,7 @@ SHLIB_OBJS=alloc.o alloc_str.o ctmatch.o db.o fastbuf.o fb-file.o fb-mem.o lists log.o log2.o md5.o md5hex.o mmap.o pagecache.o patimatch.o patmatch.o pool.o \ prime.o random.o realloc.o regex.o timer.o url.o wildmatch.o \ wordsplit.o str_ctype.o str_upper.o bucket.o conf.o object.o sorter.o \ - finger.o proctitle.o ipaccess.o profile.o bitsig.o + finger.o proctitle.o ipaccess.o profile.o bitsig.o randomkey.o obj/lib/libsh.a: $(addprefix obj/lib/,$(SHLIB_OBJS)) diff --git a/lib/lib.h b/lib/lib.h index 22169423..dea7c10c 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -176,4 +176,8 @@ void munmap_file(void *start, unsigned len); void setproctitle_init(int argc, char **argv); void setproctitle(char *msg, ...) __attribute__((format(printf,1,2))); +/* randomkey.c */ + +void randomkey(byte *buf, uns size); + #endif diff --git a/lib/randomkey.c b/lib/randomkey.c new file mode 100644 index 00000000..03cba197 --- /dev/null +++ b/lib/randomkey.c @@ -0,0 +1,22 @@ +/* + * Sherlock Library -- Cryptographically Safe Random Key Generator + * + * (c) 2002 Martin Mares + */ + +#include "lib/lib.h" + +#include +#include + +void +randomkey(byte *buf, uns size) +{ + int fd; + + if ((fd = open("/dev/urandom", O_RDONLY, 0)) < 0) + die("Unable to open /dev/urandom: %m"); + if (read(fd, buf, size) != (int) size) + die("Error reading /dev/urandom: %m"); + close(fd); +} -- 2.39.2