📄 crypt_sha2.c
字号:
message_length_bits = cpu2be64(message_length_bits); NdisMoveMemory(&pSHA_CTX->Block[56], &message_length_bits, 8); SHA1_Hash(pSHA_CTX); /* Return message digest, transform the UINT32 hash value to bytes */ for (index = 0; index < 5;index++) pSHA_CTX->HashValue[index] = cpu2be32(pSHA_CTX->HashValue[index]); /* End of for */ NdisMoveMemory(DigestMessage, pSHA_CTX->HashValue, SHA1_DIGEST_SIZE);} /* End of SHA1_End *//*========================================================================Routine Description: SHA1 algorithmArguments: message Message context messageLen The length of message in bytesReturn Value: digestMessage Digest messageNote: None========================================================================*/VOID RT_SHA1 ( IN const UINT8 Message[], IN UINT MessageLen, OUT UINT8 DigestMessage[]){ SHA1_CTX_STRUC sha_ctx; NdisZeroMemory(&sha_ctx, sizeof(SHA1_CTX_STRUC)); SHA1_Init(&sha_ctx); SHA1_Append(&sha_ctx, Message, MessageLen); SHA1_End(&sha_ctx, DigestMessage);} /* End of RT_SHA1 */#endif /* SHA1_SUPPORT */#ifdef SHA256_SUPPORT/*========================================================================Routine Description: Initial SHA256_CTX_STRUCArguments: pSHA_CTX Pointer to SHA256_CTX_STRUCReturn Value: NoneNote: None========================================================================*/VOID SHA256_Init ( IN SHA256_CTX_STRUC *pSHA_CTX){ NdisMoveMemory(pSHA_CTX->HashValue, SHA256_DefaultHashValue, sizeof(SHA256_DefaultHashValue)); NdisZeroMemory(pSHA_CTX->Block, SHA256_BLOCK_SIZE); pSHA_CTX->MessageLen = 0; pSHA_CTX->BlockLen = 0;} /* End of SHA256_Init *//*========================================================================Routine Description: SHA256 computation for one block (512 bits)Arguments: pSHA_CTX Pointer to SHA256_CTX_STRUCReturn Value: NoneNote: None========================================================================*/VOID SHA256_Hash ( IN SHA256_CTX_STRUC *pSHA_CTX){ UINT32 W_i,t; UINT32 W[64]; UINT32 a,b,c,d,e,f,g,h,T1,T2; /* Prepare the message schedule, {W_i}, 0 < t < 15 */ NdisMoveMemory(W, pSHA_CTX->Block, SHA256_BLOCK_SIZE); for (W_i = 0; W_i < 16; W_i++) W[W_i] = cpu2be32(W[W_i]); /* Endian Swap */ /* End of for */ /* SHA256 hash computation */ /* Initialize the working variables */ a = pSHA_CTX->HashValue[0]; b = pSHA_CTX->HashValue[1]; c = pSHA_CTX->HashValue[2]; d = pSHA_CTX->HashValue[3]; e = pSHA_CTX->HashValue[4]; f = pSHA_CTX->HashValue[5]; g = pSHA_CTX->HashValue[6]; h = pSHA_CTX->HashValue[7]; /* 64 rounds */ for (t = 0;t < 64;t++) { if (t > 15) /* Prepare the message schedule, {W_i}, 16 < t < 63 */ W[t] = Sigma_256_1(W[t-2]) + W[t-7] + Sigma_256_0(W[t-15]) + W[t-16]; /* End of if */ T1 = h + Zsigma_256_1(e) + Ch(e,f,g) + SHA256_K[t] + W[t]; T2 = Zsigma_256_0(a) + Maj(a,b,c); h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2; } /* End of for */ /* Compute the i^th intermediate hash value H^(i) */ pSHA_CTX->HashValue[0] += a; pSHA_CTX->HashValue[1] += b; pSHA_CTX->HashValue[2] += c; pSHA_CTX->HashValue[3] += d; pSHA_CTX->HashValue[4] += e; pSHA_CTX->HashValue[5] += f; pSHA_CTX->HashValue[6] += g; pSHA_CTX->HashValue[7] += h; NdisZeroMemory(pSHA_CTX->Block, SHA256_BLOCK_SIZE); pSHA_CTX->BlockLen = 0;} /* End of SHA256_Hash *//*========================================================================Routine Description: The message is appended to block. If block size > 64 bytes, the SHA256_Hash will be called.Arguments: pSHA_CTX Pointer to SHA256_CTX_STRUC message Message context messageLen The length of message in bytesReturn Value: NoneNote: None========================================================================*/VOID SHA256_Append ( IN SHA256_CTX_STRUC *pSHA_CTX, IN const UINT8 Message[], IN UINT MessageLen){ UINT appendLen = 0; UINT diffLen = 0; while (appendLen != MessageLen) { diffLen = MessageLen - appendLen; if ((pSHA_CTX->BlockLen + diffLen) < SHA256_BLOCK_SIZE) { NdisMoveMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen, Message + appendLen, diffLen); pSHA_CTX->BlockLen += diffLen; appendLen += diffLen; } else { NdisMoveMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen, Message + appendLen, SHA256_BLOCK_SIZE - pSHA_CTX->BlockLen); appendLen += (SHA256_BLOCK_SIZE - pSHA_CTX->BlockLen); pSHA_CTX->BlockLen = SHA256_BLOCK_SIZE; SHA256_Hash(pSHA_CTX); } /* End of if */ } /* End of while */ pSHA_CTX->MessageLen += MessageLen;} /* End of SHA256_Append *//*========================================================================Routine Description: 1. Append bit 1 to end of the message 2. Append the length of message in rightmost 64 bits 3. Transform the Hash Value to digest messageArguments: pSHA_CTX Pointer to SHA256_CTX_STRUCReturn Value: digestMessage Digest messageNote: None========================================================================*/VOID SHA256_End ( IN SHA256_CTX_STRUC *pSHA_CTX, OUT UINT8 DigestMessage[]){ UINT index; UINT64 message_length_bits; /* Append bit 1 to end of the message */ NdisFillMemory(pSHA_CTX->Block + pSHA_CTX->BlockLen, 1, 0x80); /* 55 = 64 - 8 - 1: append 1 bit(1 byte) and message length (8 bytes) */ if (pSHA_CTX->BlockLen > 55) SHA256_Hash(pSHA_CTX); /* End of if */ /* Append the length of message in rightmost 64 bits */ message_length_bits = pSHA_CTX->MessageLen*8; message_length_bits = cpu2be64(message_length_bits); NdisMoveMemory(&pSHA_CTX->Block[56], &message_length_bits, 8); SHA256_Hash(pSHA_CTX); /* Return message digest, transform the UINT32 hash value to bytes */ for (index = 0; index < 8;index++) pSHA_CTX->HashValue[index] = cpu2be32(pSHA_CTX->HashValue[index]); /* End of for */ NdisMoveMemory(DigestMessage, pSHA_CTX->HashValue, SHA256_DIGEST_SIZE);} /* End of SHA256_End *//*========================================================================Routine Description: SHA256 algorithmArguments: message Message context messageLen The length of message in bytesReturn Value: digestMessage Digest messageNote: None========================================================================*/VOID RT_SHA256 ( IN const UINT8 Message[], IN UINT MessageLen, OUT UINT8 DigestMessage[]){ SHA256_CTX_STRUC sha_ctx; NdisZeroMemory(&sha_ctx, sizeof(SHA256_CTX_STRUC)); SHA256_Init(&sha_ctx); SHA256_Append(&sha_ctx, Message, MessageLen); SHA256_End(&sha_ctx, DigestMessage);} /* End of RT_SHA256 */#endif /* SHA256_SUPPORT *//* End of crypt_sha2.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -