src/random.c

filter: Statementstmt 84.2% 15 / 19 (1 uncoverable) Branchbran 100% 6 / 10 (4 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.6% 21 / 29 (5 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/Crypt/Symmetric/Primitives/Install/Files.pm ↑ Summary src/wipe.c »
1 #include "symprim/config.h"
2 #include "symprim/symprim.h"
3 #include "symprim/random.h"
4  
5 /* One source, chosen when the dist was configured. If it fails at run time
6 * the call fails: nothing here falls through to another. */
7  
8 #if defined(_WIN32)
9  
10 #include <windows.h>
11 #include <bcrypt.h>
12  
13 const char *symprim_random_source(void) { return "BCryptGenRandom"; }
14  
15 static int system_random(void *buf, size_t len)
16 {
17 unsigned char *p = (unsigned char *)buf;
18 while (len) {
19 ULONG n = len > 0x40000000 ? 0x40000000 : (ULONG)len;
20 if (BCryptGenRandom(NULL, p, n, BCRYPT_USE_SYSTEM_PREFERRED_RNG) != 0)
21 return SYMPRIM_E_RANDOM;
22 p += n;
23 len -= n;
24 }
25 return SYMPRIM_OK;
26 }
27  
28 #elif defined(SYMPRIM_HAVE_GETRANDOM)
29  
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #ifdef SYMPRIM_HAVE_SYS_RANDOM_H
34 #include <sys/random.h>
35 #endif
36  
37 2 const char *symprim_random_source(void) { return "getrandom"; }
38  
39 /* getrandom may return short for a large request and EINTR when a signal
40 * lands. It blocks until the pool is ready, which is what is wanted. */
41 402 static int system_random(void *buf, size_t len)
42 {
43 402 unsigned char *p = (unsigned char *)buf;
44 798 ▶ while (len) {
Branch: 1 branch
BranchTrueFalseTotal
while (len) { 396 402 798
45 396 ssize_t n = getrandom(p, len, 0);
46 /* The system's own source refusing, or a signal landing while it is
47 * asked, is not something a test can arrange.
48 # uncoverable branch true
49 # uncoverable condition left */
50 396 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 396 396
51 /*
52 # uncoverable statement
53 # uncoverable branch true
54 # uncoverable branch false
55 # uncoverable condition left
56 # uncoverable condition right */
57 0 ▶ if (errno == EINTR)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) 0 0 0
58 0 continue; /* # uncoverable statement */
59 0 return SYMPRIM_E_RANDOM; /* # uncoverable statement */
60 }
61 /*
62 # uncoverable branch true
63 # uncoverable condition left */
64 396 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 0 396 396
65 0 return SYMPRIM_E_RANDOM; /* # uncoverable statement */
66 396 p += n;
67 396 len -= (size_t)n;
68 }
69 402 return SYMPRIM_OK;
70 }
71  
72 #elif defined(SYMPRIM_HAVE_GETENTROPY)
73  
74 #include <sys/types.h>
75 #include <unistd.h>
76 #ifdef SYMPRIM_HAVE_SYS_RANDOM_H
77 #include <sys/random.h>
78 #endif
79  
80 const char *symprim_random_source(void) { return "getentropy"; }
81  
82 /* getentropy refuses more than 256 bytes a call. */
83 static int system_random(void *buf, size_t len)
84 {
85 unsigned char *p = (unsigned char *)buf;
86 while (len) {
87 size_t n = len > 256 ? 256 : len;
88 /* The system's own source refusing is not something a test can
89 * arrange.
90 # uncoverable branch true */
91 if (getentropy(p, n) != 0)
92 return SYMPRIM_E_RANDOM; /* # uncoverable statement */
93 p += n;
94 len -= n;
95 }
96 return SYMPRIM_OK;
97 }
98  
99 #elif defined(SYMPRIM_HAVE_ARC4RANDOM_BUF)
100  
101 #include <stdlib.h>
102  
103 const char *symprim_random_source(void) { return "arc4random_buf"; }
104  
105 static int system_random(void *buf, size_t len)
106 {
107 if (len)
108 arc4random_buf(buf, len);
109 return SYMPRIM_OK;
110 }
111  
112 #else
113  
114 #include <errno.h>
115 #include <fcntl.h>
116 #include <sys/types.h>
117 #include <sys/stat.h>
118 #include <unistd.h>
119  
120 const char *symprim_random_source(void) { return "/dev/urandom"; }
121  
122 static int system_random(void *buf, size_t len)
123 {
124 unsigned char *p = (unsigned char *)buf;
125 struct stat st;
126 int flags = O_RDONLY;
127 int fd;
128  
129 if (!len)
130 return SYMPRIM_OK;
131 #ifdef O_CLOEXEC
132 flags |= O_CLOEXEC;
133 #endif
134 do {
135 fd = open("/dev/urandom", flags);
136 } while (fd < 0 && errno == EINTR);
137 if (fd < 0)
138 return SYMPRIM_E_RANDOM;
139 if (fstat(fd, &st) != 0 || !S_ISCHR(st.st_mode)) {
140 close(fd);
141 return SYMPRIM_E_RANDOM;
142 }
143 while (len) {
144 ssize_t n = read(fd, p, len);
145 if (n < 0) {
146 if (errno == EINTR)
147 continue;
148 close(fd);
149 return SYMPRIM_E_RANDOM;
150 }
151 if (n == 0) {
152 close(fd);
153 return SYMPRIM_E_RANDOM;
154 }
155 p += n;
156 len -= (size_t)n;
157 }
158 close(fd);
159 return SYMPRIM_OK;
160 }
161  
162 #endif
163  
164 /* A test replaces the source to pin a salt or to fail on demand. NULL puts the
165 * system's back. One per process, not guarded, as the allocator is. */
166 static symprim_random_fn replaced = NULL;
167  
168 14 void symprim_set_random(symprim_random_fn fn)
169 {
170 14 replaced = fn;
171 14 }
172  
173 408 int symprim_random(void *buf, size_t len)
174 {
175 408 ▶ return replaced ? replaced(buf, len) : system_random(buf, len);
Branch: 1 branch
BranchTrueFalseTotal
return replaced ? replaced(buf, len) : system_random(buf, len); 6 402 408
176 }
« blib/arch/Crypt/Symmetric/Primitives/Install/Files.pm ↑ Summary src/wipe.c »