• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavutil/aes.c

Go to the documentation of this file.
00001 /*
00002  * copyright (c) 2007 Michael Niedermayer <michaelni@gmx.at>
00003  *
00004  * some optimization ideas from aes128.c by Reimar Doeffinger
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "common.h"
00024 #include "aes.h"
00025 
00026 typedef struct AVAES{
00027     // Note: round_key[16] is accessed in the init code, but this only
00028     // overwrites state, which does not matter (see also r7471).
00029     uint8_t round_key[15][4][4];
00030     uint8_t state[2][4][4];
00031     int rounds;
00032 }AVAES;
00033 
00034 const int av_aes_size= sizeof(AVAES);
00035 
00036 static const uint8_t rcon[10] = {
00037   0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
00038 };
00039 
00040 static uint8_t     sbox[256];
00041 static uint8_t inv_sbox[256];
00042 #if CONFIG_SMALL
00043 static uint32_t enc_multbl[1][256];
00044 static uint32_t dec_multbl[1][256];
00045 #else
00046 static uint32_t enc_multbl[4][256];
00047 static uint32_t dec_multbl[4][256];
00048 #endif
00049 
00050 static inline void addkey(uint64_t dst[2], const uint64_t src[2], const uint64_t round_key[2]){
00051     dst[0] = src[0] ^ round_key[0];
00052     dst[1] = src[1] ^ round_key[1];
00053 }
00054 
00055 static void subshift(uint8_t s0[2][16], int s, const uint8_t *box){
00056     uint8_t (*s1)[16]= s0[0] - s;
00057     uint8_t (*s3)[16]= s0[0] + s;
00058     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]];
00059     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]];
00060     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]];
00061     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]];
00062 }
00063 
00064 static inline int mix_core(uint32_t multbl[4][256], int a, int b, int c, int d){
00065 #if CONFIG_SMALL