All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>,
	"Jason A . Donenfeld" <Jason@zx2c4.com>,
	linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org,
	linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org,
	linux-s390@vger.kernel.org, sparclinux@vger.kernel.org,
	x86@kernel.org, Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 09/18] lib/crypto: sha256: Add sha224() and sha224_update()
Date: Wed, 25 Jun 2025 00:08:10 -0700	[thread overview]
Message-ID: <20250625070819.1496119-10-ebiggers@kernel.org> (raw)
In-Reply-To: <20250625070819.1496119-1-ebiggers@kernel.org>

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.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 include/crypto/sha2.h           | 10 ++++++++--
 lib/crypto/sha256.c             | 10 ++++++++++
 lib/crypto/tests/sha224_kunit.c | 13 +------------
 3 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/include/crypto/sha2.h b/include/crypto/sha2.h
index bb181b7996cdc..e31da0743a522 100644
--- a/include/crypto/sha2.h
+++ b/include/crypto/sha2.h
@@ -112,22 +112,28 @@ struct sha512_state {
 	u64 state[SHA512_DIGEST_SIZE / 8];
 	u64 count[2];
 	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]);
 
 /* State for the SHA-512 (and SHA-384) compression function */
 struct sha512_block_state {
diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c
index 573ccecbf48bf..ccaae70880166 100644
--- a/lib/crypto/sha256.c
+++ b/lib/crypto/sha256.c
@@ -68,10 +68,20 @@ void sha256_final(struct sha256_state *sctx, u8 out[SHA256_DIGEST_SIZE])
 {
 	__sha256_final(sctx, 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;
 
 	sha256_init(&sctx);
diff --git a/lib/crypto/tests/sha224_kunit.c b/lib/crypto/tests/sha224_kunit.c
index 5015861a55112..c484c1d4a2a5e 100644
--- a/lib/crypto/tests/sha224_kunit.c
+++ b/lib/crypto/tests/sha224_kunit.c
@@ -3,26 +3,15 @@
  * Copyright 2025 Google LLC
  */
 #include <crypto/sha2.h>
 #include "sha224-testvecs.h"
 
-/* TODO: add sha224() to the library itself */
-static inline void sha224(const u8 *data, size_t len,
-			  u8 out[SHA224_DIGEST_SIZE])
-{
-	struct sha256_state state;
-
-	sha224_init(&state);
-	sha256_update(&state, data, len);
-	sha224_final(&state, out);
-}
-
 #define HASH sha224
 #define HASH_CTX sha256_state
 #define HASH_SIZE SHA224_DIGEST_SIZE
 #define HASH_INIT sha224_init
-#define HASH_UPDATE sha256_update
+#define HASH_UPDATE sha224_update
 #define HASH_FINAL sha224_final
 #define HASH_TESTVECS sha224_testvecs
 /* TODO: add HMAC-SHA224 support to the library, then enable the tests for it */
 #include "hash-test-template.h"
 
-- 
2.50.0



WARNING: multiple messages have this Message-ID (diff)
From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>,
	"Jason A . Donenfeld" <Jason@zx2c4.com>,
	linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org,
	linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org,
	linux-s390@vger.kernel.org, sparclinux@vger.kernel.org,
	x86@kernel.org, Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 09/18] lib/crypto: sha256: Add sha224() and sha224_update()
Date: Wed, 25 Jun 2025 00:08:10 -0700	[thread overview]
Message-ID: <20250625070819.1496119-10-ebiggers@kernel.org> (raw)
In-Reply-To: <20250625070819.1496119-1-ebiggers@kernel.org>

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.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 include/crypto/sha2.h           | 10 ++++++++--
 lib/crypto/sha256.c             | 10 ++++++++++
 lib/crypto/tests/sha224_kunit.c | 13 +------------
 3 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/include/crypto/sha2.h b/include/crypto/sha2.h
index bb181b7996cdc..e31da0743a522 100644
--- a/include/crypto/sha2.h
+++ b/include/crypto/sha2.h
@@ -112,22 +112,28 @@ struct sha512_state {
 	u64 state[SHA512_DIGEST_SIZE / 8];
 	u64 count[2];
 	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]);
 
 /* State for the SHA-512 (and SHA-384) compression function */
 struct sha512_block_state {
diff --git a/lib/crypto/sha256.c b/lib/crypto/sha256.c
index 573ccecbf48bf..ccaae70880166 100644
--- a/lib/crypto/sha256.c
+++ b/lib/crypto/sha256.c
@@ -68,10 +68,20 @@ void sha256_final(struct sha256_state *sctx, u8 out[SHA256_DIGEST_SIZE])
 {
 	__sha256_final(sctx, 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;
 
 	sha256_init(&sctx);
diff --git a/lib/crypto/tests/sha224_kunit.c b/lib/crypto/tests/sha224_kunit.c
index 5015861a55112..c484c1d4a2a5e 100644
--- a/lib/crypto/tests/sha224_kunit.c
+++ b/lib/crypto/tests/sha224_kunit.c
@@ -3,26 +3,15 @@
  * Copyright 2025 Google LLC
  */
 #include <crypto/sha2.h>
 #include "sha224-testvecs.h"
 
-/* TODO: add sha224() to the library itself */
-static inline void sha224(const u8 *data, size_t len,
-			  u8 out[SHA224_DIGEST_SIZE])
-{
-	struct sha256_state state;
-
-	sha224_init(&state);
-	sha256_update(&state, data, len);
-	sha224_final(&state, out);
-}
-
 #define HASH sha224
 #define HASH_CTX sha256_state
 #define HASH_SIZE SHA224_DIGEST_SIZE
 #define HASH_INIT sha224_init
-#define HASH_UPDATE sha256_update
+#define HASH_UPDATE sha224_update
 #define HASH_FINAL sha224_final
 #define HASH_TESTVECS sha224_testvecs
 /* TODO: add HMAC-SHA224 support to the library, then enable the tests for it */
 #include "hash-test-template.h"
 
-- 
2.50.0


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  parent reply	other threads:[~2025-06-25  9:53 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-25  7:08 [PATCH 00/18] SHA-256 library improvements Eric Biggers
2025-06-25  7:08 ` Eric Biggers
2025-06-25  7:08 ` [PATCH 01/18] libceph: Rename hmac_sha256() to ceph_hmac_sha256() Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 02/18] cxl/test: Simplify fw_buf_checksum_show() Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 03/18] crypto: sha512 - Use the correct legacy export format Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 04/18] lib/crypto: sha512: Reorder some code in sha512.c Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 05/18] lib/crypto: sha512: Do not include <crypto/internal/sha2.h> Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 06/18] lib/crypto: sha512: Fix a grammatical error in kerneldoc comments Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 07/18] lib/crypto: sha256: Reorder some code Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 08/18] lib/crypto: sha256: Remove sha256_blocks_simd() Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` Eric Biggers [this message]
2025-06-25  7:08   ` [PATCH 09/18] lib/crypto: sha256: Add sha224() and sha224_update() Eric Biggers
2025-06-25  7:08 ` [PATCH 10/18] lib/crypto: sha256: Make library API use strongly-typed contexts Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 11/18] lib/crypto: sha256: Propagate sha256_block_state type to implementations Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 12/18] lib/crypto: sha256: Add HMAC-SHA224 and HMAC-SHA256 support Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 13/18] crypto: sha256 - Wrap library and add HMAC support Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 14/18] crypto: sha256 - Use same state format as legacy drivers Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 15/18] lib/crypto: sha512: Remove sha256_is_arch_optimized() Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 16/18] lib/crypto: sha256: Consolidate into single module Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 17/18] lib/crypto: sha256: Sync sha256_update() with sha512_update() Eric Biggers
2025-06-25  7:08   ` Eric Biggers
2025-06-25  7:08 ` [PATCH 18/18] lib/crypto: sha256: Document the SHA-224 and SHA-256 API Eric Biggers
2025-06-25  7:08   ` Eric Biggers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250625070819.1496119-10-ebiggers@kernel.org \
    --to=ebiggers@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=ardb@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=sparclinux@vger.kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.