Mercurial > libavutil.hg
comparison aes.c @ 214:db327a8aa282 libavutil
give crypt a src and dst
same speed, 100bytes larger object file
author | michael |
---|---|
date | Tue, 16 Jan 2007 17:51:54 +0000 |
parents | e50daf73d26c |
children | 2f388c3535e5 |
comparison
equal
deleted
inserted
replaced
213:e50daf73d26c | 214:db327a8aa282 |
---|---|
43 #else | 43 #else |
44 static uint32_t enc_multbl[4][256]; | 44 static uint32_t enc_multbl[4][256]; |
45 static uint32_t dec_multbl[4][256]; | 45 static uint32_t dec_multbl[4][256]; |
46 #endif | 46 #endif |
47 | 47 |
48 static inline void addkey(uint64_t state[2], uint64_t round_key[2]){ | 48 static inline void addkey(uint64_t dst[2], uint64_t src[2], uint64_t round_key[2]){ |
49 state[2] = state[0] ^ round_key[0]; | 49 dst[0] = src[0] ^ round_key[0]; |
50 state[3] = state[1] ^ round_key[1]; | 50 dst[1] = src[1] ^ round_key[1]; |
51 } | 51 } |
52 | 52 |
53 static void subshift(uint8_t s0[2][16], int s, 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; | 54 uint8_t (*s1)[16]= s0[0] - s; |
55 uint8_t (*s3)[16]= s0[0] + s; | 55 uint8_t (*s3)[16]= s0[0] + s; |
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){ | 88 static inline void crypt(AVAES *a, int s, uint8_t *sbox, uint32_t *multbl, uint8_t *dst, uint8_t *src){ |
89 int r; | 89 int r; |
90 | 90 |
91 for(r=a->rounds; r>1; r--){ | 91 addkey(a->state[1], src, a->round_key[a->rounds]); |
92 addkey(a->state, a->round_key[r]); | 92 for(r=a->rounds-1; r>0; r--){ |
93 mix(a->state, multbl, 3-s, 1+s); | 93 mix(a->state, multbl, 3-s, 1+s); |
94 } | 94 addkey(a->state[1], a->state[0], a->round_key[r]); |
95 addkey(a->state, a->round_key[1]); | 95 } |
96 subshift(a->state[0][0], s, sbox); | 96 subshift(a->state[0][0], s, sbox); |
97 addkey(a->state, a->round_key[0]); | 97 addkey(dst, a->state[0], a->round_key[0]); |
98 } | 98 } |
99 | 99 |
100 static void aes_decrypt(AVAES *a){ | 100 static void aes_decrypt(AVAES *a, uint8_t *dst, uint8_t *src){ |
101 crypt(a, 0, inv_sbox, dec_multbl); | 101 crypt(a, 0, inv_sbox, dec_multbl, dst, src); |
102 } | 102 } |
103 | 103 |
104 static void aes_encrypt(AVAES *a){ | 104 static void aes_encrypt(AVAES *a, uint8_t *dst, uint8_t *src){ |
105 crypt(a, 2, sbox, enc_multbl); | 105 crypt(a, 2, sbox, enc_multbl, dst, src); |
106 } | 106 } |
107 | 107 |
108 static void init_multbl2(uint8_t tbl[1024], int c[4], uint8_t *log8, uint8_t *alog8, uint8_t *sbox){ | 108 static void init_multbl2(uint8_t tbl[1024], int c[4], uint8_t *log8, uint8_t *alog8, uint8_t *sbox){ |
109 int i, j; | 109 int i, j; |
110 for(i=0; i<1024; i++){ | 110 for(i=0; i<1024; i++){ |
202 {0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad, 0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3}, | 202 {0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad, 0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3}, |
203 {0}}; | 203 {0}}; |
204 uint8_t rct[2][16]= { | 204 uint8_t rct[2][16]= { |
205 {0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7, 0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf}, | 205 {0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7, 0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf}, |
206 {0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0, 0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65}}; | 206 {0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0, 0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65}}; |
207 uint8_t temp[16]; | |
207 | 208 |
208 av_aes_init(&ae, "PI=3.141592654..", 128, 0); | 209 av_aes_init(&ae, "PI=3.141592654..", 128, 0); |
209 av_aes_init(&ad, "PI=3.141592654..", 128, 1); | 210 av_aes_init(&ad, "PI=3.141592654..", 128, 1); |
210 av_log_level= AV_LOG_DEBUG; | 211 av_log_level= AV_LOG_DEBUG; |
211 | 212 |
212 for(i=0; i<2; i++){ | 213 for(i=0; i<2; i++){ |
213 av_aes_init(&b, rkey[i], 128, 1); | 214 av_aes_init(&b, rkey[i], 128, 1); |
214 memcpy(b.state, rct[i], 16); | 215 aes_decrypt(&b, temp, rct[i]); |
215 aes_decrypt(&b); | |
216 for(j=0; j<16; j++) | 216 for(j=0; j<16; j++) |
217 if(rpt[i][j] != b.state[1][0][j]) | 217 if(rpt[i][j] != temp[j]) |
218 av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n", j, rpt[i][j], b.state[1][0][j]); | 218 av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n", j, rpt[i][j], temp[j]); |
219 } | 219 } |
220 | 220 |
221 for(i=0; i<10000; i++){ | 221 for(i=0; i<10000; i++){ |
222 for(j=0; j<16; j++){ | 222 for(j=0; j<16; j++){ |
223 pt[j]= random(); | 223 pt[j]= random(); |
224 } | 224 } |
225 memcpy(ae.state, pt, 16); | |
226 {START_TIMER | 225 {START_TIMER |
227 aes_encrypt(&ae); | 226 aes_encrypt(&ae, temp, pt); |
228 if(!(i&(i-1))) | 227 if(!(i&(i-1))) |
229 av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n", ae.state[1][0][0], ae.state[1][1][1], ae.state[1][2][2], ae.state[1][3][3]); | 228 av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n", temp[0], temp[5], temp[10], temp[15]); |
230 memcpy(ad.state[0], ae.state[1], 16); | 229 aes_decrypt(&ad, temp, temp); |
231 aes_decrypt(&ad); | |
232 STOP_TIMER("aes")} | 230 STOP_TIMER("aes")} |
233 for(j=0; j<16; j++){ | 231 for(j=0; j<16; j++){ |
234 if(pt[j] != ad.state[1][0][j]){ | 232 if(pt[j] != temp[j]){ |
235 av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n", i,j, pt[j], ad.state[1][0][j]); | 233 av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n", i,j, pt[j], temp[j]); |
236 } | 234 } |
237 } | 235 } |
238 } | 236 } |
239 return 0; | 237 return 0; |
240 } | 238 } |