From: Loup Vaillant Date: Wed, 4 Dec 2019 18:24:59 +0000 (+0100) Subject: Renamed "crypto_hmac_*" to "crypto_hmac_sha512_*" X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=32525e92;p=Monocypher.git Renamed "crypto_hmac_*" to "crypto_hmac_sha512_*" There are several types of HMAC, and users may want to use other versions of HMAC as well. (For instance, they could code their own Blake2b HMAC to implement Noise). Plus, most primitives are named by their technical name. "hmac" alone is not enough. The names are longer, but this is the optional part, after all. --- diff --git a/src/optional/ed25519.c b/src/optional/ed25519.c index 78ef5f3..979ffa9 100644 --- a/src/optional/ed25519.c +++ b/src/optional/ed25519.c @@ -243,7 +243,8 @@ const crypto_sign_vtable crypto_sha512_vtable = { //////////////////// /// HMAC SHA 512 /// //////////////////// -void crypto_hmac_init(crypto_hmac_ctx *ctx, const u8 *key, size_t key_size) +void crypto_hmac_sha512_init(crypto_hmac_sha512_ctx *ctx, + const u8 *key, size_t key_size) { // hash key if it is too long if (key_size > 128) { @@ -259,13 +260,13 @@ void crypto_hmac_init(crypto_hmac_ctx *ctx, const u8 *key, size_t key_size) crypto_sha512_update(&ctx->ctx, ctx->key, 128); } -void crypto_hmac_update(crypto_hmac_ctx *ctx, - const u8 *message, size_t message_size) +void crypto_hmac_sha512_update(crypto_hmac_sha512_ctx *ctx, + const u8 *message, size_t message_size) { crypto_sha512_update(&ctx->ctx, message, message_size); } -void crypto_hmac_final(crypto_hmac_ctx *ctx, u8 hmac[64]) +void crypto_hmac_sha512_final(crypto_hmac_sha512_ctx *ctx, u8 hmac[64]) { // Finish computing inner hash crypto_sha512_final(&ctx->ctx, hmac); @@ -281,13 +282,13 @@ void crypto_hmac_final(crypto_hmac_ctx *ctx, u8 hmac[64]) WIPE_CTX(ctx); } -void crypto_hmac(u8 hmac[64], const u8 *key, size_t key_size, - const u8 *message, size_t message_size) +void crypto_hmac_sha512(u8 hmac[64], const u8 *key, size_t key_size, + const u8 *message, size_t message_size) { - crypto_hmac_ctx ctx; - crypto_hmac_init (&ctx, key, key_size); - crypto_hmac_update(&ctx, message, message_size); - crypto_hmac_final (&ctx, hmac); + crypto_hmac_sha512_ctx ctx; + crypto_hmac_sha512_init (&ctx, key, key_size); + crypto_hmac_sha512_update(&ctx, message, message_size); + crypto_hmac_sha512_final (&ctx, hmac); } diff --git a/src/optional/ed25519.h b/src/optional/ed25519.h index 259b7e9..33be482 100644 --- a/src/optional/ed25519.h +++ b/src/optional/ed25519.h @@ -21,7 +21,7 @@ typedef struct { typedef struct { uint8_t key[128]; crypto_sha512_ctx ctx; -} crypto_hmac_ctx; +} crypto_hmac_sha512_ctx; typedef struct { crypto_sign_ctx_abstract ctx; @@ -43,14 +43,14 @@ extern const crypto_sign_vtable crypto_sha512_vtable; // HMAC SHA 512 // ------------ -void crypto_hmac_init(crypto_hmac_ctx *ctx, - const uint8_t *key, size_t key_size); -void crypto_hmac_update(crypto_hmac_ctx *ctx, - const uint8_t *message, size_t message_size); -void crypto_hmac_final(crypto_hmac_ctx *ctx, uint8_t hmac[64]); -void crypto_hmac(uint8_t hmac[64], - const uint8_t *key , size_t key_size, - const uint8_t *message, size_t message_size); +void crypto_hmac_sha512_init(crypto_hmac_sha512_ctx *ctx, + const uint8_t *key, size_t key_size); +void crypto_hmac_sha512_update(crypto_hmac_sha512_ctx *ctx, + const uint8_t *message, size_t message_size); +void crypto_hmac_sha512_final(crypto_hmac_sha512_ctx *ctx, uint8_t hmac[64]); +void crypto_hmac_sha512(uint8_t hmac[64], + const uint8_t *key , size_t key_size, + const uint8_t *message, size_t message_size); // Ed25519 diff --git a/tests/test.c b/tests/test.c index f5d1cca..dd353fc 100644 --- a/tests/test.c +++ b/tests/test.c @@ -86,7 +86,7 @@ static void hmac_sha512(const vector in[], vector *out) { const vector *key = in; const vector *msg = in +1; - crypto_hmac(out->buf, key->buf, key->size, msg->buf, msg->size); + crypto_hmac_sha512(out->buf, key->buf, key->size, msg->buf, msg->size); } static void argon2i(const vector in[], vector *out) @@ -475,14 +475,14 @@ static int p_hmac_sha512() RANDOM_INPUT(input, INPUT_SIZE); // Authenticate bit by bit - crypto_hmac_ctx ctx; - crypto_hmac_init(&ctx, key, 32); - crypto_hmac_update(&ctx, input , i); - crypto_hmac_update(&ctx, input + i, INPUT_SIZE - i); - crypto_hmac_final(&ctx, hash_chunk); + crypto_hmac_sha512_ctx ctx; + crypto_hmac_sha512_init(&ctx, key, 32); + crypto_hmac_sha512_update(&ctx, input , i); + crypto_hmac_sha512_update(&ctx, input + i, INPUT_SIZE - i); + crypto_hmac_sha512_final(&ctx, hash_chunk); // Authenticate all at once - crypto_hmac(hash_whole, key, 32, input, INPUT_SIZE); + crypto_hmac_sha512(hash_whole, key, 32, input, INPUT_SIZE); // Compare the results (must be the same) status |= memcmp(hash_chunk, hash_whole, 64); @@ -501,8 +501,8 @@ static int p_hmac_sha512_overlap() u8 hash [64]; RANDOM_INPUT(key , 32); RANDOM_INPUT(input, INPUT_SIZE); - crypto_hmac(hash , key, 32, input + 64, SHA_512_BLOCK_SIZE); - crypto_hmac(input+i, key, 32, input + 64, SHA_512_BLOCK_SIZE); + crypto_hmac_sha512(hash , key, 32, input + 64, SHA_512_BLOCK_SIZE); + crypto_hmac_sha512(input+i, key, 32, input + 64, SHA_512_BLOCK_SIZE); status |= memcmp(hash, input + i, 64); } printf("%s: HMAC SHA-512 (overlaping i/o)\n", status != 0 ? "FAILED" : "OK");