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/log.h"
22 #include "libavutil/mem_internal.h"
23 #include "libavutil/aes_ctr.h"
24 
25 static const DECLARE_ALIGNED(8, uint8_t, plain)[] = {
26  0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d
27 };
28 static DECLARE_ALIGNED(8, uint8_t, tmp)[11];
29 
30 int main (void)
31 {
32  int ret = 1;
33  struct AVAESCTR *ae, *ad;
34  const uint8_t *iv;
35 
36  ae = av_aes_ctr_alloc();
37  ad = av_aes_ctr_alloc();
38 
39  if (!ae || !ad)
40  goto ERROR;
41 
42  if (av_aes_ctr_init(ae, (const uint8_t*)"0123456789abcdef") < 0)
43  goto ERROR;
44 
45  if (av_aes_ctr_init(ad, (const uint8_t*)"0123456789abcdef") < 0)
46  goto ERROR;
47 
49  iv = av_aes_ctr_get_iv(ae);
50  av_aes_ctr_set_full_iv(ad, iv);
51 
52  av_aes_ctr_crypt(ae, tmp, plain, sizeof(tmp));
53  av_aes_ctr_crypt(ad, tmp, tmp, sizeof(tmp));
54 
55  if (memcmp(tmp, plain, sizeof(tmp)) != 0){
56  av_log(NULL, AV_LOG_ERROR, "test failed\n");
57  goto ERROR;
58  }
59 
60  av_log(NULL, AV_LOG_INFO, "test passed\n");
61  ret = 0;
62 
63 ERROR:
64  av_aes_ctr_free(ae);
65  av_aes_ctr_free(ad);
66  return ret;
67 }
mem_internal.h
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
av_aes_ctr_set_random_iv
void av_aes_ctr_set_random_iv(struct AVAESCTR *a)
Generate a random iv.
Definition: aes_ctr.c:63
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_aes_ctr_get_iv
const uint8_t * av_aes_ctr_get_iv(struct AVAESCTR *a)
Get the current iv.
Definition: aes_ctr.c:58
NULL
#define NULL
Definition: coverity.c:32
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:30
av_aes_ctr_init
int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
Initialize an AVAESCTR context.
Definition: aes_ctr.c:73
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem_internal.h:104
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:52
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:83
log.h
plain
static const uint8_t plain[]
Definition: aes_ctr.c:25
ret
ret
Definition: filter_design.txt:187
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:107
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVAESCTR
Definition: aes_ctr.c:33