comparison compat/getentropy.c @ 0:d541e748cfd8

Initial revision
author Guido Berhoerster <guido+libpws@berhoerster.name>
date Tue, 10 Feb 2015 11:29:54 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d541e748cfd8
1 /*
2 * Copyright (C) 2015 Guido Berhoerster <guido+libpws@berhoerster.name>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 /* needed for syscall(2) on Linux */
25 #define _GNU_SOURCE
26
27 /* Linux >= 3.17 has getrandom(2) system call */
28 #ifdef __linux__
29 #include <unistd.h>
30 #include <sys/syscall.h>
31 #include <linux/random.h>
32 #ifdef SYS_getrandom
33 #define HAVE_GETRANDOM
34 #endif /* SYS_getrandom */
35 #endif /* __linux__ */
36 /*
37 * on unknown Unix systems without getentropy(2) or Linux without getrandom(2)
38 * fall back to * reading from /dev/(u)random
39 */
40 #ifndef HAVE_GETRANDOM
41 #include <stdio.h>
42 #ifndef RANDOM_DEVICE
43 #ifdef __linux__
44 /* on Linux /dev/urandom should be good enough */
45 #define RANDOM_DEVICE "/dev/urandom"
46 #else /* __linux__ */
47 /* on unknown Unix systems use the possibly blocking /dev/random */
48 #define RANDOM_DEVICE "/dev/random"
49 #endif /* __linux__ */
50 #endif /* !RANDOM_DEVICE */
51 #endif /* !HAVE_GETRANDOM */
52 #include <errno.h>
53
54 #include "pws-compat.h"
55
56 #ifdef HAVE_GETRANDOM
57 static int
58 getentropy_linux_getrandom(void *buf, size_t buf_len)
59 {
60 int retval;
61
62 retval = syscall(SYS_getrandom, buf, buf_len, 0);
63 if (retval < 0) {
64 return (-1);
65 } else if ((size_t)retval != buf_len) {
66 errno = EIO;
67 return (-1);
68 }
69
70 return (0);
71 }
72 #else
73 static int
74 getentropy_dev_random(void *buf, size_t buf_len)
75 {
76 FILE *fp;
77 int saved_errno;
78
79 fp = fopen(RANDOM_DEVICE, "r");
80 if (fp == NULL) {
81 return (-1);
82 }
83 if (fread(buf, 1, buf_len, fp) != buf_len) {
84 saved_errno = errno;
85 fclose(fp);
86 errno = saved_errno;
87 return (-1);
88 }
89 if (fclose(fp) != 0) {
90 return (-1);
91 }
92
93 return (0);
94 }
95 #endif /* HAVE_GETRANDOM */
96
97 int
98 getentropy(void *buf, size_t buf_len)
99 {
100 if (buf_len > 256) {
101 errno = EIO;
102 return (-1);
103 }
104
105 return (
106 #ifdef HAVE_GETRANDOM
107 getentropy_linux_getrandom(
108 #else /* HAVE_GETRANDOM */
109 getentropy_dev_random(
110 #endif /* HAVE_GETRANDOM */
111 buf, buf_len));
112 }