riscv: Clean up extension test macros
authorChristoph Müllner <christoph.muellner@vrull.eu>
Tue, 17 Jan 2023 18:31:58 +0000 (19:31 +0100)
committerPauli <pauli@openssl.org>
Thu, 16 Mar 2023 02:12:19 +0000 (13:12 +1100)
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 <christoph.muellner@vrull.eu>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/20078)

14 files changed:
crypto/modes/gcm128.c
include/crypto/aes_platform.h
include/crypto/riscv_arch.h
providers/implementations/ciphers/cipher_aes_ccm_hw.c
providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i.inc [moved from providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i_zknd_zkne.inc with 95% similarity]
providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i.inc [moved from providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i_zknd_zkne.inc with 93% similarity]
providers/implementations/ciphers/cipher_aes_gcm_hw.c
providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i.inc [moved from providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i_zknd_zkne.inc with 95% similarity]
providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i.inc [moved from providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i_zknd_zkne.inc with 97% similarity]
providers/implementations/ciphers/cipher_aes_hw.c
providers/implementations/ciphers/cipher_aes_hw_rv32i.inc [moved from providers/implementations/ciphers/cipher_aes_hw_rv32i_zknd_zkne.inc with 97% similarity]
providers/implementations/ciphers/cipher_aes_hw_rv64i.inc [moved from providers/implementations/ciphers/cipher_aes_hw_rv64i_zknd_zkne.inc with 97% similarity]
providers/implementations/ciphers/cipher_aes_ocb_hw.c
providers/implementations/ciphers/cipher_aes_xts_hw.c

index 0b31e671f0ab7404bc9829033ebf67abdac3b1c6..a49798b7f4050788802eb909b4b1b965e571c9a3 100644 (file)
@@ -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;
     }
index 5572e52a43ad5e2ea9994981e2176805e393d97e..402727f9146f7cab34e09accea7387bd399d7d31 100644 (file)
@@ -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);
index 89a40bea8490467b094d862442b077119cb62bc0..0e0f946ddcc4ff7f05c410c7d170a5e1dbdf75ea 100644 (file)
@@ -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
index 5dbb74bdcc2d3f2f5bf9ccb64086055228c9b9af..a7e9fb4a21396aab872208513d01fdd5b63e1f86 100644 (file)
@@ -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)
 {
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 345bc2faae0daf43f9f12dcfb118af15403ec56f..a09a1e8dd8c8c73106c4f7d27e3f312bdb75a6fc 100644 (file)
@@ -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;
 }
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 2f23209d0308f9cce713cfd9177500f78b4a1141..f37c36118caa370ffc8b99f1c22c3805914172c6 100644 (file)
@@ -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;
 }
index c7a98cdfbfbddc112927885ef171df4db58bc31c..3887b4916ec4f4c9bfd91a46cab73eb32f183ad2 100644 (file)
@@ -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)
 {
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 dd5878736ea11765219991567dbbe831bb53ff3c..32abd05210dae70a95cbdd279b30b4763b1c8c98 100644 (file)
@@ -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;
 }
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 44325d8469ee806c438d0e4204d2e43695b6b5d0..a89ab1781185a3bd4f5be063eaee93d8ba4f1a65 100644 (file)
@@ -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;
index 074d04d87854b66730a04c548fd64b707ba3f121..1a59f24d352d4850bf83b51e15c5391e5d927714 100644 (file)
@@ -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)
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 d3173fa401c786f4651ef458cb8398e3dcae1599..a23c08ac9e172504820e951637b0731bb539571e 100644 (file)
@@ -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;
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 762d211ef853f843aa3ca60eb2101b68d145f823..3cf3c8e3a41bee8cf226a50b71a2eb842e55bb1b 100644 (file)
@@ -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;
index 5b93d2b717f02d8a72864fb1d3419ad392ce0b02..62d762d49b8f133c5b4c290b953ea05a7b327b90 100644 (file)
@@ -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()
index c8c9cbf19e7ecbf90549a2fe5d24927259c2a13c..223b49b0b98f16f58b6f8d2a318aba80492c2c00 100644 (file)
@@ -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 */