diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2022-04-20 15:31:01 +0200 |
---|---|---|
committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2022-04-20 15:43:00 +0200 |
commit | 3c60711f836b151b8f361098475c3b0cd162dd17 (patch) | |
tree | 37c7b403593a49fc650c52b58720600528ad2e1d /util-linux | |
parent | 31ec481baf106cf9c6d8f34ae6a55ab1738dea6f (diff) | |
download | busybox-w32-3c60711f836b151b8f361098475c3b0cd162dd17.tar.gz busybox-w32-3c60711f836b151b8f361098475c3b0cd162dd17.tar.bz2 busybox-w32-3c60711f836b151b8f361098475c3b0cd162dd17.zip |
seedrng: remove some global variables
- Remove global variables and pass dfd by value, opened once instead of
twice, which shaves off some more bytes.
function old new delta
seedrng_main 1086 1088 +2
seed_dir 8 - -8
non_creditable_seed 8 - -8
lock_file 8 - -8
creditable_seed 8 - -8
seed_from_file_if_exists 456 426 -30
------------------------------------------------------------------------------
(add/remove: 0/4 grow/shrink: 1/1 up/down: 2/-62) Total: -60 bytes
text data bss dec hex filename
976236 4227 1848 982311 efd27 busybox_old
976208 4227 1816 982251 efceb busybox_unstripped
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Diffstat (limited to 'util-linux')
-rw-r--r-- | util-linux/seedrng.c | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/util-linux/seedrng.c b/util-linux/seedrng.c index 1bceae405..53be5048a 100644 --- a/util-linux/seedrng.c +++ b/util-linux/seedrng.c | |||
@@ -21,7 +21,7 @@ | |||
21 | */ | 21 | */ |
22 | 22 | ||
23 | //config:config SEEDRNG | 23 | //config:config SEEDRNG |
24 | //config: bool "seedrng (2.5 kb)" | 24 | //config: bool "seedrng (2.4 kb)" |
25 | //config: default y | 25 | //config: default y |
26 | //config: help | 26 | //config: help |
27 | //config: Seed the kernel RNG from seed files, meant to be called | 27 | //config: Seed the kernel RNG from seed files, meant to be called |
@@ -76,8 +76,6 @@ | |||
76 | #define CREDITABLE_SEED_NAME "seed.credit" | 76 | #define CREDITABLE_SEED_NAME "seed.credit" |
77 | #define NON_CREDITABLE_SEED_NAME "seed.no-credit" | 77 | #define NON_CREDITABLE_SEED_NAME "seed.no-credit" |
78 | 78 | ||
79 | static char *seed_dir, *lock_file, *creditable_seed, *non_creditable_seed; | ||
80 | |||
81 | enum seedrng_lengths { | 79 | enum seedrng_lengths { |
82 | MIN_SEED_LEN = SHA256_OUTSIZE, | 80 | MIN_SEED_LEN = SHA256_OUTSIZE, |
83 | MAX_SEED_LEN = 512 | 81 | MAX_SEED_LEN = 512 |
@@ -153,18 +151,12 @@ static int seed_rng(uint8_t *seed, size_t len, bool credit) | |||
153 | return ret ? -1 : 0; | 151 | return ret ? -1 : 0; |
154 | } | 152 | } |
155 | 153 | ||
156 | static int seed_from_file_if_exists(const char *filename, bool credit, sha256_ctx_t *hash) | 154 | static int seed_from_file_if_exists(const char *filename, int dfd, bool credit, sha256_ctx_t *hash) |
157 | { | 155 | { |
158 | uint8_t seed[MAX_SEED_LEN]; | 156 | uint8_t seed[MAX_SEED_LEN]; |
159 | ssize_t seed_len; | 157 | ssize_t seed_len; |
160 | int dfd = -1, ret = 0; | 158 | int ret = 0; |
161 | 159 | ||
162 | dfd = open(seed_dir, O_DIRECTORY | O_RDONLY); | ||
163 | if (dfd < 0) { | ||
164 | ret = -errno; | ||
165 | bb_simple_perror_msg("unable to open seed directory"); | ||
166 | goto out; | ||
167 | } | ||
168 | seed_len = open_read_close(filename, seed, sizeof(seed)); | 160 | seed_len = open_read_close(filename, seed, sizeof(seed)); |
169 | if (seed_len < 0) { | 161 | if (seed_len < 0) { |
170 | if (errno != ENOENT) { | 162 | if (errno != ENOENT) { |
@@ -189,8 +181,6 @@ static int seed_from_file_if_exists(const char *filename, bool credit, sha256_ct | |||
189 | if (ret < 0) | 181 | if (ret < 0) |
190 | bb_simple_perror_msg("unable to seed"); | 182 | bb_simple_perror_msg("unable to seed"); |
191 | out: | 183 | out: |
192 | if (ENABLE_FEATURE_CLEAN_UP && dfd >= 0) | ||
193 | close(dfd); | ||
194 | errno = -ret; | 184 | errno = -ret; |
195 | return ret ? -1 : 0; | 185 | return ret ? -1 : 0; |
196 | } | 186 | } |
@@ -200,7 +190,8 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[]) | |||
200 | { | 190 | { |
201 | static const char seedrng_prefix[] = "SeedRNG v1 Old+New Prefix"; | 191 | static const char seedrng_prefix[] = "SeedRNG v1 Old+New Prefix"; |
202 | static const char seedrng_failure[] = "SeedRNG v1 No New Seed Failure"; | 192 | static const char seedrng_failure[] = "SeedRNG v1 No New Seed Failure"; |
203 | int ret, fd = -1, lock, program_ret = 0; | 193 | char *seed_dir, *lock_file, *creditable_seed, *non_creditable_seed; |
194 | int ret, fd = -1, dfd = -1, lock, program_ret = 0; | ||
204 | uint8_t new_seed[MAX_SEED_LEN]; | 195 | uint8_t new_seed[MAX_SEED_LEN]; |
205 | size_t new_seed_len; | 196 | size_t new_seed_len; |
206 | bool new_seed_creditable; | 197 | bool new_seed_creditable; |
@@ -245,6 +236,13 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[]) | |||
245 | goto out; | 236 | goto out; |
246 | } | 237 | } |
247 | 238 | ||
239 | dfd = open(seed_dir, O_DIRECTORY | O_RDONLY); | ||
240 | if (dfd < 0) { | ||
241 | bb_simple_perror_msg("unable to open seed directory"); | ||
242 | program_ret = 1; | ||
243 | goto out; | ||
244 | } | ||
245 | |||
248 | sha256_begin(&hash); | 246 | sha256_begin(&hash); |
249 | sha256_hash(&hash, seedrng_prefix, strlen(seedrng_prefix)); | 247 | sha256_hash(&hash, seedrng_prefix, strlen(seedrng_prefix)); |
250 | clock_gettime(CLOCK_REALTIME, ×tamp); | 248 | clock_gettime(CLOCK_REALTIME, ×tamp); |
@@ -252,10 +250,10 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[]) | |||
252 | clock_gettime(CLOCK_BOOTTIME, ×tamp); | 250 | clock_gettime(CLOCK_BOOTTIME, ×tamp); |
253 | sha256_hash(&hash, ×tamp, sizeof(timestamp)); | 251 | sha256_hash(&hash, ×tamp, sizeof(timestamp)); |
254 | 252 | ||
255 | ret = seed_from_file_if_exists(non_creditable_seed, false, &hash); | 253 | ret = seed_from_file_if_exists(non_creditable_seed, dfd, false, &hash); |
256 | if (ret < 0) | 254 | if (ret < 0) |
257 | program_ret |= 1 << 1; | 255 | program_ret |= 1 << 1; |
258 | ret = seed_from_file_if_exists(creditable_seed, !skip_credit, &hash); | 256 | ret = seed_from_file_if_exists(creditable_seed, dfd, !skip_credit, &hash); |
259 | if (ret < 0) | 257 | if (ret < 0) |
260 | program_ret |= 1 << 2; | 258 | program_ret |= 1 << 2; |
261 | 259 | ||
@@ -290,6 +288,8 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[]) | |||
290 | out: | 288 | out: |
291 | if (ENABLE_FEATURE_CLEAN_UP && fd >= 0) | 289 | if (ENABLE_FEATURE_CLEAN_UP && fd >= 0) |
292 | close(fd); | 290 | close(fd); |
291 | if (ENABLE_FEATURE_CLEAN_UP && dfd >= 0) | ||
292 | close(dfd); | ||
293 | if (ENABLE_FEATURE_CLEAN_UP && lock >= 0) | 293 | if (ENABLE_FEATURE_CLEAN_UP && lock >= 0) |
294 | close(lock); | 294 | close(lock); |
295 | return program_ret; | 295 | return program_ret; |