mirror of
https://github.com/openssl/openssl.git
synced 2025-03-07 19:38:33 +08:00
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)
58 lines
1.5 KiB
C
58 lines
1.5 KiB
C
/*
|
|
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
|
|
*
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
* in the file LICENSE in the source distribution or at
|
|
* https://www.openssl.org/source/license.html
|
|
*/
|
|
|
|
/*-
|
|
* Generic support for SM4 GCM.
|
|
*/
|
|
|
|
#include "cipher_sm4_gcm.h"
|
|
#include "crypto/sm4_platform.h"
|
|
|
|
static int sm4_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
|
|
size_t keylen)
|
|
{
|
|
PROV_SM4_GCM_CTX *actx = (PROV_SM4_GCM_CTX *)ctx;
|
|
SM4_KEY *ks = &actx->ks.ks;
|
|
|
|
ctx->ks = ks;
|
|
# ifdef HWSM4_CAPABLE
|
|
if (HWSM4_CAPABLE) {
|
|
HWSM4_set_encrypt_key(key, ks);
|
|
CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f) HWSM4_encrypt);
|
|
# ifdef HWSM4_ctr32_encrypt_blocks
|
|
ctx->ctr = (ctr128_f) HWSM4_ctr32_encrypt_blocks;
|
|
# else /* HWSM4_ctr32_encrypt_blocks */
|
|
ctx->ctr = (ctr128_f)NULL;
|
|
# endif
|
|
} else
|
|
# endif /* HWSM4_CAPABLE */
|
|
{
|
|
ossl_sm4_set_key(key, ks);
|
|
CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f)ossl_sm4_encrypt);
|
|
ctx->ctr = (ctr128_f)NULL;
|
|
}
|
|
ctx->key_set = 1;
|
|
|
|
return 1;
|
|
}
|
|
|
|
static const PROV_GCM_HW sm4_gcm = {
|
|
sm4_gcm_initkey,
|
|
ossl_gcm_setiv,
|
|
ossl_gcm_aad_update,
|
|
ossl_gcm_cipher_update,
|
|
ossl_gcm_cipher_final,
|
|
ossl_gcm_one_shot
|
|
};
|
|
|
|
const PROV_GCM_HW *ossl_prov_sm4_hw_gcm(size_t keybits)
|
|
{
|
|
return &sm4_gcm;
|
|
}
|