sm3.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * SM3 Hash alogrith
  3. * thanks to Xyssl
  4. * author:goldboar
  5. * email:goldboar@163.com
  6. * 2011-10-26
  7. */
  8. //Testing data from SM3 Standards
  9. //http://www.oscca.gov.cn/News/201012/News_1199.htm
  10. // Sample 1
  11. // Input:"abc"
  12. // Output:66c7f0f4 62eeedd9 d1f2d46b dc10e4e2 4167c487 5cf2f7a2 297da02b 8f4ba8e0
  13. // Sample 2
  14. // Input:"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"
  15. // Outpuf:debe9ff9 2275b8a1 38604889 c18e5a4d 6fdb70e5 387e5765 293dcba3 9c0c5732
  16. #include "StdAfx.h"
  17. #include "sm3.h"
  18. #include <string.h>
  19. #include <stdio.h>
  20. /*
  21. * 32-bit integer manipulation macros (big endian)
  22. */
  23. #ifndef GET_ULONG_BE
  24. #define GET_ULONG_BE(n,b,i) \
  25. { \
  26. (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
  27. | ( (unsigned long) (b)[(i) + 1] << 16 ) \
  28. | ( (unsigned long) (b)[(i) + 2] << 8 ) \
  29. | ( (unsigned long) (b)[(i) + 3] ); \
  30. }
  31. #endif
  32. #ifndef PUT_ULONG_BE
  33. #define PUT_ULONG_BE(n,b,i) \
  34. { \
  35. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  36. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  37. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  38. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  39. }
  40. #endif
  41. /*
  42. #define FF0(x,y,z) ( (x) ^ (y) ^ (z))
  43. #define FF1(x,y,z) (((x) & (y)) | ( (x) & (z)) | ( (y) & (z)))
  44. #define GG0(x,y,z) ( (x) ^ (y) ^ (z))
  45. #define GG1(x,y,z) (((x) & (y)) | ( (~(x)) & (z)) )
  46. #define SHL(x,n) (((x) & 0xFFFFFFFF) << n)
  47. #define ROTL(x,n) (SHL((x),n) | ((x) >> (32 - n)))
  48. #define P0(x) ((x) ^ ROTL((x),9) ^ ROTL((x),17))
  49. #define P1(x) ((x) ^ ROTL((x),15) ^ ROTL((x),23))
  50. */
  51. unsigned long GG0(unsigned long x,unsigned long y,unsigned long z)
  52. {
  53. unsigned long u = 0;
  54. x = x & 0xFFFFFFFF;
  55. y = y & 0xFFFFFFFF;
  56. z = z & 0xFFFFFFFF;
  57. u = x ^ y ^ z;
  58. u = u & 0xFFFFFFFF;
  59. return u;
  60. }
  61. unsigned long GG1(unsigned long x,unsigned long y,unsigned long z)
  62. {
  63. unsigned long u = 0,v = 0;
  64. x = x & 0xFFFFFFFF;
  65. y = y & 0xFFFFFFFF;
  66. z = z & 0xFFFFFFFF;
  67. u = x & y;
  68. v = (~x) & z;
  69. u = u | v;
  70. u = u & 0xFFFFFFFF;
  71. return u;
  72. }
  73. unsigned long FF0(unsigned long x,unsigned long y,unsigned long z)
  74. {
  75. unsigned long u = 0;
  76. x = x & 0xFFFFFFFF;
  77. y = y & 0xFFFFFFFF;
  78. z = z & 0xFFFFFFFF;
  79. u = x ^ y ^ z;
  80. u = u & 0xFFFFFFFF;
  81. return u;
  82. }
  83. unsigned long FF1(unsigned long x,unsigned long y,unsigned long z)
  84. {
  85. unsigned long u = 0,v = 0;
  86. x = x & 0xFFFFFFFF;
  87. y = y & 0xFFFFFFFF;
  88. z = z & 0xFFFFFFFF;
  89. u = x & y;
  90. v = x & z;
  91. u = u | v;
  92. v = y & z;
  93. u = u | v;
  94. u = u & 0xFFFFFFFF;
  95. return u;
  96. }
  97. unsigned long ROTL(unsigned long x,int n)
  98. {
  99. unsigned long y = 0;
  100. x = x & 0xFFFFFFFF;
  101. n = n%32;
  102. y = (x &0xFFFFFFFF) << n;
  103. y = y | (x >> (32-n));
  104. y = y & 0xFFFFFFFF;
  105. return y;
  106. }
  107. unsigned long P0(unsigned long x)
  108. {
  109. unsigned long y = 0;
  110. x = x & 0xFFFFFFFF;
  111. y = ROTL(x,9);
  112. y = x ^ y;
  113. y = y ^ ROTL(x,17);
  114. y = y & 0xFFFFFFFF;
  115. return y;
  116. }
  117. unsigned long P1(unsigned long x)
  118. {
  119. unsigned long y = 0;
  120. x = x & 0xFFFFFFFF;
  121. y = ROTL(x,15);
  122. y = x ^ y;
  123. y = y ^ ROTL(x,23);
  124. y = y & 0xFFFFFFFF;
  125. return y;
  126. }
  127. /*
  128. * SM3 context setup
  129. */
  130. void sm3_starts( sm3_context *ctx )
  131. {
  132. ctx->total[0] = 0;
  133. ctx->total[1] = 0;
  134. ctx->state[0] = 0x7380166F;
  135. ctx->state[1] = 0x4914B2B9;
  136. ctx->state[2] = 0x172442D7;
  137. ctx->state[3] = 0xDA8A0600;
  138. ctx->state[4] = 0xA96F30BC;
  139. ctx->state[5] = 0x163138AA;
  140. ctx->state[6] = 0xE38DEE4D;
  141. ctx->state[7] = 0xB0FB0E4E;
  142. }
  143. static void sm3_process( sm3_context *ctx, unsigned char data[64] )
  144. {
  145. unsigned long SS1=0, SS2=0, TT1=0, TT2=0, W[68],W1[64];
  146. unsigned long A=0, B=0, C=0, D=0, E=0, F=0, G=0, H=0;
  147. unsigned long T[64];
  148. unsigned long Temp1,Temp2,Temp3,Temp4,Temp5;
  149. int j;
  150. #ifdef _DEBUG
  151. int i;
  152. #endif
  153. // for(j=0; j < 68; j++)
  154. // W[j] = 0;
  155. // for(j=0; j < 64; j++)
  156. // W1[j] = 0;
  157. for(j = 0; j < 16; j++)
  158. T[j] = 0x79CC4519;
  159. for(j =16; j < 64; j++)
  160. T[j] = 0x7A879D8A;
  161. GET_ULONG_BE( W[ 0], data, 0 );
  162. GET_ULONG_BE( W[ 1], data, 4 );
  163. GET_ULONG_BE( W[ 2], data, 8 );
  164. GET_ULONG_BE( W[ 3], data, 12 );
  165. GET_ULONG_BE( W[ 4], data, 16 );
  166. GET_ULONG_BE( W[ 5], data, 20 );
  167. GET_ULONG_BE( W[ 6], data, 24 );
  168. GET_ULONG_BE( W[ 7], data, 28 );
  169. GET_ULONG_BE( W[ 8], data, 32 );
  170. GET_ULONG_BE( W[ 9], data, 36 );
  171. GET_ULONG_BE( W[10], data, 40 );
  172. GET_ULONG_BE( W[11], data, 44 );
  173. GET_ULONG_BE( W[12], data, 48 );
  174. GET_ULONG_BE( W[13], data, 52 );
  175. GET_ULONG_BE( W[14], data, 56 );
  176. GET_ULONG_BE( W[15], data, 60 );
  177. #ifdef _DEBUG
  178. printf("Message with padding:\n");
  179. for(i=0; i< 8; i++)
  180. printf("%08x ",W[i]);
  181. printf("\n");
  182. for(i=8; i< 16; i++)
  183. printf("%08x ",W[i]);
  184. printf("\n");
  185. #endif
  186. for(j = 16; j < 68; j++ )
  187. {
  188. //W[j] = P1( W[j-16] ^ W[j-9] ^ ROTL(W[j-3],15)) ^ ROTL(W[j - 13],7 ) ^ W[j-6];
  189. //Why thd release's result is different with the debug's ?
  190. //Below is okay. Interesting, Perhaps VC6 has a bug of Optimizaiton.
  191. Temp1 = W[j-16] ^ W[j-9];
  192. Temp2 = ROTL(W[j-3],15);
  193. Temp3 = Temp1 ^ Temp2;
  194. Temp4 = P1(Temp3);
  195. Temp5 = ROTL(W[j - 13],7 ) ^ W[j-6];
  196. W[j] = Temp4 ^ Temp5;
  197. // 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);
  198. }
  199. #ifdef _DEBUG
  200. printf("Expanding message W0-67:\n");
  201. for(i=0; i<68; i++)
  202. {
  203. printf("%08x ",W[i]);
  204. if(((i+1) % 8) == 0) printf("\n");
  205. }
  206. printf("\n");
  207. #endif
  208. for(j = 0; j < 64; j++)
  209. {
  210. W1[j] = W[j] ^ W[j+4];
  211. }
  212. #ifdef _DEBUG
  213. printf("Expanding message W'0-63:\n");
  214. for(i=0; i<64; i++)
  215. {
  216. printf("%08x ",W1[i]);
  217. if(((i+1) % 8) == 0) printf("\n");
  218. }
  219. printf("\n");
  220. #endif
  221. A = ctx->state[0];
  222. B = ctx->state[1];
  223. C = ctx->state[2];
  224. D = ctx->state[3];
  225. E = ctx->state[4];
  226. F = ctx->state[5];
  227. G = ctx->state[6];
  228. H = ctx->state[7];
  229. #ifdef _DEBUG
  230. printf("j A B C D E F G H\n");
  231. printf(" %08x %08x %08x %08x %08x %08x %08x %08x\n",A,B,C,D,E,F,G,H);
  232. #endif
  233. for(j =0; j < 16; j++)
  234. {
  235. SS1 = ROTL((ROTL(A,12) + E + ROTL(T[j],j)), 7);
  236. SS2 = SS1 ^ ROTL(A,12);
  237. TT1 = FF0(A,B,C) + D + SS2 + W1[j];
  238. TT2 = GG0(E,F,G) + H + SS1 + W[j];
  239. D = C;
  240. C = ROTL(B,9);
  241. B = A;
  242. A = TT1;
  243. H = G;
  244. G = ROTL(F,19);
  245. F = E;
  246. E = P0(TT2);
  247. #ifdef _DEBUG
  248. printf("%02d %08x %08x %08x %08x %08x %08x %08x %08x\n",j,A,B,C,D,E,F,G,H);
  249. #endif
  250. }
  251. for(j =16; j < 64; j++)
  252. {
  253. SS1 = ROTL((ROTL(A,12) + E + ROTL(T[j],j)), 7);
  254. SS2 = SS1 ^ ROTL(A,12);
  255. TT1 = FF1(A,B,C) + D + SS2 + W1[j];
  256. TT2 = GG1(E,F,G) + H + SS1 + W[j];
  257. D = C;
  258. C = ROTL(B,9);
  259. B = A;
  260. A = TT1;
  261. H = G;
  262. G = ROTL(F,19);
  263. F = E;
  264. E = P0(TT2);
  265. #ifdef _DEBUG
  266. printf("%02d %08x %08x %08x %08x %08x %08x %08x %08x\n",j,A,B,C,D,E,F,G,H);
  267. #endif
  268. }
  269. ctx->state[0] ^= A;
  270. ctx->state[1] ^= B;
  271. ctx->state[2] ^= C;
  272. ctx->state[3] ^= D;
  273. ctx->state[4] ^= E;
  274. ctx->state[5] ^= F;
  275. ctx->state[6] ^= G;
  276. ctx->state[7] ^= H;
  277. #ifdef _DEBUG
  278. printf(" %08x %08x %08x %08x %08x %08x %08x %08x\n",ctx->state[0],ctx->state[1],ctx->state[2],
  279. ctx->state[3],ctx->state[4],ctx->state[5],ctx->state[6],ctx->state[7]);
  280. #endif
  281. }
  282. /*
  283. * SM3 process buffer
  284. */
  285. void sm3_update( sm3_context *ctx, unsigned char *input, int ilen )
  286. {
  287. int fill;
  288. unsigned long left;
  289. if( ilen <= 0 )
  290. return;
  291. left = ctx->total[0] & 0x3F;
  292. fill = 64 - left;
  293. ctx->total[0] += ilen;
  294. ctx->total[0] &= 0xFFFFFFFF;
  295. if( ctx->total[0] < (unsigned long) ilen )
  296. ctx->total[1]++;
  297. if( left && ilen >= fill )
  298. {
  299. memcpy( (void *) (ctx->buffer + left),
  300. (void *) input, fill );
  301. sm3_process( ctx, ctx->buffer );
  302. input += fill;
  303. ilen -= fill;
  304. left = 0;
  305. }
  306. while( ilen >= 64 )
  307. {
  308. sm3_process( ctx, input );
  309. input += 64;
  310. ilen -= 64;
  311. }
  312. if( ilen > 0 )
  313. {
  314. memcpy( (void *) (ctx->buffer + left),
  315. (void *) input, ilen );
  316. }
  317. }
  318. static const unsigned char sm3_padding[64] =
  319. {
  320. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  321. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  322. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  323. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  324. };
  325. /*
  326. * SM3 final digest
  327. */
  328. void sm3_finish( sm3_context *ctx, unsigned char output[32] )
  329. {
  330. unsigned long last, padn;
  331. unsigned long high, low;
  332. unsigned char msglen[8];
  333. high = ( ctx->total[0] >> 29 )
  334. | ( ctx->total[1] << 3 );
  335. low = ( ctx->total[0] << 3 );
  336. PUT_ULONG_BE( high, msglen, 0 );
  337. PUT_ULONG_BE( low, msglen, 4 );
  338. last = ctx->total[0] & 0x3F;
  339. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  340. sm3_update( ctx, (unsigned char *) sm3_padding, padn );
  341. sm3_update( ctx, msglen, 8 );
  342. PUT_ULONG_BE( ctx->state[0], output, 0 );
  343. PUT_ULONG_BE( ctx->state[1], output, 4 );
  344. PUT_ULONG_BE( ctx->state[2], output, 8 );
  345. PUT_ULONG_BE( ctx->state[3], output, 12 );
  346. PUT_ULONG_BE( ctx->state[4], output, 16 );
  347. PUT_ULONG_BE( ctx->state[5], output, 20 );
  348. PUT_ULONG_BE( ctx->state[6], output, 24 );
  349. PUT_ULONG_BE( ctx->state[7], output, 28 );
  350. }
  351. /*
  352. * output = SM3( input buffer )
  353. */
  354. void sm3( unsigned char *input, int ilen,
  355. unsigned char output[32] )
  356. {
  357. sm3_context ctx;
  358. sm3_starts( &ctx );
  359. sm3_update( &ctx, input, ilen );
  360. sm3_finish( &ctx, output );
  361. memset( &ctx, 0, sizeof( sm3_context ) );
  362. }
  363. /*
  364. * output = SM3( file contents )
  365. */
  366. int sm3_file( char *path, unsigned char output[32] )
  367. {
  368. FILE *f;
  369. size_t n;
  370. sm3_context ctx;
  371. unsigned char buf[1024];
  372. if( ( f = fopen( path, "rb" ) ) == NULL )
  373. return( 1 );
  374. sm3_starts( &ctx );
  375. while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
  376. sm3_update( &ctx, buf, (int) n );
  377. sm3_finish( &ctx, output );
  378. memset( &ctx, 0, sizeof( sm3_context ) );
  379. if( ferror( f ) != 0 )
  380. {
  381. fclose( f );
  382. return( 2 );
  383. }
  384. fclose( f );
  385. return( 0 );
  386. }
  387. /*
  388. * SM3 HMAC context setup
  389. */
  390. void sm3_hmac_starts( sm3_context *ctx, unsigned char *key, int keylen )
  391. {
  392. int i;
  393. unsigned char sum[32];
  394. if( keylen > 64 )
  395. {
  396. sm3( key, keylen, sum );
  397. keylen = 32;
  398. //keylen = ( is224 ) ? 28 : 32;
  399. key = sum;
  400. }
  401. memset( ctx->ipad, 0x36, 64 );
  402. memset( ctx->opad, 0x5C, 64 );
  403. for( i = 0; i < keylen; i++ )
  404. {
  405. ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
  406. ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
  407. }
  408. sm3_starts( ctx);
  409. sm3_update( ctx, ctx->ipad, 64 );
  410. memset( sum, 0, sizeof( sum ) );
  411. }
  412. /*
  413. * SM3 HMAC process buffer
  414. */
  415. void sm3_hmac_update( sm3_context *ctx, unsigned char *input, int ilen )
  416. {
  417. sm3_update( ctx, input, ilen );
  418. }
  419. /*
  420. * SM3 HMAC final digest
  421. */
  422. void sm3_hmac_finish( sm3_context *ctx, unsigned char output[32] )
  423. {
  424. int hlen;
  425. unsigned char tmpbuf[32];
  426. //is224 = ctx->is224;
  427. hlen = 32;
  428. sm3_finish( ctx, tmpbuf );
  429. sm3_starts( ctx );
  430. sm3_update( ctx, ctx->opad, 64 );
  431. sm3_update( ctx, tmpbuf, hlen );
  432. sm3_finish( ctx, output );
  433. memset( tmpbuf, 0, sizeof( tmpbuf ) );
  434. }
  435. /*
  436. * output = HMAC-SM#( hmac key, input buffer )
  437. */
  438. void sm3_hmac( unsigned char *key, int keylen,
  439. unsigned char *input, int ilen,
  440. unsigned char output[32] )
  441. {
  442. sm3_context ctx;
  443. sm3_hmac_starts( &ctx, key, keylen);
  444. sm3_hmac_update( &ctx, input, ilen );
  445. sm3_hmac_finish( &ctx, output );
  446. memset( &ctx, 0, sizeof( sm3_context ) );
  447. }