comparison aes.c @ 216:1c3c3b1b9596 libavutil

cbc support
author michael
date Tue, 16 Jan 2007 19:08:52 +0000
parents 2f388c3535e5
children 26de83603cfa
comparison
equal deleted inserted replaced
215:2f388c3535e5 216:1c3c3b1b9596
83 ^multbl[2][state[1][0][2]] ^ multbl[3][state[1][s1 ][3]]; 83 ^multbl[2][state[1][0][2]] ^ multbl[3][state[1][s1 ][3]];
84 ((uint32_t *)(state))[3] = multbl[0][state[1][3][0]] ^ multbl[1][state[1][s1-1][1]] 84 ((uint32_t *)(state))[3] = multbl[0][state[1][3][0]] ^ multbl[1][state[1][s1-1][1]]
85 ^multbl[2][state[1][1][2]] ^ multbl[3][state[1][s3-1][3]]; 85 ^multbl[2][state[1][1][2]] ^ multbl[3][state[1][s3-1][3]];
86 } 86 }
87 87
88 static inline void crypt(AVAES *a, int s, uint8_t *sbox, uint32_t *multbl, uint8_t *dst, uint8_t *src){ 88 static inline void crypt(AVAES *a, int s, uint8_t *sbox, uint32_t *multbl){
89 int r; 89 int r;
90 90
91 addkey(a->state[1], src, a->round_key[a->rounds]);
92 for(r=a->rounds-1; r>0; r--){ 91 for(r=a->rounds-1; r>0; r--){
93 mix(a->state, multbl, 3-s, 1+s); 92 mix(a->state, multbl, 3-s, 1+s);
94 addkey(a->state[1], a->state[0], a->round_key[r]); 93 addkey(a->state[1], a->state[0], a->round_key[r]);
95 } 94 }
96 subshift(a->state[0][0], s, sbox); 95 subshift(a->state[0][0], s, sbox);
97 addkey(dst, a->state[0], a->round_key[0]); 96 }
98 } 97
99 98 void aes_crypt(AVAES *a, uint8_t *dst, uint8_t *src, int count, uint8_t *iv, int decrypt){
100 static void aes_decrypt(AVAES *a, uint8_t *dst, uint8_t *src){ 99 while(count--){
101 crypt(a, 0, inv_sbox, dec_multbl, dst, src); 100 addkey(a->state[1], src, a->round_key[a->rounds]);
102 } 101 if(decrypt) {
103 102 crypt(a, 0, inv_sbox, dec_multbl);
104 static void aes_encrypt(AVAES *a, uint8_t *dst, uint8_t *src){ 103 if(iv){
105 crypt(a, 2, sbox, enc_multbl, dst, src); 104 addkey(a->state[0], a->state[0], iv);
105 memcpy(iv, src, 16);
106 }
107 addkey(dst, a->state[0], a->round_key[0]);
108 }else{
109 if(iv) addkey(a->state[1], a->state[1], iv);
110 crypt(a, 2, sbox, enc_multbl);
111 addkey(dst, a->state[0], a->round_key[0]);
112 if(iv) memcpy(iv, dst, 16);
113 }
114 src+=16;
115 dst+=16;
116 }
106 } 117 }
107 118
108 static void init_multbl2(uint8_t tbl[1024], int c[4], uint8_t *log8, uint8_t *alog8, uint8_t *sbox){ 119 static void init_multbl2(uint8_t tbl[1024], int c[4], uint8_t *log8, uint8_t *alog8, uint8_t *sbox){
109 int i, j; 120 int i, j;
110 for(i=0; i<1024; i++){ 121 for(i=0; i<1024; i++){
209 av_aes_init(&ad, "PI=3.141592654..", 128, 1); 220 av_aes_init(&ad, "PI=3.141592654..", 128, 1);
210 av_log_level= AV_LOG_DEBUG; 221 av_log_level= AV_LOG_DEBUG;
211 222
212 for(i=0; i<2; i++){ 223 for(i=0; i<2; i++){
213 av_aes_init(&b, rkey[i], 128, 1); 224 av_aes_init(&b, rkey[i], 128, 1);
214 aes_decrypt(&b, temp, rct[i]); 225 aes_crypt(&b, temp, rct[i], 1, NULL, 1);
215 for(j=0; j<16; j++) 226 for(j=0; j<16; j++)
216 if(rpt[i][j] != temp[j]) 227 if(rpt[i][j] != temp[j])
217 av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n", j, rpt[i][j], temp[j]); 228 av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n", j, rpt[i][j], temp[j]);
218 } 229 }
219 230
220 for(i=0; i<10000; i++){ 231 for(i=0; i<10000; i++){
221 for(j=0; j<16; j++){ 232 for(j=0; j<16; j++){
222 pt[j]= random(); 233 pt[j]= random();
223 } 234 }
224 {START_TIMER 235 {START_TIMER
225 aes_encrypt(&ae, temp, pt); 236 aes_crypt(&ae, temp, pt, 1, NULL, 0);
226 if(!(i&(i-1))) 237 if(!(i&(i-1)))
227 av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n", temp[0], temp[5], temp[10], temp[15]); 238 av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n", temp[0], temp[5], temp[10], temp[15]);
228 aes_decrypt(&ad, temp, temp); 239 aes_crypt(&ad, temp, temp, 1, NULL, 1);
229 STOP_TIMER("aes")} 240 STOP_TIMER("aes")}
230 for(j=0; j<16; j++){ 241 for(j=0; j<16; j++){
231 if(pt[j] != temp[j]){ 242 if(pt[j] != temp[j]){
232 av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n", i,j, pt[j], temp[j]); 243 av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n", i,j, pt[j], temp[j]);
233 } 244 }