All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] tools: use cryptographically safe RNG
@ 2024-11-02 16:32 Heinrich Schuchardt
  2024-11-14 17:26 ` Tom Rini
  0 siblings, 1 reply; 12+ messages in thread
From: Heinrich Schuchardt @ 2024-11-02 16:32 UTC (permalink / raw)
  To: Tom Rini
  Cc: Hugo Cornelis, Simon Glass, Paul-Erwan Rio, Alexander Kochetkov,
	Alexander Dahl, Philippe Reynes, u-boot, Heinrich Schuchardt

The PRNG implementing the random() function only has 2^31 states and
therefore is unsafe to use for cryptography. Use arc4random() instead.

Fixes: cc34f04efd63 ("tools: image-host.c: use random instead of rand")
Addresses-Coverity-ID: 312953 Calling risky function
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
---
 tools/image-host.c | 35 +++--------------------------------
 1 file changed, 3 insertions(+), 32 deletions(-)

diff --git a/tools/image-host.c b/tools/image-host.c
index 5e01b853c50..e24e053825b 100644
--- a/tools/image-host.c
+++ b/tools/image-host.c
@@ -364,36 +364,6 @@ static int fit_image_read_key_iv_data(const char *keydir, const char *key_iv_nam
 	return ret;
 }
 
-static int get_random_data(void *data, int size)
-{
-	unsigned char *tmp = data;
-	struct timespec date;
-	int i, ret;
-
-	if (!tmp) {
-		fprintf(stderr, "%s: pointer data is NULL\n", __func__);
-		ret = -1;
-		goto out;
-	}
-
-	ret = clock_gettime(CLOCK_MONOTONIC, &date);
-	if (ret) {
-		fprintf(stderr, "%s: clock_gettime has failed (%s)\n", __func__,
-			strerror(errno));
-		goto out;
-	}
-
-	srandom(date.tv_nsec);
-
-	for (i = 0; i < size; i++) {
-		*tmp = random() & 0xff;
-		tmp++;
-	}
-
- out:
-	return ret;
-}
-
 static int fit_image_setup_cipher(struct image_cipher_info *info,
 				  const char *keydir, void *fit,
 				  const char *image_name, int image_noffset,
@@ -465,8 +435,9 @@ static int fit_image_setup_cipher(struct image_cipher_info *info,
 		if (ret < 0)
 			goto out;
 	} else {
-		/* Generate an ramdom IV */
-		ret = get_random_data((void *)info->iv, info->cipher->iv_len);
+		/* Generate a random IV */
+		arc4random_buf((void *)info->iv, info->cipher->iv_len);
+		ret = 0;
 	}
 
  out:
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-11-15 22:48 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-02 16:32 [PATCH 1/1] tools: use cryptographically safe RNG Heinrich Schuchardt
2024-11-14 17:26 ` Tom Rini
2024-11-14 17:35   ` Heinrich Schuchardt
2024-11-14 17:39     ` Tom Rini
2024-11-15  0:21       ` Mark Kettenis
2024-11-15  7:18         ` Rasmus Villemoes
2024-11-15  8:34           ` Heinrich Schuchardt
2024-11-15 10:41             ` Rasmus Villemoes
2024-11-15 11:19             ` Torsten Duwe
2024-11-15 14:32           ` Mark Kettenis
2024-11-15 19:21             ` Rasmus Villemoes
2024-11-15 22:48               ` Mark Kettenis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.