diff options
author | Eric Biggers <ebiggers@kernel.org> | 2025-06-30 09:06:36 -0700 |
---|---|---|
committer | Eric Biggers <ebiggers@kernel.org> | 2025-07-04 10:18:53 -0700 |
commit | 6fa4b292204b15e0e269a9fd33bc99b5e36b6883 (patch) | |
tree | 7798cc81d1595afe16918837c5cb5f9be1ab5886 | |
parent | 9f97707bdb1e479ea15e14e5525164f5f1128e97 (diff) |
lib/crypto: sha256: Add sha224() and sha224_update()
Add a one-shot SHA-224 computation function sha224(), for consistency
with sha256(), sha384(), and sha512() which all already exist.
Similarly, add sha224_update(). While for now it's identical to
sha256_update(), omitting it makes the API harder to use since users
have to "know" which functions are the same between SHA-224 and SHA-256.
Also, this is a prerequisite for using different context types for each.
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20250630160645.3198-6-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
-rw-r--r-- | include/crypto/sha2.h | 10 | ||||
-rw-r--r-- | lib/crypto/sha256.c | 10 |
2 files changed, 18 insertions, 2 deletions
diff --git a/include/crypto/sha2.h b/include/crypto/sha2.h index bb181b7996cd..e31da0743a52 100644 --- a/include/crypto/sha2.h +++ b/include/crypto/sha2.h @@ -114,18 +114,24 @@ struct sha512_state { u8 buf[SHA512_BLOCK_SIZE]; }; +void sha256_update(struct sha256_state *sctx, const u8 *data, size_t len); + static inline void sha224_init(struct sha256_state *sctx) { sha224_block_init(&sctx->ctx); } -/* Simply use sha256_update as it is equivalent to sha224_update. */ +static inline void sha224_update(struct sha256_state *sctx, + const u8 *data, size_t len) +{ + sha256_update(sctx, data, len); +} void sha224_final(struct sha256_state *sctx, u8 out[SHA224_DIGEST_SIZE]); +void sha224(const u8 *data, size_t len, u8 out[SHA224_DIGEST_SIZE]); static inline void sha256_init(struct sha256_state *sctx) { sha256_block_init(&sctx->ctx); } -void sha256_update(struct sha256_state *sctx, const u8 *data, size_t len); void sha256_final(struct sha256_state *sctx, u8 out[SHA256_DIGEST_SIZE]); void sha256(const u8 *data, size_t len, u8 out[SHA256_DIGEST_SIZE]); diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c index 573ccecbf48b..ccaae7088016 100644 --- a/lib/crypto/sha256.c +++ b/lib/crypto/sha256.c @@ -70,6 +70,16 @@ void sha256_final(struct sha256_state *sctx, u8 out[SHA256_DIGEST_SIZE]) } EXPORT_SYMBOL(sha256_final); +void sha224(const u8 *data, size_t len, u8 out[SHA224_DIGEST_SIZE]) +{ + struct sha256_state sctx; + + sha224_init(&sctx); + sha224_update(&sctx, data, len); + sha224_final(&sctx, out); +} +EXPORT_SYMBOL(sha224); + void sha256(const u8 *data, size_t len, u8 out[SHA256_DIGEST_SIZE]) { struct sha256_state sctx; |