From: Collin Funk Date: Wed, 3 Sep 2025 02:16:17 +0000 (-0700) Subject: crypto/sha3-buffer: Don't abort on OOM. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=06e7da510bd055f524c1baff0890c861bef2b6a4;p=thirdparty%2Fgnulib.git crypto/sha3-buffer: Don't abort on OOM. * modules/crypto/sha3-buffer (Depends-on): Add stddef-h. * lib/sha3.h: Include stddef.h. (sha3_ctx): Use a stack allocated buffer used to cast to store an EVP_MD_CTX. * lib/sha3.c (DEFINE_SHA3_INIT_CTX): Initialize the structure to zero. Use the stack allocated buffer instead of calling EVP_MD_CTX_create. (sha3_read_ctx): Just call sha3_finish_ctx similar to the other crypto modules. (sha3_finish_ctx): Remove call to EVP_MD_CTX_free. Call EVP_DigestFinal_ex. --- diff --git a/ChangeLog b/ChangeLog index a016eec8f0..3752bb9198 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,17 @@ 2025-09-03 Collin Funk + crypto/sha3-buffer: Don't abort on OOM. + * modules/crypto/sha3-buffer (Depends-on): Add stddef-h. + * lib/sha3.h: Include stddef.h. + (sha3_ctx): Use a stack allocated buffer used to cast to store an + EVP_MD_CTX. + * lib/sha3.c (DEFINE_SHA3_INIT_CTX): Initialize the structure to zero. + Use the stack allocated buffer instead of calling EVP_MD_CTX_create. + (sha3_read_ctx): Just call sha3_finish_ctx similar to the other crypto + modules. + (sha3_finish_ctx): Remove call to EVP_MD_CTX_free. Call + EVP_DigestFinal_ex. + gnulib-tool: Avoid Automake error when using --create-testdir (regr. today). * gnulib-tool.sh (func_emit_tests_Makefile_am): Initialize AM_CFLAGS and AM_CXXFLAGS when creating a test directory. diff --git a/lib/sha3.c b/lib/sha3.c index 5da85db5f2..e30210fb3e 100644 --- a/lib/sha3.c +++ b/lib/sha3.c @@ -323,11 +323,11 @@ sha3_process_block (const void *buffer, size_t len, struct sha3_ctx *ctx) void \ sha3_##SIZE##_init_ctx (struct sha3_ctx *ctx) \ { \ - int rc; \ - ctx->evp_ctx = EVP_MD_CTX_create (); \ - if (ctx->evp_ctx == NULL) \ - abort (); \ - rc = EVP_DigestInit_ex (ctx->evp_ctx, EVP_sha3_##SIZE (), NULL); \ + /* EVP_DigestInit_ex expects all bytes to be zero. */ \ + memset (ctx, 0, sizeof *ctx); \ + EVP_MD_CTX *evp_ctx = (EVP_MD_CTX *) ctx->evp_ctx_buffer; \ + int rc = EVP_DigestInit_ex (evp_ctx, EVP_sha3_##SIZE (), NULL); \ + /* This should never fail. */ \ if (rc == 0) \ abort (); \ } @@ -341,17 +341,17 @@ void * sha3_read_ctx (const struct sha3_ctx *ctx, void *resbuf) { /* Assume any unprocessed bytes in ctx are not to be ignored. */ - int result = EVP_DigestFinal_ex (ctx->evp_ctx, resbuf, NULL); - if (result == 0) - abort (); - return resbuf; + return sha3_finish_ctx ((struct sha3_ctx *) ctx, resbuf); } void * sha3_finish_ctx (struct sha3_ctx *ctx, void *resbuf) { - void *result = sha3_read_ctx (ctx, resbuf); - EVP_MD_CTX_free (ctx->evp_ctx); + EVP_MD_CTX *evp_ctx = (EVP_MD_CTX *) ctx->evp_ctx_buffer; + /* This should never fail. */ + int result = EVP_DigestFinal_ex (evp_ctx, resbuf, NULL); + if (result == 0) + abort (); return resbuf; } @@ -373,7 +373,8 @@ DEFINE_SHA3_BUFFER (512) void sha3_process_bytes (const void *buffer, size_t len, struct sha3_ctx *ctx) { - int result = EVP_DigestUpdate (ctx->evp_ctx, buffer, len); + EVP_MD_CTX *evp_ctx = (EVP_MD_CTX *) ctx->evp_ctx_buffer; + int result = EVP_DigestUpdate (evp_ctx, buffer, len); if (result == 0) abort (); } diff --git a/lib/sha3.h b/lib/sha3.h index 7de1216fda..adce43eac7 100644 --- a/lib/sha3.h +++ b/lib/sha3.h @@ -19,6 +19,7 @@ #ifndef SHA3_H # define SHA3_H 1 +# include # include # include @@ -50,9 +51,9 @@ enum { SHA3_512_BLOCK_SIZE = 576 / 8 }; struct sha3_ctx { # if HAVE_OPENSSL_SHA3 - /* This is an incomplete type, so we can only place a pointer in the - struct. */ - EVP_MD_CTX *evp_ctx; + /* EVP_MD_CTX is an incomplete type. EVP_MD_CTX_create allocates 72 bytes of + memory as of 2025-09-02. */ + max_align_t evp_ctx_buffer[256 / sizeof (max_align_t)]; # else u64 state[25]; uint8_t buffer[144]; /* Up to BLOCKLEN in use. */ diff --git a/modules/crypto/sha3-buffer b/modules/crypto/sha3-buffer index 83b3a090ff..e0e654e563 100644 --- a/modules/crypto/sha3-buffer +++ b/modules/crypto/sha3-buffer @@ -10,6 +10,7 @@ m4/sha3.m4 Depends-on: byteswap c99 +stddef-h stdint-h u64