Debian Codegraph @

src/raw_ctr.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2014, Legrandin <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include "common.h"
33
34 FAKE_INIT(raw_ctr)
35
36 #include "block_base.h"
37
38 #define ERR_CTR_COUNTER_BLOCK_LEN ((6 << 16) | 1)
39 #define ERR_CTR_REPEATED_KEY_STREAM ((6 << 16) | 2)
40
41 #define NR_BLOCKS 8
42
43 typedef struct {
44 BlockBase *cipher;
45
46 /**
47 * A counter block is always as big as a cipher block.
48 * It is made up by three areas:
49 * 1) Prefix - immutable - can be empty
50 * 2) Counter - mutable (+1 per block) - at least 1 byte
51 * 3) Postfix - immutable - can be empty
52 */
53 uint8_t *counter_blocks; /** block_len * NR_BLOCKS bytes **/
54
55 uint8_t *counter; /** point to counter in 1st block within counter_blocks **/
56 size_t counter_len;
57 unsigned little_endian;
58
59 uint8_t *keystream; /** block_len * NR_BLOCKS bytes **/
60 size_t used_ks; /** Bytes we already used in the key stream **/
61
62 /** Number of bytes we have encrypted so far **/
63 uint64_t length_lo, length_hi;
64
65 /** Max number of bytes we may encrypt at most **/
66 uint64_t length_max_lo, length_max_hi;
67 } CtrModeState;
68
69 typedef void (*Increment)(uint8_t *pCounter, size_t counter_len, unsigned amount);
70
71 static inline void increment_le(uint8_t *pCounter, size_t counter_len, unsigned amount) {
72 size_t i;
73
74 for (i=0; i<counter_len && amount>0; i++, pCounter++) {
75 *pCounter = (uint8_t)(*pCounter + amount);
76 amount = *pCounter < amount;
77 }
78 }
79
80 static inline void increment_be(uint8_t *pCounter, size_t counter_len, unsigned amount) {
81 size_t i;
82
83 pCounter += counter_len - 1;
84 for (i=0; i<counter_len && amount>0; i++, pCounter--) {
85 *pCounter = (uint8_t)(*pCounter + amount);
86 amount = *pCounter < amount;
87 }
88 }
89
90 /*
91 * Create the initial sequence of counter blocks
92 */
93 static uint8_t* create_counter_blocks(uint8_t *counter_block0, size_t block_len, size_t prefix_len, unsigned counter_len, Increment increment)
94 {
95 unsigned i;
96 uint8_t *counter_blocks, *current;
97
98 counter_blocks = current = align_alloc(block_len * NR_BLOCKS, (unsigned)block_len);
99 if (NULL == counter_blocks) {
100 return NULL;
101 }
102
103 memcpy(current, counter_block0, block_len);
104 current += block_len;
105
106 for (i=0; i<NR_BLOCKS-1; i++ ) {
107 memcpy(current, current - block_len, block_len);
108 increment(current + prefix_len, counter_len, 1);
109 current += block_len;
110 }
111
112 return counter_blocks;
113 }
114
115 static uint8_t* create_keystream(BlockBase *cipher, uint8_t *counter_blocks, size_t block_len)
116 {
117 uint8_t *keystream;
118
119 keystream = align_alloc(block_len * NR_BLOCKS, (unsigned)block_len);
120 if (NULL == keystream) {
121 return NULL;
122 }
123
124 cipher->encrypt(cipher,
125 counter_blocks,
126 keystream,
127 cipher->block_len * NR_BLOCKS);
128
129 return keystream;
130 }
131
132 EXPORT_SYM int CTR_start_operation(BlockBase *cipher,
133 uint8_t counter_block0[],
134 size_t counter_block0_len,
135 size_t prefix_len,
136 unsigned counter_len,
137 unsigned little_endian,
138 CtrModeState **pResult)
139 {
140 CtrModeState *ctr_state;
141 size_t block_len;
142 Increment increment = little_endian ? increment_le : increment_be;
143
144 if (NULL == cipher || NULL == counter_block0 || NULL == pResult) {
145 return ERR_NULL;
146 }
147
148 block_len = cipher->block_len;
149
150 if (block_len != counter_block0_len ||
151 counter_len == 0 || counter_len > block_len ||
152 block_len < (prefix_len + counter_len)) {
153 return ERR_CTR_COUNTER_BLOCK_LEN;
154 }
155
156 ctr_state = calloc(1, sizeof(CtrModeState));
157 if (NULL == ctr_state) {
158 return ERR_MEMORY;
159 }
160
161 ctr_state->cipher = cipher;
162
163 ctr_state->counter_blocks = create_counter_blocks(counter_block0,
164 block_len,
165 prefix_len,
166 counter_len,
167 increment);
168 if (NULL == ctr_state->counter_blocks) {
169 goto error;
170 }
171
172 ctr_state->counter = ctr_state->counter_blocks + prefix_len;
173 ctr_state->counter_len = counter_len;
174 ctr_state->little_endian = little_endian;
175
176 ctr_state->keystream = create_keystream(cipher, ctr_state->counter_blocks, block_len);
177 if (NULL == ctr_state->keystream) {
178 goto error;
179 }
180 ctr_state->used_ks = 0;
181
182 ctr_state->length_lo = ctr_state->length_hi = 0;
183 ctr_state->length_max_lo = ctr_state->length_max_hi = 0;
184
185 assert(block_len < 256);
186 assert(block_len > 0);
187 if (counter_len < 8)
188 ctr_state->length_max_lo = (uint64_t)block_len << (counter_len*8);
189 if (counter_len >= 8 && counter_len < 16)
190 ctr_state->length_max_hi = (uint64_t)block_len << ((counter_len-8)*8);
191
192 /** length_max_hi and length_max_lo are both zero when counter_len is 16 **/
193
194 *pResult = ctr_state;
195 return 0;
196
197 error:
198 align_free(ctr_state->keystream);
199 align_free(ctr_state->counter_blocks);
200 free(ctr_state);
201 return ERR_MEMORY;
202 }
203
204 static inline void update_keystream(CtrModeState *ctr_state)
205 {
206 unsigned i;
207 uint8_t *counter;
208 size_t block_len;
209
210 counter = ctr_state->counter;
211 block_len = ctr_state->cipher->block_len;
212
213 /** Update all consecutive counter blocks **/
214 if (ctr_state->little_endian) {
215 for (i=0; i<NR_BLOCKS; i++) {
216 increment_le(counter, ctr_state->counter_len, NR_BLOCKS);
217 counter += block_len;
218 }
219 } else {
220 for (i=0; i<NR_BLOCKS; i++) {
221 increment_be(counter, ctr_state->counter_len, NR_BLOCKS);
222 counter += block_len;
223 }
224 }
225
226 ctr_state->cipher->encrypt(ctr_state->cipher,
227 ctr_state->counter_blocks,
228 ctr_state->keystream,
229 ctr_state->cipher->block_len * NR_BLOCKS);
230 ctr_state->used_ks = 0;
231 }
232
233 EXPORT_SYM int CTR_encrypt(CtrModeState *ctr_state,
234 const uint8_t *in,
235 uint8_t *out,
236 size_t data_len)
237 {
238 size_t block_len;
239 uint64_t max_hi, max_lo;
240
241 if (NULL == ctr_state || NULL == in || NULL == out)
242 return ERR_NULL;
243
244 block_len = ctr_state->cipher->block_len;
245 max_hi = ctr_state->length_max_hi;
246 max_lo = ctr_state->length_max_lo;
247
248 while (data_len > 0) {
249 size_t ks_to_use;
250 size_t ks_size;
251 unsigned j;
252
253 ks_size = block_len * NR_BLOCKS;
254 if (ctr_state->used_ks == ks_size)
255 update_keystream(ctr_state);
256
257 ks_to_use = MIN(data_len, ks_size - ctr_state->used_ks);
258
259 for (j=0; j<ks_to_use; j++) {
260 *out++ = *in++ ^ ctr_state->keystream[j + ctr_state->used_ks];
261 }
262
263 data_len -= ks_to_use;
264 ctr_state->used_ks += ks_to_use;
265
266 ctr_state->length_lo += ks_to_use;
267 if (ctr_state->length_lo < ks_to_use) {
268 ctr_state->length_hi++;
269 if (ctr_state->length_hi == 0)
270 return ERR_CTR_REPEATED_KEY_STREAM;
271 }
272
273 /** 128-bit counter **/
274 if (0 == max_lo && 0 == max_hi)
275 continue;
276
277 if (ctr_state->length_hi > max_hi)
278 return ERR_CTR_REPEATED_KEY_STREAM;
279 if (ctr_state->length_hi == max_hi &&
280 ctr_state->length_lo > max_lo)
281 return ERR_CTR_REPEATED_KEY_STREAM;
282 }
283
284 return 0;
285 }
286
287 EXPORT_SYM int CTR_decrypt(CtrModeState *ctr_state,
288 const uint8_t *in,
289 uint8_t *out,
290 size_t data_len)
291 {
292 return CTR_encrypt(ctr_state, in, out, data_len);
293 }
294
295 EXPORT_SYM int CTR_stop_operation(CtrModeState *ctr_state)
296 {
297 if (NULL == ctr_state)
298 return ERR_NULL;
299 ctr_state->cipher->destructor(ctr_state->cipher);
300 align_free(ctr_state->keystream);
301 align_free(ctr_state->counter_blocks);
302 free(ctr_state);
303 return 0;
304 }