FFmpeg
aes_ctr.c
Go to the documentation of this file.
1 /*
2  * AES-CTR cipher
3  * Copyright (c) 2015 Eran Kornblau <erankor at gmail dot com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "common.h"
23 #include "aes_ctr.h"
24 #include "aes.h"
25 #include "random_seed.h"
26 
27 #define AES_BLOCK_SIZE (16)
28 
29 typedef struct AVAESCTR {
30  struct AVAES* aes;
34 } AVAESCTR;
35 
37 {
38  return av_mallocz(sizeof(struct AVAESCTR));
39 }
40 
41 void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t* iv)
42 {
43  memcpy(a->counter, iv, AES_CTR_IV_SIZE);
44  memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
45  a->block_offset = 0;
46 }
47 
48 void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t* iv)
49 {
50  memcpy(a->counter, iv, sizeof(a->counter));
51  a->block_offset = 0;
52 }
53 
55 {
56  return a->counter;
57 }
58 
60 {
61  uint32_t iv[2];
62 
63  iv[0] = av_get_random_seed();
64  iv[1] = av_get_random_seed();
65 
67 }
68 
69 int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
70 {
71  a->aes = av_aes_alloc();
72  if (!a->aes) {
73  return AVERROR(ENOMEM);
74  }
75 
76  av_aes_init(a->aes, key, 128, 0);
77 
78  memset(a->counter, 0, sizeof(a->counter));
79  a->block_offset = 0;
80 
81  return 0;
82 }
83 
84 void av_aes_ctr_free(struct AVAESCTR *a)
85 {
86  if (a) {
87  av_freep(&a->aes);
88  av_free(a);
89  }
90 }
91 
93 {
94  uint8_t* cur_pos;
95 
96  for (cur_pos = counter + 7; cur_pos >= counter; cur_pos--) {
97  (*cur_pos)++;
98  if (*cur_pos != 0) {
99  break;
100  }
101  }
102 }
103 
105 {
106  av_aes_ctr_increment_be64(a->counter);
107  memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
108  a->block_offset = 0;
109 }
110 
111 void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
112 {
113  const uint8_t* src_end = src + count;
114  const uint8_t* cur_end_pos;
115  uint8_t* encrypted_counter_pos;
116 
117  while (src < src_end) {
118  if (a->block_offset == 0) {
119  av_aes_crypt(a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
120 
121  av_aes_ctr_increment_be64(a->counter + 8);
122  }
123 
124  encrypted_counter_pos = a->encrypted_counter + a->block_offset;
125  cur_end_pos = src + AES_BLOCK_SIZE - a->block_offset;
126  cur_end_pos = FFMIN(cur_end_pos, src_end);
127 
128  a->block_offset += cur_end_pos - src;
129  a->block_offset &= (AES_BLOCK_SIZE - 1);
130 
131  while (src < cur_end_pos) {
132  *dst++ = *src++ ^ *encrypted_counter_pos++;
133  }
134  }
135 }
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:195
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVAESCTR::block_offset
int block_offset
Definition: aes_ctr.c:33
av_aes_ctr_init
int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
Initialize an AVAESCTR context.
Definition: aes_ctr.c:69
av_aes_ctr_get_iv
const uint8_t * av_aes_ctr_get_iv(struct AVAESCTR *a)
Get the current iv.
Definition: aes_ctr.c:54
AES_CTR_IV_SIZE
#define AES_CTR_IV_SIZE
Definition: aes_ctr.h:31
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:120
av_aes_ctr_alloc
struct AVAESCTR * av_aes_ctr_alloc(void)
Allocate an AVAESCTR context.
Definition: aes_ctr.c:36
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:111
av_aes_ctr_set_random_iv
void av_aes_ctr_set_random_iv(struct AVAESCTR *a)
Generate a random iv.
Definition: aes_ctr.c:59
AES_BLOCK_SIZE
#define AES_BLOCK_SIZE
Definition: aes_ctr.c:27
key
const char * key
Definition: hwcontext_opencl.c:168
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:163
src
#define src
Definition: vp8dsp.c:255
av_aes_alloc
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition: aes.c:31
av_aes_ctr_free
void av_aes_ctr_free(struct AVAESCTR *a)
Release an AVAESCTR context.
Definition: aes_ctr.c:84
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:41
aes_ctr.h
AVAESCTR::encrypted_counter
uint8_t encrypted_counter[AES_BLOCK_SIZE]
Definition: aes_ctr.c:32
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
common.h
uint8_t
uint8_t
Definition: audio_convert.c:194
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
AVAES
Definition: aes_internal.h:34
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:48
random_seed.h
AVAESCTR::counter
uint8_t counter[AES_BLOCK_SIZE]
Definition: aes_ctr.c:31
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_aes_ctr_increment_iv
void av_aes_ctr_increment_iv(struct AVAESCTR *a)
Increment the top 64 bit of the iv (performed after each frame)
Definition: aes_ctr.c:104
av_aes_ctr_increment_be64
static void av_aes_ctr_increment_be64(uint8_t *counter)
Definition: aes_ctr.c:92
AVAESCTR
Definition: aes_ctr.c:29
AVAESCTR::aes
struct AVAES * aes
Definition: aes_ctr.c:30