diff mbox series

[6/8] crypto: lib/sm3 - Export generic block function

Message ID e77446f9cf0598d63dd4a766fd69a8d67d9ea94b.1744455146.git.herbert@gondor.apana.org.au (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show
Series crypto: hash - Preparation for block-only shash | expand

Commit Message

Herbert Xu April 12, 2025, 10:57 a.m. UTC
Export the generic block function so that it can be used by the
Crypto API.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
---
 include/crypto/sm3.h |  1 +
 lib/crypto/sm3.c     | 25 ++++++++++++-------------
 2 files changed, 13 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/include/crypto/sm3.h b/include/crypto/sm3.h
index 1f021ad0533f..d49211ba9a20 100644
--- a/include/crypto/sm3.h
+++ b/include/crypto/sm3.h
@@ -58,6 +58,7 @@  static inline void sm3_init(struct sm3_state *sctx)
 	sctx->count = 0;
 }
 
+void sm3_block_generic(struct sm3_state *sctx, u8 const *data, int blocks);
 void sm3_update(struct sm3_state *sctx, const u8 *data, unsigned int len);
 void sm3_final(struct sm3_state *sctx, u8 *out);
 
diff --git a/lib/crypto/sm3.c b/lib/crypto/sm3.c
index 18c2fb73ba16..de64aa913280 100644
--- a/lib/crypto/sm3.c
+++ b/lib/crypto/sm3.c
@@ -166,19 +166,22 @@  static void sm3_transform(struct sm3_state *sctx, u8 const *data, u32 W[16])
 #undef W1
 #undef W2
 
-static inline void sm3_block(struct sm3_state *sctx,
-		u8 const *data, int blocks, u32 W[16])
+void sm3_block_generic(struct sm3_state *sctx, u8 const *data, int blocks)
 {
-	while (blocks--) {
+	u32 W[16];
+
+	do {
 		sm3_transform(sctx, data, W);
 		data += SM3_BLOCK_SIZE;
-	}
+	} while (--blocks);
+
+	memzero_explicit(W, sizeof(W));
 }
+EXPORT_SYMBOL_GPL(sm3_block_generic);
 
 void sm3_update(struct sm3_state *sctx, const u8 *data, unsigned int len)
 {
 	unsigned int partial = sctx->count % SM3_BLOCK_SIZE;
-	u32 W[16];
 
 	sctx->count += len;
 
@@ -192,19 +195,17 @@  void sm3_update(struct sm3_state *sctx, const u8 *data, unsigned int len)
 			data += p;
 			len -= p;
 
-			sm3_block(sctx, sctx->buffer, 1, W);
+			sm3_block_generic(sctx, sctx->buffer, 1);
 		}
 
 		blocks = len / SM3_BLOCK_SIZE;
 		len %= SM3_BLOCK_SIZE;
 
 		if (blocks) {
-			sm3_block(sctx, data, blocks, W);
+			sm3_block_generic(sctx, data, blocks);
 			data += blocks * SM3_BLOCK_SIZE;
 		}
 
-		memzero_explicit(W, sizeof(W));
-
 		partial = 0;
 	}
 	if (len)
@@ -218,7 +219,6 @@  void sm3_final(struct sm3_state *sctx, u8 *out)
 	__be64 *bits = (__be64 *)(sctx->buffer + bit_offset);
 	__be32 *digest = (__be32 *)out;
 	unsigned int partial = sctx->count % SM3_BLOCK_SIZE;
-	u32 W[16];
 	int i;
 
 	sctx->buffer[partial++] = 0x80;
@@ -226,18 +226,17 @@  void sm3_final(struct sm3_state *sctx, u8 *out)
 		memset(sctx->buffer + partial, 0, SM3_BLOCK_SIZE - partial);
 		partial = 0;
 
-		sm3_block(sctx, sctx->buffer, 1, W);
+		sm3_block_generic(sctx, sctx->buffer, 1);
 	}
 
 	memset(sctx->buffer + partial, 0, bit_offset - partial);
 	*bits = cpu_to_be64(sctx->count << 3);
-	sm3_block(sctx, sctx->buffer, 1, W);
+	sm3_block_generic(sctx, sctx->buffer, 1);
 
 	for (i = 0; i < 8; i++)
 		put_unaligned_be32(sctx->state[i], digest++);
 
 	/* Zeroize sensitive information. */
-	memzero_explicit(W, sizeof(W));
 	memzero_explicit(sctx, sizeof(*sctx));
 }
 EXPORT_SYMBOL_GPL(sm3_final);