1 /*
2  * Copyright 2021 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 /*-
11  * Generic support for SM4 CCM.
12  */
13 
14 #include "cipher_sm4_ccm.h"
15 
ccm_sm4_initkey(PROV_CCM_CTX * ctx,const unsigned char * key,size_t keylen)16 static int ccm_sm4_initkey(PROV_CCM_CTX *ctx,
17                            const unsigned char *key, size_t keylen)
18 {
19     PROV_SM4_CCM_CTX *actx = (PROV_SM4_CCM_CTX *)ctx;
20 
21     ossl_sm4_set_key(key, &actx->ks.ks);
22     CRYPTO_ccm128_init(&ctx->ccm_ctx, ctx->m, ctx->l, &actx->ks.ks,
23                        (block128_f)ossl_sm4_encrypt);
24     ctx->str = NULL;
25     ctx->key_set = 1;
26     return 1;
27 }
28 
29 static const PROV_CCM_HW ccm_sm4 = {
30     ccm_sm4_initkey,
31     ossl_ccm_generic_setiv,
32     ossl_ccm_generic_setaad,
33     ossl_ccm_generic_auth_encrypt,
34     ossl_ccm_generic_auth_decrypt,
35     ossl_ccm_generic_gettag
36 };
37 
ossl_prov_sm4_hw_ccm(size_t keybits)38 const PROV_CCM_HW *ossl_prov_sm4_hw_ccm(size_t keybits)
39 {
40     return &ccm_sm4;
41 }
42