From 0c5d2819a5b124ba4692f2fb1c2f1298fd373b8c Mon Sep 17 00:00:00 2001 From: Michael Niedermayer Date: Sun, 14 Jan 2007 19:39:38 +0000 Subject: [PATCH] merge encrypt and decrypt so the source is simpler and the compiler can choose with inlining if it wants speed or small size Originally committed as revision 7494 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavutil/aes.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/libavutil/aes.c b/libavutil/aes.c index a54cab1..1697e9e 100644 --- a/libavutil/aes.c +++ b/libavutil/aes.c @@ -71,40 +71,30 @@ static inline void mix(uint8_t state[4][4], uint32_t multbl[4][256]){ #endif } -void av_aes_decrypt(AVAES *a){ +static inline void crypt(AVAES *a, int s, uint8_t *sbox, uint32_t *multbl){ int t, r; for(r=a->rounds; r>1; r--){ addkey(a->state, a->round_key[r]); - SUBSHIFT3x((a->state[0]+1)) + SUBSHIFT3x((a->state[0]+1+s)) SUBSHIFT2x((a->state[0]+2)) - SUBSHIFT1x((a->state[0]+3)) - mix(a->state, dec_multbl); + SUBSHIFT1x((a->state[0]+3-s)) + mix(a->state, multbl); } addkey(a->state, a->round_key[1]); - SUBSHIFT0((a->state[0]+0), inv_sbox) - SUBSHIFT3((a->state[0]+1), inv_sbox) - SUBSHIFT2((a->state[0]+2), inv_sbox) - SUBSHIFT1((a->state[0]+3), inv_sbox) + SUBSHIFT0((a->state[0]+0 ), sbox) + SUBSHIFT3((a->state[0]+1+s), sbox) + SUBSHIFT2((a->state[0]+2 ), sbox) + SUBSHIFT1((a->state[0]+3-s), sbox) addkey(a->state, a->round_key[0]); } -void av_aes_encrypt(AVAES *a){ - int r, t; +void av_aes_decrypt(AVAES *a){ + crypt(a, 0, inv_sbox, dec_multbl); +} - for(r=0; rrounds-1; r++){ - addkey(a->state, a->round_key[r]); - SUBSHIFT1x((a->state[0]+1)) - SUBSHIFT2x((a->state[0]+2)) - SUBSHIFT3x((a->state[0]+3)) - mix(a->state, enc_multbl); - } - addkey(a->state, a->round_key[r]); - SUBSHIFT0((a->state[0]+0), sbox) - SUBSHIFT1((a->state[0]+1), sbox) - SUBSHIFT2((a->state[0]+2), sbox) - SUBSHIFT3((a->state[0]+3), sbox) - addkey(a->state, a->round_key[r+1]); +void av_aes_encrypt(AVAES *a){ + crypt(a, 2, sbox, enc_multbl); } static init_multbl2(uint8_t tbl[1024], int c[4], uint8_t *log8, uint8_t *alog8, uint8_t *sbox){ @@ -188,6 +178,11 @@ AVAES *av_aes_init(uint8_t *key, int key_bits, int decrypt) { a->round_key[i][0][j]= sbox[a->round_key[i][0][j]]; mix(a->round_key[i], dec_multbl); } + }else{ + for(i=0; i<(rounds+1)/2; i++){ + for(j=0; j<16; j++) + FFSWAP(int, a->round_key[i][0][j], a->round_key[rounds-i][0][j]); + } } return a; -- 2.7.4