📄 aesopt.h
字号:
#if 1 /* set tables for the last encryption round */
#define LAST_ENC_ROUND FOUR_TABLES
#elif 0
#define LAST_ENC_ROUND ONE_TABLE
#else
#define LAST_ENC_ROUND NO_TABLES
#endif
#if 1 /* set tables for the normal decryption round */
#define DEC_ROUND FOUR_TABLES
#elif 0
#define DEC_ROUND ONE_TABLE
#else
#define DEC_ROUND NO_TABLES
#endif
#if 1 /* set tables for the last decryption round */
#define LAST_DEC_ROUND FOUR_TABLES
#elif 0
#define LAST_DEC_ROUND ONE_TABLE
#else
#define LAST_DEC_ROUND NO_TABLES
#endif
/* The decryption key schedule can be speeded up with tables in the same
way that the round functions can. Include or exclude the following
defines to set this requirement.
*/
#if 1
#define KEY_SCHED FOUR_TABLES
#elif 0
#define KEY_SCHED ONE_TABLE
#else
#define KEY_SCHED NO_TABLES
#endif
/* END OF CONFIGURATION OPTIONS */
#define NO_TABLES 0 /* DO NOT CHANGE */
#define ONE_TABLE 1 /* DO NOT CHANGE */
#define FOUR_TABLES 4 /* DO NOT CHANGE */
#define NONE 0 /* DO NOT CHANGE */
#define PARTIAL 1 /* DO NOT CHANGE */
#define FULL 2 /* DO NOT CHANGE */
#if defined(BLOCK_SIZE) && ((BLOCK_SIZE & 3) || BLOCK_SIZE < 16 || BLOCK_SIZE > 32)
#error An illegal block size has been specified.
#endif
#if !defined(BLOCK_SIZE)
#define RC_LENGTH 29
#else
#define RC_LENGTH 5 * BLOCK_SIZE / 4 - (BLOCK_SIZE == 16 ? 10 : 11)
#endif
/* Disable at least some poor combinations of options */
#if ENC_ROUND == NO_TABLES && LAST_ENC_ROUND != NO_TABLES
#undef LAST_ENC_ROUND
#define LAST_ENC_ROUND NO_TABLES
#elif ENC_ROUND == ONE_TABLE && LAST_ENC_ROUND == FOUR_TABLES
#undef LAST_ENC_ROUND
#define LAST_ENC_ROUND ONE_TABLE
#endif
#if ENC_ROUND == NO_TABLES && ENC_UNROLL != NONE
#undef ENC_UNROLL
#define ENC_UNROLL NONE
#endif
#if DEC_ROUND == NO_TABLES && LAST_DEC_ROUND != NO_TABLES
#undef LAST_DEC_ROUND
#define LAST_DEC_ROUND NO_TABLES
#elif DEC_ROUND == ONE_TABLE && LAST_DEC_ROUND == FOUR_TABLES
#undef LAST_DEC_ROUND
#define LAST_DEC_ROUND ONE_TABLE
#endif
#if DEC_ROUND == NO_TABLES && DEC_UNROLL != NONE
#undef DEC_UNROLL
#define DEC_UNROLL NONE
#endif
/* upr(x,n): rotates bytes within words by n positions, moving bytes to
higher index positions with wrap around into low positions
ups(x,n): moves bytes by n positions to higher index positions in
words but without wrap around
bval(x,n): extracts a byte from a word
NOTE: The definitions given here are intended only for use with
unsigned variables and with shift counts that are compile
time constants
*/
#if (INTERNAL_BYTE_ORDER == AES_LITTLE_ENDIAN)
#if defined(_MSC_VER)
#define upr(x,n) _lrotl((aes_32t)(x), 8 * (n))
#else
#define upr(x,n) ((aes_32t)(x) << 8 * (n) | (aes_32t)(x) >> 32 - 8 * (n))
#endif
#define ups(x,n) ((aes_32t)(x) << 8 * (n))
#define bval(x,n) ((aes_08t)((x) >> 8 * (n)))
#define bytes2word(b0, b1, b2, b3) \
(((aes_32t)(b3) << 24) | ((aes_32t)(b2) << 16) | ((aes_32t)(b1) << 8) | (b0))
#endif
#if (INTERNAL_BYTE_ORDER == AES_BIG_ENDIAN)
#define upr(x,n) ((aes_32t)(x) >> 8 * (n) | (aes_32t)(x) << 32 - 8 * (n))
#define ups(x,n) ((aes_32t)(x) >> 8 * (n)))
#define bval(x,n) ((aes_08t)((x) >> 24 - 8 * (n)))
#define bytes2word(b0, b1, b2, b3) \
(((aes_32t)(b0) << 24) | ((aes_32t)(b1) << 16) | ((aes_32t)(b2) << 8) | (b3))
#endif
#if defined(SAFE_IO)
#define word_in(x) bytes2word((x)[0], (x)[1], (x)[2], (x)[3])
#define word_out(x,v) { (x)[0] = bval(v,0); (x)[1] = bval(v,1); \
(x)[2] = bval(v,2); (x)[3] = bval(v,3); }
#elif (INTERNAL_BYTE_ORDER == PLATFORM_BYTE_ORDER)
#define word_in(x) *(aes_32t*)(x)
#define word_out(x,v) *(aes_32t*)(x) = (v)
#else
#if !defined(bswap_32)
#if !defined(_MSC_VER)
#define _lrotl(x,n) ((aes_32t)(x) << n | (aes_32t)(x) >> 32 - n)
#endif
#define bswap_32(x) ((_lrotl((x),8) & 0x00ff00ff) | (_lrotl((x),24) & 0xff00ff00))
#endif
#define word_in(x) bswap_32(*(aes_32t*)(x))
#define word_out(x,v) *(aes_32t*)(x) = bswap_32(v)
#endif
/* the finite field modular polynomial and elements */
#define WPOLY 0x011b
#define BPOLY 0x1b
/* multiply four bytes in GF(2^8) by 'x' {02} in parallel */
#define m1 0x80808080
#define m2 0x7f7f7f7f
#define FFmulX(x) ((((x) & m2) << 1) ^ ((((x) & m1) >> 7) * BPOLY))
/* The following defines provide alternative definitions of FFmulX that might
give improved performance if a fast 32-bit multiply is not available. Note
that a temporary variable u needs to be defined where FFmulX is used.
#define FFmulX(x) (u = (x) & m1, u |= (u >> 1), ((x) & m2) << 1) ^ ((u >> 3) | (u >> 6))
#define m4 (0x01010101 * BPOLY)
#define FFmulX(x) (u = (x) & m1, ((x) & m2) << 1) ^ ((u - (u >> 7)) & m4)
*/
/* Work out which tables are needed for the different options */
#ifdef AES_ASM
#ifdef ENC_ROUND
#undef ENC_ROUND
#endif
#define ENC_ROUND FOUR_TABLES
#ifdef LAST_ENC_ROUND
#undef LAST_ENC_ROUND
#endif
#define LAST_ENC_ROUND FOUR_TABLES
#ifdef DEC_ROUND
#undef DEC_ROUND
#endif
#define DEC_ROUND FOUR_TABLES
#ifdef LAST_DEC_ROUND
#undef LAST_DEC_ROUND
#endif
#define LAST_DEC_ROUND FOUR_TABLES
#ifdef KEY_SCHED
#undef KEY_SCHED
#define KEY_SCHED FOUR_TABLES
#endif
#endif
#if defined(ENCRYPTION) || defined(AES_ASM)
#if ENC_ROUND == ONE_TABLE
#define FT1_SET
#elif ENC_ROUND == FOUR_TABLES
#define FT4_SET
#else
#define SBX_SET
#endif
#if LAST_ENC_ROUND == ONE_TABLE
#define FL1_SET
#elif LAST_ENC_ROUND == FOUR_TABLES
#define FL4_SET
#elif !defined(SBX_SET)
#define SBX_SET
#endif
#endif
#if defined(DECRYPTION) || defined(AES_ASM)
#if DEC_ROUND == ONE_TABLE
#define IT1_SET
#elif DEC_ROUND == FOUR_TABLES
#define IT4_SET
#else
#define ISB_SET
#endif
#if LAST_DEC_ROUND == ONE_TABLE
#define IL1_SET
#elif LAST_DEC_ROUND == FOUR_TABLES
#define IL4_SET
#elif !defined(ISB_SET)
#define ISB_SET
#endif
#endif
#if defined(ENCRYPTION_KEY_SCHEDULE) || defined(DECRYPTION_KEY_SCHEDULE)
#if KEY_SCHED == ONE_TABLE
#define LS1_SET
#define IM1_SET
#elif KEY_SCHED == FOUR_TABLES
#define LS4_SET
#define IM4_SET
#elif !defined(SBX_SET)
#define SBX_SET
#endif
#endif
#ifdef FIXED_TABLES
#define prefx extern const
#else
#define prefx extern
extern aes_08t tab_init;
void gen_tabs(void);
#endif
prefx aes_32t rcon_tab[29];
#ifdef SBX_SET
prefx aes_08t s_box[256];
#endif
#ifdef ISB_SET
prefx aes_08t inv_s_box[256];
#endif
#ifdef FT1_SET
prefx aes_32t ft_tab[256];
#endif
#ifdef FT4_SET
prefx aes_32t ft_tab[4][256];
#endif
#ifdef FL1_SET
prefx aes_32t fl_tab[256];
#endif
#ifdef FL4_SET
prefx aes_32t fl_tab[4][256];
#endif
#ifdef IT1_SET
prefx aes_32t it_tab[256];
#endif
#ifdef IT4_SET
prefx aes_32t it_tab[4][256];
#endif
#ifdef IL1_SET
prefx aes_32t il_tab[256];
#endif
#ifdef IL4_SET
prefx aes_32t il_tab[4][256];
#endif
#ifdef LS1_SET
#ifdef FL1_SET
#undef LS1_SET
#else
prefx aes_32t ls_tab[256];
#endif
#endif
#ifdef LS4_SET
#ifdef FL4_SET
#undef LS4_SET
#else
prefx aes_32t ls_tab[4][256];
#endif
#endif
#ifdef IM1_SET
prefx aes_32t im_tab[256];
#endif
#ifdef IM4_SET
prefx aes_32t im_tab[4][256];
#endif
/* Set the number of columns in nc. Note that it is important
that nc is a constant which is known at compile time if the
highest speed version of the code is needed.
*/
#if defined(BLOCK_SIZE)
#define nc (BLOCK_SIZE >> 2)
#else
#define nc (cx->n_blk >> 2)
#endif
/* generic definitions of Rijndael macros that use tables */
#define no_table(x,box,vf,rf,c) bytes2word( \
box[bval(vf(x,0,c),rf(0,c))], \
box[bval(vf(x,1,c),rf(1,c))], \
box[bval(vf(x,2,c),rf(2,c))], \
box[bval(vf(x,3,c),rf(3,c))])
#define one_table(x,op,tab,vf,rf,c) \
( tab[bval(vf(x,0,c),rf(0,c))] \
^ op(tab[bval(vf(x,1,c),rf(1,c))],1) \
^ op(tab[bval(vf(x,2,c),rf(2,c))],2) \
^ op(tab[bval(vf(x,3,c),rf(3,c))],3))
#define four_tables(x,tab,vf,rf,c) \
( tab[0][bval(vf(x,0,c),rf(0,c))] \
^ tab[1][bval(vf(x,1,c),rf(1,c))] \
^ tab[2][bval(vf(x,2,c),rf(2,c))] \
^ tab[3][bval(vf(x,3,c),rf(3,c))])
#define vf1(x,r,c) (x)
#define rf1(r,c) (r)
#define rf2(r,c) ((r-c)&3)
/* perform forward and inverse column mix operation on four bytes in long word x in */
/* parallel. NOTE: x must be a simple variable, NOT an expression in these macros. */
#define dec_fmvars
#if defined(FM4_SET) /* not currently used */
#define fwd_mcol(x) four_tables(x,fm_tab,vf1,rf1,0)
#elif defined(FM1_SET) /* not currently used */
#define fwd_mcol(x) one_table(x,upr,fm_tab,vf1,rf1,0)
#else
#undef dec_fmvars
#define dec_fmvars aes_32t f1, f2;
#define fwd_mcol(x) (f1 = (x), f2 = FFmulX(f1), f2 ^ upr(f1 ^ f2, 3) ^ upr(f1, 2) ^ upr(f1, 1))
#endif
#define dec_imvars
#if defined(IM4_SET)
#define inv_mcol(x) four_tables(x,im_tab,vf1,rf1,0)
#elif defined(IM1_SET)
#define inv_mcol(x) one_table(x,upr,im_tab,vf1,rf1,0)
#else
#undef dec_imvars
#define dec_imvars aes_32t f2, f4, f8, f9;
#define inv_mcol(x) \
(f9 = (x), f2 = FFmulX(f9), f4 = FFmulX(f2), f8 = FFmulX(f4), f9 ^= f8, \
f2 ^= f4 ^ f8 ^ upr(f2 ^ f9,3) ^ upr(f4 ^ f9,2) ^ upr(f9,1))
#endif
#if defined(FL4_SET)
#define ls_box(x,c) four_tables(x,fl_tab,vf1,rf2,c)
#elif defined(LS4_SET)
#define ls_box(x,c) four_tables(x,ls_tab,vf1,rf2,c)
#elif defined(FL1_SET)
#define ls_box(x,c) one_table(x,upr,fl_tab,vf1,rf2,c)
#elif defined(LS1_SET)
#define ls_box(x,c) one_table(x,upr,ls_tab,vf1,rf2,c)
#else
#define ls_box(x,c) no_table(x,s_box,vf1,rf2,c)
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -