1/*
2 * Copyright 2022-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 * RISC-V 32 ZKND ZKNE support for AES modes ecb, cbc, ofb, cfb, ctr.
12 * This file is included by cipher_aes_hw.c
13 */
14
15#define cipher_hw_rv32i_zknd_zkne_cbc    ossl_cipher_hw_generic_cbc
16#define cipher_hw_rv32i_zknd_zkne_ecb    ossl_cipher_hw_generic_ecb
17#define cipher_hw_rv32i_zknd_zkne_ofb128 ossl_cipher_hw_generic_ofb128
18#define cipher_hw_rv32i_zknd_zkne_cfb128 ossl_cipher_hw_generic_cfb128
19#define cipher_hw_rv32i_zknd_zkne_cfb8   ossl_cipher_hw_generic_cfb8
20#define cipher_hw_rv32i_zknd_zkne_cfb1   ossl_cipher_hw_generic_cfb1
21#define cipher_hw_rv32i_zknd_zkne_ctr    ossl_cipher_hw_generic_ctr
22
23#define cipher_hw_rv32i_zbkb_zknd_zkne_cbc    ossl_cipher_hw_generic_cbc
24#define cipher_hw_rv32i_zbkb_zknd_zkne_ecb    ossl_cipher_hw_generic_ecb
25#define cipher_hw_rv32i_zbkb_zknd_zkne_ofb128 ossl_cipher_hw_generic_ofb128
26#define cipher_hw_rv32i_zbkb_zknd_zkne_cfb128 ossl_cipher_hw_generic_cfb128
27#define cipher_hw_rv32i_zbkb_zknd_zkne_cfb8   ossl_cipher_hw_generic_cfb8
28#define cipher_hw_rv32i_zbkb_zknd_zkne_cfb1   ossl_cipher_hw_generic_cfb1
29#define cipher_hw_rv32i_zbkb_zknd_zkne_ctr    ossl_cipher_hw_generic_ctr
30
31static int cipher_hw_rv32i_zknd_zkne_initkey(PROV_CIPHER_CTX *dat,
32                                             const unsigned char *key, size_t keylen)
33{
34    int ret;
35    PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
36    AES_KEY *ks = &adat->ks.ks;
37
38    dat->ks = ks;
39
40    if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
41        && !dat->enc) {
42        ret = rv32i_zknd_zkne_set_decrypt_key(key, keylen * 8, ks);
43        dat->block = (block128_f) rv32i_zknd_decrypt;
44        dat->stream.cbc = NULL;
45    } else {
46        ret = rv32i_zkne_set_encrypt_key(key, keylen * 8, ks);
47        dat->block = (block128_f) rv32i_zkne_encrypt;
48        dat->stream.cbc = NULL;
49    }
50
51    if (ret < 0) {
52        ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
53        return 0;
54    }
55
56    return 1;
57}
58
59static int cipher_hw_rv32i_zbkb_zknd_zkne_initkey(PROV_CIPHER_CTX *dat,
60                                                  const unsigned char *key, size_t keylen)
61{
62    int ret;
63    PROV_AES_CTX *adat = (PROV_AES_CTX *)dat;
64    AES_KEY *ks = &adat->ks.ks;
65
66    dat->ks = ks;
67
68    if ((dat->mode == EVP_CIPH_ECB_MODE || dat->mode == EVP_CIPH_CBC_MODE)
69        && !dat->enc) {
70        ret = rv32i_zbkb_zknd_zkne_set_decrypt_key(key, keylen * 8, ks);
71        dat->block = (block128_f) rv32i_zknd_decrypt;
72        dat->stream.cbc = NULL;
73    } else {
74        ret = rv32i_zbkb_zkne_set_encrypt_key(key, keylen * 8, ks);
75        dat->block = (block128_f) rv32i_zkne_encrypt;
76        dat->stream.cbc = NULL;
77    }
78
79    if (ret < 0) {
80        ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SETUP_FAILED);
81        return 0;
82    }
83
84    return 1;
85}
86
87#define PROV_CIPHER_HW_declare(mode)                                           \
88static const PROV_CIPHER_HW rv32i_zknd_zkne_##mode = {                         \
89    cipher_hw_rv32i_zknd_zkne_initkey,                                         \
90    cipher_hw_rv32i_zknd_zkne_##mode,                                          \
91    cipher_hw_aes_copyctx                                                      \
92};                                                                             \
93static const PROV_CIPHER_HW rv32i_zbkb_zknd_zkne_##mode = {                    \
94    cipher_hw_rv32i_zbkb_zknd_zkne_initkey,                                    \
95    cipher_hw_rv32i_zbkb_zknd_zkne_##mode,                                     \
96    cipher_hw_aes_copyctx                                                      \
97};
98#define PROV_CIPHER_HW_select(mode)                                            \
99if (RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE())                                        \
100    return &rv32i_zbkb_zknd_zkne_##mode;                                       \
101if (RISCV_HAS_ZKND_AND_ZKNE())                                                 \
102    return &rv32i_zknd_zkne_##mode;
103