xref: /netbsd-src/external/bsd/unbound/dist/compat/getentropy_solaris.c (revision 91f7d55fb697b5e0475da4718fa34c3a3ebeac85)
1d0eba39bSchristos /*	$OpenBSD: getentropy_solaris.c,v 1.4 2014/07/12 20:41:47 wouter Exp $	*/
23b6c3722Schristos 
33b6c3722Schristos /*
43b6c3722Schristos  * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
53b6c3722Schristos  * Copyright (c) 2014 Bob Beck <beck@obtuse.com>
63b6c3722Schristos  *
73b6c3722Schristos  * Permission to use, copy, modify, and distribute this software for any
83b6c3722Schristos  * purpose with or without fee is hereby granted, provided that the above
93b6c3722Schristos  * copyright notice and this permission notice appear in all copies.
103b6c3722Schristos  *
113b6c3722Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
123b6c3722Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
133b6c3722Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
143b6c3722Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
153b6c3722Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
163b6c3722Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
173b6c3722Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
183b6c3722Schristos  */
1901049ae6Schristos #include "config.h"
20d0eba39bSchristos 
213b6c3722Schristos #include <sys/types.h>
223b6c3722Schristos #include <sys/param.h>
233b6c3722Schristos #include <sys/ioctl.h>
243b6c3722Schristos #include <sys/resource.h>
253b6c3722Schristos #include <sys/syscall.h>
263b6c3722Schristos #include <sys/statvfs.h>
273b6c3722Schristos #include <sys/socket.h>
283b6c3722Schristos #include <sys/mount.h>
293b6c3722Schristos #include <sys/mman.h>
303b6c3722Schristos #include <sys/stat.h>
313b6c3722Schristos #include <sys/time.h>
323b6c3722Schristos #include <stdlib.h>
333b6c3722Schristos #ifdef HAVE_STDINT_H
343b6c3722Schristos #include <stdint.h>
353b6c3722Schristos #endif
363b6c3722Schristos #include <stdio.h>
373b6c3722Schristos #include <termios.h>
383b6c3722Schristos #include <fcntl.h>
393b6c3722Schristos #include <signal.h>
403b6c3722Schristos #include <string.h>
413b6c3722Schristos #include <errno.h>
423b6c3722Schristos #include <unistd.h>
433b6c3722Schristos #include <time.h>
443b6c3722Schristos #ifdef HAVE_SYS_SHA2_H
453b6c3722Schristos #include <sys/sha2.h>
463b6c3722Schristos #define SHA512_Init SHA512Init
473b6c3722Schristos #define SHA512_Update SHA512Update
483b6c3722Schristos #define SHA512_Final SHA512Final
493b6c3722Schristos #else
50*91f7d55fSchristos #include <openssl/sha.h>
513b6c3722Schristos #endif
523b6c3722Schristos 
533b6c3722Schristos #include <sys/vfs.h>
543b6c3722Schristos #include <sys/statfs.h>
553b6c3722Schristos #include <sys/loadavg.h>
563b6c3722Schristos 
573b6c3722Schristos #define REPEAT 5
583b6c3722Schristos #define min(a, b) (((a) < (b)) ? (a) : (b))
593b6c3722Schristos 
603b6c3722Schristos #define HX(a, b) \
613b6c3722Schristos 	do { \
623b6c3722Schristos 		if ((a)) \
633b6c3722Schristos 			HD(errno); \
643b6c3722Schristos 		else \
653b6c3722Schristos 			HD(b); \
663b6c3722Schristos 	} while (0)
673b6c3722Schristos 
683b6c3722Schristos #define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
693b6c3722Schristos #define HD(x)	 (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
703b6c3722Schristos #define HF(x)	 (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
713b6c3722Schristos 
723b6c3722Schristos int	getentropy(void *buf, size_t len);
733b6c3722Schristos 
74d0eba39bSchristos #ifdef CAN_REFERENCE_MAIN
75d0eba39bSchristos extern int main(int, char *argv[]);
76d0eba39bSchristos #endif
77d0eba39bSchristos static int gotdata(char *buf, size_t len);
783b6c3722Schristos static int getentropy_urandom(void *buf, size_t len, const char *path,
793b6c3722Schristos     int devfscheck);
803b6c3722Schristos static int getentropy_fallback(void *buf, size_t len);
813b6c3722Schristos 
823b6c3722Schristos int
getentropy(void * buf,size_t len)833b6c3722Schristos getentropy(void *buf, size_t len)
843b6c3722Schristos {
853b6c3722Schristos 	int ret = -1;
863b6c3722Schristos 
873b6c3722Schristos 	if (len > 256) {
883b6c3722Schristos 		errno = EIO;
89d0eba39bSchristos 		return -1;
903b6c3722Schristos 	}
913b6c3722Schristos 
923b6c3722Schristos 	/*
933b6c3722Schristos 	 * Try to get entropy with /dev/urandom
943b6c3722Schristos 	 *
953b6c3722Schristos 	 * Solaris provides /dev/urandom as a symbolic link to
963b6c3722Schristos 	 * /devices/pseudo/random@0:urandom which is provided by
973b6c3722Schristos 	 * a devfs filesystem.  Best practice is to use O_NOFOLLOW,
983b6c3722Schristos 	 * so we must try the unpublished name directly.
993b6c3722Schristos 	 *
1003b6c3722Schristos 	 * This can fail if the process is inside a chroot which lacks
1013b6c3722Schristos 	 * the devfs mount, or if file descriptors are exhausted.
1023b6c3722Schristos 	 */
1033b6c3722Schristos 	ret = getentropy_urandom(buf, len,
1043b6c3722Schristos 	    "/devices/pseudo/random@0:urandom", 1);
1053b6c3722Schristos 	if (ret != -1)
1063b6c3722Schristos 		return (ret);
1073b6c3722Schristos 
1083b6c3722Schristos 	/*
1093b6c3722Schristos 	 * Unfortunately, chroot spaces on Solaris are sometimes setup
1103b6c3722Schristos 	 * with direct device node of the well-known /dev/urandom name
1113b6c3722Schristos 	 * (perhaps to avoid dragging all of devfs into the space).
1123b6c3722Schristos 	 *
1133b6c3722Schristos 	 * This can fail if the process is inside a chroot or if file
1143b6c3722Schristos 	 * descriptors are exhausted.
1153b6c3722Schristos 	 */
1163b6c3722Schristos 	ret = getentropy_urandom(buf, len, "/dev/urandom", 0);
1173b6c3722Schristos 	if (ret != -1)
1183b6c3722Schristos 		return (ret);
1193b6c3722Schristos 
1203b6c3722Schristos 	/*
1213b6c3722Schristos 	 * Entropy collection via /dev/urandom has failed.
1223b6c3722Schristos 	 *
1233b6c3722Schristos 	 * No other API exists for collecting entropy, and we have
1243b6c3722Schristos 	 * no failsafe way to get it on Solaris that is not sensitive
1253b6c3722Schristos 	 * to resource exhaustion.
1263b6c3722Schristos 	 *
1273b6c3722Schristos 	 * We have very few options:
1283b6c3722Schristos 	 *     - Even syslog_r is unsafe to call at this low level, so
1293b6c3722Schristos 	 *	 there is no way to alert the user or program.
1303b6c3722Schristos 	 *     - Cannot call abort() because some systems have unsafe
1313b6c3722Schristos 	 *	 corefiles.
1323b6c3722Schristos 	 *     - Could raise(SIGKILL) resulting in silent program termination.
1333b6c3722Schristos 	 *     - Return EIO, to hint that arc4random's stir function
1343b6c3722Schristos 	 *       should raise(SIGKILL)
1353b6c3722Schristos 	 *     - Do the best under the circumstances....
1363b6c3722Schristos 	 *
1373b6c3722Schristos 	 * This code path exists to bring light to the issue that Solaris
1383b6c3722Schristos 	 * does not provide a failsafe API for entropy collection.
1393b6c3722Schristos 	 *
1403b6c3722Schristos 	 * We hope this demonstrates that Solaris should consider
1413b6c3722Schristos 	 * providing a new failsafe API which works in a chroot or
1423b6c3722Schristos 	 * when file descriptors are exhausted.
1433b6c3722Schristos 	 */
1443b6c3722Schristos #undef FAIL_INSTEAD_OF_TRYING_FALLBACK
1453b6c3722Schristos #ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK
1463b6c3722Schristos 	raise(SIGKILL);
1473b6c3722Schristos #endif
1483b6c3722Schristos 	ret = getentropy_fallback(buf, len);
1493b6c3722Schristos 	if (ret != -1)
1503b6c3722Schristos 		return (ret);
1513b6c3722Schristos 
1523b6c3722Schristos 	errno = EIO;
1533b6c3722Schristos 	return (ret);
1543b6c3722Schristos }
1553b6c3722Schristos 
156d0eba39bSchristos /*
157d0eba39bSchristos  * Basic sanity checking; wish we could do better.
158d0eba39bSchristos  */
159d0eba39bSchristos static int
gotdata(char * buf,size_t len)160d0eba39bSchristos gotdata(char *buf, size_t len)
161d0eba39bSchristos {
162d0eba39bSchristos 	char	any_set = 0;
163d0eba39bSchristos 	size_t	i;
164d0eba39bSchristos 
165d0eba39bSchristos 	for (i = 0; i < len; ++i)
166d0eba39bSchristos 		any_set |= buf[i];
167d0eba39bSchristos 	if (any_set == 0)
168d0eba39bSchristos 		return -1;
169d0eba39bSchristos 	return 0;
170d0eba39bSchristos }
171d0eba39bSchristos 
1723b6c3722Schristos static int
getentropy_urandom(void * buf,size_t len,const char * path,int devfscheck)1733b6c3722Schristos getentropy_urandom(void *buf, size_t len, const char *path, int devfscheck)
1743b6c3722Schristos {
1753b6c3722Schristos 	struct stat st;
1763b6c3722Schristos 	size_t i;
1773b6c3722Schristos 	int fd, flags;
1783b6c3722Schristos 	int save_errno = errno;
1793b6c3722Schristos 
1803b6c3722Schristos start:
1813b6c3722Schristos 
1823b6c3722Schristos 	flags = O_RDONLY;
1833b6c3722Schristos #ifdef O_NOFOLLOW
1843b6c3722Schristos 	flags |= O_NOFOLLOW;
1853b6c3722Schristos #endif
1863b6c3722Schristos #ifdef O_CLOEXEC
1873b6c3722Schristos 	flags |= O_CLOEXEC;
1883b6c3722Schristos #endif
1893b6c3722Schristos 	fd = open(path, flags, 0);
1903b6c3722Schristos 	if (fd == -1) {
1913b6c3722Schristos 		if (errno == EINTR)
1923b6c3722Schristos 			goto start;
1933b6c3722Schristos 		goto nodevrandom;
1943b6c3722Schristos 	}
1953b6c3722Schristos #ifndef O_CLOEXEC
1963b6c3722Schristos 	fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
1973b6c3722Schristos #endif
1983b6c3722Schristos 
1993b6c3722Schristos 	/* Lightly verify that the device node looks sane */
2003b6c3722Schristos 	if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode) ||
2013b6c3722Schristos 	    (devfscheck && (strcmp(st.st_fstype, "devfs") != 0))) {
2023b6c3722Schristos 		close(fd);
2033b6c3722Schristos 		goto nodevrandom;
2043b6c3722Schristos 	}
2053b6c3722Schristos 	for (i = 0; i < len; ) {
2063b6c3722Schristos 		size_t wanted = len - i;
2073b6c3722Schristos 		ssize_t ret = read(fd, (char *)buf + i, wanted);
2083b6c3722Schristos 
2093b6c3722Schristos 		if (ret == -1) {
2103b6c3722Schristos 			if (errno == EAGAIN || errno == EINTR)
2113b6c3722Schristos 				continue;
2123b6c3722Schristos 			close(fd);
2133b6c3722Schristos 			goto nodevrandom;
2143b6c3722Schristos 		}
2153b6c3722Schristos 		i += ret;
2163b6c3722Schristos 	}
2173b6c3722Schristos 	close(fd);
218d0eba39bSchristos 	if (gotdata(buf, len) == 0) {
2193b6c3722Schristos 		errno = save_errno;
220d0eba39bSchristos 		return 0;		/* satisfied */
221d0eba39bSchristos 	}
2223b6c3722Schristos nodevrandom:
2233b6c3722Schristos 	errno = EIO;
224d0eba39bSchristos 	return -1;
2253b6c3722Schristos }
2263b6c3722Schristos 
2273b6c3722Schristos static const int cl[] = {
2283b6c3722Schristos 	CLOCK_REALTIME,
2293b6c3722Schristos #ifdef CLOCK_MONOTONIC
2303b6c3722Schristos 	CLOCK_MONOTONIC,
2313b6c3722Schristos #endif
2323b6c3722Schristos #ifdef CLOCK_MONOTONIC_RAW
2333b6c3722Schristos 	CLOCK_MONOTONIC_RAW,
2343b6c3722Schristos #endif
2353b6c3722Schristos #ifdef CLOCK_TAI
2363b6c3722Schristos 	CLOCK_TAI,
2373b6c3722Schristos #endif
2383b6c3722Schristos #ifdef CLOCK_VIRTUAL
2393b6c3722Schristos 	CLOCK_VIRTUAL,
2403b6c3722Schristos #endif
2413b6c3722Schristos #ifdef CLOCK_UPTIME
2423b6c3722Schristos 	CLOCK_UPTIME,
2433b6c3722Schristos #endif
2443b6c3722Schristos #ifdef CLOCK_PROCESS_CPUTIME_ID
2453b6c3722Schristos 	CLOCK_PROCESS_CPUTIME_ID,
2463b6c3722Schristos #endif
2473b6c3722Schristos #ifdef CLOCK_THREAD_CPUTIME_ID
2483b6c3722Schristos 	CLOCK_THREAD_CPUTIME_ID,
2493b6c3722Schristos #endif
2503b6c3722Schristos };
2513b6c3722Schristos 
2523b6c3722Schristos static int
getentropy_fallback(void * buf,size_t len)2533b6c3722Schristos getentropy_fallback(void *buf, size_t len)
2543b6c3722Schristos {
2553b6c3722Schristos 	uint8_t results[SHA512_DIGEST_LENGTH];
2563b6c3722Schristos 	int save_errno = errno, e, pgs = getpagesize(), faster = 0, repeat;
2573b6c3722Schristos 	static int cnt;
2583b6c3722Schristos 	struct timespec ts;
2593b6c3722Schristos 	struct timeval tv;
2603b6c3722Schristos 	double loadavg[3];
2613b6c3722Schristos 	struct rusage ru;
2623b6c3722Schristos 	sigset_t sigset;
2633b6c3722Schristos 	struct stat st;
2643b6c3722Schristos 	SHA512_CTX ctx;
2653b6c3722Schristos 	static pid_t lastpid;
2663b6c3722Schristos 	pid_t pid;
2673b6c3722Schristos 	size_t i, ii, m;
2683b6c3722Schristos 	char *p;
2693b6c3722Schristos 
2703b6c3722Schristos 	pid = getpid();
2713b6c3722Schristos 	if (lastpid == pid) {
2723b6c3722Schristos 		faster = 1;
2733b6c3722Schristos 		repeat = 2;
2743b6c3722Schristos 	} else {
2753b6c3722Schristos 		faster = 0;
2763b6c3722Schristos 		lastpid = pid;
2773b6c3722Schristos 		repeat = REPEAT;
2783b6c3722Schristos 	}
2793b6c3722Schristos 	for (i = 0; i < len; ) {
2803b6c3722Schristos 		int j;
2813b6c3722Schristos 		SHA512_Init(&ctx);
2823b6c3722Schristos 		for (j = 0; j < repeat; j++) {
2833b6c3722Schristos 			HX((e = gettimeofday(&tv, NULL)) == -1, tv);
2843b6c3722Schristos 			if (e != -1) {
2853b6c3722Schristos 				cnt += (int)tv.tv_sec;
2863b6c3722Schristos 				cnt += (int)tv.tv_usec;
2873b6c3722Schristos 			}
2883b6c3722Schristos 
2893b6c3722Schristos 			for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); ii++)
2903b6c3722Schristos 				HX(clock_gettime(cl[ii], &ts) == -1, ts);
2913b6c3722Schristos 
2923b6c3722Schristos 			HX((pid = getpid()) == -1, pid);
2933b6c3722Schristos 			HX((pid = getsid(pid)) == -1, pid);
2943b6c3722Schristos 			HX((pid = getppid()) == -1, pid);
2953b6c3722Schristos 			HX((pid = getpgid(0)) == -1, pid);
2963b6c3722Schristos 			HX((e = getpriority(0, 0)) == -1, e);
2973b6c3722Schristos 			HX((getloadavg(loadavg, 3) == -1), loadavg);
2983b6c3722Schristos 
2993b6c3722Schristos 			if (!faster) {
3003b6c3722Schristos 				ts.tv_sec = 0;
3013b6c3722Schristos 				ts.tv_nsec = 1;
3023b6c3722Schristos 				(void) nanosleep(&ts, NULL);
3033b6c3722Schristos 			}
3043b6c3722Schristos 
3053b6c3722Schristos 			HX(sigpending(&sigset) == -1, sigset);
3063b6c3722Schristos 			HX(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1,
3073b6c3722Schristos 			    sigset);
3083b6c3722Schristos 
309d0eba39bSchristos #ifdef CAN_REFERENCE_MAIN
310d0eba39bSchristos 			HF(main);		/* an addr in program */
311d0eba39bSchristos #endif
3123b6c3722Schristos 			HF(getentropy);	/* an addr in this library */
3133b6c3722Schristos 			HF(printf);		/* an addr in libc */
3143b6c3722Schristos 			p = (char *)&p;
3153b6c3722Schristos 			HD(p);		/* an addr on stack */
3163b6c3722Schristos 			p = (char *)&errno;
3173b6c3722Schristos 			HD(p);		/* the addr of errno */
3183b6c3722Schristos 
3193b6c3722Schristos 			if (i == 0) {
3203b6c3722Schristos 				struct sockaddr_storage ss;
3213b6c3722Schristos 				struct statvfs stvfs;
3223b6c3722Schristos 				struct termios tios;
3233b6c3722Schristos 				socklen_t ssl;
3243b6c3722Schristos 				off_t off;
3253b6c3722Schristos 
3263b6c3722Schristos 				/*
3273b6c3722Schristos 				 * Prime-sized mappings encourage fragmentation;
3283b6c3722Schristos 				 * thus exposing some address entropy.
3293b6c3722Schristos 				 */
3303b6c3722Schristos 				struct mm {
3313b6c3722Schristos 					size_t	npg;
3323b6c3722Schristos 					void	*p;
3333b6c3722Schristos 				} mm[] =	 {
3343b6c3722Schristos 					{ 17, MAP_FAILED }, { 3, MAP_FAILED },
3353b6c3722Schristos 					{ 11, MAP_FAILED }, { 2, MAP_FAILED },
3363b6c3722Schristos 					{ 5, MAP_FAILED }, { 3, MAP_FAILED },
3373b6c3722Schristos 					{ 7, MAP_FAILED }, { 1, MAP_FAILED },
3383b6c3722Schristos 					{ 57, MAP_FAILED }, { 3, MAP_FAILED },
3393b6c3722Schristos 					{ 131, MAP_FAILED }, { 1, MAP_FAILED },
3403b6c3722Schristos 				};
3413b6c3722Schristos 
3423b6c3722Schristos 				for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
3433b6c3722Schristos 					HX(mm[m].p = mmap(NULL,
3443b6c3722Schristos 					    mm[m].npg * pgs,
3453b6c3722Schristos 					    PROT_READ|PROT_WRITE,
3463b6c3722Schristos 					    MAP_PRIVATE|MAP_ANON, -1,
3473b6c3722Schristos 					    (off_t)0), mm[m].p);
3483b6c3722Schristos 					if (mm[m].p != MAP_FAILED) {
3493b6c3722Schristos 						size_t mo;
3503b6c3722Schristos 
3513b6c3722Schristos 						/* Touch some memory... */
3523b6c3722Schristos 						p = mm[m].p;
3533b6c3722Schristos 						mo = cnt %
3543b6c3722Schristos 						    (mm[m].npg * pgs - 1);
3553b6c3722Schristos 						p[mo] = 1;
3563b6c3722Schristos 						cnt += (int)((long)(mm[m].p)
3573b6c3722Schristos 						    / pgs);
3583b6c3722Schristos 					}
3593b6c3722Schristos 
3603b6c3722Schristos 					/* Check cnts and times... */
3613b6c3722Schristos 					for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]);
3623b6c3722Schristos 					    ii++) {
3633b6c3722Schristos 						HX((e = clock_gettime(cl[ii],
3643b6c3722Schristos 						    &ts)) == -1, ts);
3653b6c3722Schristos 						if (e != -1)
3663b6c3722Schristos 							cnt += (int)ts.tv_nsec;
3673b6c3722Schristos 					}
3683b6c3722Schristos 
3693b6c3722Schristos 					HX((e = getrusage(RUSAGE_SELF,
3703b6c3722Schristos 					    &ru)) == -1, ru);
3713b6c3722Schristos 					if (e != -1) {
3723b6c3722Schristos 						cnt += (int)ru.ru_utime.tv_sec;
3733b6c3722Schristos 						cnt += (int)ru.ru_utime.tv_usec;
3743b6c3722Schristos 					}
3753b6c3722Schristos 				}
3763b6c3722Schristos 
3773b6c3722Schristos 				for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
3783b6c3722Schristos 					if (mm[m].p != MAP_FAILED)
3793b6c3722Schristos 						munmap(mm[m].p, mm[m].npg * pgs);
3803b6c3722Schristos 					mm[m].p = MAP_FAILED;
3813b6c3722Schristos 				}
3823b6c3722Schristos 
3833b6c3722Schristos 				HX(stat(".", &st) == -1, st);
3843b6c3722Schristos 				HX(statvfs(".", &stvfs) == -1, stvfs);
3853b6c3722Schristos 
3863b6c3722Schristos 				HX(stat("/", &st) == -1, st);
3873b6c3722Schristos 				HX(statvfs("/", &stvfs) == -1, stvfs);
3883b6c3722Schristos 
3893b6c3722Schristos 				HX((e = fstat(0, &st)) == -1, st);
3903b6c3722Schristos 				if (e == -1) {
3913b6c3722Schristos 					if (S_ISREG(st.st_mode) ||
3923b6c3722Schristos 					    S_ISFIFO(st.st_mode) ||
3933b6c3722Schristos 					    S_ISSOCK(st.st_mode)) {
3943b6c3722Schristos 						HX(fstatvfs(0, &stvfs) == -1,
3953b6c3722Schristos 						    stvfs);
3963b6c3722Schristos 						HX((off = lseek(0, (off_t)0,
3973b6c3722Schristos 						    SEEK_CUR)) < 0, off);
3983b6c3722Schristos 					}
3993b6c3722Schristos 					if (S_ISCHR(st.st_mode)) {
4003b6c3722Schristos 						HX(tcgetattr(0, &tios) == -1,
4013b6c3722Schristos 						    tios);
4023b6c3722Schristos 					} else if (S_ISSOCK(st.st_mode)) {
4033b6c3722Schristos 						memset(&ss, 0, sizeof ss);
4043b6c3722Schristos 						ssl = sizeof(ss);
4053b6c3722Schristos 						HX(getpeername(0,
4063b6c3722Schristos 						    (void *)&ss, &ssl) == -1,
4073b6c3722Schristos 						    ss);
4083b6c3722Schristos 					}
4093b6c3722Schristos 				}
4103b6c3722Schristos 
4113b6c3722Schristos 				HX((e = getrusage(RUSAGE_CHILDREN,
4123b6c3722Schristos 				    &ru)) == -1, ru);
4133b6c3722Schristos 				if (e != -1) {
4143b6c3722Schristos 					cnt += (int)ru.ru_utime.tv_sec;
4153b6c3722Schristos 					cnt += (int)ru.ru_utime.tv_usec;
4163b6c3722Schristos 				}
4173b6c3722Schristos 			} else {
4183b6c3722Schristos 				/* Subsequent hashes absorb previous result */
4193b6c3722Schristos 				HD(results);
4203b6c3722Schristos 			}
4213b6c3722Schristos 
4223b6c3722Schristos 			HX((e = gettimeofday(&tv, NULL)) == -1, tv);
4233b6c3722Schristos 			if (e != -1) {
4243b6c3722Schristos 				cnt += (int)tv.tv_sec;
4253b6c3722Schristos 				cnt += (int)tv.tv_usec;
4263b6c3722Schristos 			}
4273b6c3722Schristos 
4283b6c3722Schristos 			HD(cnt);
4293b6c3722Schristos 		}
4303b6c3722Schristos 		SHA512_Final(results, &ctx);
4313b6c3722Schristos 		memcpy((char *)buf + i, results, min(sizeof(results), len - i));
4323b6c3722Schristos 		i += min(sizeof(results), len - i);
4333b6c3722Schristos 	}
434d0eba39bSchristos 	memset(results, 0, sizeof results);
435d0eba39bSchristos 	if (gotdata(buf, len) == 0) {
4363b6c3722Schristos 		errno = save_errno;
437d0eba39bSchristos 		return 0;		/* satisfied */
438d0eba39bSchristos 	}
439d0eba39bSchristos 	errno = EIO;
440d0eba39bSchristos 	return -1;
4413b6c3722Schristos }
442