Coverage Report

Created: 2022-12-08 06:09

/src/gnupg/g10/cipher-aead.c
Line
Count
Source (jump to first uncovered line)
1
/* cipher-aead.c - Enciphering filter for AEAD modes
2
 * Copyright (C) 2018 Werner koch
3
 *
4
 * This file is part of GnuPG.
5
 *
6
 * GnuPG is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GnuPG 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 General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
18
 * SPDX-License-Identifier: GPL-3.0+
19
 */
20
21
#include <config.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <errno.h>
26
27
#include "gpg.h"
28
#include "../common/status.h"
29
#include "../common/iobuf.h"
30
#include "../common/util.h"
31
#include "filter.h"
32
#include "packet.h"
33
#include "options.h"
34
#include "main.h"
35
36
37
/* The size of the buffer we allocate to encrypt the data.  This must
38
 * be a multiple of the OCB blocksize (16 byte).  */
39
0
#define AEAD_ENC_BUFFER_SIZE (64*1024)
40
41
42
/* Wrapper around iobuf_write to make sure that a proper error code is
43
 * always returned.  */
44
static gpg_error_t
45
my_iobuf_write (iobuf_t a, const void *buffer, size_t buflen)
46
0
{
47
0
  if (iobuf_write (a, buffer, buflen))
48
0
    {
49
0
      gpg_error_t err = iobuf_error (a);
50
0
      if (!err || !gpg_err_code (err)) /* (The latter should never happen) */
51
0
        err = gpg_error (GPG_ERR_EIO);
52
0
      return err;
53
0
    }
54
0
  return 0;
55
0
}
56
57
58
/* Set the nonce and the additional data for the current chunk.  If
59
 * FINAL is set the final AEAD chunk is processed.  This also reset
60
 * the encryption machinery so that the handle can be used for a new
61
 * chunk.  */
62
static gpg_error_t
63
set_nonce_and_ad (cipher_filter_context_t *cfx, int final)
64
0
{
65
0
  gpg_error_t err;
66
0
  unsigned char nonce[16];
67
0
  unsigned char ad[21];
68
0
  int i;
69
70
0
  switch (cfx->dek->use_aead)
71
0
    {
72
0
    case AEAD_ALGO_OCB:
73
0
      memcpy (nonce, cfx->startiv, 15);
74
0
      i = 7;
75
0
      break;
76
77
0
    case AEAD_ALGO_EAX:
78
0
      memcpy (nonce, cfx->startiv, 16);
79
0
      i = 8;
80
0
      break;
81
82
0
    default:
83
0
      BUG ();
84
0
    }
85
86
0
  nonce[i++] ^= cfx->chunkindex >> 56;
87
0
  nonce[i++] ^= cfx->chunkindex >> 48;
88
0
  nonce[i++] ^= cfx->chunkindex >> 40;
89
0
  nonce[i++] ^= cfx->chunkindex >> 32;
90
0
  nonce[i++] ^= cfx->chunkindex >> 24;
91
0
  nonce[i++] ^= cfx->chunkindex >> 16;
92
0
  nonce[i++] ^= cfx->chunkindex >>  8;
93
0
  nonce[i++] ^= cfx->chunkindex;
94
95
0
  if (DBG_CRYPTO)
96
0
    log_printhex (nonce, 15, "nonce:");
97
0
  err = gcry_cipher_setiv (cfx->cipher_hd, nonce, i);
98
0
  if (err)
99
0
    return err;
100
101
0
  ad[0] = (0xc0 | PKT_ENCRYPTED_AEAD);
102
0
  ad[1] = 1;
103
0
  ad[2] = cfx->dek->algo;
104
0
  ad[3] = cfx->dek->use_aead;
105
0
  ad[4] = cfx->chunkbyte;
106
0
  ad[5] = cfx->chunkindex >> 56;
107
0
  ad[6] = cfx->chunkindex >> 48;
108
0
  ad[7] = cfx->chunkindex >> 40;
109
0
  ad[8] = cfx->chunkindex >> 32;
110
0
  ad[9] = cfx->chunkindex >> 24;
111
0
  ad[10]= cfx->chunkindex >> 16;
112
0
  ad[11]= cfx->chunkindex >>  8;
113
0
  ad[12]= cfx->chunkindex;
114
0
  if (final)
115
0
    {
116
0
      ad[13] = cfx->total >> 56;
117
0
      ad[14] = cfx->total >> 48;
118
0
      ad[15] = cfx->total >> 40;
119
0
      ad[16] = cfx->total >> 32;
120
0
      ad[17] = cfx->total >> 24;
121
0
      ad[18] = cfx->total >> 16;
122
0
      ad[19] = cfx->total >>  8;
123
0
      ad[20] = cfx->total;
124
0
    }
125
0
  if (DBG_CRYPTO)
126
0
    log_printhex (ad, final? 21 : 13, "authdata:");
127
0
  return gcry_cipher_authenticate (cfx->cipher_hd, ad, final? 21 : 13);
128
0
}
129
130
131
static gpg_error_t
132
write_header (cipher_filter_context_t *cfx, iobuf_t a)
133
0
{
134
0
  gpg_error_t err;
135
0
  PACKET pkt;
136
0
  PKT_encrypted ed;
137
0
  unsigned int blocksize;
138
0
  unsigned int startivlen;
139
0
  enum gcry_cipher_modes ciphermode;
140
141
0
  log_assert (cfx->dek->use_aead);
142
143
0
  blocksize = openpgp_cipher_get_algo_blklen (cfx->dek->algo);
144
0
  if (blocksize != 16 )
145
0
    log_fatal ("unsupported blocksize %u for AEAD\n", blocksize);
146
147
0
  err = openpgp_aead_algo_info (cfx->dek->use_aead, &ciphermode, &startivlen);
148
0
  if (err)
149
0
    goto leave;
150
151
0
  log_assert (opt.chunk_size >= 6 && opt.chunk_size <= 62);
152
0
  cfx->chunkbyte = opt.chunk_size - 6;
153
0
  cfx->chunksize = (uint64_t)1 << (cfx->chunkbyte + 6);
154
0
  cfx->chunklen = 0;
155
0
  cfx->bufsize = AEAD_ENC_BUFFER_SIZE;
156
0
  cfx->buflen = 0;
157
0
  cfx->buffer = xtrymalloc (cfx->bufsize);
158
0
  if (!cfx->buffer)
159
0
    return gpg_error_from_syserror ();
160
161
0
  memset (&ed, 0, sizeof ed);
162
0
  ed.new_ctb = 1;  /* (Is anyway required for the packet type).  */
163
0
  ed.len = 0; /* fixme: cfx->datalen */
164
0
  ed.extralen    = startivlen + 16; /* (16 is the taglen) */
165
0
  ed.cipher_algo = cfx->dek->algo;
166
0
  ed.aead_algo   = cfx->dek->use_aead;
167
0
  ed.chunkbyte   = cfx->chunkbyte;
168
169
0
  init_packet (&pkt);
170
0
  pkt.pkttype = PKT_ENCRYPTED_AEAD;
171
0
  pkt.pkt.encrypted = &ed;
172
173
0
  if (DBG_FILTER)
174
0
    log_debug ("aead packet: len=%lu extralen=%d\n",
175
0
               (unsigned long)ed.len, ed.extralen);
176
177
0
  write_status_printf (STATUS_BEGIN_ENCRYPTION, "0 %d %d",
178
0
                       cfx->dek->algo, ed.aead_algo);
179
0
  print_cipher_algo_note (cfx->dek->algo);
180
181
0
  if (build_packet( a, &pkt))
182
0
    log_bug ("build_packet(ENCRYPTED_AEAD) failed\n");
183
184
0
  log_assert (sizeof cfx->startiv >= startivlen);
185
0
  gcry_randomize (cfx->startiv, startivlen, GCRY_STRONG_RANDOM);
186
0
  err = my_iobuf_write (a, cfx->startiv, startivlen);
187
0
  if (err)
188
0
    goto leave;
189
190
0
  err = openpgp_cipher_open (&cfx->cipher_hd,
191
0
                             cfx->dek->algo,
192
0
                             ciphermode,
193
0
                             GCRY_CIPHER_SECURE);
194
0
  if (err)
195
0
    goto leave;
196
197
0
  if (DBG_CRYPTO)
198
0
    log_printhex (cfx->dek->key, cfx->dek->keylen, "thekey:");
199
0
  err = gcry_cipher_setkey (cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen);
200
0
  if (err)
201
0
    return err;
202
203
0
  cfx->wrote_header = 1;
204
205
0
 leave:
206
0
  return err;
207
0
}
208
209
210
/* Get and write the auth tag to stream A.  */
211
static gpg_error_t
212
write_auth_tag (cipher_filter_context_t *cfx, iobuf_t a)
213
0
{
214
0
  gpg_error_t err;
215
0
  char tag[16];
216
217
0
  err = gcry_cipher_gettag (cfx->cipher_hd, tag, 16);
218
0
  if (err)
219
0
    goto leave;
220
0
  err = my_iobuf_write (a, tag, 16);
221
0
  if (err)
222
0
    goto leave;
223
224
0
 leave:
225
0
  if (err)
226
0
    log_error ("write_auth_tag failed: %s\n", gpg_strerror (err));
227
0
  return err;
228
0
}
229
230
231
/* Write the final chunk to stream A.  */
232
static gpg_error_t
233
write_final_chunk (cipher_filter_context_t *cfx, iobuf_t a)
234
0
{
235
0
  gpg_error_t err;
236
0
  char dummy[1];
237
238
0
  err = set_nonce_and_ad (cfx, 1);
239
0
  if (err)
240
0
    goto leave;
241
242
0
  gcry_cipher_final (cfx->cipher_hd);
243
244
  /* Encrypt an empty string.  */
245
0
  err = gcry_cipher_encrypt (cfx->cipher_hd, dummy, 0, NULL, 0);
246
0
  if (err)
247
0
    goto leave;
248
249
0
  err = write_auth_tag (cfx, a);
250
251
0
 leave:
252
0
  return err;
253
0
}
254
255
256
/* The core of the flush sub-function of cipher_filter_aead.   */
257
static gpg_error_t
258
do_flush (cipher_filter_context_t *cfx, iobuf_t a, byte *buf, size_t size)
259
0
{
260
0
  gpg_error_t err = 0;
261
0
  int finalize = 0;
262
0
  size_t n;
263
264
  /* Put the data into a buffer, flush and encrypt as needed.  */
265
0
  if (DBG_FILTER)
266
0
    log_debug ("flushing %zu bytes (cur buflen=%zu)\n", size, cfx->buflen);
267
0
  do
268
0
    {
269
0
      const unsigned fast_threshold = 512;
270
0
      const byte *src_buf = NULL;
271
0
      int enc_now = 0;
272
273
0
      if (cfx->buflen + size < cfx->bufsize)
274
0
        n = size;
275
0
      else
276
0
        n = cfx->bufsize - cfx->buflen;
277
278
0
      if (cfx->buflen % fast_threshold != 0)
279
0
  {
280
    /* Attempt to align cfx->buflen to fast threshold size first. */
281
0
    size_t nalign = fast_threshold - (cfx->buflen % fast_threshold);
282
0
    if (nalign < n)
283
0
      {
284
0
        n = nalign;
285
0
      }
286
0
  }
287
0
      else if (cfx->buflen == 0 && n >= fast_threshold)
288
0
  {
289
    /* Handle large input buffers as multiple of cipher blocksize. */
290
0
    n = (n / 16) * 16;
291
0
  }
292
293
0
      if (cfx->chunklen + cfx->buflen + n >= cfx->chunksize)
294
0
        {
295
0
          size_t n1 = cfx->chunksize - (cfx->chunklen + cfx->buflen);
296
0
          finalize = 1;
297
0
          if (DBG_FILTER)
298
0
            log_debug ("chunksize %"PRIu64" reached;"
299
0
                       " cur buflen=%zu using %zu of %zu\n",
300
0
                       cfx->chunksize, cfx->buflen,
301
0
                       n1, n);
302
0
          n = n1;
303
0
        }
304
305
0
      if (!finalize && cfx->buflen % 16 == 0 && cfx->buflen > 0
306
0
    && size >= fast_threshold)
307
0
  {
308
    /* If cfx->buffer is aligned and remaining input buffer length
309
     * is long, encrypt cfx->buffer inplace now to allow fast path
310
     * handling on next loop iteration. */
311
0
    src_buf = cfx->buffer;
312
0
    enc_now = 1;
313
0
    n = 0;
314
0
  }
315
0
      else if (cfx->buflen == 0 && n >= fast_threshold)
316
0
  {
317
    /* Fast path for large input buffer. This avoids memcpy and
318
     * instead encrypts directly from input to cfx->buffer. */
319
0
    log_assert (n % 16 == 0 || finalize);
320
0
    src_buf = buf;
321
0
    cfx->buflen = n;
322
0
    buf += n;
323
0
    size -= n;
324
0
    enc_now = 1;
325
0
  }
326
0
      else if (n > 0)
327
0
  {
328
0
    memcpy (cfx->buffer + cfx->buflen, buf, n);
329
0
    src_buf = cfx->buffer;
330
0
    cfx->buflen += n;
331
0
    buf  += n;
332
0
    size -= n;
333
0
  }
334
335
0
      if (cfx->buflen == cfx->bufsize || enc_now || finalize)
336
0
        {
337
0
          if (DBG_FILTER)
338
0
            log_debug ("encrypting: size=%zu buflen=%zu %s%s n=%zu\n",
339
0
                       size, cfx->buflen, finalize?"(finalize)":"",
340
0
           enc_now?"(now)":"", n);
341
342
0
          if (!cfx->chunklen)
343
0
            {
344
0
              if (DBG_FILTER)
345
0
                log_debug ("start encrypting a new chunk\n");
346
0
              err = set_nonce_and_ad (cfx, 0);
347
0
              if (err)
348
0
                goto leave;
349
0
            }
350
351
0
          if (finalize)
352
0
            gcry_cipher_final (cfx->cipher_hd);
353
0
          if (DBG_FILTER)
354
0
            {
355
0
              if (finalize)
356
0
                log_printhex (src_buf, cfx->buflen, "plain(1):");
357
0
              else if (cfx->buflen > 32)
358
0
                log_printhex (src_buf + cfx->buflen - 32, 32,
359
0
                              "plain(last32):");
360
0
            }
361
362
          /* Take care: even with a buflen of zero an encrypt needs to
363
           * be called after gcry_cipher_final and before
364
           * gcry_cipher_gettag - at least with libgcrypt 1.8 and OCB
365
           * mode.  */
366
0
    err = gcry_cipher_encrypt (cfx->cipher_hd, cfx->buffer,
367
0
             cfx->buflen, src_buf, cfx->buflen);
368
0
          if (err)
369
0
            goto leave;
370
0
          if (finalize && DBG_FILTER)
371
0
            log_printhex (cfx->buffer, cfx->buflen, "ciphr(1):");
372
0
          err = my_iobuf_write (a, cfx->buffer, cfx->buflen);
373
0
          if (err)
374
0
            goto leave;
375
0
          cfx->chunklen += cfx->buflen;
376
0
          cfx->total += cfx->buflen;
377
0
          cfx->buflen = 0;
378
379
0
          if (finalize)
380
0
            {
381
0
              if (DBG_FILTER)
382
0
                log_debug ("writing tag: chunklen=%ju total=%ju\n",
383
0
                           (uintmax_t)cfx->chunklen, (uintmax_t)cfx->total);
384
0
              err = write_auth_tag (cfx, a);
385
0
              if (err)
386
0
                goto leave;
387
388
0
              cfx->chunkindex++;
389
0
              cfx->chunklen = 0;
390
0
              finalize = 0;
391
0
            }
392
0
        }
393
0
    }
394
0
  while (size);
395
396
0
 leave:
397
0
  return err;
398
0
}
399
400
401
/* The core of the free sub-function of cipher_filter_aead.   */
402
static gpg_error_t
403
do_free (cipher_filter_context_t *cfx, iobuf_t a)
404
0
{
405
0
  gpg_error_t err = 0;
406
407
0
  if (DBG_FILTER)
408
0
    log_debug ("do_free: buflen=%zu\n", cfx->buflen);
409
410
0
  if (cfx->chunklen || cfx->buflen)
411
0
    {
412
0
      if (DBG_FILTER)
413
0
        log_debug ("encrypting last %zu bytes of the last chunk\n",cfx->buflen);
414
415
0
      if (!cfx->chunklen)
416
0
        {
417
0
          if (DBG_FILTER)
418
0
            log_debug ("start encrypting a new chunk\n");
419
0
          err = set_nonce_and_ad (cfx, 0);
420
0
          if (err)
421
0
            goto leave;
422
0
        }
423
424
0
      gcry_cipher_final (cfx->cipher_hd);
425
0
      err = gcry_cipher_encrypt (cfx->cipher_hd, cfx->buffer, cfx->buflen,
426
0
                                 NULL, 0);
427
0
      if (err)
428
0
        goto leave;
429
0
      err = my_iobuf_write (a, cfx->buffer, cfx->buflen);
430
0
      if (err)
431
0
        goto leave;
432
      /* log_printhex (cfx->buffer, cfx->buflen, "wrote:"); */
433
0
      cfx->chunklen += cfx->buflen;
434
0
      cfx->total += cfx->buflen;
435
436
      /* Get and write the authentication tag.  */
437
0
      if (DBG_FILTER)
438
0
        log_debug ("writing tag: chunklen=%ju total=%ju\n",
439
0
                   (uintmax_t)cfx->chunklen, (uintmax_t)cfx->total);
440
0
      err = write_auth_tag (cfx, a);
441
0
      if (err)
442
0
        goto leave;
443
0
      cfx->chunkindex++;
444
0
      cfx->chunklen = 0;
445
0
    }
446
447
  /* Write the final chunk.  */
448
0
  if (DBG_FILTER)
449
0
    log_debug ("creating final chunk\n");
450
0
  err = write_final_chunk (cfx, a);
451
452
0
 leave:
453
0
  xfree (cfx->buffer);
454
0
  cfx->buffer = NULL;
455
0
  gcry_cipher_close (cfx->cipher_hd);
456
0
  cfx->cipher_hd = NULL;
457
0
  return err;
458
0
}
459
460
461
/*
462
 * This filter is used to encrypt data with an AEAD algorithm
463
 */
464
int
465
cipher_filter_aead (void *opaque, int control,
466
                    iobuf_t a, byte *buf, size_t *ret_len)
467
0
{
468
0
  cipher_filter_context_t *cfx = opaque;
469
0
  size_t size = *ret_len;
470
0
  int rc = 0;
471
472
0
  if (control == IOBUFCTRL_UNDERFLOW) /* decrypt */
473
0
    {
474
0
      rc = -1; /* not used */
475
0
    }
476
0
  else if (control == IOBUFCTRL_FLUSH) /* encrypt */
477
0
    {
478
0
      if (!cfx->wrote_header && (rc=write_header (cfx, a)))
479
0
        ;
480
0
      else
481
0
        rc = do_flush (cfx, a, buf, size);
482
0
    }
483
0
  else if (control == IOBUFCTRL_FREE)
484
0
    {
485
0
      rc = do_free (cfx, a);
486
0
    }
487
0
  else if (control == IOBUFCTRL_DESC)
488
0
    {
489
0
      mem2str (buf, "cipher_filter_aead", *ret_len);
490
0
    }
491
492
0
  return rc;
493
0
}