2025-09-06 Collin Funk <collin.funk1@gmail.com>
+ crypto/sha3-buffer: Set errno when OpenSSL functions fail.
+ * lib/sha3.c: Include <errno.h>
+ (DEFINE_SHA3_INIT_CTX): Set errno to ENOMEM if function fails.
+ (sha3_finish_ctx, sha3_process_block): Set errno to EINVAL on failure.
+
crypto/sha3, crypto/sha3-buffer: Don't leak memory when using OpenSSL.
Reported by Pádraig Brady in:
<https://lists.gnu.org/archive/html/bug-gnulib/2025-09/msg00058.html>.
#else /* OpenSSL implementation. */
+/* We avoid using all of EVP error strings. Just guess a reasonable errno. */
+#include <errno.h>
+
#define DEFINE_SHA3_INIT_CTX(SIZE) \
bool \
sha3_##SIZE##_init_ctx (struct sha3_ctx *ctx) \
int result; \
ctx->evp_ctx = EVP_MD_CTX_create (); \
if (ctx->evp_ctx == NULL) \
- return false; \
+ { \
+ errno = ENOMEM; \
+ return false; \
+ } \
result = EVP_DigestInit_ex (ctx->evp_ctx, EVP_sha3_##SIZE (), \
NULL); \
if (result == 0) \
{ \
+ errno = ENOMEM; \
sha3_free_ctx (ctx); \
return false; \
} \
int result = EVP_DigestFinal_ex (ctx->evp_ctx, resbuf, NULL);
sha3_free_ctx (ctx);
if (result == 0)
- return NULL;
+ {
+ errno = EINVAL;
+ return NULL;
+ }
return resbuf;
}
int result = EVP_DigestUpdate (ctx->evp_ctx, buffer, len);
if (result == 0)
{
+ errno = EINVAL;
sha3_free_ctx (ctx);
return false;
}