diff options
| author | Tom Rini <[email protected]> | 2025-02-24 17:15:14 -0600 |
|---|---|---|
| committer | Tom Rini <[email protected]> | 2025-02-24 17:15:14 -0600 |
| commit | 3ecda19009ebbe46a64b0629f8b64173c7a551c0 (patch) | |
| tree | ad8bc5289901745c546cafaa3a7cab4577c298c3 /tools | |
| parent | 523a56cc54637a0c04a1e87c262599faf26d7d69 (diff) | |
| parent | dc0ee458f1afae4cb5c8a7b2c875bb24ffdf71ca (diff) | |
Merge tag 'v2025.04-rc3' into next
Prepare v2025.04-rc3
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/image-host.c | 53 |
1 files changed, 33 insertions, 20 deletions
diff --git a/tools/image-host.c b/tools/image-host.c index 05d8c898209..14e8bd52a65 100644 --- a/tools/image-host.c +++ b/tools/image-host.c @@ -364,33 +364,46 @@ 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) +/** + * get_random_data() - fill buffer with random data + * + * There is no common cryptographically safe function in Linux and BSD. + * Hence directly access the /dev/urandom PRNG. + * + * @data: buffer to fill + * @size: buffer size + */ +static int get_random_data(void *data, size_t 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; - } + int fd; + int ret; - ret = clock_gettime(CLOCK_MONOTONIC, &date); - if (ret) { - fprintf(stderr, "%s: clock_gettime has failed (%s)\n", __func__, - strerror(errno)); - goto out; + fd = open("/dev/urandom", O_RDONLY); + if (fd < 0) { + perror("Failed to open /dev/urandom"); + return -1; } - srandom(date.tv_nsec); + while (size) { + ssize_t count; - for (i = 0; i < size; i++) { - *tmp = random() & 0xff; - tmp++; + count = read(fd, data, size); + if (count < 0) { + if (errno == EINTR) { + continue; + } else { + perror("Failed to read from /dev/urandom"); + ret = -1; + goto out; + } + } + data += count; + size -= count; } + ret = 0; +out: + close(fd); - out: return ret; } |
