• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifdef HKS_CONFIG_FILE
17 #include HKS_CONFIG_FILE
18 #else
19 #include "hks_config.h"
20 #endif
21 
22 #ifdef HKS_SUPPORT_HMAC_C
23 
24 #ifdef HUKS_LOG_MINI_EXT_ENABLED
25 #include "log.h"
26 #endif
27 
28 #include "hks_mbedtls_hmac.h"
29 
30 #include <mbedtls/md.h>
31 #include <securec.h>
32 
33 #include "hks_common_check.h"
34 #include "hks_log.h"
35 #include "hks_mbedtls_common.h"
36 #include "hks_mem.h"
37 #include "hks_template.h"
38 
39 struct HksMbedtlsHmacCtx {
40     uint32_t digestAlg;
41     void    *append;
42 } HksMbedtlsHmacCtx;
43 
44 #ifdef HKS_SUPPORT_HMAC_GENERATE_KEY
HksMbedtlsHmacGenerateKey(const struct HksKeySpec * spec,struct HksBlob * key)45 int32_t HksMbedtlsHmacGenerateKey(const struct HksKeySpec *spec, struct HksBlob *key)
46 {
47     if ((spec->keyLen == 0) || (spec->keyLen % HKS_BITS_PER_BYTE != 0)) {
48         return HKS_ERROR_INVALID_ARGUMENT;
49     }
50 
51     const uint32_t keyByteLen = spec->keyLen / HKS_BITS_PER_BYTE;
52 
53     uint8_t *outKey = (uint8_t *)HksMalloc(keyByteLen);
54     HKS_IF_NULL_RETURN(outKey, HKS_ERROR_MALLOC_FAIL)
55 
56     mbedtls_entropy_context entropy;
57     mbedtls_ctr_drbg_context ctrDrbg;
58     (void)memset_s(&entropy, sizeof(mbedtls_entropy_context), 0, sizeof(mbedtls_entropy_context));
59     (void)memset_s(&ctrDrbg, sizeof(mbedtls_ctr_drbg_context), 0, sizeof(mbedtls_ctr_drbg_context));
60     int32_t ret = HksCtrDrbgSeed(&ctrDrbg, &entropy);
61     if (ret != HKS_SUCCESS) {
62         HKS_FREE(outKey);
63         return ret;
64     }
65 
66     do {
67         ret = mbedtls_ctr_drbg_random(&ctrDrbg, outKey, keyByteLen);
68         if (ret != HKS_MBEDTLS_SUCCESS) {
69             HKS_LOG_E("Mbedtls ctr drbg random failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
70 #ifdef HUKS_LOG_MINI_EXT_ENABLED
71             HILOG_ERROR(HILOG_MODULE_SCY, "Mbedtls ctr drbg random failed! mbedtls ret = 0x%{public}X", ret);
72 #endif
73             (void)memset_s(outKey, keyByteLen, 0, keyByteLen);
74             HKS_FREE(outKey);
75             break;
76         }
77 
78         key->data = outKey;
79         key->size = keyByteLen;
80     } while (0);
81 
82     mbedtls_ctr_drbg_free(&ctrDrbg);
83     mbedtls_entropy_free(&entropy);
84     return ret;
85 }
86 #endif /* HKS_SUPPORT_HMAC_GENERATE_KEY */
87 
HksMbedtlsHmac(const struct HksBlob * key,uint32_t digestAlg,const struct HksBlob * msg,struct HksBlob * mac)88 int32_t HksMbedtlsHmac(const struct HksBlob *key,
89     uint32_t digestAlg, const struct HksBlob *msg, struct HksBlob *mac)
90 {
91     /* input params have been checked */
92     uint32_t mbedtlsAlg;
93     int32_t ret = HksToMbedtlsDigestAlg(digestAlg, &mbedtlsAlg);
94     HKS_IF_NOT_SUCC_RETURN(ret, ret)
95 
96     ret = mbedtls_md_hmac(mbedtls_md_info_from_type((mbedtls_md_type_t)mbedtlsAlg),
97         key->data, key->size, msg->data, msg->size, mac->data);
98     if (ret != HKS_MBEDTLS_SUCCESS) {
99         HKS_LOG_E("Mbedtls hmac failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
100 #ifdef HUKS_LOG_MINI_EXT_ENABLED
101         HILOG_ERROR(HILOG_MODULE_SCY, "Mbedtls hmac failed! mbedtls ret = 0x%{public}X", ret);
102 #endif
103         (void)memset_s(mac->data, mac->size, 0, mac->size);
104         return ret;
105     }
106 
107     ret = HksGetDigestLen(digestAlg, &(mac->size));
108     if (ret != HKS_SUCCESS) {
109         HKS_LOG_E("Get digest len failed!");
110         (void)memset_s(mac->data, mac->size, 0, mac->size);
111     }
112 
113     return ret;
114 }
115 
HksMbedtlsHmacInit(void ** cryptoCtx,const struct HksBlob * key,uint32_t digestAlg)116 int32_t HksMbedtlsHmacInit(void **cryptoCtx, const struct HksBlob *key, uint32_t digestAlg)
117 {
118     /* input params have been checked */
119     uint32_t mbedtlsAlg;
120     int32_t ret = HksToMbedtlsDigestAlg(digestAlg, &mbedtlsAlg);
121     HKS_IF_NOT_SUCC_RETURN(ret, ret)
122 
123     if (mbedtls_md_info_from_type((mbedtls_md_type_t)mbedtlsAlg) == NULL) {
124         HKS_LOG_E("Mbedtls hmac engine info failed!");
125         return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
126     }
127 
128     mbedtls_md_context_t *hmacCtx = (mbedtls_md_context_t *)HksMalloc(sizeof(mbedtls_md_context_t));
129     HKS_IF_NULL_LOGE_RETURN(hmacCtx, HKS_ERROR_MALLOC_FAIL, "Mbedtls hmac init hmacCtx malloc fail!")
130 
131     mbedtls_md_init(hmacCtx);
132 
133     ret = mbedtls_md_setup(hmacCtx, mbedtls_md_info_from_type((mbedtls_md_type_t)mbedtlsAlg), 1);
134     if (ret != HKS_MBEDTLS_SUCCESS) {
135         HKS_LOG_E("Mbedtls hmac setup failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
136         mbedtls_md_free(hmacCtx);
137         HKS_FREE(hmacCtx);
138         return ret;
139     }
140 
141     ret = mbedtls_md_hmac_starts(hmacCtx, key->data, key->size);
142     if (ret != HKS_MBEDTLS_SUCCESS) {
143         HKS_LOG_E("Mbedtls hmac start failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
144         mbedtls_md_free(hmacCtx);
145         HKS_FREE(hmacCtx);
146         return ret;
147     }
148 
149     struct HksMbedtlsHmacCtx *outCtx = (struct HksMbedtlsHmacCtx *)HksMalloc(sizeof(struct HksMbedtlsHmacCtx));
150     if (outCtx == NULL) {
151         HKS_LOG_E("Mbedtls hmac start failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
152         mbedtls_md_free(hmacCtx);
153         HKS_FREE(hmacCtx);
154         return HKS_ERROR_MALLOC_FAIL;
155     }
156 
157     outCtx->digestAlg = digestAlg;
158     outCtx->append = (void *)hmacCtx;
159     *cryptoCtx = (void *)outCtx;
160     return HKS_SUCCESS;
161 }
162 
HksMbedtlsHmacUpdate(void * cryptoCtx,const struct HksBlob * msg)163 int32_t HksMbedtlsHmacUpdate(void *cryptoCtx, const struct HksBlob *msg)
164 {
165     struct HksMbedtlsHmacCtx *hctx = (struct HksMbedtlsHmacCtx *)cryptoCtx;
166     mbedtls_md_context_t *hmacCtx = (mbedtls_md_context_t *)hctx->append;
167     HKS_IF_NULL_LOGE_RETURN(hmacCtx, HKS_ERROR_MALLOC_FAIL, "Mbedtls hmac update hmacCtx is null!")
168 
169     int32_t ret = mbedtls_md_hmac_update(hmacCtx, msg->data, msg->size);
170     if (ret != HKS_MBEDTLS_SUCCESS) {
171         HKS_LOG_E("Mbedtls hmac start failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
172         return ret;
173     }
174 
175     return HKS_SUCCESS;
176 }
177 
HksMbedtlsHmacFinal(void ** cryptoCtx,struct HksBlob * msg,struct HksBlob * mac)178 int32_t HksMbedtlsHmacFinal(void **cryptoCtx, struct HksBlob *msg, struct HksBlob *mac)
179 {
180     struct HksMbedtlsHmacCtx *hctx = (struct HksMbedtlsHmacCtx *)*cryptoCtx;
181     mbedtls_md_context_t *hmacCtx = (mbedtls_md_context_t *)hctx->append;
182     if (hmacCtx == NULL) {
183         HKS_FREE(*cryptoCtx);
184         return HKS_ERROR_NULL_POINTER;
185     }
186 
187     int32_t ret;
188     if (msg->size != 0) {
189         ret = mbedtls_md_hmac_update(hmacCtx, msg->data, msg->size);
190         if (ret != HKS_MBEDTLS_SUCCESS) {
191             HKS_LOG_E("Mbedtls hmac start failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
192             HksMbedtlsHmacHalFreeCtx(cryptoCtx);
193             return ret;
194         }
195     }
196 
197     ret = mbedtls_md_hmac_finish(hmacCtx, mac->data);
198     if (ret != HKS_MBEDTLS_SUCCESS) {
199         HKS_LOG_E("Mbedtls hmac finish failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
200         (void)memset_s(mac->data, mac->size, 0, mac->size);
201         HksMbedtlsHmacHalFreeCtx(cryptoCtx);
202         return ret;
203     }
204 
205     ret = HksGetDigestLen(hctx->digestAlg, &(mac->size));
206     if (ret != HKS_SUCCESS) {
207         HKS_LOG_E("Get digest len failed!");
208         HksMbedtlsHmacHalFreeCtx(cryptoCtx);
209         return ret;
210     }
211 
212     HksMbedtlsHmacHalFreeCtx(cryptoCtx);
213     return HKS_SUCCESS;
214 }
215 
HksMbedtlsHmacHalFreeCtx(void ** cryptoCtx)216 void HksMbedtlsHmacHalFreeCtx(void **cryptoCtx)
217 {
218     if (cryptoCtx == NULL || *cryptoCtx == NULL) {
219         HKS_LOG_E("Mbedtls hmac free ctx is null");
220         return;
221     }
222 
223     struct HksMbedtlsHmacCtx *hctx = (struct HksMbedtlsHmacCtx *)*cryptoCtx;
224     if (hctx->append != NULL) {
225         mbedtls_md_free((mbedtls_md_context_t *)hctx->append);
226         HKS_FREE(hctx->append);
227     }
228     HKS_FREE(*cryptoCtx);
229 }
230 #endif /* HKS_SUPPORT_HMAC_C */
231