资源简介
杂凑算法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\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
文件 2 2012-09-16 18:41 sm3\sm3\Debug\li
............此处省略44个文件信息
相关资源
- c编译器,将C语言编译成masm32汇编语言
- SM3算法C语言实现
- 国密SM3摘要算法工具
- SM3算法IP核
- SM2/SM3算法C语言实现
- SM2_SM3_SM4_C语言实现+SM3_C++实现+国家密
- c 编译器 masm32 汇编 可自举
- sm2加密,解密,签名,验签sm3哈希基
- SM2_SM3_SM4_C语言实现
- SM3密码算法C语言实现
- SM2SM3SM4国密算法C语言实现VS2008
- 国密算法 SM2_SM3_SM4 C语言实现
- Sm2_sm3_sm4_c语言实现.zip
- SM2SM3;SM4;国密算法的C语言实现.rar
- SM3算法
- linux下C语言实现SM9国密算法
- 基于C语言实现国密SM3算法
- MASM32 TOOL editbin.exe
评论
共有 条评论