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  * 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 
49 {
50  return a->counter;
51 }
52 
54 {
55  uint32_t iv[2];
56 
57  iv[0] = av_get_random_seed();
58  iv[1] = av_get_random_seed();
59 
60  av_aes_ctr_set_iv(a, (uint8_t*)iv);
61 }
62 
63 int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
64 {
65  a->aes = av_aes_alloc();
66  if (!a->aes) {
67  return AVERROR(ENOMEM);
68  }
69 
70  av_aes_init(a->aes, key, 128, 0);
71 
72  memset(a->counter, 0, sizeof(a->counter));
73  a->block_offset = 0;
74 
75  return 0;
76 }
77 
78 void av_aes_ctr_free(struct AVAESCTR *a)
79 {
80  if (a) {
81  av_freep(&a->aes);
82  av_free(a);
83  }
84 }
85 
87 {
88  uint8_t* cur_pos;
89 
90  for (cur_pos = counter + 7; cur_pos >= counter; cur_pos--) {
91  (*cur_pos)++;
92  if (*cur_pos != 0) {
93  break;
94  }
95  }
96 }
97 
99 {
101  memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
102  a->block_offset = 0;
103 }
104 
105 void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
106 {
107  const uint8_t* src_end = src + count;
108  const uint8_t* cur_end_pos;
109  uint8_t* encrypted_counter_pos;
110 
111  while (src < src_end) {
112  if (a->block_offset == 0) {
113  av_aes_crypt(a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
114 
116  }
117 
118  encrypted_counter_pos = a->encrypted_counter + a->block_offset;
119  cur_end_pos = src + AES_BLOCK_SIZE - a->block_offset;
120  cur_end_pos = FFMIN(cur_end_pos, src_end);
121 
122  a->block_offset += cur_end_pos - src;
123  a->block_offset &= (AES_BLOCK_SIZE - 1);
124 
125  while (src < cur_end_pos) {
126  *dst++ = *src++ ^ *encrypted_counter_pos++;
127  }
128  }
129 }
uint8_t counter[AES_BLOCK_SIZE]
Definition: aes_ctr.c:31
#define NULL
Definition: coverity.c:32
int block_offset
Definition: aes_ctr.c:33
static void av_aes_ctr_increment_be64(uint8_t *counter)
Definition: aes_ctr.c:86
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
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:98
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
uint8_t encrypted_counter[AES_BLOCK_SIZE]
Definition: aes_ctr.c:32
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
void av_aes_ctr_free(struct AVAESCTR *a)
Release an AVAESCTR context.
Definition: aes_ctr.c:78
#define AVERROR(e)
Definition: error.h:43
struct AVAES * av_aes_alloc(void)
Allocate an AVAES context.
Definition: aes.c:31
GLsizei count
Definition: opengl_enc.c:109
struct AVAESCTR * av_aes_ctr_alloc(void)
Allocate an AVAESCTR context.
Definition: aes_ctr.c:36
#define FFMIN(a, b)
Definition: common.h:96
#define src
Definition: vp9dsp.c:530
void av_aes_ctr_set_random_iv(struct AVAESCTR *a)
Generate a random iv.
Definition: aes_ctr.c:53
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition: aes.c:195
#define AES_BLOCK_SIZE
Definition: aes_ctr.c:27
void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t *iv)
Forcefully change the iv.
Definition: aes_ctr.c:41
common internal and external API header
#define av_free(p)
int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
Initialize an AVAESCTR context.
Definition: aes_ctr.c:63
#define av_freep(p)
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:114
#define AES_CTR_IV_SIZE
Definition: aes_ctr.h:31
const uint8_t * av_aes_ctr_get_iv(struct AVAESCTR *a)
Get the current iv.
Definition: aes_ctr.c:48
struct AVAES * aes
Definition: aes_ctr.c:30