Libav
aes.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/aes.c"
20 
21 #include <string.h>
22 
23 #include "libavutil/lfg.h"
24 #include "libavutil/log.h"
25 
26 int main(int argc, char **argv)
27 {
28  int i, j;
29  AVAES b;
30  static const uint8_t rkey[2][16] = {
31  { 0 },
32  { 0x10, 0xa5, 0x88, 0x69, 0xd7, 0x4b, 0xe5, 0xa3,
33  0x74, 0xcf, 0x86, 0x7c, 0xfb, 0x47, 0x38, 0x59 }
34  };
35  static const uint8_t rpt[2][16] = {
36  { 0x6a, 0x84, 0x86, 0x7c, 0xd7, 0x7e, 0x12, 0xad,
37  0x07, 0xea, 0x1b, 0xe8, 0x95, 0xc5, 0x3f, 0xa3 },
38  { 0 }
39  };
40  static const uint8_t rct[2][16] = {
41  { 0x73, 0x22, 0x81, 0xc0, 0xa0, 0xaa, 0xb8, 0xf7,
42  0xa5, 0x4a, 0x0c, 0x67, 0xa0, 0xc4, 0x5e, 0xcf },
43  { 0x6d, 0x25, 0x1e, 0x69, 0x44, 0xb0, 0x51, 0xe0,
44  0x4e, 0xaa, 0x6f, 0xb4, 0xdb, 0xf7, 0x84, 0x65 }
45  };
46  uint8_t pt[16], temp[16];
47  int err = 0;
48 
50 
51  for (i = 0; i < 2; i++) {
52  av_aes_init(&b, rkey[i], 128, 1);
53  av_aes_crypt(&b, temp, rct[i], 1, NULL, 1);
54  for (j = 0; j < 16; j++) {
55  if (rpt[i][j] != temp[j]) {
56  av_log(NULL, AV_LOG_ERROR, "%d %02X %02X\n",
57  j, rpt[i][j], temp[j]);
58  err = 1;
59  }
60  }
61  }
62 
63  if (argc > 1 && !strcmp(argv[1], "-t")) {
64  AVAES ae, ad;
65  AVLFG prng;
66 
67  av_aes_init(&ae, "PI=3.141592654..", 128, 0);
68  av_aes_init(&ad, "PI=3.141592654..", 128, 1);
69  av_lfg_init(&prng, 1);
70 
71  for (i = 0; i < 10000; i++) {
72  for (j = 0; j < 16; j++)
73  pt[j] = av_lfg_get(&prng);
74  {
76  av_aes_crypt(&ae, temp, pt, 1, NULL, 0);
77  if (!(i & (i - 1)))
78  av_log(NULL, AV_LOG_ERROR, "%02X %02X %02X %02X\n",
79  temp[0], temp[5], temp[10], temp[15]);
80  av_aes_crypt(&ad, temp, temp, 1, NULL, 1);
81  STOP_TIMER("aes");
82  }
83  for (j = 0; j < 16; j++) {
84  if (pt[j] != temp[j]) {
85  av_log(NULL, AV_LOG_ERROR, "%d %d %02X %02X\n",
86  i, j, pt[j], temp[j]);
87  }
88  }
89  }
90  }
91  return err;
92 }
Definition: lfg.h:25
void av_log_set_level(int level)
Set the log level.
Definition: log.c:205
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
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:143
uint8_t
#define b
Definition: input.c:52
int pt
Definition: rtp.c:34
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
int main(int argc, char **argv)
Definition: aes.c:26
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition: aes.c:194
NULL
Definition: eval.c:55
#define START_TIMER
Definition: timer.h:86
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
#define STOP_TIMER(id)
Definition: timer.h:87
Definition: aes.c:35