2020-11-25

SM3算法

  1. 摘要算法
    数据摘要算法是密码学中非常重要的一个分支,它通过对所有数据提取指纹信息以实现数字签名,数据完整性校验等功能,由于其不可逆性,有时候会被用于敏感信息的加密,数据摘要算法也被称为哈希(hash)算法,散列算法。
      摘要函数在密码学中具有重要的地位,被广泛应用在数字签名,消息认证,数据完整性检测等领域。摘要函数通常被认为需要满足三个基本特性:碰撞稳固性,原根稳固性和第二原根稳固性。
      2005年,Wang等人给出了MD5算法和SHA-1算法的碰撞攻击方法,现今被广泛应用的MD5算法和SHA-1算法不再是安全的算法。
      SM3密码摘要算法是中国国家密码管理局2010年公布的中国商用密码杂凑算法标准。SM3算法适用于商用密码应用中的数字签名和验证,是在SHA-256基础上改进实现的一种算法。SM3算法采用Merkle-Damgard结构,消息分组长度为512位,摘要值长度为256位。
      SM3算法的压缩函数与SHA-256的压缩函数具有相似的结构,但是SM3算法的设计更加复杂,比如压缩函数的每一轮都使用2个消息字。
    现今为止,SM3算法的安全性相对较高。
    2.SM3密码杂凑算法
    SM3杂凑算法是我国自主设计的密码杂凑算法,适用于商用密码应用中的数字签名和验证消息认证码的生成与验证以及随机数的生成,可满足多种密码应用的安全需求。为了保证杂凑算法的安全性,其产生的杂凑值的长度不应太短,例如MD5输出128比特杂凑值,输出长度太短,影响其安全性SHA-1算法的输出长度为160比特,SM3算法的输出长度为256比特,因此SM3算法的安全性要高于MD5算法和SHA-1算法。
    SM3由国家密码管理局于2010年12月17日发布。相关标准为“GM/T 0004-2012 《SM3密码杂凑算法》”。
    密码散列函数是散列函数的一种。它被认为是一种单向函数,也就是说极其难以由散列函数输出的结果,回推输入的数据是什么。这样的单向函数被称为“现代密码学的驮马”。这种散列函数的输入数据,通常被称为消息(message),而它的输出结果,经常被称为消息摘要(message digest)或摘要(digest)。
    在信息安全中,有许多重要的应用,都使用了密码散列函数来实现,例如数字签名,消息认证码。
    特性
    一个理想的密码散列函数应该有四个主要的特性:
    • 对于任何一个给定的消息,它都很容易就能运算出散列数值。
    • 难以由一个已知的散列数值,去推算出原始的消息。
    • 在不更动散列数值的前提下,修改消息内容是不可行的。
    • 对于两个不同的消息,它不能给与相同的散列数值。 [1]

  2. sm3算法实现
    消息message:任意有限长度的比特串。本文本中消息作为杂凑算法的输入数据。
    杂凑值hash value:杂凑算法作用于消息后输出的特定长度的比特串。本文本中的杂凑值长度为256比特。
    3.1 常数与函数
    3.1.1 初始值
    IV = 7380166f 4914b2b9 172442d7 da8a0600 a96f30bc 163138aa e38dee4d b0fb0e4e
    3.1.2 常量
    Tj={ 79cc4519 0≤j≤15;7a879d8a 16≤j≤63}
    3.1.3 布尔函数
    FFj(X,Y,Z)={XYZ 0≤j≤15;(X & Y)|(X&Z)|(Y&Z) 16≤j≤63}
    GGj(X,Y,Z)={XYZ 0≤j≤15;(X & Y)|(~X&Z)16≤j≤63}
    X,Y,Z为字(32bit)
    3.1.4 置换函数
    P0(X)= X(X<<<9)(X<<<17)
    P1(X)= X(X<<<15)(X<<<23)
    X 为字
    3.2 算法简介
    SM3算法能够对长度为l(l<2^64)bit的消息m,进行填充和迭代压缩,生成杂凑值,最终的杂凑值为256bit。
    3.2.1 填充过程
    设消息m的长度为l bit,首先将bit"1"添加到消息末尾,再加k个“0”,k是满足l+1+k =448 mod 512的最小非负整数。然后再添加一个64bit串,该串是l的二进制表示,填充后的消息m’长度为512的整数倍。
    3.2.2 迭代压缩
    迭代过程:
    m’按照512bit进行分组:m’=B(0)B(1)…B(n-1)
    n=(l+k+65)/512.
    迭代过程如下:
    FOR i=0 to n-1
      V(i+1) = CF(V(i),B(i)
    ENDFOR
    CF为压缩函数,V(0)为初始值IV,迭代压缩的结果为V(n)
    消息扩展:
    消息分组B(i)扩展生成132个字W0,W1,…W67,W0’,W1’…W63’。
    a)消息分组B(i)划分为16个字W0,W1,…W15.
    b)FOR j=16 to 67
      Wj=P1(Wj-16 ^ Wj-9 ^ (Wj-3 <<<15)(Wj-13<<<7)Wj-6
    ENDFOR
    c)FOR j=0 to 63
      Wj’=Wj^Wj+4
    ENDFOR
    压缩函数:
    A/B/C/D/E/F/G/H为字寄存器,SS1/SS2/TT1/TT2为中间变量,压缩函数V(i+1) = CF(V(i),B(i))计算过程如下:
    ABCDEFGH = V(i)
    FOR j=0 to 63
      SS1 = ((A<<<12)+E+(Tj<<   SS2 = SS1^(A<<<12)
      TT1 = FFj(A,B,C)+D+SS2+Wj’
      TT2 = GGj(E,F,G)+H+SS1+Wj
      D = C
      C = B<<<9
      B = A
      A = TT1
      H = G
      G = F<<<19
      F = E
      E = P0(TT2)
    ENDFOR
    V(i+1) = ABCDEFGH^V(i)
    运算过程中,字按照大端格式存储。
    3.2.3 杂凑结果
    杂凑结果为256bit值y=ABCDEFGH=V(n)
    3.3 sm3在芯片中的实际使用方法
    芯片中有sm3函数的入口,每次只能输入8个字节(64位)的16进制数,所以要把文件拆分成N*8字节,并转为16进制,需要执行sm3函数N次
    前面N-1次和最后一次执行函数所用的报文不同,前面N-1次中报文的p1=01(没有返回杂凑值,但是函数中记录下迭代中间结果,作为下一次迭代输入,不输出),最后一次报文的p1=02,(结合上面迭代中间值,输出最终杂凑值)

  3. SM3算法C实现

sm3.h
/*
* sm3.h
*
* 为使此算法兼容32位、64位下Linux或Windows系统,
* 选择 int 来表示 32 位整数。
* 消息长度最大限定为 2**32 - 1(单位:比特),
* 且为 8 的倍数(消息的最小单元为字节)。
*/
#ifndef _SM3_H_
#define _SM3_H_
 
/*
* SM3算法产生的哈希值大小(单位:字节)
*/
#define SM3_HASH_SIZE 32 
 
/*
* SM3上下文
*/
typedef struct sm3_ctx
{
     
	unsigned int m_hash[SM3_HASH_SIZE / 4];
	unsigned char msg_block[64];
} sm3_ctx;
 
/*
* SM3计算函数
*/
unsigned char *sm3_calc(const unsigned char *message,
	unsigned int msg_len, unsigned char digest[SM3_HASH_SIZE]);
 
#endif // _SM3_H_

sm3.c

/*
* sm3.c
*/
#include 
#include 
#include "sm3.h"
 
/*
* 判断运行环境是否为小端
*/
static const int endianTest = 1;
#define is_little_endian() (*(char *)&endianTest == 1)
 
/*
* 向左循环移位
*/
#define left_rotate(word, bits) ( (word) << (bits) | (word) >> (32 - (bits)) )
 
/*
* 反转四字节整型字节序
*/
unsigned int *reverse_word(unsigned int *word)
{
     
	unsigned char *byte, temp;
 
	byte = (unsigned char *)word;
	temp = byte[0];
	byte[0] = byte[3];
	byte[3] = temp;
 
	temp = byte[1];
	byte[1] = byte[2];
	byte[2] = temp;
	return word;
}
 
/*
* T
*/
unsigned int T(int i)
{
     
	if (i >= 0 && i <= 15)
		return 0x79CC4519;
	else if (i >= 16 && i <= 63)
		return 0x7A879D8A;
	else
		return 0;
}
 
/*
* FF
*/
unsigned int FF(unsigned int X, unsigned int Y, unsigned int Z, int i)
{
     
	if (i >= 0 && i <= 15)
		return X ^ Y ^ Z;
	else if (i >= 16 && i <= 63)
		return (X & Y) | (X & Z) | (Y & Z);
	else
		return 0;
}
 
/*
* GG
*/
unsigned int GG(unsigned int X, unsigned int Y, unsigned int Z, int i)
{
     
	if (i >= 0 && i <= 15)
		return X ^ Y ^ Z;
	else if (i >= 16 && i <= 63)
		return (X & Y) | (~X & Z);
	else
		return 0;
}
 
/*
* P0
*/
unsigned int P0(unsigned int X)
{
     
	return X ^ left_rotate(X, 9) ^ left_rotate(X, 17);
}
 
/*
* P1
*/
unsigned int P1(unsigned int X)
{
     
	return X ^ left_rotate(X, 15) ^ left_rotate(X, 23);
}
 
/*
* 初始化函数
*/
void SM3Init(sm3_ctx *context)
{
     
	context->m_hash[0] = 0x7380166F;
	context->m_hash[1] = 0x4914B2B9;
	context->m_hash[2] = 0x172442D7;
	context->m_hash[3] = 0xDA8A0600;
	context->m_hash[4] = 0xA96F30BC;
	context->m_hash[5] = 0x163138AA;
	context->m_hash[6] = 0xE38DEE4D;
	context->m_hash[7] = 0xB0FB0E4E;
}
 
/*
* 处理消息块
*/
void SM3ProcessMsg_block(sm3_ctx *context)
{
     
	int i;
	unsigned int W[68];
	unsigned int W_[64];
	unsigned int A, B, C, D, E, F, G, H, SS1, SS2, TT1, TT2;
 
	/* 消息扩展 */
	for (i = 0; i < 16; i++)
	{
     
		W[i] = *(unsigned int *)(context->msg_block + i * 4);
		if (is_little_endian())
			reverse_word(W + i);
		//printf("%d: %x\n", i, W[i]);    
	}
	for (i = 16; i < 68; i++)
	{
     
		W[i] = P1(W[i - 16] ^ W[i - 9] ^ left_rotate(W[i - 3], 15))
			^ left_rotate(W[i - 13], 7)
			^ W[i - 6];
		//printf("%d: %x\n", i, W[i]);    
	}
	for (i = 0; i < 64; i++)
	{
     
		W_[i] = W[i] ^ W[i + 4];
		//printf("%d: %x\n", i, W_[i]);    
	}
 
	/* 消息压缩 */
	A = context->m_hash[0];
	B = context->m_hash[1];
	C = context->m_hash[2];
	D = context->m_hash[3];
	E = context->m_hash[4];
	F = context->m_hash[5];
	G = context->m_hash[6];
	H = context->m_hash[7];
	for (i = 0; i < 64; i++)
	{
     
		SS1 = left_rotate((left_rotate(A, 12) + E + left_rotate(T(i), i)), 7);
		SS2 = SS1 ^ left_rotate(A, 12);
		TT1 = FF(A, B, C, i) + D + SS2 + W_[i];
		TT2 = GG(E, F, G, i) + H + SS1 + W[i];
		D = C;
		C = left_rotate(B, 9);
		B = A;
		A = TT1;
		H = G;
		G = left_rotate(F, 19);
		F = E;
		E = P0(TT2);
	}
	context->m_hash[0] ^= A;
	context->m_hash[1] ^= B;
	context->m_hash[2] ^= C;
	context->m_hash[3] ^= D;
	context->m_hash[4] ^= E;
	context->m_hash[5] ^= F;
	context->m_hash[6] ^= G;
	context->m_hash[7] ^= H;
}
 
/*
* SM3算法主函数
*/
unsigned char *sm3_calc(const unsigned char *message,
	unsigned int msg_len, unsigned char digest[SM3_HASH_SIZE])
{
     
	sm3_ctx context;
	unsigned int i, remainder, bitLen;
 
	/* 初始化上下文 */
	SM3Init(&context);
 
	/* 对前面的消息分组进行处理 */
	for (i = 0; i < msg_len / 64; i++)
	{
     
		memcpy(context.msg_block, message + i * 64, 64);
		SM3ProcessMsg_block(&context);
	}
 
	/* 填充消息分组,并处理 */
	bitLen = msg_len * 8;
	if (is_little_endian())
		reverse_word(&bitLen);
	remainder = msg_len % 64;
	memcpy(context.msg_block, message + i * 64, remainder);
	context.msg_block[remainder] = 0x80;
	if (remainder <= 55)
	{
     
		/* 长度按照大端法占8个字节,该程序只考虑长度在 2**32 - 1(单位:比特)以内的情况,
		* 故将高 4 个字节赋为 0 。*/
		memset(context.msg_block + remainder + 1, 0, 64 - remainder - 1 - 8 + 4);
		memcpy(context.msg_block + 64 - 4, &bitLen, 4);
		SM3ProcessMsg_block(&context);
	}
	else
	{
     
		memset(context.msg_block + remainder + 1, 0, 64 - remainder - 1);
		SM3ProcessMsg_block(&context);
		/* 长度按照大端法占8个字节,该程序只考虑长度在 2**32 - 1(单位:比特)以内的情况,
		* 故将高 4 个字节赋为 0 。*/
		memset(context.msg_block, 0, 64 - 4);
		memcpy(context.msg_block + 64 - 4, &bitLen, 4);
		SM3ProcessMsg_block(&context);
	}
 
	/* 返回结果 */
	if (is_little_endian())
	for (i = 0; i < 8; i++)
		reverse_word(context.m_hash + i);
	memcpy(digest, context.m_hash, SM3_HASH_SIZE);
 
	return digest;
}
sm3test.c

#include 
#include 
#include "sm3.h"
#include 
 
//#pragma comment(lib,"sm3dll2")
//extern "C" void SM3Call(const unsigned char *message,unsigned int msg_len, unsigned char digest[SM3_HASH_SIZE]);
 
int main( int argc, char *argv[] )
{
     
	unsigned char input[256] = "abc";
	int ilen = 3;
	unsigned char output[32];
	int i;
	// ctx;
 
	printf("Message:\n");
	printf("%s\n",input);
 
	sm3_calc(input, ilen, output);
	printf("Hash:\n   ");
	for(i=0; i<32; i++)
	{
     
		printf("%02x",output[i]);
		if (((i+1) % 4 ) == 0) printf(" ");
	} 
 
	printf("\n");
 
 
	unsigned char input2[256] = "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd";
	int ilen2 = 64;
	unsigned char output2[32];
	int i2;
	// ctx;
 
	printf("Message:\n");
	printf("%s\n", input2);
 
	sm3_calc(input2, ilen2, output2);
	printf("Hash:\n   ");
	for (i2 = 0; i2<32; i2++)
	{
     
		printf("%02x", output2[i2]);
		if (((i2 + 1) % 4) == 0) printf(" ");
	}
	printf("\n");
	system("pause");
}

软件实现2#ifndef XYSSL_SM3_H
#define XYSSL_SM3_H


/**
 * \brief          SM3 context structure
 */
typedef struct
{
     
    unsigned long total[2];     /*!< number of bytes processed  */
    unsigned long state[8];     /*!< intermediate digest state  */
    unsigned char buffer[64];   /*!< data block being processed */

    unsigned char ipad[64];     /*!< HMAC: inner padding        */
    unsigned char opad[64];     /*!< HMAC: outer padding        */

}
sm3_context;

#ifdef __cplusplus
extern "C" {
     
#endif

/**
 * \brief          SM3 context setup
 *
 * \param ctx      context to be initialized
 */
void sm3_starts( sm3_context *ctx );

/**
 * \brief          SM3 process buffer
 *
 * \param ctx      SM3 context
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 */
void sm3_update( sm3_context *ctx, unsigned char *input, int ilen );

/**
 * \brief          SM3 final digest
 *
 * \param ctx      SM3 context
 */
void sm3_finish( sm3_context *ctx, unsigned char output[32] );

/**
 * \brief          Output = SM3( input buffer )
 *
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 * \param output   SM3 checksum result
 */
void sm3( unsigned char *input, int ilen,
           unsigned char output[32]);

/**
 * \brief          Output = SM3( file contents )
 *
 * \param path     input file name
 * \param output   SM3 checksum result
 *
 * \return         0 if successful, 1 if fopen failed,
 *                 or 2 if fread failed
 */
int sm3_file( char *path, unsigned char output[32] );

/**
 * \brief          SM3 HMAC context setup
 *
 * \param ctx      HMAC context to be initialized
 * \param key      HMAC secret key
 * \param keylen   length of the HMAC key
 */
void sm3_hmac_starts( sm3_context *ctx, unsigned char *key, int keylen);

/**
 * \brief          SM3 HMAC process buffer
 *
 * \param ctx      HMAC context
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 */
void sm3_hmac_update( sm3_context *ctx, unsigned char *input, int ilen );

/**
 * \brief          SM3 HMAC final digest
 *
 * \param ctx      HMAC context
 * \param output   SM3 HMAC checksum result
 */
void sm3_hmac_finish( sm3_context *ctx, unsigned char output[32] );

/**
 * \brief          Output = HMAC-SM3( hmac key, input buffer )
 *
 * \param key      HMAC secret key
 * \param keylen   length of the HMAC key
 * \param input    buffer holding the  data
 * \param ilen     length of the input data
 * \param output   HMAC-SM3 result
 */
void sm3_hmac( unsigned char *key, int keylen,
                unsigned char *input, int ilen,
                unsigned char output[32] );


#ifdef __cplusplus
}
#endif

#endif /* sm3.h */

#include "sm3.h"
#include 
#include 

/*
 * 32-bit integer manipulation macros (big endian)
 */
#ifndef GET_ULONG_BE
#define GET_ULONG_BE(n,b,i)                             \
{                                                       \
    (n) = ( (unsigned long) (b)[(i)    ] << 24 )        \
        | ( (unsigned long) (b)[(i) + 1] << 16 )        \
        | ( (unsigned long) (b)[(i) + 2] <<  8 )        \
        | ( (unsigned long) (b)[(i) + 3]       );       \
}
#endif

#ifndef PUT_ULONG_BE
#define PUT_ULONG_BE(n,b,i)                             \
{                                                       \
    (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
    (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
    (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
    (b)[(i) + 3] = (unsigned char) ( (n)       );       \
}
#endif

/*
 * SM3 context setup
 */
void sm3_starts( sm3_context *ctx )
{
     
    ctx->total[0] = 0;
    ctx->total[1] = 0;

    ctx->state[0] = 0x7380166F;
    ctx->state[1] = 0x4914B2B9;
    ctx->state[2] = 0x172442D7;
    ctx->state[3] = 0xDA8A0600;
    ctx->state[4] = 0xA96F30BC;
    ctx->state[5] = 0x163138AA;
    ctx->state[6] = 0xE38DEE4D;
    ctx->state[7] = 0xB0FB0E4E;

}

static void sm3_process( sm3_context *ctx, unsigned char data[64] )
{
     
    unsigned long SS1, SS2, TT1, TT2, W[68],W1[64];
    unsigned long A, B, C, D, E, F, G, H;
	unsigned long T[64];
	unsigned long Temp1,Temp2,Temp3,Temp4,Temp5;
	int j;
#ifdef _DEBUG
	int i;
#endif

// 	for(j=0; j < 68; j++)
// 		W[j] = 0;
// 	for(j=0; j < 64; j++)
// 		W1[j] = 0;
	
	for(j = 0; j < 16; j++)
		T[j] = 0x79CC4519;
	for(j =16; j < 64; j++)
		T[j] = 0x7A879D8A;

    GET_ULONG_BE( W[ 0], data,  0 );
    GET_ULONG_BE( W[ 1], data,  4 );
    GET_ULONG_BE( W[ 2], data,  8 );
    GET_ULONG_BE( W[ 3], data, 12 );
    GET_ULONG_BE( W[ 4], data, 16 );
    GET_ULONG_BE( W[ 5], data, 20 );
    GET_ULONG_BE( W[ 6], data, 24 );
    GET_ULONG_BE( W[ 7], data, 28 );
    GET_ULONG_BE( W[ 8], data, 32 );
    GET_ULONG_BE( W[ 9], data, 36 );
    GET_ULONG_BE( W[10], data, 40 );
    GET_ULONG_BE( W[11], data, 44 );
    GET_ULONG_BE( W[12], data, 48 );
    GET_ULONG_BE( W[13], data, 52 );
    GET_ULONG_BE( W[14], data, 56 );
    GET_ULONG_BE( W[15], data, 60 );

#ifdef _DEBUG 
	printf("Message with padding:\n");
	for(i=0; i< 8; i++)
		printf("%08x ",W[i]);
	printf("\n");
	for(i=8; i< 16; i++)
		printf("%08x ",W[i]);
	printf("\n");
#endif

#define FF0(x,y,z) ( (x) ^ (y) ^ (z)) 
#define FF1(x,y,z) (((x) & (y)) | ( (x) & (z)) | ( (y) & (z)))

#define GG0(x,y,z) ( (x) ^ (y) ^ (z)) 
#define GG1(x,y,z) (((x) & (y)) | ( (~(x)) & (z)) )


#define  SHL(x,n) (((x) & 0xFFFFFFFF) << n)
#define ROTL(x,n) (SHL((x),n) | ((x) >> (32 - n)))

#define P0(x) ((x) ^  ROTL((x),9) ^ ROTL((x),17)) 
#define P1(x) ((x) ^  ROTL((x),15) ^ ROTL((x),23)) 

	for(j = 16; j < 68; j++ )
	{
     
		//W[j] = P1( W[j-16] ^ W[j-9] ^ ROTL(W[j-3],15)) ^ ROTL(W[j - 13],7 ) ^ W[j-6];
		//Why thd release's result is different with the debug's ?
		//Below is okay. Interesting, Perhaps VC6 has a bug of Optimizaiton.
		
		Temp1 = W[j-16] ^ W[j-9];
		Temp2 = ROTL(W[j-3],15);
		Temp3 = Temp1 ^ Temp2;
		Temp4 = P1(Temp3);
		Temp5 =  ROTL(W[j - 13],7 ) ^ W[j-6];
		W[j] = Temp4 ^ Temp5;
	}

#ifdef _DEBUG 
	printf("Expanding message W0-67:\n");
	for(i=0; i<68; i++)
	{
     
		printf("%08x ",W[i]);
		if(((i+1) % 8) == 0) printf("\n");
	}
	printf("\n");
#endif

	for(j =  0; j < 64; j++)
	{
     
        W1[j] = W[j] ^ W[j+4];
	}

#ifdef _DEBUG 
	printf("Expanding message W'0-63:\n");
	for(i=0; i<64; i++)
	{
     
		printf("%08x ",W1[i]);
		if(((i+1) % 8) == 0) printf("\n");
	}
	printf("\n");
#endif

    A = ctx->state[0];
    B = ctx->state[1];
    C = ctx->state[2];
    D = ctx->state[3];
    E = ctx->state[4];
    F = ctx->state[5];
    G = ctx->state[6];
    H = ctx->state[7];
#ifdef _DEBUG       
	printf("j     A       B        C         D         E        F        G       H\n");
	printf("   %08x %08x %08x %08x %08x %08x %08x %08x\n",A,B,C,D,E,F,G,H);
#endif

	for(j =0; j < 16; j++)
	{
     
		SS1 = ROTL((ROTL(A,12) + E + ROTL(T[j],j)), 7); 
		SS2 = SS1 ^ ROTL(A,12);
		TT1 = FF0(A,B,C) + D + SS2 + W1[j];
		TT2 = GG0(E,F,G) + H + SS1 + W[j];
		D = C;
		C = ROTL(B,9);
		B = A;
		A = TT1;
		H = G;
		G = ROTL(F,19);
		F = E;
		E = P0(TT2);
#ifdef _DEBUG 
		printf("%02d %08x %08x %08x %08x %08x %08x %08x %08x\n",j,A,B,C,D,E,F,G,H);
#endif
	}
	
	for(j =16; j < 64; j++)
	{
     
		SS1 = ROTL((ROTL(A,12) + E + ROTL(T[j],j)), 7); 
		SS2 = SS1 ^ ROTL(A,12);
		TT1 = FF1(A,B,C) + D + SS2 + W1[j];
		TT2 = GG1(E,F,G) + H + SS1 + W[j];
		D = C;
		C = ROTL(B,9);
		B = A;
		A = TT1;
		H = G;
		G = ROTL(F,19);
		F = E;
		E = P0(TT2);
#ifdef _DEBUG 
		printf("%02d %08x %08x %08x %08x %08x %08x %08x %08x\n",j,A,B,C,D,E,F,G,H);
#endif	
	}

    ctx->state[0] ^= A;
    ctx->state[1] ^= B;
    ctx->state[2] ^= C;
    ctx->state[3] ^= D;
    ctx->state[4] ^= E;
    ctx->state[5] ^= F;
    ctx->state[6] ^= G;
    ctx->state[7] ^= H;
#ifdef _DEBUG 
	   printf("   %08x %08x %08x %08x %08x %08x %08x %08x\n",ctx->state[0],ctx->state[1],ctx->state[2],
		                          ctx->state[3],ctx->state[4],ctx->state[5],ctx->state[6],ctx->state[7]);
#endif
}

/*
 * SM3 process buffer
 */
void sm3_update( sm3_context *ctx, unsigned char *input, int ilen )
{
     
    int fill;
    unsigned long left;

    if( ilen <= 0 )
        return;

    left = ctx->total[0] & 0x3F;
    fill = 64 - left;

    ctx->total[0] += ilen;
    ctx->total[0] &= 0xFFFFFFFF;

    if( ctx->total[0] < (unsigned long) ilen )
        ctx->total[1]++;

    if( left && ilen >= fill )
    {
     
        memcpy( (void *) (ctx->buffer + left),
                (void *) input, fill );
        sm3_process( ctx, ctx->buffer );
        input += fill;
        ilen  -= fill;
        left = 0;
    }

    while( ilen >= 64 )
    {
     
        sm3_process( ctx, input );
        input += 64;
        ilen  -= 64;
    }

    if( ilen > 0 )
    {
     
        memcpy( (void *) (ctx->buffer + left),
                (void *) input, ilen );
    }
}

static const unsigned char sm3_padding[64] =
{
     
 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

/*
 * SM3 final digest
 */
void sm3_finish( sm3_context *ctx, unsigned char output[32] )
{
     
    unsigned long last, padn;
    unsigned long high, low;
    unsigned char msglen[8];

    high = ( ctx->total[0] >> 29 )
         | ( ctx->total[1] <<  3 );
    low  = ( ctx->total[0] <<  3 );

    PUT_ULONG_BE( high, msglen, 0 );
    PUT_ULONG_BE( low,  msglen, 4 );

    last = ctx->total[0] & 0x3F;
    padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );

    sm3_update( ctx, (unsigned char *) sm3_padding, padn );
    sm3_update( ctx, msglen, 8 );

    PUT_ULONG_BE( ctx->state[0], output,  0 );
    PUT_ULONG_BE( ctx->state[1], output,  4 );
    PUT_ULONG_BE( ctx->state[2], output,  8 );
    PUT_ULONG_BE( ctx->state[3], output, 12 );
    PUT_ULONG_BE( ctx->state[4], output, 16 );
    PUT_ULONG_BE( ctx->state[5], output, 20 );
    PUT_ULONG_BE( ctx->state[6], output, 24 );
    PUT_ULONG_BE( ctx->state[7], output, 28 );
}

/*
 * output = SM3( input buffer )
 */
void sm3( unsigned char *input, int ilen,
           unsigned char output[32] )
{
     
    sm3_context ctx;

    sm3_starts( &ctx );
    sm3_update( &ctx, input, ilen );
    sm3_finish( &ctx, output );

    memset( &ctx, 0, sizeof( sm3_context ) );
}

/*
 * output = SM3( file contents )
 */
int sm3_file( char *path, unsigned char output[32] )
{
     
    FILE *f;
    size_t n;
    sm3_context ctx;
    unsigned char buf[1024];

    if( ( f = fopen( path, "rb" ) ) == NULL )
        return( 1 );

    sm3_starts( &ctx );

    while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
        sm3_update( &ctx, buf, (int) n );

    sm3_finish( &ctx, output );

    memset( &ctx, 0, sizeof( sm3_context ) );

    if( ferror( f ) != 0 )
    {
     
        fclose( f );
        return( 2 );
    }

    fclose( f );
    return( 0 );
}

/*
 * SM3 HMAC context setup
 */
void sm3_hmac_starts( sm3_context *ctx, unsigned char *key, int keylen )
{
     
    int i;
    unsigned char sum[32];

    if( keylen > 64 )
    {
     
        sm3( key, keylen, sum );
        keylen = 32;
		//keylen = ( is224 ) ? 28 : 32;
        key = sum;
    }

    memset( ctx->ipad, 0x36, 64 );
    memset( ctx->opad, 0x5C, 64 );

    for( i = 0; i < keylen; i++ )
    {
     
        ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
        ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
    }

    sm3_starts( ctx);
    sm3_update( ctx, ctx->ipad, 64 );

    memset( sum, 0, sizeof( sum ) );
}

/*
 * SM3 HMAC process buffer
 */
void sm3_hmac_update( sm3_context *ctx, unsigned char *input, int ilen )
{
     
    sm3_update( ctx, input, ilen );
}

/*
 * SM3 HMAC final digest
 */
void sm3_hmac_finish( sm3_context *ctx, unsigned char output[32] )
{
     
    int hlen;
    unsigned char tmpbuf[32];

    //is224 = ctx->is224;
    hlen =  32;

    sm3_finish( ctx, tmpbuf );
    sm3_starts( ctx );
    sm3_update( ctx, ctx->opad, 64 );
    sm3_update( ctx, tmpbuf, hlen );
    sm3_finish( ctx, output );

    memset( tmpbuf, 0, sizeof( tmpbuf ) );
}

/*
 * output = HMAC-SM#( hmac key, input buffer )
 */
void sm3_hmac( unsigned char *key, int keylen,
                unsigned char *input, int ilen,
                unsigned char output[32] )
{
     
    sm3_context ctx;

    sm3_hmac_starts( &ctx, key, keylen);
    sm3_hmac_update( &ctx, input, ilen );
    sm3_hmac_finish( &ctx, output );

    memset( &ctx, 0, sizeof( sm3_context ) );
}

// Sample 1
// Input:"abc"  
// Output:66c7f0f4 62eeedd9 d1f2d46b dc10e4e2 4167c487 5cf2f7a2 297da02b 8f4ba8e0

// Sample 2 
// Input:"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"
// Outpuf:debe9ff9 2275b8a1 38604889 c18e5a4d 6fdb70e5 387e5765 293dcba3 9c0c5732

#include 
#include 
#include "sm3.h"

int main( int argc, char *argv[] )
{
     
	unsigned char *input = "abc";
	int ilen = 3;
	unsigned char output[32];
	int i;
	sm3_context ctx;

	printf("Message:\n");
	printf("%s\n",input);

	sm3(input, ilen, output);
	printf("Hash:\n   ");
	for(i=0; i<32; i++)
	{
     
		printf("%02x",output[i]);
		if (((i+1) % 4 ) == 0) printf(" ");
	} 
	printf("\n");

	printf("Message:\n");
	for(i=0; i < 16; i++)
		printf("abcd");
	printf("\n");

    sm3_starts( &ctx );
	for(i=0; i < 16; i++)
		sm3_update( &ctx, "abcd", 4 );
    sm3_finish( &ctx, output );
    memset( &ctx, 0, sizeof( sm3_context ) );
	
	printf("Hash:\n   ");
	for(i=0; i<32; i++)
	{
     
		printf("%02x",output[i]);
		if (((i+1) % 4 ) == 0) printf(" ");
	}   
	printf("\n");

}

你可能感兴趣的:(密码学,算法,密码学)