[PATCH 1/1] tools: use cryptographically safe RNG
Heinrich Schuchardt
heinrich.schuchardt at canonical.com
Sat Nov 2 17:32:59 CET 2024
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 at 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
More information about the U-Boot
mailing list