From 73041a2bba78fd2bb602eb22043ac5709ced07d3 Mon Sep 17 00:00:00 2001 From: kpcyrd Date: Wed, 2 Sep 2015 14:03:28 +0200 Subject: [PATCH] Add getentropy(2) randomness source --- crypto/random/seed/GetEntropyRandomSeed.c | 64 +++++++++++++++++++++++ crypto/random/seed/GetEntropyRandomSeed.h | 31 +++++++++++ crypto/random/seed/SystemRandomSeed.c | 1 + 3 files changed, 96 insertions(+) create mode 100644 crypto/random/seed/GetEntropyRandomSeed.c create mode 100644 crypto/random/seed/GetEntropyRandomSeed.h diff --git a/crypto/random/seed/GetEntropyRandomSeed.c b/crypto/random/seed/GetEntropyRandomSeed.c new file mode 100644 index 000000000..60bd6d799 --- /dev/null +++ b/crypto/random/seed/GetEntropyRandomSeed.c @@ -0,0 +1,64 @@ +/* vim: set expandtab ts=4 sw=4: */ +/* + * You may redistribute this program and/or modify it under the terms of + * the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#define _GNU_SOURCE +#include "crypto/random/seed/GetEntropyRandomSeed.h" +#include "util/Identity.h" +#include "util/Bits.h" + +#include +#include +#include + +#ifndef __OPENBSD__ +static int getentropy(void *buf, size_t buflen) +{ + int ret; + + if (buflen > 256) { + goto failure; + } + + ret = syscall(SYS_getrandom, buf, buflen, 0); + + if (ret < 0) { + return ret; + } + + if ((size_t)ret == buflen) { + return 0; + } + + failure: + errno = EIO; + return -1; +} +#endif + +static int get(struct RandomSeed* randomSeed, uint64_t output[8]) +{ + if (getentropy(output, 64) < 0) { + return -1; + } else { + return 0; + } +} + +struct RandomSeed* GetEntropyRandomSeed_new(struct Allocator* alloc) +{ + return Allocator_clone(alloc, (&(struct RandomSeed) { + .get = get, + .name = "getentropy(2)" + })); +} diff --git a/crypto/random/seed/GetEntropyRandomSeed.h b/crypto/random/seed/GetEntropyRandomSeed.h new file mode 100644 index 000000000..83bb378f9 --- /dev/null +++ b/crypto/random/seed/GetEntropyRandomSeed.h @@ -0,0 +1,31 @@ +/* vim: set expandtab ts=4 sw=4: */ +/* + * You may redistribute this program and/or modify it under the terms of + * the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef GetEntropyRandomSeed_H +#define GetEntropyRandomSeed_H + +#include "crypto/random/seed/RandomSeed.h" +#include "crypto/random/seed/RandomSeedProvider.h" +#include "memory/Allocator.h" +#include "util/Linker.h" + +#include + +#if defined __OPENBSD__ || defined SYS_getrandom + Linker_require("crypto/random/seed/GetEntropyRandomSeed.c") + struct RandomSeed* GetEntropyRandomSeed_new(struct Allocator* alloc); + RandomSeedProvider_register(GetEntropyRandomSeed_new) +#endif + +#endif diff --git a/crypto/random/seed/SystemRandomSeed.c b/crypto/random/seed/SystemRandomSeed.c index c8f3a5fec..76921b624 100644 --- a/crypto/random/seed/SystemRandomSeed.c +++ b/crypto/random/seed/SystemRandomSeed.c @@ -21,6 +21,7 @@ #include "crypto/random/seed/DevUrandomRandomSeed.h" #include "crypto/random/seed/LinuxRandomUuidSysctlRandomSeed.h" #include "crypto/random/seed/ProcSysKernelRandomUuidRandomSeed.h" +#include "crypto/random/seed/GetEntropyRandomSeed.h" static RandomSeed_Provider PROVIDERS[] = { RandomSeedProvider_list() }; #define PROVIDERS_COUNT ((int)(sizeof(PROVIDERS) / sizeof(RandomSeed_Provider)))