Debian Codegraph @

src/raw_ocb.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_ocb)
35
36 #include "block_base.h"
37 #include <assert.h>
38 #include <stdio.h>
39
40 #define BLOCK_SIZE 16
41
42 typedef uint8_t DataBlock[BLOCK_SIZE];
43
44 typedef struct {
45 BlockBase *cipher;
46
47 DataBlock L_star;
48 DataBlock L_dollar;
49 DataBlock L[65]; /** 0..64 **/
50
51 /** Associated data **/
52 uint64_t counter_A;
53 DataBlock offset_A;
54 DataBlock sum;
55
56 /** Ciphertext/plaintext **/
57 uint64_t counter_P;
58 DataBlock offset_P;
59 DataBlock checksum;
60 } OcbModeState;
61
62 static void double_L(DataBlock *out, DataBlock *in)
63 {
64 unsigned carry;
65 int i;
66
67 carry = 0;
68 for (i=BLOCK_SIZE-1; i>=0; i--) {
69 unsigned t;
70
71 t = ((unsigned)(*in)[i] << 1) | carry;
72 carry = t >> 8;
73 (*out)[i] = (uint8_t)t;
74 }
75 carry |= 0x100;
76 carry |= carry << 1;
77 carry |= carry << 2;
78 carry |= carry << 4;
79 (*out)[BLOCK_SIZE-1] = (uint8_t)((*out)[BLOCK_SIZE-1] ^ (carry & 0x87));
80 }
81
82 static unsigned ntz(uint64_t counter)
83 {
84 unsigned i;
85 for (i=0; i<65; i++) {
86 if (counter & 1)
87 return i;
88 counter >>= 1;
89 }
90 return 64;
91 }
92
93 EXPORT_SYM int OCB_start_operation(BlockBase *cipher,
94 const uint8_t *offset_0,
95 size_t offset_0_len,
96 OcbModeState **pState)
97 {
98
99 OcbModeState *state;
100 int result;
101 unsigned i;
102
103 if ((NULL == cipher) || (NULL == pState)) {
104 return ERR_NULL;
105 }
106
107 if ((BLOCK_SIZE != cipher->block_len) || (BLOCK_SIZE != offset_0_len)) {
108 return ERR_BLOCK_SIZE;
109 }
110
111 *pState = state = calloc(1, sizeof(OcbModeState));
112 if (NULL == state) {
113 return ERR_MEMORY;
114 }
115
116 state->cipher = cipher;
117
118 result = state->cipher->encrypt(state->cipher, state->checksum, state->L_star, BLOCK_SIZE);
119 if (result)
120 return result;
121
122 double_L(&state->L_dollar, &state->L_star);
123 double_L(&state->L[0], &state->L_dollar);
124 for (i=1; i<=64; i++)
125 double_L(&state->L[i], &state->L[i-1]);
126
127 memcpy(state->offset_P, offset_0, BLOCK_SIZE);
128
129 state->counter_A = state->counter_P = 1;
130
131 return 0;
132 }
133
134 enum OcbDirection { OCB_ENCRYPT, OCB_DECRYPT };
135
136 EXPORT_SYM int OCB_transcrypt(OcbModeState *state,
137 const uint8_t *in,
138 uint8_t *out,
139 size_t in_len,
140 enum OcbDirection direction)
141 {
142 CipherOperation process = NULL;
143 const uint8_t *checksummed = NULL;
144 int result;
145 unsigned i;
146
147 if ((NULL == state) || (NULL == out) || (NULL == in))
148 return ERR_NULL;
149
150 assert(OCB_ENCRYPT==direction || OCB_DECRYPT==direction);
151 checksummed = OCB_ENCRYPT==direction ? in : out;
152 process = OCB_ENCRYPT==direction ? state->cipher->encrypt : state->cipher->decrypt;
153
154 for (;in_len>=BLOCK_SIZE; in_len-=BLOCK_SIZE) {
155 unsigned idx;
156 DataBlock pre;
157
158 idx = ntz(state->counter_P);
159 for (i=0; i<BLOCK_SIZE; i++) {
160 state->offset_P[i] ^= state->L[idx][i];
161 pre[i] = in[i] ^ state->offset_P[i];
162 }
163 if (++state->counter_P == 0)
164 return ERR_MAX_DATA;
165
166 result = process(state->cipher, pre, out, BLOCK_SIZE);
167 if (result)
168 return result;
169
170 for (i=0; i<BLOCK_SIZE; i++) {
171 out[i] ^= state->offset_P[i];
172 state->checksum[i] ^= checksummed[i];
173 }
174
175 in += BLOCK_SIZE;
176 checksummed += BLOCK_SIZE;
177 out += BLOCK_SIZE;
178 }
179
180 /** Process last piece (if any) **/
181 if (in_len>0) {
182 DataBlock pad;
183
184 for (i=0; i<BLOCK_SIZE; i++)
185 state->offset_P[i] ^= state->L_star[i];
186
187 result = state->cipher->encrypt(state->cipher, state->offset_P, pad, BLOCK_SIZE);
188 if (result)
189 return result;
190
191 for (i=0; i<in_len; i++) {
192 out[i] = in[i] ^ pad[i];
193 state->checksum[i] ^= checksummed[i];
194 }
195 state->checksum[in_len] ^= 0x80;
196 }
197
198 return 0;
199 }
200
201 /**
202 * Encrypt a piece of plaintext.
203 *
204 * @state The block cipher state.
205 * @in A pointer to the plaintext. It is aligned to the 16 byte boundary
206 * unless it is the last block.
207 * @out A pointer to an output buffer, that will hold the ciphertext.
208 * The caller must allocate an area of memory as big as the plaintext.
209 * @in_len The size of the plaintext pointed to by @in.
210 *
211 * @return 0 in case of success, otherwise the relevant error code.
212 */
213 EXPORT_SYM int OCB_encrypt(OcbModeState *state,
214 const uint8_t *in,
215 uint8_t *out,
216 size_t in_len)
217 {
218 return OCB_transcrypt(state, in, out, in_len, OCB_ENCRYPT);
219 }
220
221 /**
222 * Decrypt a piece of ciphertext.
223 *
224 * @state The block cipher state.
225 * @in A pointer to the ciphertext. It is aligned to the 16 byte boundary
226 * unless it is the last block.
227 * @out A pointer to an output buffer, that will hold the plaintext.
228 * The caller must allocate an area of memory as big as the ciphertext.
229 * @in_len The size of the ciphertext pointed to by @in.
230 *
231 * @return 0 in case of success, otherwise the relevant error code.
232 */
233 EXPORT_SYM int OCB_decrypt(OcbModeState *state,
234 const uint8_t *in,
235 uint8_t *out,
236 size_t in_len)
237 {
238 return OCB_transcrypt(state, in, out, in_len, OCB_DECRYPT);
239 }
240
241 /**
242 * Process a piece of authenticated data.
243 *
244 * @state The block cipher state.
245 * @in A pointer to the authenticated data.
246 * It must be aligned to the 16 byte boundary, unless it is
247 * the last piece.
248 * @in_len The size of the authenticated data pointed to by @in.
249 */
250 EXPORT_SYM int OCB_update(OcbModeState *state,
251 const uint8_t *in,
252 size_t in_len)
253 {
254 int result;
255 unsigned i;
256 DataBlock pt;
257 DataBlock ct;
258
259 if ((NULL == state) || (NULL == in))
260 return ERR_NULL;
261
262 for (;in_len>=BLOCK_SIZE; in_len-=BLOCK_SIZE) {
263 unsigned idx;
264
265 idx = ntz(state->counter_A);
266 for (i=0; i<BLOCK_SIZE; i++) {
267 state->offset_A[i] ^= state->L[idx][i];
268 pt[i] = in[i] ^ state->offset_A[i];
269 }
270 if (++state->counter_A == 0)
271 return ERR_MAX_DATA;
272
273 result = state->cipher->encrypt(state->cipher, pt, ct, BLOCK_SIZE);
274 if (result)
275 return result;
276
277 for (i=0; i<BLOCK_SIZE; i++)
278 state->sum[i] ^= ct[i];
279
280 in += BLOCK_SIZE;
281 }
282
283 /** Process last piece (if any) **/
284 if (in_len>0) {
285 memset(pt, 0, sizeof pt);
286 memcpy(pt, in, in_len);
287 pt[in_len] = 0x80;
288
289 for (i=0; i<BLOCK_SIZE; i++)
290 pt[i] ^= state->offset_A[i] ^ state->L_star[i];
291
292 result = state->cipher->encrypt(state->cipher, pt, ct, BLOCK_SIZE);
293 if (result)
294 return result;
295
296 for (i=0; i<BLOCK_SIZE; i++)
297 state->sum[i] ^= ct[i];
298 }
299
300 return 0;
301 }
302
303 EXPORT_SYM int OCB_digest(OcbModeState *state,
304 uint8_t *tag,
305 size_t tag_len)
306 {
307 DataBlock pt;
308 unsigned i;
309 int result;
310
311 if ((NULL == state) || (NULL == tag))
312 return ERR_NULL;
313
314 if (BLOCK_SIZE != tag_len)
315 return ERR_TAG_SIZE;
316
317 for (i=0; i<BLOCK_SIZE; i++)
318 pt[i] = state->checksum[i] ^ state->offset_P[i] ^ state->L_dollar[i];
319
320 result = state->cipher->encrypt(state->cipher, pt, tag, BLOCK_SIZE);
321 if (result)
322 return result;
323
324 /** state->sum is HASH(K, A) **/
325 for (i=0; i<BLOCK_SIZE; i++)
326 tag[i] ^= state->sum[i];
327
328 return 0;
329 }
330
331 EXPORT_SYM int OCB_stop_operation(OcbModeState *state)
332 {
333 if (NULL == state)
334 return ERR_NULL;
335 state->cipher->destructor(state->cipher);
336 free(state);
337 return 0;
338 }