summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorderaadt <>2014-07-20 03:24:10 +0000
committerderaadt <>2014-07-20 03:24:10 +0000
commit62a42f016f6e7c8be86b80a527c3637a7496569f (patch)
tree9046687e16533c9e51448873939ef20cd7f9e81f
parentde31b28670bb42af2043a9bcf71bd18085538484 (diff)
downloadopenbsd-62a42f016f6e7c8be86b80a527c3637a7496569f.tar.gz
openbsd-62a42f016f6e7c8be86b80a527c3637a7496569f.tar.bz2
openbsd-62a42f016f6e7c8be86b80a527c3637a7496569f.zip
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.
-rw-r--r--src/lib/libcrypto/arc4random/getentropy_linux.c38
-rw-r--r--src/lib/libcrypto/crypto/getentropy_linux.c38
2 files changed, 74 insertions, 2 deletions
diff --git a/src/lib/libcrypto/arc4random/getentropy_linux.c b/src/lib/libcrypto/arc4random/getentropy_linux.c
index 04f21e147b..2ad844624f 100644
--- a/src/lib/libcrypto/arc4random/getentropy_linux.c
+++ b/src/lib/libcrypto/arc4random/getentropy_linux.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: getentropy_linux.c,v 1.27 2014/07/19 16:12:00 deraadt Exp $ */ 1/* $OpenBSD: getentropy_linux.c,v 1.28 2014/07/20 03:24:10 deraadt Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org> 4 * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
@@ -74,6 +74,7 @@
74int getentropy(void *buf, size_t len); 74int getentropy(void *buf, size_t len);
75 75
76static int gotdata(char *buf, size_t len); 76static int gotdata(char *buf, size_t len);
77static int getentropy_getrandom(void *buf, size_t len);
77static int getentropy_urandom(void *buf, size_t len); 78static int getentropy_urandom(void *buf, size_t len);
78#ifdef CTL_MAXNAME 79#ifdef CTL_MAXNAME
79static int getentropy_sysctl(void *buf, size_t len); 80static int getentropy_sysctl(void *buf, size_t len);
@@ -92,6 +93,13 @@ getentropy(void *buf, size_t len)
92 } 93 }
93 94
94 /* 95 /*
96 * Try descriptor-less getrandom()
97 */
98 ret = getentropy_getrandom(buf, len);
99 if (ret != -1)
100 return (ret);
101
102 /*
95 * Try to get entropy with /dev/urandom 103 * Try to get entropy with /dev/urandom
96 * 104 *
97 * This can fail if the process is inside a chroot or if file 105 * This can fail if the process is inside a chroot or if file
@@ -177,6 +185,34 @@ gotdata(char *buf, size_t len)
177} 185}
178 186
179static int 187static int
188getentropy_getrandom(void *buf, size_t len)
189{
190#if 0
191
192/* Hand-definitions until the API becomes commonplace */
193#ifndef SYS__getrandom
194#ifdef __LP64__
195#define SYS__getrandom 317
196#else
197#define SYS__getrandom 354
198#endif
199#endif
200 struct __getrandom_args args = {
201 .buf = buf;
202 .len = len;
203 .flags = 0;
204 };
205
206 if (len > 256)
207 return (-1);
208 ret = syscall(SYS__getrandom, &args);
209 if (ret == len)
210 return (0);
211#endif
212 return -1;
213}
214
215static int
180getentropy_urandom(void *buf, size_t len) 216getentropy_urandom(void *buf, size_t len)
181{ 217{
182 struct stat st; 218 struct stat st;
diff --git a/src/lib/libcrypto/crypto/getentropy_linux.c b/src/lib/libcrypto/crypto/getentropy_linux.c
index 04f21e147b..2ad844624f 100644
--- a/src/lib/libcrypto/crypto/getentropy_linux.c
+++ b/src/lib/libcrypto/crypto/getentropy_linux.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: getentropy_linux.c,v 1.27 2014/07/19 16:12:00 deraadt Exp $ */ 1/* $OpenBSD: getentropy_linux.c,v 1.28 2014/07/20 03:24:10 deraadt Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org> 4 * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
@@ -74,6 +74,7 @@
74int getentropy(void *buf, size_t len); 74int getentropy(void *buf, size_t len);
75 75
76static int gotdata(char *buf, size_t len); 76static int gotdata(char *buf, size_t len);
77static int getentropy_getrandom(void *buf, size_t len);
77static int getentropy_urandom(void *buf, size_t len); 78static int getentropy_urandom(void *buf, size_t len);
78#ifdef CTL_MAXNAME 79#ifdef CTL_MAXNAME
79static int getentropy_sysctl(void *buf, size_t len); 80static int getentropy_sysctl(void *buf, size_t len);
@@ -92,6 +93,13 @@ getentropy(void *buf, size_t len)
92 } 93 }
93 94
94 /* 95 /*
96 * Try descriptor-less getrandom()
97 */
98 ret = getentropy_getrandom(buf, len);
99 if (ret != -1)
100 return (ret);
101
102 /*
95 * Try to get entropy with /dev/urandom 103 * Try to get entropy with /dev/urandom
96 * 104 *
97 * This can fail if the process is inside a chroot or if file 105 * This can fail if the process is inside a chroot or if file
@@ -177,6 +185,34 @@ gotdata(char *buf, size_t len)
177} 185}
178 186
179static int 187static int
188getentropy_getrandom(void *buf, size_t len)
189{
190#if 0
191
192/* Hand-definitions until the API becomes commonplace */
193#ifndef SYS__getrandom
194#ifdef __LP64__
195#define SYS__getrandom 317
196#else
197#define SYS__getrandom 354
198#endif
199#endif
200 struct __getrandom_args args = {
201 .buf = buf;
202 .len = len;
203 .flags = 0;
204 };
205
206 if (len > 256)
207 return (-1);
208 ret = syscall(SYS__getrandom, &args);
209 if (ret == len)
210 return (0);
211#endif
212 return -1;
213}
214
215static int
180getentropy_urandom(void *buf, size_t len) 216getentropy_urandom(void *buf, size_t len)
181{ 217{
182 struct stat st; 218 struct stat st;