[PATCH 2/3] bulkhelp: change bulk function definition to allow modifying context

Jussi Kivilinna jussi.kivilinna at iki.fi
Wed Jan 4 18:53:04 CET 2023


* cipher/bulkhelp.h (bulk_crypt_fn_t): Make 'ctx' non-constant and
change 'num_blks' from 'unsigned int' to 'size_t'.
* cipher/camellia-glue.c (camellia_encrypt_blk1_32)
(camellia_encrypt_blk1_64, camellia_decrypt_blk1_32)
(camellia_decrypt_blk1_64): Adjust to match 'bulk_crypt_fn_t'.
* cipher/serpent.c (serpent_crypt_blk1_16, serpent_encrypt_blk1_16)
(serpent_decrypt_blk1_16): Likewise.
* cipher/sm4.c (crypt_blk1_16_fn_t, _gcry_sm4_aesni_avx_crypt_blk1_8)
(sm4_aesni_avx_crypt_blk1_16, _gcry_sm4_aesni_avx2_crypt_blk1_16)
(sm4_aesni_avx2_crypt_blk1_16, _gcry_sm4_gfni_avx2_crypt_blk1_16)
(sm4_gfni_avx2_crypt_blk1_16, _gcry_sm4_gfni_avx512_crypt_blk1_16)
(_gcry_sm4_gfni_avx512_crypt_blk32, sm4_gfni_avx512_crypt_blk1_16)
(_gcry_sm4_aarch64_crypt_blk1_8, sm4_aarch64_crypt_blk1_16)
(_gcry_sm4_armv8_ce_crypt_blk1_8, sm4_armv8_ce_crypt_blk1_16)
(_gcry_sm4_armv9_sve_ce_crypt, sm4_armv9_sve_ce_crypt_blk1_16)
(sm4_crypt_blocks, sm4_crypt_blk1_32, sm4_encrypt_blk1_32)
(sm4_decrypt_blk1_32): Likewise.
* cipher/twofish.c (twofish_crypt_blk1_16, twofish_encrypt_blk1_16)
(twofish_decrypt_blk1_16): Likewise.
--

Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 cipher/bulkhelp.h      |  4 +--
 cipher/camellia-glue.c | 20 ++++++-------
 cipher/serpent.c       | 14 ++++-----
 cipher/sm4.c           | 68 ++++++++++++++++++++----------------------
 cipher/twofish.c       | 14 ++++-----
 5 files changed, 59 insertions(+), 61 deletions(-)

diff --git a/cipher/bulkhelp.h b/cipher/bulkhelp.h
index b86abc27..833262e2 100644
--- a/cipher/bulkhelp.h
+++ b/cipher/bulkhelp.h
@@ -32,9 +32,9 @@ typedef u64 ocb_L_uintptr_t;
 typedef uintptr_t ocb_L_uintptr_t;
 #endif
 
-typedef unsigned int (*bulk_crypt_fn_t) (const void *ctx, byte *out,
+typedef unsigned int (*bulk_crypt_fn_t) (void *ctx, byte *out,
                                          const byte *in,
-                                         unsigned int num_blks);
+                                         size_t num_blks);
 
 
 static inline ocb_L_uintptr_t *
diff --git a/cipher/camellia-glue.c b/cipher/camellia-glue.c
index a81d586a..2e00f563 100644
--- a/cipher/camellia-glue.c
+++ b/cipher/camellia-glue.c
@@ -616,8 +616,8 @@ camellia_decrypt(void *c, byte *outbuf, const byte *inbuf)
 
 
 static unsigned int
-camellia_encrypt_blk1_32 (const void *priv, byte *outbuf, const byte *inbuf,
-                          unsigned int num_blks)
+camellia_encrypt_blk1_32 (void *priv, byte *outbuf, const byte *inbuf,
+			  size_t num_blks)
 {
   const CAMELLIA_context *ctx = priv;
   unsigned int stack_burn_size = 0;
@@ -664,10 +664,10 @@ camellia_encrypt_blk1_32 (const void *priv, byte *outbuf, const byte *inbuf,
 }
 
 static unsigned int
-camellia_encrypt_blk1_64 (const void *priv, byte *outbuf, const byte *inbuf,
-                          unsigned int num_blks)
+camellia_encrypt_blk1_64 (void *priv, byte *outbuf, const byte *inbuf,
+			  size_t num_blks)
 {
-  const CAMELLIA_context *ctx = priv;
+  CAMELLIA_context *ctx = priv;
   unsigned int stack_burn_size = 0;
   unsigned int nburn;
 
@@ -696,8 +696,8 @@ camellia_encrypt_blk1_64 (const void *priv, byte *outbuf, const byte *inbuf,
 }
 
 static unsigned int
-camellia_decrypt_blk1_32 (const void *priv, byte *outbuf, const byte *inbuf,
-                          unsigned int num_blks)
+camellia_decrypt_blk1_32 (void *priv, byte *outbuf, const byte *inbuf,
+			  size_t num_blks)
 {
   const CAMELLIA_context *ctx = priv;
   unsigned int stack_burn_size = 0;
@@ -744,10 +744,10 @@ camellia_decrypt_blk1_32 (const void *priv, byte *outbuf, const byte *inbuf,
 }
 
 static unsigned int
-camellia_decrypt_blk1_64 (const void *priv, byte *outbuf, const byte *inbuf,
-                          unsigned int num_blks)
+camellia_decrypt_blk1_64 (void *priv, byte *outbuf, const byte *inbuf,
+			  size_t num_blks)
 {
-  const CAMELLIA_context *ctx = priv;
+  CAMELLIA_context *ctx = priv;
   unsigned int stack_burn_size = 0;
   unsigned int nburn;
 
diff --git a/cipher/serpent.c b/cipher/serpent.c
index 0a9ed27c..8fa47c7c 100644
--- a/cipher/serpent.c
+++ b/cipher/serpent.c
@@ -1557,10 +1557,10 @@ _gcry_serpent_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg,
 
 
 static unsigned int
-serpent_crypt_blk1_16(const void *context, byte *out, const byte *in,
-		      unsigned int num_blks, int encrypt)
+serpent_crypt_blk1_16(void *context, byte *out, const byte *in,
+		      size_t num_blks, int encrypt)
 {
-  const serpent_context_t *ctx = context;
+  serpent_context_t *ctx = context;
   unsigned int burn, burn_stack_depth = 0;
 
 #ifdef USE_AVX2
@@ -1612,15 +1612,15 @@ serpent_crypt_blk1_16(const void *context, byte *out, const byte *in,
 }
 
 static unsigned int
-serpent_encrypt_blk1_16(const void *ctx, byte *out, const byte *in,
-			unsigned int num_blks)
+serpent_encrypt_blk1_16(void *ctx, byte *out, const byte *in,
+			size_t num_blks)
 {
   return serpent_crypt_blk1_16 (ctx, out, in, num_blks, 1);
 }
 
 static unsigned int
-serpent_decrypt_blk1_16(const void *ctx, byte *out, const byte *in,
-			unsigned int num_blks)
+serpent_decrypt_blk1_16(void *ctx, byte *out, const byte *in,
+			size_t num_blks)
 {
   return serpent_crypt_blk1_16 (ctx, out, in, num_blks, 0);
 }
diff --git a/cipher/sm4.c b/cipher/sm4.c
index 20852cfb..0e89be78 100644
--- a/cipher/sm4.c
+++ b/cipher/sm4.c
@@ -141,9 +141,7 @@ static size_t _gcry_sm4_ocb_crypt (gcry_cipher_hd_t c, void *outbuf_arg,
 static size_t _gcry_sm4_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg,
 				  size_t nblocks);
 
-typedef unsigned int (*crypt_blk1_16_fn_t) (const void *ctx, byte *out,
-                                            const byte *in,
-                                            unsigned int num_blks);
+typedef bulk_crypt_fn_t crypt_blk1_16_fn_t;
 
 typedef struct
 {
@@ -274,12 +272,12 @@ extern void _gcry_sm4_aesni_avx_ocb_auth(const u32 *rk_enc,
 					 const u64 Ls[8]) ASM_FUNC_ABI;
 
 extern unsigned int
-_gcry_sm4_aesni_avx_crypt_blk1_8(const u32 *rk, byte *out, const byte *in,
+_gcry_sm4_aesni_avx_crypt_blk1_8(u32 *rk, byte *out, const byte *in,
 				 unsigned int num_blks) ASM_FUNC_ABI;
 
 static inline unsigned int
-sm4_aesni_avx_crypt_blk1_16(const void *rk, byte *out, const byte *in,
-                            unsigned int num_blks)
+sm4_aesni_avx_crypt_blk1_16(void *rk, byte *out, const byte *in,
+                            size_t num_blks)
 {
   if (num_blks > 8)
     {
@@ -328,12 +326,12 @@ extern void _gcry_sm4_aesni_avx2_ocb_auth(const u32 *rk_enc,
 					  const u64 Ls[16]) ASM_FUNC_ABI;
 
 extern unsigned int
-_gcry_sm4_aesni_avx2_crypt_blk1_16(const u32 *rk, byte *out, const byte *in,
+_gcry_sm4_aesni_avx2_crypt_blk1_16(u32 *rk, byte *out, const byte *in,
 				   unsigned int num_blks) ASM_FUNC_ABI;
 
 static inline unsigned int
-sm4_aesni_avx2_crypt_blk1_16(const void *rk, byte *out, const byte *in,
-                             unsigned int num_blks)
+sm4_aesni_avx2_crypt_blk1_16(void *rk, byte *out, const byte *in,
+			     size_t num_blks)
 {
 #ifdef USE_AESNI_AVX
   /* Use 128-bit register implementation for short input. */
@@ -384,12 +382,12 @@ extern void _gcry_sm4_gfni_avx2_ocb_auth(const u32 *rk_enc,
 					 const u64 Ls[16]) ASM_FUNC_ABI;
 
 extern unsigned int
-_gcry_sm4_gfni_avx2_crypt_blk1_16(const u32 *rk, byte *out, const byte *in,
+_gcry_sm4_gfni_avx2_crypt_blk1_16(u32 *rk, byte *out, const byte *in,
 				  unsigned int num_blks) ASM_FUNC_ABI;
 
 static inline unsigned int
-sm4_gfni_avx2_crypt_blk1_16(const void *rk, byte *out, const byte *in,
-			   unsigned int num_blks)
+sm4_gfni_avx2_crypt_blk1_16(void *rk, byte *out, const byte *in,
+			    size_t num_blks)
 {
   return _gcry_sm4_gfni_avx2_crypt_blk1_16(rk, out, in, num_blks);
 }
@@ -460,16 +458,16 @@ extern void _gcry_sm4_gfni_avx512_ocb_dec_blk32(const u32 *rk_dec,
                                                 const u64 Ls[32]) ASM_FUNC_ABI;
 
 extern unsigned int
-_gcry_sm4_gfni_avx512_crypt_blk1_16(const u32 *rk, byte *out, const byte *in,
+_gcry_sm4_gfni_avx512_crypt_blk1_16(u32 *rk, byte *out, const byte *in,
                                     unsigned int num_blks) ASM_FUNC_ABI;
 
 extern unsigned int
-_gcry_sm4_gfni_avx512_crypt_blk32(const u32 *rk, byte *out,
+_gcry_sm4_gfni_avx512_crypt_blk32(u32 *rk, byte *out,
                                   const byte *in) ASM_FUNC_ABI;
 
 static inline unsigned int
-sm4_gfni_avx512_crypt_blk1_16(const void *rk, byte *out, const byte *in,
-                              unsigned int num_blks)
+sm4_gfni_avx512_crypt_blk1_16(void *rk, byte *out, const byte *in,
+			      size_t num_blks)
 {
   return _gcry_sm4_gfni_avx512_crypt_blk1_16(rk, out, in, num_blks);
 }
@@ -496,13 +494,13 @@ extern void _gcry_sm4_aarch64_cfb_dec(const u32 *rk_enc, byte *out,
 				      byte *iv,
 				      size_t nblocks);
 
-extern void _gcry_sm4_aarch64_crypt_blk1_8(const u32 *rk, byte *out,
+extern void _gcry_sm4_aarch64_crypt_blk1_8(u32 *rk, byte *out,
 					   const byte *in,
 					   size_t num_blocks);
 
 static inline unsigned int
-sm4_aarch64_crypt_blk1_16(const void *rk, byte *out, const byte *in,
-                          unsigned int num_blks)
+sm4_aarch64_crypt_blk1_16(void *rk, byte *out, const byte *in,
+			  size_t num_blks)
 {
   if (num_blks > 8)
     {
@@ -547,13 +545,13 @@ extern void _gcry_sm4_armv8_ce_xts_crypt(const u32 *rk, byte *out,
 					 byte *tweak,
 					 size_t nblocks);
 
-extern void _gcry_sm4_armv8_ce_crypt_blk1_8(const u32 *rk, byte *out,
+extern void _gcry_sm4_armv8_ce_crypt_blk1_8(u32 *rk, byte *out,
 					    const byte *in,
 					    size_t num_blocks);
 
 static inline unsigned int
-sm4_armv8_ce_crypt_blk1_16(const void *rk, byte *out, const byte *in,
-                           unsigned int num_blks)
+sm4_armv8_ce_crypt_blk1_16(void *rk, byte *out, const byte *in,
+			   size_t num_blks)
 {
   if (num_blks > 8)
     {
@@ -570,7 +568,7 @@ sm4_armv8_ce_crypt_blk1_16(const void *rk, byte *out, const byte *in,
 #endif /* USE_ARM_CE */
 
 #ifdef USE_ARM_SVE_CE
-extern void _gcry_sm4_armv9_sve_ce_crypt(const u32 *rk, byte *out,
+extern void _gcry_sm4_armv9_sve_ce_crypt(u32 *rk, byte *out,
 					 const byte *in,
 					 size_t nblocks);
 
@@ -590,8 +588,8 @@ extern void _gcry_sm4_armv9_sve_ce_cfb_dec(const u32 *rk_enc, byte *out,
 					   size_t nblocks);
 
 static inline unsigned int
-sm4_armv9_sve_ce_crypt_blk1_16(const void *rk, byte *out, const byte *in,
-			       unsigned int num_blks)
+sm4_armv9_sve_ce_crypt_blk1_16(void *rk, byte *out, const byte *in,
+			       size_t num_blks)
 {
   _gcry_sm4_armv9_sve_ce_crypt(rk, out, in, num_blks);
   return 0;
@@ -934,8 +932,8 @@ sm4_do_crypt_blks2 (const u32 *rk, byte *out, const byte *in)
 }
 
 static unsigned int
-sm4_crypt_blocks (const void *ctx, byte *out, const byte *in,
-		  unsigned int num_blks)
+sm4_crypt_blocks (void *ctx, byte *out, const byte *in,
+		  size_t num_blks)
 {
   const u32 *rk = ctx;
   unsigned int burn_depth = 0;
@@ -1468,8 +1466,8 @@ _gcry_sm4_cfb_dec(void *context, unsigned char *iv,
 }
 
 static unsigned int
-sm4_crypt_blk1_32 (const SM4_context *ctx, byte *outbuf, const byte *inbuf,
-                   unsigned int num_blks, const u32 *rk)
+sm4_crypt_blk1_32 (SM4_context *ctx, byte *outbuf, const byte *inbuf,
+		   size_t num_blks, u32 *rk)
 {
   crypt_blk1_16_fn_t crypt_blk1_16 = ctx->crypt_blk1_16;
   unsigned int stack_burn_size = 0;
@@ -1506,18 +1504,18 @@ sm4_crypt_blk1_32 (const SM4_context *ctx, byte *outbuf, const byte *inbuf,
 }
 
 static unsigned int
-sm4_encrypt_blk1_32 (const void *context, byte *out, const byte *in,
-                     unsigned int num_blks)
+sm4_encrypt_blk1_32 (void *context, byte *out, const byte *in,
+		     size_t num_blks)
 {
-  const SM4_context *ctx = context;
+  SM4_context *ctx = context;
   return sm4_crypt_blk1_32 (ctx, out, in, num_blks, ctx->rkey_enc);
 }
 
 static unsigned int
-sm4_decrypt_blk1_32 (const void *context, byte *out, const byte *in,
-                     unsigned int num_blks)
+sm4_decrypt_blk1_32 (void *context, byte *out, const byte *in,
+		     size_t num_blks)
 {
-  const SM4_context *ctx = context;
+  SM4_context *ctx = context;
   return sm4_crypt_blk1_32 (ctx, out, in, num_blks, ctx->rkey_dec);
 }
 
diff --git a/cipher/twofish.c b/cipher/twofish.c
index 92c463fc..e5eae770 100644
--- a/cipher/twofish.c
+++ b/cipher/twofish.c
@@ -1541,10 +1541,10 @@ _gcry_twofish_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg,
 
 
 static unsigned int
-twofish_crypt_blk1_16(const void *context, byte *out, const byte *in,
-		      unsigned int num_blks, int encrypt)
+twofish_crypt_blk1_16(void *context, byte *out, const byte *in,
+		      size_t num_blks, int encrypt)
 {
-  const TWOFISH_context *ctx = context;
+  TWOFISH_context *ctx = context;
   unsigned int burn, burn_stack_depth = 0;
 
 #ifdef USE_AVX2
@@ -1584,15 +1584,15 @@ twofish_crypt_blk1_16(const void *context, byte *out, const byte *in,
 }
 
 static unsigned int
-twofish_encrypt_blk1_16(const void *ctx, byte *out, const byte *in,
-			unsigned int num_blks)
+twofish_encrypt_blk1_16(void *ctx, byte *out, const byte *in,
+			size_t num_blks)
 {
   return twofish_crypt_blk1_16 (ctx, out, in, num_blks, 1);
 }
 
 static unsigned int
-twofish_decrypt_blk1_16(const void *ctx, byte *out, const byte *in,
-			unsigned int num_blks)
+twofish_decrypt_blk1_16(void *ctx, byte *out, const byte *in,
+			size_t num_blks)
 {
   return twofish_crypt_blk1_16 (ctx, out, in, num_blks, 0);
 }
-- 
2.37.2




More information about the Gcrypt-devel mailing list