我用C语言实现了SPECK密码64/96。
我认为,加密和解密运行得很好。
然而,当我检查论文中的测试向量时,我的实现是错误的。(这篇论文是The Simon and Speck Families of Lightweight Block Ciphers的--可能还有其他来源。)我不知道我哪里出错了。
我错过了什么?
我的实现是:
#define ROR(x, r) ((x >> r) | (x << (32-r)))
#define ROL(x, r) ((x << r) | (x >> (32-r)))
#define ROUNDS 26
#define u32 unsigned int
void Speck64Encrypt(u32 ct[], const u32 pt[], const u32 K[]){
u32 i;
u32 L[ROUNDS + 1] = { 0, };
u32 RK[ROUNDS] = { 0, };
RK[0] = K[0];
L[0] = K[1];
L[1] = K[2];
for (i = 0; i < ROUNDS - 1; i++)
{
L[i + 2] = (RK[i] + ROR(L[i], 8)) ^ i;
RK[i + 1] = ROL(RK[i], 3) ^ L[i + 2];
} // Round Key Generation!!!
ct[0] = pt[0];
ct[1] = pt[1];
for (i = 0; i < ROUNDS; i++)
{
ct[1] = (ROR(ct[1], 8) + ct[0]) ^ RK[i];
ct[0] = ROL(ct[0], 3) ^ ct[1];
}
}
int main()
{
u32 k[3] = { 0x13121110, 0x0b0a0908, 0x03020100 };
u32 pt[2] = { 0x74614620, 0x736e6165 };
u32 ct[2] = {0, };
Speck64Encrypt(ct, pt, k);
printf("Expect : 0x9f7952ec, 0x4175946c\n");
printf("Result : %08x, %08x \n", ct[0], ct[1]);
return 0;
}发布于 2018-03-13 09:02:49
您已经反向输入了key的单词和明文。你应该有K0=03020100,K1=0b0a0908,K2=13121110。还有,pt1=74614620,pt0=736e6165。
https://stackoverflow.com/questions/49216696
复制相似问题