]> git.sesse.net Git - ffmpeg/blob - libavutil/tests/aes.c
Merge commit '7d8d726be7dc46343ab1c98c339c1ed44bcb07c1'
[ffmpeg] / libavutil / tests / aes.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 // LCOV_EXCL_START
20 #include <string.h>
21
22 #include "libavutil/aes.h"
23 #include "libavutil/lfg.h"
24 #include "libavutil/log.h"
25 #include "libavutil/mem.h"
26
27 int main(int argc, char **argv)
28 {
29     int i, j;
30     struct AVAES *b;
31     static const uint8_t rkey[2][16] = {
32         { 0 },
33         { 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3,
34           0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59 }
35     };
36     static const uint8_t rpt[2][16] = {
37         { 0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad,
38           0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3 },
39         { 0 }
40     };
41     static const uint8_t rct[2][16] = {
42         { 0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7,
43           0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf },
44         { 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0,
45           0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65 }
46     };
47     uint8_t pt[32];
48     uint8_t temp[32];
49     uint8_t iv[2][16];
50     int err = 0;
51
52     b = av_aes_alloc();
53     if (!b)
54         return 1;
55
56     av_log_set_level(AV_LOG_DEBUG);
57
58     for (i = 0; i < 2; i++) {
59         av_aes_init(b, rkey[i], 128, 1);
60         av_aes_crypt(b, temp, rct[i], 1, NULL, 1);
61         for (j = 0; j < 16; j++) {
62             if (rpt[i][j] != temp[j]) {
63                 av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n",
64                        j, rpt[i][j], temp[j]);
65                 err = 1;
66             }
67         }
68     }
69     av_free(b);
70
71     if (argc > 1 && !strcmp(argv[1], "-t")) {
72         struct AVAES *ae, *ad;
73         AVLFG prng;
74
75         ae = av_aes_alloc();
76         ad = av_aes_alloc();
77
78         if (!ae || !ad) {
79             av_free(ae);
80             av_free(ad);
81             return 1;
82         }
83
84         av_aes_init(ae, (const uint8_t*)"PI=3.141592654..", 128, 0);
85         av_aes_init(ad, (const uint8_t*)"PI=3.141592654..", 128, 1);
86         av_lfg_init(&prng, 1);
87
88         for (i = 0; i < 10000; i++) {
89             for (j = 0; j < 32; j++)
90                 pt[j] = av_lfg_get(&prng);
91             for (j = 0; j < 16; j++)
92                 iv[0][j] = iv[1][j] = av_lfg_get(&prng);
93             {
94                 START_TIMER;
95                 av_aes_crypt(ae, temp, pt, 2, iv[0], 0);
96                 if (!(i & (i - 1)))
97                     av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n",
98                            temp[0], temp[5], temp[10], temp[15]);
99                 av_aes_crypt(ad, temp, temp, 2, iv[1], 1);
100                 av_aes_crypt(ae, temp, pt, 2, NULL, 0);
101                 if (!(i & (i - 1)))
102                     av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n",
103                            temp[0], temp[5], temp[10], temp[15]);
104                 av_aes_crypt(ad, temp, temp, 2, NULL, 1);
105                 STOP_TIMER("aes");
106             }
107             for (j = 0; j < 16; j++) {
108                 if (pt[j] != temp[j]) {
109                     av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n",
110                            i, j, pt[j], temp[j]);
111                 }
112             }
113         }
114         av_free(ae);
115         av_free(ad);
116     }
117     return err;
118 }
119 // LCOV_EXCL_STOP