]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
siphash: convert siphash to use param decoder
authorPauli <ppzgs1@gmail.com>
Mon, 14 Jul 2025 01:15:43 +0000 (11:15 +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/siphash_prov.c.in

index 08a393c39c69f7b8a4c0de4766f7abee76665973..1d8c7faa8b0b755a75ab4e8b0a321ab8a9f6b821 100644 (file)
@@ -6,6 +6,9 @@
  * in the file LICENSE in the source distribution or at
  * https://www.openssl.org/source/license.html
  */
+{-
+use OpenSSL::paramnames qw(produce_param_decoder);
+-}
 
 #include <string.h>
 #include <openssl/core_dispatch.h>
@@ -15,6 +18,7 @@
 #include <openssl/err.h>
 #include <openssl/proverr.h>
 
+#include "internal/cryptlib.h"
 #include "crypto/siphash.h"
 
 #include "prov/implementations.h"
@@ -148,74 +152,70 @@ static int siphash_final(void *vmacctx, unsigned char *out, size_t *outl,
     return SipHash_Final(&ctx->siphash, out, hlen);
 }
 
+{- produce_param_decoder('siphash_get_ctx_params',
+                         (['MAC_PARAM_SIZE',        'size', 'size_t'],
+                          ['MAC_PARAM_C_ROUNDS',    'c',    'uint'],
+                          ['MAC_PARAM_D_ROUNDS',    'd',    'uint'],
+                         )); -}
+
 static const OSSL_PARAM *siphash_gettable_ctx_params(ossl_unused void *ctx,
                                                      ossl_unused void *provctx)
 {
-    static const OSSL_PARAM known_gettable_ctx_params[] = {
-        OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
-        OSSL_PARAM_uint(OSSL_MAC_PARAM_C_ROUNDS, NULL),
-        OSSL_PARAM_uint(OSSL_MAC_PARAM_D_ROUNDS, NULL),
-        OSSL_PARAM_END
-    };
-
-    return known_gettable_ctx_params;
+    return siphash_get_ctx_params_list;
 }
 
 static int siphash_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
 {
     struct siphash_data_st *ctx = vmacctx;
-    OSSL_PARAM *p;
+    struct siphash_get_ctx_params_st p;
+
+    if (ctx == NULL || !siphash_get_ctx_params_decoder(params, &p))
+        return 0;
 
-    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
-        && !OSSL_PARAM_set_size_t(p, siphash_size(vmacctx)))
+    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, siphash_size(vmacctx)))
         return 0;
-    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_C_ROUNDS)) != NULL
-        && !OSSL_PARAM_set_uint(p, crounds(ctx)))
+    if (p.c != NULL && !OSSL_PARAM_set_uint(p.c, crounds(ctx)))
         return 0;
-    if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_D_ROUNDS)) != NULL
-        && !OSSL_PARAM_set_uint(p, drounds(ctx)))
+    if (p.d != NULL && !OSSL_PARAM_set_uint(p.d, drounds(ctx)))
         return 0;
     return 1;
 }
 
+{- produce_param_decoder('siphash_set_params',
+                         (['MAC_PARAM_SIZE',        'size', 'size_t'],
+                          ['MAC_PARAM_KEY',         'key',  'octet_string'],
+                          ['MAC_PARAM_C_ROUNDS',    'c',    'uint'],
+                          ['MAC_PARAM_D_ROUNDS',    'd',    'uint'],
+                         )); -}
+
 static const OSSL_PARAM *siphash_settable_ctx_params(ossl_unused void *ctx,
                                                      void *provctx)
 {
-    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_uint(OSSL_MAC_PARAM_C_ROUNDS, NULL),
-        OSSL_PARAM_uint(OSSL_MAC_PARAM_D_ROUNDS, NULL),
-        OSSL_PARAM_END
-    };
-
-    return known_settable_ctx_params;
+    return siphash_set_params_list;
 }
 
 static int siphash_set_params(void *vmacctx, const OSSL_PARAM *params)
 {
     struct siphash_data_st *ctx = vmacctx;
-    const OSSL_PARAM *p = NULL;
+    struct siphash_set_params_st p;
     size_t size;
 
-    if (ossl_param_is_empty(params))
-        return 1;
+    if (ctx == NULL || !siphash_set_params_decoder(params, &p))
+        return 0;
 
-    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) {
-        if (!OSSL_PARAM_get_size_t(p, &size)
+    if (p.size != NULL) {
+        if (!OSSL_PARAM_get_size_t(p.size, &size)
             || !SipHash_set_hash_size(&ctx->siphash, size)
             || !SipHash_set_hash_size(&ctx->sipcopy, size))
             return 0;
     }
-    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_C_ROUNDS)) != NULL
-            && !OSSL_PARAM_get_uint(p, &ctx->crounds))
+    if (p.c != NULL && !OSSL_PARAM_get_uint(p.c, &ctx->crounds))
         return 0;
-    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_D_ROUNDS)) != NULL
-            && !OSSL_PARAM_get_uint(p, &ctx->drounds))
+    if (p.d != NULL && !OSSL_PARAM_get_uint(p.d, &ctx->drounds))
         return 0;
-    if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL)
-        if (p->data_type != OSSL_PARAM_OCTET_STRING
-            || !siphash_setkey(ctx, p->data, p->data_size))
+    if (p.key != NULL)
+        if (p.key->data_type != OSSL_PARAM_OCTET_STRING
+            || !siphash_setkey(ctx, p.key->data, p.key->data_size))
             return 0;
     return 1;
 }