Module Name:    src
Committed By:   riastradh
Date:           Mon Jun 15 01:24:21 UTC 2020

Modified Files:
        src/sys/arch/x86/x86: cpu_rng.c

Log Message:
Count down bits of entropy, not bits of data, in x86 cpu_rng.

Fixes logic in this loop for XSTORERNG on VIA CPUs, which are deemed
to have half the entropy per bit of data as RDSEED on Intel CPUs, so
that it gathers enough entropy on the first request, not on the
second request.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/x86/x86/cpu_rng.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/cpu_rng.c
diff -u src/sys/arch/x86/x86/cpu_rng.c:1.16 src/sys/arch/x86/x86/cpu_rng.c:1.17
--- src/sys/arch/x86/x86/cpu_rng.c:1.16	Mon Jun 15 01:23:44 2020
+++ src/sys/arch/x86/x86/cpu_rng.c	Mon Jun 15 01:24:20 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu_rng.c,v 1.16 2020/06/15 01:23:44 riastradh Exp $ */
+/* $NetBSD: cpu_rng.c,v 1.17 2020/06/15 01:24:20 riastradh Exp $ */
 
 /*-
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -254,7 +254,7 @@ cpu_rng_get(size_t nbytes, void *cookie)
 	uint64_t buf[2*N];
 	unsigned i, nbits = 0;
 
-	for (; nbytes; nbytes -= MIN(nbytes, sizeof buf)) {
+	while (nbytes) {
 		/*
 		 * The fraction of outputs this rejects in correct
 		 * operation is 1/2^256, which is close enough to zero
@@ -269,6 +269,7 @@ cpu_rng_get(size_t nbytes, void *cookie)
 			nbits = 0;
 		}
 		rnd_add_data_sync(&cpu_rng_source, buf, sizeof buf, nbits);
+		nbytes -= MIN(MIN(nbytes, sizeof buf), MAX(1, 8*nbits));
 	}
 #undef N
 }

Reply via email to