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:
#elif defined(GHASH_ASM_RISCV) && __riscv_xlen == 64
/* RISCV defaults; gmult already set above */
ctx->ghash = NULL;
#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;
}
ctx->ginit = gcm_init_clmul_rv64i_zbb_zbc;
ctx->gmult = gcm_gmult_clmul_rv64i_zbb_zbc;
}
# elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64
/* RISC-V 64 support */
# include "riscv_arch.h"
# 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);
int rv64i_zkne_set_encrypt_key(const unsigned char *userKey, const int bits,
AES_KEY *key);
# elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 32
/* RISC-V 32 support */
# include "riscv_arch.h"
# 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);
int rv32i_zkne_set_encrypt_key(const unsigned char *userKey, const int bits,
AES_KEY *key);
# include "riscv_arch.def"
;
# 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())
+
# include "cipher_aes_ccm_hw_aesni.inc"
#elif defined(SPARC_AES_CAPABLE)
# include "cipher_aes_ccm_hw_t4.inc"
# 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)
{
#else
const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits)
{
const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits)
{
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;
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;
}
return &rv32i_zknd_zkne_ccm;
return &aes_ccm;
}
const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits)
{
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;
# include "cipher_aes_gcm_hw_armv8.inc"
#elif defined(PPC_AES_GCM_CAPABLE)
# include "cipher_aes_gcm_hw_ppc.inc"
# 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)
{
#else
const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits)
{
const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits)
{
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;
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;
}
return &rv32i_zknd_zkne_gcm;
return &aes_gcm;
}
const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits)
{
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;
return &rv64i_zknd_zkne_gcm;
else
return &aes_gcm;
# include "cipher_aes_hw_t4.inc"
#elif defined(S390X_aes_128_CAPABLE)
# include "cipher_aes_hw_s390x.inc"
# 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)
#else
/* The generic case */
# define PROV_CIPHER_HW_declare(mode)
cipher_hw_aes_copyctx \
};
#define PROV_CIPHER_HW_select(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; \
return &rv32i_zbkb_zknd_zkne_##mode; \
-if (RV32I_ZKND_ZKNE_CAPABLE) \
+if (RISCV_HAS_ZKND_AND_ZKNE()) \
return &rv32i_zknd_zkne_##mode;
return &rv32i_zknd_zkne_##mode;
cipher_hw_aes_copyctx \
};
#define PROV_CIPHER_HW_select(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;
return &rv64i_zknd_zkne_##mode;
# define PROV_CIPHER_HW_select() \
if (SPARC_AES_CAPABLE) \
return &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,
static int cipher_hw_aes_ocb_rv64i_zknd_zkne_initkey(PROV_CIPHER_CTX *vctx,
const unsigned char *key,
NULL \
};
# define PROV_CIPHER_HW_select() \
NULL \
};
# define PROV_CIPHER_HW_select() \
- if (RV64I_ZKND_ZKNE_CAPABLE) \
+ if (RISCV_HAS_ZKND_AND_ZKNE()) \
return &aes_rv64i_zknd_zkne_ocb;
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,
static int cipher_hw_aes_ocb_rv32i_zknd_zkne_initkey(PROV_CIPHER_CTX *vctx,
const unsigned char *key,
NULL \
};
# define PROV_CIPHER_HW_select() \
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; \
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()
return &aes_rv32i_zknd_zkne_ocb;
#else
# define PROV_CIPHER_HW_declare()
# define PROV_CIPHER_HW_select_xts() \
if (SPARC_AES_CAPABLE) \
return &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,
static int cipher_hw_aes_xts_rv64i_zknd_zkne_initkey(PROV_CIPHER_CTX *ctx,
const unsigned char *key,
cipher_hw_aes_xts_copyctx \
};
# define PROV_CIPHER_HW_select_xts() \
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;
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,
static int cipher_hw_aes_xts_rv32i_zknd_zkne_initkey(PROV_CIPHER_CTX *ctx,
const unsigned char *key,
cipher_hw_aes_xts_copyctx \
};
# define PROV_CIPHER_HW_select_xts() \
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; \
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 */
return &aes_xts_rv32i_zknd_zkne;
# else
/* The generic case */