xref: /freebsd/crypto/openssl/providers/implementations/ciphers/cipher_sm4_ccm.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1*e7be843bSPierre Pronchery /*
2*e7be843bSPierre Pronchery  * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.
3*e7be843bSPierre Pronchery  *
4*e7be843bSPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e7be843bSPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*e7be843bSPierre Pronchery  * in the file LICENSE in the source distribution or at
7*e7be843bSPierre Pronchery  * https://www.openssl.org/source/license.html
8*e7be843bSPierre Pronchery  */
9*e7be843bSPierre Pronchery 
10*e7be843bSPierre Pronchery /* Dispatch functions for SM4 CCM mode */
11*e7be843bSPierre Pronchery 
12*e7be843bSPierre Pronchery #include "cipher_sm4_ccm.h"
13*e7be843bSPierre Pronchery #include "prov/implementations.h"
14*e7be843bSPierre Pronchery #include "prov/providercommon.h"
15*e7be843bSPierre Pronchery 
16*e7be843bSPierre Pronchery static OSSL_FUNC_cipher_freectx_fn sm4_ccm_freectx;
17*e7be843bSPierre Pronchery 
sm4_ccm_newctx(void * provctx,size_t keybits)18*e7be843bSPierre Pronchery static void *sm4_ccm_newctx(void *provctx, size_t keybits)
19*e7be843bSPierre Pronchery {
20*e7be843bSPierre Pronchery     PROV_SM4_CCM_CTX *ctx;
21*e7be843bSPierre Pronchery 
22*e7be843bSPierre Pronchery     if (!ossl_prov_is_running())
23*e7be843bSPierre Pronchery         return NULL;
24*e7be843bSPierre Pronchery 
25*e7be843bSPierre Pronchery     ctx = OPENSSL_zalloc(sizeof(*ctx));
26*e7be843bSPierre Pronchery     if (ctx != NULL)
27*e7be843bSPierre Pronchery         ossl_ccm_initctx(&ctx->base, keybits, ossl_prov_sm4_hw_ccm(keybits));
28*e7be843bSPierre Pronchery     return ctx;
29*e7be843bSPierre Pronchery }
30*e7be843bSPierre Pronchery 
sm4_ccm_dupctx(void * provctx)31*e7be843bSPierre Pronchery static void *sm4_ccm_dupctx(void *provctx)
32*e7be843bSPierre Pronchery {
33*e7be843bSPierre Pronchery     PROV_SM4_CCM_CTX *ctx = provctx;
34*e7be843bSPierre Pronchery     PROV_SM4_CCM_CTX *dctx = NULL;
35*e7be843bSPierre Pronchery 
36*e7be843bSPierre Pronchery     if (ctx == NULL)
37*e7be843bSPierre Pronchery         return NULL;
38*e7be843bSPierre Pronchery 
39*e7be843bSPierre Pronchery     dctx = OPENSSL_memdup(ctx, sizeof(*ctx));
40*e7be843bSPierre Pronchery     if (dctx != NULL && dctx->base.ccm_ctx.key != NULL)
41*e7be843bSPierre Pronchery         dctx->base.ccm_ctx.key = &dctx->ks.ks;
42*e7be843bSPierre Pronchery 
43*e7be843bSPierre Pronchery     return dctx;
44*e7be843bSPierre Pronchery }
45*e7be843bSPierre Pronchery 
sm4_ccm_freectx(void * vctx)46*e7be843bSPierre Pronchery static void sm4_ccm_freectx(void *vctx)
47*e7be843bSPierre Pronchery {
48*e7be843bSPierre Pronchery     PROV_SM4_CCM_CTX *ctx = (PROV_SM4_CCM_CTX *)vctx;
49*e7be843bSPierre Pronchery 
50*e7be843bSPierre Pronchery     OPENSSL_clear_free(ctx,  sizeof(*ctx));
51*e7be843bSPierre Pronchery }
52*e7be843bSPierre Pronchery 
53*e7be843bSPierre Pronchery /* sm4128ccm functions */
54*e7be843bSPierre Pronchery IMPLEMENT_aead_cipher(sm4, ccm, CCM, AEAD_FLAGS, 128, 8, 96);
55