xref: /freebsd/crypto/openssl/providers/implementations/ciphers/cipher_aes_ccm.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1b077aed3SPierre Pronchery /*
2*e0c4386eSCy Schubert  * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery  *
4b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery  */
9b077aed3SPierre Pronchery 
10b077aed3SPierre Pronchery /*
11b077aed3SPierre Pronchery  * AES low level APIs are deprecated for public use, but still ok for internal
12b077aed3SPierre Pronchery  * use where we're using them to implement the higher level EVP interface, as is
13b077aed3SPierre Pronchery  * the case here.
14b077aed3SPierre Pronchery  */
15b077aed3SPierre Pronchery #include "internal/deprecated.h"
16b077aed3SPierre Pronchery 
17b077aed3SPierre Pronchery /* Dispatch functions for AES CCM mode */
18b077aed3SPierre Pronchery 
19b077aed3SPierre Pronchery #include "cipher_aes_ccm.h"
20b077aed3SPierre Pronchery #include "prov/implementations.h"
21b077aed3SPierre Pronchery #include "prov/providercommon.h"
22b077aed3SPierre Pronchery 
aes_ccm_newctx(void * provctx,size_t keybits)23b077aed3SPierre Pronchery static void *aes_ccm_newctx(void *provctx, size_t keybits)
24b077aed3SPierre Pronchery {
25b077aed3SPierre Pronchery     PROV_AES_CCM_CTX *ctx;
26b077aed3SPierre Pronchery 
27b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
28b077aed3SPierre Pronchery         return NULL;
29b077aed3SPierre Pronchery 
30b077aed3SPierre Pronchery     ctx = OPENSSL_zalloc(sizeof(*ctx));
31b077aed3SPierre Pronchery     if (ctx != NULL)
32b077aed3SPierre Pronchery         ossl_ccm_initctx(&ctx->base, keybits, ossl_prov_aes_hw_ccm(keybits));
33b077aed3SPierre Pronchery     return ctx;
34b077aed3SPierre Pronchery }
35b077aed3SPierre Pronchery 
aes_ccm_dupctx(void * provctx)36*e0c4386eSCy Schubert static void *aes_ccm_dupctx(void *provctx)
37*e0c4386eSCy Schubert {
38*e0c4386eSCy Schubert     PROV_AES_CCM_CTX *ctx = provctx;
39*e0c4386eSCy Schubert     PROV_AES_CCM_CTX *dupctx = NULL;
40*e0c4386eSCy Schubert 
41*e0c4386eSCy Schubert     if (ctx == NULL)
42*e0c4386eSCy Schubert         return NULL;
43*e0c4386eSCy Schubert     dupctx = OPENSSL_memdup(provctx, sizeof(*ctx));
44*e0c4386eSCy Schubert     if (dupctx == NULL)
45*e0c4386eSCy Schubert         return NULL;
46*e0c4386eSCy Schubert     /*
47*e0c4386eSCy Schubert      * ossl_cm_initctx, via the ossl_prov_aes_hw_ccm functions assign a
48*e0c4386eSCy Schubert      * provctx->ccm.ks.ks to the ccm context key so we need to point it to
49*e0c4386eSCy Schubert      * the memduped copy
50*e0c4386eSCy Schubert      */
51*e0c4386eSCy Schubert     dupctx->base.ccm_ctx.key = &dupctx->ccm.ks.ks;
52*e0c4386eSCy Schubert 
53*e0c4386eSCy Schubert     return dupctx;
54*e0c4386eSCy Schubert }
55*e0c4386eSCy Schubert 
56b077aed3SPierre Pronchery static OSSL_FUNC_cipher_freectx_fn aes_ccm_freectx;
aes_ccm_freectx(void * vctx)57b077aed3SPierre Pronchery static void aes_ccm_freectx(void *vctx)
58b077aed3SPierre Pronchery {
59b077aed3SPierre Pronchery     PROV_AES_CCM_CTX *ctx = (PROV_AES_CCM_CTX *)vctx;
60b077aed3SPierre Pronchery 
61b077aed3SPierre Pronchery     OPENSSL_clear_free(ctx,  sizeof(*ctx));
62b077aed3SPierre Pronchery }
63b077aed3SPierre Pronchery 
64b077aed3SPierre Pronchery /* ossl_aes128ccm_functions */
65b077aed3SPierre Pronchery IMPLEMENT_aead_cipher(aes, ccm, CCM, AEAD_FLAGS, 128, 8, 96);
66b077aed3SPierre Pronchery /* ossl_aes192ccm_functions */
67b077aed3SPierre Pronchery IMPLEMENT_aead_cipher(aes, ccm, CCM, AEAD_FLAGS, 192, 8, 96);
68b077aed3SPierre Pronchery /* ossl_aes256ccm_functions */
69b077aed3SPierre Pronchery IMPLEMENT_aead_cipher(aes, ccm, CCM, AEAD_FLAGS, 256, 8, 96);
70