]> git.codecow.com Git - Monocypher.git/commitdiff
Renamed "crypto_hmac_*" to "crypto_hmac_sha512_*"
authorLoup Vaillant <loup@loup-vaillant.fr>
Wed, 4 Dec 2019 18:24:59 +0000 (19:24 +0100)
committerLoup Vaillant <loup@loup-vaillant.fr>
Wed, 4 Dec 2019 18:24:59 +0000 (19:24 +0100)
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.

src/optional/ed25519.c
src/optional/ed25519.h
tests/test.c

index 78ef5f38e9c0053a506df64645b705efa57f3dfe..979ffa952137d5f856c56ad6bb0a842ff97e07a4 100644 (file)
@@ -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);
 }
 
 
index 259b7e929e9f3c4c87cac5741b651862951e67c3..33be4822a5dea14db19f1501119a16e0381414ae 100644 (file)
@@ -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
index f5d1ccaff92daedd0cc68ef125ca12ac26883592..dd353fc64c9547ad16f37853378a94ce531cc3d1 100644 (file)
@@ -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");