aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2022-05-01 17:01:14 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2022-05-01 17:02:20 +0200
commita157c4c978d3e984f3cb7e2fc02d5ce428d5f82e (patch)
tree29f1786eae1db4ff8645f19cf5a26054c3cb7cc1
parent54867fec12e23a0606fd74e999ee30e34eea6a74 (diff)
downloadbusybox-w32-a157c4c978d3e984f3cb7e2fc02d5ce428d5f82e.tar.gz
busybox-w32-a157c4c978d3e984f3cb7e2fc02d5ce428d5f82e.tar.bz2
busybox-w32-a157c4c978d3e984f3cb7e2fc02d5ce428d5f82e.zip
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.c36
1 files changed, 15 insertions, 21 deletions
diff --git a/util-linux/seedrng.c b/util-linux/seedrng.c
index 390dec12a..8c81835f6 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)
112 return is_creditable; 112 return is_creditable;
113} 113}
114 114
115static void seed_rng(uint8_t *seed, size_t len, bool credit) 115static void seed_from_file_if_exists(const char *filename, int dfd, bool credit, sha256_ctx_t *hash)
116{ 116{
117 struct { 117 struct {
118 int entropy_count; 118 int entropy_count;
119 int buf_size; 119 int buf_size;
120 uint8_t buffer[MAX_SEED_LEN]; 120 uint8_t buf[MAX_SEED_LEN];
121 } req; 121 } req;
122 int random_fd;
123
124 req.entropy_count = credit ? len * 8 : 0;
125 req.buf_size = len;
126 memcpy(req.buffer, seed, len);
127
128 random_fd = xopen("/dev/urandom", O_RDONLY);
129 xioctl(random_fd, RNDADDENTROPY, &req);
130 if (ENABLE_FEATURE_CLEAN_UP)
131 close(random_fd);
132}
133
134static void seed_from_file_if_exists(const char *filename, int dfd, bool credit, sha256_ctx_t *hash)
135{
136 uint8_t seed[MAX_SEED_LEN];
137 ssize_t seed_len; 122 ssize_t seed_len;
138 123
139 seed_len = open_read_close(filename, seed, sizeof(seed)); 124 seed_len = open_read_close(filename, req.buf, sizeof(req.buf));
140 if (seed_len < 0) { 125 if (seed_len < 0) {
141 if (errno != ENOENT) 126 if (errno != ENOENT)
142 bb_perror_msg_and_die("can't read '%s'", filename); 127 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,
144 } 129 }
145 xunlink(filename); 130 xunlink(filename);
146 if (seed_len != 0) { 131 if (seed_len != 0) {
132 int fd;
133
147 /* We are going to use this data to seed the RNG: 134 /* We are going to use this data to seed the RNG:
148 * we believe it to genuinely containing entropy. 135 * we believe it to genuinely containing entropy.
149 * If this just-unlinked file survives 136 * If this just-unlinked file survives
@@ -156,10 +143,17 @@ static void seed_from_file_if_exists(const char *filename, int dfd, bool credit,
156 143
157//Length is not random, and taking its address spills variable to stack 144//Length is not random, and taking its address spills variable to stack
158// sha256_hash(hash, &seed_len, sizeof(seed_len)); 145// sha256_hash(hash, &seed_len, sizeof(seed_len));
159 sha256_hash(hash, seed, seed_len); 146 sha256_hash(hash, req.buf, seed_len);
147
148 req.buf_size = seed_len;
149 seed_len *= 8;
150 req.entropy_count = credit ? seed_len : 0;
160 printf("Seeding %u bits %s crediting\n", 151 printf("Seeding %u bits %s crediting\n",
161 (unsigned)seed_len * 8, credit ? "and" : "without"); 152 (unsigned)seed_len, credit ? "and" : "without");
162 seed_rng(seed, seed_len, credit); 153 fd = xopen("/dev/urandom", O_RDONLY);
154 xioctl(fd, RNDADDENTROPY, &req);
155 if (ENABLE_FEATURE_CLEAN_UP)
156 close(fd);
163 } 157 }
164} 158}
165 159