FFmpeg
aes.c
Go to the documentation of this file.
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 
21 #include "libavutil/timer.h"
22 
23 #include <string.h>
24 
25 #include "libavutil/aes.h"
26 #include "libavutil/lfg.h"
27 #include "libavutil/log.h"
28 #include "libavutil/mem.h"
29 
30 int main(int argc, char **argv)
31 {
32  int i, j;
33  struct AVAES *b;
34  static const uint8_t rkey[2][16] = {
35  { 0 },
36  { 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3,
37  0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59 }
38  };
39  static const uint8_t rpt[2][16] = {
40  { 0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad,
41  0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3 },
42  { 0 }
43  };
44  static const uint8_t rct[2][16] = {
45  { 0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7,
46  0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf },
47  { 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0,
48  0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65 }
49  };
50  uint8_t pt[32];
51  uint8_t temp[32];
52  uint8_t iv[2][16];
53  int err = 0;
54 
55  b = av_aes_alloc();
56  if (!b)
57  return 1;
58 
60 
61  for (i = 0; i < 2; i++) {
62  av_aes_init(b, rkey[i], 128, 1);
63  av_aes_crypt(b, temp, rct[i], 1, NULL, 1);
64  for (j = 0; j < 16; j++) {
65  if (rpt[i][j] != temp[j]) {
66  av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n",
67  j, rpt[i][j], temp[j]);
68  err = 1;
69  }
70  }
71  }
72  av_free(b);
73 
74  if (argc > 1 && !strcmp(argv[1], "-t")) {
75  struct AVAES *ae, *ad;
76  AVLFG prng;
77 
78  ae = av_aes_alloc();
79  ad = av_aes_alloc();
80 
81  if (!ae || !ad) {
82  av_free(ae);
83  av_free(ad);
84  return 1;
85  }
86 
87  av_aes_init(ae, (const uint8_t*)"PI=3.141592654..", 128, 0);
88  av_aes_init(ad, (const uint8_t*)"PI=3.141592654..", 128, 1);
89  av_lfg_init(&prng, 1);
90 
91  for (i = 0; i < 10000; i++) {
92  for (j = 0; j < 32; j++)
93  pt[j] = av_lfg_get(&prng);
94  for (j = 0; j < 16; j++)
95  iv[0][j] = iv[1][j] = av_lfg_get(&prng);
96  {
98  av_aes_crypt(ae, temp, pt, 2, iv[0], 0);
99  if (!(i & (i - 1)))
100  av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n",
101  temp[0], temp[5], temp[10], temp[15]);
102  av_aes_crypt(ad, temp, temp, 2, iv[1], 1);
103  av_aes_crypt(ae, temp, pt, 2, NULL, 0);
104  if (!(i & (i - 1)))
105  av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n",
106  temp[0], temp[5], temp[10], temp[15]);
107  av_aes_crypt(ad, temp, temp, 2, NULL, 1);
108  STOP_TIMER("aes");
109  }
110  for (j = 0; j < 16; j++) {
111  if (pt[j] != temp[j]) {
112  av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n",
113  i, j, pt[j], temp[j]);
114  }
115  }
116  }
117  av_free(ae);
118  av_free(ad);
119  }
120  return err;
121 }
122 // LCOV_EXCL_STOP
av_aes_init
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition: aes.c:201
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
b
#define b
Definition: input.c:41
main
int main(int argc, char **argv)
Definition: aes.c:30
START_TIMER
#define START_TIMER
Definition: timer.h:152
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_lfg_get
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:53
lfg.h
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
aes.h
NULL
#define NULL
Definition: coverity.c:32
av_aes_crypt
void av_aes_crypt(AVAES *a, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypt or decrypt a buffer using a previously initialized context.
Definition: aes.c:169
av_aes_alloc
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition: aes.c:35
timer.h
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
pt
int pt
Definition: rtp.c:35
av_log_set_level
void av_log_set_level(int level)
Set the log level.
Definition: log.c:442
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVAES
Definition: aes_internal.h:34
temp
else temp
Definition: vf_mcdeint.c:248
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
STOP_TIMER
#define STOP_TIMER(id)
Definition: timer.h:153