git: 0e9cdcc5141e - stable/14 - random: Avoid magic numbers

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Sun, 29 Sep 2024 15:25:12 UTC
The branch stable/14 has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=0e9cdcc5141e82ac3d9fa8331c3a4ed19933afa9

commit 0e9cdcc5141e82ac3d9fa8331c3a4ed19933afa9
Author:     Colin Percival <cperciva@FreeBSD.org>
AuthorDate: 2024-09-18 05:12:04 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2024-09-29 15:24:52 +0000

    random: Avoid magic numbers
    
    Move RANDOM_FORTUNA_{NPOOLS,DEFPOOLSIZE} from fortuna.c to fortuna.h
    and use RANDOM_FORTUNA_DEFPOOLSIZE in random_harvestq.c rather than
    having a magic (albeit explained in a comment) number.  The NPOOLS
    value will be used in a later commit.
    
    Reviewed by:    cem
    MFC after:      1 week
    Sponsored by:   Amazon
    Differential Revision:  https://reviews.freebsd.org/D46693
    
    (cherry picked from commit 32fce09268ddd97efb4412529ba57293554c5985)
---
 sys/dev/random/fortuna.c         | 2 --
 sys/dev/random/fortuna.h         | 4 ++++
 sys/dev/random/random_harvestq.c | 9 +++++----
 3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/sys/dev/random/fortuna.c b/sys/dev/random/fortuna.c
index 11b2dcde1db8..ec27acc63b29 100644
--- a/sys/dev/random/fortuna.c
+++ b/sys/dev/random/fortuna.c
@@ -72,8 +72,6 @@
 #include <dev/random/fortuna.h>
 
 /* Defined in FS&K */
-#define	RANDOM_FORTUNA_NPOOLS 32		/* The number of accumulation pools */
-#define	RANDOM_FORTUNA_DEFPOOLSIZE 64		/* The default pool size/length for a (re)seed */
 #define	RANDOM_FORTUNA_MAX_READ (1 << 20)	/* Max bytes from AES before rekeying */
 #define	RANDOM_FORTUNA_BLOCKS_PER_KEY (1 << 16)	/* Max blocks from AES before rekeying */
 CTASSERT(RANDOM_FORTUNA_BLOCKS_PER_KEY * RANDOM_BLOCKSIZE ==
diff --git a/sys/dev/random/fortuna.h b/sys/dev/random/fortuna.h
index cb4683514989..7378edb9238c 100644
--- a/sys/dev/random/fortuna.h
+++ b/sys/dev/random/fortuna.h
@@ -27,6 +27,10 @@
 #ifndef SYS_DEV_RANDOM_FORTUNA_H_INCLUDED
 #define	SYS_DEV_RANDOM_FORTUNA_H_INCLUDED
 
+/* Defined in FS&K */
+#define	RANDOM_FORTUNA_NPOOLS 32		/* The number of accumulation pools */
+#define	RANDOM_FORTUNA_DEFPOOLSIZE 64		/* The default pool size/length for a (re)seed */
+
 #ifdef _KERNEL
 typedef struct mtx mtx_t;
 #define	RANDOM_RESEED_INIT_LOCK(x)		mtx_init(&fortuna_state.fs_mtx, "reseed mutex", NULL, MTX_DEF)
diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c
index 09b81950281b..cb01f7b77195 100644
--- a/sys/dev/random/random_harvestq.c
+++ b/sys/dev/random/random_harvestq.c
@@ -55,6 +55,7 @@
 #include <crypto/rijndael/rijndael-api-fst.h>
 #include <crypto/sha2/sha256.h>
 
+#include <dev/random/fortuna.h>
 #include <dev/random/hash.h>
 #include <dev/random/randomdev.h>
 #include <dev/random/random_harvestq.h>
@@ -260,8 +261,8 @@ random_sources_feed(void)
 	 * stuck for a few seconds with random_kthread gradually collecting a
 	 * small chunk of entropy every 1 / RANDOM_KTHREAD_HZ seconds.
 	 *
-	 * The value 64 below is RANDOM_FORTUNA_DEFPOOLSIZE, i.e. chosen to
-	 * fill Fortuna's pools in the default configuration.  With another
+	 * We collect RANDOM_FORTUNA_DEFPOOLSIZE bytes per pool, i.e. enough
+	 * to fill Fortuna's pools in the default configuration.  With another
 	 * PRNG or smaller pools for Fortuna, we might collect more entropy
 	 * than needed to fill the pools, but this is harmless; alternatively,
 	 * a different PRNG, larger pools, or fast entropy sources which are
@@ -271,8 +272,8 @@ random_sources_feed(void)
 	 * try again for a large amount of entropy.
 	 */
 	if (!p_random_alg_context->ra_seeded())
-		npools = howmany(p_random_alg_context->ra_poolcount * 64,
-		    sizeof(entropy));
+		npools = howmany(p_random_alg_context->ra_poolcount *
+		    RANDOM_FORTUNA_DEFPOOLSIZE, sizeof(entropy));
 
 	/*
 	 * Step over all of live entropy sources, and feed their output