]> granicus.if.org Git - openssl/commitdiff
Use macros internally for algorithm names
authorRichard Levitte <levitte@openssl.org>
Thu, 15 Aug 2019 10:39:20 +0000 (12:39 +0200)
committerRichard Levitte <levitte@openssl.org>
Mon, 19 Aug 2019 06:10:16 +0000 (08:10 +0200)
The macros are defined in include/openssl/core_names.h and follow the
naming standard OSSL_{OPNAME}_NAME_{ALGONAME}, where {OPNAME} is the
name of the operation (such as MAC) and {ALGONAME} is the name of the
algorithm.  Example: OSSL_MAC_NAME_HMAC

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9635)

crypto/evp/p_lib.c
crypto/kdf/sskdf.c
crypto/kdf/tls1_prf.c
crypto/modes/siv128.c
include/openssl/core_names.h
providers/common/macs/kmac_prov.c
test/evp_kdf_test.c

index 90e13f485420ad03807b2b2f7c0b8747535f0c9c..bc573d02084f57ef9602ca015579810c6d935b52 100644 (file)
@@ -327,7 +327,7 @@ EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
     OPENSSL_CTX *libctx =
         prov == NULL ? NULL : ossl_provider_library_context(prov);
     EVP_PKEY *ret = EVP_PKEY_new();
-    EVP_MAC *cmac = EVP_MAC_fetch(libctx, "CMAC", NULL);
+    EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
     EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
     OSSL_PARAM params[4];
     size_t paramsn = 0;
index 7e5a68be5ecdefe090acd3c224f2bff8bea04a3f..a4a9abff5a3f49cda19284522e85e2449fcf6e82 100644 (file)
@@ -467,7 +467,7 @@ static int sskdf_derive(EVP_KDF_IMPL *impl, unsigned char *key, size_t keylen)
          * block size?
          */
         macname = EVP_MAC_name(impl->mac);
-        if (strcmp(macname, "HMAC") == 0) {
+        if (strcmp(macname, OSSL_MAC_NAME_HMAC) == 0) {
             /* H(x) = HMAC(x, salt, hash) */
             if (impl->md == NULL) {
                 KDFerr(KDF_F_SSKDF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
@@ -476,12 +476,12 @@ static int sskdf_derive(EVP_KDF_IMPL *impl, unsigned char *key, size_t keylen)
             default_salt_len = EVP_MD_block_size(impl->md);
             if (default_salt_len <= 0)
                 return 0;
-        } else if (strcmp(macname, "KMAC128") == 0
-                   || strcmp(macname, "KMAC256") == 0) {
+        } else if (strcmp(macname, OSSL_MAC_NAME_KMAC128) == 0
+                   || strcmp(macname, OSSL_MAC_NAME_KMAC256) == 0) {
             /* H(x) = KMACzzz(x, salt, custom) */
             custom = kmac_custom_str;
             custom_len = sizeof(kmac_custom_str);
-            if (strcmp(macname, "KMAC128") == 0)
+            if (strcmp(macname, OSSL_MAC_NAME_KMAC128) == 0)
                 default_salt_len = SSKDF_KMAC128_DEFAULT_SALT_SIZE;
             else
                 default_salt_len = SSKDF_KMAC256_DEFAULT_SALT_SIZE;
index 1df201dd3ffa9dbddc1335a6980f0b9c61c3cbfe..b53c417737b5e64b98efbfe24e4ce025ea7d46a7 100644 (file)
@@ -243,7 +243,7 @@ static int tls1_prf_P_hash(const EVP_MD *md,
     int mac_flags;
     const char *mdname = EVP_MD_name(md);
 
-    mac = EVP_MAC_fetch(NULL, "HMAC", NULL); /* Implicit fetch */
+    mac = EVP_MAC_fetch(NULL, OSSL_MAC_NAME_HMAC, NULL); /* Implicit fetch */
     ctx_init = EVP_MAC_CTX_new(mac);
     if (ctx_init == NULL)
         goto err;
index 04abea25c10c02cbccdfb6e08b9f8b0e3a5643ff..626d2f37ecf8a725fda4655816aaf902fd09c438 100644 (file)
@@ -186,7 +186,8 @@ int CRYPTO_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen,
     if (key == NULL || cbc == NULL || ctr == NULL
             || (ctx->cipher_ctx = EVP_CIPHER_CTX_new()) == NULL
             /* TODO(3.0) library context */
-            || (ctx->mac = EVP_MAC_fetch(NULL, "CMAC", NULL)) == NULL
+            || (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)
index c1bc3a7d7bf96c51aa85be029a5a1e92c62b507c..76fe37ee4a4a88491b363901e54ba3938a6260da 100644 (file)
@@ -64,6 +64,10 @@ extern "C" {
 #define OSSL_DIGEST_PARAM_SIZE      "size" /* OSSL_PARAM_INTEGER */
 #define OSSL_DIGEST_PARAM_FLAGS     "flags" /* OSSL_PARAM_UNSIGNED_INTEGER */
 
+/* Known DIGEST names (not a complete list) */
+#define OSSL_DIGEST_NAME_KECCAK_KMAC128 "KECCAK_KMAC128"
+#define OSSL_DIGEST_NAME_KECCAK_KMAC256 "KECCAK_KMAC256"
+
 /* MAC parameters */
 #define OSSL_MAC_PARAM_KEY          "key"       /* octet string */
 #define OSSL_MAC_PARAM_IV           "iv"        /* octet string */
@@ -84,6 +88,12 @@ extern "C" {
 #define OSSL_MAC_PARAM_DIGESTSIZE   "digestsize" /* size_t */
 #define OSSL_MAC_PARAM_OUTLEN       "outlen"    /* size_t */
 
+/* Known MAC names (not a complete list) */
+#define OSSL_MAC_NAME_CMAC          "CMAC"
+#define OSSL_MAC_NAME_HMAC          "HMAC"
+#define OSSL_MAC_NAME_KMAC128       "KMAC128"
+#define OSSL_MAC_NAME_KMAC256       "KMAC256"
+
 /* PKEY parameters */
 /* Diffie-Hellman Parameters */
 #define OSSL_PKEY_PARAM_DH_P         "dh-p"
index 84c8fce2c922b1c80009da4ca24e8053d864155e..70dad097949885096e3109fc03a1052783a47c5d 100644 (file)
@@ -203,12 +203,12 @@ static void *kmac_fetch_new(void *provctx, const char *mdname)
 
 static void *kmac128_new(void *provctx)
 {
-    return kmac_fetch_new(provctx, "KECCAK_KMAC128");
+    return kmac_fetch_new(provctx, OSSL_DIGEST_NAME_KECCAK_KMAC128);
 }
 
 static void *kmac256_new(void *provctx)
 {
-    return kmac_fetch_new(provctx, "KECCAK_KMAC256");
+    return kmac_fetch_new(provctx, OSSL_DIGEST_NAME_KECCAK_KMAC256);
 }
 
 static void *kmac_dup(void *vsrc)
index b3d1648c0d6c3bc356b390c6311475e5addca8f9..1a131a7995ecc459a56e04b919b36f2f3cc3c36c 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <openssl/evp.h>
 #include <openssl/kdf.h>
+#include <openssl/core_names.h>
 #include "testutil.h"
 
 static int test_kdf_tls1_prf(void)
@@ -278,7 +279,8 @@ static int test_kdf_ss_hmac(void)
 
     ret =
         TEST_ptr(kctx = EVP_KDF_CTX_new_id(EVP_KDF_SS))
-        && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAC, "HMAC"), 0)
+        && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAC,
+                                    OSSL_MAC_NAME_HMAC), 0)
         && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD,  EVP_sha256()), 0)
         && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, z, sizeof(z)), 0)
         && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SSKDF_INFO, other,
@@ -317,7 +319,8 @@ static int test_kdf_ss_kmac(void)
 
     ret =
         TEST_ptr(kctx = EVP_KDF_CTX_new_id(EVP_KDF_SS))
-        && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAC, "KMAC128"), 0)
+        && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAC,
+                                    OSSL_MAC_NAME_KMAC128), 0)
         && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, z,
                                     sizeof(z)), 0)
         && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SSKDF_INFO, other,