1 /*
2  * Copyright 2019-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 /* AES CCM mode */
11 
12 /*
13  * This file uses the low level AES functions (which are deprecated for
14  * non-internal use) in order to implement provider AES ciphers.
15  */
16 #include "internal/deprecated.h"
17 
18 #include "cipher_aes_ccm.h"
19 
20 #define AES_HW_CCM_SET_KEY_FN(fn_set_enc_key, fn_blk, fn_ccm_enc, fn_ccm_dec)  \
21     fn_set_enc_key(key, keylen * 8, &actx->ccm.ks.ks);                         \
22     CRYPTO_ccm128_init(&ctx->ccm_ctx, ctx->m, ctx->l, &actx->ccm.ks.ks,        \
23                        (block128_f)fn_blk);                                    \
24     ctx->str = ctx->enc ? (ccm128_f)fn_ccm_enc : (ccm128_f)fn_ccm_dec;         \
25     ctx->key_set = 1;
26 
ccm_generic_aes_initkey(PROV_CCM_CTX * ctx,const unsigned char * key,size_t keylen)27 static int ccm_generic_aes_initkey(PROV_CCM_CTX *ctx, const unsigned char *key,
28                                    size_t keylen)
29 {
30     PROV_AES_CCM_CTX *actx = (PROV_AES_CCM_CTX *)ctx;
31 
32 #ifdef HWAES_CAPABLE
33     if (HWAES_CAPABLE) {
34         AES_HW_CCM_SET_KEY_FN(HWAES_set_encrypt_key, HWAES_encrypt, NULL, NULL);
35     } else
36 #endif /* HWAES_CAPABLE */
37 
38 #ifdef VPAES_CAPABLE
39     if (VPAES_CAPABLE) {
40         AES_HW_CCM_SET_KEY_FN(vpaes_set_encrypt_key, vpaes_encrypt, NULL, NULL);
41     } else
42 #endif
43     {
44         AES_HW_CCM_SET_KEY_FN(AES_set_encrypt_key, AES_encrypt, NULL, NULL)
45     }
46     return 1;
47 }
48 
49 static const PROV_CCM_HW aes_ccm = {
50     ccm_generic_aes_initkey,
51     ossl_ccm_generic_setiv,
52     ossl_ccm_generic_setaad,
53     ossl_ccm_generic_auth_encrypt,
54     ossl_ccm_generic_auth_decrypt,
55     ossl_ccm_generic_gettag
56 };
57 
58 #if defined(S390X_aes_128_CAPABLE)
59 # include "cipher_aes_ccm_hw_s390x.inc"
60 #elif defined(AESNI_CAPABLE)
61 # include "cipher_aes_ccm_hw_aesni.inc"
62 #elif defined(SPARC_AES_CAPABLE)
63 # include "cipher_aes_ccm_hw_t4.inc"
64 #elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64
65 # include "cipher_aes_ccm_hw_rv64i.inc"
66 #elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 32
67 # include "cipher_aes_ccm_hw_rv32i.inc"
68 #else
ossl_prov_aes_hw_ccm(size_t keybits)69 const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits)
70 {
71     return &aes_ccm;
72 }
73 #endif
74