官方demo使用的是HMAC-MD5 和 SHA1,全称为“Hash Message Authentication Code”,中文名“散列消息鉴别码”,主要是利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。MD5 和 SHA1 就是这种散列函数。HMAC 还可以使用一个用于计算和确认消息鉴别值的密钥。
DEMO原文数据和秘钥数据如下:54686520686173682070726f636573736f7220697320612066756c6c7920636f6d706c69616e7420696d706c656d656e746174696f6e206f662074686520736563757265206861736820616c676f726974686d20285348412d31292c20746865204d443520286d6573736167652d64696765737420616c676f726974686d203529206861736820616c676f726974686d20616e642074686520484d414320286b657965642d68617368206d6573736167652061757468656e7469636174696f6e20636f64652920616c676f726974686d207375697461626c6520666f7220612076617269657479206f66206170706c69636174696f6e732e2a2a2a2053544d3332202a2a2a
在https://www.toolhelper.cn/DigestAlgorithm/HMAC_SHA网页上进行校验计算HMAC-MD5 和 SHA1与demo中比较的数据相一致。
字符编码:Hex(16进制,不编码)
校验:2B47EF34F66658BE3B2AC6EC5885F21ED59FDDAD
更改输入数据为0x12,0x34,共两个字节,其中HMAC-MD5 为407D8BBD08B21E9D51FDD5EB1300858F
HMAC-SHA1为A588AA7142F6A108B22A89589D9C462B9CC89C8C
更改工程中的原文数据,秘钥还使用原demo中的261个字节数据,编译下载程序,初始化后硬件hash计算的HMAC-MD5 、SHA1如果与预设的结果一致,LED1、LED2灯点亮。
//输入数据的大小
#define INPUT_TAB_SIZE ((uint32_t) 2)//61)/* The size of the input data "aInput" */
HASH_HandleTypeDef hhash;
//秘钥
__ALIGN_BEGIN static const uint8_t pKeyHASH[261] __ALIGN_END = {0x54,0x68,0x65,0x20,0x68,0x61,0x73,0x68,0x20,0x70,0x72,0x6f,0x63,0x65,0x73,0x73,0x6f,0x72,0x20,0x69,0x73,0x20,0x61,0x20,0x66,0x75,0x6c,0x6c,0x79,0x20,0x63,0x6f,0x6d,0x70,0x6c,0x69,0x61,0x6e,0x74,0x20,0x69,0x6d,0x70,0x6c,0x65,0x6d,0x65,0x6e,0x74,0x61,0x74,0x69,0x6f,0x6e,0x20,0x6f,0x66,0x20,0x74,0x68,0x65,0x20,0x73,0x65,0x63,0x75,0x72,0x65,0x20,0x68,0x61,0x73,0x68,0x20,0x61,0x6c,0x67,0x6f,0x72,0x69,0x74,0x68,0x6d,0x20,0x28,0x53,0x48,0x41,0x2d,0x31,0x29,0x2c,0x20,0x74,0x68,0x65,0x20,0x4d,0x44,0x35,0x20,0x28,0x6d,0x65,0x73,0x73,0x61,0x67,0x65,0x2d,0x64,0x69,0x67,0x65,0x73,0x74,0x20,0x61,0x6c,0x67,0x6f,0x72,0x69,0x74,0x68,0x6d,0x20,0x35,0x29,0x20,0x68,0x61,0x73,0x68,0x20,0x61,0x6c,0x67,0x6f,0x72,0x69,0x74,0x68,0x6d,0x20,0x61,0x6e,0x64,0x20,0x74,0x68,0x65,0x20,0x48,0x4d,0x41,0x43,0x20,0x28,0x6b,0x65,0x79,0x65,0x64,0x2d,0x68,0x61,0x73,0x68,0x20,0x6d,0x65,0x73,0x73,0x61,0x67,0x65,0x20,0x61,0x75,0x74,0x68,0x65,0x6e,0x74,0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x20,0x63,0x6f,0x64,0x65,0x29,0x20,0x61,0x6c,0x67,0x6f,0x72,0x69,0x74,0x68,0x6d,0x20,0x73,0x75,0x69,0x74,0x61,0x62,0x6c,0x65,0x20,0x66,0x6f,0x72,0x20,0x61,0x20,0x76,0x61,0x72,0x69,0x65,0x74,0x79,0x20,0x6f,0x66,0x20,0x61,0x70,0x70,0x6c,0x69,0x63,0x61,0x74,0x69,0x6f,0x6e,0x73,0x2e,0x2a,0x2a,0x2a,0x20,0x53,0x54,0x4d,0x33,0x32,0x20,0x2a,0x2a,0x2a};
//输入的数据
__ALIGN_BEGIN const uint8_t aInput[INPUT_TAB_SIZE] __ALIGN_END = {0x12,0x34};
__ALIGN_BEGIN static uint8_t aMD5Digest[16] __ALIGN_END;
__ALIGN_BEGIN static uint8_t aExpectMD5Digest[16] __ALIGN_END = {0x40,0x7D,0x8B, 0xBD, 0x08, 0xB2, 0x1E, 0x9D, 0x51, 0xFD, 0xD5, 0xEB, 0x13, 0x00, 0x85, 0x8F};
__ALIGN_BEGIN static uint8_t aSHA1Digest[20] __ALIGN_END;
__ALIGN_BEGIN static uint8_t aExpectSHA1Digest[20] __ALIGN_END = {0xA5,0x88,0xAA,0x71,0x42,0xF6,0xA1,0x08,0xB2,0x2A,0x89,0x58,0x9D,0x9C,0x46,0x2B,0x9C,0xC8,0x9C,0x8C};
static void MX_HASH_Init(void);
int main(void)
{
...
MX_HASH_Init();
if (HAL_HMAC_SHA1_Start(&hhash, (uint8_t *)aInput, INPUT_TAB_SIZE, aSHA1Digest, 0xFF) != HAL_OK)
{
Error_Handler(); }
if (memcmp(aSHA1Digest, aExpectSHA1Digest, sizeof(aExpectSHA1Digest) / sizeof(aExpectSHA1Digest[0])) != 0)
{
Error_Handler(); }
else
{ BSP_LED_On(LED1); }
if(HAL_HASH_DeInit(&hhash) != HAL_OK)
{ Error_Handler(); }
MX_HASH_Init();
/* Compute HMAC-MD5 */
if (HAL_HMAC_MD5_Start(&hhash, (uint8_t *)aInput, INPUT_TAB_SIZE, aMD5Digest, 0xFF) != HAL_OK)
{
Error_Handler(); }
/* Compare computed digest with expected one */
if (memcmp(aMD5Digest, aExpectMD5Digest, sizeof(aExpectMD5Digest) / sizeof(aExpectMD5Digest[0])) != 0)
{
Error_Handler(); }
else
{BSP_LED_On(LED2);}
while (1)
{}
程序运行,LED1、LED2随即点亮,硬件HASH速度很快,解析与预设一致;