Skip to content

Commit 101e99c

Browse files
ebiggersherbertx
authored andcommitted
crypto: testmgr - generate power-of-2 lengths more often
Implementations of hash functions often have special cases when lengths are a multiple of the hash function's internal block size (e.g. 64 for SHA-256, 128 for SHA-512). Currently, when the fuzz testing code generates lengths, it doesn't prefer any length mod 64 over any other. This limits the coverage of these special cases. Therefore, this patch updates the fuzz testing code to generate power-of-2 lengths and divide messages exactly in half a bit more often. Reviewed-by: Sami Tolvanen <[email protected]> Acked-by: Ard Biesheuvel <[email protected]> Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent dd52b5e commit 101e99c

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

crypto/testmgr.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -916,14 +916,20 @@ static unsigned int generate_random_length(struct rnd_state *rng,
916916

917917
switch (prandom_u32_below(rng, 4)) {
918918
case 0:
919-
return len % 64;
919+
len %= 64;
920+
break;
920921
case 1:
921-
return len % 256;
922+
len %= 256;
923+
break;
922924
case 2:
923-
return len % 1024;
925+
len %= 1024;
926+
break;
924927
default:
925-
return len;
928+
break;
926929
}
930+
if (len && prandom_u32_below(rng, 4) == 0)
931+
len = rounddown_pow_of_two(len);
932+
return len;
927933
}
928934

929935
/* Flip a random bit in the given nonempty data buffer */
@@ -1019,6 +1025,8 @@ static char *generate_random_sgl_divisions(struct rnd_state *rng,
10191025

10201026
if (div == &divs[max_divs - 1] || prandom_bool(rng))
10211027
this_len = remaining;
1028+
else if (prandom_u32_below(rng, 4) == 0)
1029+
this_len = (remaining + 1) / 2;
10221030
else
10231031
this_len = prandom_u32_inclusive(rng, 1, remaining);
10241032
div->proportion_of_total = this_len;

0 commit comments

Comments
 (0)