diff options
author | Bernhard Reutner-Fischer | 2022-05-01 17:01:14 +0200 |
---|---|---|
committer | Denys Vlasenko | 2022-05-01 17:02:20 +0200 |
commit | a157c4c978d3e984f3cb7e2fc02d5ce428d5f82e (patch) | |
tree | 29f1786eae1db4ff8645f19cf5a26054c3cb7cc1 | |
parent | 54867fec12e23a0606fd74e999ee30e34eea6a74 (diff) | |
download | busybox-a157c4c978d3e984f3cb7e2fc02d5ce428d5f82e.zip busybox-a157c4c978d3e984f3cb7e2fc02d5ce428d5f82e.tar.gz |
seedrng: manually inline seed_rng
We can now remove a separate buffer
function old new delta
seedrng_main 930 884 -46
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | util-linux/seedrng.c | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/util-linux/seedrng.c b/util-linux/seedrng.c index 390dec1..8c81835 100644 --- a/util-linux/seedrng.c +++ b/util-linux/seedrng.c @@ -112,31 +112,16 @@ static bool read_new_seed(uint8_t *seed, size_t len) return is_creditable; } -static void seed_rng(uint8_t *seed, size_t len, bool credit) +static void seed_from_file_if_exists(const char *filename, int dfd, bool credit, sha256_ctx_t *hash) { struct { int entropy_count; int buf_size; - uint8_t buffer[MAX_SEED_LEN]; + uint8_t buf[MAX_SEED_LEN]; } req; - int random_fd; - - req.entropy_count = credit ? len * 8 : 0; - req.buf_size = len; - memcpy(req.buffer, seed, len); - - random_fd = xopen("/dev/urandom", O_RDONLY); - xioctl(random_fd, RNDADDENTROPY, &req); - if (ENABLE_FEATURE_CLEAN_UP) - close(random_fd); -} - -static void seed_from_file_if_exists(const char *filename, int dfd, bool credit, sha256_ctx_t *hash) -{ - uint8_t seed[MAX_SEED_LEN]; ssize_t seed_len; - seed_len = open_read_close(filename, seed, sizeof(seed)); + seed_len = open_read_close(filename, req.buf, sizeof(req.buf)); if (seed_len < 0) { if (errno != ENOENT) bb_perror_msg_and_die("can't read '%s'", filename); @@ -144,6 +129,8 @@ static void seed_from_file_if_exists(const char *filename, int dfd, bool credit, } xunlink(filename); if (seed_len != 0) { + int fd; + /* We are going to use this data to seed the RNG: * we believe it to genuinely containing entropy. * If this just-unlinked file survives @@ -156,10 +143,17 @@ static void seed_from_file_if_exists(const char *filename, int dfd, bool credit, //Length is not random, and taking its address spills variable to stack // sha256_hash(hash, &seed_len, sizeof(seed_len)); - sha256_hash(hash, seed, seed_len); + sha256_hash(hash, req.buf, seed_len); + + req.buf_size = seed_len; + seed_len *= 8; + req.entropy_count = credit ? seed_len : 0; printf("Seeding %u bits %s crediting\n", - (unsigned)seed_len * 8, credit ? "and" : "without"); - seed_rng(seed, seed_len, credit); + (unsigned)seed_len, credit ? "and" : "without"); + fd = xopen("/dev/urandom", O_RDONLY); + xioctl(fd, RNDADDENTROPY, &req); + if (ENABLE_FEATURE_CLEAN_UP) + close(fd); } } |