mirror of
https://github.com/smaeul/u-boot.git
synced 2025-10-14 04:46:01 +01:00
tools: use cryptographically safe RNG
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>
This commit is contained in:
parent
e93f11148a
commit
50e8089c1d
@ -364,33 +364,46 @@ static int fit_image_read_key_iv_data(const char *keydir, const char *key_iv_nam
|
|||||||
return ret;
|
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;
|
int fd;
|
||||||
struct timespec date;
|
int ret;
|
||||||
int i, ret;
|
|
||||||
|
|
||||||
if (!tmp) {
|
fd = open("/dev/urandom", O_RDONLY);
|
||||||
fprintf(stderr, "%s: pointer data is NULL\n", __func__);
|
if (fd < 0) {
|
||||||
|
perror("Failed to open /dev/urandom");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (size) {
|
||||||
|
ssize_t count;
|
||||||
|
|
||||||
|
count = read(fd, data, size);
|
||||||
|
if (count < 0) {
|
||||||
|
if (errno == EINTR) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
perror("Failed to read from /dev/urandom");
|
||||||
ret = -1;
|
ret = -1;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = clock_gettime(CLOCK_MONOTONIC, &date);
|
|
||||||
if (ret) {
|
|
||||||
fprintf(stderr, "%s: clock_gettime has failed (%s)\n", __func__,
|
|
||||||
strerror(errno));
|
|
||||||
goto out;
|
|
||||||
}
|
}
|
||||||
|
data += count;
|
||||||
srandom(date.tv_nsec);
|
size -= count;
|
||||||
|
|
||||||
for (i = 0; i < size; i++) {
|
|
||||||
*tmp = random() & 0xff;
|
|
||||||
tmp++;
|
|
||||||
}
|
}
|
||||||
|
ret = 0;
|
||||||
|
out:
|
||||||
|
close(fd);
|
||||||
|
|
||||||
out:
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user