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 GCM.
12 * This file is included by cipher_aes_gcm_hw.c
13 */
14
15static int rv32i_zknd_zkne_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
16                                       size_t keylen)
17{
18    PROV_AES_GCM_CTX *actx = (PROV_AES_GCM_CTX *)ctx;
19    AES_KEY *ks = &actx->ks.ks;
20
21    GCM_HW_SET_KEY_CTR_FN(ks, rv32i_zkne_set_encrypt_key, rv32i_zkne_encrypt,
22                          NULL);
23    return 1;
24}
25
26static int rv32i_zbkb_zknd_zkne_gcm_initkey(PROV_GCM_CTX *ctx,
27                                            const unsigned char *key,
28                                            size_t keylen)
29{
30    PROV_AES_GCM_CTX *actx = (PROV_AES_GCM_CTX *)ctx;
31    AES_KEY *ks = &actx->ks.ks;
32
33    GCM_HW_SET_KEY_CTR_FN(ks, rv32i_zbkb_zkne_set_encrypt_key, rv32i_zkne_encrypt,
34                          NULL);
35    return 1;
36}
37
38static const PROV_GCM_HW rv32i_zknd_zkne_gcm = {
39    rv32i_zknd_zkne_gcm_initkey,
40    ossl_gcm_setiv,
41    ossl_gcm_aad_update,
42    generic_aes_gcm_cipher_update,
43    ossl_gcm_cipher_final,
44    ossl_gcm_one_shot
45};
46
47static const PROV_GCM_HW rv32i_zbkb_zknd_zkne_gcm = {
48    rv32i_zbkb_zknd_zkne_gcm_initkey,
49    ossl_gcm_setiv,
50    ossl_gcm_aad_update,
51    generic_aes_gcm_cipher_update,
52    ossl_gcm_cipher_final,
53    ossl_gcm_one_shot
54};
55
56const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits)
57{
58    if (RISCV_HAS_ZBKB_AND_ZKND_AND_ZKNE())
59        return &rv32i_zbkb_zknd_zkne_gcm;
60    if (RISCV_HAS_ZKND_AND_ZKNE())
61        return &rv32i_zknd_zkne_gcm;
62    return &aes_gcm;
63}
64