From fea29d5564a180c3b3762f6c4f587573eeeb118d Mon Sep 17 00:00:00 2001 From: deraadt Date: Sun, 20 Jul 2014 03:24:10 +0000 Subject: [PATCH] Demonstrate how new linux getrandom() will be called, at least until it shows up in libraries. Even the system call is probably not finalized. Bit dissapointed it has turned out to be a descriptor-less read() with EINVAL and EINTR error conditions, but we can work with it. --- lib/libcrypto/arc4random/getentropy_linux.c | 38 ++++++++++++++++++++- lib/libcrypto/crypto/getentropy_linux.c | 38 ++++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/lib/libcrypto/arc4random/getentropy_linux.c b/lib/libcrypto/arc4random/getentropy_linux.c index 04f21e147bb..2ad844624f1 100644 --- a/lib/libcrypto/arc4random/getentropy_linux.c +++ b/lib/libcrypto/arc4random/getentropy_linux.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getentropy_linux.c,v 1.27 2014/07/19 16:12:00 deraadt Exp $ */ +/* $OpenBSD: getentropy_linux.c,v 1.28 2014/07/20 03:24:10 deraadt Exp $ */ /* * Copyright (c) 2014 Theo de Raadt @@ -74,6 +74,7 @@ int getentropy(void *buf, size_t len); static int gotdata(char *buf, size_t len); +static int getentropy_getrandom(void *buf, size_t len); static int getentropy_urandom(void *buf, size_t len); #ifdef CTL_MAXNAME static int getentropy_sysctl(void *buf, size_t len); @@ -91,6 +92,13 @@ getentropy(void *buf, size_t len) return -1; } + /* + * Try descriptor-less getrandom() + */ + ret = getentropy_getrandom(buf, len); + if (ret != -1) + return (ret); + /* * Try to get entropy with /dev/urandom * @@ -176,6 +184,34 @@ gotdata(char *buf, size_t len) return 0; } +static int +getentropy_getrandom(void *buf, size_t len) +{ +#if 0 + +/* Hand-definitions until the API becomes commonplace */ +#ifndef SYS__getrandom +#ifdef __LP64__ +#define SYS__getrandom 317 +#else +#define SYS__getrandom 354 +#endif +#endif + struct __getrandom_args args = { + .buf = buf; + .len = len; + .flags = 0; + }; + + if (len > 256) + return (-1); + ret = syscall(SYS__getrandom, &args); + if (ret == len) + return (0); +#endif + return -1; +} + static int getentropy_urandom(void *buf, size_t len) { diff --git a/lib/libcrypto/crypto/getentropy_linux.c b/lib/libcrypto/crypto/getentropy_linux.c index 04f21e147bb..2ad844624f1 100644 --- a/lib/libcrypto/crypto/getentropy_linux.c +++ b/lib/libcrypto/crypto/getentropy_linux.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getentropy_linux.c,v 1.27 2014/07/19 16:12:00 deraadt Exp $ */ +/* $OpenBSD: getentropy_linux.c,v 1.28 2014/07/20 03:24:10 deraadt Exp $ */ /* * Copyright (c) 2014 Theo de Raadt @@ -74,6 +74,7 @@ int getentropy(void *buf, size_t len); static int gotdata(char *buf, size_t len); +static int getentropy_getrandom(void *buf, size_t len); static int getentropy_urandom(void *buf, size_t len); #ifdef CTL_MAXNAME static int getentropy_sysctl(void *buf, size_t len); @@ -91,6 +92,13 @@ getentropy(void *buf, size_t len) return -1; } + /* + * Try descriptor-less getrandom() + */ + ret = getentropy_getrandom(buf, len); + if (ret != -1) + return (ret); + /* * Try to get entropy with /dev/urandom * @@ -176,6 +184,34 @@ gotdata(char *buf, size_t len) return 0; } +static int +getentropy_getrandom(void *buf, size_t len) +{ +#if 0 + +/* Hand-definitions until the API becomes commonplace */ +#ifndef SYS__getrandom +#ifdef __LP64__ +#define SYS__getrandom 317 +#else +#define SYS__getrandom 354 +#endif +#endif + struct __getrandom_args args = { + .buf = buf; + .len = len; + .flags = 0; + }; + + if (len > 256) + return (-1); + ret = syscall(SYS__getrandom, &args); + if (ret == len) + return (0); +#endif + return -1; +} + static int getentropy_urandom(void *buf, size_t len) { -- 2.20.1