1/*
2 * Copyright 2019 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#include "cipher_aes_ccm.h"
13
14#define AES_HW_CCM_SET_KEY_FN(fn_set_enc_key, fn_blk, fn_ccm_enc, fn_ccm_dec) \
15 fn_set_enc_key(key, keylen * 8, &actx->ccm.ks.ks); \
16 CRYPTO_ccm128_init(&ctx->ccm_ctx, ctx->m, ctx->l, &actx->ccm.ks.ks, \
17 (block128_f)fn_blk); \
18 ctx->str = ctx->enc ? (ccm128_f)fn_ccm_enc : (ccm128_f)fn_ccm_dec; \
19 ctx->key_set = 1;
20
21static int ccm_generic_aes_initkey(PROV_CCM_CTX *ctx, const unsigned char *key,
22 size_t keylen)
23{
24 PROV_AES_CCM_CTX *actx = (PROV_AES_CCM_CTX *)ctx;
25
26#ifdef HWAES_CAPABLE
27 if (HWAES_CAPABLE) {
28 AES_HW_CCM_SET_KEY_FN(HWAES_set_encrypt_key, HWAES_encrypt, NULL, NULL);
29 } else
30#endif /* HWAES_CAPABLE */
31
32#ifdef VPAES_CAPABLE
33 if (VPAES_CAPABLE) {
34 AES_HW_CCM_SET_KEY_FN(vpaes_set_encrypt_key, vpaes_encrypt, NULL, NULL);
35 } else
36#endif
37 {
38 AES_HW_CCM_SET_KEY_FN(AES_set_encrypt_key, AES_encrypt, NULL, NULL)
39 }
40 return 1;
41}
42
43static const PROV_CCM_HW aes_ccm = {
44 ccm_generic_aes_initkey,
45 ccm_generic_setiv,
46 ccm_generic_setaad,
47 ccm_generic_auth_encrypt,
48 ccm_generic_auth_decrypt,
49 ccm_generic_gettag
50};
51
52#if defined(S390X_aes_128_CAPABLE)
53# include "cipher_aes_ccm_hw_s390x.inc"
54#elif defined(AESNI_CAPABLE)
55# include "cipher_aes_ccm_hw_aesni.inc"
56#elif defined(SPARC_AES_CAPABLE)
57# include "cipher_aes_ccm_hw_t4.inc"
58#else
59const PROV_CCM_HW *PROV_AES_HW_ccm(size_t keybits)
60{
61 return &aes_ccm;
62}
63#endif
64