Coverage Report

Created: 2026-05-30 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/g10/decrypt-data.c
Line
Count
Source
1
/* decrypt-data.c - Decrypt an encrypted data packet
2
 * Copyright (C) 1998-2001, 2005-2006, 2009 Free Software Foundation, Inc.
3
 * Copyright (C) 1998-2001, 2005-2006, 2009, 2018 Werner Koch
4
 *
5
 * This file is part of GnuPG.
6
 *
7
 * GnuPG is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * GnuPG is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19
 * SPDX-License-Identifier: GPL-3.0-or-later
20
 */
21
22
#include <config.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
27
#include "gpg.h"
28
#include "../common/util.h"
29
#include "packet.h"
30
#include "options.h"
31
#include "../common/i18n.h"
32
#include "../common/status.h"
33
#include "../common/compliance.h"
34
35
36
static int aead_decode_filter (void *opaque, int control, iobuf_t a,
37
                               byte *buf, size_t *ret_len);
38
static int mdc_decode_filter ( void *opaque, int control, IOBUF a,
39
                               byte *buf, size_t *ret_len);
40
static int decode_filter ( void *opaque, int control, IOBUF a,
41
          byte *buf, size_t *ret_len);
42
43
/* Our context object.  */
44
struct decode_filter_context_s
45
{
46
  /* Redcounter (max value is 2).  We need it because we do not know
47
   * whether the iobuf or the outer control code frees this object
48
   * first.  */
49
  int  refcount;
50
51
  /* The cipher handle.  */
52
  gcry_cipher_hd_t cipher_hd;
53
54
  /* The hash handle for use in MDC mode.  */
55
  gcry_md_hd_t mdc_hash;
56
57
  /* The start IV for AEAD encryption.   */
58
  byte startiv[16];
59
60
  /* The holdback buffer and its used length.  For AEAD we need 32+1
61
   * bytes but we use 48 byte.  For MDC we need 22 bytes; here
62
   * holdbacklen will either 0 or 22.  */
63
  char holdback[48];
64
  unsigned int holdbacklen;
65
66
  /* Working on a partial length packet.  */
67
  unsigned int partial : 1;
68
69
  /* EOF indicator with these true values:
70
   *   1 = normal EOF
71
   *   2 = premature EOF (tag or hash incomplete)
72
   *   3 = premature EOF (general)       */
73
  unsigned int eof_seen : 2;
74
75
  /* Flag to convey an error from aead_checktag.  */
76
  unsigned int checktag_failed : 1;
77
78
  /* The actually used cipher algo for AEAD.  */
79
  byte cipher_algo;
80
81
  /* The AEAD algo.  */
82
  byte aead_algo;
83
84
  /* The encoded chunk byte for AEAD.  */
85
  byte chunkbyte;
86
87
  /* The decoded CHUNKBYTE.  */
88
  uint64_t chunksize;
89
90
  /* The chunk index for AEAD.  */
91
  uint64_t chunkindex;
92
93
  /* The number of bytes in the current chunk.  */
94
  uint64_t chunklen;
95
96
  /* The total count of decrypted plaintext octets.  */
97
  uint64_t total;
98
99
  /* Remaining bytes in the packet according to the packet header.
100
   * Not used if PARTIAL is true.  */
101
  size_t length;
102
};
103
typedef struct decode_filter_context_s *decode_filter_ctx_t;
104
105
106
/* Helper to release the decode context.  */
107
static void
108
release_dfx_context (decode_filter_ctx_t dfx)
109
0
{
110
0
  if (!dfx)
111
0
    return;
112
113
0
  log_assert (dfx->refcount);
114
0
  if ( !--dfx->refcount )
115
0
    {
116
0
      gcry_cipher_close (dfx->cipher_hd);
117
0
      dfx->cipher_hd = NULL;
118
0
      gcry_md_close (dfx->mdc_hash);
119
0
      dfx->mdc_hash = NULL;
120
0
      xfree (dfx);
121
0
    }
122
0
}
123
124
125
/* Set the nonce and the additional data for the current chunk.  This
126
 * also reset the decryption machinery so that the handle can be
127
 * used for a new chunk.  If FINAL is set the final AEAD chunk is
128
 * processed.  */
129
static gpg_error_t
130
aead_set_nonce_and_ad (decode_filter_ctx_t dfx, int final)
131
0
{
132
0
  gpg_error_t err;
133
0
  unsigned char ad[21];
134
0
  unsigned char nonce[16];
135
0
  int i;
136
137
0
  switch (dfx->aead_algo)
138
0
    {
139
0
    case AEAD_ALGO_OCB:
140
0
      memcpy (nonce, dfx->startiv, 15);
141
0
      i = 7;
142
0
      break;
143
144
0
    case AEAD_ALGO_EAX:
145
0
      memcpy (nonce, dfx->startiv, 16);
146
0
      i = 8;
147
0
      break;
148
149
0
    default:
150
0
      BUG ();
151
0
    }
152
0
  nonce[i++] ^= dfx->chunkindex >> 56;
153
0
  nonce[i++] ^= dfx->chunkindex >> 48;
154
0
  nonce[i++] ^= dfx->chunkindex >> 40;
155
0
  nonce[i++] ^= dfx->chunkindex >> 32;
156
0
  nonce[i++] ^= dfx->chunkindex >> 24;
157
0
  nonce[i++] ^= dfx->chunkindex >> 16;
158
0
  nonce[i++] ^= dfx->chunkindex >>  8;
159
0
  nonce[i++] ^= dfx->chunkindex;
160
161
0
  if (DBG_CRYPTO)
162
0
    log_printhex (nonce, i, "nonce:");
163
0
  err = gcry_cipher_setiv (dfx->cipher_hd, nonce, i);
164
0
  if (err)
165
0
    return err;
166
167
0
  ad[0] = (0xc0 | PKT_ENCRYPTED_AEAD);
168
0
  ad[1] = 1;
169
0
  ad[2] = dfx->cipher_algo;
170
0
  ad[3] = dfx->aead_algo;
171
0
  ad[4] = dfx->chunkbyte;
172
0
  ad[5] = dfx->chunkindex >> 56;
173
0
  ad[6] = dfx->chunkindex >> 48;
174
0
  ad[7] = dfx->chunkindex >> 40;
175
0
  ad[8] = dfx->chunkindex >> 32;
176
0
  ad[9] = dfx->chunkindex >> 24;
177
0
  ad[10]= dfx->chunkindex >> 16;
178
0
  ad[11]= dfx->chunkindex >>  8;
179
0
  ad[12]= dfx->chunkindex;
180
0
  if (final)
181
0
    {
182
0
      ad[13] = dfx->total >> 56;
183
0
      ad[14] = dfx->total >> 48;
184
0
      ad[15] = dfx->total >> 40;
185
0
      ad[16] = dfx->total >> 32;
186
0
      ad[17] = dfx->total >> 24;
187
0
      ad[18] = dfx->total >> 16;
188
0
      ad[19] = dfx->total >>  8;
189
0
      ad[20] = dfx->total;
190
0
    }
191
0
  if (DBG_CRYPTO)
192
0
    log_printhex (ad, final? 21 : 13, "authdata:");
193
0
  return gcry_cipher_authenticate (dfx->cipher_hd, ad, final? 21 : 13);
194
0
}
195
196
197
/* Helper to check the 16 byte tag in TAGBUF.  The FINAL flag is only
198
 * for debug messages.  */
199
static gpg_error_t
200
aead_checktag (decode_filter_ctx_t dfx, int final, const void *tagbuf)
201
0
{
202
0
  gpg_error_t err;
203
204
0
  if (DBG_FILTER)
205
0
    log_printhex (tagbuf, 16, "tag:");
206
0
  err = gcry_cipher_checktag (dfx->cipher_hd, tagbuf, 16);
207
0
  if (err)
208
0
    {
209
0
      log_error ("gcry_cipher_checktag%s failed: %s\n",
210
0
                 final? " (final)":"", gpg_strerror (err));
211
0
      write_status_error ("aead_checktag", err);
212
0
      dfx->checktag_failed = 1;
213
0
      return err;
214
0
    }
215
0
  if (DBG_FILTER)
216
0
    log_debug ("%stag is valid\n", final?"final ":"");
217
0
  return 0;
218
0
}
219
220
221
/****************
222
 * Decrypt the data, specified by ED with the key DEK.  On return
223
 * COMPLIANCE_ERROR is set to true iff the decryption can claim that
224
 * it was compliant in the current mode; otherwise this flag is set to
225
 * false.
226
 */
227
int
228
decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek,
229
              int *compliance_error)
230
0
{
231
0
  decode_filter_ctx_t dfx;
232
0
  enum gcry_cipher_modes ciphermode;
233
0
  unsigned int startivlen;
234
0
  byte *p;
235
0
  int rc=0, c, i;
236
0
  byte temp[32];
237
0
  unsigned int blocksize;
238
0
  unsigned int nprefix;
239
240
0
  *compliance_error = 0;
241
242
0
  dfx = xtrycalloc (1, sizeof *dfx);
243
0
  if (!dfx)
244
0
    return gpg_error_from_syserror ();
245
0
  dfx->refcount = 1;
246
247
0
  if ( opt.verbose && !dek->algo_info_printed )
248
0
    {
249
0
      if (!openpgp_cipher_test_algo (dek->algo))
250
0
        log_info (_("%s encrypted data\n"),
251
0
                  openpgp_cipher_algo_mode_name (dek->algo, ed->aead_algo));
252
0
      else
253
0
        log_info (_("encrypted with unknown algorithm %d\n"), dek->algo );
254
0
      dek->algo_info_printed = 1;
255
0
    }
256
257
0
  if (ed->aead_algo)
258
0
    {
259
0
      rc = openpgp_aead_algo_info (ed->aead_algo, &ciphermode, &startivlen);
260
0
      if (rc)
261
0
        goto leave;
262
0
      log_assert (startivlen <= sizeof dfx->startiv);
263
0
    }
264
0
  else
265
0
    ciphermode = GCRY_CIPHER_MODE_CFB;
266
267
  /* Check compliance.  */
268
0
  if (!gnupg_cipher_is_allowed (opt.compliance, 0, dek->algo, ciphermode))
269
0
    {
270
0
      gpgrt_log (opt.show_only_session_key? GPGRT_LOGLVL_INFO
271
0
                 /*                     */: GPGRT_LOGLVL_ERROR,
272
0
                 _("cipher algorithm '%s' may not be used in %s mode\n"),
273
0
     openpgp_cipher_algo_mode_name (dek->algo,ed->aead_algo),
274
0
     gnupg_compliance_option_string (opt.compliance));
275
0
      *compliance_error = 1;
276
0
      if (opt.flags.require_compliance && !opt.show_only_session_key)
277
0
        {
278
          /* We fail early in this case because it does not make sense
279
           * to first decrypt everything.  */
280
0
          rc = gpg_error (GPG_ERR_CIPHER_ALGO);
281
0
          goto leave;
282
0
        }
283
0
    }
284
285
0
  write_status_printf (STATUS_DECRYPTION_INFO, "%d %d %d %d",
286
0
                       ed->mdc_method, dek->algo, ed->aead_algo,
287
0
                       *compliance_error);
288
289
0
  if (opt.show_session_key)
290
0
    {
291
0
      char numbuf[30];
292
0
      char *hexbuf;
293
294
0
      if (ed->aead_algo)
295
0
        snprintf (numbuf, sizeof numbuf, "%d.%u:", dek->algo, ed->aead_algo);
296
0
      else
297
0
        snprintf (numbuf, sizeof numbuf, "%d:", dek->algo);
298
0
      hexbuf = bin2hex (dek->key, dek->keylen, NULL);
299
0
      if (!hexbuf)
300
0
        {
301
0
          rc = gpg_error_from_syserror ();
302
0
          goto leave;
303
0
        }
304
0
      log_info ("session key: '%s%s'\n", numbuf, hexbuf);
305
0
      write_status_strings (STATUS_SESSION_KEY, numbuf, hexbuf, NULL);
306
0
      xfree (hexbuf);
307
0
    }
308
309
0
  if (opt.show_session_hash)
310
0
    {
311
0
      char hashbuf[32];  /* For SHA-256.  */
312
0
      char *rad64buf;
313
314
0
      gcry_md_hash_buffer (GCRY_MD_SHA256, hashbuf, dek->key, dek->keylen);
315
0
      rad64buf = make_radix64_string (hashbuf, sizeof hashbuf);
316
0
      write_status_text (STATUS_SESSION_HASH, rad64buf);
317
0
      xfree (rad64buf);
318
0
    }
319
320
0
  if (opt.show_only_session_key)
321
0
    {
322
0
      rc = 0;
323
0
      goto leave;
324
0
    }
325
326
0
  rc = openpgp_cipher_test_algo (dek->algo);
327
0
  if (rc)
328
0
    goto leave;
329
0
  blocksize = openpgp_cipher_get_algo_blklen (dek->algo);
330
0
  if ( !blocksize || blocksize > 16 )
331
0
    log_fatal ("unsupported blocksize %u\n", blocksize );
332
333
0
  if (ed->aead_algo)
334
0
    {
335
0
      if (blocksize != 16)
336
0
        {
337
0
          rc = gpg_error (GPG_ERR_CIPHER_ALGO);
338
0
          goto leave;
339
0
        }
340
341
0
      if (ed->chunkbyte > 56)
342
0
        {
343
0
          log_error ("invalid AEAD chunkbyte %u\n", ed->chunkbyte);
344
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
345
0
          goto leave;
346
0
        }
347
348
      /* Read the Start-IV. */
349
0
      if (ed->len)
350
0
        {
351
0
          for (i=0; i < startivlen && ed->len; i++, ed->len--)
352
0
            {
353
0
              if ((c=iobuf_get (ed->buf)) == -1)
354
0
                break;
355
0
              dfx->startiv[i] = c;
356
0
            }
357
0
        }
358
0
      else
359
0
        {
360
0
          for (i=0; i < startivlen; i++ )
361
0
            if ( (c=iobuf_get (ed->buf)) == -1 )
362
0
              break;
363
0
            else
364
0
              dfx->startiv[i] = c;
365
0
        }
366
0
      if (i != startivlen)
367
0
        {
368
0
          log_error ("Start-IV in AEAD packet too short (%d/%u)\n",
369
0
                     i, startivlen);
370
0
          rc = gpg_error (GPG_ERR_TOO_SHORT);
371
0
          goto leave;
372
0
        }
373
374
0
      dfx->cipher_algo = ed->cipher_algo;
375
0
      dfx->aead_algo = ed->aead_algo;
376
0
      dfx->chunkbyte = ed->chunkbyte;
377
0
      dfx->chunksize = (uint64_t)1 << (dfx->chunkbyte + 6);
378
379
0
      if (dek->algo != dfx->cipher_algo)
380
0
        log_info ("Note: different cipher algorithms used (%s/%s)\n",
381
0
                  openpgp_cipher_algo_name (dek->algo),
382
0
                  openpgp_cipher_algo_name (dfx->cipher_algo));
383
384
0
      rc = openpgp_cipher_open (&dfx->cipher_hd,
385
0
                                dfx->cipher_algo,
386
0
                                ciphermode,
387
0
                                GCRY_CIPHER_SECURE);
388
0
      if (rc)
389
0
        goto leave; /* Should never happen.  */
390
391
0
      if (DBG_CRYPTO)
392
0
        log_printhex (dek->key, dek->keylen, "thekey:");
393
0
      rc = gcry_cipher_setkey (dfx->cipher_hd, dek->key, dek->keylen);
394
0
      if (gpg_err_code (rc) == GPG_ERR_WEAK_KEY)
395
0
        {
396
0
          log_info (_("WARNING: message was encrypted with"
397
0
                      " a weak key in the symmetric cipher.\n"));
398
0
          rc = 0;
399
0
        }
400
0
      else if (rc)
401
0
        {
402
0
          log_error("key setup failed: %s\n", gpg_strerror (rc));
403
0
          goto leave;
404
0
        }
405
406
0
      if (!ed->buf)
407
0
        {
408
0
          log_error(_("problem handling encrypted packet\n"));
409
0
          goto leave;
410
0
        }
411
412
0
    }
413
0
  else /* CFB encryption.  */
414
0
    {
415
0
      nprefix = blocksize;
416
0
      if ( ed->len && ed->len < (nprefix+2) )
417
0
        {
418
          /* An invalid message.  We can't check that during parsing
419
           * because we may not know the used cipher then.  */
420
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
421
0
          goto leave;
422
0
        }
423
424
0
      if ( ed->mdc_method )
425
0
        {
426
0
          if (gcry_md_open (&dfx->mdc_hash, ed->mdc_method, 0 ))
427
0
            BUG ();
428
0
          if ( DBG_HASHING )
429
0
            gcry_md_debug (dfx->mdc_hash, "checkmdc");
430
0
        }
431
432
0
      rc = openpgp_cipher_open (&dfx->cipher_hd, dek->algo,
433
0
                                GCRY_CIPHER_MODE_CFB,
434
0
                                (GCRY_CIPHER_SECURE
435
0
                                 | ((ed->mdc_method || dek->algo >= 100)?
436
0
                                    0 : GCRY_CIPHER_ENABLE_SYNC)));
437
0
      if (rc)
438
0
        {
439
          /* We should never get an error here cause we already checked
440
           * that the algorithm is available.  */
441
0
          BUG();
442
0
        }
443
444
445
      /* log_hexdump( "thekey", dek->key, dek->keylen );*/
446
0
      rc = gcry_cipher_setkey (dfx->cipher_hd, dek->key, dek->keylen);
447
0
      if ( gpg_err_code (rc) == GPG_ERR_WEAK_KEY )
448
0
        {
449
0
          log_info (_("WARNING: message was encrypted with"
450
0
                      " a weak key in the symmetric cipher.\n"));
451
0
          rc=0;
452
0
        }
453
0
      else if (rc)
454
0
        {
455
0
          log_error ("key setup failed: %s\n", gpg_strerror (rc) );
456
0
          goto leave;
457
0
        }
458
459
0
      if (!ed->buf)
460
0
        {
461
0
          log_error (_("problem handling encrypted packet\n"));
462
0
          rc = gpg_error (GPG_ERR_INV_PACKET);
463
0
          goto leave;
464
0
        }
465
466
0
      gcry_cipher_setiv (dfx->cipher_hd, NULL, 0);
467
468
0
      if ( ed->len )
469
0
        {
470
0
          for (i=0; i < (nprefix+2) && ed->len; i++, ed->len-- )
471
0
            {
472
0
              if ( (c=iobuf_get(ed->buf)) == -1 )
473
0
                break;
474
0
              else
475
0
                temp[i] = c;
476
0
            }
477
0
        }
478
0
      else
479
0
        {
480
0
          for (i=0; i < (nprefix+2); i++ )
481
0
            if ( (c=iobuf_get(ed->buf)) == -1 )
482
0
              break;
483
0
            else
484
0
              temp[i] = c;
485
0
        }
486
487
0
      gcry_cipher_decrypt (dfx->cipher_hd, temp, nprefix+2, NULL, 0);
488
0
      gcry_cipher_sync (dfx->cipher_hd);
489
0
      p = temp;
490
      /* log_hexdump( "prefix", temp, nprefix+2 ); */
491
0
      if (dek->symmetric
492
0
          && (p[nprefix-2] != p[nprefix] || p[nprefix-1] != p[nprefix+1]) )
493
0
        {
494
0
          rc = gpg_error (GPG_ERR_BAD_KEY);
495
0
          goto leave;
496
0
        }
497
498
0
      if ( dfx->mdc_hash )
499
0
        gcry_md_write (dfx->mdc_hash, temp, nprefix+2);
500
0
    }
501
502
0
  dfx->refcount++;
503
0
  dfx->partial = !!ed->is_partial;
504
0
  dfx->length = ed->len;
505
0
  dfx->checktag_failed = 0;
506
0
  if (ed->aead_algo)
507
0
    iobuf_push_filter ( ed->buf, aead_decode_filter, dfx );
508
0
  else if (ed->mdc_method)
509
0
    iobuf_push_filter ( ed->buf, mdc_decode_filter, dfx );
510
0
  else
511
0
    iobuf_push_filter ( ed->buf, decode_filter, dfx );
512
513
0
  if (opt.unwrap_encryption)
514
0
    {
515
0
      char *filename = NULL;
516
0
      estream_t fp;
517
518
0
      rc = get_output_file ("", 0, ed->buf, &filename, &fp);
519
0
      if (! rc)
520
0
        {
521
0
          iobuf_t output = iobuf_esopen (fp, "w", 0, 0);
522
0
          armor_filter_context_t *afx = NULL;
523
524
0
    es_setbuf (fp, NULL);
525
526
0
          if (opt.armor)
527
0
            {
528
0
              afx = new_armor_context ();
529
0
              push_armor_filter (afx, output);
530
0
            }
531
532
0
          iobuf_copy (output, ed->buf);
533
0
          if ((rc = iobuf_error (ed->buf)))
534
0
            log_error (_("error reading '%s': %s\n"),
535
0
                       filename, gpg_strerror (rc));
536
0
          else if ((rc = iobuf_error (output)))
537
0
            log_error (_("error writing '%s': %s\n"),
538
0
                       filename, gpg_strerror (rc));
539
540
0
          iobuf_close (output);
541
0
          release_armor_context (afx);
542
0
        }
543
0
      xfree (filename);
544
0
    }
545
0
  else
546
0
    proc_packets (ctrl, procctx, ed->buf );
547
548
0
  ed->buf = NULL;
549
0
  if (dfx->eof_seen > 1 )
550
0
    rc = gpg_error (GPG_ERR_INV_PACKET);
551
0
  else if (dfx->checktag_failed)
552
0
    {
553
0
      rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
554
0
    }
555
0
  else if ( ed->mdc_method )
556
0
    {
557
      /* We used to let parse-packet.c handle the MDC packet but this
558
         turned out to be a problem with compressed packets: With old
559
         style packets there is no length information available and
560
         the decompressor uses an implicit end.  However we can't know
561
         this implicit end beforehand (:-) and thus may feed the
562
         decompressor with more bytes than actually needed.  It would
563
         be possible to unread the extra bytes but due to our weird
564
         iobuf system any unread is non reliable due to filters
565
         already popped off.  The easy and sane solution is to care
566
         about the MDC packet only here and never pass it to the
567
         packet parser.  Fortunatley the OpenPGP spec requires a
568
         strict format for the MDC packet so that we know that 22
569
         bytes are appended.  */
570
0
      int datalen = gcry_md_get_algo_dlen (ed->mdc_method);
571
572
0
      log_assert (dfx->cipher_hd);
573
0
      log_assert (dfx->mdc_hash);
574
0
      gcry_cipher_decrypt (dfx->cipher_hd, dfx->holdback, 22, NULL, 0);
575
0
      gcry_md_write (dfx->mdc_hash, dfx->holdback, 2);
576
0
      gcry_md_final (dfx->mdc_hash);
577
578
0
      if (   dfx->holdback[0] != '\xd3'
579
0
          || dfx->holdback[1] != '\x14'
580
0
          || datalen != 20
581
0
          || memcmp (gcry_md_read (dfx->mdc_hash, 0), dfx->holdback+2, datalen))
582
0
        rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
583
      /* log_printhex("MDC message:", dfx->holdback, 22); */
584
      /* log_printhex("MDC calc:", gcry_md_read (dfx->mdc_hash,0), datalen); */
585
0
    }
586
587
0
 leave:
588
0
  release_dfx_context (dfx);
589
0
  return rc;
590
0
}
591
592
593
/* Fill BUFFER with up to NBYTES-OFFSET from STREAM utilizing
594
 * information from the context DFX.  Returns the new offset which is
595
 * the number of bytes read plus the original offset.  On EOF the
596
 * respective flag in DFX is set. */
597
static size_t
598
fill_buffer (decode_filter_ctx_t dfx, iobuf_t stream,
599
             byte *buffer, size_t nbytes, size_t offset)
600
0
{
601
0
  size_t nread = offset;
602
0
  size_t curr;
603
0
  int ret;
604
605
0
  if (dfx->partial)
606
0
    {
607
0
      while (nread < nbytes)
608
0
        {
609
0
          curr = nbytes - nread;
610
611
0
          ret = iobuf_read (stream, &buffer[nread], curr);
612
0
          if (ret == -1)
613
0
            {
614
0
              dfx->eof_seen = 1; /* Normal EOF. */
615
0
              break;
616
0
            }
617
618
0
          nread += ret;
619
0
        }
620
0
    }
621
0
  else
622
0
    {
623
0
      while (nread < nbytes && dfx->length)
624
0
        {
625
0
          curr = nbytes - nread;
626
0
          if (curr > dfx->length)
627
0
            curr = dfx->length;
628
629
0
          ret = iobuf_read (stream, &buffer[nread], curr);
630
0
          if (ret == -1)
631
0
            {
632
0
              dfx->eof_seen = 3; /* Premature EOF. */
633
0
              break;
634
0
            }
635
636
0
          nread += ret;
637
0
          dfx->length -= ret;
638
0
        }
639
0
      if (!dfx->length)
640
0
        dfx->eof_seen = 1; /* Normal EOF.  */
641
0
    }
642
643
0
  return nread;
644
0
}
645
646
647
/* The core of the AEAD decryption.  This is the underflow function of
648
 * the aead_decode_filter.  */
649
static gpg_error_t
650
aead_underflow (decode_filter_ctx_t dfx, iobuf_t a, byte *buf, size_t *ret_len)
651
0
{
652
0
  const size_t size = *ret_len; /* The allocated size of BUF.  */
653
0
  gpg_error_t err = 0;
654
0
  size_t totallen = 0; /* The number of bytes to return on success or EOF.  */
655
0
  size_t off = 0;      /* The offset into the buffer.  */
656
0
  size_t len;          /* The current number of bytes in BUF+OFF.  */
657
658
0
  log_assert (size > 48); /* Our code requires at least this size.  */
659
660
  /* Copy the rest from the last call of this function into BUF.  */
661
0
  len = dfx->holdbacklen;
662
0
  dfx->holdbacklen = 0;
663
0
  memcpy (buf, dfx->holdback, len);
664
665
0
  if (DBG_FILTER)
666
0
    log_debug ("aead_underflow: size=%zu len=%zu%s%s\n", size, len,
667
0
               dfx->partial? " partial":"", dfx->eof_seen? " eof":"");
668
669
  /* Read and fill up BUF.  We need to watch out for an EOF so that we
670
   * can detect the last chunk which is commonly shorter than the
671
   * chunksize.  After the last data byte from the last chunk 32 more
672
   * bytes are expected for the last chunk's tag and the following
673
   * final chunk's tag.  To detect the EOF we need to try reading at least
674
   * one further byte; however we try to read 16 extra bytes to avoid
675
   * single byte reads in some lower layers.  The outcome is that we
676
   * have up to 48 extra extra octets which we will later put into the
677
   * holdback buffer for the next invocation (which handles the EOF
678
   * case).  */
679
0
  len = fill_buffer (dfx, a, buf, size, len);
680
0
  if (len < 32)
681
0
    {
682
      /* Not enough data for the last two tags.  */
683
0
      err = gpg_error (GPG_ERR_TRUNCATED);
684
0
      goto leave;
685
0
    }
686
0
  if (dfx->eof_seen)
687
0
    {
688
      /* If have seen an EOF we copy only the last two auth tags into
689
       * the holdback buffer.  */
690
0
      dfx->holdbacklen = 32;
691
0
      memcpy (dfx->holdback, buf+len-32, 32);
692
0
      len -= 32;
693
0
    }
694
0
  else
695
0
    {
696
      /* If have not seen an EOF we copy the entire extra 48 bytes
697
       * into the holdback buffer for processing at the next call of
698
       * this function.  */
699
0
      dfx->holdbacklen = len > 48? 48 : len;
700
0
      memcpy (dfx->holdback, buf+len-dfx->holdbacklen, dfx->holdbacklen);
701
0
      len -= dfx->holdbacklen;
702
0
    }
703
  /* log_printhex (dfx->holdback, dfx->holdbacklen, "holdback:"); */
704
705
  /* Decrypt the buffer.  This first requires a loop to handle the
706
   * case when a chunk ends within the buffer.  */
707
0
  if (DBG_FILTER)
708
0
    log_debug ("decrypt: chunklen=%llu total=%llu size=%zu len=%zu%s\n",
709
0
               (unsigned long long)dfx->chunklen,
710
0
               (unsigned long long)dfx->total,
711
0
               size, len,
712
0
               dfx->eof_seen? " eof":"");
713
714
0
  while (len && dfx->chunklen + len >= dfx->chunksize)
715
0
    {
716
0
      size_t n = dfx->chunksize - dfx->chunklen;
717
0
      byte tagbuf[16];
718
719
0
      if (DBG_FILTER)
720
0
        log_debug ("chunksize will be reached: n=%zu\n", n);
721
722
0
      if (!dfx->chunklen)
723
0
        {
724
          /* First data for this chunk - prepare.  */
725
0
          err = aead_set_nonce_and_ad (dfx, 0);
726
0
          if (err)
727
0
            goto leave;
728
0
        }
729
730
      /* log_printhex (buf, n, "ciph:"); */
731
0
      gcry_cipher_final (dfx->cipher_hd);
732
0
      err = gcry_cipher_decrypt (dfx->cipher_hd, buf+off, n, NULL, 0);
733
0
      if (err)
734
0
        {
735
0
          log_error ("gcry_cipher_decrypt failed (1): %s\n",
736
0
                     gpg_strerror (err));
737
0
          goto leave;
738
0
        }
739
      /* log_printhex (buf, n, "plai:"); */
740
0
      totallen += n;
741
0
      dfx->chunklen += n;
742
0
      dfx->total += n;
743
0
      off += n;
744
0
      len -= n;
745
746
0
      if (DBG_FILTER)
747
0
        log_debug ("ndecrypted: %zu (nchunk=%llu) bytes left: %zu at off=%zu\n",
748
0
                   totallen, (unsigned long long)dfx->chunklen, len, off);
749
750
      /* Check the tag.  */
751
0
      if (len < 16)
752
0
        {
753
          /* The tag is not entirely in the buffer.  Read the rest of
754
           * the tag from the holdback buffer.  Then shift the holdback
755
           * buffer and fill it up again.  */
756
0
          memcpy (tagbuf, buf+off, len);
757
0
          memcpy (tagbuf + len, dfx->holdback, 16 - len);
758
0
          dfx->holdbacklen -= 16-len;
759
0
          memmove (dfx->holdback, dfx->holdback + (16-len), dfx->holdbacklen);
760
761
0
          if (dfx->eof_seen)
762
0
            {
763
              /* We should have the last chunk's tag in TAGBUF and the
764
               * final tag in HOLDBACKBUF.  */
765
0
              if (len || dfx->holdbacklen != 16)
766
0
                {
767
                  /* Not enough data for the last two tags.  */
768
0
                  err = gpg_error (GPG_ERR_TRUNCATED);
769
0
                  goto leave;
770
0
                }
771
0
            }
772
0
          else
773
0
            {
774
0
              len = 0;
775
0
              dfx->holdbacklen = fill_buffer (dfx, a, dfx->holdback, 48,
776
0
                                              dfx->holdbacklen);
777
0
              if (dfx->holdbacklen < 32)
778
0
                {
779
                  /* Not enough data for the last two tags.  */
780
0
                  err = gpg_error (GPG_ERR_TRUNCATED);
781
0
                  goto leave;
782
0
                }
783
0
            }
784
0
        }
785
0
      else /* We already have the full tag.  */
786
0
        {
787
0
          memcpy (tagbuf, buf+off, 16);
788
          /* Remove that tag from the output.  */
789
0
          memmove (buf + off, buf + off + 16, len - 16);
790
0
          len -= 16;
791
0
        }
792
0
      err = aead_checktag (dfx, 0, tagbuf);
793
0
      if (err)
794
0
        goto leave;
795
0
      dfx->chunklen = 0;
796
0
      dfx->chunkindex++;
797
798
0
      continue;
799
0
    }
800
801
  /* The bulk decryption of our buffer.  */
802
0
  if (len)
803
0
    {
804
0
      if (!dfx->chunklen)
805
0
        {
806
          /* First data for this chunk - prepare.  */
807
0
          err = aead_set_nonce_and_ad (dfx, 0);
808
0
          if (err)
809
0
            goto leave;
810
0
        }
811
812
0
      if (dfx->eof_seen)
813
0
        {
814
          /* This is the last block of the last chunk.  Its length may
815
           * not be a multiple of the block length.  */
816
0
          gcry_cipher_final (dfx->cipher_hd);
817
0
        }
818
0
      err = gcry_cipher_decrypt (dfx->cipher_hd, buf + off, len, NULL, 0);
819
0
      if (err)
820
0
        {
821
0
          log_error ("gcry_cipher_decrypt failed (2): %s\n",
822
0
                     gpg_strerror (err));
823
0
          goto leave;
824
0
        }
825
0
      totallen += len;
826
0
      dfx->chunklen += len;
827
0
      dfx->total += len;
828
0
      if (DBG_FILTER)
829
0
        log_debug ("ndecrypted: %zu (nchunk=%llu)\n",
830
0
                   totallen, (unsigned long long)dfx->chunklen);
831
0
    }
832
833
0
  if (dfx->eof_seen)
834
0
    {
835
836
0
      if (dfx->chunklen)
837
0
        {
838
0
          if (DBG_FILTER)
839
0
            log_debug ("eof seen: holdback has the last and final tag\n");
840
0
          log_assert (dfx->holdbacklen >= 32);
841
0
          err = aead_checktag (dfx, 0, dfx->holdback);
842
0
          if (err)
843
0
            goto leave;
844
0
          dfx->chunklen = 0;
845
0
          dfx->chunkindex++;
846
0
          off = 16;
847
0
        }
848
0
      else
849
0
        {
850
0
          if (DBG_FILTER)
851
0
            log_debug ("eof seen: holdback has the final tag\n");
852
0
          log_assert (dfx->holdbacklen >= 16);
853
0
          off = 0;
854
0
        }
855
856
      /* Check the final chunk.  */
857
0
      err = aead_set_nonce_and_ad (dfx, 1);
858
0
      if (err)
859
0
        goto leave;
860
0
      gcry_cipher_final (dfx->cipher_hd);
861
      /* Decrypt an empty string (using HOLDBACK as a dummy).  */
862
0
      err = gcry_cipher_decrypt (dfx->cipher_hd, dfx->holdback, 0, NULL, 0);
863
0
      if (err)
864
0
        {
865
0
          log_error ("gcry_cipher_decrypt failed (final): %s\n",
866
0
                     gpg_strerror (err));
867
0
          goto leave;
868
0
        }
869
0
      err = aead_checktag (dfx, 1, dfx->holdback+off);
870
0
      if (err)
871
0
        goto leave;
872
0
      err = gpg_error (GPG_ERR_EOF);
873
0
    }
874
875
0
 leave:
876
0
  if (DBG_FILTER)
877
0
    log_debug ("aead_underflow: returning %zu (%s)\n",
878
0
               totallen, gpg_strerror (err));
879
880
  /* In case of an auth error we map the error code to the same as
881
   * used by the MDC decryption.  */
882
0
  if (gpg_err_code (err) == GPG_ERR_CHECKSUM)
883
0
    err = gpg_error (GPG_ERR_BAD_SIGNATURE);
884
885
  /* In case of an error we better wipe out the buffer than to convey
886
   * partly decrypted data.  */
887
0
  if (err && gpg_err_code (err) != GPG_ERR_EOF)
888
0
    memset (buf, 0, size);
889
890
0
  *ret_len = totallen;
891
892
0
  return err;
893
0
}
894
895
896
/* The IOBUF filter used to decrypt AEAD encrypted data.  */
897
static int
898
aead_decode_filter (void *opaque, int control, IOBUF a,
899
                    byte *buf, size_t *ret_len)
900
0
{
901
0
  decode_filter_ctx_t dfx = opaque;
902
0
  int rc = 0;
903
904
0
  if ( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen )
905
0
    {
906
0
      *ret_len = 0;
907
0
      rc = -1;
908
0
    }
909
0
  else if ( control == IOBUFCTRL_UNDERFLOW )
910
0
    {
911
0
      log_assert (a);
912
913
0
      rc = aead_underflow (dfx, a, buf, ret_len);
914
0
      if (gpg_err_code (rc) == GPG_ERR_EOF)
915
0
        rc = -1; /* We need to use the old convention in the filter.  */
916
917
0
    }
918
0
  else if ( control == IOBUFCTRL_FREE )
919
0
    {
920
0
      release_dfx_context (dfx);
921
0
    }
922
0
  else if ( control == IOBUFCTRL_DESC )
923
0
    {
924
0
      mem2str (buf, "aead_decode_filter", *ret_len);
925
0
    }
926
927
0
  return rc;
928
0
}
929
930
931
static int
932
mdc_decode_filter (void *opaque, int control, IOBUF a,
933
                   byte *buf, size_t *ret_len)
934
0
{
935
0
  decode_filter_ctx_t dfx = opaque;
936
0
  size_t n, size = *ret_len;
937
0
  int rc = 0;
938
939
  /* Note: We need to distinguish between a partial and a fixed length
940
     packet.  The first is the usual case as created by GPG.  However
941
     for short messages the format degrades to a fixed length packet
942
     and other implementations might use fixed length as well.  Only
943
     looking for the EOF on fixed data works only if the encrypted
944
     packet is not followed by other data.  This used to be a long
945
     standing bug which was fixed on 2009-10-02.  */
946
947
0
  if ( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen )
948
0
    {
949
0
      *ret_len = 0;
950
0
      rc = -1;
951
0
    }
952
0
  else if( control == IOBUFCTRL_UNDERFLOW )
953
0
    {
954
0
      log_assert (a);
955
0
      log_assert (size > 44); /* Our code requires at least this size.  */
956
957
      /* Get at least 22 bytes and put it ahead in the buffer.  */
958
0
      n = fill_buffer (dfx, a, buf, 44, 22);
959
0
      if (n == 44)
960
0
        {
961
          /* We have enough stuff - flush the holdback buffer.  */
962
0
          if ( !dfx->holdbacklen )  /* First time. */
963
0
            {
964
0
              memcpy (buf, buf+22, 22);
965
0
              n = 22;
966
0
      }
967
0
          else
968
0
            {
969
0
              memcpy (buf, dfx->holdback, 22);
970
0
      }
971
972
          /* Fill up the buffer. */
973
0
          n = fill_buffer (dfx, a, buf, size, n);
974
975
          /* Move the trailing 22 bytes back to the holdback buffer.  We
976
             have at least 44 bytes thus a memmove is not needed.  */
977
0
          n -= 22;
978
0
          memcpy (dfx->holdback, buf+n, 22 );
979
0
          dfx->holdbacklen = 22;
980
0
  }
981
0
      else if ( !dfx->holdbacklen ) /* EOF seen but empty holdback. */
982
0
        {
983
          /* This is bad because it means an incomplete hash. */
984
0
          n -= 22;
985
0
          memcpy (buf, buf+22, n );
986
0
          dfx->eof_seen = 2; /* EOF with incomplete hash.  */
987
0
  }
988
0
      else  /* EOF seen (i.e. read less than 22 bytes). */
989
0
        {
990
0
          memcpy (buf, dfx->holdback, 22 );
991
0
          n -= 22;
992
0
          memcpy (dfx->holdback, buf+n, 22 );
993
0
          dfx->eof_seen = 1; /* Normal EOF. */
994
0
  }
995
996
0
      if ( n )
997
0
        {
998
0
          if ( dfx->cipher_hd )
999
0
            gcry_cipher_decrypt (dfx->cipher_hd, buf, n, NULL, 0);
1000
0
          if ( dfx->mdc_hash )
1001
0
            gcry_md_write (dfx->mdc_hash, buf, n);
1002
0
  }
1003
0
      else
1004
0
        {
1005
0
          log_assert ( dfx->eof_seen );
1006
0
          rc = -1; /* Return EOF.  */
1007
0
  }
1008
0
      *ret_len = n;
1009
0
    }
1010
0
  else if ( control == IOBUFCTRL_FREE )
1011
0
    {
1012
0
      release_dfx_context (dfx);
1013
0
    }
1014
0
  else if ( control == IOBUFCTRL_DESC )
1015
0
    {
1016
0
      mem2str (buf, "mdc_decode_filter", *ret_len);
1017
0
    }
1018
0
  return rc;
1019
0
}
1020
1021
1022
static int
1023
decode_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len)
1024
0
{
1025
0
  decode_filter_ctx_t fc = opaque;
1026
0
  size_t size = *ret_len;
1027
0
  size_t n;
1028
0
  int rc = 0;
1029
1030
1031
0
  if ( control == IOBUFCTRL_UNDERFLOW && fc->eof_seen )
1032
0
    {
1033
0
      *ret_len = 0;
1034
0
      rc = -1;
1035
0
    }
1036
0
  else if ( control == IOBUFCTRL_UNDERFLOW )
1037
0
    {
1038
0
      log_assert (a);
1039
1040
0
      n = fill_buffer (fc, a, buf, size, 0);
1041
0
      if (n)
1042
0
        {
1043
0
          if (fc->cipher_hd)
1044
0
            gcry_cipher_decrypt (fc->cipher_hd, buf, n, NULL, 0);
1045
0
        }
1046
0
      else
1047
0
        {
1048
0
          if (!fc->eof_seen)
1049
0
            fc->eof_seen = 1;
1050
0
          rc = -1; /* Return EOF. */
1051
0
        }
1052
0
      *ret_len = n;
1053
0
    }
1054
0
  else if ( control == IOBUFCTRL_FREE )
1055
0
    {
1056
0
      release_dfx_context (fc);
1057
0
    }
1058
0
  else if ( control == IOBUFCTRL_DESC )
1059
0
    {
1060
0
      mem2str (buf, "decode_filter", *ret_len);
1061
0
    }
1062
0
  return rc;
1063
0
}