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 64 ZKND ZKNE support for AES CCM.
12 * This file is included by cipher_aes_ccm_hw.c
13 */
14
15static int ccm_rv64i_zknd_zkne_initkey(PROV_CCM_CTX *ctx, const unsigned char *key,
16                             size_t keylen)
17{
18    PROV_AES_CCM_CTX *actx = (PROV_AES_CCM_CTX *)ctx;
19
20    AES_HW_CCM_SET_KEY_FN(rv64i_zkne_set_encrypt_key, rv64i_zkne_encrypt,
21                          NULL, NULL);
22    return 1;
23}
24
25static const PROV_CCM_HW rv64i_zknd_zkne_ccm = {
26    ccm_rv64i_zknd_zkne_initkey,
27    ossl_ccm_generic_setiv,
28    ossl_ccm_generic_setaad,
29    ossl_ccm_generic_auth_encrypt,
30    ossl_ccm_generic_auth_decrypt,
31    ossl_ccm_generic_gettag
32};
33
34/*-
35 * RISC-V RV64 ZVKNED support for AES CCM.
36 * This file is included by cipher_aes_ccm_hw.c
37 */
38
39static int ccm_rv64i_zvkned_initkey(PROV_CCM_CTX *ctx, const unsigned char *key,
40                                    size_t keylen)
41{
42    PROV_AES_CCM_CTX *actx = (PROV_AES_CCM_CTX *)ctx;
43
44    /* Zvkned only supports 128 and 256 bit keys for key schedule generation. */
45    if (keylen * 8 == 128 || keylen * 8 == 256) {
46        AES_HW_CCM_SET_KEY_FN(rv64i_zvkned_set_encrypt_key, rv64i_zvkned_encrypt,
47                              NULL, NULL);
48    } else {
49        AES_HW_CCM_SET_KEY_FN(AES_set_encrypt_key, rv64i_zvkned_encrypt, NULL, NULL)
50    }
51    return 1;
52}
53
54static const PROV_CCM_HW rv64i_zvkned_ccm = {
55    ccm_rv64i_zvkned_initkey,
56    ossl_ccm_generic_setiv,
57    ossl_ccm_generic_setaad,
58    ossl_ccm_generic_auth_encrypt,
59    ossl_ccm_generic_auth_decrypt,
60    ossl_ccm_generic_gettag
61};
62
63const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits)
64{
65    if (RISCV_HAS_ZVKNED() && riscv_vlen() >= 128)
66        return &rv64i_zvkned_ccm;
67    else if (RISCV_HAS_ZKND_AND_ZKNE())
68        return &rv64i_zknd_zkne_ccm;
69    else
70        return &aes_ccm;
71}
72