Sfoglia il codice sorgente

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 <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)
Christoph Müllner 1 anno fa
parent
commit
86c69fe841

+ 1 - 1
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;
     }

+ 0 - 3
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);

+ 5 - 0
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

+ 4 - 4
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)
 {

+ 2 - 2
providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i_zknd_zkne.inc → 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;
 }

+ 1 - 1
providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i_zknd_zkne.inc → 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;
 }

+ 4 - 4
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)
 {

+ 2 - 2
providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i_zknd_zkne.inc → 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;
 }

+ 1 - 1
providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i_zknd_zkne.inc → 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;

+ 4 - 4
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)

+ 2 - 2
providers/implementations/ciphers/cipher_aes_hw_rv32i_zknd_zkne.inc → 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;

+ 1 - 1
providers/implementations/ciphers/cipher_aes_hw_rv64i_zknd_zkne.inc → 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;

+ 7 - 5
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()

+ 7 - 5
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 */