summaryrefslogtreecommitdiff
path: root/providers/implementations/ciphers/cipher_sm4_hw.c
diff options
context:
space:
mode:
authorDaniel Hu <Daniel.Hu@arm.com>2021-10-19 22:49:05 +0100
committerTomas Mraz <tomas@openssl.org>2022-01-18 11:52:14 +0100
commit15b7175f558bf9eb057ec3266685486f727dd70f (patch)
treeda3c4336ac7b9a7b8555d4809857342468baba59 /providers/implementations/ciphers/cipher_sm4_hw.c
parentc1167f09d840b109ef1c1c1485e3de64be2fc625 (diff)
downloadopenssl-new-15b7175f558bf9eb057ec3266685486f727dd70f.tar.gz
SM4 optimization for ARM by HW instruction
This patch implements the SM4 optimization for ARM processor, using SM4 HW instruction, which is an optional feature of crypto extension for aarch64 V8. Tested on some modern ARM micro-architectures with SM4 support, the performance uplift can be observed around 8X~40X over existing C implementation in openssl. Algorithms that can be parallelized (like CTR, ECB, CBC decryption) are on higher end, with algorithm like CBC encryption on lower end (due to inter-block dependency) Perf data on Yitian-710 2.75GHz hardware, before and after optimization: Before: type 16 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes 16384 bytes SM4-CTR 105787.80k 107837.87k 108380.84k 108462.08k 108549.46k 108554.92k SM4-ECB 111924.58k 118173.76k 119776.00k 120093.70k 120264.02k 120274.94k SM4-CBC 106428.09k 109190.98k 109674.33k 109774.51k 109827.41k 109827.41k After (7.4x - 36.6x faster): type 16 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes 16384 bytes SM4-CTR 781979.02k 2432994.28k 3437753.86k 3834177.88k 3963715.58k 3974556.33k SM4-ECB 937590.69k 2941689.02k 3945751.81k 4328655.87k 4459181.40k 4468692.31k SM4-CBC 890639.88k 1027746.58k 1050621.78k 1056696.66k 1058613.93k 1058701.31k Signed-off-by: Daniel Hu <Daniel.Hu@arm.com> Reviewed-by: Paul Dale <pauli@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from https://github.com/openssl/openssl/pull/17455)
Diffstat (limited to 'providers/implementations/ciphers/cipher_sm4_hw.c')
-rw-r--r--providers/implementations/ciphers/cipher_sm4_hw.c57
1 files changed, 51 insertions, 6 deletions
diff --git a/providers/implementations/ciphers/cipher_sm4_hw.c b/providers/implementations/ciphers/cipher_sm4_hw.c
index 0db04b1a74..4cd3d3d669 100644
--- a/providers/implementations/ciphers/cipher_sm4_hw.c
+++ b/providers/implementations/ciphers/cipher_sm4_hw.c
@@ -15,14 +15,59 @@ static int cipher_hw_sm4_initkey(PROV_CIPHER_CTX *ctx,
PROV_SM4_CTX *sctx = (PROV_SM4_CTX *)ctx;
SM4_KEY *ks = &sctx->ks.ks;
- ossl_sm4_set_key(key, ks);
ctx->ks = ks;
if (ctx->enc
|| (ctx->mode != EVP_CIPH_ECB_MODE
- && ctx->mode != EVP_CIPH_CBC_MODE))
- ctx->block = (block128_f)ossl_sm4_encrypt;
- else
- ctx->block = (block128_f)ossl_sm4_decrypt;
+ && ctx->mode != EVP_CIPH_CBC_MODE)) {
+#ifdef HWSM4_CAPABLE
+ if (HWSM4_CAPABLE) {
+ HWSM4_set_encrypt_key(key, ks);
+ ctx->block = (block128_f)HWSM4_encrypt;
+ ctx->stream.cbc = NULL;
+#ifdef HWSM4_cbc_encrypt
+ if (ctx->mode == EVP_CIPH_CBC_MODE)
+ ctx->stream.cbc = (cbc128_f)HWSM4_cbc_encrypt;
+ else
+#endif
+#ifdef HWSM4_ecb_encrypt
+ if (ctx->mode == EVP_CIPH_ECB_MODE)
+ ctx->stream.ecb = (ecb128_f)HWSM4_ecb_encrypt;
+ else
+#endif
+#ifdef HWSM4_ctr32_encrypt_blocks
+ if (ctx->mode == EVP_CIPH_CTR_MODE)
+ ctx->stream.ctr = (ctr128_f)HWSM4_ctr32_encrypt_blocks;
+ else
+#endif
+ (void)0; /* terminate potentially open 'else' */
+ } else
+#endif
+ {
+ ossl_sm4_set_key(key, ks);
+ ctx->block = (block128_f)ossl_sm4_encrypt;
+ }
+ } else {
+#ifdef HWSM4_CAPABLE
+ if (HWSM4_CAPABLE) {
+ HWSM4_set_decrypt_key(key, ks);
+ ctx->block = (block128_f)HWSM4_decrypt;
+ ctx->stream.cbc = NULL;
+#ifdef HWSM4_cbc_encrypt
+ if (ctx->mode == EVP_CIPH_CBC_MODE)
+ ctx->stream.cbc = (cbc128_f)HWSM4_cbc_encrypt;
+#endif
+#ifdef HWSM4_ecb_encrypt
+ if (ctx->mode == EVP_CIPH_ECB_MODE)
+ ctx->stream.ecb = (ecb128_f)HWSM4_ecb_encrypt;
+#endif
+ } else
+#endif
+ {
+ ossl_sm4_set_key(key, ks);
+ ctx->block = (block128_f)ossl_sm4_decrypt;
+ }
+ }
+
return 1;
}
@@ -31,7 +76,7 @@ IMPLEMENT_CIPHER_HW_COPYCTX(cipher_hw_sm4_copyctx, PROV_SM4_CTX)
# define PROV_CIPHER_HW_sm4_mode(mode) \
static const PROV_CIPHER_HW sm4_##mode = { \
cipher_hw_sm4_initkey, \
- ossl_cipher_hw_chunked_##mode, \
+ ossl_cipher_hw_generic_##mode, \
cipher_hw_sm4_copyctx \
}; \
const PROV_CIPHER_HW *ossl_prov_cipher_hw_sm4_##mode(size_t keybits) \