mirror of
https://github.com/systemd/systemd.git
synced 2024-10-29 21:55:36 +03:00
sha256: add sha256_direct()/SHA256_DIRECT() helpers
This commit is contained in:
parent
00b4663813
commit
558d96240b
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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]) {})
|
||||
|
Loading…
Reference in New Issue
Block a user