]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sha256: add sha256_direct()/SHA256_DIRECT() helpers 24362/head
authorLennart Poettering <lennart@poettering.net>
Wed, 17 Aug 2022 09:32:38 +0000 (11:32 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 19 Aug 2022 10:53:04 +0000 (12:53 +0200)
src/basic/hmac.c
src/boot/efi/random-seed.c
src/fundamental/sha256.c
src/fundamental/sha256.h

index 1e4e380aaa925dd24d7bcaaaee0a16ef59ff09c0..a5f66d560566b93c53152649613b282ae69adbcb 100644 (file)
@@ -29,9 +29,7 @@ void hmac_sha256(const void *key,
 
         /* The key needs to be block size length or less, hash it if it's longer. */
         if (key_size > HMAC_BLOCK_SIZE) {
-                sha256_init_ctx(&hash);
-                sha256_process_bytes(key, key_size, &hash);
-                sha256_finish_ctx(&hash, replacement_key);
+                sha256_direct(key, key_size, replacement_key);
                 key = replacement_key;
                 key_size = SHA256_DIGEST_SIZE;
         }
index 652634bdc9b087f9a1cfe5c9bff90b0501ce3294..aea4f7e532624372976f3568f9b1c4c8c111092f 100644 (file)
@@ -215,17 +215,8 @@ static void validate_sha256(void) {
                     0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1 }},
         };
 
-        for (UINTN i = 0; i < ELEMENTSOF(array); i++) {
-                struct sha256_ctx hash;
-                uint8_t result[HASH_VALUE_SIZE];
-
-                sha256_init_ctx(&hash);
-                sha256_process_bytes(array[i].string, strlen8(array[i].string), &hash);
-                sha256_finish_ctx(&hash, result);
-
-                assert(memcmp(result, array[i].hash, HASH_VALUE_SIZE) == 0);
-        }
-
+        for (UINTN i = 0; i < ELEMENTSOF(array); i++)
+                assert(memcmp(SHA256_DIRECT(array[i].string, strlen8(array[i].string)), array[i].hash, HASH_VALUE_SIZE) == 0);
 #endif
 }
 
index 7ead5f169c50745d6bd94652b62073ef32ead5ba..43ee996b6f7c1b3da13bd42cc5c770e884617291 100644 (file)
@@ -289,3 +289,10 @@ static void sha256_process_block(const void *buffer, size_t len, struct sha256_c
         ctx->H[6] = g;
         ctx->H[7] = h;
 }
+
+uint8_t* sha256_direct(const void *buffer, size_t sz, uint8_t result[static SHA256_DIGEST_SIZE]) {
+        struct sha256_ctx ctx;
+        sha256_init_ctx(&ctx);
+        sha256_process_bytes(buffer, sz, &ctx);
+        return sha256_finish_ctx(&ctx, result);
+}
index 337e746c49802ccd822a7d879a1926d84a901108..31790c2ebd1737a0ab95fc37ba48b9d49fc76355 100644 (file)
@@ -27,3 +27,7 @@ struct sha256_ctx {
 void sha256_init_ctx(struct sha256_ctx *ctx);
 uint8_t *sha256_finish_ctx(struct sha256_ctx *ctx, uint8_t resbuf[static SHA256_DIGEST_SIZE]);
 void sha256_process_bytes(const void *buffer, size_t len, struct sha256_ctx *ctx);
+
+uint8_t* sha256_direct(const void *buffer, size_t sz, uint8_t result[static SHA256_DIGEST_SIZE]);
+
+#define SHA256_DIRECT(buffer, sz) sha256_direct(buffer, sz, (uint8_t[SHA256_DIGEST_SIZE]) {})