]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
SHA3 - Move the buffered absorb function into sha3.c
authorslontis <shane.lontis@oracle.com>
Fri, 17 Oct 2025 05:11:11 +0000 (16:11 +1100)
committerslontis <shane.lontis@oracle.com>
Tue, 17 Feb 2026 01:16:50 +0000 (12:16 +1100)
This code was sitting inside the sha3 provider where it could not be
called directly.

Reviewed-by: Paul Dale <paul.dale@oracle.com>
Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/28941)

crypto/sha/sha3.c
include/internal/sha3.h
providers/implementations/digests/sha3_prov.c

index 21e1070beed2705b22c78f39fafdc03c10375565..226f9113066f2e6e7dced648dc28aeff528f7a61 100644 (file)
@@ -12,6 +12,7 @@
 #include "crypto/s390x_arch.h"
 #endif
 #include "internal/sha3.h"
+#include "internal/common.h"
 
 void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r, int next);
 
@@ -127,6 +128,43 @@ int ossl_sha3_final(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen)
     return 1;
 }
 
+/* This is a buffered absorb function. */
+int ossl_sha3_absorb(KECCAK1600_CTX *ctx, const unsigned char *inp, size_t len)
+{
+    const size_t bsz = ctx->block_size;
+    size_t num, rem;
+
+    if (ossl_unlikely(len == 0))
+        return 1;
+
+    /* Is there anything in the buffer already ? */
+    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 */
+        if (len < rem) {
+            memcpy(ctx->buf + num, inp, len);
+            ctx->bufsz += len;
+            return 1;
+        }
+        /* otherwise fill up the buffer and absorb the buffer */
+        memcpy(ctx->buf + num, inp, rem);
+        /* Update the input pointer */
+        inp += rem;
+        len -= rem;
+        ctx->meth.absorb(ctx, ctx->buf, bsz);
+        ctx->bufsz = 0;
+    }
+    /* 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 (ossl_likely(rem)) {
+        memcpy(ctx->buf, inp + len - rem, rem);
+        ctx->bufsz = rem;
+    }
+    return 1;
+}
+
 /*
  * This method can be called multiple times.
  * Rather than heavily modifying assembler for SHA3_squeeze(),
index d52780a660f98dc5adde89ddde92b25f3886f0b7..6b3f76a5b822ca9016f424d697f95e58f8e162db 100644 (file)
@@ -55,6 +55,7 @@ int ossl_keccak_init(KECCAK1600_CTX *ctx, unsigned char pad,
 int ossl_sha3_update(KECCAK1600_CTX *ctx, const void *_inp, size_t len);
 int ossl_sha3_final(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen);
 int ossl_sha3_squeeze(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen);
+int ossl_sha3_absorb(KECCAK1600_CTX *ctx, const unsigned char *inp, size_t len);
 
 size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len,
     size_t r);
index 57adcc2efd28e84ce46cf3e3eea3f9e24115f880..38b7c6ee77b077da2f1f646d5fca3aced1da9f12 100644 (file)
@@ -103,39 +103,7 @@ static int keccak_init_params(void *vctx, const OSSL_PARAM params[])
 
 static int keccak_update(void *vctx, const unsigned char *inp, size_t len)
 {
-    KECCAK1600_CTX *ctx = vctx;
-    const size_t bsz = ctx->block_size;
-    size_t num, rem;
-
-    if (ossl_unlikely(len == 0))
-        return 1;
-
-    /* Is there anything in the buffer already ? */
-    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 */
-        if (len < rem) {
-            memcpy(ctx->buf + num, inp, len);
-            ctx->bufsz += len;
-            return 1;
-        }
-        /* otherwise fill up the buffer and absorb the buffer */
-        memcpy(ctx->buf + num, inp, rem);
-        /* Update the input pointer */
-        inp += rem;
-        len -= rem;
-        ctx->meth.absorb(ctx, ctx->buf, bsz);
-        ctx->bufsz = 0;
-    }
-    /* 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 (ossl_likely(rem)) {
-        memcpy(ctx->buf, inp + len - rem, rem);
-        ctx->bufsz = rem;
-    }
-    return 1;
+    return ossl_sha3_absorb((KECCAK1600_CTX *)vctx, inp, len);
 }
 
 static int keccak_final(void *vctx, unsigned char *out, size_t *outl,