From: Christoph Müllner Date: Tue, 17 Jan 2023 18:31:58 +0000 (+0100) Subject: riscv: Clean up extension test macros X-Git-Tag: openssl-3.2.0-alpha1~1158 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=86c69fe84118f0dca656d9bfc1131052e2a8e9b8;p=thirdparty%2Fopenssl.git riscv: Clean up extension test macros In RISC-V we have multiple extensions, that can be used to accelerate processing. The known extensions are defined in riscv_arch.def. From that file test functions of the following form are generated: RISCV_HAS_$ext(). In recent commits new ways to define the availability of these test macros have been defined. E.g.: #define RV32I_ZKND_ZKNE_CAPABLE \ (RISCV_HAS_ZKND() && RISCV_HAS_ZKNE()) [...] #define RV64I_ZKND_ZKNE_CAPABLE \ (RISCV_HAS_ZKND() && RISCV_HAS_ZKNE()) This leaves us with two different APIs to test capabilities. Further, creating the same macros for RV32 and RV64 results in duplicated code (see example above). This inconsistent situation makes it hard to integrate further code. So let's clean this up with the following steps: * Replace RV32I_* and RV64I_* macros by RICSV_HAS_* macros * Move all test macros into riscv_arch.h * Use "AND" and "OR" to combine tests with more than one extension * Rename include files for accelerated processing (remove extension postfix). We end up with compile time tests for RV32/RV64 and run-time tests for available extensions. Adding new routines (e.g. for vector crypto instructions) should be straightforward. Testing showed no regressions. Signed-off-by: Christoph Müllner Reviewed-by: Tomas Mraz Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/20078) --- diff --git a/crypto/modes/gcm128.c b/crypto/modes/gcm128.c index 0b31e671f0a..a49798b7f40 100644 --- a/crypto/modes/gcm128.c +++ b/crypto/modes/gcm128.c @@ -502,7 +502,7 @@ static void gcm_get_funcs(struct gcm_funcs_st *ctx) #elif defined(GHASH_ASM_RISCV) && __riscv_xlen == 64 /* RISCV defaults; gmult already set above */ ctx->ghash = NULL; - if (RISCV_HAS_ZBB() && RISCV_HAS_ZBC()) { + if (RISCV_HAS_ZBB_AND_ZBC()) { ctx->ginit = gcm_init_clmul_rv64i_zbb_zbc; ctx->gmult = gcm_gmult_clmul_rv64i_zbb_zbc; } diff --git a/include/crypto/aes_platform.h b/include/crypto/aes_platform.h index 5572e52a43a..402727f9146 100644 --- a/include/crypto/aes_platform.h +++ b/include/crypto/aes_platform.h @@ -434,7 +434,6 @@ void aes256_t4_xts_decrypt(const unsigned char *in, unsigned char *out, # elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64 /* RISC-V 64 support */ # include "riscv_arch.h" -# define RV64I_ZKND_ZKNE_CAPABLE (RISCV_HAS_ZKND() && RISCV_HAS_ZKNE()) int rv64i_zkne_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); @@ -447,8 +446,6 @@ void rv64i_zknd_decrypt(const unsigned char *in, unsigned char *out, # elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 32 /* RISC-V 32 support */ # include "riscv_arch.h" -# define RV32I_ZKND_ZKNE_CAPABLE (RISCV_HAS_ZKND() && RISCV_HAS_ZKNE()) -# define RV32I_ZBKB_ZKND_ZKNE_CAPABLE (RV32I_ZKND_ZKNE_CAPABLE && RISCV_HAS_ZBKB()) int rv32i_zkne_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key); diff --git a/include/crypto/riscv_arch.h b/include/crypto/riscv_arch.h index 89a40bea849..0e0f946ddcc 100644 --- a/include/crypto/riscv_arch.h +++ b/include/crypto/riscv_arch.h @@ -56,4 +56,9 @@ static const size_t kRISCVNumCaps = # include "riscv_arch.def" ; +/* Extension combination tests. */ +#define RISCV_HAS_ZBB_AND_ZBC() (RISCV_HAS_ZBB() && RISCV_HAS_ZBC()) +#define RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE() (RISCV_HAS_ZBKB() && RISCV_HAS_ZKND() && RISCV_HAS_ZKNE()) +#define RISCV_HAS_ZKND_AND_ZKNE() (RISCV_HAS_ZKND() && RISCV_HAS_ZKNE()) + #endif diff --git a/providers/implementations/ciphers/cipher_aes_ccm_hw.c b/providers/implementations/ciphers/cipher_aes_ccm_hw.c index 5dbb74bdcc2..a7e9fb4a213 100644 --- a/providers/implementations/ciphers/cipher_aes_ccm_hw.c +++ b/providers/implementations/ciphers/cipher_aes_ccm_hw.c @@ -61,10 +61,10 @@ static const PROV_CCM_HW aes_ccm = { # include "cipher_aes_ccm_hw_aesni.inc" #elif defined(SPARC_AES_CAPABLE) # include "cipher_aes_ccm_hw_t4.inc" -#elif defined(RV64I_ZKND_ZKNE_CAPABLE) -# include "cipher_aes_ccm_hw_rv64i_zknd_zkne.inc" -#elif defined(RV32I_ZBKB_ZKND_ZKNE_CAPABLE) && defined(RV32I_ZKND_ZKNE_CAPABLE) -# include "cipher_aes_ccm_hw_rv32i_zknd_zkne.inc" +#elif defined(__riscv) && __riscv_xlen == 64 +# include "cipher_aes_ccm_hw_rv64i.inc" +#elif defined(__riscv) && __riscv_xlen == 32 +# include "cipher_aes_ccm_hw_rv32i.inc" #else const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits) { diff --git a/providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i_zknd_zkne.inc b/providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i.inc similarity index 95% rename from providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i_zknd_zkne.inc rename to providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i.inc index 345bc2faae0..a09a1e8dd8c 100644 --- a/providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i_zknd_zkne.inc +++ b/providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i.inc @@ -52,9 +52,9 @@ static const PROV_CCM_HW rv32i_zbkb_zknd_zkne_ccm = { const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits) { - if (RV32I_ZBKB_ZKND_ZKNE_CAPABLE) + if (RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE()) return &rv32i_zbkb_zknd_zkne_ccm; - if (RV32I_ZKND_ZKNE_CAPABLE) + if (RISCV_HAS_ZKND_AND_ZKNE()) return &rv32i_zknd_zkne_ccm; return &aes_ccm; } diff --git a/providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i_zknd_zkne.inc b/providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i.inc similarity index 93% rename from providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i_zknd_zkne.inc rename to providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i.inc index 2f23209d030..f37c36118ca 100644 --- a/providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i_zknd_zkne.inc +++ b/providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i.inc @@ -33,5 +33,5 @@ static const PROV_CCM_HW rv64i_zknd_zkne_ccm = { const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits) { - return RV64I_ZKND_ZKNE_CAPABLE ? &rv64i_zknd_zkne_ccm : &aes_ccm; + return RISCV_HAS_ZKND_AND_ZKNE() ? &rv64i_zknd_zkne_ccm : &aes_ccm; } diff --git a/providers/implementations/ciphers/cipher_aes_gcm_hw.c b/providers/implementations/ciphers/cipher_aes_gcm_hw.c index c7a98cdfbfb..3887b4916ec 100644 --- a/providers/implementations/ciphers/cipher_aes_gcm_hw.c +++ b/providers/implementations/ciphers/cipher_aes_gcm_hw.c @@ -142,10 +142,10 @@ static const PROV_GCM_HW aes_gcm = { # include "cipher_aes_gcm_hw_armv8.inc" #elif defined(PPC_AES_GCM_CAPABLE) # include "cipher_aes_gcm_hw_ppc.inc" -#elif defined(RV64I_ZKND_ZKNE_CAPABLE) -# include "cipher_aes_gcm_hw_rv64i_zknd_zkne.inc" -#elif defined(RV32I_ZBKB_ZKND_ZKNE_CAPABLE) && defined(RV32I_ZKND_ZKNE_CAPABLE) -# include "cipher_aes_gcm_hw_rv32i_zknd_zkne.inc" +#elif defined(__riscv) && __riscv_xlen == 64 +# include "cipher_aes_gcm_hw_rv64i.inc" +#elif defined(__riscv) && __riscv_xlen == 32 +# include "cipher_aes_gcm_hw_rv32i.inc" #else const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits) { diff --git a/providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i_zknd_zkne.inc b/providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i.inc similarity index 95% rename from providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i_zknd_zkne.inc rename to providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i.inc index dd5878736ea..32abd05210d 100644 --- a/providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i_zknd_zkne.inc +++ b/providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i.inc @@ -55,9 +55,9 @@ static const PROV_GCM_HW rv32i_zbkb_zknd_zkne_gcm = { const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits) { - if (RV32I_ZBKB_ZKND_ZKNE_CAPABLE) + if (RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE()) return &rv32i_zbkb_zknd_zkne_gcm; - if (RV32I_ZKND_ZKNE_CAPABLE) + if (RISCV_HAS_ZKND_AND_ZKNE()) return &rv32i_zknd_zkne_gcm; return &aes_gcm; } diff --git a/providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i_zknd_zkne.inc b/providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i.inc similarity index 97% rename from providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i_zknd_zkne.inc rename to providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i.inc index 44325d8469e..a89ab178118 100644 --- a/providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i_zknd_zkne.inc +++ b/providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i.inc @@ -33,7 +33,7 @@ static const PROV_GCM_HW rv64i_zknd_zkne_gcm = { const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits) { - if (RV64I_ZKND_ZKNE_CAPABLE) + if (RISCV_HAS_ZKND_AND_ZKNE()) return &rv64i_zknd_zkne_gcm; else return &aes_gcm; diff --git a/providers/implementations/ciphers/cipher_aes_hw.c b/providers/implementations/ciphers/cipher_aes_hw.c index 074d04d8785..1a59f24d352 100644 --- a/providers/implementations/ciphers/cipher_aes_hw.c +++ b/providers/implementations/ciphers/cipher_aes_hw.c @@ -142,10 +142,10 @@ const PROV_CIPHER_HW *ossl_prov_cipher_hw_aes_##mode(size_t keybits) \ # include "cipher_aes_hw_t4.inc" #elif defined(S390X_aes_128_CAPABLE) # include "cipher_aes_hw_s390x.inc" -#elif defined(RV64I_ZKND_ZKNE_CAPABLE) -# include "cipher_aes_hw_rv64i_zknd_zkne.inc" -#elif defined(RV32I_ZBKB_ZKND_ZKNE_CAPABLE) && defined(RV32I_ZKND_ZKNE_CAPABLE) -# include "cipher_aes_hw_rv32i_zknd_zkne.inc" +#elif defined(__riscv) && __riscv_xlen == 64 +# include "cipher_aes_hw_rv64i.inc" +#elif defined(__riscv) && __riscv_xlen == 32 +# include "cipher_aes_hw_rv32i.inc" #else /* The generic case */ # define PROV_CIPHER_HW_declare(mode) diff --git a/providers/implementations/ciphers/cipher_aes_hw_rv32i_zknd_zkne.inc b/providers/implementations/ciphers/cipher_aes_hw_rv32i.inc similarity index 97% rename from providers/implementations/ciphers/cipher_aes_hw_rv32i_zknd_zkne.inc rename to providers/implementations/ciphers/cipher_aes_hw_rv32i.inc index d3173fa401c..a23c08ac9e1 100644 --- a/providers/implementations/ciphers/cipher_aes_hw_rv32i_zknd_zkne.inc +++ b/providers/implementations/ciphers/cipher_aes_hw_rv32i.inc @@ -96,7 +96,7 @@ static const PROV_CIPHER_HW rv32i_zbkb_zknd_zkne_##mode = { \ cipher_hw_aes_copyctx \ }; #define PROV_CIPHER_HW_select(mode) \ -if (RV32I_ZBKB_ZKND_ZKNE_CAPABLE) \ +if (RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE()) \ return &rv32i_zbkb_zknd_zkne_##mode; \ -if (RV32I_ZKND_ZKNE_CAPABLE) \ +if (RISCV_HAS_ZKND_AND_ZKNE()) \ return &rv32i_zknd_zkne_##mode; diff --git a/providers/implementations/ciphers/cipher_aes_hw_rv64i_zknd_zkne.inc b/providers/implementations/ciphers/cipher_aes_hw_rv64i.inc similarity index 97% rename from providers/implementations/ciphers/cipher_aes_hw_rv64i_zknd_zkne.inc rename to providers/implementations/ciphers/cipher_aes_hw_rv64i.inc index 762d211ef85..3cf3c8e3a41 100644 --- a/providers/implementations/ciphers/cipher_aes_hw_rv64i_zknd_zkne.inc +++ b/providers/implementations/ciphers/cipher_aes_hw_rv64i.inc @@ -55,5 +55,5 @@ static const PROV_CIPHER_HW rv64i_zknd_zkne_##mode = { \ cipher_hw_aes_copyctx \ }; #define PROV_CIPHER_HW_select(mode) \ -if (RV64I_ZKND_ZKNE_CAPABLE) \ +if (RISCV_HAS_ZKND_AND_ZKNE()) \ return &rv64i_zknd_zkne_##mode; diff --git a/providers/implementations/ciphers/cipher_aes_ocb_hw.c b/providers/implementations/ciphers/cipher_aes_ocb_hw.c index 5b93d2b717f..62d762d49b8 100644 --- a/providers/implementations/ciphers/cipher_aes_ocb_hw.c +++ b/providers/implementations/ciphers/cipher_aes_ocb_hw.c @@ -103,7 +103,8 @@ static const PROV_CIPHER_HW aes_t4_ocb = { \ # define PROV_CIPHER_HW_select() \ if (SPARC_AES_CAPABLE) \ return &aes_t4_ocb; -#elif defined(RV64I_ZKND_ZKNE_CAPABLE) + +#elif defined(__riscv) && __riscv_xlen == 64 static int cipher_hw_aes_ocb_rv64i_zknd_zkne_initkey(PROV_CIPHER_CTX *vctx, const unsigned char *key, @@ -122,9 +123,10 @@ static const PROV_CIPHER_HW aes_rv64i_zknd_zkne_ocb = { \ NULL \ }; # define PROV_CIPHER_HW_select() \ - if (RV64I_ZKND_ZKNE_CAPABLE) \ + if (RISCV_HAS_ZKND_AND_ZKNE()) \ return &aes_rv64i_zknd_zkne_ocb; -#elif defined(RV32I_ZBKB_ZKND_ZKNE_CAPABLE) && defined(RV32I_ZKND_ZKNE_CAPABLE) + +#elif defined(__riscv) && __riscv_xlen == 32 static int cipher_hw_aes_ocb_rv32i_zknd_zkne_initkey(PROV_CIPHER_CTX *vctx, const unsigned char *key, @@ -158,9 +160,9 @@ static const PROV_CIPHER_HW aes_rv32i_zbkb_zknd_zkne_ocb = { \ NULL \ }; # define PROV_CIPHER_HW_select() \ - if (RV32I_ZBKB_ZKND_ZKNE_CAPABLE) \ + if (RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE()) \ return &aes_rv32i_zbkb_zknd_zkne_ocb; \ - if (RV32I_ZKND_ZKNE_CAPABLE) \ + if (RISCV_HAS_ZKND_AND_ZKNE()) \ return &aes_rv32i_zknd_zkne_ocb; #else # define PROV_CIPHER_HW_declare() diff --git a/providers/implementations/ciphers/cipher_aes_xts_hw.c b/providers/implementations/ciphers/cipher_aes_xts_hw.c index c8c9cbf19e7..223b49b0b98 100644 --- a/providers/implementations/ciphers/cipher_aes_xts_hw.c +++ b/providers/implementations/ciphers/cipher_aes_xts_hw.c @@ -158,7 +158,8 @@ static const PROV_CIPHER_HW aes_xts_t4 = { \ # define PROV_CIPHER_HW_select_xts() \ if (SPARC_AES_CAPABLE) \ return &aes_xts_t4; -#elif defined(RV64I_ZKND_ZKNE_CAPABLE) + +#elif defined(__riscv) && __riscv_xlen == 64 static int cipher_hw_aes_xts_rv64i_zknd_zkne_initkey(PROV_CIPHER_CTX *ctx, const unsigned char *key, @@ -181,9 +182,10 @@ static const PROV_CIPHER_HW aes_xts_rv64i_zknd_zkne = { \ cipher_hw_aes_xts_copyctx \ }; # define PROV_CIPHER_HW_select_xts() \ -if (RV64I_ZKND_ZKNE_CAPABLE) \ +if (RISCV_HAS_ZKND_AND_ZKNE()) \ return &aes_xts_rv64i_zknd_zkne; -#elif defined(RV32I_ZBKB_ZKND_ZKNE_CAPABLE) && defined(RV32I_ZKND_ZKNE_CAPABLE) + +#elif defined(__riscv) && __riscv_xlen == 32 static int cipher_hw_aes_xts_rv32i_zknd_zkne_initkey(PROV_CIPHER_CTX *ctx, const unsigned char *key, @@ -221,9 +223,9 @@ static const PROV_CIPHER_HW aes_xts_rv32i_zbkb_zknd_zkne = { \ cipher_hw_aes_xts_copyctx \ }; # define PROV_CIPHER_HW_select_xts() \ -if (RV32I_ZBKB_ZKND_ZKNE_CAPABLE) \ +if (RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE()) \ return &aes_xts_rv32i_zbkb_zknd_zkne; \ -if (RV32I_ZKND_ZKNE_CAPABLE) \ +if (RISCV_HAS_ZKND_ZKNE()) \ return &aes_xts_rv32i_zknd_zkne; # else /* The generic case */