random-seed: cleanup code nits
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 5 Jan 2022 13:36:13 +0000 (14:36 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 5 Jan 2022 17:09:22 +0000 (02:09 +0900)
This incorporates various nits from the post-merge review on #21986.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
src/random-seed/random-seed.c

index bba833572037474dff3c3279e2cd16afb94a6f3a..867d55169d7344aa801381cef00dfeafbbef5420 100644 (file)
@@ -104,11 +104,10 @@ static CreditEntropy may_credit(int seed_fd) {
 }
 
 static int run(int argc, char *argv[]) {
+        bool read_seed_file, write_seed_file, synchronous, hashed_old_seed = false;
         _cleanup_close_ int seed_fd = -1, random_fd = -1;
-        bool read_seed_file, write_seed_file, synchronous;
         _cleanup_free_ void* buf = NULL;
         struct sha256_ctx hash_state;
-        uint8_t hash[32];
         size_t buf_size;
         struct stat st;
         ssize_t k, l;
@@ -214,6 +213,16 @@ static int run(int argc, char *argv[]) {
                 else {
                         CreditEntropy lets_credit;
 
+                        /* If we're going to later write out a seed file, initialize a hash state with
+                         * the contents of the seed file we just read, so that the new one can't regress
+                         * in entropy. */
+                        if (write_seed_file) {
+                                sha256_init_ctx(&hash_state);
+                                sha256_process_bytes(&k, sizeof(k), &hash_state); /* Hash length to distinguish from new seed. */
+                                sha256_process_bytes(buf, k, &hash_state);
+                                hashed_old_seed = true;
+                        }
+
                         (void) lseek(seed_fd, 0, SEEK_SET);
 
                         lets_credit = may_credit(seed_fd);
@@ -245,16 +254,6 @@ static int run(int argc, char *argv[]) {
                         if (r < 0)
                                 log_error_errno(r, "Failed to write seed to /dev/urandom: %m");
                 }
-                /* If we're going to later write out a seed file, initialize a hash state with
-                 * the contents of the seed file we just read, so that the new one can't regress
-                 * in entropy. */
-                if (write_seed_file) {
-                        sha256_init_ctx(&hash_state);
-                        if (k < 0)
-                                k = 0;
-                        sha256_process_bytes(&k, sizeof(k), &hash_state);
-                        sha256_process_bytes(buf, k, &hash_state);
-                }
         }
 
         if (write_seed_file) {
@@ -293,11 +292,12 @@ static int run(int argc, char *argv[]) {
                 /* If we previously read in a seed file, then hash the new seed into the old one,
                  * and replace the last 32 bytes of the seed with the hash output, so that the
                  * new seed file can't regress in entropy. */
-                if (read_seed_file) {
-                        sha256_process_bytes(&k, sizeof(k), &hash_state);
+                if (hashed_old_seed) {
+                        uint8_t hash[32];
+                        sha256_process_bytes(&k, sizeof(k), &hash_state); /* Hash length to distinguish from old seed. */
                         sha256_process_bytes(buf, k, &hash_state);
                         sha256_finish_ctx(&hash_state, hash);
-                        l = MIN(k, 32);
+                        l = MIN((size_t)k, sizeof(hash));
                         memcpy((uint8_t *)buf + k - l, hash, l);
                 }