FFmpeg
aes_ctr.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 #include <string.h>
20 
21 #include "libavutil/random_seed.h"
22 #include "libavutil/log.h"
23 #include "libavutil/mem_internal.h"
24 #include "libavutil/aes_ctr.h"
25 
26 static const DECLARE_ALIGNED(8, uint8_t, plain)[] = {
27  0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f,
28  0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f
29 };
30 
31 static const DECLARE_ALIGNED(8, uint8_t, encrypted)[] = {
32  0x95, 0xcd, 0x9a, 0x8a, 0x83, 0xa2, 0x1a, 0x84, 0x92, 0xed,
33  0xd6, 0xf2, 0x57, 0x2f, 0x61, 0x98, 0xbc, 0x20, 0x98, 0xee
34 };
35 
36 static const DECLARE_ALIGNED(8, uint8_t, fixed_iv)[] = {
37  0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef
38 };
39 
40 static const DECLARE_ALIGNED(8, uint8_t, fixed_key)[] = {
41  0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
42  0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
43 };
44 
45 static DECLARE_ALIGNED(8, uint32_t, key)[4];
46 
47 static DECLARE_ALIGNED(8, uint8_t, tmp)[20];
48 
49 int main (void)
50 {
51  int ret = 1;
52  struct AVAESCTR *ae, *ad;
53  const uint8_t *iv, *k;
54 
55  for (int i = 0; i < 2; i++) {
56  ae = av_aes_ctr_alloc();
57  ad = av_aes_ctr_alloc();
58 
59  if (!ae || !ad)
60  goto ERROR;
61 
62  if (i)
63  k = fixed_key;
64  else {
65  // Note: av_random_bytes() should be used in a real world scenario,
66  // but since that function can fail, av_get_random_seed() is used
67  // here for the purpose of this test, as its output is sufficient.
68  key[0] = av_get_random_seed();
69  key[1] = av_get_random_seed();
70  key[2] = av_get_random_seed();
71  key[3] = av_get_random_seed();
72  k = (uint8_t *)key;
73  }
74 
75  if (av_aes_ctr_init(ae, k) < 0)
76  goto ERROR;
77 
78  if (av_aes_ctr_init(ad, k) < 0)
79  goto ERROR;
80 
81  if (i)
83  else
85  iv = av_aes_ctr_get_iv(ae);
86  av_aes_ctr_set_full_iv(ad, iv);
87 
88  av_aes_ctr_crypt(ae, tmp, plain, sizeof(tmp));
89  if (i && memcmp(tmp, encrypted, sizeof(tmp)) != 0) {
90  av_log(NULL, AV_LOG_ERROR, "test failed\n");
91  goto ERROR;
92  }
93 
94  av_aes_ctr_crypt(ad, tmp, tmp, sizeof(tmp));
95  if (memcmp(tmp, plain, sizeof(tmp)) != 0){
96  av_log(NULL, AV_LOG_ERROR, "test failed\n");
97  goto ERROR;
98  }
99 
100  av_aes_ctr_free(ae);
101  av_aes_ctr_free(ad);
102  ae = ad = NULL;
103  }
104 
105  av_log(NULL, AV_LOG_INFO, "test passed\n");
106  ret = 0;
107 
108 ERROR:
109  av_aes_ctr_free(ae);
110  av_aes_ctr_free(ad);
111  return ret;
112 }
mem_internal.h
fixed_iv
static const uint8_t fixed_iv[]
Definition: aes_ctr.c:36
av_aes_ctr_set_random_iv
void av_aes_ctr_set_random_iv(struct AVAESCTR *a)
Generate a random iv.
Definition: aes_ctr.c:61
fixed_key
static const uint8_t fixed_key[]
Definition: aes_ctr.c:40
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:196
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
encrypted
static const uint8_t encrypted[]
Definition: aes_ctr.c:31
av_aes_ctr_get_iv
const uint8_t * av_aes_ctr_get_iv(struct AVAESCTR *a)
Get the current iv.
Definition: aes_ctr.c:56
NULL
#define NULL
Definition: coverity.c:32
tmp
static uint8_t tmp[20]
Definition: aes_ctr.c:47
ERROR
static void ERROR(const char *str)
Definition: audio_fifo.c:58
av_aes_ctr_alloc
struct AVAESCTR * av_aes_ctr_alloc(void)
Allocate an AVAESCTR context.
Definition: aes_ctr.c:40
main
int main(void)
Definition: aes_ctr.c:49
av_aes_ctr_init
int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
Initialize an AVAESCTR context.
Definition: aes_ctr.c:71
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
av_aes_ctr_set_iv
void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t *iv)
Forcefully change the 8-byte iv.
Definition: aes_ctr.c:45
av_aes_ctr_set_full_iv
void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t *iv)
Forcefully change the "full" 16-byte iv, including the counter.
Definition: aes_ctr.c:51
aes_ctr.h
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
av_aes_ctr_free
void av_aes_ctr_free(struct AVAESCTR *a)
Release an AVAESCTR context.
Definition: aes_ctr.c:80
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
plain
static const uint8_t plain[]
Definition: aes_ctr.c:26
ret
ret
Definition: filter_design.txt:187
random_seed.h
av_aes_ctr_crypt
void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
Process a buffer using a previously initialized context.
Definition: aes_ctr.c:97
key
static uint32_t key[4]
Definition: aes_ctr.c:45
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVAESCTR
Definition: aes_ctr.c:34