Merge pull request #24362 from poettering/sha256-tweaks

some tweaks to the sha256 implementation
This commit is contained in:
Lennart Poettering
2022-08-19 16:33:35 +02:00
committed by GitHub
5 changed files with 18 additions and 18 deletions
+1 -3
View 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;
}
+2 -11
View 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
}
+9 -2
View File
@@ -104,7 +104,7 @@ void sha256_init_ctx(struct sha256_ctx *ctx) {
/* Process the remaining bytes in the internal buffer and the usual
prolog according to the standard and write the result to RESBUF. */
void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf) {
uint8_t *sha256_finish_ctx(struct sha256_ctx *ctx, uint8_t resbuf[static SHA256_DIGEST_SIZE]) {
/* Take yet unprocessed bytes into account. */
uint32_t bytes = ctx->buflen;
size_t pad;
@@ -129,7 +129,7 @@ void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf) {
/* Put result from CTX in first 32 bytes following RESBUF. */
for (size_t i = 0; i < 8; ++i)
if (UNALIGNED_P(resbuf))
memcpy((uint8_t*) resbuf + i * sizeof(uint32_t), (uint32_t[]) { SWAP(ctx->H[i]) }, sizeof(uint32_t));
memcpy(resbuf + i * sizeof(uint32_t), (uint32_t[]) { SWAP(ctx->H[i]) }, sizeof(uint32_t));
else
((uint32_t *) resbuf)[i] = SWAP(ctx->H[i]);
@@ -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);
}
+5 -1
View File
@@ -25,5 +25,9 @@ struct sha256_ctx {
};
void sha256_init_ctx(struct sha256_ctx *ctx);
void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf);
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]) {})
+1 -1
View File
@@ -293,7 +293,7 @@ static int run(int argc, char *argv[]) {
* 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 (hashed_old_seed) {
uint8_t hash[32];
uint8_t hash[SHA256_DIGEST_SIZE];
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);