aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2022-04-21 12:37:32 +0200
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2022-04-24 10:01:07 +0200
commitf9ea8ba5ed6d43b8b233eac433521eb158e359d1 (patch)
treea6821c4598b703f3a8a336d4b64136857c177501
parent831c754c91f798c53a133bc2cb84eaf38ed32352 (diff)
downloadbusybox-w32-f9ea8ba5ed6d43b8b233eac433521eb158e359d1.tar.gz
busybox-w32-f9ea8ba5ed6d43b8b233eac433521eb158e359d1.tar.bz2
busybox-w32-f9ea8ba5ed6d43b8b233eac433521eb158e359d1.zip
seedrng: code-golf even smaller
Since we're passing 0 as the timeout, we don't need safe_poll. Remove cleanup at end of program, since OS does that, which lets us simplify control flow. Factor repeated function calls into ternary loop. function old new delta seedrng_main 1061 1459 +398 seed_from_file_if_exists 468 - -468 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 1/0 up/down: 398/-468) Total: -70 bytes text data bss dec hex filename 1052781 16515 1816 1071112 105808 busybox_old 1052711 16515 1816 1071042 1057c2 busybox_unstripped Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
-rw-r--r--util-linux/seedrng.c42
1 files changed, 14 insertions, 28 deletions
diff --git a/util-linux/seedrng.c b/util-linux/seedrng.c
index 5a41addf0..374e7f676 100644
--- a/util-linux/seedrng.c
+++ b/util-linux/seedrng.c
@@ -102,7 +102,7 @@ static int read_new_seed(uint8_t *seed, size_t len, bool *is_creditable)
102 }; 102 };
103 if (random_fd.fd < 0) 103 if (random_fd.fd < 0)
104 return -1; 104 return -1;
105 *is_creditable = safe_poll(&random_fd, 1, 0) == 1; 105 *is_creditable = poll(&random_fd, 1, 0) == 1;
106 close(random_fd.fd); 106 close(random_fd.fd);
107 } else if (getrandom(seed, len, GRND_INSECURE) == (ssize_t)len) 107 } else if (getrandom(seed, len, GRND_INSECURE) == (ssize_t)len)
108 return 0; 108 return 0;
@@ -174,15 +174,13 @@ int seedrng_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
174int seedrng_main(int argc UNUSED_PARAM, char *argv[]) 174int seedrng_main(int argc UNUSED_PARAM, char *argv[])
175{ 175{
176 const char *seed_dir = DEFAULT_SEED_DIR, *creditable_seed, *non_creditable_seed; 176 const char *seed_dir = DEFAULT_SEED_DIR, *creditable_seed, *non_creditable_seed;
177 int ret, fd = -1, dfd = -1, program_ret = 0; 177 int fd, dfd, program_ret = 0;
178 uint8_t new_seed[MAX_SEED_LEN]; 178 uint8_t new_seed[MAX_SEED_LEN];
179 size_t new_seed_len; 179 size_t new_seed_len;
180 bool new_seed_creditable; 180 bool new_seed_creditable, skip_credit = false;
181 bool skip_credit = false;
182 struct timespec timestamp = { 0 }; 181 struct timespec timestamp = { 0 };
183 sha256_ctx_t hash; 182 sha256_ctx_t hash;
184 183
185 int opt;
186 enum { 184 enum {
187 OPT_d = (1 << 0), 185 OPT_d = (1 << 0),
188 OPT_n = (1 << 1) 186 OPT_n = (1 << 1)
@@ -194,8 +192,7 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
194 ; 192 ;
195#endif 193#endif
196 194
197 opt = getopt32long(argv, "d:n", longopts, &seed_dir); 195 skip_credit = getopt32long(argv, "d:n", longopts, &seed_dir) & OPT_n;
198 skip_credit = opt & OPT_n;
199 creditable_seed = concat_path_file(seed_dir, CREDITABLE_SEED_NAME); 196 creditable_seed = concat_path_file(seed_dir, CREDITABLE_SEED_NAME);
200 non_creditable_seed = concat_path_file(seed_dir, NON_CREDITABLE_SEED_NAME); 197 non_creditable_seed = concat_path_file(seed_dir, NON_CREDITABLE_SEED_NAME);
201 198
@@ -207,11 +204,8 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
207 bb_perror_msg_and_die("unable to %s seed directory", "create"); 204 bb_perror_msg_and_die("unable to %s seed directory", "create");
208 205
209 dfd = open(seed_dir, O_DIRECTORY | O_RDONLY); 206 dfd = open(seed_dir, O_DIRECTORY | O_RDONLY);
210 if (dfd < 0 || flock(dfd, LOCK_EX) < 0) { 207 if (dfd < 0 || flock(dfd, LOCK_EX) < 0)
211 bb_perror_msg("unable to %s seed directory", "lock"); 208 bb_perror_msg_and_die("unable to %s seed directory", "lock");
212 program_ret = 1;
213 goto out;
214 }
215 209
216 sha256_begin(&hash); 210 sha256_begin(&hash);
217 sha256_hash(&hash, "SeedRNG v1 Old+New Prefix", 25); 211 sha256_hash(&hash, "SeedRNG v1 Old+New Prefix", 25);
@@ -220,16 +214,14 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
220 clock_gettime(CLOCK_BOOTTIME, &timestamp); 214 clock_gettime(CLOCK_BOOTTIME, &timestamp);
221 sha256_hash(&hash, &timestamp, sizeof(timestamp)); 215 sha256_hash(&hash, &timestamp, sizeof(timestamp));
222 216
223 ret = seed_from_file_if_exists(non_creditable_seed, dfd, false, &hash); 217 for (int i = 1; i < 3; ++i) {
224 if (ret < 0) 218 if (seed_from_file_if_exists(i == 1 ? non_creditable_seed : creditable_seed,
225 program_ret |= 1 << 1; 219 dfd, i == 1 ? false : !skip_credit, &hash) < 0)
226 ret = seed_from_file_if_exists(creditable_seed, dfd, !skip_credit, &hash); 220 program_ret |= 1 << i;
227 if (ret < 0) 221 }
228 program_ret |= 1 << 2;
229 222
230 new_seed_len = determine_optimal_seed_len(); 223 new_seed_len = determine_optimal_seed_len();
231 ret = read_new_seed(new_seed, new_seed_len, &new_seed_creditable); 224 if (read_new_seed(new_seed, new_seed_len, &new_seed_creditable) < 0) {
232 if (ret < 0) {
233 bb_perror_msg("unable to%s seed", " read new"); 225 bb_perror_msg("unable to%s seed", " read new");
234 new_seed_len = SHA256_OUTSIZE; 226 new_seed_len = SHA256_OUTSIZE;
235 memset(new_seed, 0, SHA256_OUTSIZE); 227 memset(new_seed, 0, SHA256_OUTSIZE);
@@ -243,17 +235,11 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
243 fd = open(non_creditable_seed, O_WRONLY | O_CREAT | O_TRUNC, 0400); 235 fd = open(non_creditable_seed, O_WRONLY | O_CREAT | O_TRUNC, 0400);
244 if (fd < 0 || full_write(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || fsync(fd) < 0) { 236 if (fd < 0 || full_write(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || fsync(fd) < 0) {
245 bb_perror_msg("unable to%s seed", " write"); 237 bb_perror_msg("unable to%s seed", " write");
246 program_ret |= 1 << 4; 238 return program_ret | (1 << 4);
247 goto out;
248 } 239 }
249 if (new_seed_creditable && rename(non_creditable_seed, creditable_seed) < 0) { 240 if (new_seed_creditable && rename(non_creditable_seed, creditable_seed) < 0) {
250 bb_simple_perror_msg("unable to make new seed creditable"); 241 bb_simple_perror_msg("unable to make new seed creditable");
251 program_ret |= 1 << 5; 242 return program_ret | (1 << 5);
252 } 243 }
253out:
254 if (ENABLE_FEATURE_CLEAN_UP && fd >= 0)
255 close(fd);
256 if (ENABLE_FEATURE_CLEAN_UP && dfd >= 0)
257 close(dfd);
258 return program_ret; 244 return program_ret;
259} 245}