123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- /*
- * SM3 Hash alogrith
- * thanks to Xyssl
- * author:goldboar
- * email:goldboar@163.com
- * 2011-10-26
- */
- //Testing data from SM3 Standards
- //http://www.oscca.gov.cn/News/201012/News_1199.htm
- // 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 "StdAfx.h"
- #include "sm3.h"
- #include <string.h>
- #include <stdio.h>
- /*
- * 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
- /*
- #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))
- */
- unsigned long GG0(unsigned long x,unsigned long y,unsigned long z)
- {
- unsigned long u = 0;
- x = x & 0xFFFFFFFF;
- y = y & 0xFFFFFFFF;
- z = z & 0xFFFFFFFF;
-
- u = x ^ y ^ z;
-
- u = u & 0xFFFFFFFF;
- return u;
- }
- unsigned long GG1(unsigned long x,unsigned long y,unsigned long z)
- {
- unsigned long u = 0,v = 0;
- x = x & 0xFFFFFFFF;
- y = y & 0xFFFFFFFF;
- z = z & 0xFFFFFFFF;
-
- u = x & y;
- v = (~x) & z;
- u = u | v;
-
- u = u & 0xFFFFFFFF;
-
- return u;
- }
- unsigned long FF0(unsigned long x,unsigned long y,unsigned long z)
- {
- unsigned long u = 0;
- x = x & 0xFFFFFFFF;
- y = y & 0xFFFFFFFF;
- z = z & 0xFFFFFFFF;
- u = x ^ y ^ z;
- u = u & 0xFFFFFFFF;
- return u;
- }
- unsigned long FF1(unsigned long x,unsigned long y,unsigned long z)
- {
- unsigned long u = 0,v = 0;
- x = x & 0xFFFFFFFF;
- y = y & 0xFFFFFFFF;
- z = z & 0xFFFFFFFF;
- u = x & y;
- v = x & z;
- u = u | v;
- v = y & z;
- u = u | v;
- u = u & 0xFFFFFFFF;
- return u;
- }
- unsigned long ROTL(unsigned long x,int n)
- {
- unsigned long y = 0;
- x = x & 0xFFFFFFFF;
- n = n%32;
- y = (x &0xFFFFFFFF) << n;
- y = y | (x >> (32-n));
- y = y & 0xFFFFFFFF;
- return y;
- }
- unsigned long P0(unsigned long x)
- {
- unsigned long y = 0;
- x = x & 0xFFFFFFFF;
- y = ROTL(x,9);
- y = x ^ y;
- y = y ^ ROTL(x,17);
- y = y & 0xFFFFFFFF;
- return y;
- }
- unsigned long P1(unsigned long x)
- {
- unsigned long y = 0;
- x = x & 0xFFFFFFFF;
- y = ROTL(x,15);
- y = x ^ y;
- y = y ^ ROTL(x,23);
-
- y = y & 0xFFFFFFFF;
-
- return y;
- }
- /*
- * 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=0, SS2=0, TT1=0, TT2=0, W[68],W1[64];
- unsigned long A=0, B=0, C=0, D=0, E=0, F=0, G=0, H=0;
- 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
- 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;
- // printf("%08X %08X %08X %08X %08X %08X j=%d\n",W[j-16],W[j-9],W[j-3],W[j-13],W[j-6],W[j],j);
- }
- #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 ) );
- }
|