00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "common.h"
00024 #include "aes.h"
00025 #include "intreadwrite.h"
00026
00027 typedef union {
00028 uint64_t u64[2];
00029 uint32_t u32[4];
00030 uint8_t u8x4[4][4];
00031 uint8_t u8[16];
00032 } av_aes_block;
00033
00034 typedef struct AVAES {
00035
00036
00037 av_aes_block round_key[15];
00038 av_aes_block state[2];
00039 int rounds;
00040 } AVAES;
00041
00042 const int av_aes_size= sizeof(AVAES);
00043
00044 static const uint8_t rcon[10] = {
00045 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
00046 };
00047
00048 static uint8_t sbox[256];
00049 static uint8_t inv_sbox[256];
00050 #if CONFIG_SMALL
00051 static uint32_t enc_multbl[1][256];
00052 static uint32_t dec_multbl[1][256];
00053 #else
00054 static uint32_t enc_multbl[4][256];
00055 static uint32_t dec_multbl[4][256];
00056 #endif
00057
00058 #if HAVE_BIGENDIAN
00059 # define ROT(x, s) ((x >> s) | (x << (32-s)))
00060 #else
00061 # define ROT(x, s) ((x << s) | (x >> (32-s)))
00062 #endif
00063
00064 static inline void addkey(av_aes_block *dst, const av_aes_block *src,
00065 const av_aes_block *round_key)
00066 {
00067 dst->u64[0] = src->u64[0] ^ round_key->u64[0];
00068 dst->u64[1] = src->u64[1] ^ round_key->u64[1];
00069 }
00070
00071 static inline void addkey_s(av_aes_block *dst, const uint8_t *src,
00072 const av_aes_block *round_key)
00073 {
00074 dst->u64[0] = AV_RN64(src) ^ round_key->u64[0];
00075 dst->u64[1] = AV_RN64(src + 8) ^ round_key->u64[1];
00076 }
00077
00078 static inline void addkey_d(uint8_t *dst, const av_aes_block *src,
00079 const av_aes_block *round_key)
00080 {
00081 AV_WN64(dst, src->u64[0] ^ round_key->u64[0]);
00082 AV_WN64(dst + 8, src->u64[1] ^ round_key->u64[1]);
00083 }
00084
00085 static void subshift(av_aes_block s0[2], int s, const uint8_t *box)
00086 {
00087 av_aes_block *s1 = (av_aes_block *) (s0[0].u8 - s);
00088 av_aes_block *s3 = (av_aes_block *) (s0[0].u8 + s);
00089
00090 s0[0].u8[ 0] = box[s0[1].u8[ 0]];
00091 s0[0].u8[ 4] = box[s0[1].u8[ 4]];
00092 s0[0].u8[ 8] = box[s0[1].u8[ 8]];
00093 s0[0].u8[12] = box[s0[1].u8[12]];
00094 s1[0].u8[ 3] = box[s1[1].u8[ 7]];
00095 s1[0].u8[ 7] = box[s1[1].u8[11]];
00096 s1[0].u8[11] = box[s1[1].u8[15]];
00097 s1[0].u8[15] = box[s1[1].u8[ 3]];
00098 s0[0].u8[ 2] = box[s0[1].u8[10]];
00099 s0[0].u8[10] = box[s0[1].u8[ 2]];
00100 s0[0].u8[ 6] = box[s0[1].u8[14]];
00101 s0[0].u8[14] = box[s0[1].u8[ 6]];
00102 s3[0].u8[ 1] = box[s3[1].u8[13]];
00103 s3[0].u8[13] = box[s3[1].u8[ 9]];
00104 s3[0].u8[ 9] = box[s3[1].u8[ 5]];
00105 s3[0].u8[ 5] = box[s3[1].u8[ 1]];
00106 }
00107
00108 static inline int mix_core(uint32_t multbl[][256], int a, int b, int c, int d){
00109 #if CONFIG_SMALL
00110 return multbl[0][a] ^ ROT(multbl[0][b], 8) ^ ROT(multbl[0][c], 16) ^ ROT(multbl[0][d], 24);
00111 #else
00112 return multbl[0][a] ^ multbl[1][b] ^ multbl[2][c] ^ multbl[3][d];
00113 #endif
00114 }
00115
00116 static inline void mix(av_aes_block state[2], uint32_t multbl[][256], int s1, int s3){
00117 uint8_t (*src)[4] = state[1].u8x4;
00118 state[0].u32[0] = mix_core(multbl, src[0][0], src[s1 ][1], src[2][2], src[s3 ][3]);
00119 state[0].u32[1] = mix_core(multbl, src[1][0], src[s3-1][1], src[3][2], src[s1-1][3]);
00120 state[0].u32[2] = mix_core(multbl, src[2][0], src[s3 ][1], src[0][2], src[s1 ][3]);
00121 state[0].u32[3] = mix_core(multbl, src[3][0], src[s1-1][1], src[1][2], src[s3-1][3]);
00122 }
00123
00124 static inline void crypt(AVAES *a, int s, const uint8_t *sbox,
00125 uint32_t multbl[][256])
00126 {
00127 int r;
00128
00129 for (r = a->rounds - 1; r > 0; r--) {
00130 mix(a->state, multbl, 3 - s, 1 + s);
00131 addkey(&a->state[1], &a->state[0], &a->round_key[r]);
00132 }
00133
00134 subshift(&a->state[0], s, sbox);
00135 }
00136
00137 void av_aes_crypt(AVAES *a, uint8_t *dst, const uint8_t *src,
00138 int count, uint8_t *iv, int decrypt)
00139 {
00140 while (count--) {
00141 addkey_s(&a->state[1], src, &a->round_key[a->rounds]);
00142 if (decrypt) {
00143 crypt(a, 0, inv_sbox, dec_multbl);
00144 if (iv) {
00145 addkey_s(&a->state[0], iv, &a->state[0]);
00146 memcpy(iv, src, 16);
00147 }
00148 addkey_d(dst, &a->state[0], &a->round_key[0]);
00149 } else {
00150 if (iv)
00151 addkey_s(&a->state[1], iv, &a->state[1]);
00152 crypt(a, 2, sbox, enc_multbl);
00153 addkey_d(dst, &a->state[0], &a->round_key[0]);
00154 if (iv)
00155 memcpy(iv, dst, 16);
00156 }
00157 src += 16;
00158 dst += 16;
00159 }
00160 }
00161
00162 static void init_multbl2(uint32_t tbl[][256], const int c[4],
00163 const uint8_t *log8, const uint8_t *alog8,
00164 const uint8_t *sbox)
00165 {
00166 int i;
00167
00168 for (i = 0; i < 256; i++) {
00169 int x = sbox[i];
00170 if (x) {
00171 int k, l, m, n;
00172 x = log8[x];
00173 k = alog8[x + log8[c[0]]];
00174 l = alog8[x + log8[c[1]]];
00175 m = alog8[x + log8[c[2]]];
00176 n = alog8[x + log8[c[3]]];
00177 tbl[0][i] = AV_NE(MKBETAG(k,l,m,n), MKTAG(k,l,m,n));
00178 #if !CONFIG_SMALL
00179 tbl[1][i] = ROT(tbl[0][i], 8);
00180 tbl[2][i] = ROT(tbl[0][i], 16);
00181 tbl[3][i] = ROT(tbl[0][i], 24);
00182 #endif
00183 }
00184 }
00185 }
00186
00187
00188 int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
00189 {
00190 int i, j, t, rconpointer = 0;
00191 uint8_t tk[8][4];
00192 int KC = key_bits >> 5;
00193 int rounds = KC + 6;
00194 uint8_t log8[256];
00195 uint8_t alog8[512];
00196
00197 if (!enc_multbl[FF_ARRAY_ELEMS(enc_multbl)-1][FF_ARRAY_ELEMS(enc_multbl[0])-1]) {
00198 j = 1;
00199 for (i = 0; i < 255; i++) {
00200 alog8[i] = alog8[i + 255] = j;
00201 log8[j] = i;
00202 j ^= j + j;
00203 if (j > 255)
00204 j ^= 0x11B;
00205 }
00206 for (i = 0; i < 256; i++) {
00207 j = i ? alog8[255 - log8[i]] : 0;
00208 j ^= (j << 1) ^ (j << 2) ^ (j << 3) ^ (j << 4);
00209 j = (j ^ (j >> 8) ^ 99) & 255;
00210 inv_sbox[j] = i;
00211 sbox[i] = j;
00212 }
00213 init_multbl2(dec_multbl, (const int[4]) { 0xe, 0x9, 0xd, 0xb },
00214 log8, alog8, inv_sbox);
00215 init_multbl2(enc_multbl, (const int[4]) { 0x2, 0x1, 0x1, 0x3 },
00216 log8, alog8, sbox);
00217 }
00218
00219 if (key_bits != 128 && key_bits != 192 && key_bits != 256)
00220 return -1;
00221
00222 a->rounds = rounds;
00223
00224 memcpy(tk, key, KC * 4);
00225 memcpy(a->round_key[0].u8, key, KC * 4);
00226
00227 for (t = KC * 4; t < (rounds + 1) * 16; t += KC * 4) {
00228 for (i = 0; i < 4; i++)
00229 tk[0][i] ^= sbox[tk[KC - 1][(i + 1) & 3]];
00230 tk[0][0] ^= rcon[rconpointer++];
00231
00232 for (j = 1; j < KC; j++) {
00233 if (KC != 8 || j != KC >> 1)
00234 for (i = 0; i < 4; i++)
00235 tk[j][i] ^= tk[j - 1][i];
00236 else
00237 for (i = 0; i < 4; i++)
00238 tk[j][i] ^= sbox[tk[j - 1][i]];
00239 }
00240
00241 memcpy(a->round_key[0].u8 + t, tk, KC * 4);
00242 }
00243
00244 if (decrypt) {
00245 for (i = 1; i < rounds; i++) {
00246 av_aes_block tmp[3];
00247 tmp[2] = a->round_key[i];
00248 subshift(&tmp[1], 0, sbox);
00249 mix(tmp, dec_multbl, 1, 3);
00250 a->round_key[i] = tmp[0];
00251 }
00252 } else {
00253 for (i = 0; i < (rounds + 1) >> 1; i++) {
00254 FFSWAP(av_aes_block, a->round_key[i], a->round_key[rounds-i]);
00255 }
00256 }
00257
00258 return 0;
00259 }
00260
00261 #ifdef TEST
00262
00263 #include <string.h>
00264 #include "lfg.h"
00265 #include "log.h"
00266
00267 int main(int argc, char **argv)
00268 {
00269 int i, j;
00270 AVAES b;
00271 uint8_t rkey[2][16] = {
00272 { 0 },
00273 { 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3,
00274 0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59 }
00275 };
00276 uint8_t pt[16], rpt[2][16]= {
00277 { 0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad,
00278 0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3 },
00279 { 0 }
00280 };
00281 uint8_t rct[2][16]= {
00282 { 0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7,
00283 0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf },
00284 { 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0,
00285 0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65 }
00286 };
00287 uint8_t temp[16];
00288 int err = 0;
00289
00290 av_log_set_level(AV_LOG_DEBUG);
00291
00292 for (i = 0; i < 2; i++) {
00293 av_aes_init(&b, rkey[i], 128, 1);
00294 av_aes_crypt(&b, temp, rct[i], 1, NULL, 1);
00295 for (j = 0; j < 16; j++) {
00296 if (rpt[i][j] != temp[j]) {
00297 av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n",
00298 j, rpt[i][j], temp[j]);
00299 err = 1;
00300 }
00301 }
00302 }
00303
00304 if (argc > 1 && !strcmp(argv[1], "-t")) {
00305 AVAES ae, ad;
00306 AVLFG prng;
00307
00308 av_aes_init(&ae, "PI=3.141592654..", 128, 0);
00309 av_aes_init(&ad, "PI=3.141592654..", 128, 1);
00310 av_lfg_init(&prng, 1);
00311
00312 for (i = 0; i < 10000; i++) {
00313 for (j = 0; j < 16; j++) {
00314 pt[j] = av_lfg_get(&prng);
00315 }
00316 {
00317 START_TIMER;
00318 av_aes_crypt(&ae, temp, pt, 1, NULL, 0);
00319 if (!(i & (i - 1)))
00320 av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n",
00321 temp[0], temp[5], temp[10], temp[15]);
00322 av_aes_crypt(&ad, temp, temp, 1, NULL, 1);
00323 STOP_TIMER("aes");
00324 }
00325 for (j = 0; j < 16; j++) {
00326 if (pt[j] != temp[j]) {
00327 av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n",
00328 i, j, pt[j], temp[j]);
00329 }
00330 }
00331 }
00332 }
00333 return err;
00334 }
00335
00336 #endif