Skip to contentSkip to conversation

crypto/testmgr.c: use prandom_u32_max() & prandom_bytes()

RFC PATCH In review George Spelvin 1 participant 1 message Mar 28, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 14/50 crypto/testmgr.c: use prandom_u32_max() & prandom_bytes()

...in a couple of places where they're appropriate.

There are many other places where successive code blocks make calls like prandom_u32() % 2 followed immediately by prandom_u32() % 4. This could be easily written to use three bits of one call, but at some cost in clarity and obvious-correctness, which is more important that efficiency in self-test code.

---
 crypto/testmgr.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
crypto/testmgr.c +2 -3
index e8f21f7348a48..bc9252768bdba 100644
@@ -770,7 +770,7 @@ static void mutate_buffer(u8 *buf, size_t count)
 	if (prandom_u32() % 4 == 0) {
 		num_flips = min_t(size_t, 1 << (prandom_u32() % 8), count * 8);
 		for (i = 0; i < num_flips; i++) {
-			pos = prandom_u32() % (count * 8);
+			pos = prandom_u32_max(count * 8);
 			buf[pos / 8] ^= 1 << (pos % 8);
 		}
 	}
@@ -821,8 +821,7 @@ static void generate_random_bytes(u8 *buf, size_t count)
 		break;
 	default:
 		/* Fully random bytes */
-		for (i = 0; i < count; i++)
-			buf[i] = (u8)prandom_u32();
+		prandom_bytes(buf, count);
 	}
 }
 
Signature · 1 line
2.26.0