Get rid of the diversity of names for MAC parameters
[openssl.git] / crypto / modes / siv128.c
index f4d07d514a00c36c0a208f0dbc314e457e19afb3..de6a3b853f19121eb102c0e6ba7289b63c16575b 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 #include <openssl/crypto.h>
-#include <openssl/cmac.h>
-#include "modes_lcl.h"
+#include <openssl/evp.h>
+#include <openssl/core_names.h>
+#include <openssl/params.h>
+#include "internal/modes_int.h"
+#include "internal/siv_int.h"
 
 #ifndef OPENSSL_NO_SIV
 
@@ -93,30 +96,38 @@ __owur static ossl_inline int siv128_do_s2v_p(SIV128_CONTEXT *ctx, SIV_BLOCK *ou
 {
     SIV_BLOCK t;
     size_t out_len = sizeof(out->byte);
+    EVP_MAC_CTX *mac_ctx;
+    int ret = 0;
 
-    if (!CMAC_CTX_copy(ctx->cmac_ctx, ctx->cmac_ctx_init))
+    mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init);
+    if (mac_ctx == NULL)
         return 0;
 
     if (len >= SIV_LEN) {
-        if (!CMAC_Update(ctx->cmac_ctx, in, len - SIV_LEN))
-            return 0;
+        if (!EVP_MAC_update(mac_ctx, in, len - SIV_LEN))
+            goto err;
         memcpy(&t, in + (len-SIV_LEN), SIV_LEN);
         siv128_xorblock(&t, &ctx->d);
-        if (!CMAC_Update(ctx->cmac_ctx, t.byte, SIV_LEN))
-            return 0;
+        if (!EVP_MAC_update(mac_ctx, t.byte, SIV_LEN))
+            goto err;
     } else {
         memset(&t, 0, sizeof(t));
         memcpy(&t, in, len);
         t.byte[len] = 0x80;
         siv128_dbl(&ctx->d);
         siv128_xorblock(&t, &ctx->d);
-        if (!CMAC_Update(ctx->cmac_ctx, t.byte, SIV_LEN))
-            return 0;
+        if (!EVP_MAC_update(mac_ctx, t.byte, SIV_LEN))
+            goto err;
     }
-    if (!CMAC_Final(ctx->cmac_ctx, out->byte, &out_len)
+    if (!EVP_MAC_final(mac_ctx, out->byte, &out_len, sizeof(out->byte))
         || out_len != SIV_LEN)
-        return 0;
-    return 1;
+        goto err;
+
+    ret = 1;
+
+err:
+    EVP_MAC_CTX_free(mac_ctx);
+    return ret;
 }
 
 
@@ -157,26 +168,40 @@ int CRYPTO_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen,
 {
     static const unsigned char zero[SIV_LEN] = { 0 };
     size_t out_len = SIV_LEN;
+    EVP_MAC_CTX *mac_ctx = NULL;
+    OSSL_PARAM params[3];
+    const char *cbc_name = EVP_CIPHER_name(cbc);
+
+    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
+                                                 (char *)cbc_name,
+                                                 strlen(cbc_name) + 1);
+    params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
+                                                  (void *)key, klen);
+    params[2] = OSSL_PARAM_construct_end();
 
     memset(&ctx->d, 0, sizeof(ctx->d));
     ctx->cipher_ctx = NULL;
-    ctx->cmac_ctx = NULL;
-    ctx->cmac_ctx_init = NULL;
+    ctx->mac_ctx_init = NULL;
 
     if (key == NULL || cbc == NULL || ctr == NULL
             || (ctx->cipher_ctx = EVP_CIPHER_CTX_new()) == NULL
-            || (ctx->cmac_ctx_init = CMAC_CTX_new()) == NULL
-            || (ctx->cmac_ctx = CMAC_CTX_new()) == NULL
-            || !CMAC_Init(ctx->cmac_ctx_init, key, klen, cbc, NULL)
+            /* TODO(3.0) library context */
+            || (ctx->mac =
+                EVP_MAC_fetch(NULL, OSSL_MAC_NAME_CMAC, NULL)) == NULL
+            || (ctx->mac_ctx_init = EVP_MAC_CTX_new(ctx->mac)) == NULL
+            || !EVP_MAC_CTX_set_params(ctx->mac_ctx_init, params)
             || !EVP_EncryptInit_ex(ctx->cipher_ctx, ctr, NULL, key + klen, NULL)
-            || !CMAC_CTX_copy(ctx->cmac_ctx, ctx->cmac_ctx_init)
-            || !CMAC_Update(ctx->cmac_ctx, zero, sizeof(zero))
-            || !CMAC_Final(ctx->cmac_ctx, ctx->d.byte, &out_len)) {
+            || (mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init)) == NULL
+            || !EVP_MAC_update(mac_ctx, zero, sizeof(zero))
+            || !EVP_MAC_final(mac_ctx, ctx->d.byte, &out_len,
+                              sizeof(ctx->d.byte))) {
         EVP_CIPHER_CTX_free(ctx->cipher_ctx);
-        CMAC_CTX_free(ctx->cmac_ctx_init);
-        CMAC_CTX_free(ctx->cmac_ctx);
+        EVP_MAC_CTX_free(ctx->mac_ctx_init);
+        EVP_MAC_CTX_free(mac_ctx);
+        EVP_MAC_free(ctx->mac);
         return 0;
     }
+    EVP_MAC_CTX_free(mac_ctx);
 
     ctx->final_ret = -1;
     ctx->crypto_ok = 1;
@@ -192,9 +217,10 @@ int CRYPTO_siv128_copy_ctx(SIV128_CONTEXT *dest, SIV128_CONTEXT *src)
     memcpy(&dest->d, &src->d, sizeof(src->d));
     if (!EVP_CIPHER_CTX_copy(dest->cipher_ctx, src->cipher_ctx))
         return 0;
-    if (!CMAC_CTX_copy(dest->cmac_ctx_init, src->cmac_ctx_init))
+    EVP_MAC_CTX_free(dest->mac_ctx_init);
+    dest->mac_ctx_init = EVP_MAC_CTX_dup(src->mac_ctx_init);
+    if (dest->mac_ctx_init == NULL)
         return 0;
-    /* no need to copy cmac_ctx since it's temp storage */
     return 1;
 }
 
@@ -206,21 +232,25 @@ int CRYPTO_siv128_copy_ctx(SIV128_CONTEXT *dest, SIV128_CONTEXT *src)
 int CRYPTO_siv128_aad(SIV128_CONTEXT *ctx, const unsigned char *aad,
                       size_t len)
 {
-    SIV_BLOCK cmac_out;
+    SIV_BLOCK mac_out;
     size_t out_len = SIV_LEN;
+    EVP_MAC_CTX *mac_ctx;
 
     siv128_dbl(&ctx->d);
 
-    if (!CMAC_CTX_copy(ctx->cmac_ctx, ctx->cmac_ctx_init)
-        || !CMAC_Update(ctx->cmac_ctx, aad, len)
-        || !CMAC_Final(ctx->cmac_ctx, cmac_out.byte, &out_len)
-        || out_len != SIV_LEN)
+    if ((mac_ctx = EVP_MAC_CTX_dup(ctx->mac_ctx_init)) == NULL
+        || !EVP_MAC_update(mac_ctx, aad, len)
+        || !EVP_MAC_final(mac_ctx, mac_out.byte, &out_len,
+                          sizeof(mac_out.byte))
+        || out_len != SIV_LEN) {
+        EVP_MAC_CTX_free(mac_ctx);
         return 0;
+    }
+    EVP_MAC_CTX_free(mac_ctx);
 
-    siv128_xorblock(&ctx->d, &cmac_out);
+    siv128_xorblock(&ctx->d, &mac_out);
 
     return 1;
-
 }
 
 /*
@@ -328,10 +358,10 @@ int CRYPTO_siv128_cleanup(SIV128_CONTEXT *ctx)
     if (ctx != NULL) {
         EVP_CIPHER_CTX_free(ctx->cipher_ctx);
         ctx->cipher_ctx = NULL;
-        CMAC_CTX_free(ctx->cmac_ctx_init);
-        ctx->cmac_ctx_init = NULL;
-        CMAC_CTX_free(ctx->cmac_ctx);
-        ctx->cmac_ctx = NULL;
+        EVP_MAC_CTX_free(ctx->mac_ctx_init);
+        ctx->mac_ctx_init = NULL;
+        EVP_MAC_free(ctx->mac);
+        ctx->mac = NULL;
         OPENSSL_cleanse(&ctx->d, sizeof(ctx->d));
         OPENSSL_cleanse(&ctx->tag, sizeof(ctx->tag));
         ctx->final_ret = -1;