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, (int)(keylen * 8), &actx->ccm.ks.ks); \
22 CRYPTO_ccm128_init(&ctx->ccm_ctx, (unsigned int)ctx->m, \
23 (unsigned int)ctx->l, &actx->ccm.ks.ks, \
24 (block128_f)fn_blk); \
25 ctx->str = ctx->enc ? (ccm128_f)fn_ccm_enc : (ccm128_f)fn_ccm_dec; \
26 ctx->key_set = 1;
27
ccm_generic_aes_initkey(PROV_CCM_CTX * ctx,const unsigned char * key,size_t keylen)28 static int ccm_generic_aes_initkey(PROV_CCM_CTX *ctx, const unsigned char *key,
29 size_t keylen)
30 {
31 PROV_AES_CCM_CTX *actx = (PROV_AES_CCM_CTX *)ctx;
32
33 #ifdef HWAES_CAPABLE
34 if (HWAES_CAPABLE) {
35 AES_HW_CCM_SET_KEY_FN(HWAES_set_encrypt_key, HWAES_encrypt, NULL, NULL);
36 } else
37 #endif /* HWAES_CAPABLE */
38
39 #ifdef VPAES_CAPABLE
40 if (VPAES_CAPABLE) {
41 AES_HW_CCM_SET_KEY_FN(vpaes_set_encrypt_key, vpaes_encrypt, NULL, NULL);
42 } else
43 #endif
44 {
45 AES_HW_CCM_SET_KEY_FN(AES_set_encrypt_key, AES_encrypt, NULL, NULL)
46 }
47 return 1;
48 }
49
50 static const PROV_CCM_HW aes_ccm = {
51 ccm_generic_aes_initkey,
52 ossl_ccm_generic_setiv,
53 ossl_ccm_generic_setaad,
54 ossl_ccm_generic_auth_encrypt,
55 ossl_ccm_generic_auth_decrypt,
56 ossl_ccm_generic_gettag
57 };
58
59 #if defined(S390X_aes_128_CAPABLE)
60 # include "cipher_aes_ccm_hw_s390x.inc"
61 #elif defined(AESNI_CAPABLE)
62 # include "cipher_aes_ccm_hw_aesni.inc"
63 #elif defined(SPARC_AES_CAPABLE)
64 # include "cipher_aes_ccm_hw_t4.inc"
65 #elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64
66 # include "cipher_aes_ccm_hw_rv64i.inc"
67 #elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 32
68 # include "cipher_aes_ccm_hw_rv32i.inc"
69 #else
ossl_prov_aes_hw_ccm(size_t keybits)70 const PROV_CCM_HW *ossl_prov_aes_hw_ccm(size_t keybits)
71 {
72 return &aes_ccm;
73 }
74 #endif
75