[PATCH 5/8] sm4: accelerate ECB (for benchmarking)

Jussi Kivilinna jussi.kivilinna at iki.fi
Sun Oct 23 18:16:05 CEST 2022


* cipher/sm4.c (_gcry_sm4_ecb_crypt): New.
(sm4_setkey): Setup ECB bulk function.
--

Benchmark on AMD Ryzen 9 7900X:

Before:
 SM4            |  nanosecs/byte   mebibytes/sec   cycles/byte  auto Mhz
        ECB enc |      4.75 ns/B     200.6 MiB/s     26.74 c/B      5625
        ECB dec |      4.79 ns/B     199.3 MiB/s     26.92 c/B      5625

After (OCB for reference):
 SM4            |  nanosecs/byte   mebibytes/sec   cycles/byte  auto Mhz
        ECB enc |     0.252 ns/B      3782 MiB/s      1.42 c/B      5624
        ECB dec |     0.253 ns/B      3770 MiB/s      1.42 c/B      5625
        OCB enc |     0.277 ns/B      3446 MiB/s      1.56 c/B      5625
        OCB dec |     0.281 ns/B      3399 MiB/s      1.54 c/B      5500

GnuPG-bug-id: T6242
Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 cipher/sm4.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/cipher/sm4.c b/cipher/sm4.c
index 32a21dd9..20852cfb 100644
--- a/cipher/sm4.c
+++ b/cipher/sm4.c
@@ -129,6 +129,9 @@ static void _gcry_sm4_cfb_dec (void *context, unsigned char *iv,
 static void _gcry_sm4_xts_crypt (void *context, unsigned char *tweak,
                                  void *outbuf_arg, const void *inbuf_arg,
                                  size_t nblocks, int encrypt);
+static void _gcry_sm4_ecb_crypt (void *context, void *outbuf_arg,
+				 const void *inbuf_arg, size_t nblocks,
+				 int encrypt);
 static void _gcry_sm4_ctr32le_enc(void *context, unsigned char *ctr,
                                   void *outbuf_arg, const void *inbuf_arg,
                                   size_t nblocks);
@@ -796,6 +799,7 @@ sm4_setkey (void *context, const byte *key, const unsigned keylen,
   bulk_ops->cfb_dec = _gcry_sm4_cfb_dec;
   bulk_ops->ctr_enc = _gcry_sm4_ctr_enc;
   bulk_ops->xts_crypt = _gcry_sm4_xts_crypt;
+  bulk_ops->ecb_crypt = _gcry_sm4_ecb_crypt;
   bulk_ops->ctr32le_enc = _gcry_sm4_ctr32le_enc;
   bulk_ops->ocb_crypt = _gcry_sm4_ocb_crypt;
   bulk_ops->ocb_auth  = _gcry_sm4_ocb_auth;
@@ -1517,6 +1521,34 @@ sm4_decrypt_blk1_32 (const void *context, byte *out, const byte *in,
   return sm4_crypt_blk1_32 (ctx, out, in, num_blks, ctx->rkey_dec);
 }
 
+/* Bulk encryption/decryption in ECB mode. */
+static void
+_gcry_sm4_ecb_crypt (void *context, void *outbuf_arg,
+		     const void *inbuf_arg, size_t nblocks, int encrypt)
+{
+  SM4_context *ctx = context;
+  unsigned char *outbuf = outbuf_arg;
+  const unsigned char *inbuf = inbuf_arg;
+  int burn_stack_depth = 0;
+
+  /* Process remaining blocks. */
+  if (nblocks)
+    {
+      size_t nburn;
+
+      if (ctx->crypt_blk1_16 == &sm4_crypt_blocks)
+	prefetch_sbox_table ();
+
+      nburn = bulk_ecb_crypt_128(ctx, encrypt ? sm4_encrypt_blk1_32
+                                              : sm4_decrypt_blk1_32,
+                                 outbuf, inbuf, nblocks, 32);
+      burn_stack_depth = nburn > burn_stack_depth ? nburn : burn_stack_depth;
+    }
+
+  if (burn_stack_depth)
+    _gcry_burn_stack(burn_stack_depth);
+}
+
 /* Bulk encryption/decryption of complete blocks in XTS mode. */
 static void
 _gcry_sm4_xts_crypt (void *context, unsigned char *tweak, void *outbuf_arg,
-- 
2.37.2




More information about the Gcrypt-devel mailing list