Coverage Report

Created: 2026-01-09 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/g10/cipher-cfb.c
Line
Count
Source
1
/* cipher-cfb.c - Enciphering filter for the old CFB mode.
2
 * Copyright (C) 1998-2003, 2006, 2009 Free Software Foundation, Inc.
3
 * Copyright (C) 1998-2003, 2006, 2009, 2017 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+
20
 */
21
22
#include <config.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <errno.h>
27
28
#include "gpg.h"
29
#include "../common/status.h"
30
#include "../common/iobuf.h"
31
#include "../common/util.h"
32
#include "filter.h"
33
#include "packet.h"
34
#include "options.h"
35
#include "main.h"
36
#include "../common/i18n.h"
37
#include "../common/status.h"
38
39
40
#define MIN_PARTIAL_SIZE 512
41
42
43
static void
44
write_header (cipher_filter_context_t *cfx, iobuf_t a)
45
0
{
46
0
  gcry_error_t err;
47
0
  PACKET pkt;
48
0
  PKT_encrypted ed;
49
0
  byte temp[18];
50
0
  unsigned int blocksize;
51
0
  unsigned int nprefix;
52
53
0
  blocksize = openpgp_cipher_get_algo_blklen (cfx->dek->algo);
54
0
  if ( blocksize < 8 || blocksize > 16 )
55
0
    log_fatal ("unsupported blocksize %u\n", blocksize);
56
57
0
  memset (&ed, 0, sizeof ed);
58
0
  ed.len = cfx->datalen;
59
0
  ed.extralen = blocksize + 2;
60
0
  ed.new_ctb = !ed.len;
61
0
  if (cfx->dek->use_mdc)
62
0
    {
63
0
      ed.mdc_method = DIGEST_ALGO_SHA1;
64
0
      gcry_md_open (&cfx->mdc_hash, DIGEST_ALGO_SHA1, 0);
65
0
      if (DBG_HASHING)
66
0
        gcry_md_debug (cfx->mdc_hash, "creatmdc");
67
0
    }
68
0
  else
69
0
    {
70
0
      log_info (_("WARNING: "
71
0
                  "encrypting without integrity protection is dangerous\n"));
72
0
      log_info (_("Hint: Do not use option %s\n"), "--rfc2440");
73
0
    }
74
75
0
  init_packet (&pkt);
76
0
  pkt.pkttype = cfx->dek->use_mdc? PKT_ENCRYPTED_MDC : PKT_ENCRYPTED;
77
0
  pkt.pkt.encrypted = &ed;
78
0
  if (build_packet( a, &pkt))
79
0
    log_bug ("build_packet(ENCR_DATA) failed\n");
80
0
  nprefix = blocksize;
81
0
  gcry_randomize (temp, nprefix, GCRY_STRONG_RANDOM );
82
0
  temp[nprefix] = temp[nprefix-2];
83
0
  temp[nprefix+1] = temp[nprefix-1];
84
0
  print_cipher_algo_note (cfx->dek->algo);
85
0
  err = openpgp_cipher_open (&cfx->cipher_hd,
86
0
                             cfx->dek->algo,
87
0
                             GCRY_CIPHER_MODE_CFB,
88
0
                             (GCRY_CIPHER_SECURE
89
0
                              | ((cfx->dek->use_mdc || cfx->dek->algo >= 100)?
90
0
                                 0 : GCRY_CIPHER_ENABLE_SYNC)));
91
0
  if (err)
92
0
    {
93
      /* We should never get an error here cause we already checked,
94
       * that the algorithm is available.  */
95
0
      BUG();
96
0
    }
97
98
  /* log_hexdump ("thekey", cfx->dek->key, cfx->dek->keylen); */
99
0
  gcry_cipher_setkey (cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen);
100
0
  gcry_cipher_setiv (cfx->cipher_hd, NULL, 0);
101
  /* log_hexdump ("prefix", temp, nprefix+2); */
102
0
  if (cfx->mdc_hash) /* Hash the "IV". */
103
0
    gcry_md_write (cfx->mdc_hash, temp, nprefix+2 );
104
0
  gcry_cipher_encrypt (cfx->cipher_hd, temp, nprefix+2, NULL, 0);
105
0
  gcry_cipher_sync (cfx->cipher_hd);
106
0
  iobuf_write (a, temp, nprefix+2);
107
108
0
  cfx->short_blklen_warn = (blocksize < 16);
109
0
  cfx->short_blklen_count = nprefix+2;
110
111
0
  cfx->wrote_header = 1;
112
0
}
113
114
115
/*
116
 * This filter is used to en/de-cipher data with a symmetric algorithm
117
 */
118
int
119
cipher_filter_cfb (void *opaque, int control,
120
                   iobuf_t a, byte *buf, size_t *ret_len)
121
0
{
122
0
  cipher_filter_context_t *cfx = opaque;
123
0
  size_t size = *ret_len;
124
0
  int rc = 0;
125
126
0
  if (control == IOBUFCTRL_UNDERFLOW) /* decrypt */
127
0
    {
128
0
      rc = -1; /* not yet used */
129
0
    }
130
0
  else if (control == IOBUFCTRL_FLUSH) /* encrypt */
131
0
    {
132
0
      log_assert (a);
133
0
      if (!cfx->wrote_header)
134
0
        write_header (cfx, a);
135
0
      if (cfx->mdc_hash)
136
0
        gcry_md_write (cfx->mdc_hash, buf, size);
137
0
      gcry_cipher_encrypt (cfx->cipher_hd, buf, size, NULL, 0);
138
0
      if (cfx->short_blklen_warn)
139
0
        {
140
0
          cfx->short_blklen_count += size;
141
0
          if (cfx->short_blklen_count > (150 * 1024 * 1024))
142
0
            {
143
0
              log_info ("WARNING: encrypting more than %d MiB with algorithm "
144
0
                        "%s should be avoided\n", 150,
145
0
                        openpgp_cipher_algo_name (cfx->dek->algo));
146
0
              cfx->short_blklen_warn = 0; /* Don't show again.  */
147
0
            }
148
0
        }
149
150
0
      rc = iobuf_write (a, buf, size);
151
0
    }
152
0
  else if (control == IOBUFCTRL_FREE)
153
0
    {
154
0
      if (cfx->mdc_hash)
155
0
        {
156
0
          byte *hash;
157
0
          int hashlen = gcry_md_get_algo_dlen (gcry_md_get_algo(cfx->mdc_hash));
158
0
          byte temp[22];
159
160
0
          log_assert (hashlen == 20);
161
          /* We must hash the prefix of the MDC packet here. */
162
0
          temp[0] = 0xd3;
163
0
          temp[1] = 0x14;
164
0
          gcry_md_putc (cfx->mdc_hash, temp[0]);
165
0
          gcry_md_putc (cfx->mdc_hash, temp[1]);
166
167
0
          gcry_md_final (cfx->mdc_hash);
168
0
          hash = gcry_md_read (cfx->mdc_hash, 0);
169
0
          memcpy(temp+2, hash, 20);
170
0
          gcry_cipher_encrypt (cfx->cipher_hd, temp, 22, NULL, 0);
171
0
          gcry_md_close (cfx->mdc_hash); cfx->mdc_hash = NULL;
172
0
          if (iobuf_write( a, temp, 22))
173
0
            log_error ("writing MDC packet failed\n");
174
0
  }
175
176
0
      gcry_cipher_close (cfx->cipher_hd);
177
0
    }
178
0
  else if (control == IOBUFCTRL_DESC)
179
0
    {
180
0
      mem2str (buf, "cipher_filter_cfb", *ret_len);
181
0
    }
182
0
  else if (control == IOBUFCTRL_INIT)
183
0
    {
184
0
      write_status_printf (STATUS_BEGIN_ENCRYPTION, "%d %d",
185
0
                           cfx->dek->use_mdc ? DIGEST_ALGO_SHA1 : 0,
186
0
                           cfx->dek->algo);
187
0
    }
188
189
0
  return rc;
190
0
}