refactor(crypto): publish keccak_Init with configurable bit size
What changed, and why it matters
This commit is a straightforward code cleanup: it exposes a single internal SHA3/Keccak initialization function so other code (specifically Rust) can call it directly with a chosen hash size, instead of going through four separate wrapper functions. There is no bug fix, behavior change, or security patch in the diff.
No security action required. Treat as a normal refactoring/API-visibility change during routine review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change adds a new public sha3_Init(SHA3_CTX *ctx, unsigned bits) function that validates bits against the allowed SHA3 output sizes (224, 256, 384, 512) and then delegates to the existing static keccak_Init(). It also adds the necessary standard headers (stddef.h, stdbool.h) to sha3.h. The existing fixed-size init functions (sha3_224_Init, etc.) are untouched. No memory safety, cryptographic, or logic flaws are introduced by this refactor.
Changed components
crypto/sha3.ccrypto/sha3.hInspect captured patch +18 / −0
### crypto/sha3.c
@@ -54,6 +54,21 @@ static void keccak_Init(SHA3_CTX *ctx, unsigned bits)
assert(rate <= 1600 && (rate % 64) == 0);
}
+/** Initialize a sha3/keccak context for given number of output bits.
+ *
+ * Returns false if the number of output bits is invalid, true otherwise.
+ * (with a valid number of bits, the initialization cannot fail).
+ */
+bool sha3_Init(SHA3_CTX *ctx, unsigned bits)
+{
+ if (bits != 224 && bits != 256 && bits != 384 && bits != 512) {
+ return false;
+ }
+
+ keccak_Init(ctx, bits);
+ return true;
+}
+
/**
* Initialize context before calculating hash.
*
### crypto/sha3.h
@@ -20,7 +20,9 @@
#ifndef __SHA3_H__
#define __SHA3_H__
+#include <stddef.h>
#include <stdint.h>
+#include <stdbool.h>
#include "options.h"
#ifdef __cplusplus
@@ -65,6 +67,7 @@ void sha3_224_Init(SHA3_CTX *ctx);
void sha3_256_Init(SHA3_CTX *ctx);
void sha3_384_Init(SHA3_CTX *ctx);
void sha3_512_Init(SHA3_CTX *ctx);
+bool sha3_Init(SHA3_CTX *ctx, unsigned bits);
void sha3_Update(SHA3_CTX *ctx, const unsigned char* msg, size_t size);
void sha3_Final(SHA3_CTX *ctx, unsigned char* result);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.