Skip to content

Commit

Permalink
Add getentropy(2) randomness source
Browse files Browse the repository at this point in the history
  • Loading branch information
kpcyrd committed Sep 2, 2015
1 parent 00a90f0 commit 73041a2
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
64 changes: 64 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include "crypto/random/seed/GetEntropyRandomSeed.h"
#include "util/Identity.h"
#include "util/Bits.h"

#include <unistd.h>
#include <errno.h>
#include <sys/syscall.h>

#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)"
}));
}
31 changes: 31 additions & 0 deletions 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 <http://www.gnu.org/licenses/>.
*/
#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 <sys/syscall.h>

#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
1 change: 1 addition & 0 deletions crypto/random/seed/SystemRandomSeed.c
Expand Up @@ -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)))
Expand Down

0 comments on commit 73041a2

Please sign in to comment.