国密SM3算法在linux和windows平台结果不一致问题
什么是sm3,是一种类似于sha256的哈希算法,是咱们国家的哈希标准算法;
最近在使用sm3算法时,同样的一份数据,调用同样的sm3接口,发现得到的结果是不一样的;
那么在应用过的过程中,如果同样的算法出的结果不一样,那验证签名业务就会不过,出问题;
最后发现是sm3算法在内部使用了unsigned long,
这里要注意 long, 在 windows64平台下占用4个字节 ;
而long, 在linux64平台下占用8个字节;
这种差异直接导致了windows和linux结果的差异;
原sm3.cpp 中使用了 unsigned long ,如下
1 2 3 4 5 6 7 8 9 | #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 |
//解决方式就是,修改sm3.cpp,将所有unsigned long 改成 unsigned int;
这样即支持linux和windows平台就保持一致了;
修改后的源码
sm3.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | /** * \file sm3.h * thanks to Xyssl * SM3 standards:http://www.oscca.gov.cn/News/201012/News_1199.htm * author:goldboar * email:goldboar@163.com * 2011-10-26 */ #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 */ |
sm3.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | /* * 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 "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 int ) (b)[(i) ] << 24 ) \ | ( (unsigned int ) (b)[(i) + 1] << 16 ) \ | ( (unsigned int ) (b)[(i) + 2] << 8 ) \ | ( (unsigned int ) (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 int SS1, SS2, TT1, TT2, W[68],W1[64]; unsigned int A, B, C, D, E, F, G, H; unsigned int T[64]; unsigned int 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 int 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 int ) 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 int last, padn; unsigned int 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 ) ); } |
测试代码test.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h> #include "sm3.h" int main () { //test sm3_hash unsigned char tp[2] = {0x1,0x2}; unsigned char hspV[2014] = {0}; int hspL = 0; sm3(tp, 2, hspV); FILE *fp = NULL; fp = fopen ( "./sm3hsv.dat" , "wb" ); fwrite (hspV,1,32,fp); fclose (fp); printf ( "sm3=%d\n" ,hspL); return 0; } |
最后通过md5比较生成的hash文件即可验证;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具