From 51f59d99602fb922af863ded116b420c942501b4 Mon Sep 17 00:00:00 2001
From: beck <>
Date: Tue, 8 Jul 2014 09:24:27 +0000
Subject: unbreak last commit - but same intent, make re-seed less expensive

---
 src/lib/libcrypto/arc4random/getentropy_linux.c | 28 ++++++++++++++-----------
 src/lib/libcrypto/crypto/getentropy_linux.c     | 28 ++++++++++++++-----------
 2 files changed, 32 insertions(+), 24 deletions(-)

(limited to 'src/lib')

diff --git a/src/lib/libcrypto/arc4random/getentropy_linux.c b/src/lib/libcrypto/arc4random/getentropy_linux.c
index a7f5991201..a76604fe77 100644
--- a/src/lib/libcrypto/arc4random/getentropy_linux.c
+++ b/src/lib/libcrypto/arc4random/getentropy_linux.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: getentropy_linux.c,v 1.15 2014/07/08 08:33:43 deraadt Exp $	*/
+/*	$OpenBSD: getentropy_linux.c,v 1.16 2014/07/08 09:24:27 beck Exp $	*/
 
 /*
  * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
@@ -288,7 +288,7 @@ static int
 getentropy_fallback(void *buf, size_t len)
 {
 	uint8_t results[SHA512_DIGEST_LENGTH];
-	int save_errno = errno, e, m, pgs = getpagesize(), repeat = 0;
+	int save_errno = errno, e, m, pgs = getpagesize(), faster = 0, repeat;
 	static int cnt;
 	struct timespec ts;
 	struct timeval tv;
@@ -298,19 +298,21 @@ getentropy_fallback(void *buf, size_t len)
 	SHA512_CTX ctx;
 	static pid_t lastpid;
 	pid_t pid;
-	size_t i, ii;
+	size_t i, j, ii;
 	char *p;
 
 	pid = getpid();
-	if (lastpid == getpid())
-		repeat = REPEAT - 1;
-	else
+	if (lastpid == pid) {
+		faster = 1;
+		repeat = 2;
+	} else {
+		faster = 0;
 		lastpid = pid;
-
+		repeat = REPEAT;
+	}
 	for (i = 0; i < len; ) {
 		SHA512_Init(&ctx);
-		for (; repeat < REPEAT; repeat++) {
-
+		for (j = 0; j < repeat; j++) {
 			HX((e = gettimeofday(&tv, NULL)) == -1, tv);
 			if (e != -1) {
 				cnt += (int)tv.tv_sec;
@@ -326,9 +328,11 @@ getentropy_fallback(void *buf, size_t len)
 			HX((pid = getpgid(0)) == -1, pid);
 			HX((m = getpriority(0, 0)) == -1, m);
 
-			ts.tv_sec = 0;
-			ts.tv_nsec = 1;
-			(void) nanosleep(&ts, NULL);
+			if (!faster) {
+				ts.tv_sec = 0;
+				ts.tv_nsec = 1;
+				(void) nanosleep(&ts, NULL);
+			}
 
 			HX(sigpending(&sigset) == -1, sigset);
 			HX(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1,
diff --git a/src/lib/libcrypto/crypto/getentropy_linux.c b/src/lib/libcrypto/crypto/getentropy_linux.c
index a7f5991201..a76604fe77 100644
--- a/src/lib/libcrypto/crypto/getentropy_linux.c
+++ b/src/lib/libcrypto/crypto/getentropy_linux.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: getentropy_linux.c,v 1.15 2014/07/08 08:33:43 deraadt Exp $	*/
+/*	$OpenBSD: getentropy_linux.c,v 1.16 2014/07/08 09:24:27 beck Exp $	*/
 
 /*
  * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
@@ -288,7 +288,7 @@ static int
 getentropy_fallback(void *buf, size_t len)
 {
 	uint8_t results[SHA512_DIGEST_LENGTH];
-	int save_errno = errno, e, m, pgs = getpagesize(), repeat = 0;
+	int save_errno = errno, e, m, pgs = getpagesize(), faster = 0, repeat;
 	static int cnt;
 	struct timespec ts;
 	struct timeval tv;
@@ -298,19 +298,21 @@ getentropy_fallback(void *buf, size_t len)
 	SHA512_CTX ctx;
 	static pid_t lastpid;
 	pid_t pid;
-	size_t i, ii;
+	size_t i, j, ii;
 	char *p;
 
 	pid = getpid();
-	if (lastpid == getpid())
-		repeat = REPEAT - 1;
-	else
+	if (lastpid == pid) {
+		faster = 1;
+		repeat = 2;
+	} else {
+		faster = 0;
 		lastpid = pid;
-
+		repeat = REPEAT;
+	}
 	for (i = 0; i < len; ) {
 		SHA512_Init(&ctx);
-		for (; repeat < REPEAT; repeat++) {
-
+		for (j = 0; j < repeat; j++) {
 			HX((e = gettimeofday(&tv, NULL)) == -1, tv);
 			if (e != -1) {
 				cnt += (int)tv.tv_sec;
@@ -326,9 +328,11 @@ getentropy_fallback(void *buf, size_t len)
 			HX((pid = getpgid(0)) == -1, pid);
 			HX((m = getpriority(0, 0)) == -1, m);
 
-			ts.tv_sec = 0;
-			ts.tv_nsec = 1;
-			(void) nanosleep(&ts, NULL);
+			if (!faster) {
+				ts.tv_sec = 0;
+				ts.tv_nsec = 1;
+				(void) nanosleep(&ts, NULL);
+			}
 
 			HX(sigpending(&sigset) == -1, sigset);
 			HX(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1,
-- 
cgit v1.2.3-55-g6feb