summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/libcrypto/arc4random/getentropy_osx.c79
-rw-r--r--src/lib/libcrypto/crypto/getentropy_osx.c79
2 files changed, 80 insertions, 78 deletions
diff --git a/src/lib/libcrypto/arc4random/getentropy_osx.c b/src/lib/libcrypto/arc4random/getentropy_osx.c
index 4b1ca3cce7..67583f9e13 100644
--- a/src/lib/libcrypto/arc4random/getentropy_osx.c
+++ b/src/lib/libcrypto/arc4random/getentropy_osx.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: getentropy_osx.c,v 1.2 2014/07/09 13:23:15 bcook Exp $ */ 1/* $OpenBSD: getentropy_osx.c,v 1.3 2014/07/12 14:48:00 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>
@@ -67,8 +67,10 @@
67 else \ 67 else \
68 HD(b); \ 68 HD(b); \
69 } while (0) 69 } while (0)
70
70#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l))) 71#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
71#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x))) 72#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
73
72int getentropy(void *buf, size_t len); 74int getentropy(void *buf, size_t len);
73 75
74extern int main(int, char *argv[]); 76extern int main(int, char *argv[]);
@@ -100,8 +102,8 @@ getentropy(void *buf, size_t len)
100 * Entropy collection via /dev/urandom and sysctl have failed. 102 * Entropy collection via /dev/urandom and sysctl have failed.
101 * 103 *
102 * No other API exists for collecting entropy, and we have 104 * No other API exists for collecting entropy, and we have
103 * no failsafe way to get it on OSX that is not sensitive 105 * no failsafe way to get it on OSX that is not sensitive
104 * to resource exhaustion. 106 * to resource exhaustion.
105 * 107 *
106 * We have very few options: 108 * We have very few options:
107 * - Even syslog_r is unsafe to call at this low level, so 109 * - Even syslog_r is unsafe to call at this low level, so
@@ -120,8 +122,8 @@ getentropy(void *buf, size_t len)
120 * providing a new failsafe API which works in a chroot or 122 * providing a new failsafe API which works in a chroot or
121 * when file descriptors are exhausted. 123 * when file descriptors are exhausted.
122 */ 124 */
123#undef FAIL_WHEN_SYSTEM_ENTROPY_FAILS 125#undef FAIL_INSTEAD_OF_TRYING_FALLBACK
124#ifdef FAIL_WHEN_SYSTEM_ENTROPY_FAILS 126#ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK
125 raise(SIGKILL); 127 raise(SIGKILL);
126#endif 128#endif
127 ret = getentropy_fallback(buf, len); 129 ret = getentropy_fallback(buf, len);
@@ -158,12 +160,12 @@ getentropy_urandom(void *buf, size_t len)
158 160
159start: 161start:
160 162
161 flags = O_RDONLY; 163 flags = O_RDONLY;
162#ifdef O_NOFOLLOW 164#ifdef O_NOFOLLOW
163 flags |= O_NOFOLLOW; 165 flags |= O_NOFOLLOW;
164#endif 166#endif
165#ifdef O_CLOEXEC 167#ifdef O_CLOEXEC
166 flags |= O_CLOEXEC; 168 flags |= O_CLOEXEC;
167#endif 169#endif
168 fd = open("/dev/urandom", flags, 0); 170 fd = open("/dev/urandom", flags, 0);
169 if (fd == -1) { 171 if (fd == -1) {
@@ -202,33 +204,34 @@ nodevrandom:
202 return -1; 204 return -1;
203} 205}
204 206
207static int tcpmib[] = { CTL_NET, AF_INET, IPPROTO_TCP, TCPCTL_STATS };
208static int udpmib[] = { CTL_NET, AF_INET, IPPROTO_UDP, UDPCTL_STATS };
209static int ipmib[] = { CTL_NET, AF_INET, IPPROTO_IP, IPCTL_STATS };
210static int kmib[] = { CTL_KERN, KERN_USRSTACK };
211static int hwmib[] = { CTL_HW, HW_USERMEM };
212
205static int 213static int
206getentropy_fallback(void *buf, size_t len) 214getentropy_fallback(void *buf, size_t len)
207{ 215{
208 int tcpmib[] = { CTL_NET, AF_INET, IPPROTO_TCP, TCPCTL_STATS };
209 int udpmib[] = { CTL_NET, AF_INET, IPPROTO_UDP, UDPCTL_STATS };
210 int ipmib[] = { CTL_NET, AF_INET, IPPROTO_IP, IPCTL_STATS };
211 int kmib[] = { CTL_KERN, KERN_USRSTACK };
212 int hwmib[] = { CTL_HW, HW_USERMEM };
213 int save_errno = errno, e, m, pgs = getpagesize(), faster = 0, repeat;
214 uint8_t results[SHA512_DIGEST_LENGTH]; 216 uint8_t results[SHA512_DIGEST_LENGTH];
215 struct tcpstat tcpstat; 217 int save_errno = errno, e, m, pgs = getpagesize(), faster = 0, repeat;
216 struct udpstat udpstat; 218 static int cnt;
217 struct ipstat ipstat;
218 u_int64_t mach_time;
219 unsigned int idata;
220 struct timespec ts; 219 struct timespec ts;
221 struct timeval tv; 220 struct timeval tv;
222 struct rusage ru; 221 struct rusage ru;
223 sigset_t sigset; 222 sigset_t sigset;
224 struct stat st; 223 struct stat st;
225 static int cnt;
226 SHA512_CTX ctx; 224 SHA512_CTX ctx;
227 static pid_t lastpid; 225 static pid_t lastpid;
228 void * addr;
229 pid_t pid; 226 pid_t pid;
230 size_t i; 227 size_t i, ii;
231 char *p; 228 char *p;
229 struct tcpstat tcpstat;
230 struct udpstat udpstat;
231 struct ipstat ipstat;
232 u_int64_t mach_time;
233 unsigned int idata;
234 void *addr;
232 235
233 pid = getpid(); 236 pid = getpid();
234 if (lastpid == pid) { 237 if (lastpid == pid) {
@@ -243,35 +246,34 @@ getentropy_fallback(void *buf, size_t len)
243 int j; 246 int j;
244 SHA512_Init(&ctx); 247 SHA512_Init(&ctx);
245 for (j = 0; j < repeat; j++) { 248 for (j = 0; j < repeat; j++) {
246 size_t len;
247 HX((e = gettimeofday(&tv, NULL)) == -1, tv); 249 HX((e = gettimeofday(&tv, NULL)) == -1, tv);
248 if (e != -1) { 250 if (e != -1) {
249 cnt += (int)tv.tv_sec; 251 cnt += (int)tv.tv_sec;
250 cnt += (int)tv.tv_usec; 252 cnt += (int)tv.tv_usec;
251 } 253 }
252 254
253 mach_time = mach_absolute_time(); 255 mach_time = mach_absolute_time();
254 HD(mach_time); 256 HD(mach_time);
255 257
256 len = sizeof(addr); 258 ii = sizeof(addr);
257 HX(sysctl(kmib, sizeof(kmib) / sizeof(kmib[0]), 259 HX(sysctl(kmib, sizeof(kmib) / sizeof(kmib[0]),
258 &addr, &len, NULL, 0) == -1, addr); 260 &addr, &ii, NULL, 0) == -1, addr);
259 261
260 len = sizeof(idata); 262 ii = sizeof(idata);
261 HX(sysctl(hwmib, sizeof(hwmib) / sizeof(hwmib[0]), 263 HX(sysctl(hwmib, sizeof(hwmib) / sizeof(hwmib[0]),
262 &idata, &len, NULL, 0) == -1, idata); 264 &idata, &ii, NULL, 0) == -1, idata);
263 265
264 len = sizeof(tcpstat); 266 ii = sizeof(tcpstat);
265 HX(sysctl(tcpmib, sizeof(tcpmib) / sizeof(tcpmib[0]), 267 HX(sysctl(tcpmib, sizeof(tcpmib) / sizeof(tcpmib[0]),
266 &tcpstat, &len, NULL, 0) == -1, tcpstat); 268 &tcpstat, &ii, NULL, 0) == -1, tcpstat);
267 269
268 len = sizeof(udpstat); 270 ii = sizeof(udpstat);
269 HX(sysctl(udpmib, sizeof(udpmib) / sizeof(udpmib[0]), 271 HX(sysctl(udpmib, sizeof(udpmib) / sizeof(udpmib[0]),
270 &udpstat, &len, NULL, 0) == -1, udpstat); 272 &udpstat, &ii, NULL, 0) == -1, udpstat);
271 273
272 len = sizeof(ipstat); 274 ii = sizeof(ipstat);
273 HX(sysctl(ipmib, sizeof(ipmib) / sizeof(ipmib[0]), 275 HX(sysctl(ipmib, sizeof(ipmib) / sizeof(ipmib[0]),
274 &ipstat, &len, NULL, 0) == -1, ipstat); 276 &ipstat, &ii, NULL, 0) == -1, ipstat);
275 277
276 HX((pid = getpid()) == -1, pid); 278 HX((pid = getpid()) == -1, pid);
277 HX((pid = getsid(pid)) == -1, pid); 279 HX((pid = getsid(pid)) == -1, pid);
@@ -340,9 +342,9 @@ getentropy_fallback(void *buf, size_t len)
340 } 342 }
341 343
342 /* Check cnts and times... */ 344 /* Check cnts and times... */
343 mach_time = mach_absolute_time(); 345 mach_time = mach_absolute_time();
344 HD(mach_time); 346 HD(mach_time);
345 cnt += (int)mach_time; 347 cnt += (int)mach_time;
346 348
347 HX((e = getrusage(RUSAGE_SELF, 349 HX((e = getrusage(RUSAGE_SELF,
348 &ru)) == -1, ru); 350 &ru)) == -1, ru);
@@ -410,7 +412,6 @@ getentropy_fallback(void *buf, size_t len)
410 HD(cnt); 412 HD(cnt);
411 } 413 }
412 414
413
414 SHA512_Final(results, &ctx); 415 SHA512_Final(results, &ctx);
415 memcpy(buf + i, results, min(sizeof(results), len - i)); 416 memcpy(buf + i, results, min(sizeof(results), len - i));
416 i += min(sizeof(results), len - i); 417 i += min(sizeof(results), len - i);
diff --git a/src/lib/libcrypto/crypto/getentropy_osx.c b/src/lib/libcrypto/crypto/getentropy_osx.c
index 4b1ca3cce7..67583f9e13 100644
--- a/src/lib/libcrypto/crypto/getentropy_osx.c
+++ b/src/lib/libcrypto/crypto/getentropy_osx.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: getentropy_osx.c,v 1.2 2014/07/09 13:23:15 bcook Exp $ */ 1/* $OpenBSD: getentropy_osx.c,v 1.3 2014/07/12 14:48:00 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>
@@ -67,8 +67,10 @@
67 else \ 67 else \
68 HD(b); \ 68 HD(b); \
69 } while (0) 69 } while (0)
70
70#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l))) 71#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
71#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x))) 72#define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
73
72int getentropy(void *buf, size_t len); 74int getentropy(void *buf, size_t len);
73 75
74extern int main(int, char *argv[]); 76extern int main(int, char *argv[]);
@@ -100,8 +102,8 @@ getentropy(void *buf, size_t len)
100 * Entropy collection via /dev/urandom and sysctl have failed. 102 * Entropy collection via /dev/urandom and sysctl have failed.
101 * 103 *
102 * No other API exists for collecting entropy, and we have 104 * No other API exists for collecting entropy, and we have
103 * no failsafe way to get it on OSX that is not sensitive 105 * no failsafe way to get it on OSX that is not sensitive
104 * to resource exhaustion. 106 * to resource exhaustion.
105 * 107 *
106 * We have very few options: 108 * We have very few options:
107 * - Even syslog_r is unsafe to call at this low level, so 109 * - Even syslog_r is unsafe to call at this low level, so
@@ -120,8 +122,8 @@ getentropy(void *buf, size_t len)
120 * providing a new failsafe API which works in a chroot or 122 * providing a new failsafe API which works in a chroot or
121 * when file descriptors are exhausted. 123 * when file descriptors are exhausted.
122 */ 124 */
123#undef FAIL_WHEN_SYSTEM_ENTROPY_FAILS 125#undef FAIL_INSTEAD_OF_TRYING_FALLBACK
124#ifdef FAIL_WHEN_SYSTEM_ENTROPY_FAILS 126#ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK
125 raise(SIGKILL); 127 raise(SIGKILL);
126#endif 128#endif
127 ret = getentropy_fallback(buf, len); 129 ret = getentropy_fallback(buf, len);
@@ -158,12 +160,12 @@ getentropy_urandom(void *buf, size_t len)
158 160
159start: 161start:
160 162
161 flags = O_RDONLY; 163 flags = O_RDONLY;
162#ifdef O_NOFOLLOW 164#ifdef O_NOFOLLOW
163 flags |= O_NOFOLLOW; 165 flags |= O_NOFOLLOW;
164#endif 166#endif
165#ifdef O_CLOEXEC 167#ifdef O_CLOEXEC
166 flags |= O_CLOEXEC; 168 flags |= O_CLOEXEC;
167#endif 169#endif
168 fd = open("/dev/urandom", flags, 0); 170 fd = open("/dev/urandom", flags, 0);
169 if (fd == -1) { 171 if (fd == -1) {
@@ -202,33 +204,34 @@ nodevrandom:
202 return -1; 204 return -1;
203} 205}
204 206
207static int tcpmib[] = { CTL_NET, AF_INET, IPPROTO_TCP, TCPCTL_STATS };
208static int udpmib[] = { CTL_NET, AF_INET, IPPROTO_UDP, UDPCTL_STATS };
209static int ipmib[] = { CTL_NET, AF_INET, IPPROTO_IP, IPCTL_STATS };
210static int kmib[] = { CTL_KERN, KERN_USRSTACK };
211static int hwmib[] = { CTL_HW, HW_USERMEM };
212
205static int 213static int
206getentropy_fallback(void *buf, size_t len) 214getentropy_fallback(void *buf, size_t len)
207{ 215{
208 int tcpmib[] = { CTL_NET, AF_INET, IPPROTO_TCP, TCPCTL_STATS };
209 int udpmib[] = { CTL_NET, AF_INET, IPPROTO_UDP, UDPCTL_STATS };
210 int ipmib[] = { CTL_NET, AF_INET, IPPROTO_IP, IPCTL_STATS };
211 int kmib[] = { CTL_KERN, KERN_USRSTACK };
212 int hwmib[] = { CTL_HW, HW_USERMEM };
213 int save_errno = errno, e, m, pgs = getpagesize(), faster = 0, repeat;
214 uint8_t results[SHA512_DIGEST_LENGTH]; 216 uint8_t results[SHA512_DIGEST_LENGTH];
215 struct tcpstat tcpstat; 217 int save_errno = errno, e, m, pgs = getpagesize(), faster = 0, repeat;
216 struct udpstat udpstat; 218 static int cnt;
217 struct ipstat ipstat;
218 u_int64_t mach_time;
219 unsigned int idata;
220 struct timespec ts; 219 struct timespec ts;
221 struct timeval tv; 220 struct timeval tv;
222 struct rusage ru; 221 struct rusage ru;
223 sigset_t sigset; 222 sigset_t sigset;
224 struct stat st; 223 struct stat st;
225 static int cnt;
226 SHA512_CTX ctx; 224 SHA512_CTX ctx;
227 static pid_t lastpid; 225 static pid_t lastpid;
228 void * addr;
229 pid_t pid; 226 pid_t pid;
230 size_t i; 227 size_t i, ii;
231 char *p; 228 char *p;
229 struct tcpstat tcpstat;
230 struct udpstat udpstat;
231 struct ipstat ipstat;
232 u_int64_t mach_time;
233 unsigned int idata;
234 void *addr;
232 235
233 pid = getpid(); 236 pid = getpid();
234 if (lastpid == pid) { 237 if (lastpid == pid) {
@@ -243,35 +246,34 @@ getentropy_fallback(void *buf, size_t len)
243 int j; 246 int j;
244 SHA512_Init(&ctx); 247 SHA512_Init(&ctx);
245 for (j = 0; j < repeat; j++) { 248 for (j = 0; j < repeat; j++) {
246 size_t len;
247 HX((e = gettimeofday(&tv, NULL)) == -1, tv); 249 HX((e = gettimeofday(&tv, NULL)) == -1, tv);
248 if (e != -1) { 250 if (e != -1) {
249 cnt += (int)tv.tv_sec; 251 cnt += (int)tv.tv_sec;
250 cnt += (int)tv.tv_usec; 252 cnt += (int)tv.tv_usec;
251 } 253 }
252 254
253 mach_time = mach_absolute_time(); 255 mach_time = mach_absolute_time();
254 HD(mach_time); 256 HD(mach_time);
255 257
256 len = sizeof(addr); 258 ii = sizeof(addr);
257 HX(sysctl(kmib, sizeof(kmib) / sizeof(kmib[0]), 259 HX(sysctl(kmib, sizeof(kmib) / sizeof(kmib[0]),
258 &addr, &len, NULL, 0) == -1, addr); 260 &addr, &ii, NULL, 0) == -1, addr);
259 261
260 len = sizeof(idata); 262 ii = sizeof(idata);
261 HX(sysctl(hwmib, sizeof(hwmib) / sizeof(hwmib[0]), 263 HX(sysctl(hwmib, sizeof(hwmib) / sizeof(hwmib[0]),
262 &idata, &len, NULL, 0) == -1, idata); 264 &idata, &ii, NULL, 0) == -1, idata);
263 265
264 len = sizeof(tcpstat); 266 ii = sizeof(tcpstat);
265 HX(sysctl(tcpmib, sizeof(tcpmib) / sizeof(tcpmib[0]), 267 HX(sysctl(tcpmib, sizeof(tcpmib) / sizeof(tcpmib[0]),
266 &tcpstat, &len, NULL, 0) == -1, tcpstat); 268 &tcpstat, &ii, NULL, 0) == -1, tcpstat);
267 269
268 len = sizeof(udpstat); 270 ii = sizeof(udpstat);
269 HX(sysctl(udpmib, sizeof(udpmib) / sizeof(udpmib[0]), 271 HX(sysctl(udpmib, sizeof(udpmib) / sizeof(udpmib[0]),
270 &udpstat, &len, NULL, 0) == -1, udpstat); 272 &udpstat, &ii, NULL, 0) == -1, udpstat);
271 273
272 len = sizeof(ipstat); 274 ii = sizeof(ipstat);
273 HX(sysctl(ipmib, sizeof(ipmib) / sizeof(ipmib[0]), 275 HX(sysctl(ipmib, sizeof(ipmib) / sizeof(ipmib[0]),
274 &ipstat, &len, NULL, 0) == -1, ipstat); 276 &ipstat, &ii, NULL, 0) == -1, ipstat);
275 277
276 HX((pid = getpid()) == -1, pid); 278 HX((pid = getpid()) == -1, pid);
277 HX((pid = getsid(pid)) == -1, pid); 279 HX((pid = getsid(pid)) == -1, pid);
@@ -340,9 +342,9 @@ getentropy_fallback(void *buf, size_t len)
340 } 342 }
341 343
342 /* Check cnts and times... */ 344 /* Check cnts and times... */
343 mach_time = mach_absolute_time(); 345 mach_time = mach_absolute_time();
344 HD(mach_time); 346 HD(mach_time);
345 cnt += (int)mach_time; 347 cnt += (int)mach_time;
346 348
347 HX((e = getrusage(RUSAGE_SELF, 349 HX((e = getrusage(RUSAGE_SELF,
348 &ru)) == -1, ru); 350 &ru)) == -1, ru);
@@ -410,7 +412,6 @@ getentropy_fallback(void *buf, size_t len)
410 HD(cnt); 412 HD(cnt);
411 } 413 }
412 414
413
414 SHA512_Final(results, &ctx); 415 SHA512_Final(results, &ctx);
415 memcpy(buf + i, results, min(sizeof(results), len - i)); 416 memcpy(buf + i, results, min(sizeof(results), len - i));
416 i += min(sizeof(results), len - i); 417 i += min(sizeof(results), len - i);