Index: trunk/src/libs/openssl-3.1.3/include/crypto/context.h
===================================================================
--- trunk/src/libs/openssl-3.1.3/include/crypto/context.h	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/include/crypto/context.h	(revision 101240)
@@ -0,0 +1,41 @@
+/*
+ * 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
+ */
+
+#include <openssl/core.h>
+
+void *ossl_provider_store_new(OSSL_LIB_CTX *);
+void *ossl_property_string_data_new(OSSL_LIB_CTX *);
+void *ossl_stored_namemap_new(OSSL_LIB_CTX *);
+void *ossl_property_defns_new(OSSL_LIB_CTX *);
+void *ossl_ctx_global_properties_new(OSSL_LIB_CTX *);
+void *ossl_rand_ctx_new(OSSL_LIB_CTX *);
+void *ossl_prov_conf_ctx_new(OSSL_LIB_CTX *);
+void *ossl_bio_core_globals_new(OSSL_LIB_CTX *);
+void *ossl_child_prov_ctx_new(OSSL_LIB_CTX *);
+void *ossl_prov_drbg_nonce_ctx_new(OSSL_LIB_CTX *);
+void *ossl_self_test_set_callback_new(OSSL_LIB_CTX *);
+void *ossl_rand_crng_ctx_new(OSSL_LIB_CTX *);
+void *ossl_thread_event_ctx_new(OSSL_LIB_CTX *);
+void *ossl_fips_prov_ossl_ctx_new(OSSL_LIB_CTX *);
+
+void ossl_provider_store_free(void *);
+void ossl_property_string_data_free(void *);
+void ossl_stored_namemap_free(void *);
+void ossl_property_defns_free(void *);
+void ossl_ctx_global_properties_free(void *);
+void ossl_rand_ctx_free(void *);
+void ossl_prov_conf_ctx_free(void *);
+void ossl_bio_core_globals_free(void *);
+void ossl_child_prov_ctx_free(void *);
+void ossl_prov_drbg_nonce_ctx_free(void *);
+void ossl_self_test_set_callback_free(void *);
+void ossl_rand_crng_ctx_free(void *);
+void ossl_thread_event_ctx_free(void *);
+void ossl_fips_prov_ossl_ctx_free(void *);
+void ossl_release_default_drbg_ctx(void);
Index: trunk/src/libs/openssl-3.1.3/include/crypto/riscv_arch.def
===================================================================
--- trunk/src/libs/openssl-3.1.3/include/crypto/riscv_arch.def	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/include/crypto/riscv_arch.def	(revision 101240)
@@ -0,0 +1,43 @@
+/*
+ * 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
+ */
+
+/* X Macro Definitions for Specification of RISC-V Arch Capabilities */
+
+/*
+ * Each RISC-V capability ends up encoded as a single set bit in an array of
+ * words. When specifying a new capability, write a new RISCV_DEFINE_CAP
+ * statement, with an argument as the extension name in all-caps,
+ * second argument as the index in the array where the capability will be stored
+ * and third argument as the index of the bit to be used to encode the
+ * capability.
+ * RISCV_DEFINE_CAP(EXTENSION NAME, array index, bit index) */
+
+RISCV_DEFINE_CAP(ZBA, 0, 0)
+RISCV_DEFINE_CAP(ZBB, 0, 1)
+RISCV_DEFINE_CAP(ZBC, 0, 2)
+RISCV_DEFINE_CAP(ZBS, 0, 3)
+RISCV_DEFINE_CAP(ZBKB, 0, 4)
+RISCV_DEFINE_CAP(ZBKC, 0, 5)
+RISCV_DEFINE_CAP(ZBKX, 0, 6)
+RISCV_DEFINE_CAP(ZKND, 0, 7)
+RISCV_DEFINE_CAP(ZKNE, 0, 8)
+RISCV_DEFINE_CAP(ZKNH, 0, 9)
+RISCV_DEFINE_CAP(ZKSED, 0, 10)
+RISCV_DEFINE_CAP(ZKSH, 0, 11)
+RISCV_DEFINE_CAP(ZKR, 0, 12)
+RISCV_DEFINE_CAP(ZKT, 0, 13)
+
+/*
+ * In the future ...
+ * RISCV_DEFINE_CAP(ZFOO, 0, 31)
+ * RISCV_DEFINE_CAP(ZBAR, 1, 0)
+ * ... and so on.
+ */
+
+#undef RISCV_DEFINE_CAP
Index: trunk/src/libs/openssl-3.1.3/include/crypto/riscv_arch.h
===================================================================
--- trunk/src/libs/openssl-3.1.3/include/crypto/riscv_arch.h	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/include/crypto/riscv_arch.h	(revision 101240)
@@ -0,0 +1,59 @@
+/*
+ * 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
+ */
+
+#ifndef OSSL_CRYPTO_RISCV_ARCH_H
+# define OSSL_CRYPTO_RISCV_ARCH_H
+
+# include <ctype.h>
+# include <stdint.h>
+
+# define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX) +1
+extern uint32_t OPENSSL_riscvcap_P[ ((
+# include "riscv_arch.def"
+) + sizeof(uint32_t) - 1) / sizeof(uint32_t) ];
+
+# ifdef OPENSSL_RISCVCAP_IMPL
+#  define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX) +1
+uint32_t OPENSSL_riscvcap_P[ ((
+#  include "riscv_arch.def"
+) + sizeof(uint32_t) - 1) / sizeof(uint32_t) ];
+# endif
+
+# define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX)                   \
+    static inline int RISCV_HAS_##NAME(void)                        \
+    {                                                               \
+        return (OPENSSL_riscvcap_P[INDEX] & (1 << BIT_INDEX)) != 0; \
+    }
+# include "riscv_arch.def"
+
+struct RISCV_capability_s {
+    const char *name;
+    size_t index;
+    size_t bit_offset;
+};
+
+# define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX) +1
+extern const struct RISCV_capability_s RISCV_capabilities[
+# include "riscv_arch.def"
+];
+
+# ifdef OPENSSL_RISCVCAP_IMPL
+#  define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX) \
+    { #NAME, INDEX, BIT_INDEX },
+const struct RISCV_capability_s RISCV_capabilities[] = {
+#  include "riscv_arch.def"
+};
+# endif
+
+# define RISCV_DEFINE_CAP(NAME, INDEX, BIT_INDEX) +1
+static const size_t kRISCVNumCaps =
+# include "riscv_arch.def"
+;
+
+#endif
Index: trunk/src/libs/openssl-3.1.3/include/crypto/sm4_platform.h
===================================================================
--- trunk/src/libs/openssl-3.1.3/include/crypto/sm4_platform.h	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/include/crypto/sm4_platform.h	(revision 101240)
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2022-2023 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
+ */
+
+#ifndef OSSL_SM4_PLATFORM_H
+# define OSSL_SM4_PLATFORM_H
+# pragma once
+
+# if defined(OPENSSL_CPUID_OBJ)
+#  if defined(__aarch64__)
+#   include "arm_arch.h"
+extern unsigned int OPENSSL_arm_midr;
+static inline int vpsm4_capable(void)
+{
+    return (OPENSSL_armcap_P & ARMV8_CPUID) &&
+            (MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_V1) ||
+             MIDR_IS_CPU_MODEL(OPENSSL_arm_midr, ARM_CPU_IMP_ARM, ARM_CPU_PART_N1));
+}
+#   if defined(VPSM4_ASM)
+#    define VPSM4_CAPABLE vpsm4_capable()
+#   endif
+#   define HWSM4_CAPABLE (OPENSSL_armcap_P & ARMV8_SM4)
+#   define HWSM4_set_encrypt_key sm4_v8_set_encrypt_key
+#   define HWSM4_set_decrypt_key sm4_v8_set_decrypt_key
+#   define HWSM4_encrypt sm4_v8_encrypt
+#   define HWSM4_decrypt sm4_v8_decrypt
+#   define HWSM4_cbc_encrypt sm4_v8_cbc_encrypt
+#   define HWSM4_ecb_encrypt sm4_v8_ecb_encrypt
+#   define HWSM4_ctr32_encrypt_blocks sm4_v8_ctr32_encrypt_blocks
+#  endif
+# endif /* OPENSSL_CPUID_OBJ */
+
+# if defined(HWSM4_CAPABLE)
+int HWSM4_set_encrypt_key(const unsigned char *userKey, SM4_KEY *key);
+int HWSM4_set_decrypt_key(const unsigned char *userKey, SM4_KEY *key);
+void HWSM4_encrypt(const unsigned char *in, unsigned char *out,
+                   const SM4_KEY *key);
+void HWSM4_decrypt(const unsigned char *in, unsigned char *out,
+                   const SM4_KEY *key);
+void HWSM4_cbc_encrypt(const unsigned char *in, unsigned char *out,
+                       size_t length, const SM4_KEY *key,
+                       unsigned char *ivec, const int enc);
+void HWSM4_ecb_encrypt(const unsigned char *in, unsigned char *out,
+                       size_t length, const SM4_KEY *key,
+                       const int enc);
+void HWSM4_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
+                                size_t len, const void *key,
+                                const unsigned char ivec[16]);
+# endif /* HWSM4_CAPABLE */
+
+#ifdef VPSM4_CAPABLE
+int vpsm4_set_encrypt_key(const unsigned char *userKey, SM4_KEY *key);
+int vpsm4_set_decrypt_key(const unsigned char *userKey, SM4_KEY *key);
+void vpsm4_encrypt(const unsigned char *in, unsigned char *out,
+                   const SM4_KEY *key);
+void vpsm4_decrypt(const unsigned char *in, unsigned char *out,
+                   const SM4_KEY *key);
+void vpsm4_cbc_encrypt(const unsigned char *in, unsigned char *out,
+                       size_t length, const SM4_KEY *key,
+                       unsigned char *ivec, const int enc);
+void vpsm4_ecb_encrypt(const unsigned char *in, unsigned char *out,
+                       size_t length, const SM4_KEY *key,
+                       const int enc);
+void vpsm4_ctr32_encrypt_blocks(const unsigned char *in, unsigned char *out,
+                                size_t len, const void *key,
+                                const unsigned char ivec[16]);
+# endif /* VPSM4_CAPABLE */
+
+
+#endif /* OSSL_SM4_PLATFORM_H */
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i_zknd_zkne.inc
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i_zknd_zkne.inc	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_ccm_hw_rv32i_zknd_zkne.inc	(revision 101240)
@@ -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;
+}
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i_zknd_zkne.inc
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i_zknd_zkne.inc	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_ccm_hw_rv64i_zknd_zkne.inc	(revision 101240)
@@ -0,0 +1,37 @@
+/*
+ * 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 64 ZKND ZKNE support for AES CCM.
+ * This file is included by cipher_aes_ccm_hw.c
+ */
+
+static int ccm_rv64i_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(rv64i_zkne_set_encrypt_key, rv64i_zkne_encrypt,
+                          NULL, NULL);
+    return 1;
+}
+
+static const PROV_CCM_HW rv64i_zknd_zkne_ccm = {
+    ccm_rv64i_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)
+{
+    return RV64I_ZKND_ZKNE_CAPABLE ? &rv64i_zknd_zkne_ccm : &aes_ccm;
+}
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_ppc.inc
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_ppc.inc	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_ppc.inc	(revision 101240)
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2001-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
+ */
+
+/*-
+ * PPC support for AES GCM.
+ * This file is included by cipher_aes_gcm_hw.c
+ */
+
+static int aes_ppc_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, aes_p8_set_encrypt_key, aes_p8_encrypt,
+                          aes_p8_ctr32_encrypt_blocks);
+    return 1;
+}
+
+static inline u32 UTO32(unsigned char *buf)
+{
+    return ((u32) buf[0] << 24) | ((u32) buf[1] << 16) | ((u32) buf[2] << 8) | ((u32) buf[3]);
+}
+
+static inline u32 add32TOU(unsigned char buf[4], u32 n)
+{
+    u32 r;
+
+    r = UTO32(buf);
+    r += n;
+    buf[0] = (unsigned char) (r >> 24) & 0xFF;
+    buf[1] = (unsigned char) (r >> 16) & 0xFF;
+    buf[2] = (unsigned char) (r >> 8) & 0xFF;
+    buf[3] = (unsigned char) r & 0xFF;
+    return r;
+}
+
+static size_t ppc_aes_gcm_crypt(const unsigned char *in, unsigned char *out, size_t len,
+                                const void *key, unsigned char ivec[16], u64 *Xi, int encrypt)
+{
+    int s = 0;
+    int ndone = 0;
+    int ctr_reset = 0;
+    u64 blocks_unused;
+    u64 nb = len / 16;
+    u64 next_ctr = 0;
+    unsigned char ctr_saved[12];
+
+    memcpy(ctr_saved, ivec, 12);
+
+    while (nb) {
+        blocks_unused = (u64) 0xffffffffU + 1 - (u64) UTO32 (ivec + 12);
+        if (nb > blocks_unused) {
+            len = blocks_unused * 16;
+            nb -= blocks_unused;
+            next_ctr = blocks_unused;
+            ctr_reset = 1;
+        } else {
+            len = nb * 16;
+            next_ctr = nb;
+            nb = 0;
+        }
+
+        s = encrypt ? ppc_aes_gcm_encrypt(in, out, len, key, ivec, Xi)
+                    : ppc_aes_gcm_decrypt(in, out, len, key, ivec, Xi);
+
+        /* add counter to ivec */
+        add32TOU(ivec + 12, (u32) next_ctr);
+        if (ctr_reset) {
+            ctr_reset = 0;
+            in += len;
+            out += len;
+        }
+        memcpy(ivec, ctr_saved, 12);
+        ndone += s;
+    }
+
+    return ndone;
+}
+
+static int ppc_aes_gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,
+                                     size_t len, unsigned char *out)
+{
+    if (ctx->enc) {
+        if (ctx->ctr != NULL) {
+            size_t bulk = 0;
+
+            if (len >= AES_GCM_ENC_BYTES && AES_GCM_ASM_PPC(ctx)) {
+                size_t res = (16 - ctx->gcm.mres) % 16;
+
+                if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, res))
+                    return 0;
+
+                bulk = ppc_aes_gcm_crypt(in + res, out + res, len - res,
+                                         ctx->gcm.key,
+                                         ctx->gcm.Yi.c, ctx->gcm.Xi.u, 1);
+
+                ctx->gcm.len.u[1] += bulk;
+                bulk += res;
+            }
+            if (CRYPTO_gcm128_encrypt_ctr32(&ctx->gcm, in + bulk, out + bulk,
+                                            len - bulk, ctx->ctr))
+                return 0;
+        } else {
+            if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, len))
+                return 0;
+        }
+    } else {
+        if (ctx->ctr != NULL) {
+            size_t bulk = 0;
+
+            if (len >= AES_GCM_DEC_BYTES && AES_GCM_ASM_PPC(ctx)) {
+                size_t res = (16 - ctx->gcm.mres) % 16;
+
+                if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, res))
+                    return -1;
+
+                bulk = ppc_aes_gcm_crypt(in + res, out + res, len - res,
+                                         ctx->gcm.key,
+                                         ctx->gcm.Yi.c, ctx->gcm.Xi.u, 0);
+
+                ctx->gcm.len.u[1] += bulk;
+                bulk += res;
+            }
+            if (CRYPTO_gcm128_decrypt_ctr32(&ctx->gcm, in + bulk, out + bulk,
+                                            len - bulk, ctx->ctr))
+                return 0;
+        } else {
+            if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, len))
+                return 0;
+        }
+    }
+    return 1;
+}
+
+static const PROV_GCM_HW aes_ppc_gcm = {
+    aes_ppc_gcm_initkey,
+    ossl_gcm_setiv,
+    ossl_gcm_aad_update,
+    ppc_aes_gcm_cipher_update,
+    ossl_gcm_cipher_final,
+    ossl_gcm_one_shot
+};
+
+const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits)
+{
+    return PPC_AES_GCM_CAPABLE ? &aes_ppc_gcm : &aes_gcm;
+}
+
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i_zknd_zkne.inc
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i_zknd_zkne.inc	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_rv32i_zknd_zkne.inc	(revision 101240)
@@ -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;
+}
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i_zknd_zkne.inc
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i_zknd_zkne.inc	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_rv64i_zknd_zkne.inc	(revision 101240)
@@ -0,0 +1,40 @@
+/*
+ * 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 64 ZKND ZKNE support for AES GCM.
+ * This file is included by cipher_aes_gcm_hw.c
+ */
+
+static int rv64i_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, rv64i_zkne_set_encrypt_key, rv64i_zkne_encrypt,
+                          NULL);
+    return 1;
+}
+
+static const PROV_GCM_HW rv64i_zknd_zkne_gcm = {
+    rv64i_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 (RV64I_ZKND_ZKNE_CAPABLE)
+        return &rv64i_zknd_zkne_gcm;
+    else
+        return &aes_gcm;
+}
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_vaes_avx512.inc
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_vaes_avx512.inc	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_gcm_hw_vaes_avx512.inc	(revision 101240)
@@ -0,0 +1,205 @@
+/*
+ * Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright (c) 2021, Intel Corporation. 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
+ */
+
+/*-
+ * AVX512 VAES + VPCLMULDQD support for AES GCM.
+ * This file is included by cipher_aes_gcm_hw_aesni.inc
+ */
+
+#undef VAES_GCM_ENABLED
+#if (defined(__x86_64) || defined(__x86_64__) || \
+     defined(_M_AMD64) || defined(_M_X64))
+# define VAES_GCM_ENABLED
+
+/* Returns non-zero when AVX512F + VAES + VPCLMULDQD combination is available */
+int ossl_vaes_vpclmulqdq_capable(void);
+
+# define OSSL_AES_GCM_UPDATE(direction)                                 \
+    void ossl_aes_gcm_ ## direction ## _avx512(const void *ks,          \
+                                               void *gcm128ctx,         \
+                                               unsigned int *pblocklen, \
+                                               const unsigned char *in, \
+                                               size_t len,              \
+                                               unsigned char *out);
+
+OSSL_AES_GCM_UPDATE(encrypt)
+OSSL_AES_GCM_UPDATE(decrypt)
+
+void ossl_aes_gcm_init_avx512(const void *ks, void *gcm128ctx);
+void ossl_aes_gcm_setiv_avx512(const void *ks, void *gcm128ctx,
+                               const unsigned char *iv, size_t ivlen);
+void ossl_aes_gcm_update_aad_avx512(void *gcm128ctx, const unsigned char *aad,
+                                    size_t aadlen);
+void ossl_aes_gcm_finalize_avx512(void *gcm128ctx, unsigned int pblocklen);
+
+void ossl_gcm_gmult_avx512(u64 Xi[2], const void *gcm128ctx);
+
+static int vaes_gcm_setkey(PROV_GCM_CTX *ctx, const unsigned char *key,
+                           size_t keylen)
+{
+    GCM128_CONTEXT *gcmctx = &ctx->gcm;
+    PROV_AES_GCM_CTX *actx = (PROV_AES_GCM_CTX *)ctx;
+    AES_KEY *ks = &actx->ks.ks;
+
+    ctx->ks = ks;
+    aesni_set_encrypt_key(key, keylen * 8, ks);
+    memset(gcmctx, 0, sizeof(*gcmctx));
+    gcmctx->key = ks;
+    ctx->key_set = 1;
+
+    ossl_aes_gcm_init_avx512(ks, gcmctx);
+
+    return 1;
+}
+
+static int vaes_gcm_setiv(PROV_GCM_CTX *ctx, const unsigned char *iv,
+                          size_t ivlen)
+{
+    GCM128_CONTEXT *gcmctx = &ctx->gcm;
+
+    gcmctx->Yi.u[0] = 0;           /* Current counter */
+    gcmctx->Yi.u[1] = 0;
+    gcmctx->Xi.u[0] = 0;           /* AAD hash */
+    gcmctx->Xi.u[1] = 0;
+    gcmctx->len.u[0] = 0;          /* AAD length */
+    gcmctx->len.u[1] = 0;          /* Message length */
+    gcmctx->ares = 0;
+    gcmctx->mres = 0;
+
+    /* IV is limited by 2^64 bits, thus 2^61 bytes */
+    if (ivlen > (U64(1) << 61))
+        return 0;
+
+    ossl_aes_gcm_setiv_avx512(ctx->ks, gcmctx, iv, ivlen);
+
+    return 1;
+}
+
+static int vaes_gcm_aadupdate(PROV_GCM_CTX *ctx,
+                              const unsigned char *aad,
+                              size_t aad_len)
+{
+    GCM128_CONTEXT *gcmctx = &ctx->gcm;
+    u64 alen = gcmctx->len.u[0];
+    unsigned int ares;
+    size_t i, lenBlks;
+
+    /* Bad sequence: call of AAD update after message processing */
+    if (gcmctx->len.u[1] > 0)
+        return 0;
+
+    alen += aad_len;
+    /* AAD is limited by 2^64 bits, thus 2^61 bytes */
+    if ((alen > (U64(1) << 61)) || (alen < aad_len))
+        return 0;
+
+    gcmctx->len.u[0] = alen;
+
+    ares = gcmctx->ares;
+    /* Partial AAD block left from previous AAD update calls */
+    if (ares > 0) {
+        /*
+         * Fill partial block buffer till full block
+         * (note, the hash is stored reflected)
+         */
+        while (ares > 0 && aad_len > 0) {
+            gcmctx->Xi.c[15 - ares] ^= *(aad++);
+            --aad_len;
+            ares = (ares + 1) % AES_BLOCK_SIZE;
+        }
+        /* Full block gathered */
+        if (ares == 0) {
+            ossl_gcm_gmult_avx512(gcmctx->Xi.u, gcmctx);
+        } else { /* no more AAD */
+            gcmctx->ares = ares;
+            return 1;
+        }
+    }
+
+    /* Bulk AAD processing */
+    lenBlks = aad_len & ((size_t)(-AES_BLOCK_SIZE));
+    if (lenBlks > 0) {
+        ossl_aes_gcm_update_aad_avx512(gcmctx, aad, lenBlks);
+        aad += lenBlks;
+        aad_len -= lenBlks;
+    }
+
+    /* Add remaining AAD to the hash (note, the hash is stored reflected) */
+    if (aad_len > 0) {
+        ares = aad_len;
+        for (i = 0; i < aad_len; i++)
+            gcmctx->Xi.c[15 - i] ^= aad[i];
+    }
+
+    gcmctx->ares = ares;
+
+    return 1;
+}
+
+static int vaes_gcm_cipherupdate(PROV_GCM_CTX *ctx, const unsigned char *in,
+                                 size_t len, unsigned char *out)
+{
+    GCM128_CONTEXT *gcmctx = &ctx->gcm;
+    u64 mlen = gcmctx->len.u[1];
+
+    mlen += len;
+    if (mlen > ((U64(1) << 36) - 32) || (mlen < len))
+        return 0;
+
+    gcmctx->len.u[1] = mlen;
+
+    /* Finalize GHASH(AAD) if AAD partial blocks left unprocessed */
+    if (gcmctx->ares > 0) {
+        ossl_gcm_gmult_avx512(gcmctx->Xi.u, gcmctx);
+        gcmctx->ares = 0;
+    }
+
+    if (ctx->enc)
+        ossl_aes_gcm_encrypt_avx512(ctx->ks, gcmctx, &gcmctx->mres, in, len, out);
+    else
+        ossl_aes_gcm_decrypt_avx512(ctx->ks, gcmctx, &gcmctx->mres, in, len, out);
+
+    return 1;
+}
+
+static int vaes_gcm_cipherfinal(PROV_GCM_CTX *ctx, unsigned char *tag)
+{
+    GCM128_CONTEXT *gcmctx = &ctx->gcm;
+    unsigned int *res = &gcmctx->mres;
+
+    /* Finalize AAD processing */
+    if (gcmctx->ares > 0)
+        res = &gcmctx->ares;
+
+    ossl_aes_gcm_finalize_avx512(gcmctx, *res);
+
+    if (ctx->enc) {
+        ctx->taglen = GCM_TAG_MAX_SIZE;
+        memcpy(tag, gcmctx->Xi.c,
+               ctx->taglen <= sizeof(gcmctx->Xi.c) ? ctx->taglen :
+               sizeof(gcmctx->Xi.c));
+        *res = 0;
+    } else {
+        return !CRYPTO_memcmp(gcmctx->Xi.c, tag, ctx->taglen);
+    }
+
+    return 1;
+}
+
+static const PROV_GCM_HW vaes_gcm = {
+    vaes_gcm_setkey,
+    vaes_gcm_setiv,
+    vaes_gcm_aadupdate,
+    vaes_gcm_cipherupdate,
+    vaes_gcm_cipherfinal,
+    ossl_gcm_one_shot
+};
+
+#endif
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_hw_rv32i_zknd_zkne.inc
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_hw_rv32i_zknd_zkne.inc	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_hw_rv32i_zknd_zkne.inc	(revision 101240)
@@ -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;
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_hw_rv64i_zknd_zkne.inc
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_hw_rv64i_zknd_zkne.inc	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_aes_hw_rv64i_zknd_zkne.inc	(revision 101240)
@@ -0,0 +1,59 @@
+/*
+ * 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 64 ZKND ZKNE support for AES modes ecb, cbc, ofb, cfb, ctr.
+ * This file is included by cipher_aes_hw.c
+ */
+
+#define cipher_hw_rv64i_zknd_zkne_cbc    ossl_cipher_hw_generic_cbc
+#define cipher_hw_rv64i_zknd_zkne_ecb    ossl_cipher_hw_generic_ecb
+#define cipher_hw_rv64i_zknd_zkne_ofb128 ossl_cipher_hw_generic_ofb128
+#define cipher_hw_rv64i_zknd_zkne_cfb128 ossl_cipher_hw_generic_cfb128
+#define cipher_hw_rv64i_zknd_zkne_cfb8   ossl_cipher_hw_generic_cfb8
+#define cipher_hw_rv64i_zknd_zkne_cfb1   ossl_cipher_hw_generic_cfb1
+#define cipher_hw_rv64i_zknd_zkne_ctr    ossl_cipher_hw_generic_ctr
+
+static int cipher_hw_rv64i_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 = rv64i_zknd_set_decrypt_key(key, keylen * 8, ks);
+        dat->block = (block128_f) rv64i_zknd_decrypt;
+        dat->stream.cbc = NULL;
+    } else {
+        ret = rv64i_zkne_set_encrypt_key(key, keylen * 8, ks);
+        dat->block = (block128_f) rv64i_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 rv64i_zknd_zkne_##mode = {                         \
+    cipher_hw_rv64i_zknd_zkne_initkey,                                         \
+    cipher_hw_rv64i_zknd_zkne_##mode,                                          \
+    cipher_hw_aes_copyctx                                                      \
+};
+#define PROV_CIPHER_HW_select(mode)                                            \
+if (RV64I_ZKND_ZKNE_CAPABLE)                                                   \
+    return &rv64i_zknd_zkne_##mode;
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_ccm.c
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_ccm.c	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_ccm.c	(revision 101240)
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2021-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
+ */
+
+/* Dispatch functions for SM4 CCM mode */
+
+#include "cipher_sm4_ccm.h"
+#include "prov/implementations.h"
+#include "prov/providercommon.h"
+
+static OSSL_FUNC_cipher_freectx_fn sm4_ccm_freectx;
+
+static void *sm4_ccm_newctx(void *provctx, size_t keybits)
+{
+    PROV_SM4_CCM_CTX *ctx;
+
+    if (!ossl_prov_is_running())
+        return NULL;
+
+    ctx = OPENSSL_zalloc(sizeof(*ctx));
+    if (ctx != NULL)
+        ossl_ccm_initctx(&ctx->base, keybits, ossl_prov_sm4_hw_ccm(keybits));
+    return ctx;
+}
+
+static void sm4_ccm_freectx(void *vctx)
+{
+    PROV_SM4_CCM_CTX *ctx = (PROV_SM4_CCM_CTX *)vctx;
+
+    OPENSSL_clear_free(ctx,  sizeof(*ctx));
+}
+
+/* sm4128ccm functions */
+IMPLEMENT_aead_cipher(sm4, ccm, CCM, AEAD_FLAGS, 128, 8, 96);
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_ccm.h
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_ccm.h	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_ccm.h	(revision 101240)
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2021-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
+ */
+
+#include "crypto/sm4.h"
+#include "prov/ciphercommon.h"
+#include "prov/ciphercommon_ccm.h"
+
+typedef struct prov_sm4_ccm_ctx_st {
+    PROV_CCM_CTX base; /* Must be first */
+    union {
+        OSSL_UNION_ALIGN;
+        SM4_KEY ks;
+    } ks;                       /* SM4 key schedule to use */
+} PROV_SM4_CCM_CTX;
+
+const PROV_CCM_HW *ossl_prov_sm4_hw_ccm(size_t keylen);
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_ccm_hw.c
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_ccm_hw.c	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_ccm_hw.c	(revision 101240)
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2021-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
+ */
+
+/*-
+ * Generic support for SM4 CCM.
+ */
+
+#include "cipher_sm4_ccm.h"
+
+static int ccm_sm4_initkey(PROV_CCM_CTX *ctx,
+                           const unsigned char *key, size_t keylen)
+{
+    PROV_SM4_CCM_CTX *actx = (PROV_SM4_CCM_CTX *)ctx;
+
+    ossl_sm4_set_key(key, &actx->ks.ks);
+    CRYPTO_ccm128_init(&ctx->ccm_ctx, ctx->m, ctx->l, &actx->ks.ks,
+                       (block128_f)ossl_sm4_encrypt);
+    ctx->str = NULL;
+    ctx->key_set = 1;
+    return 1;
+}
+
+static const PROV_CCM_HW ccm_sm4 = {
+    ccm_sm4_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_sm4_hw_ccm(size_t keybits)
+{
+    return &ccm_sm4;
+}
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_gcm.c
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_gcm.c	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_gcm.c	(revision 101240)
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2021-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
+ */
+
+/* Dispatch functions for SM4 GCM mode */
+
+#include "cipher_sm4_gcm.h"
+#include "prov/implementations.h"
+#include "prov/providercommon.h"
+
+static OSSL_FUNC_cipher_freectx_fn sm4_gcm_freectx;
+
+static void *sm4_gcm_newctx(void *provctx, size_t keybits)
+{
+    PROV_SM4_GCM_CTX *ctx;
+
+    if (!ossl_prov_is_running())
+        return NULL;
+
+    ctx = OPENSSL_zalloc(sizeof(*ctx));
+    if (ctx != NULL)
+        ossl_gcm_initctx(provctx, &ctx->base, keybits,
+                         ossl_prov_sm4_hw_gcm(keybits));
+    return ctx;
+}
+
+static void sm4_gcm_freectx(void *vctx)
+{
+    PROV_SM4_GCM_CTX *ctx = (PROV_SM4_GCM_CTX *)vctx;
+
+    OPENSSL_clear_free(ctx,  sizeof(*ctx));
+}
+
+/* ossl_sm4128gcm_functions */
+IMPLEMENT_aead_cipher(sm4, gcm, GCM, AEAD_FLAGS, 128, 8, 96);
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_gcm.h
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_gcm.h	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_gcm.h	(revision 101240)
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2021-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
+ */
+
+#include "crypto/sm4.h"
+#include "prov/ciphercommon.h"
+#include "prov/ciphercommon_gcm.h"
+
+typedef struct prov_sm4_gcm_ctx_st {
+    PROV_GCM_CTX base;              /* must be first entry in struct */
+    union {
+        OSSL_UNION_ALIGN;
+        SM4_KEY ks;
+    } ks;
+} PROV_SM4_GCM_CTX;
+
+const PROV_GCM_HW *ossl_prov_sm4_hw_gcm(size_t keybits);
Index: trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_gcm_hw.c
===================================================================
--- trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_gcm_hw.c	(revision 101240)
+++ trunk/src/libs/openssl-3.1.3/providers/implementations/ciphers/cipher_sm4_gcm_hw.c	(revision 101240)
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2021-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
+ */
+
+/*-
+ * Generic support for SM4 GCM.
+ */
+
+#include "cipher_sm4_gcm.h"
+#include "crypto/sm4_platform.h"
+
+static int sm4_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
+                           size_t keylen)
+{
+    PROV_SM4_GCM_CTX *actx = (PROV_SM4_GCM_CTX *)ctx;
+    SM4_KEY *ks = &actx->ks.ks;
+
+    ctx->ks = ks;
+# ifdef HWSM4_CAPABLE
+    if (HWSM4_CAPABLE) {
+        HWSM4_set_encrypt_key(key, ks);
+        CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f) HWSM4_encrypt);
+#  ifdef HWSM4_ctr32_encrypt_blocks
+        ctx->ctr = (ctr128_f) HWSM4_ctr32_encrypt_blocks;
+#  else /* HWSM4_ctr32_encrypt_blocks */
+        ctx->ctr = (ctr128_f)NULL;
+#  endif
+    } else
+# endif /* HWSM4_CAPABLE */
+# ifdef VPSM4_CAPABLE
+    if (VPSM4_CAPABLE) {
+        vpsm4_set_encrypt_key(key, ks);
+        CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f) vpsm4_encrypt);
+        ctx->ctr = (ctr128_f) vpsm4_ctr32_encrypt_blocks;
+    } else
+# endif /* VPSM4_CAPABLE */
+    {
+        ossl_sm4_set_key(key, ks);
+        CRYPTO_gcm128_init(&ctx->gcm, ks, (block128_f)ossl_sm4_encrypt);
+        ctx->ctr = (ctr128_f)NULL;
+    }
+    ctx->key_set = 1;
+
+    return 1;
+}
+
+static int hw_gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,
+                                size_t len, unsigned char *out)
+{
+    if (ctx->enc) {
+        if (ctx->ctr != NULL) {
+            if (CRYPTO_gcm128_encrypt_ctr32(&ctx->gcm, in, out, len, ctx->ctr))
+                return 0;
+        } else {
+            if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, len))
+                return 0;
+        }
+    } else {
+        if (ctx->ctr != NULL) {
+            if (CRYPTO_gcm128_decrypt_ctr32(&ctx->gcm, in, out, len, ctx->ctr))
+                return 0;
+        } else {
+            if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, len))
+                return 0;
+        }
+    }
+    return 1;
+}
+
+static const PROV_GCM_HW sm4_gcm = {
+    sm4_gcm_initkey,
+    ossl_gcm_setiv,
+    ossl_gcm_aad_update,
+    hw_gcm_cipher_update,
+    ossl_gcm_cipher_final,
+    ossl_gcm_one_shot
+};
+
+const PROV_GCM_HW *ossl_prov_sm4_hw_gcm(size_t keybits)
+{
+    return &sm4_gcm;
+}
