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