• 大小: 1.01MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-20
  • 语言: C/C++
  • 标签: SM3  

资源简介

杂凑算法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(“\n“);
printf(“%02x“ a[i]);
}
printf(“\n“);
}

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

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轮分组的消息;\n“ 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(“\n扩展后信息W:\n“);
printBit32(W 68);
for(int j = 0; j < 64; j ++)
W_[j] = W[j] ^ W[j + 4];
printf(“\n扩展后信息W_:\n“);
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(“\n迭代压缩中间值\n“);
    printf(“ j    A        B        C        D        E        F        G        H    \n“);
printf(“   %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n“ 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\n“ 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  sm3\Debug\
     文件       33792  2012-09-16 18:41  sm3\Debug\sm3.exe
     文件      322344  2012-09-16 18:41  sm3\Debug\sm3.ilk
     文件      445440  2012-09-16 18:41  sm3\Debug\sm3.pdb
     目录           0  2013-10-21 12:22  sm3\ipch\
     目录           0  2013-10-21 12:22  sm3\ipch\sm3-550e18fd\
     文件     2359296  2013-10-21 12:22  sm3\ipch\sm3-550e18fd\sm3-9390d8df.ipch
     目录           0  2012-03-23 18:57  sm3\sm3\
     目录           0  2012-09-16 18:41  sm3\sm3\Debug\
     文件        4028  2012-09-16 18:41  sm3\sm3\Debug\CL.read.1.tlog
     文件        1366  2012-09-16 18:41  sm3\sm3\Debug\CL.write.1.tlog
     文件        1406  2012-09-16 18:41  sm3\sm3\Debug\cl.command.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link-cvtres.read.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link-cvtres.write.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.1596-cvtres.read.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.1596-cvtres.write.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.1596.read.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.1596.write.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.1716-cvtres.read.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.1716-cvtres.write.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.1716.read.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.1716.write.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.3232-cvtres.read.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.3232-cvtres.write.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.3232.read.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.3232.write.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.360-cvtres.read.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.360-cvtres.write.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.360.read.1.tlog
     文件           2  2012-09-16 18:41  sm3\sm3\Debug\link.360.write.1.tlog
............此处省略44个文件信息

评论

共有 条评论