杂凑算法SM3的C++实现


杂凑算法SM3的C++实现,并验证正确。
资源截图
代码片段和文件信息
#include “sm3.h“

Bit32 FF(Bit32 X Bit32 Y Bit32 Z int j)
{
if(j < 16)
return X ^ Y ^ Z;
else
return (X & Y) | (X & Z) | (Y & Z);
}

Bit32 GG(Bit32 X Bit32 Y Bit32 Z int j)
{
if(j < 16)
return X ^ Y ^ Z;
else
return (X & Y) | (~X & Z);
}

Bit32 LShift(Bit32 X int n)
{
return (X << n) | (X >> (32 - n));
}

Bit32 P0(Bit32 X)
{
return X ^ (LShift(X 9)) ^ (LShift(X 17));
}

Bit32 P1(Bit32 X)
{
return X ^ (LShift(X 15)) ^ (LShift(X 23));
}

void printBit8(Bit8 *a Bit32 len)
{
for(Bit32 i = 0; i < len; i ++)
{
if(i != 0 && i %  4 == 0)
printf(“ “);
if(i != 0 && i % 32 == 0)
printf(“
“);
printf(“%02x“ a[i]);
}
printf(“
“);
}

void printBit32(Bit32 *a Bit32 len)
{
for(Bit32 i = 0; i < len; i ++)
{
if(i != 0 && i % 8 == 0)
printf(“
“);
printf(“%08x “ a[i]);
}
printf(“
“);
}

void Bit8_Bit32(Bit32 *X Bit8 *Y size_t len)
{
for(size_t i = 0 j = 0; j < len; i++ j += 4)

X[i] = (((Bit32)Y[j]) << 24) | (((Bit32)Y[j + 1]) << 16) |
    (((Bit32)Y[j + 2]) << 8) | ((Bit32)Y[j + 3]);

}

void Bit32_to_Bit8(Bit8 *X Bit32 *Y size_t len)
{
for(size_t i = 0 j = 0; j < len; i ++ j += 4) 
{
X[j] = (Bit8)(Y[i] >> 24 & 0xff);
X[j + 1] = (Bit8)(Y[i] >> 16 & 0xff);
X[j + 2] = (Bit8)(Y[i] >>  8 & 0xff);
X[j + 3] = (Bit8)(Y[i]     & 0xff);
}
}

void SM3_Compress(SM3_CTX *ctx)
{
    Bit32 A B C D E F G H T SS1 SS2 TT1 TT2 W[68] W_[64];
static int i = 1;
printf(“第%2d轮分组的消息;
“ i++);
printBit8(ctx->buf 64);
    Bit8_Bit32(W ctx->buf 64);
for(int j = 16; j < 68; j ++)
W[j] = P1(W[j - 16] ^ W[j - 9] ^ LShift(W[j - 3] 15)) ^ LShift(W[j - 13]7) ^ W[j - 6];    
    printf(“
扩展后信息W:
“);
printBit32(W 68);
for(int j = 0; j < 64; j ++)
W_[j] = W[j] ^ W[j + 4];
printf(“
扩展后信息W_:
“);
printBit32(W_64);
A =  ctx->V[0]; B = ctx->V[1]; C = ctx->V[2]; D = ctx->V[3];
E =  ctx->V[4]; F = ctx->V[5]; G = ctx->V[6]; H = ctx->V[7];
printf(“
迭代压缩中间值
“);
    printf(“ j    A        B        C        D        E        F        G        H    
“);
printf(“   %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx
“ A B C D E F G H);
for(int j = 0; j < 64; j ++)
{
if(j < 16)
T = 0x79cc4519;
else
T = 0x7a879d8a;
SS1 = LShift(LShift(A 12) + E + LShift(T j) 7);
SS2 = SS1 ^ LShift(A 12);
TT1 = FF(A B C j) + D + SS2 + W_[j];
TT2 = GG(E F G j) + H + SS1 + W[j];
D = C;
C = LShift(B 9);
B = A;
A = TT1;
H = G;
G = LShift(F 19);
F = E;
E = P0(TT2);
printf(“%2d %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx
“ j A B C D E F G H);
}
ctx->V[0] ^= A; ctx->V[1] ^= B; ctx->V[2] ^= C; ctx->V[3] ^= D;
ctx->V[4] ^= E; ctx->V[5] ^= F; ctx->V[6] ^= G; ctx->V[7] ^= H;
memset(ctx->buf 0 64);
ctx->curlen = 0; 
}

int SM3_Init(SM3_CTX* ctx)
{
ctx->V[0] = 0x7380166f;
ctx->V[1] = 0x4914b

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-10-21 12:23  sm3
     目录           0  2012-03-30 18:21  sm3Debug
     文件       33792  2012-09-16 18:41  sm3Debugsm3.exe
     文件      322344  2012-09-16 18:41  sm3Debugsm3.ilk
     文件      445440  2012-09-16 18:41  sm3Debugsm3.pdb
     目录           0  2013-10-21 12:22  sm3ipch
     目录           0  2013-10-21 12:22  sm3ipchsm3-550e18fd
     文件     2359296  2013-10-21 12:22  sm3ipchsm3-550e18fdsm3-9390d8df.ipch
     目录           0  2012-03-23 18:57  sm3sm3
     目录           0  2012-09-16 18:41  sm3sm3Debug
     文件        4028  2012-09-16 18:41  sm3sm3DebugCL.read.1.tlog
     文件        1366  2012-09-16 18:41  sm3sm3DebugCL.write.1.tlog
     文件        1406  2012-09-16 18:41  sm3sm3Debugcl.command.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink-cvtres.read.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink-cvtres.write.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.1596-cvtres.read.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.1596-cvtres.write.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.1596.read.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.1596.write.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.1716-cvtres.read.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.1716-cvtres.write.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.1716.read.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.1716.write.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.3232-cvtres.read.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.3232-cvtres.write.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.3232.read.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.3232.write.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.360-cvtres.read.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.360-cvtres.write.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.360.read.1.tlog
     文件           2  2012-09-16 18:41  sm3sm3Debuglink.360.write.1.tlog
............此处省略44个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)