其他分享
首页 > 其他分享> > HMAC - SM3/SHA1

HMAC - SM3/SHA1

作者:互联网

11

#ifndef __HMAC_H__
#define __HMAC_H__

#include <stdint.h>

void hmac_sm3(const uint8_t *data, uint32_t data_len, const uint8_t *key, uint32_t key_len, uint8_t *out);
void hmac_sha1(const uint8_t *data, uint32_t data_len, const uint8_t *key, uint32_t key_len, uint8_t *out);

#endif /* __HMAC_H__ */
#include <stddef.h>
#include <string.h>
#include "sm3.h"
#include "sha1.h"
#include "hmac.h"

#define SM3_BLOCK_SIZE      64
#define SHA1_BLOCK_SIZE     64

/**************************************************
函数名称:hmac_sm3
函数功能:HMAC计算,杂凑函数SM3
入口参数:data,data_len,key,key_len
出口参数:out
返回值:void
data限长1024-SM3_BLOCK_SIZE
**************************************************/
void hmac_sm3(const uint8_t *data, uint32_t data_len, const uint8_t *key, uint32_t key_len, uint8_t *out)
{
    int i;
    uint8_t hK[SM3_BLOCK_SIZE];
    uint8_t hK1[SM3_BLOCK_SIZE];
    uint8_t hK2[SM3_BLOCK_SIZE];
    uint8_t lH[SM3_HASH_SIZE];
    uint8_t llH[SM3_HASH_SIZE];
    uint8_t hash_buf[1024];

    //1.密钥扩展
    memset(hK, 0x00, sizeof(hK));
    if (key_len > SM3_BLOCK_SIZE)
    {
        SM3Calc(key, key_len, hK);
    }
    else
    {
        memcpy(hK, key, key_len);
    }

    for (i = 0; i < SM3_BLOCK_SIZE; i++)
    {
        hK1[i] = hK[i] ^ 0x36;
        hK2[i] = hK[i] ^ 0x5C;
    }

    //2.杂凑计算
    memcpy(hash_buf, hK1, sizeof(hK1));
    memcpy(hash_buf+sizeof(hK1), data, data_len);
    SM3Calc((const uint8_t *)hash_buf, sizeof(hK1)+data_len, lH);

    //3.输出变换
    memcpy(hash_buf, hK2, sizeof(hK2));
    memcpy(hash_buf+sizeof(hK2), lH, sizeof(lH));
    SM3Calc((const uint8_t *)hash_buf, sizeof(hK2)+sizeof(lH), llH);

    //4.截断操作
    memcpy(out, llH, SM3_HASH_SIZE);

    return ;
}

/**************************************************
函数名称:hmac_sha1
函数功能:HMAC计算,杂凑函数sha1
入口参数:data,data_len,key,key_len
出口参数:out
返回值:void
**************************************************/
void hmac_sha1(const uint8_t *data, uint32_t data_len, const uint8_t *key, uint32_t key_len, uint8_t *out)
{
    int i;
    uint8_t hK[SHA1_BLOCK_SIZE];
    uint8_t hK1[SHA1_BLOCK_SIZE];
    uint8_t hK2[SHA1_BLOCK_SIZE];
    uint8_t lH[SHA1HashSize];
    uint8_t llH[SHA1HashSize];

    size_t hash_buf_size =  SHA1_BLOCK_SIZE + ((data_len < SHA1HashSize) ? SHA1HashSize : data_len);
    uint8_t hash_buf[hash_buf_size];

    //1.密钥扩展
    memset(hK, 0x00, sizeof(hK));
    if (key_len > SHA1_BLOCK_SIZE)
    {
        SHA1Encrypt((uint8_t *)key, key_len, hK);
    }
    else
    {
        memcpy(hK, key, key_len);
    }

    for (i = 0; i < SHA1_BLOCK_SIZE; i++)
    {
        hK1[i] = hK[i] ^ 0x36;
        hK2[i] = hK[i] ^ 0x5C;
    }

    //2.杂凑计算
    memcpy(hash_buf, hK1, sizeof(hK1));
    memcpy(hash_buf+sizeof(hK1), data, data_len);
    SHA1Encrypt(hash_buf, sizeof(hK1)+data_len, lH);

    //3.输出变换
    memcpy(hash_buf, hK2, sizeof(hK2));
    memcpy(hash_buf+sizeof(hK2), lH, sizeof(lH));
    SHA1Encrypt(hash_buf, sizeof(hK2)+sizeof(lH), llH);

    //4.截断操作
    memcpy(out, llH, SHA1HashSize);

    return ;
}

 

标签:SHA1,SM3,uint8,len,key,sizeof,data,HMAC,SIZE
来源: https://www.cnblogs.com/damm/p/15852429.html