Debian Codegraph @

cipher/bulkhelp.h

raw
1 /* bulkhelp.h - Some bulk processing helpers
2 * Copyright (C) 2022 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef GCRYPT_BULKHELP_H
20 #define GCRYPT_BULKHELP_H
21
22
23 #include "g10lib.h"
24 #include "cipher-internal.h"
25
26
27 #ifdef __x86_64__
28 /* Use u64 to store pointers for x32 support (assembly function assumes
29 * 64-bit pointers). */
30 typedef u64 ocb_L_uintptr_t;
31 #else
32 typedef uintptr_t ocb_L_uintptr_t;
33 #endif
34
35 typedef unsigned int (*bulk_crypt_fn_t) (void *ctx, byte *out,
36 const byte *in,
37 size_t num_blks);
38
39
40 static inline ocb_L_uintptr_t *
41 bulk_ocb_prepare_L_pointers_array_blk64 (gcry_cipher_hd_t c,
42 ocb_L_uintptr_t Ls[64], u64 blkn)
43 {
44 unsigned int n = 64 - (blkn % 64);
45 unsigned int i;
46
47 for (i = 0; i < 64; i += 8)
48 {
49 Ls[(i + 0 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
50 Ls[(i + 1 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
51 Ls[(i + 2 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
52 Ls[(i + 3 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[2];
53 Ls[(i + 4 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
54 Ls[(i + 5 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
55 Ls[(i + 6 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
56 }
57
58 Ls[(7 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
59 Ls[(15 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[4];
60 Ls[(23 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
61 Ls[(31 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[5];
62 Ls[(39 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
63 Ls[(47 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[4];
64 Ls[(55 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
65 return &Ls[(63 + n) % 64];
66 }
67
68
69 static inline ocb_L_uintptr_t *
70 bulk_ocb_prepare_L_pointers_array_blk32 (gcry_cipher_hd_t c,
71 ocb_L_uintptr_t Ls[32], u64 blkn)
72 {
73 unsigned int n = 32 - (blkn % 32);
74 unsigned int i;
75
76 for (i = 0; i < 32; i += 8)
77 {
78 Ls[(i + 0 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
79 Ls[(i + 1 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
80 Ls[(i + 2 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
81 Ls[(i + 3 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[2];
82 Ls[(i + 4 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
83 Ls[(i + 5 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
84 Ls[(i + 6 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
85 }
86
87 Ls[(7 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
88 Ls[(15 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[4];
89 Ls[(23 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
90 return &Ls[(31 + n) % 32];
91 }
92
93
94 static inline ocb_L_uintptr_t *
95 bulk_ocb_prepare_L_pointers_array_blk16 (gcry_cipher_hd_t c,
96 ocb_L_uintptr_t Ls[16], u64 blkn)
97 {
98 unsigned int n = 16 - (blkn % 16);
99 unsigned int i;
100
101 for (i = 0; i < 16; i += 8)
102 {
103 Ls[(i + 0 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
104 Ls[(i + 1 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
105 Ls[(i + 2 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
106 Ls[(i + 3 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[2];
107 Ls[(i + 4 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
108 Ls[(i + 5 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
109 Ls[(i + 6 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
110 }
111
112 Ls[(7 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
113 return &Ls[(15 + n) % 16];
114 }
115
116
117 static inline ocb_L_uintptr_t *
118 bulk_ocb_prepare_L_pointers_array_blk8 (gcry_cipher_hd_t c,
119 ocb_L_uintptr_t Ls[8], u64 blkn)
120 {
121 unsigned int n = 8 - (blkn % 8);
122
123 Ls[(0 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
124 Ls[(1 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
125 Ls[(2 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
126 Ls[(3 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[2];
127 Ls[(4 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
128 Ls[(5 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
129 Ls[(6 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
130 Ls[(7 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
131
132 return &Ls[(7 + n) % 8];
133 }
134
135
136 static inline unsigned int
137 bulk_ctr_enc_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
138 const byte *inbuf, size_t nblocks, byte *ctr,
139 byte *tmpbuf, size_t tmpbuf_nblocks,
140 unsigned int *num_used_tmpblocks)
141 {
142 unsigned int tmp_used = 16;
143 unsigned int burn_depth = 0;
144 unsigned int nburn;
145
146 while (nblocks >= 1)
147 {
148 size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
149 size_t i;
150
151 if (curr_blks * 16 > tmp_used)
152 tmp_used = curr_blks * 16;
153
154 cipher_block_cpy (tmpbuf + 0 * 16, ctr, 16);
155 for (i = 1; i < curr_blks; i++)
156 {
157 cipher_block_cpy (&tmpbuf[i * 16], ctr, 16);
158 cipher_block_add (&tmpbuf[i * 16], i, 16);
159 }
160 cipher_block_add (ctr, curr_blks, 16);
161
162 nburn = crypt_fn (priv, tmpbuf, tmpbuf, curr_blks);
163 burn_depth = nburn > burn_depth ? nburn : burn_depth;
164
165 for (i = 0; i < curr_blks; i++)
166 {
167 cipher_block_xor (outbuf, &tmpbuf[i * 16], inbuf, 16);
168 outbuf += 16;
169 inbuf += 16;
170 }
171
172 nblocks -= curr_blks;
173 }
174
175 *num_used_tmpblocks = tmp_used;
176 return burn_depth;
177 }
178
179
180 static inline unsigned int
181 bulk_ctr32le_enc_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
182 const byte *inbuf, size_t nblocks, byte *ctr,
183 byte *tmpbuf, size_t tmpbuf_nblocks,
184 unsigned int *num_used_tmpblocks)
185 {
186 unsigned int tmp_used = 16;
187 unsigned int burn_depth = 0;
188 unsigned int nburn;
189
190 while (nblocks >= 1)
191 {
192 size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
193 u64 ctr_lo = buf_get_le64(ctr + 0 * 8);
194 u64 ctr_hi = buf_get_he64(ctr + 1 * 8);
195 size_t i;
196
197 if (curr_blks * 16 > tmp_used)
198 tmp_used = curr_blks * 16;
199
200 cipher_block_cpy (tmpbuf + 0 * 16, ctr, 16);
201 for (i = 1; i < curr_blks; i++)
202 {
203 u32 lo_u32 = (u32)ctr_lo + i;
204 u64 lo_u64 = ctr_lo & ~(u64)(u32)-1;
205 lo_u64 += lo_u32;
206 buf_put_le64(&tmpbuf[0 * 8 + i * 16], lo_u64);
207 buf_put_he64(&tmpbuf[1 * 8 + i * 16], ctr_hi);
208 }
209 buf_put_le32(ctr, (u32)ctr_lo + curr_blks);
210
211 nburn = crypt_fn (priv, tmpbuf, tmpbuf, curr_blks);
212 burn_depth = nburn > burn_depth ? nburn : burn_depth;
213
214 for (i = 0; i < curr_blks; i++)
215 {
216 cipher_block_xor (outbuf, &tmpbuf[i * 16], inbuf, 16);
217 outbuf += 16;
218 inbuf += 16;
219 }
220
221 nblocks -= curr_blks;
222 }
223
224 *num_used_tmpblocks = tmp_used;
225 return burn_depth;
226 }
227
228
229 static inline unsigned int
230 bulk_cbc_dec_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
231 const byte *inbuf, size_t nblocks, byte *iv,
232 byte *tmpbuf, size_t tmpbuf_nblocks,
233 unsigned int *num_used_tmpblocks)
234 {
235 unsigned int tmp_used = 16;
236 unsigned int burn_depth = 0;
237 unsigned int nburn;
238
239 while (nblocks >= 1)
240 {
241 size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
242 size_t i;
243
244 if (curr_blks * 16 > tmp_used)
245 tmp_used = curr_blks * 16;
246
247 nburn = crypt_fn (priv, tmpbuf, inbuf, curr_blks);
248 burn_depth = nburn > burn_depth ? nburn : burn_depth;
249
250 for (i = 0; i < curr_blks; i++)
251 {
252 cipher_block_xor_n_copy_2(outbuf, &tmpbuf[i * 16], iv, inbuf, 16);
253 outbuf += 16;
254 inbuf += 16;
255 }
256
257 nblocks -= curr_blks;
258 }
259
260 *num_used_tmpblocks = tmp_used;
261 return burn_depth;
262 }
263
264
265 static inline unsigned int
266 bulk_cfb_dec_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
267 const byte *inbuf, size_t nblocks, byte *iv,
268 byte *tmpbuf, size_t tmpbuf_nblocks,
269 unsigned int *num_used_tmpblocks)
270 {
271 unsigned int tmp_used = 16;
272 unsigned int burn_depth = 0;
273 unsigned int nburn;
274
275 while (nblocks >= 1)
276 {
277 size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
278 size_t i;
279
280 if (curr_blks * 16 > tmp_used)
281 tmp_used = curr_blks * 16;
282
283 cipher_block_cpy (&tmpbuf[0 * 16], iv, 16);
284 if (curr_blks > 1)
285 memcpy (&tmpbuf[1 * 16], &inbuf[(1 - 1) * 16], 16 * curr_blks - 16);
286 cipher_block_cpy (iv, &inbuf[(curr_blks - 1) * 16], 16);
287
288 nburn = crypt_fn (priv, tmpbuf, tmpbuf, curr_blks);
289 burn_depth = nburn > burn_depth ? nburn : burn_depth;
290
291 for (i = 0; i < curr_blks; i++)
292 {
293 cipher_block_xor (outbuf, inbuf, &tmpbuf[i * 16], 16);
294 outbuf += 16;
295 inbuf += 16;
296 }
297
298 nblocks -= curr_blks;
299 }
300
301 *num_used_tmpblocks = tmp_used;
302 return burn_depth;
303 }
304
305
306 static inline unsigned int
307 bulk_ocb_crypt_128 (gcry_cipher_hd_t c, void *priv, bulk_crypt_fn_t crypt_fn,
308 byte *outbuf, const byte *inbuf, size_t nblocks, u64 *blkn,
309 int encrypt, byte *tmpbuf, size_t tmpbuf_nblocks,
310 unsigned int *num_used_tmpblocks)
311 {
312 unsigned int tmp_used = 16;
313 unsigned int burn_depth = 0;
314 unsigned int nburn;
315
316 while (nblocks >= 1)
317 {
318 size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
319 size_t i;
320
321 if (curr_blks * 16 > tmp_used)
322 tmp_used = curr_blks * 16;
323
324 for (i = 0; i < curr_blks; i++)
325 {
326 const unsigned char *l = ocb_get_l(c, ++*blkn);
327
328 /* Checksum_i = Checksum_{i-1} xor P_i */
329 if (encrypt)
330 cipher_block_xor_1(c->u_ctr.ctr, &inbuf[i * 16], 16);
331
332 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
333 cipher_block_xor_2dst (&tmpbuf[i * 16], c->u_iv.iv, l, 16);
334 cipher_block_xor (&outbuf[i * 16], &inbuf[i * 16],
335 c->u_iv.iv, 16);
336 }
337
338 /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
339 nburn = crypt_fn (priv, outbuf, outbuf, curr_blks);
340 burn_depth = nburn > burn_depth ? nburn : burn_depth;
341
342 for (i = 0; i < curr_blks; i++)
343 {
344 cipher_block_xor_1 (&outbuf[i * 16], &tmpbuf[i * 16], 16);
345
346 /* Checksum_i = Checksum_{i-1} xor P_i */
347 if (!encrypt)
348 cipher_block_xor_1(c->u_ctr.ctr, &outbuf[i * 16], 16);
349 }
350
351 outbuf += curr_blks * 16;
352 inbuf += curr_blks * 16;
353 nblocks -= curr_blks;
354 }
355
356 *num_used_tmpblocks = tmp_used;
357 return burn_depth;
358 }
359
360
361 static inline unsigned int
362 bulk_ocb_auth_128 (gcry_cipher_hd_t c, void *priv, bulk_crypt_fn_t crypt_fn,
363 const byte *abuf, size_t nblocks, u64 *blkn, byte *tmpbuf,
364 size_t tmpbuf_nblocks, unsigned int *num_used_tmpblocks)
365 {
366 unsigned int tmp_used = 16;
367 unsigned int burn_depth = 0;
368 unsigned int nburn;
369
370 while (nblocks >= 1)
371 {
372 size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
373 size_t i;
374
375 if (curr_blks * 16 > tmp_used)
376 tmp_used = curr_blks * 16;
377
378 for (i = 0; i < curr_blks; i++)
379 {
380 const unsigned char *l = ocb_get_l(c, ++*blkn);
381
382 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
383 cipher_block_xor_2dst (&tmpbuf[i * 16],
384 c->u_mode.ocb.aad_offset, l, 16);
385 cipher_block_xor_1 (&tmpbuf[i * 16], &abuf[i * 16], 16);
386 }
387
388 /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
389 nburn = crypt_fn (priv, tmpbuf, tmpbuf, curr_blks);
390 burn_depth = nburn > burn_depth ? nburn : burn_depth;
391
392 for (i = 0; i < curr_blks; i++)
393 {
394 cipher_block_xor_1 (c->u_mode.ocb.aad_sum, &tmpbuf[i * 16], 16);
395 }
396
397 abuf += curr_blks * 16;
398 nblocks -= curr_blks;
399 }
400
401 *num_used_tmpblocks = tmp_used;
402 return burn_depth;
403 }
404
405
406 static inline unsigned int
407 bulk_xts_crypt_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
408 const byte *inbuf, size_t nblocks, byte *tweak,
409 byte *tmpbuf, size_t tmpbuf_nblocks,
410 unsigned int *num_used_tmpblocks)
411 {
412 u64 tweak_lo, tweak_hi, tweak_next_lo, tweak_next_hi, tmp_lo, tmp_hi, carry;
413 unsigned int tmp_used = 16;
414 unsigned int burn_depth = 0;
415 unsigned int nburn;
416
417 tweak_next_lo = buf_get_le64 (tweak + 0);
418 tweak_next_hi = buf_get_le64 (tweak + 8);
419
420 while (nblocks >= 1)
421 {
422 size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
423 size_t i;
424
425 if (curr_blks * 16 > tmp_used)
426 tmp_used = curr_blks * 16;
427
428 for (i = 0; i < curr_blks; i++)
429 {
430 tweak_lo = tweak_next_lo;
431 tweak_hi = tweak_next_hi;
432
433 /* Generate next tweak. */
434 carry = -(tweak_next_hi >> 63) & 0x87;
435 tweak_next_hi = (tweak_next_hi << 1) + (tweak_next_lo >> 63);
436 tweak_next_lo = (tweak_next_lo << 1) ^ carry;
437
438 /* Xor-Encrypt/Decrypt-Xor block. */
439 tmp_lo = buf_get_le64 (inbuf + i * 16 + 0) ^ tweak_lo;
440 tmp_hi = buf_get_le64 (inbuf + i * 16 + 8) ^ tweak_hi;
441 buf_put_he64 (&tmpbuf[i * 16 + 0], tweak_lo);
442 buf_put_he64 (&tmpbuf[i * 16 + 8], tweak_hi);
443 buf_put_le64 (outbuf + i * 16 + 0, tmp_lo);
444 buf_put_le64 (outbuf + i * 16 + 8, tmp_hi);
445 }
446
447 nburn = crypt_fn (priv, outbuf, outbuf, curr_blks);
448 burn_depth = nburn > burn_depth ? nburn : burn_depth;
449
450 for (i = 0; i < curr_blks; i++)
451 {
452 /* Xor-Encrypt/Decrypt-Xor block. */
453 tweak_lo = buf_get_he64 (&tmpbuf[i * 16 + 0]);
454 tweak_hi = buf_get_he64 (&tmpbuf[i * 16 + 8]);
455 tmp_lo = buf_get_le64 (outbuf + i * 16 + 0) ^ tweak_lo;
456 tmp_hi = buf_get_le64 (outbuf + i * 16 + 8) ^ tweak_hi;
457 buf_put_le64 (outbuf + i * 16 + 0, tmp_lo);
458 buf_put_le64 (outbuf + i * 16 + 8, tmp_hi);
459 }
460
461 inbuf += curr_blks * 16;
462 outbuf += curr_blks * 16;
463 nblocks -= curr_blks;
464 }
465
466 buf_put_le64 (tweak + 0, tweak_next_lo);
467 buf_put_le64 (tweak + 8, tweak_next_hi);
468
469 *num_used_tmpblocks = tmp_used;
470 return burn_depth;
471 }
472
473 static inline unsigned int
474 bulk_ecb_crypt_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
475 const byte *inbuf, size_t nblocks, size_t fn_max_nblocks)
476 {
477 unsigned int burn_depth = 0;
478 unsigned int nburn;
479
480 while (nblocks >= 1)
481 {
482 size_t curr_blks = nblocks > fn_max_nblocks ? fn_max_nblocks : nblocks;
483 nburn = crypt_fn (priv, outbuf, inbuf, curr_blks);
484 burn_depth = nburn > burn_depth ? nburn : burn_depth;
485 inbuf += curr_blks * 16;
486 outbuf += curr_blks * 16;
487 nblocks -= curr_blks;
488 }
489
490 return burn_depth;
491 }
492
493 #endif /*GCRYPT_BULKHELP_H*/