FFmpeg
Loading...
Searching...
No Matches
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 <string.h>
23
24#include "aes_ctr.h"
25#include "aes.h"
26#include "aes_internal.h"
27#include "intreadwrite.h"
28#include "macros.h"
29#include "mem.h"
30#include "random_seed.h"
31
32#define AES_BLOCK_SIZE (16)
33
40
42{
43 return av_mallocz(sizeof(struct AVAESCTR));
44}
45
46void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t* iv)
47{
48 memcpy(a->counter, iv, AES_CTR_IV_SIZE);
49 memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
50 a->block_offset = 0;
51}
52
53void av_aes_ctr_set_full_iv(struct AVAESCTR *a, const uint8_t* iv)
54{
55 memcpy(a->counter, iv, sizeof(a->counter));
56 a->block_offset = 0;
57}
58
59const uint8_t* av_aes_ctr_get_iv(struct AVAESCTR *a)
60{
61 return a->counter;
62}
63
65{
66 uint32_t iv[2];
67
68 iv[0] = av_get_random_seed();
69 iv[1] = av_get_random_seed();
70
71 av_aes_ctr_set_iv(a, (uint8_t*)iv);
72}
73
74int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
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
85{
86 av_free(a);
87}
88
89static inline void av_aes_ctr_increment_be64(uint8_t* counter)
90{
91 uint64_t c = AV_RB64A(counter) + 1;
93}
94
96{
98 memset(a->counter + AES_CTR_IV_SIZE, 0, sizeof(a->counter) - AES_CTR_IV_SIZE);
99 a->block_offset = 0;
100}
101
102void av_aes_ctr_crypt(struct AVAESCTR *a, uint8_t *dst, const uint8_t *src, int count)
103{
104 if (a->block_offset && count > 0) {
105 int left = FFMIN(count, AES_BLOCK_SIZE - a->block_offset);
106 for (int len = 0; len < left; len++)
107 dst[len] = src[len] ^ a->encrypted_counter[a->block_offset++];
108 a->block_offset &= AES_BLOCK_SIZE - 1;
109 dst += left;
110 src += left;
111 count -= left;
112 }
113
114 while (count >= AES_BLOCK_SIZE) {
115 av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
116 av_aes_ctr_increment_be64(a->counter + 8);
117#if HAVE_FAST_64BIT
118 for (int len = 0; len < AES_BLOCK_SIZE; len += 8)
119 AV_WN64(&dst[len], AV_RN64(&src[len]) ^ AV_RN64A(&a->encrypted_counter[len]));
120#else
121 for (int len = 0; len < AES_BLOCK_SIZE; len += 4)
122 AV_WN32(&dst[len], AV_RN32(&src[len]) ^ AV_RN32A(&a->encrypted_counter[len]));
123#endif
126 count -= AES_BLOCK_SIZE;
127 }
128
129 if (count > 0) {
130 av_aes_crypt(&a->aes, a->encrypted_counter, a->counter, 1, NULL, 0);
131 av_aes_ctr_increment_be64(a->counter + 8);
132 for (int len = 0; len < count; len++)
133 dst[len] = src[len] ^ a->encrypted_counter[a->block_offset++];
134 }
135}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static void av_aes_ctr_increment_be64(uint8_t *counter)
Definition aes_ctr.c:89
#define AES_BLOCK_SIZE
Definition aes_ctr.c:32
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define NULL
Definition coverity.c:32
const char * key
#define AES_CTR_IV_SIZE
Definition aes_ctr.h:36
struct AVAESCTR * av_aes_ctr_alloc(void)
Allocate an AVAESCTR context.
Definition aes_ctr.c:41
void av_aes_ctr_set_iv(struct AVAESCTR *a, const uint8_t *iv)
Forcefully change the 8-byte iv.
Definition aes_ctr.c:46
int av_aes_ctr_init(struct AVAESCTR *a, const uint8_t *key)
Initialize an AVAESCTR context.
Definition aes_ctr.c:74
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:53
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:102
void av_aes_ctr_free(struct AVAESCTR *a)
Release an AVAESCTR context.
Definition aes_ctr.c:84
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:95
void av_aes_ctr_set_random_iv(struct AVAESCTR *a)
Generate a random iv.
Definition aes_ctr.c:64
const uint8_t * av_aes_ctr_get_iv(struct AVAESCTR *a)
Get the current iv.
Definition aes_ctr.c:59
int av_aes_init(AVAES *a, const uint8_t *key, int key_bits, int decrypt)
Initialize an AVAES context.
Definition aes.c:231
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:171
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
int a
#define AV_WN32(p, v)
#define AV_RN64(p)
#define AV_RN32(p)
#define AV_WB64A(p, v)
#define AV_WN64(p, v)
#define AV_RN32A(p)
#define AV_RB64A(p)
#define AV_RN64A(p)
Utility Preprocessor macros.
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
int block_offset
Definition aes_ctr.c:37
AVAES aes
Definition aes_ctr.c:38
uint8_t counter[AES_BLOCK_SIZE]
Definition aes_ctr.c:35
uint8_t encrypted_counter[AES_BLOCK_SIZE]
Definition aes_ctr.c:36
#define av_free(p)
#define av_mallocz(s)
#define src
Definition vp8dsp.c:248
int len
static double c[64]