Revision 6572e1a4 libavutil/aes.c

View differences:

libavutil/aes.c
30 30
    int rounds;
31 31
}AVAES;
32 32

  
33
const int av_aes_size= sizeof(AVAES);
34

  
33 35
static const uint8_t rcon[10] = {
34 36
  0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
35 37
};
......
112 114
}
113 115

  
114 116
// this is based on the reference AES code by Paulo Barreto and Vincent Rijmen
115
AVAES *av_aes_init(uint8_t *key, int key_bits, int decrypt) {
116
    AVAES *a;
117
int av_aes_init(AVAES *a, uint8_t *key, int key_bits, int decrypt) {
117 118
    int i, j, t, rconpointer = 0;
118 119
    uint8_t tk[8][4];
119 120
    int KC= key_bits>>5;
......
142 143
    }
143 144

  
144 145
    if(key_bits!=128 && key_bits!=192 && key_bits!=256)
145
        return NULL;
146
        return -1;
146 147

  
147
    a= av_malloc(sizeof(AVAES));
148 148
    a->rounds= rounds;
149 149

  
150 150
    memcpy(tk, key, KC*4);
......
178 178
        }
179 179
    }
180 180

  
181
    return a;
181
    return 0;
182 182
}
183 183

  
184 184
#ifdef TEST
185 185

  
186 186
int main(){
187 187
    int i,j;
188
    AVAES *ae= av_aes_init("PI=3.141592654..", 128, 0);
189
    AVAES *ad= av_aes_init("PI=3.141592654..", 128, 1);
188
    AVAES ae, ad, b;
190 189
    uint8_t rkey[2][16]= {
191 190
        {0},
192 191
        {0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3, 0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59}};
......
197 196
        {0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7, 0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf},
198 197
        {0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0, 0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65}};
199 198

  
199
    av_aes_init(&ae, "PI=3.141592654..", 128, 0);
200
    av_aes_init(&ad, "PI=3.141592654..", 128, 1);
200 201
    av_log_level= AV_LOG_DEBUG;
201 202

  
202 203
    for(i=0; i<2; i++){
203
        AVAES *b= av_aes_init(rkey[i], 128, 1);
204
        memcpy(b->state, rct[i], 16);
205
        av_aes_decrypt(b);
204
        av_aes_init(&b, rkey[i], 128, 1);
205
        memcpy(b.state, rct[i], 16);
206
        av_aes_decrypt(&b);
206 207
        for(j=0; j<16; j++)
207
            if(rpt[i][j] != b->state[0][j])
208
                av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n", j, rpt[i][j], b->state[0][j]);
208
            if(rpt[i][j] != b.state[0][j])
209
                av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n", j, rpt[i][j], b.state[0][j]);
209 210
    }
210 211

  
211 212
    for(i=0; i<10000; i++){
212 213
        for(j=0; j<16; j++){
213 214
            pt[j]= random();
214 215
        }
215
        memcpy(ae->state, pt, 16);
216
        memcpy(ae.state, pt, 16);
216 217
{START_TIMER
217
        av_aes_encrypt(ae);
218
        av_aes_encrypt(&ae);
218 219
        if(!(i&(i-1)))
219
            av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n", ae->state[0][0], ae->state[1][1], ae->state[2][2], ae->state[3][3]);
220
        memcpy(ad->state, ae->state, 16);
221
        av_aes_decrypt(ad);
220
            av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n", ae.state[0][0], ae.state[1][1], ae.state[2][2], ae.state[3][3]);
221
        memcpy(ad.state, ae.state, 16);
222
        av_aes_decrypt(&ad);
222 223
STOP_TIMER("aes")}
223 224
        for(j=0; j<16; j++){
224
            if(pt[j] != ad->state[0][j]){
225
                av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n", i,j, pt[j], ad->state[0][j]);
225
            if(pt[j] != ad.state[0][j]){
226
                av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n", i,j, pt[j], ad.state[0][j]);
226 227
            }
227 228
        }
228 229
    }

Also available in: Unified diff