1 /**
2 ******************************************************************************
3 * @file rtl8721dhp_sha.c
4 * @author
5 * @version V1.0.0
6 * @date 2016-05-17
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the HW crypto:
9 * - SHA1/SHA2
10 * - HMAC SHA1/SHA2
11 ******************************************************************************
12 * @attention
13 *
14 * This module is a confidential and proprietary property of RealTek and
15 * possession or use of this module requires written permission of RealTek.
16 *
17 * Copyright(c) 2015, Realtek Semiconductor Corporation. All rights reserved.
18 ******************************************************************************
19 */
20
21 #include "ameba_soc.h"
22 #include "hal_crypto.h"
23
24 unsigned char *hmac_sha1_key = NULL;
25 uint32_t hmac_sha1_keylen = 0;
26 unsigned char *hmac_sha2_key = NULL;
27 uint32_t hmac_sha2_keylen = 0;
28 /**
29 * @brief SHA1 init
30 * @retval 0 : SUCCESS <br>
31 * others : fail, refer to ERRNO
32 */
rtl_crypto_sha1_init(void)33 int rtl_crypto_sha1_init(void)
34 {
35 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
36 const u8* pCipherKey = NULL;
37 const u32 lenCipherKey = 0;
38 const u8* pAuthKey = NULL;
39 const u32 lenAuthKey = 0;
40
41 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
42
43 // for sequential hash
44 pIE->hmac_seq_is_recorded = 0;
45 pIE->hmac_seq_buf_is_used_bytes = 0;
46
47 return CRYPTO_SetSecurityModeAD(pIE, CIPHER_TYPE_NO_CIPHER, AUTH_TYPE_SHA1,
48 pCipherKey, lenCipherKey, pAuthKey, lenAuthKey);
49 }
50
51 /**
52 * @brief SHA1 process.
53 * @param message : input buffer
54 * @param msglen : input buffer length
55 * @param pDigest : the result of SHA1 function
56 * @retval 0 : SUCCESS <br>
57 * others : fail, refer to ERRNO
58 */
rtl_crypto_sha1_process(IN const u8 * message,IN const u32 msglen,OUT u8 * pDigest)59 int rtl_crypto_sha1_process(
60 IN const u8* message, IN const u32 msglen,
61 OUT u8* pDigest)
62 {
63 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
64
65 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA1)
66 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
67
68 return CRYPTO_ProcessAD(pIE, message, msglen, NULL, 0, NULL, 0, pDigest, NULL);
69 }
70
71 /**
72 * @brief SHA1 update.
73 * @param message : input buffer
74 * @param msglen : input buffer length
75 * @note Use in sequential hash
76 * @retval 0 : SUCCESS <br>
77 * others : fail, refer to ERRNO
78 */
rtl_crypto_sha1_update(IN const u8 * message,IN const u32 msglen)79 int rtl_crypto_sha1_update(IN const u8* message, IN const u32 msglen)
80 {
81 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
82 int ret = SUCCESS;
83
84 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
85 assert_param(message != NULL);
86 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA1)
87 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
88 /*
89 if ( pIE->hmac_seq_is_recorded != 0 ) {
90 ret = CRYPTO_SendSeqBuf(NULL);
91 if ( ret != SUCCESS ) return ret;
92 }
93 */
94 pIE->hmac_seq_last_message = (u8*)message;
95 pIE->hmac_seq_last_msglen = msglen;
96 pIE->hmac_seq_is_recorded = 1;
97 ret = CRYPTO_SendSeqBuf(NULL);
98
99 return ret;
100 }
101
102 /**
103 * @brief SHA1 final process.
104 * @param pDigest : the result of SHA1 function
105 * @note Use in sequential hash, process the last block
106 * @retval 0 : SUCCESS <br>
107 * others : fail, refer to ERRNO
108 */
rtl_crypto_sha1_final(OUT u8 * pDigest)109 int rtl_crypto_sha1_final(OUT u8* pDigest)
110 {
111 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
112
113 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
114 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA1)
115 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
116
117 if ( pDigest == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
118
119 if ( pIE->hmac_seq_is_recorded == 0 ) return _ERRNO_CRYPTO_HASH_FINAL_NO_UPDATE;
120
121 //return CRYPTO_SendSeqBuf(pDigest);
122 return CRYPTO_ProcessAD(pIE, (const uint8_t *)pIE->hmac_seq_buf, pIE->hmac_seq_buf_is_used_bytes, NULL, 0, NULL, 0, pDigest, NULL);
123 }
124
125 /**
126 * @ SHA1 init & process
127 *
128 * @param message : input buffer
129 * @param msglen : input buffer length
130 * @param pDigest : the result of SHA1 function
131 * @retval 0 : SUCCESS <br>
132 * others : fail, refer to ERRNO
133 */
rtl_crypto_sha1(IN const u8 * message,IN const u32 msglen,OUT u8 * pDigest)134 int rtl_crypto_sha1(IN const u8* message, IN const u32 msglen, OUT u8* pDigest)
135 {
136 int ret;
137
138 ret = rtl_crypto_sha1_init();
139 if ( ret != SUCCESS ) return ret;
140
141 ret = rtl_crypto_sha1_process(message, msglen, pDigest);
142
143 return ret;
144 }
145
146 /**
147 * @brief SHA2 init
148 * @param sha2type : This parameter can be one of the following values:
149 * @arg SHA2_224
150 * @arg SHA2_256
151 * @arg SHA2_384
152 * @arg SHA2_512
153 * @retval 0 : SUCCESS <br>
154 * others : fail, refer to ERRNO
155 */
rtl_crypto_sha2_init(IN const SHA2_TYPE sha2type)156 int rtl_crypto_sha2_init(IN const SHA2_TYPE sha2type)
157 {
158 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
159 int auth_type;
160 const u8* pCipherKey = NULL;
161 const u32 lenCipherKey = 0;
162 const u8* pAuthKey = NULL;
163 const u32 lenAuthKey = 0;
164
165 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
166
167 switch (sha2type) {
168 case SHA2_224 :
169 auth_type = AUTH_TYPE_SHA2_224_ALL;
170 break;
171 case SHA2_256 :
172 auth_type = AUTH_TYPE_SHA2_256_ALL;
173 break;
174 case SHA2_384 :
175 case SHA2_512 :
176 default:
177 return FAIL;
178 }
179
180 // for sequential hash
181 pIE->hmac_seq_is_recorded = 0;
182 pIE->hmac_seq_buf_is_used_bytes = 0;
183
184 return CRYPTO_SetSecurityModeAD(pIE, CIPHER_TYPE_NO_CIPHER, auth_type,
185 pCipherKey, lenCipherKey, pAuthKey, lenAuthKey);
186 }
187
188 /**
189 * @brief SHA2 process.
190 * @param message : input buffer
191 * @param msglen : input buffer length
192 * @param pDigest : the result of SHA2 function
193 * @retval 0 : SUCCESS <br>
194 * others : fail, refer to ERRNO
195 */
rtl_crypto_sha2_process(IN const u8 * message,IN const u32 msglen,OUT u8 * pDigest)196 int rtl_crypto_sha2_process(IN const u8* message, IN const u32 msglen, OUT u8* pDigest)
197 {
198 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
199
200 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA2)
201 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
202
203 return CRYPTO_ProcessAD(pIE, message, msglen, NULL, 0, NULL, 0, pDigest, NULL);
204 }
205
206 /**
207 * @brief SHA2 update.
208 * @param message : input buffer
209 * @param msglen : input buffer length
210 * @note Use in sequential hash
211 * @retval 0 : SUCCESS <br>
212 * others : fail, refer to ERRNO
213 */
rtl_crypto_sha2_update(IN const u8 * message,IN const u32 msglen)214 int rtl_crypto_sha2_update(IN const u8* message, IN const u32 msglen)
215 {
216 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
217 int ret = SUCCESS;
218
219 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
220 assert_param(message != NULL);
221 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA2)
222 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
223 /*
224 if ( pIE->hmac_seq_is_recorded != 0 ) {
225 ret = CRYPTO_SendSeqBuf(NULL);
226 if ( ret != SUCCESS ) return ret;
227 }
228 */
229 pIE->hmac_seq_last_message = (u8*)message;
230 pIE->hmac_seq_last_msglen = msglen;
231 pIE->hmac_seq_is_recorded = 1;
232 ret = CRYPTO_SendSeqBuf(NULL);
233
234 return ret;
235 }
236
237 /**
238 * @brief SHA2 final process.
239 * @param pDigest : the result of SHA2 function
240 * @note Use in sequential hash, process the last block
241 * @retval 0 : SUCCESS <br>
242 * others : fail, refer to ERRNO
243 */
rtl_crypto_sha2_final(OUT u8 * pDigest)244 int rtl_crypto_sha2_final(OUT u8* pDigest)
245 {
246 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
247
248 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
249 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA2)
250 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
251
252 if ( pDigest == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
253
254 if ( pIE->hmac_seq_is_recorded == 0 ) return _ERRNO_CRYPTO_HASH_FINAL_NO_UPDATE;
255
256 //return CRYPTO_SendSeqBuf(pDigest);
257 return CRYPTO_ProcessAD(pIE, (const uint8_t *)pIE->hmac_seq_buf, pIE->hmac_seq_buf_is_used_bytes, NULL, 0, NULL, 0, pDigest, NULL);
258 }
259
260 /**
261 * @brief SHA2 init & process
262 * @param sha2type : This parameter can be one of the following values:
263 * @arg SHA2_224
264 * @arg SHA2_256
265 * @arg SHA2_384
266 * @arg SHA2_512
267 * @param message : input buffer
268 * @param msglen : input buffer length
269 * @param pDigest : the result of SHA2 function
270 * @retval 0 : SUCCESS <br>
271 * others : fail, refer to ERRNO
272 */
rtl_crypto_sha2(IN const SHA2_TYPE sha2type,IN const u8 * message,IN const u32 msglen,OUT u8 * pDigest)273 int rtl_crypto_sha2(IN const SHA2_TYPE sha2type, IN const u8* message, IN const u32 msglen,
274 OUT u8* pDigest)
275 {
276 int ret;
277
278 ret = rtl_crypto_sha2_init(sha2type);
279 if ( ret != SUCCESS ) return ret;
280
281 ret = rtl_crypto_sha2_process(message, msglen, pDigest);
282
283 return ret;
284 }
285
286 /**
287 * @brief HMAC-SHA1 start, get key
288 * @param key : need to be 4 byte alignment
289 * @param keylen : key length
290 * @retval 0 : SUCCESS <br>
291 * others : fail, refer to ERRNO
292 */
rtl_crypto_hmac_sha1_start(IN const u8 * key,IN const u32 keylen)293 int rtl_crypto_hmac_sha1_start(IN const u8* key, IN const u32 keylen)
294 {
295 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
296 for (uint32_t i = 0; i < keylen; ++i) {
297 hmac_sha1_key[i] = key[i];
298 }
299 hmac_sha1_keylen = keylen;
300 return SUCCESS;
301 }
302
303 /**
304 * @brief HMAC-SHA1 init, set key
305 * @param key : need to be 4 byte alignment
306 * @param keylen : key length
307 * @retval 0 : SUCCESS <br>
308 * others : fail, refer to ERRNO
309 */
rtl_crypto_hmac_sha1_init(IN const u8 * key,IN const u32 keylen)310 int rtl_crypto_hmac_sha1_init(IN const u8* key, IN const u32 keylen)
311 {
312 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
313 u8* pCipherKey = NULL;
314 u32 lenCipherKey = 0;
315 u8* pAuthKey = NULL;
316 u32 lenAuthKey = 0;
317
318 if (key == NULL) {
319 if (hmac_sha1_key == NULL) return _ERRNO_CRYPTO_NULL_POINTER;
320 memcpy(pAuthKey, hmac_sha1_key, hmac_sha1_keylen);
321 lenAuthKey = hmac_sha1_keylen;
322 } else {
323 memcpy(pAuthKey, key, keylen);
324 lenAuthKey = keylen;
325 }
326 if ( (u32)(pAuthKey) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4 byte alignment
327 if ( lenAuthKey > CRYPTO_AUTH_PADDING ) return _ERRNO_CRYPTO_KEY_OutRange;
328
329 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
330
331 // for sequential hash
332 pIE->hmac_seq_is_recorded = 0;
333 pIE->hmac_seq_buf_is_used_bytes = 0;
334
335 return CRYPTO_SetSecurityModeAD(pIE, CIPHER_TYPE_NO_CIPHER, AUTH_TYPE_HMAC_SHA1,
336 pCipherKey, lenCipherKey, pAuthKey, lenAuthKey);
337 }
338
339 /**
340 * @brief HMAC-SHA1 process.
341 * @param message : input buffer
342 * @param msglen : input buffer length
343 * @param pDigest : the result of HMAC-SHA1 function
344 * @retval 0 : SUCCESS <br>
345 * others : fail, refer to ERRNO
346 */
rtl_crypto_hmac_sha1_process(IN const u8 * message,IN const u32 msglen,OUT u8 * pDigest)347 int rtl_crypto_hmac_sha1_process(
348 IN const u8* message, IN const u32 msglen,
349 OUT u8* pDigest)
350 {
351 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
352
353 if ( (pIE->auth_type & AUTH_TYPE_MASK_HMAC) == 0 )
354 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
355
356 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA1 )
357 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
358
359 return CRYPTO_ProcessAD(pIE, message, msglen, NULL, 0, NULL, 0, pDigest, NULL);
360 }
361
362 /**
363 * @brief HMAC-SHA1 update.
364 * @param message : input buffer
365 * @param msglen : input buffer length
366 * @note Use in sequential hash
367 * @retval 0 : SUCCESS <br>
368 * others : fail, refer to ERRNO
369 */
rtl_crypto_hmac_sha1_update(IN const u8 * message,IN const u32 msglen)370 int rtl_crypto_hmac_sha1_update(IN const u8* message, IN const u32 msglen)
371 {
372 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
373 int ret = SUCCESS;
374
375 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
376 assert_param(message != NULL);
377 if ( (pIE->auth_type & AUTH_TYPE_MASK_HMAC) == 0 )
378 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
379
380 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA1)
381 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
382 /*
383 if ( pIE->hmac_seq_is_recorded != 0 ) {
384 ret = CRYPTO_SendSeqBuf(NULL);
385 if ( ret != SUCCESS ) return ret;
386 }
387 */
388 pIE->hmac_seq_last_message = (u8*)message;
389 pIE->hmac_seq_last_msglen = msglen;
390 pIE->hmac_seq_is_recorded = 1;
391 ret = CRYPTO_SendSeqBuf(NULL);
392
393 return ret;
394 }
395
396 /**
397 * @brief HMAC-SHA1 final process.
398 * @param pDigest : the result of HMAC-SHA1 function
399 * @note Use in sequential hash, process the last block
400 * @retval 0 : SUCCESS <br>
401 * others : fail, refer to ERRNO
402 */
rtl_crypto_hmac_sha1_final(OUT u8 * pDigest)403 int rtl_crypto_hmac_sha1_final(OUT u8* pDigest)
404 {
405 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
406
407 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
408 if ( (pIE->auth_type & AUTH_TYPE_MASK_HMAC) == 0 )
409 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
410
411 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA1 )
412 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
413
414 if ( pDigest == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
415
416 if ( pIE->hmac_seq_is_recorded == 0 ) return _ERRNO_CRYPTO_HASH_FINAL_NO_UPDATE;
417
418 //return CRYPTO_SendSeqBuf(pDigest);
419 return CRYPTO_ProcessAD(pIE, (const uint8_t *)pIE->hmac_seq_buf, pIE->hmac_seq_buf_is_used_bytes, NULL, 0, NULL, 0, pDigest, NULL);
420 }
421
422 /**
423 * @brief HMAC-SHA1 init & process
424 * @param key : need to be 4 byte alignment
425 * @param keylen : key length
426 * @param message : input buffer
427 * @param msglen : input buffer length
428 * @param pDigest : the result of HMAC-SHA1 function
429 * @retval 0 : SUCCESS <br>
430 * others : fail, refer to ERRNO
431 */
rtl_crypto_hmac_sha1(IN const u8 * message,IN const u32 msglen,IN const u8 * key,IN const u32 keylen,OUT u8 * pDigest)432 int rtl_crypto_hmac_sha1(
433 IN const u8* message, IN const u32 msglen,
434 IN const u8* key, IN const u32 keylen,
435 OUT u8* pDigest)
436 {
437 int ret;
438
439 ret = rtl_crypto_hmac_sha1_init(key, keylen);
440 if ( ret != SUCCESS ) return ret;
441
442 ret = rtl_crypto_hmac_sha1_process(message, msglen, pDigest);
443
444 return ret;
445 }
446
447 /**
448 * @brief HMAC-SHA2 start, get key
449 * @param key : need to be 4 byte alignment
450 * @param keylen : key length
451 * @retval 0 : SUCCESS <br>
452 * others : fail, refer to ERRNO
453 */
rtl_crypto_hmac_sha2_start(IN const u8 * key,IN const u32 keylen)454 int rtl_crypto_hmac_sha2_start(IN const u8* key, IN const u32 keylen)
455 {
456 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
457 for (uint32_t i = 0; i < keylen; ++i) {
458 hmac_sha2_key[i] = key[i];
459 }
460 hmac_sha2_keylen = keylen;
461 return SUCCESS;
462 }
463
464 /**
465 * @brief HMAC-SHA2 init, set key
466 * @param key : need to be 4 byte alignment
467 * @param keylen : key length
468 * @param sha2type : This parameter can be one of the following values:
469 * @arg SHA2_224
470 * @arg SHA2_256
471 * @arg SHA2_384
472 * @arg SHA2_512
473 * @retval 0 : SUCCESS <br>
474 * others : fail, refer to ERRNO
475 */
rtl_crypto_hmac_sha2_init(IN const SHA2_TYPE sha2type,IN const u8 * key,IN const u32 keylen)476 int rtl_crypto_hmac_sha2_init(
477 IN const SHA2_TYPE sha2type,
478 IN const u8* key, IN const u32 keylen)
479 {
480 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
481 int auth_type;
482 u8* pCipherKey = NULL;
483 u32 lenCipherKey = 0;
484 u8* pAuthKey = NULL;
485 u32 lenAuthKey = 0;
486
487 if (key == NULL) {
488 if (hmac_sha2_key == NULL) return _ERRNO_CRYPTO_NULL_POINTER;
489 memcpy(pAuthKey, hmac_sha2_key, hmac_sha2_keylen);
490 lenAuthKey = hmac_sha2_keylen;
491 } else {
492 memcpy(pAuthKey, key, keylen);
493 lenAuthKey = keylen;
494 }
495
496 if ( (u32)(pAuthKey) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4 byte alignment
497 if ( lenAuthKey > CRYPTO_AUTH_PADDING ) return _ERRNO_CRYPTO_KEY_OutRange;
498
499 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
500
501 switch (sha2type) {
502 case SHA2_224 :
503 auth_type = AUTH_TYPE_HMAC_SHA2_224_ALL;
504 break;
505 case SHA2_256 :
506 auth_type = AUTH_TYPE_HMAC_SHA2_256_ALL;
507 break;
508 case SHA2_384 :
509 case SHA2_512 :
510 default:
511 return FAIL;
512 }
513
514 // for sequential hash
515 pIE->hmac_seq_is_recorded = 0;
516 pIE->hmac_seq_buf_is_used_bytes = 0;
517
518 return CRYPTO_SetSecurityModeAD(pIE, CIPHER_TYPE_NO_CIPHER, auth_type,
519 pCipherKey, lenCipherKey, pAuthKey, lenAuthKey);
520 }
521
522 /**
523 * @brief HMAC-SHA2 process.
524 * @param message : input buffer
525 * @param msglen : input buffer length
526 * @param pDigest : the result of HMAC-SHA2 function
527 * @retval 0 : SUCCESS <br>
528 * others : fail, refer to ERRNO
529 */
rtl_crypto_hmac_sha2_process(IN const u8 * message,IN const u32 msglen,OUT u8 * pDigest)530 int rtl_crypto_hmac_sha2_process(
531 IN const u8* message, IN const u32 msglen,
532 OUT u8* pDigest)
533 {
534 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
535
536 if ( (pIE->auth_type & AUTH_TYPE_MASK_HMAC) == 0 )
537 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
538
539 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA2 )
540 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
541
542 return CRYPTO_ProcessAD(pIE, message, msglen, NULL, 0, NULL, 0, pDigest, NULL);
543 }
544
545 /**
546 * @brief HMAC-SHA2 update.
547 * @param message : input buffer
548 * @param msglen : input buffer length
549 * @note Use in sequential hash
550 * @retval 0 : SUCCESS <br>
551 * others : fail, refer to ERRNO
552 */
rtl_crypto_hmac_sha2_update(IN const u8 * message,IN const u32 msglen)553 int rtl_crypto_hmac_sha2_update(IN const u8* message, IN const u32 msglen)
554 {
555 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
556 int ret = SUCCESS;
557
558 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
559 assert_param(message != NULL);
560 if ( (pIE->auth_type & AUTH_TYPE_MASK_HMAC) == 0 )
561 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
562
563 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA2 )
564 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
565 /*
566 if ( pIE->hmac_seq_is_recorded != 0 ) {
567 ret = CRYPTO_SendSeqBuf(NULL);
568 if ( ret != SUCCESS ) return ret;
569 }
570 */
571 pIE->hmac_seq_last_message = (u8*)message;
572 pIE->hmac_seq_last_msglen = msglen;
573 pIE->hmac_seq_is_recorded = 1;
574 ret = CRYPTO_SendSeqBuf(NULL);
575
576 return ret;
577 }
578
579 /**
580 * @brief HMAC-SHA2 final process.
581 * @param pDigest : the result of HMAC-SHA2 function
582 * @note Use in sequential hash, process the last block
583 * @retval 0 : SUCCESS <br>
584 * others : fail, refer to ERRNO
585 */
rtl_crypto_hmac_sha2_final(OUT u8 * pDigest)586 int rtl_crypto_hmac_sha2_final(OUT u8* pDigest)
587 {
588 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
589
590 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
591 if ( (pIE->auth_type & AUTH_TYPE_MASK_HMAC) == 0 )
592 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
593
594 if ( (pIE->auth_type & AUTH_TYPE_MASK_FUNC) != AUTH_TYPE_SHA2)
595 return _ERRNO_CRYPTO_AUTH_TYPE_NOT_MATCH;
596
597 if ( pDigest == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
598
599 if ( pIE->hmac_seq_is_recorded == 0 ) return _ERRNO_CRYPTO_HASH_FINAL_NO_UPDATE;
600
601 //return CRYPTO_SendSeqBuf(pDigest);
602 return CRYPTO_ProcessAD(pIE, (const uint8_t *)pIE->hmac_seq_buf, pIE->hmac_seq_buf_is_used_bytes, NULL, 0, NULL, 0, pDigest, NULL);
603 }
604
605 /**
606 * @brief HMAC-SHA2 init & process
607 * @param key : need to be 4 byte alignment
608 * @param keylen : key length
609 * @param sha2type : This parameter can be one of the following values:
610 * @arg SHA2_224
611 * @arg SHA2_256
612 * @arg SHA2_384
613 * @arg SHA2_512
614 * @param message : input buffer
615 * @param msglen : input buffer length
616 * @param pDigest : the result of HMAC-SHA2 function
617 * @retval 0 : SUCCESS <br>
618 * others : fail, refer to ERRNO
619 */
rtl_crypto_hmac_sha2(IN const SHA2_TYPE sha2type,IN const u8 * message,IN const u32 msglen,IN const u8 * key,IN const u32 keylen,OUT u8 * pDigest)620 int rtl_crypto_hmac_sha2(
621 IN const SHA2_TYPE sha2type,
622 IN const u8* message, IN const u32 msglen,
623 IN const u8* key, IN const u32 keylen,
624 OUT u8* pDigest)
625 {
626 int ret;
627
628 ret = rtl_crypto_hmac_sha2_init(sha2type, key, keylen);
629 if ( ret != SUCCESS ) return ret;
630
631 ret = rtl_crypto_hmac_sha2_process(message, msglen, pDigest);
632
633 return ret;
634 }
635 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
636