summaryrefslogtreecommitdiff
path: root/cipher/bulkhelp.h
blob: 833262e2a6418c4e63db56c57ec33bd705c64c84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* bulkhelp.h  -  Some bulk processing helpers
 * Copyright (C) 2022 Jussi Kivilinna <jussi.kivilinna@iki.fi>
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
#ifndef GCRYPT_BULKHELP_H
#define GCRYPT_BULKHELP_H


#include "g10lib.h"
#include "cipher-internal.h"


#ifdef __x86_64__
/* Use u64 to store pointers for x32 support (assembly function assumes
 * 64-bit pointers). */
typedef u64 ocb_L_uintptr_t;
#else
typedef uintptr_t ocb_L_uintptr_t;
#endif

typedef unsigned int (*bulk_crypt_fn_t) (void *ctx, byte *out,
                                         const byte *in,
                                         size_t num_blks);


static inline ocb_L_uintptr_t *
bulk_ocb_prepare_L_pointers_array_blk64 (gcry_cipher_hd_t c,
                                         ocb_L_uintptr_t Ls[64], u64 blkn)
{
  unsigned int n = 64 - (blkn % 64);
  unsigned int i;

  for (i = 0; i < 64; i += 8)
    {
      Ls[(i + 0 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
      Ls[(i + 1 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
      Ls[(i + 2 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
      Ls[(i + 3 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[2];
      Ls[(i + 4 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
      Ls[(i + 5 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
      Ls[(i + 6 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
    }

  Ls[(7 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
  Ls[(15 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[4];
  Ls[(23 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
  Ls[(31 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[5];
  Ls[(39 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
  Ls[(47 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[4];
  Ls[(55 + n) % 64] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
  return &Ls[(63 + n) % 64];
}


static inline ocb_L_uintptr_t *
bulk_ocb_prepare_L_pointers_array_blk32 (gcry_cipher_hd_t c,
                                         ocb_L_uintptr_t Ls[32], u64 blkn)
{
  unsigned int n = 32 - (blkn % 32);
  unsigned int i;

  for (i = 0; i < 32; i += 8)
    {
      Ls[(i + 0 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
      Ls[(i + 1 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
      Ls[(i + 2 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
      Ls[(i + 3 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[2];
      Ls[(i + 4 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
      Ls[(i + 5 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
      Ls[(i + 6 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
    }

  Ls[(7 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
  Ls[(15 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[4];
  Ls[(23 + n) % 32] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
  return &Ls[(31 + n) % 32];
}


static inline ocb_L_uintptr_t *
bulk_ocb_prepare_L_pointers_array_blk16 (gcry_cipher_hd_t c,
                                         ocb_L_uintptr_t Ls[16], u64 blkn)
{
  unsigned int n = 16 - (blkn % 16);
  unsigned int i;

  for (i = 0; i < 16; i += 8)
    {
      Ls[(i + 0 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
      Ls[(i + 1 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
      Ls[(i + 2 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
      Ls[(i + 3 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[2];
      Ls[(i + 4 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
      Ls[(i + 5 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
      Ls[(i + 6 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
    }

  Ls[(7 + n) % 16] = (uintptr_t)(void *)c->u_mode.ocb.L[3];
  return &Ls[(15 + n) % 16];
}


static inline ocb_L_uintptr_t *
bulk_ocb_prepare_L_pointers_array_blk8 (gcry_cipher_hd_t c,
                                        ocb_L_uintptr_t Ls[8], u64 blkn)
{
  unsigned int n = 8 - (blkn % 8);

  Ls[(0 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
  Ls[(1 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
  Ls[(2 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
  Ls[(3 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[2];
  Ls[(4 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
  Ls[(5 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[1];
  Ls[(6 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[0];
  Ls[(7 + n) % 8] = (uintptr_t)(void *)c->u_mode.ocb.L[3];

  return &Ls[(7 + n) % 8];
}


static inline unsigned int
bulk_ctr_enc_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
                  const byte *inbuf, size_t nblocks, byte *ctr,
                  byte *tmpbuf, size_t tmpbuf_nblocks,
                  unsigned int *num_used_tmpblocks)
{
  unsigned int tmp_used = 16;
  unsigned int burn_depth = 0;
  unsigned int nburn;

  while (nblocks >= 1)
    {
      size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
      size_t i;

      if (curr_blks * 16 > tmp_used)
        tmp_used = curr_blks * 16;

      cipher_block_cpy (tmpbuf + 0 * 16, ctr, 16);
      for (i = 1; i < curr_blks; i++)
        {
          cipher_block_cpy (&tmpbuf[i * 16], ctr, 16);
          cipher_block_add (&tmpbuf[i * 16], i, 16);
        }
      cipher_block_add (ctr, curr_blks, 16);

      nburn = crypt_fn (priv, tmpbuf, tmpbuf, curr_blks);
      burn_depth = nburn > burn_depth ? nburn : burn_depth;

      for (i = 0; i < curr_blks; i++)
        {
          cipher_block_xor (outbuf, &tmpbuf[i * 16], inbuf, 16);
          outbuf += 16;
          inbuf += 16;
        }

      nblocks -= curr_blks;
    }

  *num_used_tmpblocks = tmp_used;
  return burn_depth;
}


static inline unsigned int
bulk_ctr32le_enc_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
                      const byte *inbuf, size_t nblocks, byte *ctr,
                      byte *tmpbuf, size_t tmpbuf_nblocks,
                      unsigned int *num_used_tmpblocks)
{
  unsigned int tmp_used = 16;
  unsigned int burn_depth = 0;
  unsigned int nburn;

  while (nblocks >= 1)
    {
      size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
      u64 ctr_lo = buf_get_le64(ctr + 0 * 8);
      u64 ctr_hi = buf_get_he64(ctr + 1 * 8);
      size_t i;

      if (curr_blks * 16 > tmp_used)
        tmp_used = curr_blks * 16;

      cipher_block_cpy (tmpbuf + 0 * 16, ctr, 16);
      for (i = 1; i < curr_blks; i++)
        {
          u32 lo_u32 = (u32)ctr_lo + i;
          u64 lo_u64 = ctr_lo & ~(u64)(u32)-1;
          lo_u64 += lo_u32;
          buf_put_le64(&tmpbuf[0 * 8 + i * 16], lo_u64);
          buf_put_he64(&tmpbuf[1 * 8 + i * 16], ctr_hi);
        }
      buf_put_le32(ctr, (u32)ctr_lo + curr_blks);

      nburn = crypt_fn (priv, tmpbuf, tmpbuf, curr_blks);
      burn_depth = nburn > burn_depth ? nburn : burn_depth;

      for (i = 0; i < curr_blks; i++)
        {
          cipher_block_xor (outbuf, &tmpbuf[i * 16], inbuf, 16);
          outbuf += 16;
          inbuf += 16;
        }

      nblocks -= curr_blks;
    }

  *num_used_tmpblocks = tmp_used;
  return burn_depth;
}


static inline unsigned int
bulk_cbc_dec_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
                  const byte *inbuf, size_t nblocks, byte *iv,
                  byte *tmpbuf, size_t tmpbuf_nblocks,
                  unsigned int *num_used_tmpblocks)
{
  unsigned int tmp_used = 16;
  unsigned int burn_depth = 0;
  unsigned int nburn;

  while (nblocks >= 1)
    {
      size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
      size_t i;

      if (curr_blks * 16 > tmp_used)
        tmp_used = curr_blks * 16;

      nburn = crypt_fn (priv, tmpbuf, inbuf, curr_blks);
      burn_depth = nburn > burn_depth ? nburn : burn_depth;

      for (i = 0; i < curr_blks; i++)
        {
          cipher_block_xor_n_copy_2(outbuf, &tmpbuf[i * 16], iv, inbuf, 16);
          outbuf += 16;
          inbuf += 16;
        }

      nblocks -= curr_blks;
    }

  *num_used_tmpblocks = tmp_used;
  return burn_depth;
}


static inline unsigned int
bulk_cfb_dec_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
                  const byte *inbuf, size_t nblocks, byte *iv,
                  byte *tmpbuf, size_t tmpbuf_nblocks,
                  unsigned int *num_used_tmpblocks)
{
  unsigned int tmp_used = 16;
  unsigned int burn_depth = 0;
  unsigned int nburn;

  while (nblocks >= 1)
    {
      size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
      size_t i;

      if (curr_blks * 16 > tmp_used)
        tmp_used = curr_blks * 16;

      cipher_block_cpy (&tmpbuf[0 * 16], iv, 16);
      if (curr_blks > 1)
        memcpy (&tmpbuf[1 * 16], &inbuf[(1 - 1) * 16], 16 * curr_blks - 16);
      cipher_block_cpy (iv, &inbuf[(curr_blks - 1) * 16], 16);

      nburn = crypt_fn (priv, tmpbuf, tmpbuf, curr_blks);
      burn_depth = nburn > burn_depth ? nburn : burn_depth;

      for (i = 0; i < curr_blks; i++)
        {
          cipher_block_xor (outbuf, inbuf, &tmpbuf[i * 16], 16);
          outbuf += 16;
          inbuf += 16;
        }

      nblocks -= curr_blks;
    }

  *num_used_tmpblocks = tmp_used;
  return burn_depth;
}


static inline unsigned int
bulk_ocb_crypt_128 (gcry_cipher_hd_t c, void *priv, bulk_crypt_fn_t crypt_fn,
                    byte *outbuf, const byte *inbuf, size_t nblocks, u64 *blkn,
                    int encrypt, byte *tmpbuf, size_t tmpbuf_nblocks,
                    unsigned int *num_used_tmpblocks)
{
  unsigned int tmp_used = 16;
  unsigned int burn_depth = 0;
  unsigned int nburn;

  while (nblocks >= 1)
    {
      size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
      size_t i;

      if (curr_blks * 16 > tmp_used)
        tmp_used = curr_blks * 16;

      for (i = 0; i < curr_blks; i++)
        {
          const unsigned char *l = ocb_get_l(c, ++*blkn);

          /* Checksum_i = Checksum_{i-1} xor P_i  */
          if (encrypt)
            cipher_block_xor_1(c->u_ctr.ctr, &inbuf[i * 16], 16);

          /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
          cipher_block_xor_2dst (&tmpbuf[i * 16], c->u_iv.iv, l, 16);
          cipher_block_xor (&outbuf[i * 16], &inbuf[i * 16],
                            c->u_iv.iv, 16);
        }

      /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i)  */
      nburn = crypt_fn (priv, outbuf, outbuf, curr_blks);
      burn_depth = nburn > burn_depth ? nburn : burn_depth;

      for (i = 0; i < curr_blks; i++)
        {
          cipher_block_xor_1 (&outbuf[i * 16], &tmpbuf[i * 16], 16);

          /* Checksum_i = Checksum_{i-1} xor P_i  */
          if (!encrypt)
              cipher_block_xor_1(c->u_ctr.ctr, &outbuf[i * 16], 16);
        }

      outbuf += curr_blks * 16;
      inbuf  += curr_blks * 16;
      nblocks -= curr_blks;
    }

  *num_used_tmpblocks = tmp_used;
  return burn_depth;
}


static inline unsigned int
bulk_ocb_auth_128 (gcry_cipher_hd_t c, void *priv, bulk_crypt_fn_t crypt_fn,
                   const byte *abuf, size_t nblocks, u64 *blkn, byte *tmpbuf,
                   size_t tmpbuf_nblocks, unsigned int *num_used_tmpblocks)
{
  unsigned int tmp_used = 16;
  unsigned int burn_depth = 0;
  unsigned int nburn;

  while (nblocks >= 1)
    {
      size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
      size_t i;

      if (curr_blks * 16 > tmp_used)
        tmp_used = curr_blks * 16;

      for (i = 0; i < curr_blks; i++)
        {
          const unsigned char *l = ocb_get_l(c, ++*blkn);

          /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
          cipher_block_xor_2dst (&tmpbuf[i * 16],
                                  c->u_mode.ocb.aad_offset, l, 16);
          cipher_block_xor_1 (&tmpbuf[i * 16], &abuf[i * 16], 16);
        }

      /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i)  */
      nburn = crypt_fn (priv, tmpbuf, tmpbuf, curr_blks);
      burn_depth = nburn > burn_depth ? nburn : burn_depth;

      for (i = 0; i < curr_blks; i++)
        {
          cipher_block_xor_1 (c->u_mode.ocb.aad_sum, &tmpbuf[i * 16], 16);
        }

      abuf += curr_blks * 16;
      nblocks -= curr_blks;
    }

  *num_used_tmpblocks = tmp_used;
  return burn_depth;
}


static inline unsigned int
bulk_xts_crypt_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
                    const byte *inbuf, size_t nblocks, byte *tweak,
                    byte *tmpbuf, size_t tmpbuf_nblocks,
                    unsigned int *num_used_tmpblocks)
{
  u64 tweak_lo, tweak_hi, tweak_next_lo, tweak_next_hi, tmp_lo, tmp_hi, carry;
  unsigned int tmp_used = 16;
  unsigned int burn_depth = 0;
  unsigned int nburn;

  tweak_next_lo = buf_get_le64 (tweak + 0);
  tweak_next_hi = buf_get_le64 (tweak + 8);

  while (nblocks >= 1)
    {
      size_t curr_blks = nblocks > tmpbuf_nblocks ? tmpbuf_nblocks : nblocks;
      size_t i;

      if (curr_blks * 16 > tmp_used)
        tmp_used = curr_blks * 16;

      for (i = 0; i < curr_blks; i++)
        {
          tweak_lo = tweak_next_lo;
          tweak_hi = tweak_next_hi;

          /* Generate next tweak. */
          carry = -(tweak_next_hi >> 63) & 0x87;
          tweak_next_hi = (tweak_next_hi << 1) + (tweak_next_lo >> 63);
          tweak_next_lo = (tweak_next_lo << 1) ^ carry;

          /* Xor-Encrypt/Decrypt-Xor block. */
          tmp_lo = buf_get_le64 (inbuf + i * 16 + 0) ^ tweak_lo;
          tmp_hi = buf_get_le64 (inbuf + i * 16 + 8) ^ tweak_hi;
          buf_put_he64 (&tmpbuf[i * 16 + 0], tweak_lo);
          buf_put_he64 (&tmpbuf[i * 16 + 8], tweak_hi);
          buf_put_le64 (outbuf + i * 16 + 0, tmp_lo);
          buf_put_le64 (outbuf + i * 16 + 8, tmp_hi);
        }

      nburn = crypt_fn (priv, outbuf, outbuf, curr_blks);
      burn_depth = nburn > burn_depth ? nburn : burn_depth;

      for (i = 0; i < curr_blks; i++)
        {
          /* Xor-Encrypt/Decrypt-Xor block. */
          tweak_lo = buf_get_he64 (&tmpbuf[i * 16 + 0]);
          tweak_hi = buf_get_he64 (&tmpbuf[i * 16 + 8]);
          tmp_lo = buf_get_le64 (outbuf + i * 16 + 0) ^ tweak_lo;
          tmp_hi = buf_get_le64 (outbuf + i * 16 + 8) ^ tweak_hi;
          buf_put_le64 (outbuf + i * 16 + 0, tmp_lo);
          buf_put_le64 (outbuf + i * 16 + 8, tmp_hi);
        }

      inbuf += curr_blks * 16;
      outbuf += curr_blks * 16;
      nblocks -= curr_blks;
    }

  buf_put_le64 (tweak + 0, tweak_next_lo);
  buf_put_le64 (tweak + 8, tweak_next_hi);

  *num_used_tmpblocks = tmp_used;
  return burn_depth;
}

static inline unsigned int
bulk_ecb_crypt_128 (void *priv, bulk_crypt_fn_t crypt_fn, byte *outbuf,
		    const byte *inbuf, size_t nblocks, size_t fn_max_nblocks)
{
  unsigned int burn_depth = 0;
  unsigned int nburn;

  while (nblocks >= 1)
    {
      size_t curr_blks = nblocks > fn_max_nblocks ? fn_max_nblocks : nblocks;
      nburn = crypt_fn (priv, outbuf, inbuf, curr_blks);
      burn_depth = nburn > burn_depth ? nburn : burn_depth;
      inbuf += curr_blks * 16;
      outbuf += curr_blks * 16;
      nblocks -= curr_blks;
    }

  return burn_depth;
}

#endif /*GCRYPT_BULKHELP_H*/