• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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  * Description: cipher hash driver. \n
16  *
17  * History: \n
18  * 2023-03-22, Create file. \n
19  */
20 #include "drv_hash.h"
21 #include "drv_inner.h"
22 
23 #include "hal_hash.h"
24 
25 #include "crypto_drv_common.h"
26 
27 #define CRYPTO_INVALID_CHN_NUM          (0xFFFFFFFF)
28 #define HASH_COMPAT_ERRNO(err_code)     DRV_COMPAT_ERRNO(ERROR_MODULE_HASH, err_code)
29 #define hash_null_ptr_chk(ptr)   \
30     crypto_chk_return((ptr) == TD_NULL, HASH_COMPAT_ERRNO(ERROR_PARAM_IS_NULL), #ptr" is NULL\n")
31 
32 #define CRYPTO_DRV_HASH_MAX_DMA_SIZE    (CRYPTO_HASH_DRV_BUFFER_SIZE * CRYPTO_HASH_HARD_CHN_CNT)
33 static td_bool g_drv_hash_init = TD_FALSE;
34 
35 #if defined(CRYPTO_CONFIG_ROMBOOT_ENV)
36 static td_u8 g_drv_dma_buf[CRYPTO_DRV_HASH_MAX_DMA_SIZE];
37 #else
38 static td_u8 *g_drv_dma_buf = TD_NULL;
39 #endif
40 
41 typedef struct {
42     td_u32 length[2];
43     td_u32 state[CRYPTO_HASH_RESULT_SIZE_MAX_IN_WORD];
44 #if defined(CRYPTO_SOFT_HMAC_SUPPORT)
45     td_u8 o_key_pad[CRYPTO_HASH_BLOCK_SIZE_MAX];
46     td_u8 i_key_pad[CRYPTO_HASH_BLOCK_SIZE_MAX];
47 #endif
48     td_u8 tail[CRYPTO_HASH_BLOCK_SIZE_MAX];
49     td_u32 tail_len;
50     td_u8 *data_buffer;
51     td_u32 data_buffer_len;
52 } crypto_hash_ctx;
53 
54 typedef struct {
55     crypto_hash_type hash_type;
56     crypto_hash_ctx hash_ctx;
57     td_u32 open         : 1;
58     td_u32 is_keyslot   : 1;
59 } hal_hash_channel_context;
60 
61 typedef struct {
62     crypto_hash_type hash_type;
63     const td_u8 *g_state;
64     td_u32 val_size;
65 } hash_state_t;
66 
67 static hal_hash_channel_context g_hash_channel_ctx_list[CRYPTO_HASH_HARD_CHN_CNT];
68 
69 // SHA-1, the initial hash value
70 static const td_u8 g_sha1_ival[20] = {
71     0x67, 0x45, 0x23, 0x01,
72     0xEF, 0xCD, 0xAB, 0x89,
73     0x98, 0xBA, 0xDC, 0xFE,
74     0x10, 0x32, 0x54, 0x76,
75     0xC3, 0xD2, 0xE1, 0xF0
76 };
77 
78 // SHA-224, the initial hash value
79 static const td_u8 g_sha224_ival[32] = {
80     0xC1, 0x05, 0x9E, 0xD8,
81     0x36, 0x7C, 0xD5, 0x07,
82     0x30, 0x70, 0xDD, 0x17,
83     0xF7, 0x0E, 0x59, 0x39,
84     0xFF, 0xC0, 0x0B, 0x31,
85     0x68, 0x58, 0x15, 0x11,
86     0x64, 0xF9, 0x8F, 0xA7,
87     0xBE, 0xFA, 0x4F, 0xA4
88 };
89 
90 // SHA-256, the initial hash value
91 static const td_u8 g_sha256_ival[32] = {
92     0x6A, 0x09, 0xE6, 0x67,
93     0xBB, 0x67, 0xAE, 0x85,
94     0x3C, 0x6E, 0xF3, 0x72,
95     0xA5, 0x4F, 0xF5, 0x3A,
96     0x51, 0x0E, 0x52, 0x7F,
97     0x9B, 0x05, 0x68, 0x8C,
98     0x1F, 0x83, 0xD9, 0xAB,
99     0x5B, 0xE0, 0xCD, 0x19
100 };
101 
102 // SHA-384, the initial hash value
103 static const td_u8 g_sha384_ival[64] = {
104     0xCB, 0xBB, 0x9D, 0x5D, 0xC1, 0x05, 0x9E, 0xD8,
105     0x62, 0x9A, 0x29, 0x2A, 0x36, 0x7C, 0xD5, 0x07,
106     0x91, 0x59, 0x01, 0x5A, 0x30, 0x70, 0xDD, 0x17,
107     0x15, 0x2F, 0xEC, 0xD8, 0xF7, 0x0E, 0x59, 0x39,
108     0x67, 0x33, 0x26, 0x67, 0xFF, 0xC0, 0x0B, 0x31,
109     0x8E, 0xB4, 0x4A, 0x87, 0x68, 0x58, 0x15, 0x11,
110     0xDB, 0x0C, 0x2E, 0x0D, 0x64, 0xF9, 0x8F, 0xA7,
111     0x47, 0xB5, 0x48, 0x1D, 0xBE, 0xFA, 0x4F, 0xA4
112 };
113 
114 // SHA-512, the initial hash value
115 static const td_u8 g_sha512_ival[64] = {
116     0x6A, 0x09, 0xE6, 0x67, 0xF3, 0xBC, 0xC9, 0x08,
117     0xBB, 0x67, 0xAE, 0x85, 0x84, 0xCA, 0xA7, 0x3B,
118     0x3C, 0x6E, 0xF3, 0x72, 0xFE, 0x94, 0xF8, 0x2B,
119     0xA5, 0x4F, 0xF5, 0x3A, 0x5F, 0x1D, 0x36, 0xF1,
120     0x51, 0x0E, 0x52, 0x7F, 0xAD, 0xE6, 0x82, 0xD1,
121     0x9B, 0x05, 0x68, 0x8C, 0x2B, 0x3E, 0x6C, 0x1F,
122     0x1F, 0x83, 0xD9, 0xAB, 0xFB, 0x41, 0xBD, 0x6B,
123     0x5B, 0xE0, 0xCD, 0x19, 0x13, 0x7E, 0x21, 0x79
124 };
125 
126 // SM3, the initial hash value
127 static const td_u8 g_sm3_ival[32] = {
128     0x73, 0x80, 0x16, 0x6F,
129     0x49, 0x14, 0xB2, 0xB9,
130     0x17, 0x24, 0x42, 0xD7,
131     0xDA, 0x8A, 0x06, 0x00,
132     0xA9, 0x6F, 0x30, 0xBC,
133     0x16, 0x31, 0x38, 0xAA,
134     0xE3, 0x8D, 0xEE, 0x4D,
135     0xB0, 0xFB, 0x0E, 0x4E
136 };
137 
138 static const hash_state_t g_hash_state[] = {
139     {CRYPTO_HASH_TYPE_SHA1, g_sha1_ival, sizeof(g_sha1_ival)},
140     {CRYPTO_HASH_TYPE_SHA224, g_sha224_ival, sizeof(g_sha224_ival)},
141     {CRYPTO_HASH_TYPE_SHA256, g_sha256_ival, sizeof(g_sha256_ival)},
142     {CRYPTO_HASH_TYPE_SHA384, g_sha384_ival, sizeof(g_sha384_ival)},
143     {CRYPTO_HASH_TYPE_SHA512, g_sha512_ival, sizeof(g_sha512_ival)},
144     {CRYPTO_HASH_TYPE_SM3, g_sm3_ival, sizeof(g_sm3_ival)},
145     {CRYPTO_HASH_TYPE_HMAC_SHA1, g_sha1_ival, sizeof(g_sha1_ival)},
146     {CRYPTO_HASH_TYPE_HMAC_SHA224, g_sha224_ival, sizeof(g_sha224_ival)},
147     {CRYPTO_HASH_TYPE_HMAC_SHA256, g_sha256_ival, sizeof(g_sha256_ival)},
148     {CRYPTO_HASH_TYPE_HMAC_SHA384, g_sha384_ival, sizeof(g_sha384_ival)},
149     {CRYPTO_HASH_TYPE_HMAC_SHA512, g_sha512_ival, sizeof(g_sha512_ival)},
150     {CRYPTO_HASH_TYPE_HMAC_SM3, g_sm3_ival, sizeof(g_sm3_ival)}
151 };
152 
drv_hash_get_state(crypto_hash_type hash_type,td_u32 * state,td_u32 state_size)153 static td_s32 drv_hash_get_state(crypto_hash_type hash_type, td_u32 *state, td_u32 state_size)
154 {
155     td_s32 ret = TD_SUCCESS;
156     td_u32 i;
157 
158     for (i = 0; i < sizeof(g_hash_state) / sizeof(hash_state_t); i++) {
159         if (g_hash_state[i].hash_type == hash_type) {
160             break;
161         }
162     }
163 
164     ret = memcpy_s(state, state_size, g_hash_state[i].g_state, g_hash_state[i].val_size);
165     return ret;
166 }
167 
drv_cipher_hash_init(td_void)168 td_s32 drv_cipher_hash_init(td_void)
169 {
170     td_s32 ret = TD_SUCCESS;
171 
172     crypto_drv_func_enter();
173     if (g_drv_hash_init == TD_TRUE) {
174         return TD_SUCCESS;
175     }
176 
177     ret = hal_cipher_hash_init();
178     crypto_chk_return(ret != CRYPTO_SUCCESS, ret, "hal_cipher_hash_init failed\n");
179 
180 #if !defined(CRYPTO_CONFIG_ROMBOOT_ENV)
181     g_drv_dma_buf = crypto_malloc_mmz(CRYPTO_DRV_HASH_MAX_DMA_SIZE);
182     if (g_drv_dma_buf == TD_NULL) {
183         crypto_log_err("crypto_malloc_mmz failed\n");
184         hal_cipher_hash_deinit();
185         return HASH_COMPAT_ERRNO(ERROR_MALLOC);
186     }
187 #endif
188     g_drv_hash_init = TD_TRUE;
189     crypto_drv_func_exit();
190     return ret;
191 }
192 
drv_cipher_hash_deinit(td_void)193 td_s32 drv_cipher_hash_deinit(td_void)
194 {
195     td_u32 i;
196     hal_hash_channel_context *ctx = TD_NULL;
197     crypto_drv_func_enter();
198     if (g_drv_hash_init == TD_FALSE) {
199         return TD_SUCCESS;
200     }
201 
202     for (i = 0; i < CRYPTO_HASH_HARD_CHN_CNT; i++) {
203         ctx = &g_hash_channel_ctx_list[i];
204         if (ctx->open == TD_FALSE) {
205             continue;
206         }
207         (td_void)drv_cipher_hash_destroy(i);
208     }
209 
210     if (g_drv_dma_buf != TD_NULL) {
211         (td_void)memset_s(g_drv_dma_buf, CRYPTO_DRV_HASH_MAX_DMA_SIZE, 0, CRYPTO_DRV_HASH_MAX_DMA_SIZE);
212 #if !defined(CRYPTO_CONFIG_ROMBOOT_ENV)
213         crypto_free_coherent(g_drv_dma_buf);
214         g_drv_dma_buf = TD_NULL;
215 #endif
216     }
217     hal_cipher_hash_deinit();
218     g_drv_hash_init = TD_FALSE;
219     crypto_drv_func_exit();
220     return TD_SUCCESS;
221 }
222 
drv_hash_lock_chn(td_u32 * chn_num)223 static td_s32 drv_hash_lock_chn(td_u32 *chn_num)
224 {
225     td_u32 i;
226     td_s32 ret = TD_SUCCESS;
227     *chn_num = CRYPTO_INVALID_CHN_NUM;
228     /* Lock one free Hash Hard Channel. */
229     for (i = 1; i < CRYPTO_HASH_HARD_CHN_CNT; i++) {
230         if (g_hash_channel_ctx_list[i].open == TD_TRUE) {
231             continue;
232         }
233 
234         ret = hal_hash_lock(i);
235         if (ret == TD_SUCCESS) {
236             *chn_num = i;
237             break;
238         }
239     }
240 
241     if (*chn_num == CRYPTO_INVALID_CHN_NUM) {
242         crypto_log_err("Hash Channel is Busy\n");
243         return HASH_COMPAT_ERRNO(ERROR_CHN_BUSY);
244     }
245     return TD_SUCCESS;
246 }
247 
inner_drv_hmac_start(td_u32 chn_num,hal_hash_channel_context * ctx,const crypto_hash_attr * hash_attr)248 static td_s32 inner_drv_hmac_start(td_u32 chn_num, hal_hash_channel_context *ctx, const crypto_hash_attr *hash_attr)
249 {
250 #if defined(CRYPTO_SOFT_HMAC_SUPPORT)
251     td_s32 ret = TD_SUCCESS;
252     td_u32 i;
253     td_u32 block_size = crypto_hash_get_block_size(hash_attr->hash_type) / CRYPTO_BITS_IN_BYTE;
254     if (hash_attr->key_len != 0) {
255         ret = memcpy_s(ctx->hash_ctx.tail, sizeof(ctx->hash_ctx.tail), hash_attr->key, hash_attr->key_len);
256         crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
257     }
258 
259     /* Calc o_key_pad and i_key_pad */
260     for (i = 0; i < block_size; i++) {
261         ctx->hash_ctx.o_key_pad[i] = 0x5c ^ ctx->hash_ctx.tail[i];
262         ctx->hash_ctx.i_key_pad[i] = 0x36 ^ ctx->hash_ctx.tail[i];
263     }
264     ret = memcpy_s(ctx->hash_ctx.data_buffer, ctx->hash_ctx.data_buffer_len, ctx->hash_ctx.i_key_pad, block_size);
265     crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
266 
267     ctx->hash_ctx.length[1] += block_size * CRYPTO_BITS_IN_BYTE;
268 
269     crypto_cache_flush((uintptr_t)ctx->hash_ctx.data_buffer, block_size);
270     ret = hal_cipher_hash_add_in_node(chn_num, crypto_get_phys_addr(ctx->hash_ctx.data_buffer), block_size,
271         IN_NODE_TYPE_FIRST | IN_NODE_TYPE_LAST);
272     crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_add_in_node failed\n");
273 
274     ret = hal_cipher_hash_start(chn_num, TD_FALSE);
275     crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_start failed\n");
276 
277     ret = hal_cipher_hash_wait_done(chn_num, ctx->hash_ctx.state, sizeof(ctx->hash_ctx.state));
278     crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_wait_done failed\n");
279 
280     return ret;
281 #else
282     crypto_unused(chn_num);
283     crypto_unused(ctx);
284     crypto_unused(hash_attr);
285 
286     return HASH_COMPAT_ERRNO(ERROR_UNSUPPORT);
287 #endif
288 }
289 
drv_cipher_hash_start(td_handle * drv_hash_handle,const crypto_hash_attr * hash_attr)290 td_s32 drv_cipher_hash_start(td_handle *drv_hash_handle, const crypto_hash_attr *hash_attr)
291 {
292     td_u32 chn_num = CRYPTO_INVALID_CHN_NUM;
293     td_s32 ret = TD_FAILURE;
294     hal_hash_channel_context *ctx = TD_NULL;
295     crypto_drv_func_enter();
296     crypto_chk_return(g_drv_hash_init == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_NOT_INIT), "call init first\n");
297 
298     ret = inner_hash_start_param_chk(drv_hash_handle, hash_attr);
299     if (ret != TD_SUCCESS) {
300         return ret;
301     }
302     crypto_chk_return(crypto_hash_support(hash_attr->hash_type) == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_UNSUPPORT),
303         "alg is unsupport\n");
304 
305     /* Lock one free Hash Hard Channel. */
306     ret = drv_hash_lock_chn(&chn_num);
307     if (ret != TD_SUCCESS) {
308         crypto_log_err("drv_hash_lock_chn failed\n");
309         return ret;
310     }
311     ctx = &g_hash_channel_ctx_list[chn_num];
312     (td_void)memset_s(ctx, sizeof(hal_hash_channel_context), 0, sizeof(hal_hash_channel_context));
313 
314     /* Alloc data_buffer. */
315     ctx->hash_ctx.data_buffer = g_drv_dma_buf + CRYPTO_HASH_DRV_BUFFER_SIZE * chn_num;
316     ctx->hash_ctx.data_buffer_len = CRYPTO_HASH_DRV_BUFFER_SIZE;
317     (td_void)memset_s(ctx->hash_ctx.data_buffer, ctx->hash_ctx.data_buffer_len, 0, ctx->hash_ctx.data_buffer_len);
318     ctx->hash_type = hash_attr->hash_type;
319     /* Set Config. */
320     ret = drv_hash_get_state(hash_attr->hash_type, ctx->hash_ctx.state, sizeof(ctx->hash_ctx.state));
321     crypto_chk_goto(ret != CRYPTO_SUCCESS, error_hash_unlock, "drv_hash_get_state failed\n");
322 
323     ret = hal_cipher_hash_config(chn_num, hash_attr->hash_type, ctx->hash_ctx.state);
324     crypto_chk_goto(ret != CRYPTO_SUCCESS, error_hash_unlock, "hal_cipher_hash_config failed\n");
325 
326     if (crypto_hash_is_hmac(hash_attr->hash_type) == TD_TRUE) {
327         if (hash_attr->is_keyslot) {
328             ret = hal_cipher_hash_attach(chn_num, hash_attr->drv_keyslot_handle);
329             crypto_chk_goto(ret != CRYPTO_SUCCESS, error_hash_unlock, "hal_cipher_hash_attach failed\n");
330         } else {
331             ret = inner_drv_hmac_start(chn_num, ctx, hash_attr);
332             crypto_chk_goto(ret != TD_SUCCESS, error_hash_unlock, "inner_drv_hmac_start failed\n");
333         }
334         ctx->is_keyslot = hash_attr->is_keyslot;
335     }
336 
337     *drv_hash_handle = chn_num;
338     ctx->open = TD_TRUE;
339     crypto_drv_func_exit();
340     return ret;
341 error_hash_unlock:
342     (td_void)hal_hash_unlock(chn_num);
343     return ret;
344 }
345 
drv_cipher_hash_update(td_handle drv_hash_handle,const crypto_buf_attr * src_buf,const td_u32 len)346 td_s32 drv_cipher_hash_update(td_handle drv_hash_handle,  const crypto_buf_attr *src_buf, const td_u32 len)
347 {
348     td_s32 ret = TD_SUCCESS;
349     td_u32 chn_num = (td_u32)drv_hash_handle;
350     hal_hash_channel_context *ctx = TD_NULL;
351     td_u32 tail_len = 0;
352     td_u32 *length = TD_NULL;
353     td_u32 block_size;
354     td_u32 processing_len = 0;
355     td_u32 processed_len = 0;
356     td_u8 *data_buffer = TD_NULL;
357     td_u32 left = 0;
358     td_u32 node_type = IN_NODE_TYPE_FIRST;
359     crypto_drv_func_enter();
360     crypto_chk_return(g_drv_hash_init == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_NOT_INIT), "call init first\n");
361 
362     hash_null_ptr_chk(src_buf);
363     hash_null_ptr_chk(src_buf->virt_addr);
364 
365     ret = inner_hash_drv_handle_chk(drv_hash_handle);
366     if (ret != TD_SUCCESS) {
367         return ret;
368     }
369 
370     ctx = &g_hash_channel_ctx_list[chn_num];
371     crypto_chk_return(ctx->open == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_CTX_CLOSED), "ctx is closed!\n");
372 
373     data_buffer = ctx->hash_ctx.data_buffer;
374     crypto_chk_return(data_buffer == TD_NULL, HASH_COMPAT_ERRNO(ERROR_UNEXPECTED), "unexpected error\n");
375 
376     left = len;
377     tail_len = ctx->hash_ctx.tail_len;
378     length = ctx->hash_ctx.length;
379     block_size = crypto_hash_get_block_size(ctx->hash_type) / CRYPTO_BITS_IN_BYTE;
380     if (ctx->is_keyslot == TD_FALSE) {
381         node_type |= IN_NODE_TYPE_LAST;
382     }
383     /* 如果数据能保存到 Tail 里,Update 只进行拷贝. */
384     if (tail_len + left < block_size) {
385         processing_len = left;
386         ret = memcpy_s(ctx->hash_ctx.tail + tail_len, sizeof(ctx->hash_ctx.tail) - tail_len,
387             src_buf->virt_addr, processing_len);
388         crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
389         length[1] += processing_len * CRYPTO_BITS_IN_BYTE;
390         ctx->hash_ctx.tail_len += processing_len;
391         return TD_SUCCESS;
392     }
393 
394     /* Process the tail. */
395     ret = memcpy_s(data_buffer, ctx->hash_ctx.data_buffer_len, ctx->hash_ctx.tail, tail_len);
396     crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
397 
398     processing_len = block_size - tail_len;
399     ret = memcpy_s(data_buffer + tail_len, ctx->hash_ctx.data_buffer_len - tail_len,
400         src_buf->virt_addr, processing_len);
401     crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
402 
403     crypto_cache_flush((uintptr_t)data_buffer, block_size);
404     ret = hal_cipher_hash_add_in_node(chn_num, crypto_get_phys_addr(data_buffer), block_size, node_type);
405     crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_add_in_node failed\n");
406 
407     ret = hal_cipher_hash_start(chn_num, TD_FALSE);
408     crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_start failed\n");
409 
410     ret = hal_cipher_hash_wait_done(chn_num, ctx->hash_ctx.state, sizeof(ctx->hash_ctx.state));
411     crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_wait_done failed\n");
412 
413     left -= processing_len;
414     processed_len += processing_len;
415     ctx->hash_ctx.tail_len = 0;
416 
417     /* Process the Remain Data. */
418     while (left >= block_size) {
419         if (left >= ctx->hash_ctx.data_buffer_len) {
420             processing_len = ctx->hash_ctx.data_buffer_len;
421         } else {
422             processing_len = left - left % block_size;
423         }
424         ret = memcpy_s(data_buffer, ctx->hash_ctx.data_buffer_len,
425             (td_u8 *)((uintptr_t)(src_buf->virt_addr) + processed_len), processing_len);
426         crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
427 
428         crypto_cache_flush((uintptr_t)data_buffer, processing_len);
429         ret = hal_cipher_hash_add_in_node(chn_num, crypto_get_phys_addr(data_buffer), processing_len, node_type);
430         crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_add_in_node failed\n");
431 
432         ret = hal_cipher_hash_start(chn_num, TD_TRUE);
433         crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_start failed\n");
434 
435         ret = hal_cipher_hash_wait_done(chn_num, ctx->hash_ctx.state, sizeof(ctx->hash_ctx.state));
436         crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_wait_done failed\n");
437 
438         left -= processing_len;
439         processed_len += processing_len;
440     }
441 
442     if (left != 0) {
443         ret = memcpy_s(ctx->hash_ctx.tail, sizeof(ctx->hash_ctx.tail),
444         (td_u8 *)((uintptr_t)(src_buf->virt_addr) + processed_len), left);
445         crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
446     }
447     ctx->hash_ctx.tail_len = left;
448     length[1] += len * CRYPTO_BITS_IN_BYTE;
449     crypto_drv_func_exit();
450     return ret;
451 }
452 
drv_process_tail(td_u32 chn_num,hal_hash_channel_context * ctx)453 static td_s32 drv_process_tail(td_u32 chn_num, hal_hash_channel_context *ctx)
454 {
455     td_s32 ret = TD_SUCCESS;
456     td_u8 *buffer = ctx->hash_ctx.data_buffer;
457     td_u32 buffer_len = ctx->hash_ctx.data_buffer_len;
458     td_u32 tail_len = ctx->hash_ctx.tail_len;
459     td_u32 *length = ctx->hash_ctx.length;
460     td_u32 max_message_len = crypto_hash_get_message_len(ctx->hash_type) / CRYPTO_BITS_IN_BYTE;
461     td_u32 block_size = crypto_hash_get_block_size(ctx->hash_type) / CRYPTO_BITS_IN_BYTE;
462     td_u32 processing_len = 0;
463 
464     ret = memcpy_s(buffer, buffer_len, ctx->hash_ctx.tail, tail_len);
465     crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
466 
467     (td_void)memset_s(buffer + tail_len, ctx->hash_ctx.data_buffer_len - tail_len,
468         0x00, ctx->hash_ctx.data_buffer_len - tail_len);
469 
470     if (tail_len + 1 + max_message_len > block_size) {
471         processing_len = block_size * 2; // in this case , we need 2 buffer
472     } else {
473         processing_len = block_size;
474     }
475     buffer[tail_len] = 0x80;
476     if (ctx->is_keyslot != 0) {
477         length[1] += block_size * CRYPTO_BITS_IN_BYTE;
478     }
479     length[1] = crypto_cpu_to_be32(length[1]);
480 
481     ret = memcpy_s(buffer + processing_len - sizeof(ctx->hash_ctx.length), sizeof(ctx->hash_ctx.length),
482         length, sizeof(ctx->hash_ctx.length));
483     crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
484 
485     crypto_cache_flush((uintptr_t)buffer, processing_len);
486     ret = hal_cipher_hash_add_in_node(chn_num, crypto_get_phys_addr(buffer), processing_len,
487         IN_NODE_TYPE_FIRST | IN_NODE_TYPE_LAST);
488     crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_add_in_node failed\n");
489 
490     ret = hal_cipher_hash_start(chn_num, TD_TRUE);
491     crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_start failed\n");
492 
493     ret = hal_cipher_hash_wait_done(chn_num, ctx->hash_ctx.state, sizeof(ctx->hash_ctx.state));
494     if (ret != TD_SUCCESS) {
495         crypto_print("hal_cipher_hash_wait_done failed\n");
496         return ret;
497     }
498 
499     return ret;
500 }
501 
502 #if defined(CRYPTO_SOFT_HMAC_SUPPORT)
drv_hmac_finish_process(td_u32 chn_num,hal_hash_channel_context * ctx,td_u8 * out,td_u32 out_len)503 static td_s32 drv_hmac_finish_process(td_u32 chn_num, hal_hash_channel_context *ctx, td_u8 *out, td_u32 out_len)
504 {
505     td_s32 ret = TD_SUCCESS;
506     td_u8 *buffer = ctx->hash_ctx.data_buffer;
507     td_u32 buffer_len = ctx->hash_ctx.data_buffer_len;
508     td_u32 *length = ctx->hash_ctx.length;
509     td_u32 processed_len = 0;
510     td_u32 block_size = crypto_hash_get_block_size(ctx->hash_type) / 8; // 8 means 1 byte = 8 bits
511 
512     (td_void)memset_s(buffer, buffer_len, 0, buffer_len);
513     /* Copy o_key_pad */
514     ret = memcpy_s(buffer, buffer_len, ctx->hash_ctx.o_key_pad, block_size);
515     crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
516 
517     processed_len += block_size;
518 
519     /* Copy hash. */
520     ret = memcpy_s(buffer + processed_len, buffer_len - processed_len, out, out_len);
521     crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
522 
523     processed_len += out_len;
524     buffer[processed_len] = 0x80;
525     length[1] = crypto_cpu_to_be32(processed_len * 8); // 8 means 1 byte = 8 bits
526     ret = memcpy_s(buffer + 2 * block_size - sizeof(ctx->hash_ctx.length), // 2 block offset
527         sizeof(ctx->hash_ctx.length), length, sizeof(ctx->hash_ctx.length));
528     crypto_chk_return(ret != EOK, HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
529 
530     ret = drv_hash_get_state(ctx->hash_type, ctx->hash_ctx.state, sizeof(ctx->hash_ctx.state));
531     if (ret != TD_SUCCESS) {
532         crypto_log_err("drv_hash_get_state failed\n");
533         return ret;
534     }
535     ret = hal_cipher_hash_config(chn_num, ctx->hash_type, ctx->hash_ctx.state);
536     if (ret != TD_SUCCESS) {
537         crypto_log_err("hal_cipher_hash_config failed\n");
538         return ret;
539     }
540 
541     crypto_cache_flush((uintptr_t)buffer, 2 * block_size); // 2 block_size needed
542     ret = hal_cipher_hash_add_in_node(chn_num, crypto_get_phys_addr(buffer), 2 * block_size, // 2 block_size needed
543         IN_NODE_TYPE_FIRST | IN_NODE_TYPE_LAST);
544     crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_add_in_node failed\n");
545 
546     ret = hal_cipher_hash_start(chn_num, TD_TRUE);
547     crypto_chk_return(ret != TD_SUCCESS, ret, "hal_cipher_hash_start failed\n");
548 
549     ret = hal_cipher_hash_wait_done(chn_num, ctx->hash_ctx.state, sizeof(ctx->hash_ctx.state));
550     if (ret != TD_SUCCESS) {
551         crypto_print("hal_cipher_hash_wait_done failed\n");
552         return ret;
553     }
554 
555     return ret;
556 }
557 #endif
558 
inner_hash_finish_common(td_handle drv_hash_handle,td_u8 * out,td_u32 * out_len,td_bool is_destroy)559 static td_s32 inner_hash_finish_common(td_handle drv_hash_handle, td_u8 *out, td_u32 *out_len, td_bool is_destroy)
560 {
561     td_s32 ret = TD_FAILURE;
562     td_u32 chn_num = (td_u32)drv_hash_handle;
563     hal_hash_channel_context *ctx = TD_NULL;
564     td_u32 result_size;
565 
566     crypto_drv_func_enter();
567     crypto_chk_return(g_drv_hash_init == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_NOT_INIT), "call init first\n");
568 
569     hash_null_ptr_chk(out);
570     hash_null_ptr_chk(out_len);
571 
572     ret = inner_hash_drv_handle_chk(drv_hash_handle);
573     if (ret != TD_SUCCESS) {
574         return ret;
575     }
576 
577     ctx = &g_hash_channel_ctx_list[chn_num];
578     crypto_chk_return(ctx->open == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_CTX_CLOSED), "ctx is closed!\n");
579 
580     crypto_chk_return(ctx->hash_ctx.data_buffer == TD_NULL, HASH_COMPAT_ERRNO(ERROR_UNEXPECTED), "unexpected error\n");
581 
582     result_size = crypto_hash_get_result_size(ctx->hash_type) / CRYPTO_BITS_IN_BYTE;
583     crypto_chk_return(*out_len < result_size, HASH_COMPAT_ERRNO(ERROR_INVALID_PARAM), "out's size is not enough\n");
584 
585     /* Process the Tail Data. */
586     ret = drv_process_tail(chn_num, ctx);
587     crypto_chk_goto(ret != TD_SUCCESS, exit_hash_unlock, "drv_process_tail failed\n");
588 
589     ret = memcpy_s(out, *out_len, ctx->hash_ctx.state, result_size);
590     crypto_chk_goto(ret != TD_SUCCESS, exit_hash_unlock, "memcpy_s failed\n");
591 
592 #if defined(CRYPTO_SOFT_HMAC_SUPPORT)
593     if (crypto_hash_is_hmac(ctx->hash_type) && ctx->is_keyslot == TD_FALSE) {
594         ret = drv_hmac_finish_process(chn_num, ctx, out, result_size);
595         crypto_chk_goto(ret != TD_SUCCESS, exit_hash_unlock, "drv_hmac_finish_process failed\n");
596 
597         ret = memcpy_s(out, *out_len, ctx->hash_ctx.state, result_size);
598         crypto_chk_goto(ret != TD_SUCCESS, exit_hash_unlock, "memcpy_s failed\n");
599     }
600 #endif
601 
602     *out_len = result_size;
603 exit_hash_unlock:
604     if (is_destroy) {
605         hal_hash_unlock(chn_num);
606         (td_void)memset_s(ctx, sizeof(hal_hash_channel_context), 0, sizeof(hal_hash_channel_context));
607     }
608     crypto_drv_func_exit();
609     return ret;
610 }
611 
drv_cipher_hash_finish(td_handle drv_hash_handle,td_u8 * out,td_u32 * out_len)612 td_s32 drv_cipher_hash_finish(td_handle drv_hash_handle, td_u8 *out, td_u32 *out_len)
613 {
614     return inner_hash_finish_common(drv_hash_handle, out, out_len, TD_TRUE);
615 }
616 
drv_cipher_hash_finish_data(td_handle drv_hash_handle,td_u8 * out,td_u32 * out_len)617 td_s32 drv_cipher_hash_finish_data(td_handle drv_hash_handle, td_u8 *out, td_u32 *out_len)
618 {
619     return inner_hash_finish_common(drv_hash_handle, out, out_len, TD_FALSE);
620 }
621 
drv_cipher_hash_get(td_handle drv_hash_handle,crypto_hash_clone_ctx * hash_ctx)622 td_s32 drv_cipher_hash_get(td_handle drv_hash_handle, crypto_hash_clone_ctx *hash_ctx)
623 {
624     td_s32 ret = TD_SUCCESS;
625     td_u32 chn_num = (td_u32)drv_hash_handle;
626     hal_hash_channel_context *ctx = TD_NULL;
627     crypto_drv_func_enter();
628     crypto_chk_return(g_drv_hash_init == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_NOT_INIT), "call init first\n");
629 
630     hash_null_ptr_chk(hash_ctx);
631     ret = inner_hash_drv_handle_chk(drv_hash_handle);
632     if (ret != TD_SUCCESS) {
633         return ret;
634     }
635 
636     ctx = &g_hash_channel_ctx_list[chn_num];
637     crypto_chk_return(ctx->open == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_CTX_CLOSED), "ctx is closed!\n");
638 
639     (td_void)memset_s(hash_ctx, sizeof(crypto_hash_clone_ctx), 0, sizeof(crypto_hash_clone_ctx));
640     /* Clone Length. */
641     ret = memcpy_s(hash_ctx->length, sizeof(hash_ctx->length), ctx->hash_ctx.length, sizeof(ctx->hash_ctx.length));
642     crypto_chk_goto_with_ret(ret, ret != EOK, error_clear_hash_ctx,
643         HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
644 
645     /* Clone state. */
646     ret = memcpy_s(hash_ctx->state, sizeof(hash_ctx->state), ctx->hash_ctx.state, sizeof(ctx->hash_ctx.state));
647     crypto_chk_goto_with_ret(ret, ret != EOK, error_clear_hash_ctx,
648         HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
649 
650 #if defined(CRYPTO_SOFT_HMAC_SUPPORT)
651     /* Clone o_key_pad. */
652     ret = memcpy_s(hash_ctx->o_key_pad, sizeof(hash_ctx->o_key_pad),
653         ctx->hash_ctx.o_key_pad, sizeof(ctx->hash_ctx.o_key_pad));
654     crypto_chk_goto_with_ret(ret, ret != EOK, error_clear_hash_ctx,
655         HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
656 
657     /* Clone i_key_pad. */
658     ret = memcpy_s(hash_ctx->i_key_pad, sizeof(hash_ctx->i_key_pad),
659         ctx->hash_ctx.i_key_pad, sizeof(ctx->hash_ctx.i_key_pad));
660     crypto_chk_goto_with_ret(ret, ret != EOK, error_clear_hash_ctx,
661         HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
662 #endif
663     /* Clone tail. */
664     ret = memcpy_s(hash_ctx->tail, sizeof(hash_ctx->tail), ctx->hash_ctx.tail, ctx->hash_ctx.tail_len);
665     crypto_chk_goto_with_ret(ret, ret != EOK, error_clear_hash_ctx,
666         HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
667 
668     hash_ctx->tail_len = ctx->hash_ctx.tail_len;
669 
670     /* Clone Hash Type. */
671     hash_ctx->hash_type = ctx->hash_type;
672     crypto_drv_func_exit();
673 
674     return ret;
675 error_clear_hash_ctx:
676     (td_void)memset_s(hash_ctx, sizeof(crypto_hash_clone_ctx), 0, sizeof(crypto_hash_clone_ctx));
677     return ret;
678 }
679 
drv_cipher_hash_set(td_handle drv_hash_handle,const crypto_hash_clone_ctx * hash_ctx)680 td_s32 drv_cipher_hash_set(td_handle drv_hash_handle, const crypto_hash_clone_ctx *hash_ctx)
681 {
682     td_s32 ret = TD_SUCCESS;
683     td_u32 chn_num = (td_u32)drv_hash_handle;
684     hal_hash_channel_context *ctx = TD_NULL;
685     crypto_drv_func_enter();
686     crypto_chk_return(g_drv_hash_init == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_NOT_INIT), "call init first\n");
687 
688     hash_null_ptr_chk(hash_ctx);
689     ret = inner_hash_drv_handle_chk(drv_hash_handle);
690     if (ret != TD_SUCCESS) {
691         return ret;
692     }
693 
694     ctx = &g_hash_channel_ctx_list[chn_num];
695     crypto_chk_return(ctx->open == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_CTX_CLOSED), "ctx is closed!\n");
696 
697     /* Clone Length. */
698     ret = memcpy_s(ctx->hash_ctx.length, sizeof(ctx->hash_ctx.length), hash_ctx->length, sizeof(hash_ctx->length));
699     crypto_chk_goto_with_ret(ret, ret != EOK, error_clear_hash_ctx,
700         HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
701 
702     /* Clone state. */
703     ret = memcpy_s(ctx->hash_ctx.state, sizeof(ctx->hash_ctx.state), hash_ctx->state, sizeof(hash_ctx->state));
704     crypto_chk_goto_with_ret(ret, ret != EOK, error_clear_hash_ctx,
705         HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
706 
707 #if defined(CRYPTO_SOFT_HMAC_SUPPORT)
708     /* Clone o_key_pad. */
709     ret = memcpy_s(ctx->hash_ctx.o_key_pad, sizeof(ctx->hash_ctx.o_key_pad),
710         hash_ctx->o_key_pad, sizeof(hash_ctx->o_key_pad));
711     crypto_chk_goto_with_ret(ret, ret != EOK, error_clear_hash_ctx,
712         HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
713 
714     /* Clone i_key_pad. */
715     ret = memcpy_s(ctx->hash_ctx.i_key_pad, sizeof(ctx->hash_ctx.i_key_pad),
716         hash_ctx->i_key_pad, sizeof(hash_ctx->i_key_pad));
717     crypto_chk_goto_with_ret(ret, ret != EOK, error_clear_hash_ctx,
718         HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
719 #endif
720     /* Clone tail. */
721     ret = memcpy_s(ctx->hash_ctx.tail, sizeof(ctx->hash_ctx.tail), hash_ctx->tail, sizeof(hash_ctx->tail));
722     crypto_chk_goto_with_ret(ret, ret != EOK, error_clear_hash_ctx,
723         HASH_COMPAT_ERRNO(ERROR_MEMCPY_S), "memcpy_s failed\n");
724 
725     ctx->hash_ctx.tail_len = hash_ctx->tail_len;
726 
727     /* Clone Hash Type. */
728     ctx->hash_type = hash_ctx->hash_type;
729 
730     /* Set Hash Config. */
731     ret = hal_cipher_hash_config(chn_num, ctx->hash_type, ctx->hash_ctx.state);
732     if (ret != TD_SUCCESS) {
733         crypto_print("hal_cipher_hash_config expected is 0x%x, real ret is 0x%x\n", TD_SUCCESS, ret);
734         goto error_clear_hash_ctx;
735     }
736     /* Set Data Buffer. */
737     ctx->hash_ctx.data_buffer = g_drv_dma_buf + CRYPTO_HASH_DRV_BUFFER_SIZE * chn_num;
738     ctx->hash_ctx.data_buffer_len = CRYPTO_HASH_DRV_BUFFER_SIZE;
739     crypto_drv_func_exit();
740 
741     return ret;
742 error_clear_hash_ctx:
743     (td_void)memset_s(ctx, sizeof(hal_hash_channel_context), 0, sizeof(hal_hash_channel_context));
744     return ret;
745 }
746 
drv_cipher_hash_destroy(td_handle drv_hash_handle)747 td_s32 drv_cipher_hash_destroy(td_handle drv_hash_handle)
748 {
749     td_s32 ret = TD_SUCCESS;
750     td_u32 chn_num = (td_u32)drv_hash_handle;
751     hal_hash_channel_context *ctx = TD_NULL;
752     crypto_drv_func_enter();
753     crypto_chk_return(g_drv_hash_init == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_NOT_INIT), "call init first\n");
754 
755     ret = inner_hash_drv_handle_chk(drv_hash_handle);
756     if (ret != TD_SUCCESS) {
757         return ret;
758     }
759     ctx = &g_hash_channel_ctx_list[chn_num];
760     crypto_chk_return(ctx->open == TD_FALSE, HASH_COMPAT_ERRNO(ERROR_CTX_CLOSED), "ctx is closed!\n");
761 
762     hal_hash_unlock(chn_num);
763 #if defined(CRYPTO_DEBUG_ON)
764     crypto_print("Unlock Hash CHN %u success\n", chn_num);
765     hal_hash_debug();
766 #endif
767     (td_void)memset_s(ctx, sizeof(hal_hash_channel_context), 0, sizeof(hal_hash_channel_context));
768     crypto_drv_func_exit();
769     return ret;
770 }
771 
drv_hash_get_state_iv(crypto_hash_type hash_type,td_u32 * iv_size)772 const td_u32 *drv_hash_get_state_iv(crypto_hash_type hash_type, td_u32 *iv_size)
773 {
774     const td_u8 *state_val = TD_NULL;
775     td_u32 hash_alg = crypto_hash_get_alg(hash_type);
776     td_u32 hash_mode = crypto_hash_get_mode(hash_type);
777 
778     crypto_chk_return(iv_size == TD_NULL, TD_NULL, "iv_size is NULL!\n");
779 
780     switch (hash_mode) {
781         case CRYPTO_HASH_MODE_UNDEF:
782             state_val = g_sha1_ival;
783             *iv_size = (td_u32)sizeof(g_sha1_ival);
784             break;
785         case CRYPTO_HASH_MODE_224:
786             state_val = g_sha224_ival;
787             *iv_size = (td_u32)sizeof(g_sha224_ival);
788             break;
789         case CRYPTO_HASH_MODE_256: {
790             if (hash_alg == CRYPTO_HASH_ALG_SHA2) {
791                 state_val = g_sha256_ival;
792                 *iv_size = (td_u32)sizeof(g_sha256_ival);
793             } else if (hash_alg == CRYPTO_HASH_ALG_SM3) {
794                 state_val = g_sm3_ival;
795                 *iv_size = (td_u32)sizeof(g_sm3_ival);
796             }
797             break;
798         }
799         case CRYPTO_HASH_MODE_384: {
800             state_val = g_sha384_ival;
801             *iv_size = (td_u32)sizeof(g_sha384_ival);
802             break;
803         }
804         case CRYPTO_HASH_MODE_512: {
805             state_val = g_sha512_ival;
806             *iv_size = (td_u32)sizeof(g_sha512_ival);
807             break;
808         }
809         default: {
810             crypto_log_err("Invalid Hash Mode!\n");
811             break;
812         }
813     }
814     return (const td_u32 *)state_val;
815 }
816 
inner_hash_drv_handle_chk(td_handle hash_handle)817 td_s32 inner_hash_drv_handle_chk(td_handle hash_handle)
818 {
819     td_u32 chn_num = (td_u32)hash_handle;
820     crypto_chk_return(chn_num >= CRYPTO_HASH_HARD_CHN_CNT, HASH_COMPAT_ERRNO(ERROR_INVALID_HANDLE),
821         "hash_handle is invalid\n");
822     crypto_chk_return(((1 << chn_num) & CRYPTO_HASH_HARD_CHN_MASK) == 0, HASH_COMPAT_ERRNO(ERROR_INVALID_HANDLE),
823         "hash_handle is invalid\n");
824 
825     return TD_SUCCESS;
826 }
827 
inner_hash_start_param_chk(td_handle * drv_hash_handle,const crypto_hash_attr * hash_attr)828 td_s32 inner_hash_start_param_chk(td_handle *drv_hash_handle, const crypto_hash_attr *hash_attr)
829 {
830     crypto_hash_type type;
831     td_u32 block_size = 0;
832 
833     hash_null_ptr_chk(drv_hash_handle);
834     hash_null_ptr_chk(hash_attr);
835 
836     type = hash_attr->hash_type;
837     crypto_chk_return(type != CRYPTO_HASH_TYPE_SHA1 && type != CRYPTO_HASH_TYPE_SHA224 &&
838         type != CRYPTO_HASH_TYPE_SHA256 && type != CRYPTO_HASH_TYPE_SHA384 &&
839         type != CRYPTO_HASH_TYPE_SHA512 && type != CRYPTO_HASH_TYPE_SM3 &&
840         type != CRYPTO_HASH_TYPE_HMAC_SHA1 && type != CRYPTO_HASH_TYPE_HMAC_SHA224 &&
841         type != CRYPTO_HASH_TYPE_HMAC_SHA256 && type != CRYPTO_HASH_TYPE_HMAC_SHA384 &&
842         type != CRYPTO_HASH_TYPE_HMAC_SHA512 && type != CRYPTO_HASH_TYPE_HMAC_SM3,
843         HASH_COMPAT_ERRNO(ERROR_INVALID_PARAM), "hash_type is invalid!\n");
844 
845     if (crypto_hash_is_hmac(type) != TD_TRUE) {
846         return TD_SUCCESS;
847     }
848 
849     /* HMAC Check. */
850     block_size = crypto_hash_get_block_size(type) / CRYPTO_BITS_IN_BYTE;
851     if (hash_attr->is_keyslot == TD_TRUE) {
852         /* Optimize: check the validation of keyslot channel. */
853     } else {
854         if (hash_attr->key_len != 0) {
855             crypto_chk_return(hash_attr->key == TD_NULL, HASH_COMPAT_ERRNO(ERROR_PARAM_IS_NULL), "key is NULL\n");
856         }
857         crypto_chk_return(hash_attr->key_len > block_size, HASH_COMPAT_ERRNO(ERROR_INVALID_PARAM),
858             "key_len shouldn't larget than block_size\n");
859     }
860     return TD_SUCCESS;
861 }