]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
providers/implementations/digests/sha3_prov.c optimize ossl_(un)likely
authorNorbert Pocs <norbertp@openssl.org>
Thu, 3 Jul 2025 14:31:42 +0000 (16:31 +0200)
committerNeil Horman <nhorman@openssl.org>
Wed, 16 Jul 2025 17:07:15 +0000 (13:07 -0400)
Signed-off-by: Norbert Pocs <norbertp@openssl.org>
Reviewed-by: Neil Horman <nhorman@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/27961)

providers/implementations/digests/sha3_prov.c

index 407089d98fa37375ca7cb26584b72adaa7e11bbb..a006cf485144df98d68fe01b00ed2a96cf6e532c 100644 (file)
@@ -18,6 +18,7 @@
 #include "internal/sha3.h"
 #include "prov/digestcommon.h"
 #include "prov/implementations.h"
+#include "internal/common.h"
 
 #define SHA3_FLAGS PROV_DIGEST_FLAG_ALGID_ABSENT
 #define SHAKE_FLAGS (PROV_DIGEST_FLAG_XOF | PROV_DIGEST_FLAG_ALGID_ABSENT)
@@ -58,7 +59,7 @@ static sha3_squeeze_fn generic_sha3_squeeze;
 
 static int keccak_init(void *vctx, ossl_unused const OSSL_PARAM params[])
 {
-    if (!ossl_prov_is_running())
+    if (ossl_unlikely(!ossl_prov_is_running()))
         return 0;
     /* The newctx() handles most of the ctx fixed setup. */
     ossl_sha3_reset((KECCAK1600_CTX *)vctx);
@@ -77,11 +78,11 @@ static int keccak_update(void *vctx, const unsigned char *inp, size_t len)
     const size_t bsz = ctx->block_size;
     size_t num, rem;
 
-    if (len == 0)
+    if (ossl_unlikely(len == 0))
         return 1;
 
     /* Is there anything in the buffer already ? */
-    if ((num = ctx->bufsz) != 0) {
+    if (ossl_likely((num = ctx->bufsz) != 0)) {
         /* Calculate how much space is left in the buffer */
         rem = bsz - num;
         /* If the new input does not fill the buffer then just add it */
@@ -101,7 +102,7 @@ static int keccak_update(void *vctx, const unsigned char *inp, size_t len)
     /* Absorb the input - rem = leftover part of the input < blocksize) */
     rem = ctx->meth.absorb(ctx, inp, len);
     /* Copy the leftover bit of the input into the buffer */
-    if (rem) {
+    if (ossl_likely(rem)) {
         memcpy(ctx->buf, inp + len - rem, rem);
         ctx->bufsz = rem;
     }
@@ -114,13 +115,13 @@ static int keccak_final(void *vctx, unsigned char *out, size_t *outl,
     int ret = 1;
     KECCAK1600_CTX *ctx = vctx;
 
-    if (!ossl_prov_is_running())
+    if (ossl_unlikely(!ossl_prov_is_running()))
         return 0;
-    if (ctx->md_size == SIZE_MAX) {
+    if (ossl_unlikely(ctx->md_size == SIZE_MAX)) {
         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
         return 0;
     }
-    if (outlen > 0)
+    if (ossl_likely(outlen > 0))
         ret = ctx->meth.final(ctx, out, ctx->md_size);
 
     *outl = ctx->md_size;
@@ -633,16 +634,16 @@ static int shake_set_ctx_params(void *vctx, const OSSL_PARAM params[])
     const OSSL_PARAM *p;
     KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx;
 
-    if (ctx == NULL)
+    if (ossl_unlikely(ctx == NULL))
         return 0;
     if (ossl_param_is_empty(params))
         return 1;
 
     p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_XOFLEN);
-    if (p == NULL)
+    if (ossl_unlikely(p == NULL))
         p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE);
 
-    if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->md_size)) {
+    if (ossl_unlikely(p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->md_size))) {
         ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
         return 0;
     }