comparison aes.c @ 213:e50daf73d26c libavutil

change subshift a little, this reduces the object size a litlle
author michael
date Tue, 16 Jan 2007 17:40:40 +0000
parents d31d4880105e
children db327a8aa282
comparison
equal deleted inserted replaced
212:d31d4880105e 213:e50daf73d26c
48 static inline void addkey(uint64_t state[2], uint64_t round_key[2]){ 48 static inline void addkey(uint64_t state[2], uint64_t round_key[2]){
49 state[2] = state[0] ^ round_key[0]; 49 state[2] = state[0] ^ round_key[0];
50 state[3] = state[1] ^ round_key[1]; 50 state[3] = state[1] ^ round_key[1];
51 } 51 }
52 52
53 static void subshift(uint8_t s0[2][16], uint8_t s1[2][16], uint8_t s3[2][16], uint8_t *box){ 53 static void subshift(uint8_t s0[2][16], int s, uint8_t *box){
54 uint8_t (*s1)[16]= s0[0] - s;
55 uint8_t (*s3)[16]= s0[0] + s;
54 s0[0][0]=box[s0[1][ 0]]; s0[0][ 4]=box[s0[1][ 4]]; s0[0][ 8]=box[s0[1][ 8]]; s0[0][12]=box[s0[1][12]]; 56 s0[0][0]=box[s0[1][ 0]]; s0[0][ 4]=box[s0[1][ 4]]; s0[0][ 8]=box[s0[1][ 8]]; s0[0][12]=box[s0[1][12]];
55 s1[0][0]=box[s1[1][ 4]]; s1[0][ 4]=box[s1[1][ 8]]; s1[0][ 8]=box[s1[1][12]]; s1[0][12]=box[s1[1][ 0]]; 57 s1[0][3]=box[s1[1][ 7]]; s1[0][ 7]=box[s1[1][11]]; s1[0][11]=box[s1[1][15]]; s1[0][15]=box[s1[1][ 3]];
56 s0[0][2]=box[s0[1][10]]; s0[0][10]=box[s0[1][ 2]]; s0[0][ 6]=box[s0[1][14]]; s0[0][14]=box[s0[1][ 6]]; 58 s0[0][2]=box[s0[1][10]]; s0[0][10]=box[s0[1][ 2]]; s0[0][ 6]=box[s0[1][14]]; s0[0][14]=box[s0[1][ 6]];
57 s3[0][0]=box[s3[1][12]]; s3[0][12]=box[s3[1][ 8]]; s3[0][ 8]=box[s3[1][ 4]]; s3[0][ 4]=box[s3[1][ 0]]; 59 s3[0][1]=box[s3[1][13]]; s3[0][13]=box[s3[1][ 9]]; s3[0][ 9]=box[s3[1][ 5]]; s3[0][ 5]=box[s3[1][ 1]];
58 } 60 }
59 61
60 #define ROT(x,s) ((x<<s)|(x>>(32-s))) 62 #define ROT(x,s) ((x<<s)|(x>>(32-s)))
61 #if 0 63 #if 0
62 static inline void mix(uint8_t state[4][4], uint32_t multbl[4][256]){ 64 static inline void mix(uint8_t state[4][4], uint32_t multbl[4][256]){
89 for(r=a->rounds; r>1; r--){ 91 for(r=a->rounds; r>1; r--){
90 addkey(a->state, a->round_key[r]); 92 addkey(a->state, a->round_key[r]);
91 mix(a->state, multbl, 3-s, 1+s); 93 mix(a->state, multbl, 3-s, 1+s);
92 } 94 }
93 addkey(a->state, a->round_key[1]); 95 addkey(a->state, a->round_key[1]);
94 subshift(a->state[0][0], a->state[0][0]+3-s, a->state[0][0]+1+s, sbox); 96 subshift(a->state[0][0], s, sbox);
95 addkey(a->state, a->round_key[0]); 97 addkey(a->state, a->round_key[0]);
96 } 98 }
97 99
98 static void aes_decrypt(AVAES *a){ 100 static void aes_decrypt(AVAES *a){
99 crypt(a, 0, inv_sbox, dec_multbl); 101 crypt(a, 0, inv_sbox, dec_multbl);
170 172
171 if(decrypt){ 173 if(decrypt){
172 for(i=1; i<rounds; i++){ 174 for(i=1; i<rounds; i++){
173 uint8_t tmp[2][16]; 175 uint8_t tmp[2][16];
174 memcpy(tmp[1], a->round_key[i][0], 16); 176 memcpy(tmp[1], a->round_key[i][0], 16);
175 subshift(tmp[0], tmp[0]+3, tmp[0]+1, sbox); 177 subshift(tmp[0], 0, sbox);
176 memcpy(tmp[1], tmp[0], 16); 178 memcpy(tmp[1], tmp[0], 16);
177 mix(tmp, dec_multbl, 1, 3); 179 mix(tmp, dec_multbl, 1, 3);
178 memcpy(a->round_key[i][0], tmp[0], 16); 180 memcpy(a->round_key[i][0], tmp[0], 16);
179 } 181 }
180 }else{ 182 }else{