]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
blake2: use generated param decoder
authorPauli <ppzgs1@gmail.com>
Wed, 16 Jul 2025 00:50:03 +0000 (10:50 +1000)
committerPauli <ppzgs1@gmail.com>
Wed, 13 Aug 2025 01:49:43 +0000 (11:49 +1000)
Reviewed-by: Paul Yang <paulyang.inf@gmail.com>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/28142)

providers/implementations/macs/blake2_mac_impl.c

index 2ca800fad9bcd3cf465d18679e53f18b3d3a0850..bec067083fcdd5aa516081514c6a9ab8b2680096 100644 (file)
@@ -17,6 +17,8 @@
 #include "prov/implementations.h"
 #include "prov/providercommon.h"
 
+#include "prov/blake2_params.inc"
+
 /*
  * Forward declaration of everything implemented here.  This is not strictly
  * necessary for the compiler, but provides an assurance that the signatures
@@ -144,43 +146,35 @@ static int blake2_mac_final(void *vmacctx,
     return BLAKE2_FINAL(out, &macctx->ctx);
 }
 
-static const OSSL_PARAM known_gettable_ctx_params[] = {
-    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
-    OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
-    OSSL_PARAM_END
-};
+/* See blake2.h for parameter defintion */
 static const OSSL_PARAM *blake2_gettable_ctx_params(ossl_unused void *ctx,
                                                     ossl_unused void *provctx)
 {
-    return known_gettable_ctx_params;
+    return blake2_get_ctx_list;
 }
 
 static int blake2_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
 {
-    OSSL_PARAM *p;
+    struct blake2_mac_data_st *macctx = vmacctx;
+    struct blake2_get_ctx_st p;
 
-    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
-            && !OSSL_PARAM_set_size_t(p, blake2_mac_size(vmacctx)))
+    if (macctx == NULL || !blake2_get_ctx_decoder(params, &p))
         return 0;
 
-    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
-            && !OSSL_PARAM_set_size_t(p, BLAKE2_BLOCKBYTES))
+    if (p.size != NULL
+            && !OSSL_PARAM_set_size_t(p.size, blake2_mac_size(macctx)))
+        return 0;
+
+    if (p.bsize != NULL && !OSSL_PARAM_set_size_t(p.bsize, BLAKE2_BLOCKBYTES))
         return 0;
 
     return 1;
 }
 
-static const OSSL_PARAM known_settable_ctx_params[] = {
-    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
-    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
-    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, NULL, 0),
-    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_SALT, NULL, 0),
-    OSSL_PARAM_END
-};
 static const OSSL_PARAM *blake2_mac_settable_ctx_params(
             ossl_unused void *ctx, ossl_unused void *p_ctx)
 {
-    return known_settable_ctx_params;
+    return blake2_mac_set_ctx_list;
 }
 
 /*
@@ -189,15 +183,15 @@ static const OSSL_PARAM *blake2_mac_settable_ctx_params(
 static int blake2_mac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
 {
     struct blake2_mac_data_st *macctx = vmacctx;
-    const OSSL_PARAM *p;
+    struct blake2_mac_set_ctx_st p;
 
-    if (ossl_param_is_empty(params))
-        return 1;
+    if (macctx == NULL || !blake2_mac_set_ctx_decoder(params, &p))
+        return 0;
 
-    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
+    if (p.size != NULL) {
         size_t size;
 
-        if (!OSSL_PARAM_get_size_t(p, &size)
+        if (!OSSL_PARAM_get_size_t(p.size, &size)
             || size < 1
             || size > BLAKE2_OUTBYTES) {
             ERR_raise(ERR_LIB_PROV, PROV_R_NOT_XOF_OR_INVALID_LENGTH);
@@ -206,33 +200,38 @@ static int blake2_mac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
         BLAKE2_PARAM_SET_DIGEST_LENGTH(&macctx->params, (uint8_t)size);
     }
 
-    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL
-            && !blake2_setkey(macctx, p->data, p->data_size))
-        return 0;
+    if (p.key != NULL)
+        if (p.key->data_type != OSSL_PARAM_OCTET_STRING
+                || !blake2_setkey(macctx, p.key->data, p.key->data_size))
+            return 0;
 
-    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CUSTOM))
-        != NULL) {
+    if (p.cust != NULL) {
+        if (p.cust->data_type != OSSL_PARAM_OCTET_STRING)
+            return 0;
         /*
          * The OSSL_PARAM API doesn't provide direct pointer use, so we
          * must handle the OSSL_PARAM structure ourselves here
          */
-        if (p->data_size > BLAKE2_PERSONALBYTES) {
+        if (p.cust->data_size > BLAKE2_PERSONALBYTES) {
             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH);
             return 0;
         }
-        BLAKE2_PARAM_SET_PERSONAL(&macctx->params, p->data, p->data_size);
+        BLAKE2_PARAM_SET_PERSONAL(&macctx->params, p.cust->data,
+                                  p.cust->data_size);
     }
 
-    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SALT)) != NULL) {
+    if (p.salt != NULL) {
+        if (p.salt->data_type != OSSL_PARAM_OCTET_STRING)
+            return 0;
         /*
          * The OSSL_PARAM API doesn't provide direct pointer use, so we
          * must handle the OSSL_PARAM structure ourselves here as well
          */
-        if (p->data_size > BLAKE2_SALTBYTES) {
+        if (p.salt->data_size > BLAKE2_SALTBYTES) {
             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
             return 0;
         }
-        BLAKE2_PARAM_SET_SALT(&macctx->params, p->data, p->data_size);
+        BLAKE2_PARAM_SET_SALT(&macctx->params, p.salt->data, p.salt->data_size);
     }
     return 1;
 }