summaryrefslogtreecommitdiff
path: root/providers
diff options
context:
space:
mode:
authorHongren (Zenithal) Zheng <i@zenithal.me>2022-05-14 00:01:11 +0800
committerPauli <pauli@openssl.org>2022-09-05 10:20:30 +1000
commit5ccee69b1384fa9377986a6f7730e0d9a372b42b (patch)
treeb354928fcafbffc0799b4633aeb1892f181f9aa5 /providers
parentcbb15b31b98f47276cf9e87453831d96274baf66 (diff)
downloadopenssl-new-5ccee69b1384fa9377986a6f7730e0d9a372b42b.tar.gz
provider: cipher: aes: add riscv32 zkn (zbkb) support
Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Paul Dale <pauli@openssl.org> (Merged from https://github.com/openssl/openssl/pull/18308)
Diffstat (limited to 'providers')
-rw-r--r--providers/implementations/ciphers/cipher_aes_ccm_hw.c2
-rw-r--r--providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i_zknd_zkne.inc60
-rw-r--r--providers/implementations/ciphers/cipher_aes_gcm_hw.c2
-rw-r--r--providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i_zknd_zkne.inc63
-rw-r--r--providers/implementations/ciphers/cipher_aes_hw.c2
-rw-r--r--providers/implementations/ciphers/cipher_aes_hw_rv32i_zknd_zkne.inc102
-rw-r--r--providers/implementations/ciphers/cipher_aes_ocb_hw.c38
-rw-r--r--providers/implementations/ciphers/cipher_aes_xts_hw.c42
8 files changed, 311 insertions, 0 deletions
diff --git a/providers/implementations/ciphers/cipher_aes_ccm_hw.c b/providers/implementations/ciphers/cipher_aes_ccm_hw.c
index 80e1cbc782..5dbb74bdcc 100644
--- a/providers/implementations/ciphers/cipher_aes_ccm_hw.c
+++ b/providers/implementations/ciphers/cipher_aes_ccm_hw.c
@@ -63,6 +63,8 @@ static const PROV_CCM_HW aes_ccm = {
# 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"
#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_zknd_zkne.inc
new file mode 100644
index 0000000000..345bc2faae
--- /dev/null
+++ b/providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i_zknd_zkne.inc
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2022 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
+ */
+
+/*-
+ * RISC-V 32 ZKND ZKNE support for AES CCM.
+ * This file is included by cipher_aes_ccm_hw.c
+ */
+
+static int ccm_rv32i_zknd_zkne_initkey(PROV_CCM_CTX *ctx, const unsigned char *key,
+ size_t keylen)
+{
+ PROV_AES_CCM_CTX *actx = (PROV_AES_CCM_CTX *)ctx;
+
+ AES_HW_CCM_SET_KEY_FN(rv32i_zkne_set_encrypt_key, rv32i_zkne_encrypt,
+ NULL, NULL);
+ return 1;
+}
+
+static int ccm_rv32i_zbkb_zknd_zkne_initkey(PROV_CCM_CTX *ctx, const unsigned char *key,
+ size_t keylen)
+{
+ PROV_AES_CCM_CTX *actx = (PROV_AES_CCM_CTX *)ctx;
+
+ AES_HW_CCM_SET_KEY_FN(rv32i_zbkb_zkne_set_encrypt_key, rv32i_zkne_encrypt,
+ NULL, NULL);
+ return 1;
+}
+
+static const PROV_CCM_HW rv32i_zknd_zkne_ccm = {
+ ccm_rv32i_zknd_zkne_initkey,
+ ossl_ccm_generic_setiv,
+ ossl_ccm_generic_setaad,
+ ossl_ccm_generic_auth_encrypt,
+ ossl_ccm_generic_auth_decrypt,
+ ossl_ccm_generic_gettag
+};
+
+static const PROV_CCM_HW rv32i_zbkb_zknd_zkne_ccm = {
+ ccm_rv32i_zbkb_zknd_zkne_initkey,
+ ossl_ccm_generic_setiv,
+ ossl_ccm_generic_setaad,
+ ossl_ccm_generic_auth_encrypt,
+ ossl_ccm_generic_auth_decrypt,
+ ossl_ccm_generic_gettag
+};
+
+const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits)
+{
+ if (RV32I_ZBKB_ZKND_ZKNE_CAPABLE)
+ return &rv32i_zbkb_zknd_zkne_ccm;
+ if (RV32I_ZKND_ZKNE_CAPABLE)
+ return &rv32i_zknd_zkne_ccm;
+ return &aes_ccm;
+}
diff --git a/providers/implementations/ciphers/cipher_aes_gcm_hw.c b/providers/implementations/ciphers/cipher_aes_gcm_hw.c
index 75bccca492..145b207c47 100644
--- a/providers/implementations/ciphers/cipher_aes_gcm_hw.c
+++ b/providers/implementations/ciphers/cipher_aes_gcm_hw.c
@@ -145,6 +145,8 @@ static const PROV_GCM_HW aes_gcm = {
# 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"
#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_zknd_zkne.inc
new file mode 100644
index 0000000000..dd5878736e
--- /dev/null
+++ b/providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i_zknd_zkne.inc
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2022 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
+ */
+
+/*-
+ * RISC-V 32 ZKND ZKNE support for AES GCM.
+ * This file is included by cipher_aes_gcm_hw.c
+ */
+
+static int rv32i_zknd_zkne_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
+ size_t keylen)
+{
+ PROV_AES_GCM_CTX *actx = (PROV_AES_GCM_CTX *)ctx;
+ AES_KEY *ks = &actx->ks.ks;
+
+ GCM_HW_SET_KEY_CTR_FN(ks, rv32i_zkne_set_encrypt_key, rv32i_zkne_encrypt,
+ NULL);
+ return 1;
+}
+
+static int rv32i_zbkb_zknd_zkne_gcm_initkey(PROV_GCM_CTX *ctx,
+ const unsigned char *key,
+ size_t keylen)
+{
+ PROV_AES_GCM_CTX *actx = (PROV_AES_GCM_CTX *)ctx;
+ AES_KEY *ks = &actx->ks.ks;
+
+ GCM_HW_SET_KEY_CTR_FN(ks, rv32i_zbkb_zkne_set_encrypt_key, rv32i_zkne_encrypt,
+ NULL);
+ return 1;
+}
+
+static const PROV_GCM_HW rv32i_zknd_zkne_gcm = {
+ rv32i_zknd_zkne_gcm_initkey,
+ ossl_gcm_setiv,
+ ossl_gcm_aad_update,
+ generic_aes_gcm_cipher_update,
+ ossl_gcm_cipher_final,
+ ossl_gcm_one_shot
+};
+
+static const PROV_GCM_HW rv32i_zbkb_zknd_zkne_gcm = {
+ rv32i_zbkb_zknd_zkne_gcm_initkey,
+ ossl_gcm_setiv,
+ ossl_gcm_aad_update,
+ generic_aes_gcm_cipher_update,
+ ossl_gcm_cipher_final,
+ ossl_gcm_one_shot
+};
+
+const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits)
+{
+ if (RV32I_ZBKB_ZKND_ZKNE_CAPABLE)
+ return &rv32i_zbkb_zknd_zkne_gcm;
+ if (RV32I_ZKND_ZKNE_CAPABLE)
+ return &rv32i_zknd_zkne_gcm;
+ return &aes_gcm;
+}
diff --git a/providers/implementations/ciphers/cipher_aes_hw.c b/providers/implementations/ciphers/cipher_aes_hw.c
index f829d9a67f..074d04d878 100644
--- a/providers/implementations/ciphers/cipher_aes_hw.c
+++ b/providers/implementations/ciphers/cipher_aes_hw.c
@@ -144,6 +144,8 @@ const PROV_CIPHER_HW *ossl_prov_cipher_hw_aes_##mode(size_t keybits) \
# 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"
#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_zknd_zkne.inc
new file mode 100644
index 0000000000..d3173fa401
--- /dev/null
+++ b/providers/implementations/ciphers/cipher_aes_hw_rv32i_zknd_zkne.inc
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2022 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
+ */
+
+/*-
+ * RISC-V 32 ZKND ZKNE support for AES modes ecb, cbc, ofb, cfb, ctr.
+ * This file is included by cipher_aes_hw.c
+ */
+
+#define cipher_hw_rv32i_zknd_zkne_cbc ossl_cipher_hw_generic_cbc
+#define cipher_hw_rv32i_zknd_zkne_ecb ossl_cipher_hw_generic_ecb
+#define cipher_hw_rv32i_zknd_zkne_ofb128 ossl_cipher_hw_generic_ofb128
+#define cipher_hw_rv32i_zknd_zkne_cfb128 ossl_cipher_hw_generic_cfb128
+#define cipher_hw_rv32i_zknd_zkne_cfb8 ossl_cipher_hw_generic_cfb8
+#define cipher_hw_rv32i_zknd_zkne_cfb1 ossl_cipher_hw_generic_cfb1
+#define cipher_hw_rv32i_zknd_zkne_ctr ossl_cipher_hw_generic_ctr
+
+#define cipher_hw_rv32i_zbkb_zknd_zkne_cbc ossl_cipher_hw_generic_cbc
+#define cipher_hw_rv32i_zbkb_zknd_zkne_ecb ossl_cipher_hw_generic_ecb
+#define cipher_hw_rv32i_zbkb_zknd_zkne_ofb128 ossl_cipher_hw_generic_ofb128
+#define cipher_hw_rv32i_zbkb_zknd_zkne_cfb128 ossl_cipher_hw_generic_cfb128
+#define cipher_hw_rv32i_zbkb_zknd_zkne_cfb8 ossl_cipher_hw_generic_cfb8
+#define cipher_hw_rv32i_zbkb_zknd_zkne_cfb1 ossl_cipher_hw_generic_cfb1
+#define cipher_hw_rv32i_zbkb_zknd_zkne_ctr ossl_cipher_hw_generic_ctr
+
+static int cipher_hw_rv32i_zknd_zkne_initkey(PROV_CIPHER_CTX *dat,
+ const unsigned char *key, size_t keylen)
+{
+ int ret;
+ PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
+ AES_KEY *ks = &adat->ks.ks;
+
+ dat->ks = ks;
+
+ if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
+ && !dat->enc) {
+ ret = rv32i_zknd_zkne_set_decrypt_key(key, keylen * 8, ks);
+ dat->block = (block128_f) rv32i_zknd_decrypt;
+ dat->stream.cbc = NULL;
+ } else {
+ ret = rv32i_zkne_set_encrypt_key(key, keylen * 8, ks);
+ dat->block = (block128_f) rv32i_zkne_encrypt;
+ dat->stream.cbc = NULL;
+ }
+
+ if (ret < 0) {
+ ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
+ return 0;
+ }
+
+ return 1;
+}
+
+static int cipher_hw_rv32i_zbkb_zknd_zkne_initkey(PROV_CIPHER_CTX *dat,
+ const unsigned char *key, size_t keylen)
+{
+ int ret;
+ PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
+ AES_KEY *ks = &adat->ks.ks;
+
+ dat->ks = ks;
+
+ if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
+ && !dat->enc) {
+ ret = rv32i_zbkb_zknd_zkne_set_decrypt_key(key, keylen * 8, ks);
+ dat->block = (block128_f) rv32i_zknd_decrypt;
+ dat->stream.cbc = NULL;
+ } else {
+ ret = rv32i_zbkb_zkne_set_encrypt_key(key, keylen * 8, ks);
+ dat->block = (block128_f) rv32i_zkne_encrypt;
+ dat->stream.cbc = NULL;
+ }
+
+ if (ret < 0) {
+ ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
+ return 0;
+ }
+
+ return 1;
+}
+
+#define PROV_CIPHER_HW_declare(mode) \
+static const PROV_CIPHER_HW rv32i_zknd_zkne_##mode = { \
+ cipher_hw_rv32i_zknd_zkne_initkey, \
+ cipher_hw_rv32i_zknd_zkne_##mode, \
+ cipher_hw_aes_copyctx \
+}; \
+static const PROV_CIPHER_HW rv32i_zbkb_zknd_zkne_##mode = { \
+ cipher_hw_rv32i_zbkb_zknd_zkne_initkey, \
+ cipher_hw_rv32i_zbkb_zknd_zkne_##mode, \
+ cipher_hw_aes_copyctx \
+};
+#define PROV_CIPHER_HW_select(mode) \
+if (RV32I_ZBKB_ZKND_ZKNE_CAPABLE) \
+ return &rv32i_zbkb_zknd_zkne_##mode; \
+if (RV32I_ZKND_ZKNE_CAPABLE) \
+ return &rv32i_zknd_zkne_##mode;
diff --git a/providers/implementations/ciphers/cipher_aes_ocb_hw.c b/providers/implementations/ciphers/cipher_aes_ocb_hw.c
index c7824521a1..5b93d2b717 100644
--- a/providers/implementations/ciphers/cipher_aes_ocb_hw.c
+++ b/providers/implementations/ciphers/cipher_aes_ocb_hw.c
@@ -124,6 +124,44 @@ static const PROV_CIPHER_HW aes_rv64i_zknd_zkne_ocb = { \
# define PROV_CIPHER_HW_select() \
if (RV64I_ZKND_ZKNE_CAPABLE) \
return &aes_rv64i_zknd_zkne_ocb;
+#elif defined(RV32I_ZBKB_ZKND_ZKNE_CAPABLE) && defined(RV32I_ZKND_ZKNE_CAPABLE)
+
+static int cipher_hw_aes_ocb_rv32i_zknd_zkne_initkey(PROV_CIPHER_CTX *vctx,
+ const unsigned char *key,
+ size_t keylen)
+{
+ PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
+
+ OCB_SET_KEY_FN(rv32i_zkne_set_encrypt_key, rv32i_zknd_zkne_set_decrypt_key,
+ rv32i_zkne_encrypt, rv32i_zknd_decrypt, NULL, NULL);
+ return 1;
+}
+
+static int cipher_hw_aes_ocb_rv32i_zbkb_zknd_zkne_initkey(PROV_CIPHER_CTX *vctx,
+ const unsigned char *key,
+ size_t keylen)
+{
+ PROV_AES_OCB_CTX *ctx = (PROV_AES_OCB_CTX *)vctx;
+
+ OCB_SET_KEY_FN(rv32i_zbkb_zkne_set_encrypt_key, rv32i_zbkb_zknd_zkne_set_decrypt_key,
+ rv32i_zkne_encrypt, rv32i_zknd_decrypt, NULL, NULL);
+ return 1;
+}
+
+# define PROV_CIPHER_HW_declare() \
+static const PROV_CIPHER_HW aes_rv32i_zknd_zkne_ocb = { \
+ cipher_hw_aes_ocb_rv32i_zknd_zkne_initkey, \
+ NULL \
+}; \
+static const PROV_CIPHER_HW aes_rv32i_zbkb_zknd_zkne_ocb = { \
+ cipher_hw_aes_ocb_rv32i_zbkb_zknd_zkne_initkey, \
+ NULL \
+};
+# define PROV_CIPHER_HW_select() \
+ if (RV32I_ZBKB_ZKND_ZKNE_CAPABLE) \
+ return &aes_rv32i_zbkb_zknd_zkne_ocb; \
+ if (RV32I_ZKND_ZKNE_CAPABLE) \
+ return &aes_rv32i_zknd_zkne_ocb;
#else
# define PROV_CIPHER_HW_declare()
# define PROV_CIPHER_HW_select()
diff --git a/providers/implementations/ciphers/cipher_aes_xts_hw.c b/providers/implementations/ciphers/cipher_aes_xts_hw.c
index c2cbf060fb..c8c9cbf19e 100644
--- a/providers/implementations/ciphers/cipher_aes_xts_hw.c
+++ b/providers/implementations/ciphers/cipher_aes_xts_hw.c
@@ -183,6 +183,48 @@ static const PROV_CIPHER_HW aes_xts_rv64i_zknd_zkne = { \
# define PROV_CIPHER_HW_select_xts() \
if (RV64I_ZKND_ZKNE_CAPABLE) \
return &aes_xts_rv64i_zknd_zkne;
+#elif defined(RV32I_ZBKB_ZKND_ZKNE_CAPABLE) && defined(RV32I_ZKND_ZKNE_CAPABLE)
+
+static int cipher_hw_aes_xts_rv32i_zknd_zkne_initkey(PROV_CIPHER_CTX *ctx,
+ const unsigned char *key,
+ size_t keylen)
+{
+ PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)ctx;
+
+ XTS_SET_KEY_FN(rv32i_zkne_set_encrypt_key, rv32i_zknd_zkne_set_decrypt_key,
+ rv32i_zkne_encrypt, rv32i_zknd_decrypt,
+ NULL, NULL);
+ return 1;
+}
+
+static int cipher_hw_aes_xts_rv32i_zbkb_zknd_zkne_initkey(PROV_CIPHER_CTX *ctx,
+ const unsigned char *key,
+ size_t keylen)
+{
+ PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)ctx;
+
+ XTS_SET_KEY_FN(rv32i_zbkb_zkne_set_encrypt_key, rv32i_zbkb_zknd_zkne_set_decrypt_key,
+ rv32i_zkne_encrypt, rv32i_zknd_decrypt,
+ NULL, NULL);
+ return 1;
+}
+
+# define PROV_CIPHER_HW_declare_xts() \
+static const PROV_CIPHER_HW aes_xts_rv32i_zknd_zkne = { \
+ cipher_hw_aes_xts_rv32i_zknd_zkne_initkey, \
+ NULL, \
+ cipher_hw_aes_xts_copyctx \
+}; \
+static const PROV_CIPHER_HW aes_xts_rv32i_zbkb_zknd_zkne = { \
+ cipher_hw_aes_xts_rv32i_zbkb_zknd_zkne_initkey, \
+ NULL, \
+ cipher_hw_aes_xts_copyctx \
+};
+# define PROV_CIPHER_HW_select_xts() \
+if (RV32I_ZBKB_ZKND_ZKNE_CAPABLE) \
+ return &aes_xts_rv32i_zbkb_zknd_zkne; \
+if (RV32I_ZKND_ZKNE_CAPABLE) \
+ return &aes_xts_rv32i_zknd_zkne;
# else
/* The generic case */
# define PROV_CIPHER_HW_declare_xts()