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