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 "aes_internal.h"
26 #include "random_seed.h"
27 
28 #define AES_BLOCK_SIZE (16)
29 
30 typedef struct AVAESCTR {
35 } AVAESCTR;
36 
38 {
39  return av_mallocz(sizeof(struct AVAESCTR));
40 }
41 
42 void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t* iv)
43 {
44  memcpy(a->counter, iv, AES_CTR_IV_SIZE);
45  memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
46  a->block_offset = 0;
47 }
48 
49 void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t* iv)
50 {
51  memcpy(a->counter, iv, sizeof(a->counter));
52  a->block_offset = 0;
53 }
54 
55 const uint8_t* av_aes_ctr_get_iv(struct AVAESCTR *a)
56 {
57  return a->counter;
58 }
59 
61 {
62  uint32_t iv[2];
63 
64  iv[0] = av_get_random_seed();
65  iv[1] = av_get_random_seed();
66 
67  av_aes_ctr_set_iv(a, (uint8_t*)iv);
68 }
69 
70 int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
71 {
72  av_aes_init(&a->aes, key, 128, 0);
73 
74  memset(a->counter, 0, sizeof(a->counter));
75  a->block_offset = 0;
76 
77  return 0;
78 }
79 
80 void av_aes_ctr_free(struct AVAESCTR *a)
81 {
82  av_free(a);
83 }
84 
85 static void av_aes_ctr_increment_be64(uint8_t* counter)
86 {
87  uint8_t* cur_pos;
88 
89  for (cur_pos = counter + 7; cur_pos >= counter; cur_pos--) {
90  (*cur_pos)++;
91  if (*cur_pos != 0) {
92  break;
93  }
94  }
95 }
96 
98 {
99  av_aes_ctr_increment_be64(a->counter);
100  memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
101  a->block_offset = 0;
102 }
103 
104 void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
105 {
106  const uint8_t* src_end = src + count;
107  const uint8_t* cur_end_pos;
108  uint8_t* encrypted_counter_pos;
109 
110  while (src < src_end) {
111  if (a->block_offset == 0) {
112  av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
113 
114  av_aes_ctr_increment_be64(a->counter + 8);
115  }
116 
117  encrypted_counter_pos = a->encrypted_counter + a->block_offset;
118  cur_end_pos = src + AES_BLOCK_SIZE - a->block_offset;
119  cur_end_pos = FFMIN(cur_end_pos, src_end);
120 
121  a->block_offset += cur_end_pos - src;
122  a->block_offset &= (AES_BLOCK_SIZE - 1);
123 
124  while (src < cur_end_pos) {
125  *dst++ = *src++ ^ *encrypted_counter_pos++;
126  }
127  }
128 }
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
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:70
av_aes_ctr_get_iv
const uint8_t * av_aes_ctr_get_iv(struct AVAESCTR *a)
Get the current iv.
Definition: aes_ctr.c:55
AES_CTR_IV_SIZE
#define AES_CTR_IV_SIZE
Definition: aes_ctr.h:30
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:37
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:104
av_aes_ctr_set_random_iv
void av_aes_ctr_set_random_iv(struct AVAESCTR *a)
Generate a random iv.
Definition: aes_ctr.c:60
AES_BLOCK_SIZE
#define AES_BLOCK_SIZE
Definition: aes_ctr.c:28
key
const char * key
Definition: hwcontext_opencl.c:168
AVAESCTR::aes
AVAES aes
Definition: aes_ctr.c:34
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_ctr_free
void av_aes_ctr_free(struct AVAESCTR *a)
Release an AVAESCTR context.
Definition: aes_ctr.c:80
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:42
aes_ctr.h
AVAESCTR::encrypted_counter
uint8_t encrypted_counter[AES_BLOCK_SIZE]
Definition: aes_ctr.c:32
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
aes_internal.h
common.h
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
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:263
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:49
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_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:97
av_aes_ctr_increment_be64
static void av_aes_ctr_increment_be64(uint8_t *counter)
Definition: aes_ctr.c:85
AVAESCTR
Definition: aes_ctr.c:30