📄 aes.c
字号:
gen_tabs(); mode = 0x08;
}
#endif
#if !defined(BLOCK_SIZE_R)
#if defined(AES_IN_CPP)
if ( !(f_dat(cx,mode) & 0x04) ) set_blk(16);
#else
if ( !(f_dat(cx,mode) & 0x04) ) set_blk(16, cx);
#endif
#endif
f_dat(cx,mode) |= (byte)f & 3; // encryption mode = enc, dec or both
f_dat(cx,Nkey) = n_bytes >> 2;
f_dat(cx,Nrnd) = (f_dat(cx,Nkey) > n_col ? f_dat(cx,Nkey) : n_col) + 6;
f_dat(cx,e_key)[0] = word_in(in_key );
f_dat(cx,e_key)[1] = word_in(in_key + 4);
f_dat(cx,e_key)[2] = word_in(in_key + 8);
f_dat(cx,e_key)[3] = word_in(in_key + 12);
k1 = f_dat(cx,e_key);
kt = k1 + n_col * (f_dat(cx,Nrnd) + 1) - f_dat(cx,Nkey);
rcp = rcon_tab;
switch ( f_dat(cx,Nkey) )
{
case 4: do
{
t = rot3(k1[3]);
k1[4] = k1[0] ^ ls_box(t) ^ *rcp++;
k1[5] = k1[1] ^ k1[4];
k1[6] = k1[2] ^ k1[5];
k1[7] = k1[3] ^ k1[6];
k1 += 4;
}
while ( k1 < kt );
break;
case 6: f_dat(cx,e_key)[4] = word_in(in_key + 16);
f_dat(cx,e_key)[5] = word_in(in_key + 20);
do
{
t = rot3(k1[5]);
k1[ 6] = k1[0] ^ ls_box(t) ^ *rcp++;
k1[ 7] = k1[1] ^ k1[ 6];
k1[ 8] = k1[2] ^ k1[ 7];
k1[ 9] = k1[3] ^ k1[ 8];
k1[10] = k1[4] ^ k1[ 9];
k1[11] = k1[5] ^ k1[10];
k1 += 6;
}
while ( k1 < kt );
break;
case 8: f_dat(cx,e_key)[4] = word_in(in_key + 16);
f_dat(cx,e_key)[5] = word_in(in_key + 20);
f_dat(cx,e_key)[6] = word_in(in_key + 24);
f_dat(cx,e_key)[7] = word_in(in_key + 28);
do
{
t = rot3(k1[7]);
k1[ 8] = k1[0] ^ ls_box(t) ^ *rcp++;
k1[ 9] = k1[1] ^ k1[ 8];
k1[10] = k1[2] ^ k1[ 9];
k1[11] = k1[3] ^ k1[10];
k1[12] = k1[4] ^ ls_box(k1[11]);
k1[13] = k1[5] ^ k1[12];
k1[14] = k1[6] ^ k1[13];
k1[15] = k1[7] ^ k1[14];
k1 += 8;
}
while ( k1 < kt );
break;
}
#if !defined(COMPACT)
if ( f_dat(cx,mode) != enc )
{
word i, u, f2, f4, f8, f9;
k1 = f_dat(cx,d_key); kt = f_dat(cx,e_key);
cpy(k1, kt);
for ( i = 1; i < f_dat(cx,Nrnd); ++i )
mix(k1, kt);
cpy(k1, kt);
}
#endif
return aes_good;
}
#if defined(COMPACT)
#define sbx_row(bi,n) bytes2word ( \
s_box[byte0(bi[n])], \
s_box[byte1(bi[fr_sh(n,1)])], \
s_box[byte2(bi[fr_sh(n,2)])], \
s_box[byte3(bi[fr_sh(n,3)])])
#define inv_sbx_row(bi,n) bytes2word( \
inv_s_box[byte0(bi[n])], \
inv_s_box[byte1(bi[ir_sh(n,1)])], \
inv_s_box[byte2(bi[ir_sh(n,2)])], \
inv_s_box[byte3(bi[ir_sh(n,3)])])
#else
#if defined(FOUR_TABLES)
#define f_rnd(x,n) ( ft_tab[0][byte0(x[n])] ^ ft_tab[1][byte1(x[fr_sh(n,1)])] ^ ft_tab[2][byte2(x[fr_sh(n,2)])] ^ ft_tab[3][byte3(x[fr_sh(n,3)])] )
#define i_rnd(x,n) ( it_tab[0][byte0(x[n])] ^ it_tab[1][byte1(x[ir_sh(n,1)])] ^ it_tab[2][byte2(x[ir_sh(n,2)])] ^ it_tab[3][byte3(x[ir_sh(n,3)])] )
#else
#define f_rnd(x,n) ( ft_tab[byte0(x[n])] ^ rot1(ft_tab[byte1(x[fr_sh(n,1)])]) ^ rot2(ft_tab[byte2(x[fr_sh(n,2)])]) ^ rot3(ft_tab[byte3(x[fr_sh(n,3)])]) )
#define i_rnd(x,n) ( it_tab[byte0(x[n])] ^ rot1(it_tab[byte1(x[ir_sh(n,1)])]) ^ rot2(it_tab[byte2(x[ir_sh(n,2)])]) ^ rot3(it_tab[byte3(x[ir_sh(n,3)])]) )
#endif
#if !defined(LAST_ROUND_TABLES)
#define lf_rnd(x,n) bytes2word(s_box[byte0(x[n])],s_box[byte1(x[fr_sh(n,1)])],s_box[byte2(x[fr_sh(n,2)])],s_box[byte3(x[fr_sh(n,3)])])
#define li_rnd(x,n) bytes2word(inv_s_box[byte0(x[n])],inv_s_box[byte1(x[ir_sh(n,1)])],inv_s_box[byte2(x[ir_sh(n,2)])],inv_s_box[byte3(x[ir_sh(n,3)])])
#elif defined(FOUR_TABLES)
#define lf_rnd(x,n) ( fl_tab[0][byte0(x[n])] ^ fl_tab[1][byte1(x[fr_sh(n,1)])] ^ fl_tab[2][byte2(x[fr_sh(n,2)])] ^ fl_tab[3][byte3(x[fr_sh(n,3)])] )
#define li_rnd(x,n) ( il_tab[0][byte0(x[n])] ^ il_tab[1][byte1(x[ir_sh(n,1)])] ^ il_tab[2][byte2(x[ir_sh(n,2)])] ^ il_tab[3][byte3(x[ir_sh(n,3)])] )
#else
#define lf_rnd(x,n) ( fl_tab[byte0(x[n])] ^ rot1(fl_tab[byte1(x[fr_sh(n,1)])]) ^ rot2(fl_tab[byte2(x[fr_sh(n,2)])]) ^ rot3(fl_tab[byte3(x[fr_sh(n,3)])]) )
#define li_rnd(x,n) ( il_tab[byte0(x[n])] ^ rot1(il_tab[byte1(x[ir_sh(n,1)])]) ^ rot2(il_tab[byte2(x[ir_sh(n,2)])]) ^ rot3(il_tab[byte3(x[ir_sh(n,3)])]) )
#endif
#endif // COMPACT
#define si(b,k,n) b[n] = word_in(in_blk + 4 * n) ^ k[n]
#define so(b,n) word_out(out_blk + 4 * n, b[n])
#define cf(bo,bi,k,n) bo[n] = mix_col(sbx_row(bi, n)) ^ k[n]
#define ci(bo,bi,k,n) bo[n] = inv_mix_col(inv_sbx_row(bi, n) ^ k[n])
#define lf(bo,bi,k,n) bo[n] = sbx_row(bi, n) ^ k[n]
#define li(bo,bi,k,n) bo[n] = inv_sbx_row(bi, n) ^ k[n]
#define fr(bo,bi,k,n) bo[n] = f_rnd(bi,n) ^ (k)[n]
#define ir(bo,bi,k,n) bo[n] = i_rnd(bi,n) ^ (k)[n]
#define fl(bo,bi,k,n) bo[n] = lf_rnd(bi,n) ^ (k)[n]
#define il(bo,bi,k,n) bo[n] = li_rnd(bi,n) ^ (k)[n]
#if !defined(BLOCK_SIZE_R)
#define state_in(b,k) \
switch(n_col) \
{ case 8: si(b,k,7); si(b,k,6); \
case 6: si(b,k,5); si(b,k,4); \
case 4: si(b,k,3); si(b,k,2); \
si(b,k,1); si(b,k,0); \
}
#define state_out(b) \
switch(n_col) \
{ case 8: so(b,7); so(b,6); \
case 6: so(b,5); so(b,4); \
case 4: so(b,3); so(b,2); \
so(b,1); so(b,0); \
}
#if defined(COMPACT)
#define cf_round(bo,bi,k) \
switch(n_col) \
{ case 8: cf(bo,bi,k,7); cf(bo,bi,k,6); \
case 6: cf(bo,bi,k,5); cf(bo,bi,k,4); \
case 4: cf(bo,bi,k,3); cf(bo,bi,k,2); \
cf(bo,bi,k,1); cf(bo,bi,k,0); \
}
#define ci_round(bo,bi,k) \
switch(n_col) \
{ case 8: ci(bo,bi,k,7); ci(bo,bi,k,6); \
case 6: ci(bo,bi,k,5); ci(bo,bi,k,4); \
case 4: ci(bo,bi,k,3); ci(bo,bi,k,2); \
ci(bo,bi,k,1); ci(bo,bi,k,0); \
}
#define lcf_round(bo,bi,k) \
switch(n_col) \
{ case 8: lf(bo,bi,k,7); lf(bo,bi,k,6); \
case 6: lf(bo,bi,k,5); lf(bo,bi,k,4); \
case 4: lf(bo,bi,k,3); lf(bo,bi,k,2); \
lf(bo,bi,k,1); lf(bo,bi,k,0); \
}
#define lci_round(bo,bi,k) \
switch(n_col) \
{ case 8: li(bo,bi,k,7); li(bo,bi,k,6); \
case 6: li(bo,bi,k,5); li(bo,bi,k,4); \
case 4: li(bo,bi,k,3); li(bo,bi,k,2); \
li(bo,bi,k,1); li(bo,bi,k,0); \
}
#else
#define f_round(bo,bi,k) \
switch(n_col) \
{ case 8: fr(bo,bi,k,7); fr(bo,bi,k,6); \
case 6: fr(bo,bi,k,5); fr(bo,bi,k,4); \
case 4: fr(bo,bi,k,3); fr(bo,bi,k,2); \
fr(bo,bi,k,1); fr(bo,bi,k,0); \
}
#define i_round(bo,bi,k) \
switch(n_col) \
{ case 8: ir(bo,bi,k,7); ir(bo,bi,k,6); \
case 6: ir(bo,bi,k,5); ir(bo,bi,k,4); \
case 4: ir(bo,bi,k,3); ir(bo,bi,k,2); \
ir(bo,bi,k,1); ir(bo,bi,k,0); \
}
#define lf_round(bo,bi,k) \
switch(n_col) \
{ case 8: fl(bo,bi,k,7); fl(bo,bi,k,6); \
case 6: fl(bo,bi,k,5); fl(bo,bi,k,4); \
case 4: fl(bo,bi,k,3); fl(bo,bi,k,2); \
fl(bo,bi,k,1); fl(bo,bi,k,0); \
}
#define li_round(bo,bi,k) \
switch(n_col) \
{ case 8: il(bo,bi,k,7); il(bo,bi,k,6); \
case 6: il(bo,bi,k,5); il(bo,bi,k,4); \
case 4: il(bo,bi,k,3); il(bo,bi,k,2); \
il(bo,bi,k,1); il(bo,bi,k,0); \
}
#endif
#elif BLOCK_SIZE_R == 16
#define state_in(b,k) si(b,k,0); si(b,k,1); si(b,k,2); si(b,k,3)
#define state_out(b) so(b,0); so(b,1); so(b,2); so(b,3)
#if defined(COMPACT)
#define cf_round(bo,bi,k) cf(bo,bi,k,0); cf(bo,bi,k,1); cf(bo,bi,k,2); cf(bo,bi,k,3)
#define ci_round(bo,bi,k) ci(bo,bi,k,3); ci(bo,bi,k,2); ci(bo,bi,k,1); ci(bo,bi,k,0)
#define lcf_round(bo,bi,k) lf(bo,bi,k,0); lf(bo,bi,k,1); lf(bo,bi,k,2); lf(bo,bi,k,3)
#define lci_round(bo,bi,k) li(bo,bi,k,3); li(bo,bi,k,2); li(bo,bi,k,1); li(bo,bi,k,0)
#else
#define f_round(bo,bi,k) fr(bo,bi,k,0); fr(bo,bi,k,1); fr(bo,bi,k,2); fr(bo,bi,k,3)
#define i_round(bo,bi,k) ir(bo,bi,k,3); ir(bo,bi,k,2); ir(bo,bi,k,1); ir(bo,bi,k,0)
#define lf_round(bo,bi,k) fl(bo,bi,k,0); fl(bo,bi,k,1); fl(bo,bi,k,2); fl(bo,bi,k,3)
#define li_round(bo,bi,k) il(bo,bi,k,3); il(bo,bi,k,2); il(bo,bi,k,1); il(bo,bi,k,0)
#endif
#elif BLOCK_SIZE_R == 24
#define state_in(b,k) si(b,k,0); si(b,k,1); si(b,k,2); si(b,k,3); si(b,k,4); si(b,k,5)
#define state_out(b) so(b,0); so(b,1); so(b,2); so(b,3); so(b,4); so(b,5)
#if defined(COMPACT)
#define cf_round(bo,bi,k) cf(bo,bi,k,0); cf(bo,bi,k,1); cf(bo,bi,k,2); cf(bo,bi,k,3); \
cf(bo,bi,k,4); cf(bo,bi,k,5)
#define ci_round(bo,bi,k) ci(bo,bi,k,5); ci(bo,bi,k,4); ci(bo,bi,k,3); ci(bo,bi,k,2); \
ci(bo,bi,k,1); ci(bo,bi,k,0)
#define lcf_round(bo,bi,k) lf(bo,bi,k,0); lf(bo,bi,k,1); lf(bo,bi,k,2); lf(bo,bi,k,3); \
lf(bo,bi,k,4); lf(bo,bi,k,5)
#define lci_round(bo,bi,k) li(bo,bi,k,5); li(bo,bi,k,4); li(bo,bi,k,3); li(bo,bi,k,2); \
li(bo,bi,k,1); li(bo,bi,k,0)
#else
#define f_round(bo,bi,k) fr(bo,bi,k,0); fr(bo,bi,k,1); fr(bo,bi,k,2); fr(bo,bi,k,3); \
fr(bo,bi,k,4); fr(bo,bi,k,5)
#define i_round(bo,bi,k) ir(bo,bi,k,5); ir(bo,bi,k,4); ir(bo,bi,k,3); ir(bo,bi,k,2); \
ir(bo,bi,k,1); ir(bo,bi,k,0)
#define lf_round(bo,bi,k) fl(bo,bi,k,0); fl(bo,bi,k,1); fl(bo,bi,k,2); fl(bo,bi,k,3); \
fl(bo,bi,k,4); fl(bo,bi,k,5)
#define li_round(bo,bi,k) il(bo,bi,k,5); il(bo,bi,k,4); il(bo,bi,k,3); il(bo,bi,k,2); \
il(bo,bi,k,1); il(bo,bi,k,0)
#endif
#elif BLOCK_SIZE_R == 32
#define state_in(b,k) si(b,k,0); si(b,k,1); si(b,k,2); si(b,k,3); si(b,k,4); si(b,k,5); si(b,k,6); si(b,k,7)
#define state_out(b) so(b,0); so(b,1); so(b,2); so(b,3); so(b,4); so(b,5); so(b,6); so(b,7)
#if defined(COMPACT)
#define cf_round(bo,bi,k) cf(bo,bi,k,0); cf(bo,bi,k,1); cf(bo,bi,k,2); cf(bo,bi,k,3); \
cf(bo,bi,k,4); cf(bo,bi,k,5); cf(bo,bi,k,6); cf(bo,bi,k,7)
#define ci_round(bo,bi,k) ci(bo,bi,k,7); ci(bo,bi,k,6); ci(bo,bi,k,5); ci(bo,bi,k,4); \
ci(bo,bi,k,3); ci(bo,bi,k,2); ci(bo,bi,k,1); ci(bo,bi,k,0)
#define lcf_round(bo,bi,k) lf(bo,bi,k,0); lf(bo,bi,k,1); lf(bo,bi,k,2); lf(bo,bi,k,3); \
lf(bo,bi,k,4); lf(bo,bi,k,5); lf(bo,bi,k,6); lf(bo,bi,k,7)
#define lci_round(bo,bi,k) li(bo,bi,k,7); li(bo,bi,k,6); li(bo,bi,k,5); li(bo,bi,k,4); \
li(bo,bi,k,3); li(bo,bi,k,2); li(bo,bi,k,1); li(bo,bi,k,0);
#else
#define f_round(bo,bi,k) fr(bo,bi,k,0); fr(bo,bi,k,1); fr(bo,bi,k,2); fr(bo,bi,k,3); \
fr(bo,bi,k,4); fr(bo,bi,k,5); fr(bo,bi,k,6); fr(bo,bi,k,7)
#define i_round(bo,bi,k) ir(bo,bi,k,7); ir(bo,bi,k,6); ir(bo,bi,k,5); ir(bo,bi,k,4); \
ir(bo,bi,k,3); ir(bo,bi,k,2); ir(bo,bi,k,1); ir(bo,bi,k,0)
#define lf_round(bo,bi,k) fl(bo,bi,k,0); fl(bo,bi,k,1); fl(bo,bi,k,2); fl(bo,bi,k,3); \
fl(bo,bi,k,4); fl(bo,bi,k,5); fl(bo,bi,k,6); fl(bo,bi,k,7)
#define li_round(bo,bi,k) il(bo,bi,k,7); il(bo,bi,k,6); il(bo,bi,k,5); il(bo,bi,k,4); \
il(bo,bi,k,3); il(bo,bi,k,2); il(bo,bi,k,1); il(bo,bi,k,0)
#endif
#else
#error Illegal Block Size
#endif
#if defined(AES_IN_CPP)
rval aes::_encrypt(const byte in_blk[], byte out_blk[]) const
#else
cf_dec _encrypt(const byte in_blk[], byte out_blk[], const aes *cx)
#endif
{
word b0[Mcol], b1[Mcol];
const word *kp = f_dat(cx,e_key);
if ( !(f_dat(cx,mode) & 0x01) ) return aes_bad;
state_in(b0, kp);
kp += n_col;
#if defined(COMPACT)
{ word *p0 = b0, *p1 = b1, *t, rnd, u, f2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -