comparison compat/getentropy.c @ 0:a7e41e1a79c8

Initial revision
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Thu, 19 Jan 2017 22:39:51 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a7e41e1a79c8
1 /*
2 * Copyright (C) 2016 Guido Berhoerster <guido+pwm@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 #ifdef HAVE_GETRANDOM
55 static int
56 getentropy_linux_getrandom(void *buf, size_t buf_len)
57 {
58 int retval;
59
60 retval = syscall(SYS_getrandom, buf, buf_len, 0);
61 if (retval < 0) {
62 return (-1);
63 } else if ((size_t)retval != buf_len) {
64 errno = EIO;
65 return (-1);
66 }
67
68 return (0);
69 }
70 #else
71 static int
72 getentropy_dev_random(void *buf, size_t buf_len)
73 {
74 FILE *fp;
75 int saved_errno;
76
77 fp = fopen(RANDOM_DEVICE, "r");
78 if (fp == NULL) {
79 return (-1);
80 }
81 if (fread(buf, 1, buf_len, fp) != buf_len) {
82 saved_errno = errno;
83 fclose(fp);
84 errno = saved_errno;
85 return (-1);
86 }
87 if (fclose(fp) != 0) {
88 return (-1);
89 }
90
91 return (0);
92 }
93 #endif /* HAVE_GETRANDOM */
94
95 int
96 getentropy(void *buf, size_t buf_len)
97 {
98 if (buf_len > 256) {
99 errno = EIO;
100 return (-1);
101 }
102
103 return (
104 #ifdef HAVE_GETRANDOM
105 getentropy_linux_getrandom(
106 #else /* HAVE_GETRANDOM */
107 getentropy_dev_random(
108 #endif /* HAVE_GETRANDOM */
109 buf, buf_len));
110 }