lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20250704125233.2653779-5-theil.markus@gmail.com>
Date: Fri,  4 Jul 2025 14:52:33 +0200
From: Markus Theil <theil.markus@...il.com>
To: netdev@...r.kernel.org,
	linux-kernel@...r.kernel.org
Cc: davem@...emloft.net,
	akpm@...ux-foundation.org,
	Jason@...c4.com,
	Markus Theil <theil.markus@...il.com>
Subject: [PATCH v2 4/4] test_hash.c: replace custom PRNG by prandom

Use default PRNG, as there is no real need
for a custom solution here.

We know the sequence provided by our seed
and do not need to do bit magic in order
to obtain a non-zero byte. Just iterate a
small number of times, if needed.

Signed-off-by: Markus Theil <theil.markus@...il.com>
---
 lib/tests/test_hash.c | 40 +++++++++++++---------------------------
 1 file changed, 13 insertions(+), 27 deletions(-)

diff --git a/lib/tests/test_hash.c b/lib/tests/test_hash.c
index a7af39662a0a..308446ea3431 100644
--- a/lib/tests/test_hash.c
+++ b/lib/tests/test_hash.c
@@ -17,39 +17,21 @@
 #include <linux/compiler.h>
 #include <linux/types.h>
 #include <linux/module.h>
+#include <linux/prandom.h>
 #include <linux/hash.h>
 #include <linux/stringhash.h>
 #include <kunit/test.h>
 
-/* 32-bit XORSHIFT generator.  Seed must not be zero. */
-static u32 __attribute_const__
-xorshift(u32 seed)
-{
-	seed ^= seed << 13;
-	seed ^= seed >> 17;
-	seed ^= seed << 5;
-	return seed;
-}
-
-/* Given a non-zero x, returns a non-zero byte. */
-static u8 __attribute_const__
-mod255(u32 x)
-{
-	x = (x & 0xffff) + (x >> 16);	/* 1 <= x <= 0x1fffe */
-	x = (x & 0xff) + (x >> 8);	/* 1 <= x <= 0x2fd */
-	x = (x & 0xff) + (x >> 8);	/* 1 <= x <= 0x100 */
-	x = (x & 0xff) + (x >> 8);	/* 1 <= x <= 0xff */
-	return x;
-}
-
 /* Fill the buffer with non-zero bytes. */
-static void fill_buf(char *buf, size_t len, u32 seed)
+static void fill_buf(char *buf, size_t len, struct rnd_state *prng)
 {
 	size_t i;
 
 	for (i = 0; i < len; i++) {
-		seed = xorshift(seed);
-		buf[i] = mod255(seed);
+		/* we know our seeds, no need to worry about endless runtime */
+		do {
+			buf[i] = (u8) prandom_u32_state(prng);
+		} while (!buf[i]);
 	}
 }
 
@@ -143,11 +125,13 @@ test_int_hash(struct kunit *test, unsigned long long h64, u32 hash_or[2][33])
 
 static void test_string_or(struct kunit *test)
 {
-	char buf[SIZE+1];
+	struct rnd_state prng;
 	u32 string_or = 0;
+	char buf[SIZE+1];
 	int i, j;
 
-	fill_buf(buf, SIZE, 1);
+	prandom_seed_state(&prng, 0x1);
+	fill_buf(buf, SIZE, &prng);
 
 	/* Test every possible non-empty substring in the buffer. */
 	for (j = SIZE; j > 0; --j) {
@@ -171,9 +155,11 @@ static void test_hash_or(struct kunit *test)
 	char buf[SIZE+1];
 	u32 hash_or[2][33] = { { 0, } };
 	unsigned long long h64 = 0;
+	struct rnd_state prng;
 	int i, j;
 
-	fill_buf(buf, SIZE, 1);
+	prandom_seed_state(&prng, 0x1);
+	fill_buf(buf, SIZE, &prng);
 
 	/* Test every possible non-empty substring in the buffer. */
 	for (j = SIZE; j > 0; --j) {
-- 
2.49.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ