1/*
2 * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License").  You may not use
5 * this file except in compliance with the License.  You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10/*-
11 * RV64 ZVKSED support for AES modes ecb, cbc, ofb, cfb, ctr.
12 * This file is included by cipher_sm4_hw.c
13 */
14
15#define cipher_hw_rv64i_zvksed_sm4_cbc    ossl_cipher_hw_generic_cbc
16#define cipher_hw_rv64i_zvksed_sm4_ecb    ossl_cipher_hw_generic_ecb
17#define cipher_hw_rv64i_zvksed_sm4_ofb128 ossl_cipher_hw_generic_ofb128
18#define cipher_hw_rv64i_zvksed_sm4_cfb128 ossl_cipher_hw_generic_cfb128
19#define cipher_hw_rv64i_zvksed_sm4_ctr    ossl_cipher_hw_generic_ctr
20
21static int cipher_hw_rv64i_zvksed_sm4_initkey(PROV_CIPHER_CTX *ctx,
22                                              const unsigned char *key,
23                                              size_t keylen)
24{
25    PROV_SM4_CTX *sctx =  (PROV_SM4_CTX *)ctx;
26    SM4_KEY *ks = &sctx->ks.ks;
27
28    ctx->ks = ks;
29    if (ctx->enc
30            || (ctx->mode != EVP_CIPH_ECB_MODE
31                && ctx->mode != EVP_CIPH_CBC_MODE)) {
32        rv64i_zvksed_sm4_set_encrypt_key(key, ks);
33        ctx->block = (block128_f) rv64i_zvksed_sm4_encrypt;
34        ctx->stream.cbc = NULL;
35    } else {
36        rv64i_zvksed_sm4_set_decrypt_key(key, ks);
37        ctx->block = (block128_f) rv64i_zvksed_sm4_decrypt;
38        ctx->stream.cbc = NULL;
39    }
40
41    return 1;
42}
43
44#define PROV_CIPHER_HW_declare(mode)                                   \
45static const PROV_CIPHER_HW rv64i_zvksed_sm4_##mode = {                \
46    cipher_hw_rv64i_zvksed_sm4_initkey,                                \
47    cipher_hw_rv64i_zvksed_sm4_##mode,                                 \
48    cipher_hw_sm4_copyctx                                              \
49};
50#define PROV_CIPHER_HW_select(mode)                                    \
51if (RISCV_HAS_ZVKB_AND_ZVKSED() && riscv_vlen() >= 128)                \
52    return &rv64i_zvksed_sm4_##mode;
53