53 #define SHA_BLOCK_SIZE (64) 55 #define SHARED_MEM_BASE (0xd0000) 58 #define SHA256_MEM_BASE (0x180000UL) 59 #define SHA256_ENGINE_ADDR (0x180000ul) 62 #define SHA256_CTRL (SHA256_MEM_BASE+0x00) 63 #define SHA256_CTRL_START_CALC_MASK (NBIT0) 64 #define SHA256_CTRL_START_CALC_SHIFT (0) 65 #define SHA256_CTRL_PREPROCESS_MASK (NBIT1) 66 #define SHA256_CTRL_PREPROCESS_SHIFT (1) 67 #define SHA256_CTRL_HASH_HASH_MASK (NBIT2) 68 #define SHA256_CTRL_HASH_HASH_SHIFT (2) 69 #define SHA256_CTRL_INIT_SHA256_STATE_MASK (NBIT3) 70 #define SHA256_CTRL_INIT_SHA256_STATE_SHIFT (3) 71 #define SHA256_CTRL_WR_BACK_HASH_VALUE_MASK (NBIT4) 72 #define SHA256_CTRL_WR_BACK_HASH_VALUE_SHIFT (4) 73 #define SHA256_CTRL_FORCE_SHA256_QUIT_MASK (NBIT5) 74 #define SHA256_CTRL_FORCE_SHA256_QUIT_SHIFT (5) 76 #define SHA256_REGS_SHA256_CTRL_AHB_BYTE_REV_EN (NBIT6) 77 #define SHA256_REGS_SHA256_CTRL_RESERVED (NBIT7) 78 #define SHA256_REGS_SHA256_CTRL_CORE_TO_AHB_CLK_RATIO (NBIT8+ NBIT9+ NBIT10) 79 #define SHA256_REGS_SHA256_CTRL_CORE_TO_AHB_CLK_RATIO_MASK (NBIT2+ NBIT1+ NBIT0) 80 #define SHA256_REGS_SHA256_CTRL_CORE_TO_AHB_CLK_RATIO_SHIFT (8) 81 #define SHA256_REGS_SHA256_CTRL_RESERVED_11 (NBIT11) 82 #define SHA256_REGS_SHA256_CTRL_SHA1_CALC (NBIT12) 83 #define SHA256_REGS_SHA256_CTRL_PBKDF2_SHA1_CALC (NBIT13) 86 #define SHA256_START_RD_ADDR (SHA256_MEM_BASE+0x04UL) 87 #define SHA256_DATA_LENGTH (SHA256_MEM_BASE+0x08UL) 88 #define SHA256_START_WR_ADDR (SHA256_MEM_BASE+0x0cUL) 89 #define SHA256_COND_CHK_CTRL (SHA256_MEM_BASE+0x10) 90 #define SHA256_COND_CHK_CTRL_HASH_VAL_COND_CHK_MASK (NBIT1 | NBIT0) 91 #define SHA256_COND_CHK_CTRL_HASH_VAL_COND_CHK_SHIFT (0) 92 #define SHA256_COND_CHK_CTRL_STEP_VAL_MASK (NBIT6 | NBIT5 | NBIT4 | NBIT3 | NBIT2) 93 #define SHA256_COND_CHK_CTRL_STEP_VAL_SHIFT (2) 94 #define SHA256_COND_CHK_CTRL_COND_CHK_RESULT_MASK (NBIT7) 95 #define SHA256_COND_CHK_CTRL_COND_CHK_RESULT_SHIFT (7) 97 #define SHA256_MOD_DATA_RANGE (SHA256_MEM_BASE+0x14) 98 #define SHA256_MOD_DATA_RANGE_ST_BYTE_2_ADD_STP_MASK (NBIT24-1) 99 #define SHA256_MOD_DATA_RANGE_ST_BYTE_2_ADD_STP_SHIFT (0) 100 #define SHA256_MOD_DATA_RANGE_MOD_DATA_LEN_MASK (NBIT24 | NBIT25| NBIT26) 101 #define SHA256_MOD_DATA_RANGE_MOD_DATA_LEN_SHIFT (24) 104 #define SHA256_COND_CHK_STS_1 (SHA256_MEM_BASE+0x18) 105 #define SHA256_COND_CHK_STS_2 (SHA256_MEM_BASE+0x1c) 106 #define SHA256_DONE_INTR_ENABLE (SHA256_MEM_BASE+0x20) 107 #define SHA256_DONE_INTR_STS (SHA256_MEM_BASE+0x24) 108 #define SHA256_TARGET_HASH_H1 (SHA256_MEM_BASE+0x28) 109 #define SHA256_TARGET_HASH_H2 (SHA256_MEM_BASE+0x2c) 110 #define SHA256_TARGET_HASH_H3 (SHA256_MEM_BASE+0x30) 111 #define SHA256_TARGET_HASH_H4 (SHA256_MEM_BASE+0x34) 112 #define SHA256_TARGET_HASH_H5 (SHA256_MEM_BASE+0x38) 113 #define SHA256_TARGET_HASH_H6 (SHA256_MEM_BASE+0x3c) 114 #define SHA256_TARGET_HASH_H7 (SHA256_MEM_BASE+0x40) 115 #define SHA256_TARGET_HASH_H8 (SHA256_MEM_BASE+0x44) 122 #define BIGINT_ENGINE_ADDR (0x180080ul) 123 #define BIGINT_VERSION (BIGINT_ENGINE_ADDR + 0x00) 125 #define BIGINT_MISC_CTRL (BIGINT_ENGINE_ADDR + 0x04) 126 #define BIGINT_MISC_CTRL_CTL_START (NBIT0) 127 #define BIGINT_MISC_CTRL_CTL_RESET (NBIT1) 128 #define BIGINT_MISC_CTRL_CTL_MSW_FIRST (NBIT2) 129 #define BIGINT_MISC_CTRL_CTL_SWAP_BYTE_ORDER (NBIT3) 130 #define BIGINT_MISC_CTRL_CTL_FORCE_BARRETT (NBIT4) 131 #define BIGINT_MISC_CTRL_CTL_M_PRIME_VALID (NBIT5) 133 #define BIGINT_M_PRIME (BIGINT_ENGINE_ADDR + 0x08) 135 #define BIGINT_STATUS (BIGINT_ENGINE_ADDR + 0x0C) 136 #define BIGINT_STATUS_STS_DONE (NBIT0) 138 #define BIGINT_CLK_COUNT (BIGINT_ENGINE_ADDR + 0x10) 139 #define BIGINT_ADDR_X (BIGINT_ENGINE_ADDR + 0x14) 140 #define BIGINT_ADDR_E (BIGINT_ENGINE_ADDR + 0x18) 141 #define BIGINT_ADDR_M (BIGINT_ENGINE_ADDR + 0x1C) 142 #define BIGINT_ADDR_R (BIGINT_ENGINE_ADDR + 0x20) 143 #define BIGINT_LENGTH (BIGINT_ENGINE_ADDR + 0x24) 145 #define BIGINT_IRQ_STS (BIGINT_ENGINE_ADDR + 0x28) 146 #define BIGINT_IRQ_STS_DONE (NBIT0) 147 #define BIGINT_IRQ_STS_CHOOSE_MONT (NBIT1) 148 #define BIGINT_IRQ_STS_M_READ (NBIT2) 149 #define BIGINT_IRQ_STS_X_READ (NBIT3) 150 #define BIGINT_IRQ_STS_START (NBIT4) 151 #define BIGINT_IRQ_STS_PRECOMP_FINISH (NBIT5) 153 #define BIGINT_IRQ_MASK (BIGINT_ENGINE_ADDR + 0x2C) 154 #define BIGINT_IRQ_MASK_CTL_IRQ_MASK_START (NBIT4) 156 #define ENABLE_FLIPPING 1 161 #define GET_UINT32(BUF,OFFSET) (((uint32)((BUF)[OFFSET])) | ((uint32)(((BUF)[OFFSET + 1]) << 8)) | \ 162 ((uint32)(((BUF)[OFFSET + 2]) << 16)) | ((uint32)(((BUF)[OFFSET + 3]) << 24))) 164 #define PUTU32(VAL32,BUF,OFFSET) \ 167 (BUF)[OFFSET ] = BYTE_3((VAL32)); \ 168 (BUF)[OFFSET +1 ] = BYTE_2((VAL32)); \ 169 (BUF)[OFFSET +2 ] = BYTE_1((VAL32)); \ 170 (BUF)[OFFSET +3 ] = BYTE_0((VAL32)); \ 186 uint8 au8CurrentBlock[64];
188 uint8 u8InitHashFlag;
199 tstrSHA256HashCtxt *pstrSHA256 = (tstrSHA256HashCtxt*)pstrSha256Ctxt;
200 if(pstrSHA256 !=
NULL)
203 pstrSHA256->u8InitHashFlag = 1;
211 tstrSHA256HashCtxt *pstrSHA256 = (tstrSHA256HashCtxt*)pstrSha256Ctxt;
212 if(pstrSHA256 !=
NULL)
215 uint32 u32WriteAddr = SHARED_MEM_BASE;
216 uint32 u32Addr = u32WriteAddr;
220 uint32 u32CurrentBlock = 0;
224 u32ResidualBytes = pstrSHA256->u32TotalLength % SHA_BLOCK_SIZE;
227 pstrSHA256->u32TotalLength += u16DataLength;
229 if(u32ResidualBytes != 0)
231 if((u32ResidualBytes + u16DataLength) >= SHA_BLOCK_SIZE)
233 u32Offset = SHA_BLOCK_SIZE - u32ResidualBytes;
234 m2m_memcpy(&pstrSHA256->au8CurrentBlock[u32ResidualBytes], pu8Data, u32Offset);
235 pu8Data += u32Offset;
236 u16DataLength -= u32Offset;
238 nm_write_block(u32Addr, pstrSHA256->au8CurrentBlock, SHA_BLOCK_SIZE);
239 u32Addr += SHA_BLOCK_SIZE;
244 m2m_memcpy(&pstrSHA256->au8CurrentBlock[u32ResidualBytes], pu8Data, u16DataLength);
250 u32NBlocks = u16DataLength / SHA_BLOCK_SIZE;
251 u32ResidualBytes = u16DataLength % SHA_BLOCK_SIZE;
256 pu8Data += (u32NBlocks * SHA_BLOCK_SIZE);
259 u32NBlocks += u32CurrentBlock;
265 u32RegVal |= SHA256_CTRL_FORCE_SHA256_QUIT_MASK;
268 if(pstrSHA256->u8InitHashFlag)
270 pstrSHA256->u8InitHashFlag = 0;
271 u32RegVal |= SHA256_CTRL_INIT_SHA256_STATE_MASK;
274 u32ReadAddr = u32WriteAddr + (u32NBlocks * SHA_BLOCK_SIZE);
275 nm_write_reg(SHA256_DATA_LENGTH, (u32NBlocks * SHA_BLOCK_SIZE));
279 u32RegVal |= SHA256_CTRL_START_CALC_MASK;
281 u32RegVal &= ~(0x7 << 8);
282 u32RegVal |= (2 << 8);
290 u8IsDone = u32RegVal &
NBIT0;
293 if(u32ResidualBytes != 0)
295 m2m_memcpy(pstrSHA256->au8CurrentBlock, pu8Data, u32ResidualBytes);
306 tstrSHA256HashCtxt *pstrSHA256 = (tstrSHA256HashCtxt*)pstrSha256Ctxt;
307 if(pstrSHA256 !=
NULL)
310 uint32 u32WriteAddr = SHARED_MEM_BASE;
311 uint32 u32Addr = u32WriteAddr;
321 u32RegVal |= SHA256_CTRL_FORCE_SHA256_QUIT_MASK;
324 if(pstrSHA256->u8InitHashFlag)
326 pstrSHA256->u8InitHashFlag = 0;
327 u32RegVal |= SHA256_CTRL_INIT_SHA256_STATE_MASK;
331 u16Offset = (
uint16)(pstrSHA256->u32TotalLength % SHA_BLOCK_SIZE);
334 pstrSHA256->au8CurrentBlock[u16Offset ++] = 0x80;
339 u16PaddingLength = SHA_BLOCK_SIZE - u16Offset;
340 m2m_memset(&pstrSHA256->au8CurrentBlock[u16Offset], 0, u16PaddingLength);
345 if(u16PaddingLength < 8)
347 nm_write_block(u32Addr, pstrSHA256->au8CurrentBlock, SHA_BLOCK_SIZE);
348 u32Addr += SHA_BLOCK_SIZE;
349 m2m_memset(pstrSHA256->au8CurrentBlock, 0, SHA_BLOCK_SIZE);
354 PUTU32(pstrSHA256->u32TotalLength << 3, pstrSHA256->au8CurrentBlock, (SHA_BLOCK_SIZE - 4));
356 u32ReadAddr = u32WriteAddr + (u16NBlocks * SHA_BLOCK_SIZE);
357 nm_write_block(u32Addr, pstrSHA256->au8CurrentBlock, SHA_BLOCK_SIZE);
358 nm_write_reg(SHA256_DATA_LENGTH, (u16NBlocks * SHA_BLOCK_SIZE));
362 u32RegVal |= SHA256_CTRL_START_CALC_MASK;
363 u32RegVal |= SHA256_CTRL_WR_BACK_HASH_VALUE_MASK;
364 u32RegVal &= ~(0x7UL << 8);
365 u32RegVal |= (0x2UL << 8);
374 u8IsDone = u32RegVal &
NBIT0;
383 pu8Sha256Digest[u32ByteIdx ++] =
BYTE_3(au32Digest[u32Idx]);
384 pu8Sha256Digest[u32ByteIdx ++] =
BYTE_2(au32Digest[u32Idx]);
385 pu8Sha256Digest[u32ByteIdx ++] =
BYTE_1(au32Digest[u32Idx]);
386 pu8Sha256Digest[u32ByteIdx ++] =
BYTE_0(au32Digest[u32Idx]);
398 static void FlipBuffer(
uint8 *pu8InBuffer,
uint8 *pu8OutBuffer,
uint16 u16BufferSize)
401 for(u16Idx = 0; u16Idx < u16BufferSize; u16Idx ++)
403 #if ENABLE_FLIPPING == 1 404 pu8OutBuffer[u16Idx] = pu8InBuffer[u16BufferSize - u16Idx - 1];
406 pu8OutBuffer[u16Idx] = pu8InBuffer[u16Idx];
420 uint8 au8Tmp[780] = {0};
421 uint32 u32XAddr = SHARED_MEM_BASE;
425 uint8 u8EMswBits = 32;
427 uint16 u16XSizeWords,u16ESizeWords;
430 u16XSizeWords = (u16XSize + 3) / 4;
431 u16ESizeWords = (u16ESize + 3) / 4;
433 u32MAddr = u32XAddr + (u16XSizeWords * 4);
434 u32EAddr = u32MAddr + (u16XSizeWords * 4);
435 u32RAddr = u32EAddr + (u16ESizeWords * 4);
440 u32Reg |= BIGINT_MISC_CTRL_CTL_RESET;
442 u32Reg &= ~BIGINT_MISC_CTRL_CTL_RESET;
450 FlipBuffer(pu8X,au8Tmp,u16XSize);
455 FlipBuffer(pu8E, au8Tmp, u16ESize);
457 u32Exponent =
GET_UINT32(au8Tmp, (u16ESizeWords * 4) - 4);
458 while((u32Exponent &
NBIT31)== 0)
466 FlipBuffer(pu8M, au8Tmp, u16XSize);
482 u32Reg = (u16XSizeWords & 0xFF);
483 u32Reg += ((u16ESizeWords & 0xFF) << 8);
484 u32Reg += (u8EMswBits << 16);
490 u32Reg ^= BIGINT_MISC_CTRL_CTL_START;
491 u32Reg |= BIGINT_MISC_CTRL_CTL_FORCE_BARRETT;
493 #if ENABLE_FLIPPING == 0 494 u32Reg |= BIGINT_MISC_CTRL_CTL_MSW_FIRST;
502 if(u32Reg & BIGINT_IRQ_STS_DONE)
510 FlipBuffer(au8Tmp, pu8R, u16RSize);
515 #define MD5_DIGEST_SIZE (16) 516 #define SHA1_DIGEST_SIZE (20) 518 static const uint8 au8TEncodingMD5[] =
520 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86,
521 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00,
528 static const uint8 au8TEncodingSHA1[] =
530 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E,
531 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04
537 static const uint8 au8TEncodingSHA2[] =
539 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86,
540 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
552 if((pu8N !=
NULL) && (pu8E !=
NULL) && (pu8RsaSignature !=
NULL) && (pu8SignedMsgHash !=
NULL))
554 uint16 u16TLength, u16TEncodingLength;
560 if(u16HashLength == MD5_DIGEST_SIZE)
562 pu8T = (
uint8*)au8TEncodingMD5;
563 u16TEncodingLength =
sizeof(au8TEncodingMD5);
565 else if(u16HashLength == SHA1_DIGEST_SIZE)
567 pu8T = (
uint8*)au8TEncodingSHA1;
568 u16TEncodingLength =
sizeof(au8TEncodingSHA1);
572 pu8T = (
uint8*)au8TEncodingSHA2;
573 u16TEncodingLength =
sizeof(au8TEncodingSHA2);
575 u16TLength = u16TEncodingLength + 1 + u16HashLength;
579 if(u16NSize >= (u16TLength + 11))
581 uint32 u32PSLength,u32Idx = 0;
586 BigInt_ModExp(pu8RsaSignature, u16NSize, pu8E, u16ESize, pu8N, u16NSize, au8EM, u16NSize);
588 u32PSLength = u16NSize - u16TLength - 3;
598 if((au8EM[0] == 0x00) && (au8EM[1] == 0x01))
600 for(u32Idx = 2; au8EM[u32Idx] == 0xFF; u32Idx ++);
601 if(u32Idx == (u32PSLength + 2))
603 if(au8EM[u32Idx ++] == 0x00)
605 if(!
m2m_memcmp(&au8EM[u32Idx], pu8T, u16TEncodingLength))
607 u32Idx += u16TEncodingLength;
608 if(au8EM[u32Idx ++] == u16HashLength)
609 s8Ret =
m2m_memcmp(&au8EM[u32Idx], pu8SignedMsgHash, u16HashLength);
625 if((pu8N !=
NULL) && (pu8d !=
NULL) && (pu8RsaSignature !=
NULL) && (pu8SignedMsgHash !=
NULL))
627 uint16 u16TLength, u16TEncodingLength;
633 if(u16HashLength == MD5_DIGEST_SIZE)
635 pu8T = (
uint8*)au8TEncodingMD5;
636 u16TEncodingLength =
sizeof(au8TEncodingMD5);
638 else if(u16HashLength == SHA1_DIGEST_SIZE)
640 pu8T = (
uint8*)au8TEncodingSHA1;
641 u16TEncodingLength =
sizeof(au8TEncodingSHA1);
645 pu8T = (
uint8*)au8TEncodingSHA2;
646 u16TEncodingLength =
sizeof(au8TEncodingSHA2);
648 u16TLength = u16TEncodingLength + 1 + u16HashLength;
652 if(u16NSize >= (u16TLength + 11))
665 au8EM[u16Offset ++] = 0;
666 au8EM[u16Offset ++] = 1;
667 u16PSLength = u16NSize - u16TLength - 3;
668 m2m_memset(&au8EM[u16Offset], 0xFF, u16PSLength);
669 u16Offset += u16PSLength;
670 au8EM[u16Offset ++] = 0;
671 m2m_memcpy(&au8EM[u16Offset], pu8T, u16TEncodingLength);
672 u16Offset += u16TEncodingLength;
673 au8EM[u16Offset ++] = u16HashLength;
674 m2m_memcpy(&au8EM[u16Offset], pu8SignedMsgHash, u16HashLength);
679 BigInt_ModExp(au8EM, u16NSize, pu8d, u16dSize, pu8N, u16NSize, pu8RsaSignature, u16NSize);
NMI_API void m2m_memcpy(uint8 *pDst, uint8 *pSrc, uint32 sz)
Copy specified number of bytes from source buffer to destination buffer.
sint8 nm_read_block(uint32 u32Addr, uint8 *puBuf, uint32 u32Sz)
signed char sint8
Range of values between -128 to 127.
NMI_API sint8 m2m_memcmp(uint8 *pu8Buff1, uint8 *pu8Buff2, uint32 u32Size)
Compare specified number of data bytes in pu8Buff1 and pu8Buff2 and decide if they all match...
sint8 nm_write_block(uint32 u32Addr, uint8 *puBuf, uint32 u32Sz)
unsigned short uint16
Range of values between 0 to 65535.
sint8 m2m_sha256_hash_init(tstrM2mSha256Ctxt *psha256Ctxt)
SHA256 hash initialization.
This module contains WINC3400 ASIC specific internal APIs.
#define M2M_SHA256_DIGEST_LEN
NMI_API void m2m_memset(uint8 *pBuf, uint8 val, uint32 sz)
Set specified number of data bytes in specified data buffer to specified value.
sint8 nm_write_reg(uint32 u32Addr, uint32 u32Val)
This module contains WINC3400 bus APIs implementation.
sint8 m2m_sha256_hash_update(tstrM2mSha256Ctxt *psha256Ctxt, uint8 *pu8Data, uint16 u16DataLength)
SHA256 hash update.
sint8 m2m_rsa_sign_verify(uint8 *pu8N, uint16 u16NSize, uint8 *pu8E, uint16 u16ESize, uint8 *pu8SignedMsgHash, uint16 u16HashLength, uint8 *pu8RsaSignature)
RSA Signature Verification.
unsigned long uint32
Range of values between 0 to 4294967295.
unsigned char uint8
Range of values between 0 to 255.
sint8 m2m_sha256_hash_finish(tstrM2mSha256Ctxt *psha256Ctxt, uint8 *pu8Sha256Digest)
SHA256 hash finalization.
sint8 m2m_rsa_sign_gen(uint8 *pu8N, uint16 u16NSize, uint8 *pu8d, uint16 u16dSize, uint8 *pu8SignedMsgHash, uint16 u16HashLength, uint8 *pu8RsaSignature)
RSA Signature Generation.
uint32 nm_read_reg(uint32 u32Addr)