FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavutil/log.h"
20 #include "libavutil/mem.h"
21 #include "libavutil/aes_ctr.h"
22 
23 static const DECLARE_ALIGNED(8, uint8_t, plain)[] = {
24  0x6d, 0x6f, 0x73, 0x74, 0x20, 0x72, 0x61, 0x6e, 0x64, 0x6f, 0x6d
25 };
26 static DECLARE_ALIGNED(8, uint8_t, tmp)[11];
27 
28 int main (void)
29 {
30  int ret = 1;
31  struct AVAESCTR *ae, *ad;
32  const uint8_t *iv;
33 
34  ae = av_aes_ctr_alloc();
35  ad = av_aes_ctr_alloc();
36 
37  if (!ae || !ad)
38  goto ERROR;
39 
40  if (av_aes_ctr_init(ae, (const uint8_t*)"0123456789abcdef") < 0)
41  goto ERROR;
42 
43  if (av_aes_ctr_init(ad, (const uint8_t*)"0123456789abcdef") < 0)
44  goto ERROR;
45 
47  iv = av_aes_ctr_get_iv(ae);
48  av_aes_ctr_set_iv(ad, iv);
49 
50  av_aes_ctr_crypt(ae, tmp, plain, sizeof(tmp));
51  av_aes_ctr_crypt(ad, tmp, tmp, sizeof(tmp));
52 
53  if (memcmp(tmp, plain, sizeof(tmp)) != 0){
54  av_log(NULL, AV_LOG_ERROR, "test failed\n");
55  goto ERROR;
56  }
57 
58  av_log(NULL, AV_LOG_INFO, "test passed\n");
59  ret = 0;
60 
61 ERROR:
62  av_aes_ctr_free(ae);
63  av_aes_ctr_free(ad);
64  return ret;
65 }
#define NULL
Definition: coverity.c:32
int main(void)
Definition: aes_ctr.c:28
Memory handling functions.
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:105
uint8_t
#define ERROR(...)
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
Definition: mem.h:104
void av_aes_ctr_free(struct AVAESCTR *a)
Release an AVAESCTR context.
Definition: aes_ctr.c:78
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static const uint8_t plain[]
Definition: aes_ctr.c:23
struct AVAESCTR * av_aes_ctr_alloc(void)
Allocate an AVAESCTR context.
Definition: aes_ctr.c:36
void av_aes_ctr_set_random_iv(struct AVAESCTR *a)
Generate a random iv.
Definition: aes_ctr.c:53
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t *iv)
Forcefully change the iv.
Definition: aes_ctr.c:41
int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
Initialize an AVAESCTR context.
Definition: aes_ctr.c:63
const uint8_t * av_aes_ctr_get_iv(struct AVAESCTR *a)
Get the current iv.
Definition: aes_ctr.c:48
static uint8_t tmp[11]
Definition: aes_ctr.c:26