1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2022 Nuvoton Technology Corp.
10 #include <uboot_aes.h>
13 #define HASH_DIG_H_NUM 8
15 #define HASH_CTR_STS_SHA_EN BIT(0)
16 #define HASH_CTR_STS_SHA_BUSY BIT(1)
17 #define HASH_CTR_STS_SHA_RST BIT(2)
18 #define HASH_CFG_SHA1_SHA2 BIT(0)
22 npcm_sha_type_sha2 = 0,
27 struct npcm_sha_regs {
28 unsigned int hash_data_in;
29 unsigned char hash_ctr_sts;
30 unsigned char reserved_0[0x03];
31 unsigned char hash_cfg;
32 unsigned char reserved_1[0x03];
33 unsigned char hash_ver;
34 unsigned char reserved_2[0x13];
35 unsigned int hash_dig[HASH_DIG_H_NUM];
38 struct npcm_sha_priv {
39 struct npcm_sha_regs *regs;
42 static struct npcm_sha_priv *sha_priv;
44 #ifdef SHA_DEBUG_MODULE
45 #define sha_print(fmt, args...) printf(fmt, ##args)
47 #define sha_print(fmt, args...) (void)0
50 #define SHA_BLOCK_LENGTH (512 / 8)
51 #define SHA_2_HASH_LENGTH (256 / 8)
52 #define SHA_1_HASH_LENGTH (160 / 8)
53 #define SHA_HASH_LENGTH(type) ((type == npcm_sha_type_sha2) ? \
54 (SHA_2_HASH_LENGTH) : (SHA_1_HASH_LENGTH))
56 #define SHA_SECRUN_BUFF_SIZE 64
57 #define SHA_TIMEOUT 100
58 #define SHA_DATA_LAST_BYTE 0x80
60 #define SHA2_NUM_OF_SELF_TESTS 3
61 #define SHA1_NUM_OF_SELF_TESTS 4
63 #define NUVOTON_ALIGNMENT 4
65 /*-----------------------------------------------------------------------------*/
66 /* SHA instance struct handler */
67 /*-----------------------------------------------------------------------------*/
69 u32 hv[SHA_2_HASH_LENGTH / sizeof(u32)];
72 u32 block[SHA_BLOCK_LENGTH / sizeof(u32)];
77 // The # of bytes currently in the sha block buffer
78 #define SHA_BUFF_POS(length) ((length) & (SHA_BLOCK_LENGTH - 1))
80 // The # of free bytes in the sha block buffer
81 #define SHA_BUFF_FREE(length) (SHA_BLOCK_LENGTH - SHA_BUFF_POS(length))
83 static void SHA_FlushLocalBuffer_l(const u32 *buff);
84 static int SHA_BusyWait_l(void);
85 static void SHA_GetShaDigest_l(u8 *hashdigest, u8 type);
86 static void SHA_SetShaDigest_l(const u32 *hashdigest, u8 type);
87 static void SHA_SetBlock_l(const u8 *data, u32 len, u16 position, u32 *block);
88 static void SHA_ClearBlock_l(u16 len, u16 position, u32 *block);
89 static void SHA_SetLength32_l(struct SHA_HANDLE_T *handleptr, u32 *block);
91 static int SHA_Init(struct SHA_HANDLE_T *handleptr);
92 static int SHA_Start(struct SHA_HANDLE_T *handleptr, u8 type);
93 static int SHA_Update(struct SHA_HANDLE_T *handleptr, const u8 *buffer, u32 len);
94 static int SHA_Finish(struct SHA_HANDLE_T *handleptr, u8 *hashdigest);
95 static int SHA_Reset(void);
96 static int SHA_Power(bool on);
98 static void SHA_PrintRegs(void);
99 static void SHA_PrintVersion(void);
102 static struct SHA_HANDLE_T sha_handle;
104 /*----------------------------------------------------------------------------*/
105 /* Checks if give function returns int error, and returns the error */
106 /* immediately after SHA disabling */
107 /*----------------------------------------------------------------------------*/
108 int npcm_sha_check(int status)
117 /*----------------------------------------------------------------------------*/
118 /* Function: npcm_sha_calc */
120 /* Parameters: type - SHA module type */
121 /* inBuff - Pointer to a buffer containing the data to */
123 /* len - Length of the data to hash */
124 /* hashDigest - Pointer to a buffer where the reseulting */
125 /* digest will be copied to */
127 /* Returns: 0 on success or other int error code on error */
130 /* This routine performs complete SHA calculation in one */
132 /*----------------------------------------------------------------------------*/
133 int npcm_sha_calc(u8 type, const u8 *inbuff, u32 len, u8 *hashdigest)
136 struct SHA_HANDLE_T handle;
141 SHA_Start(&handle, type);
142 status = SHA_Update(&handle, inbuff, len);
143 npcm_sha_check(status);
144 status = SHA_Finish(&handle, hashdigest);
145 npcm_sha_check(status);
152 * Computes hash value of input pbuf using h/w acceleration
154 * @param in_addr A pointer to the input buffer
155 * @param bufleni Byte length of input buffer
156 * @param out_addr A pointer to the output buffer. When complete
157 * 32 bytes are copied to pout[0]...pout[31]. Thus, a user
158 * should allocate at least 32 bytes at pOut in advance.
159 * @param chunk_size chunk size for sha256
161 void hw_sha256(const uchar *in_addr, uint buflen, uchar *out_addr, uint chunk_size)
163 puts("\nhw_sha256 using BMC HW accelerator\t");
164 npcm_sha_calc(npcm_sha_type_sha2, (u8 *)in_addr, buflen, (u8 *)out_addr);
168 * Computes hash value of input pbuf using h/w acceleration
170 * @param in_addr A pointer to the input buffer
171 * @param bufleni Byte length of input buffer
172 * @param out_addr A pointer to the output buffer. When complete
173 * 32 bytes are copied to pout[0]...pout[31]. Thus, a user
174 * should allocate at least 32 bytes at pOut in advance.
175 * @param chunk_size chunk_size for sha1
177 void hw_sha1(const uchar *in_addr, uint buflen, uchar *out_addr, uint chunk_size)
179 puts("\nhw_sha1 using BMC HW accelerator\t");
180 npcm_sha_calc(npcm_sha_type_sha1, (u8 *)in_addr, buflen, (u8 *)out_addr);
184 * Create the context for sha progressive hashing using h/w acceleration
186 * @algo: Pointer to the hash_algo struct
187 * @ctxp: Pointer to the pointer of the context for hashing
188 * @return 0 if ok, -ve on error
190 int hw_sha_init(struct hash_algo *algo, void **ctxp)
192 const char *algo_name1 = "sha1";
193 const char *algo_name2 = "sha256";
195 SHA_Init(&sha_handle);
198 if (!strcmp(algo_name1, algo->name))
199 return SHA_Start(&sha_handle, npcm_sha_type_sha1);
200 else if (!strcmp(algo_name2, algo->name))
201 return SHA_Start(&sha_handle, npcm_sha_type_sha2);
207 * Update buffer for sha progressive hashing using h/w acceleration
209 * The context is freed by this function if an error occurs.
211 * @algo: Pointer to the hash_algo struct
212 * @ctx: Pointer to the context for hashing
213 * @buf: Pointer to the buffer being hashed
214 * @size: Size of the buffer being hashed
215 * @is_last: 1 if this is the last update; 0 otherwise
216 * @return 0 if ok, -ve on error
218 int hw_sha_update(struct hash_algo *algo, void *ctx, const void *buf,
219 unsigned int size, int is_last)
221 return SHA_Update(&sha_handle, buf, size);
225 * Copy sha hash result at destination location
227 * The context is freed after completion of hash operation or after an error.
229 * @algo: Pointer to the hash_algo struct
230 * @ctx: Pointer to the context for hashing
231 * @dest_buf: Pointer to the destination buffer where hash is to be copied
232 * @size: Size of the buffer being hashed
233 * @return 0 if ok, -ve on error
235 int hw_sha_finish(struct hash_algo *algo, void *ctx, void *dest_buf, int size)
239 status = SHA_Finish(&sha_handle, dest_buf);
240 npcm_sha_check(status);
241 return SHA_Power(false);
244 /*----------------------------------------------------------------------------*/
245 /* Function: SHA_Init */
247 /* Parameters: handlePtr - SHA processing handle pointer */
248 /* Returns: 0 on success or other int error code on error. */
251 /* This routine initialize the SHA module */
252 /*----------------------------------------------------------------------------*/
253 static int SHA_Init(struct SHA_HANDLE_T *handleptr)
255 handleptr->active = false;
260 /*----------------------------------------------------------------------------*/
261 /* Function: SHA_Start */
263 /* Parameters: handlePtr - SHA processing handle pointer */
264 /* type - SHA module type */
266 /* Returns: 0 on success or other int error code on error. */
269 /* This routine start a single SHA process */
270 /*----------------------------------------------------------------------------*/
271 static int SHA_Start(struct SHA_HANDLE_T *handleptr, u8 type)
273 struct npcm_sha_regs *regs = sha_priv->regs;
276 handleptr->length0 = 0;
277 handleptr->length1 = 0;
278 handleptr->type = type;
279 handleptr->active = true;
282 writeb(handleptr->type & HASH_CFG_SHA1_SHA2, ®s->hash_cfg);
284 // Reset SHA hardware
287 /* The handlePtr->hv is initialized with the correct IV as the SHA engine
288 * automatically fill the HASH_DIG_Hn registers according to SHA spec
289 * (following SHA_RST assertion)
291 SHA_GetShaDigest_l((u8 *)handleptr->hv, type);
293 // Init block with zeros
294 memset(handleptr->block, 0, sizeof(handleptr->block));
299 /*----------------------------------------------------------------------------*/
300 /* Function: SHA_Update */
302 /* Parameters: handlePtr - SHA processing handle pointer */
303 /* buffer - Pointer to the data that will be added to */
304 /* the hash calculation */
305 /* len - Length of data to add to SHA calculation */
308 /* Returns: 0 on success or other int error code on error */
311 /* This routine adds data to previously started SHA */
313 /*----------------------------------------------------------------------------*/
314 static int SHA_Update(struct SHA_HANDLE_T *handleptr, const u8 *buffer, u32 len)
316 struct npcm_sha_regs *regs = sha_priv->regs;
317 u32 localbuffer[SHA_SECRUN_BUFF_SIZE / sizeof(u32)];
324 if (!handleptr->active)
327 // Wait till SHA is not busy
328 status = SHA_BusyWait_l();
329 npcm_sha_check(status);
332 writeb(handleptr->type & HASH_CFG_SHA1_SHA2, ®s->hash_cfg);
334 // Write SHA latest digest into SHA module
335 SHA_SetShaDigest_l(handleptr->hv, handleptr->type);
337 // Set number of unhashed bytes which remained from last update
338 pos = SHA_BUFF_POS(handleptr->length0);
340 // Copy unhashed bytes which remained from last update to secrun buffer
341 SHA_SetBlock_l((u8 *)handleptr->block, pos, 0, localbuffer);
344 // Wait for the hardware to be available (in case we are hashing)
345 status = SHA_BusyWait_l();
346 npcm_sha_check(status);
348 // Move as much bytes as we can into the secrun buffer
349 bufferlen = min(len, SHA_BUFF_FREE(handleptr->length0));
351 // Copy current given buffer to the secrun buffer
352 SHA_SetBlock_l((u8 *)buffer, bufferlen, pos, localbuffer);
354 // Update size of hashed bytes
355 handleptr->length0 += bufferlen;
357 if (handleptr->length0 < bufferlen)
358 handleptr->length1++;
360 // Update length of data left to digest
363 // Update given buffer pointer
366 // If secrun buffer is full
367 if (SHA_BUFF_POS(handleptr->length0) == 0) {
368 /* We just filled up the buffer perfectly, so let it hash (we'll
369 * unload the hash only when we are done with all hashing)
371 SHA_FlushLocalBuffer_l(localbuffer);
378 // Wait till SHA is not busy
379 status = SHA_BusyWait_l();
380 npcm_sha_check(status);
382 /* Copy unhashed bytes from given buffer to handle block for next update/finish */
383 blockptr = (u8 *)handleptr->block;
385 blockptr[--bufferlen + pos] = *(--buffer);
387 // Save SHA current digest
388 SHA_GetShaDigest_l((u8 *)handleptr->hv, handleptr->type);
393 /*----------------------------------------------------------------------------*/
394 /* Function: SHA_Finish */
396 /* Parameters: handlePtr - SHA processing handle pointer */
397 /* hashDigest - Pointer to a buffer where the final digest */
398 /* will be copied to */
400 /* Returns: 0 on success or other int error code on error */
403 /* This routine finish SHA calculation and get */
404 /* the resulting SHA digest */
405 /*----------------------------------------------------------------------------*/
406 static int SHA_Finish(struct SHA_HANDLE_T *handleptr, u8 *hashdigest)
408 struct npcm_sha_regs *regs = sha_priv->regs;
409 u32 localbuffer[SHA_SECRUN_BUFF_SIZE / sizeof(u32)];
410 const u8 lastbyte = SHA_DATA_LAST_BYTE;
415 if (!handleptr->active)
419 writeb(handleptr->type & HASH_CFG_SHA1_SHA2, ®s->hash_cfg);
421 // Wait till SHA is not busy
422 status = SHA_BusyWait_l();
423 npcm_sha_check(status);
425 // Finish off the current buffer with the SHA spec'ed padding
426 pos = SHA_BUFF_POS(handleptr->length0);
429 SHA_SetShaDigest_l(handleptr->hv, handleptr->type);
431 // Load data into secrun buffer
432 SHA_SetBlock_l((u8 *)handleptr->block, pos, 0, localbuffer);
434 // Set data last byte as in SHA algorithm spec
435 SHA_SetBlock_l(&lastbyte, 1, pos++, localbuffer);
437 // If the remainder of data is longer then one block
438 if (pos > (SHA_BLOCK_LENGTH - 8)) {
439 /* The length will be in the next block Pad the rest of the last block with 0's */
440 SHA_ClearBlock_l((SHA_BLOCK_LENGTH - pos), pos, localbuffer);
442 // Hash the current block
443 SHA_FlushLocalBuffer_l(localbuffer);
447 // Wait till SHA is not busy
448 status = SHA_BusyWait_l();
449 npcm_sha_check(status);
452 // Pad the rest of the last block with 0's except for the last 8-3 bytes
453 SHA_ClearBlock_l((SHA_BLOCK_LENGTH - (8 - 3)) - pos, pos, localbuffer);
455 /* The last 8-3 bytes are set to the bit-length of the message in big-endian form */
456 SHA_SetLength32_l(handleptr, localbuffer);
458 // Hash all that, and save the hash for the caller
459 SHA_FlushLocalBuffer_l(localbuffer);
461 // Wait till SHA is not busy
462 status = SHA_BusyWait_l();
463 npcm_sha_check(status);
465 // Save SHA final digest into given buffer
466 SHA_GetShaDigest_l(hashdigest, handleptr->type);
469 handleptr->active = false;
474 /*----------------------------------------------------------------------------*/
475 /* Function: SHA_Reset */
477 /* Parameters: none */
481 /* This routine reset SHA module */
482 /*----------------------------------------------------------------------------*/
483 static int SHA_Reset(void)
485 struct npcm_sha_regs *regs = sha_priv->regs;
487 writel(readl(®s->hash_ctr_sts) | HASH_CTR_STS_SHA_RST, ®s->hash_ctr_sts);
492 /*----------------------------------------------------------------------------*/
493 /* Function: SHA_Power */
495 /* Parameters: on - true enable the module, false disable the module */
499 /* This routine set SHA module power on/off */
500 /*----------------------------------------------------------------------------*/
501 static int SHA_Power(bool on)
503 struct npcm_sha_regs *regs = sha_priv->regs;
506 hash_sts = readb(®s->hash_ctr_sts) & ~HASH_CTR_STS_SHA_EN;
507 writeb(hash_sts | (on & HASH_CTR_STS_SHA_EN), ®s->hash_ctr_sts);
513 /*----------------------------------------------------------------------------*/
514 /* Function: SHA_PrintRegs */
516 /* Parameters: none */
520 /* This routine prints the module registers */
521 /*----------------------------------------------------------------------------*/
522 static void SHA_PrintRegs(void)
524 #ifdef SHA_DEBUG_MODULE
525 struct npcm_sha_regs *regs = sha_priv->regs;
529 sha_print("/*--------------*/\n");
530 sha_print("/* SHA */\n");
531 sha_print("/*--------------*/\n\n");
533 sha_print("HASH_CTR_STS = 0x%02X\n", readb(®s->hash_ctr_sts));
534 sha_print("HASH_CFG = 0x%02X\n", readb(®s->hash_cfg));
536 for (i = 0; i < HASH_DIG_H_NUM; i++)
537 sha_print("HASH_DIG_H%d = 0x%08X\n", i, readl(®s->hash_dig[i]));
539 sha_print("HASH_VER = 0x%08X\n", readb(®s->hash_ver));
544 /*----------------------------------------------------------------------------*/
545 /* Function: SHA_PrintVersion */
547 /* Parameters: none */
551 /* This routine prints the module version */
552 /*----------------------------------------------------------------------------*/
553 static void SHA_PrintVersion(void)
555 struct npcm_sha_regs *regs = sha_priv->regs;
557 printf("SHA MODULE VER = %d\n", readb(®s->hash_ver));
561 /*----------------------------------------------------------------------------*/
562 /* Function: npcm_sha_selftest */
564 /* Parameters: type - SHA module type */
565 /* Returns: 0 on success or other int error code on error */
568 /* This routine performs various tests on the SHA HW and SW */
569 /*----------------------------------------------------------------------------*/
570 int npcm_sha_selftest(u8 type)
573 struct SHA_HANDLE_T handle;
574 u8 hashdigest[max(SHA_1_HASH_LENGTH, SHA_2_HASH_LENGTH)];
577 /*------------------------------------------------------------------------*/
578 /* SHA1 tests info */
579 /*------------------------------------------------------------------------*/
581 static const u8 sha1selftestbuff[SHA1_NUM_OF_SELF_TESTS][94] = {
583 {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
584 {"0123456789012345678901234567890123456789012345678901234567890123"},
585 {0x30, 0x5c, 0x30, 0x2c, 0x02, 0x01, 0x00, 0x30, 0x09, 0x06, 0x05, 0x2b,
586 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x30, 0x06, 0x06, 0x04, 0x67, 0x2a,
587 0x01, 0x0c, 0x04, 0x14, 0xe1, 0xb6, 0x93, 0xfe, 0x33, 0x43, 0xc1, 0x20,
588 0x5d, 0x4b, 0xaa, 0xb8, 0x63, 0xfb, 0xcf, 0x6c, 0x46, 0x1e, 0x88, 0x04,
589 0x30, 0x2c, 0x02, 0x01, 0x00, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
590 0x02, 0x1a, 0x05, 0x00, 0x30, 0x06, 0x06, 0x04, 0x67, 0x2a, 0x01, 0x0c,
591 0x04, 0x14, 0x13, 0xc1, 0x0c, 0xfc, 0xc8, 0x92, 0xd7, 0xde, 0x07, 0x1c,
592 0x40, 0xde, 0x4f, 0xcd, 0x07, 0x5b, 0x68, 0x20, 0x5a, 0x6c}
595 static const u8 sha1selftestbufflen[SHA1_NUM_OF_SELF_TESTS] = {
599 static const u8 sha1selftestexpres[SHA1_NUM_OF_SELF_TESTS][SHA_1_HASH_LENGTH] = {
600 {0xA9, 0x99, 0x3E, 0x36,
601 0x47, 0x06, 0x81, 0x6A,
602 0xBA, 0x3E, 0x25, 0x71,
603 0x78, 0x50, 0xC2, 0x6C,
604 0x9C, 0xD0, 0xD8, 0x9D},
605 {0x84, 0x98, 0x3E, 0x44,
606 0x1C, 0x3B, 0xD2, 0x6E,
607 0xBA, 0xAE, 0x4A, 0xA1,
608 0xF9, 0x51, 0x29, 0xE5,
609 0xE5, 0x46, 0x70, 0xF1},
610 {0xCF, 0x08, 0x00, 0xF7,
611 0x64, 0x4A, 0xCE, 0x3C,
612 0xB4, 0xC3, 0xFA, 0x33,
613 0x38, 0x8D, 0x3B, 0xA0,
614 0xEA, 0x3C, 0x8B, 0x6E},
615 {0xc9, 0x84, 0x45, 0xc8,
616 0x64, 0x04, 0xb1, 0xe3,
617 0x3c, 0x6b, 0x0a, 0x8c,
618 0x8b, 0x80, 0x94, 0xfc,
619 0xf3, 0xc9, 0x98, 0xab}
622 /*------------------------------------------------------------------------*/
623 /* SHA2 tests info */
624 /*------------------------------------------------------------------------*/
626 static const u8 sha2selftestbuff[SHA2_NUM_OF_SELF_TESTS][100] = {
628 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
629 {'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
630 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
631 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
632 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
633 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
634 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
635 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
636 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
637 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
638 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'}
641 static const u8 sha2selftestbufflen[SHA2_NUM_OF_SELF_TESTS] = {
645 static const u8 sha2selftestexpres[SHA2_NUM_OF_SELF_TESTS][SHA_2_HASH_LENGTH] = {
647 * SHA-256 test vectors
649 { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
650 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
651 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
652 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
653 { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
654 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
655 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
656 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
657 { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
658 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
659 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
660 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 },
663 if (type == npcm_sha_type_sha1) {
664 /*--------------------------------------------------------------------*/
666 /*--------------------------------------------------------------------*/
667 for (i = 0; i < SHA1_NUM_OF_SELF_TESTS; i++) {
669 status = npcm_sha_calc(npcm_sha_type_sha1, sha1selftestbuff[i], sha1selftestbufflen[i], hashdigest);
670 npcm_sha_check(status);
674 status = SHA_Start(&handle, npcm_sha_type_sha1);
675 npcm_sha_check(status);
676 status = SHA_Update(&handle, sha1selftestbuff[i], 73);
677 npcm_sha_check(status);
678 status = SHA_Update(&handle, &sha1selftestbuff[i][73], sha1selftestbufflen[i] - 73);
679 npcm_sha_check(status);
680 status = SHA_Finish(&handle, hashdigest);
681 npcm_sha_check(status);
685 if (memcmp(hashdigest, sha1selftestexpres[i], SHA_1_HASH_LENGTH))
690 /*--------------------------------------------------------------------*/
692 /*--------------------------------------------------------------------*/
693 for (i = 0; i < SHA2_NUM_OF_SELF_TESTS; i++) {
696 status = SHA_Start(&handle, npcm_sha_type_sha2);
697 npcm_sha_check(status);
699 for (j = 0; j < 10000; j++) { //not working
700 status = SHA_Update(&handle, sha2selftestbuff[i], sha2selftestbufflen[i]);
701 npcm_sha_check(status);
704 status = SHA_Update(&handle, sha2selftestbuff[i], sha2selftestbufflen[i]);
705 npcm_sha_check(status);
708 status = SHA_Finish(&handle, hashdigest);
709 npcm_sha_check(status);
711 if (memcmp(hashdigest, sha2selftestexpres[i], SHA_2_HASH_LENGTH))
714 npcm_sha_calc(npcm_sha_type_sha2, sha2selftestbuff[i], sha2selftestbufflen[i], hashdigest);
715 if (memcmp(hashdigest, sha2selftestexpres[i], SHA_2_HASH_LENGTH))
723 /*----------------------------------------------------------------------------*/
724 /* Function: SHA_FlushLocalBuffer_l */
729 /* Description: This routine flush secrun buffer to SHA module */
730 /*----------------------------------------------------------------------------*/
731 static void SHA_FlushLocalBuffer_l(const u32 *buff)
733 struct npcm_sha_regs *regs = sha_priv->regs;
736 for (i = 0; i < (SHA_BLOCK_LENGTH / sizeof(u32)); i++)
737 writel(buff[i], ®s->hash_data_in);
740 /*----------------------------------------------------------------------------*/
741 /* Function: SHA_BusyWait_l */
744 /* Returns: 0 if no error was found or DEFS_STATUS_ERROR otherwise */
746 /* Description: This routine wait for SHA unit to no longer be busy */
747 /*----------------------------------------------------------------------------*/
748 static int SHA_BusyWait_l(void)
750 struct npcm_sha_regs *regs = sha_priv->regs;
751 u32 timeout = SHA_TIMEOUT;
756 } while ((readb(®s->hash_ctr_sts) & HASH_CTR_STS_SHA_BUSY)
757 == HASH_CTR_STS_SHA_BUSY);
762 /*----------------------------------------------------------------------------*/
763 /* Function: SHA_GetShaDigest_l */
765 /* Parameters: hashDigest - buffer for the hash output. */
766 /* type - SHA module type */
769 /* Description: This routine copy the hash digest from the hardware */
770 /* and into given buffer (in ram) */
771 /*----------------------------------------------------------------------------*/
772 static void SHA_GetShaDigest_l(u8 *hashdigest, u8 type)
774 struct npcm_sha_regs *regs = sha_priv->regs;
776 u8 len = SHA_HASH_LENGTH(type) / sizeof(u32);
778 // Copy Bytes from SHA module to given buffer
779 for (j = 0; j < len; j++)
780 ((u32 *)hashdigest)[j] = readl(®s->hash_dig[j]);
783 /*----------------------------------------------------------------------------*/
784 /* Function: SHA_SetShaDigest_l */
786 /* Parameters: hashDigest - input buffer to set as hash digest */
787 /* type - SHA module type */
790 /* Description: This routine set the hash digest in the hardware from */
791 /* a given buffer (in ram) */
792 /*----------------------------------------------------------------------------*/
793 static void SHA_SetShaDigest_l(const u32 *hashdigest, u8 type)
795 struct npcm_sha_regs *regs = sha_priv->regs;
797 u8 len = SHA_HASH_LENGTH(type) / sizeof(u32);
799 // Copy Bytes from given buffer to SHA module
800 for (j = 0; j < len; j++)
801 writel(hashdigest[j], ®s->hash_dig[j]);
804 /*----------------------------------------------------------------------------*/
805 /* Function: SHA_SetBlock_l */
807 /* Parameters: data - data to copy */
808 /* len - size of data */
809 /* position - byte offset into the block at which data */
810 /* should be placed */
811 /* block - block buffer */
814 /* Description: This routine load bytes into block buffer */
815 /*----------------------------------------------------------------------------*/
816 static void SHA_SetBlock_l(const u8 *data, u32 len, u16 position, u32 *block)
818 u8 *dest = (u8 *)block;
820 memcpy(dest + position, data, len);
823 /*----------------------------------------------------------------------------*/
824 /* Function: SHA_SetBlock_l */
827 /* len - size of data */
828 /* position - byte offset into the block at which data */
829 /* should be placed */
830 /* block - block buffer */
833 /* Description: This routine load zero's into the block buffer */
834 /*----------------------------------------------------------------------------*/
835 static void SHA_ClearBlock_l(u16 len, u16 position, u32 *block)
837 u8 *dest = (u8 *)block;
839 memset(dest + position, 0, len);
842 /*----------------------------------------------------------------------------*/
843 /* Function: SHA_SetLength32_l */
846 /* handlePtr - SHA processing handle pointer */
847 /* block - block buffer */
850 /* Description: This routine set the length of the hash's data */
851 /* len is the 32-bit byte length of the message */
852 /*lint -efunc(734,SHA_SetLength32_l) Supperess loss of percision lint warning */
853 /*----------------------------------------------------------------------------*/
854 static void SHA_SetLength32_l(struct SHA_HANDLE_T *handleptr, u32 *block)
856 u16 *secrunbufferswappedptr = (u16 *)(void *)(block);
858 secrunbufferswappedptr[(SHA_BLOCK_LENGTH / sizeof(u16)) - 1] = (u16)
859 ((handleptr->length0 << 3) << 8) | ((u16)(handleptr->length0 << 3) >> 8);
860 secrunbufferswappedptr[(SHA_BLOCK_LENGTH / sizeof(u16)) - 2] = (u16)
861 ((handleptr->length0 >> (16 - 3)) >> 8) | ((u16)(handleptr->length0 >> (16 - 3)) << 8);
862 secrunbufferswappedptr[(SHA_BLOCK_LENGTH / sizeof(u16)) - 3] = (u16)
863 ((handleptr->length1 << 3) << 8) | ((u16)(handleptr->length1 << 3) >> 8);
864 secrunbufferswappedptr[(SHA_BLOCK_LENGTH / sizeof(u16)) - 4] = (u16)
865 ((handleptr->length1 >> (16 - 3)) >> 8) | ((u16)(handleptr->length1 >> (16 - 3)) << 8);
868 static int npcm_sha_bind(struct udevice *dev)
870 sha_priv = calloc(1, sizeof(struct npcm_sha_priv));
874 sha_priv->regs = dev_remap_addr_index(dev, 0);
875 if (!sha_priv->regs) {
876 printf("Cannot find sha reg address, binding failed\n");
880 printf("SHA: NPCM SHA module bind OK\n");
885 static const struct udevice_id npcm_sha_ids[] = {
886 { .compatible = "nuvoton,npcm845-sha" },
887 { .compatible = "nuvoton,npcm750-sha" },
891 U_BOOT_DRIVER(npcm_sha) = {
894 .of_match = npcm_sha_ids,
895 .priv_auto = sizeof(struct npcm_sha_priv),
896 .bind = npcm_sha_bind,