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 
32 #include "hks_common_check.h"
33 #include "hks_log.h"
34 #include "hks_mbedtls_common.h"
35 #include "hks_mem.h"
36 #include "hks_template.h"
37 
38 struct HksMbedtlsHmacCtx {
39     uint32_t digestAlg;
40     void    *append;
41 } HksMbedtlsHmacCtx;
42 
43 #ifdef HKS_SUPPORT_HMAC_GENERATE_KEY
HksMbedtlsHmacGenerateKey(const struct HksKeySpec * spec,struct HksBlob * key)44 int32_t HksMbedtlsHmacGenerateKey(const struct HksKeySpec *spec, struct HksBlob *key)
45 {
46     if ((spec->keyLen == 0) || (spec->keyLen % HKS_BITS_PER_BYTE != 0)) {
47         return HKS_ERROR_INVALID_ARGUMENT;
48     }
49 
50     const uint32_t keyByteLen = spec->keyLen / HKS_BITS_PER_BYTE;
51 
52     uint8_t *outKey = (uint8_t *)HksMalloc(keyByteLen);
53     HKS_IF_NULL_RETURN(outKey, HKS_ERROR_MALLOC_FAIL)
54 
55     mbedtls_entropy_context entropy;
56     mbedtls_ctr_drbg_context ctrDrbg;
57     (void)memset_s(&entropy, sizeof(mbedtls_entropy_context), 0, sizeof(mbedtls_entropy_context));
58     (void)memset_s(&ctrDrbg, sizeof(mbedtls_ctr_drbg_context), 0, sizeof(mbedtls_ctr_drbg_context));
59     int32_t ret = HksCtrDrbgSeed(&ctrDrbg, &entropy);
60     if (ret != HKS_SUCCESS) {
61         HKS_FREE(outKey);
62         return ret;
63     }
64 
65     do {
66         ret = mbedtls_ctr_drbg_random(&ctrDrbg, outKey, keyByteLen);
67         if (ret != HKS_MBEDTLS_SUCCESS) {
68             HKS_LOG_E("Mbedtls ctr drbg random failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
69 #ifdef HUKS_LOG_MINI_EXT_ENABLED
70             HILOG_ERROR(HILOG_MODULE_SCY, "Mbedtls ctr drbg random failed! mbedtls ret = 0x%{public}X", ret);
71 #endif
72             (void)memset_s(outKey, keyByteLen, 0, keyByteLen);
73             HKS_FREE(outKey);
74             break;
75         }
76 
77         key->data = outKey;
78         key->size = keyByteLen;
79     } while (0);
80 
81     mbedtls_ctr_drbg_free(&ctrDrbg);
82     mbedtls_entropy_free(&entropy);
83     return ret;
84 }
85 #endif /* HKS_SUPPORT_HMAC_GENERATE_KEY */
86 
HksMbedtlsHmac(const struct HksBlob * key,uint32_t digestAlg,const struct HksBlob * msg,struct HksBlob * mac)87 int32_t HksMbedtlsHmac(const struct HksBlob *key,
88     uint32_t digestAlg, const struct HksBlob *msg, struct HksBlob *mac)
89 {
90     /* input params have been checked */
91     uint32_t mbedtlsAlg;
92     int32_t ret = HksToMbedtlsDigestAlg(digestAlg, &mbedtlsAlg);
93     HKS_IF_NOT_SUCC_RETURN(ret, ret)
94 
95     ret = mbedtls_md_hmac(mbedtls_md_info_from_type((mbedtls_md_type_t)mbedtlsAlg),
96         key->data, key->size, msg->data, msg->size, mac->data);
97     if (ret != HKS_MBEDTLS_SUCCESS) {
98         HKS_LOG_E("Mbedtls hmac failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
99 #ifdef HUKS_LOG_MINI_EXT_ENABLED
100         HILOG_ERROR(HILOG_MODULE_SCY, "Mbedtls hmac failed! mbedtls ret = 0x%{public}X", ret);
101 #endif
102         (void)memset_s(mac->data, mac->size, 0, mac->size);
103         return ret;
104     }
105 
106     ret = HksGetDigestLen(digestAlg, &(mac->size));
107     if (ret != HKS_SUCCESS) {
108         HKS_LOG_E("Get digest len failed!");
109         (void)memset_s(mac->data, mac->size, 0, mac->size);
110     }
111 
112     return ret;
113 }
114 
HksMbedtlsHmacInit(void ** cryptoCtx,const struct HksBlob * key,uint32_t digestAlg)115 int32_t HksMbedtlsHmacInit(void **cryptoCtx, const struct HksBlob *key, uint32_t digestAlg)
116 {
117     /* input params have been checked */
118     uint32_t mbedtlsAlg;
119     int32_t ret = HksToMbedtlsDigestAlg(digestAlg, &mbedtlsAlg);
120     HKS_IF_NOT_SUCC_RETURN(ret, ret)
121 
122     if (mbedtls_md_info_from_type((mbedtls_md_type_t)mbedtlsAlg) == NULL) {
123         HKS_LOG_E("Mbedtls hmac engine info failed!");
124         return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
125     }
126 
127     mbedtls_md_context_t *hmacCtx = (mbedtls_md_context_t *)HksMalloc(sizeof(mbedtls_md_context_t));
128     HKS_IF_NULL_LOGE_RETURN(hmacCtx, HKS_ERROR_MALLOC_FAIL, "Mbedtls hmac init hmacCtx malloc fail!")
129 
130     mbedtls_md_init(hmacCtx);
131 
132     ret = mbedtls_md_setup(hmacCtx, mbedtls_md_info_from_type((mbedtls_md_type_t)mbedtlsAlg), 1);
133     if (ret != HKS_MBEDTLS_SUCCESS) {
134         HKS_LOG_E("Mbedtls hmac setup failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
135         mbedtls_md_free(hmacCtx);
136         HKS_FREE(hmacCtx);
137         return ret;
138     }
139 
140     ret = mbedtls_md_hmac_starts(hmacCtx, key->data, key->size);
141     if (ret != HKS_MBEDTLS_SUCCESS) {
142         HKS_LOG_E("Mbedtls hmac start failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
143         mbedtls_md_free(hmacCtx);
144         HKS_FREE(hmacCtx);
145         return ret;
146     }
147 
148     struct HksMbedtlsHmacCtx *outCtx = (struct HksMbedtlsHmacCtx *)HksMalloc(sizeof(struct HksMbedtlsHmacCtx));
149     if (outCtx == NULL) {
150         HKS_LOG_E("Mbedtls hmac start failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
151         mbedtls_md_free(hmacCtx);
152         HKS_FREE(hmacCtx);
153         return HKS_ERROR_MALLOC_FAIL;
154     }
155 
156     outCtx->digestAlg = digestAlg;
157     outCtx->append = (void *)hmacCtx;
158     *cryptoCtx = (void *)outCtx;
159     return HKS_SUCCESS;
160 }
161 
HksMbedtlsHmacUpdate(void * cryptoCtx,const struct HksBlob * msg)162 int32_t HksMbedtlsHmacUpdate(void *cryptoCtx, const struct HksBlob *msg)
163 {
164     struct HksMbedtlsHmacCtx *hctx = (struct HksMbedtlsHmacCtx *)cryptoCtx;
165     mbedtls_md_context_t *hmacCtx = (mbedtls_md_context_t *)hctx->append;
166     HKS_IF_NULL_LOGE_RETURN(hmacCtx, HKS_ERROR_MALLOC_FAIL, "Mbedtls hmac update hmacCtx is null!")
167 
168     int32_t ret = mbedtls_md_hmac_update(hmacCtx, msg->data, msg->size);
169     if (ret != HKS_MBEDTLS_SUCCESS) {
170         HKS_LOG_E("Mbedtls hmac start failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
171         return ret;
172     }
173 
174     return HKS_SUCCESS;
175 }
176 
HksMbedtlsHmacFinal(void ** cryptoCtx,struct HksBlob * msg,struct HksBlob * mac)177 int32_t HksMbedtlsHmacFinal(void **cryptoCtx, struct HksBlob *msg, struct HksBlob *mac)
178 {
179     struct HksMbedtlsHmacCtx *hctx = (struct HksMbedtlsHmacCtx *)*cryptoCtx;
180     mbedtls_md_context_t *hmacCtx = (mbedtls_md_context_t *)hctx->append;
181     if (hmacCtx == NULL) {
182         HKS_FREE(*cryptoCtx);
183         return HKS_ERROR_NULL_POINTER;
184     }
185 
186     int32_t ret;
187     if (msg->size != 0) {
188         ret = mbedtls_md_hmac_update(hmacCtx, msg->data, msg->size);
189         if (ret != HKS_MBEDTLS_SUCCESS) {
190             HKS_LOG_E("Mbedtls hmac start failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
191             HksMbedtlsHmacHalFreeCtx(cryptoCtx);
192             return ret;
193         }
194     }
195 
196     ret = mbedtls_md_hmac_finish(hmacCtx, mac->data);
197     if (ret != HKS_MBEDTLS_SUCCESS) {
198         HKS_LOG_E("Mbedtls hmac finish failed! mbedtls ret = 0x%" LOG_PUBLIC "X", ret);
199         (void)memset_s(mac->data, mac->size, 0, mac->size);
200         HksMbedtlsHmacHalFreeCtx(cryptoCtx);
201         return ret;
202     }
203 
204     ret = HksGetDigestLen(hctx->digestAlg, &(mac->size));
205     if (ret != HKS_SUCCESS) {
206         HKS_LOG_E("Get digest len failed!");
207         HksMbedtlsHmacHalFreeCtx(cryptoCtx);
208         return ret;
209     }
210 
211     HksMbedtlsHmacHalFreeCtx(cryptoCtx);
212     return HKS_SUCCESS;
213 }
214 
HksMbedtlsHmacHalFreeCtx(void ** cryptoCtx)215 void HksMbedtlsHmacHalFreeCtx(void **cryptoCtx)
216 {
217     if (cryptoCtx == NULL || *cryptoCtx == NULL) {
218         HKS_LOG_E("Mbedtls hmac free ctx is null");
219         return;
220     }
221 
222     struct HksMbedtlsHmacCtx *hctx = (struct HksMbedtlsHmacCtx *)*cryptoCtx;
223     if (hctx->append != NULL) {
224         mbedtls_md_free((mbedtls_md_context_t *)hctx->append);
225         HKS_FREE(hctx->append);
226     }
227     HKS_FREE(*cryptoCtx);
228 }
229 #endif /* HKS_SUPPORT_HMAC_C */
230