📄 aesvec.c
字号:
if(outf)
{
if(round < 100)
fprintf(outf, "\nR[%02i].%s", round, xt);
for(col = 0; col < Ncol; ++col)
for(row = 0; row < 4; ++row)
fprintf(outf, "%c%c", hxx[(bval(b[col][row],0) >> 4) & 15], hxx[bval(b[col][row],0) & 15]);
}
}
void bytes_out(const char* xt, const byte b[], const word len)
{ word i;
if(outf)
{
fprintf(outf, "\n%s", xt);
for(i = 0; i < len; ++i)
{
fprintf(outf, "%c%c", hxx[(b[i] >> 4) & 15], hxx[b[i] & 15]);
}
}
}
#endif
// The Forward Cipher
cf_dec c_name(encrypt)(const byte input[], byte output[], const c_name(aes) *cx)
{ byte state[Mcol][4];
word Shr[Nrow], rnd;
if(!(cx->mode & 0x01)) return aes_bad;
Shr[1] = 1;
Shr[2] = (nc == 8 ? 3 : 2);
Shr[3] = (nc > 6 ? 4 : 3);
GetState(state, input, nc);
block_out(0, "input ", state, nc);
XorRoundKey(state, cx->e_key, nc);
block_out(0, "k_sch ", (cwa_ptr*)(cx->e_key), nc);
for(rnd = 1; rnd < cx->Nrnd; ++rnd)
{
block_out(rnd, "start ", state, nc);
SubBytes(state, nc);
block_out(rnd, "s_box ", state, nc);
ShiftRows(state, nc, Shr);
block_out(rnd, "s_row ", state, nc);
MixColumns(state, nc);
block_out(rnd, "m_col ", state, nc);
XorRoundKey(state, cx->e_key + rnd * nc, nc);
block_out(rnd, "k_sch ", (cwa_ptr*)(cx->e_key + rnd * nc), nc);
}
block_out(cx->Nrnd, "start ", state, nc);
SubBytes(state, nc);
block_out(cx->Nrnd, "s_box ", state, nc);
ShiftRows(state, nc, Shr);
block_out(cx->Nrnd, "s_row ", state, nc);
XorRoundKey(state, cx->e_key + cx->Nrnd * nc, nc);
block_out(cx->Nrnd, "k_sch ", (cwa_ptr*)(cx->e_key + cx->Nrnd * nc), nc);
PutState(state, output, nc);
block_out(cx->Nrnd, "output ", state, nc);
return aes_good;
}
// The Inverse Cipher (in normal and modified equivalent form)
cf_dec c_name(decrypt)(const byte input[], byte output[], const c_name(aes) *cx)
{ byte state[Mcol][4];
word Shr[Nrow], rnd;
if(!(cx->mode & 0x02)) return aes_bad;
Shr[1] = 1;
Shr[2] = (nc == 8 ? 3 : 2);
Shr[3] = (nc > 6 ? 4 : 3);
GetState(state, input, nc);
block_out(0, "iinput ", state, nc);
XorRoundKey(state, cx->e_key + cx->Nrnd * nc, nc);
block_out(0, "ik_sch ",
(cwa_ptr*)(cx->e_key + cx->Nrnd * nc), nc);
if(mod)
{
for(rnd = cx->Nrnd - 1; rnd > 0; --rnd)
{
block_out(cx->Nrnd - rnd, "istart ", state, nc);
InvSubBytes(state, nc);
block_out(cx->Nrnd - rnd, "is_box ", state, nc);
InvShiftRows(state, nc, Shr);
block_out(cx->Nrnd - rnd, "is_row ", state, nc);
InvMixColumns(state, nc);
block_out(cx->Nrnd - rnd, "im_col ", state, nc);
XorRoundKey(state, cx->d_key + rnd * nc, nc);
block_out(cx->Nrnd - rnd, "ik_sch ",
(cwa_ptr*)(cx->d_key + rnd * nc), nc);
}
block_out(cx->Nrnd, "istart ", state, nc);
InvSubBytes(state, nc);
block_out(cx->Nrnd, "is_box ", state, nc);
InvShiftRows(state, nc, Shr);
block_out(cx->Nrnd, "is_row ", state, nc);
}
else
{
for(rnd = cx->Nrnd - 1; rnd > 0; --rnd)
{
block_out(cx->Nrnd - rnd, "istart ", state, nc);
InvShiftRows(state, nc, Shr);
block_out(cx->Nrnd - rnd, "is_row ", state, nc);
InvSubBytes(state, nc);
block_out(cx->Nrnd - rnd, "is_box ", state, nc);
XorRoundKey(state, cx->e_key + rnd * nc, nc);
block_out(cx->Nrnd - rnd, "ik_sch ",
(cwa_ptr*)(cx->e_key + rnd * nc), nc);
block_out(cx->Nrnd - rnd, "ik_add ", state, nc);
InvMixColumns(state, nc);
}
block_out(cx->Nrnd, "istart ", state, nc);
InvShiftRows(state, nc, Shr);
block_out(cx->Nrnd, "is_row ", state, nc);
InvSubBytes(state, nc);
block_out(cx->Nrnd, "is_box ", state, nc);
}
XorRoundKey(state, cx->e_key, nc);
block_out(cx->Nrnd, "ik_sch ", (cwa_ptr*)(cx->e_key), nc);
PutState(state, output, nc);
block_out(cx->Nrnd, "ioutput ", state, nc);
return aes_good;
}
byte pih[32] = // hex digits of pi
{
0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
0x4a, 0x40, 0x93, 0x82, 0x22, 0x99, 0xf3, 0x1d,
0x00, 0x82, 0xef, 0xa9, 0x8e, 0xc4, 0xe6, 0xc8
};
byte exh[32] = // hex digits of e
{
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
0x76, 0x2e, 0x71, 0x60, 0xf3, 0x8b, 0x4d, 0xa5,
0x6a, 0x78, 0x4d, 0x90, 0x45, 0x19, 0x0c, 0xfe
};
byte res[5][5][32] =
{
{
{ 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb,
0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32
},
{
0x23, 0x1d, 0x84, 0x46, 0x39, 0xb3, 0x1b, 0x41,
0x22, 0x11, 0xcf, 0xe9, 0x37, 0x12, 0xb8, 0x80
},
{ 0xf9, 0xfb, 0x29, 0xae, 0xfc, 0x38, 0x4a, 0x25,
0x03, 0x40, 0xd8, 0x33, 0xb8, 0x7e, 0xbc, 0x00
},
{
0x8f, 0xaa, 0x8f, 0xe4, 0xde, 0xe9, 0xeb, 0x17,
0xca, 0xa4, 0x79, 0x75, 0x02, 0xfc, 0x9d, 0x3f
},
{ 0x1a, 0x6e, 0x6c, 0x2c, 0x66, 0x2e, 0x7d, 0xa6,
0x50, 0x1f, 0xfb, 0x62, 0xbc, 0x9e, 0x93, 0xf3
}
},
{
{
0x16, 0xe7, 0x3a, 0xec, 0x92, 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90, 0x76, 0xb6, 0x9a, 0x46,
0xff, 0xb3, 0xb3, 0xbe, 0xae, 0x97, 0xad, 0x83,
0x13, 0xf6, 0x22, 0xf6, 0x7f, 0xed, 0xb4, 0x87,
0xde, 0x9f, 0x06, 0xb9, 0xed, 0x9c, 0x8f, 0x19
},
{
0x51, 0x4f, 0x93, 0xfb, 0x29, 0x6b, 0x5a, 0xd1,
0x6a, 0xa7, 0xdf, 0x8b, 0x57, 0x7a, 0xbc, 0xbd,
0x48, 0x4d, 0xec, 0xac, 0xcc, 0xc7, 0xfb, 0x1f,
0x18, 0xdc, 0x56, 0x73, 0x09, 0xce, 0xef, 0xfd
},
{ 0x5d, 0x71, 0x01, 0x72, 0x7b, 0xb2, 0x57, 0x81,
0xbf, 0x67, 0x15, 0xb0, 0xe6, 0x95, 0x52, 0x82,
0xb9, 0x61, 0x0e, 0x23, 0xa4, 0x3c, 0x2e, 0xb0,
0x62, 0x69, 0x9f, 0x0e, 0xbf, 0x58, 0x87, 0xb2
},
{
0xd5, 0x6c, 0x5a, 0x63, 0x62, 0x74, 0x32, 0x57,
0x9e, 0x1d, 0xd3, 0x08, 0xb2, 0xc8, 0xf1, 0x57,
0xb4, 0x0a, 0x4b, 0xfb, 0x56, 0xfe, 0xa1, 0x37,
0x7b, 0x25, 0xd3, 0xed, 0x3d, 0x6d, 0xbf, 0x80
},
{ 0xa4, 0x94, 0x06, 0x11, 0x5d, 0xfb, 0x30, 0xa4,
0x04, 0x18, 0xaa, 0xfa, 0x48, 0x69, 0xb7, 0xc6,
0xa8, 0x86, 0xff, 0x31, 0x60, 0x2a, 0x7d, 0xd1,
0x9c, 0x88, 0x9d, 0xc6, 0x4f, 0x7e, 0x4e, 0x7a
}
}
};
int main(void)
{ byte out[32], ret[32], err = 0;
word i, j;
c_name(aes) alg;
outf = fopen("dev_vec.txt", "w");
fprintf(outf, "\nThese test have been generated by Dr Brian Gladman using the");
fprintf(outf, "\nprogram aes_vec.cpp <brg@gladman.uk.net> 24th May 2001.\n");
fprintf(outf, "\nLEGEND FOR CIPHER ENCRYPT - round r = 0 to R, R = 10..14");
fprintf(outf, "\ninput: cipher input");
fprintf(outf, "\nstart: state at start of round[r]");
fprintf(outf, "\ns_box: state after s_box substitution");
fprintf(outf, "\ns_row: state after shift row transformation");
fprintf(outf, "\nm_col: state after mix column transformation");
fprintf(outf, "\nk_sch: key schedule value for round[r]");
fprintf(outf, "\noutput: cipher output\n");
fprintf(outf, "\nLEGEND FOR CIPHER DECRYPT - round r = 0 to R, R = 10..14");
fprintf(outf, "\nKEY SCHEDULE FOR KEY XOR FOLLOWED BY INVERSE MIX COLUMN");
fprintf(outf, "\niinput: inverse cipher input");
fprintf(outf, "\nistart: state at start of round[r]");
fprintf(outf, "\nis_box: state after inverse s_box substitution");
fprintf(outf, "\nis_row: state after inverse shift row transformation");
fprintf(outf, "\nik_sch: key schedule value for round[r]");
fprintf(outf, "\nik_add: state after key addition");
fprintf(outf, "\nioutput: cipher output\n");
fprintf(outf, "\nLEGEND FOR CIPHER DECRYPT (MOD) - round r = 0 to R, R = 10..14");
fprintf(outf, "\nKEY SCHEDULE FOR INVERSE MIX COLUMN FOLLOWED BY KEY XOR");
fprintf(outf, "\niinput: inverse cipher input");
fprintf(outf, "\nistart: state at start of round[r]");
fprintf(outf, "\nis_box: state after inverse s_box substitution");
fprintf(outf, "\nis_row: state after inverse shift row transformation");
fprintf(outf, "\nim_col: state after inverse mix column transformation");
fprintf(outf, "\nik_sch: key schedule value for round[r]");
fprintf(outf, "\nioutput: cipher output\n");
for(i = 0; i < 5; ++i)
{
#if !defined(BLOCK_SIZE)
f_set_blk(&alg, 16 + 4 * i);
#endif
for(j = 0; j < 5; ++j)
{
f_set_key(&alg, exh, 16 + 4 * j, both);
bytes_out("PLAINTEXT: ", pih, 16 + 4 * i);
bytes_out("KEY: ", exh, 16 + 4 * j);
fprintf(outf, "\nENCRYPT %2i byte block, %2i byte key", 16 + 4 * i, 16 + 4 * j);
f_encrypt(&alg, pih, out);
if(memcmp(out, res[i][j], 16 + 4 * i)) err++;
fprintf(outf, "\nDECRYPT %2i byte block, %2i byte key", 16 + 4 * i, 16 + 4 * j);
mod = 0; f_decrypt(&alg, out, ret);
if(memcmp(ret, pih, 16 + 4 * i)) err++;
fprintf(outf, "\nDECRYPT (MOD) %2i byte block, %2i byte key", 16 + 4 * i, 16 + 4 * j);
mod = 1; f_decrypt(&alg, out, ret);
if(memcmp(ret, pih, 16 + 4 * i)) err++;
fprintf(outf, "\n");
}
}
fclose(outf);
printf("\nThese values are %s", (err ? "in error\n\n" : "correct\n\n"));
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -