4d7ada7
Stephan Mueller reported to me recently a error in random number generation in
4d7ada7
the ansi cprng. If several small requests are made that are less than the
4d7ada7
instances block size, the remainder for loop code doesn't increment
4d7ada7
rand_data_valid in the last iteration, meaning that the last bytes in the
4d7ada7
rand_data buffer gets reused on the subsequent smaller-than-a-block request for
4d7ada7
random data.
4d7ada7
4d7ada7
The fix is pretty easy, just re-code the for loop to make sure that
4d7ada7
rand_data_valid gets incremented appropriately
4d7ada7
4d7ada7
Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
4d7ada7
Reported-by: Stephan Mueller <stephan.mueller@atsec.com>
4d7ada7
CC: Stephan Mueller <stephan.mueller@atsec.com>
4d7ada7
CC: Petr Matousek <pmatouse@redhat.com>
4d7ada7
CC: Herbert Xu <herbert@gondor.apana.org.au>
4d7ada7
CC: "David S. Miller" <davem@davemloft.net>
4d7ada7
---
4d7ada7
 crypto/ansi_cprng.c | 4 ++--
4d7ada7
 1 file changed, 2 insertions(+), 2 deletions(-)
4d7ada7
4d7ada7
diff --git a/crypto/ansi_cprng.c b/crypto/ansi_cprng.c
4d7ada7
index c0bb377..666f196 100644
4d7ada7
--- a/crypto/ansi_cprng.c
4d7ada7
+++ b/crypto/ansi_cprng.c
4d7ada7
@@ -230,11 +230,11 @@ remainder:
4d7ada7
 	 */
4d7ada7
 	if (byte_count < DEFAULT_BLK_SZ) {
4d7ada7
 empty_rbuf:
4d7ada7
-		for (; ctx->rand_data_valid < DEFAULT_BLK_SZ;
4d7ada7
-			ctx->rand_data_valid++) {
4d7ada7
+		while (ctx->rand_data_valid < DEFAULT_BLK_SZ) {
4d7ada7
 			*ptr = ctx->rand_data[ctx->rand_data_valid];
4d7ada7
 			ptr++;
4d7ada7
 			byte_count--;
4d7ada7
+			ctx->rand_data_valid++;
4d7ada7
 			if (byte_count == 0)
4d7ada7
 				goto done;
4d7ada7
 		}
4d7ada7
-- 
4d7ada7
1.8.3.1