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
30
31 #define AES_BLOCK_SIZE (16)
32
39
41 {
43 }
44
46 {
50 }
51
53 {
54 memcpy(
a->counter, iv,
sizeof(
a->counter));
56 }
57
59 {
61 }
62
64 {
65 uint32_t iv[2];
66
69
71 }
72
74 {
76
77 memset(
a->counter, 0,
sizeof(
a->counter));
79
80 return 0;
81 }
82
84 {
86 }
87
89 {
90 uint8_t* cur_pos;
91
93 (*cur_pos)++;
94 if (*cur_pos != 0) {
95 break;
96 }
97 }
98 }
99
101 {
105 }
106
108 {
109 const uint8_t* src_end =
src + count;
110 const uint8_t* cur_end_pos;
111 uint8_t* encrypted_counter_pos;
112
113 while (
src < src_end) {
114 if (
a->block_offset == 0) {
116
118 }
119
120 encrypted_counter_pos =
a->encrypted_counter +
a->block_offset;
122 cur_end_pos =
FFMIN(cur_end_pos, src_end);
123
124 a->block_offset += cur_end_pos -
src;
126
127 while (
src < cur_end_pos) {
128 *dst++ = *
src++ ^ *encrypted_counter_pos++;
129 }
130 }
131 }