• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #include "hitls_build.h"
17 #ifdef HITLS_CRYPTO_GMAC
18 
19 #include <stdlib.h>
20 #include "crypt_gmac.h"
21 #include "crypt_errno.h"
22 #include "bsl_err_internal.h"
23 
GmacIdToSymId(CRYPT_MAC_AlgId algId)24 static int32_t GmacIdToSymId(CRYPT_MAC_AlgId algId)
25 {
26     switch (algId) {
27         case CRYPT_MAC_GMAC_AES128:
28             return CRYPT_CIPHER_AES128_GCM;
29         case CRYPT_MAC_GMAC_AES192:
30             return CRYPT_CIPHER_AES192_GCM;
31         case CRYPT_MAC_GMAC_AES256:
32             return CRYPT_CIPHER_AES256_GCM;
33         default:
34             return CRYPT_CIPHER_MAX;
35     }
36 }
37 
CRYPT_GMAC_NewCtx(CRYPT_MAC_AlgId id)38 MODES_GCM_Ctx *CRYPT_GMAC_NewCtx(CRYPT_MAC_AlgId id)
39 {
40     return MODES_GCM_NewCtx(GmacIdToSymId(id));
41 }
42 
CRYPT_GMAC_Init(MODES_GCM_Ctx * ctx,const uint8_t * key,uint32_t len,void * param)43 int32_t CRYPT_GMAC_Init(MODES_GCM_Ctx *ctx, const uint8_t *key, uint32_t len, void *param)
44 {
45     (void)param;
46     return MODES_GCM_SetKey(&ctx->gcmCtx, key, len);
47 }
48 
CRYPT_GMAC_Update(MODES_GCM_Ctx * ctx,const uint8_t * in,uint32_t len)49 int32_t CRYPT_GMAC_Update(MODES_GCM_Ctx *ctx, const uint8_t *in, uint32_t len)
50 {
51     return MODES_GCM_Ctrl(ctx, CRYPT_CTRL_SET_AAD, (void *)(uintptr_t)in, len);
52 }
53 
CRYPT_GMAC_Final(MODES_GCM_Ctx * ctx,uint8_t * out,uint32_t * len)54 int32_t CRYPT_GMAC_Final(MODES_GCM_Ctx *ctx, uint8_t *out, uint32_t *len)
55 {
56     if (len == NULL) {
57         BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
58         return CRYPT_NULL_INPUT;
59     }
60     return MODES_GCM_Ctrl(ctx, CRYPT_CTRL_GET_TAG, (void *)(uintptr_t)out, *len);
61 }
62 
CRYPT_GMAC_FreeCtx(MODES_GCM_Ctx * ctx)63 void CRYPT_GMAC_FreeCtx(MODES_GCM_Ctx *ctx)
64 {
65     MODES_GCM_FreeCtx(ctx);
66 }
67 
CRYPT_GMAC_Deinit(MODES_GCM_Ctx * ctx)68 void CRYPT_GMAC_Deinit(MODES_GCM_Ctx *ctx)
69 {
70     (void)MODES_GCM_DeInitCtx(ctx);
71 }
72 
CRYPT_GMAC_Ctrl(MODES_GCM_Ctx * ctx,int32_t opt,void * val,uint32_t len)73 int32_t CRYPT_GMAC_Ctrl(MODES_GCM_Ctx *ctx, int32_t opt, void *val, uint32_t len)
74 {
75     switch (opt) {
76         case CRYPT_CTRL_SET_IV:
77             return MODES_GCM_Ctrl(ctx, CRYPT_CTRL_REINIT_STATUS, val, len);
78         case CRYPT_CTRL_GET_MACLEN:
79             return MODES_GCM_Ctrl(ctx, CRYPT_CTRL_GET_BLOCKSIZE, val, len);
80         case CRYPT_CTRL_SET_TAGLEN:
81             return MODES_GCM_Ctrl(ctx, CRYPT_CTRL_SET_TAGLEN, val, len);
82         default:
83             BSL_ERR_PUSH_ERROR(CRYPT_EAL_MAC_CTRL_TYPE_ERROR);
84             return CRYPT_EAL_MAC_CTRL_TYPE_ERROR;
85     }
86 }
87 
88 #endif /* HITLS_CRYPTO_GMAC */
89