2003-06-05 09:14:21 +02:00
|
|
|
/* cipher.c - En-/De-ciphering filter
|
2017-12-13 11:56:28 +01:00
|
|
|
* Copyright (C) 1998-2003, 2006, 2009 Free Software Foundation, Inc.
|
|
|
|
* Copyright (C) 1998-2003, 2006, 2009, 2017 Werner koch
|
2003-06-05 09:14:21 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 09:14:21 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2017-12-13 11:56:28 +01:00
|
|
|
* SPDX-License-Identifier: GPL-3.0+
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2003-06-18 21:56:13 +02:00
|
|
|
#include "gpg.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/status.h"
|
|
|
|
#include "../common/iobuf.h"
|
|
|
|
#include "../common/util.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
#include "filter.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "main.h"
|
2018-05-29 12:42:44 +02:00
|
|
|
#include "../common/i18n.h"
|
2017-03-07 12:21:23 +01:00
|
|
|
#include "../common/status.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
#define MIN_PARTIAL_SIZE 512
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2017-12-13 11:56:28 +01:00
|
|
|
write_header (cipher_filter_context_t *cfx, iobuf_t a)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2017-12-13 11:56:28 +01:00
|
|
|
gcry_error_t err;
|
|
|
|
PACKET pkt;
|
|
|
|
PKT_encrypted ed;
|
|
|
|
byte temp[18];
|
|
|
|
unsigned int blocksize;
|
|
|
|
unsigned int nprefix;
|
|
|
|
|
|
|
|
blocksize = openpgp_cipher_get_algo_blklen (cfx->dek->algo);
|
|
|
|
if ( blocksize < 8 || blocksize > 16 )
|
|
|
|
log_fatal ("unsupported blocksize %u\n", blocksize);
|
|
|
|
|
|
|
|
memset (&ed, 0, sizeof ed);
|
|
|
|
ed.len = cfx->datalen;
|
|
|
|
ed.extralen = blocksize + 2;
|
|
|
|
ed.new_ctb = !ed.len;
|
|
|
|
if (cfx->dek->use_mdc)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2017-12-13 11:56:28 +01:00
|
|
|
ed.mdc_method = DIGEST_ALGO_SHA1;
|
|
|
|
gcry_md_open (&cfx->mdc_hash, DIGEST_ALGO_SHA1, 0);
|
|
|
|
if (DBG_HASHING)
|
|
|
|
gcry_md_debug (cfx->mdc_hash, "creatmdc");
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2018-05-15 12:19:40 +02:00
|
|
|
else
|
2017-12-13 13:02:34 +01:00
|
|
|
{
|
2018-05-29 12:42:44 +02:00
|
|
|
log_info (_("WARNING: "
|
|
|
|
"encrypting without integrity protection is dangerous\n"));
|
|
|
|
log_info (_("Hint: Do not use option %s\n"), "--rfc2440");
|
2017-12-13 13:02:34 +01:00
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2017-12-13 11:58:51 +01:00
|
|
|
write_status_printf (STATUS_BEGIN_ENCRYPTION, "%d %d",
|
|
|
|
ed.mdc_method, cfx->dek->algo);
|
2017-12-13 11:56:28 +01:00
|
|
|
|
|
|
|
init_packet (&pkt);
|
|
|
|
pkt.pkttype = cfx->dek->use_mdc? PKT_ENCRYPTED_MDC : PKT_ENCRYPTED;
|
|
|
|
pkt.pkt.encrypted = &ed;
|
|
|
|
if (build_packet( a, &pkt))
|
|
|
|
log_bug ("build_packet(ENCR_DATA) failed\n");
|
|
|
|
nprefix = blocksize;
|
|
|
|
gcry_randomize (temp, nprefix, GCRY_STRONG_RANDOM );
|
|
|
|
temp[nprefix] = temp[nprefix-2];
|
|
|
|
temp[nprefix+1] = temp[nprefix-1];
|
|
|
|
print_cipher_algo_note (cfx->dek->algo);
|
|
|
|
err = openpgp_cipher_open (&cfx->cipher_hd,
|
|
|
|
cfx->dek->algo,
|
|
|
|
GCRY_CIPHER_MODE_CFB,
|
|
|
|
(GCRY_CIPHER_SECURE
|
|
|
|
| ((cfx->dek->use_mdc || cfx->dek->algo >= 100)?
|
|
|
|
0 : GCRY_CIPHER_ENABLE_SYNC)));
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
/* We should never get an error here cause we already checked,
|
|
|
|
* that the algorithm is available. */
|
|
|
|
BUG();
|
2003-06-18 21:56:13 +02:00
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
|
2017-12-13 11:56:28 +01:00
|
|
|
/* log_hexdump ("thekey", cfx->dek->key, cfx->dek->keylen); */
|
|
|
|
gcry_cipher_setkey (cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen);
|
|
|
|
gcry_cipher_setiv (cfx->cipher_hd, NULL, 0);
|
|
|
|
/* log_hexdump ("prefix", temp, nprefix+2); */
|
|
|
|
if (cfx->mdc_hash) /* Hash the "IV". */
|
|
|
|
gcry_md_write (cfx->mdc_hash, temp, nprefix+2 );
|
|
|
|
gcry_cipher_encrypt (cfx->cipher_hd, temp, nprefix+2, NULL, 0);
|
|
|
|
gcry_cipher_sync (cfx->cipher_hd);
|
|
|
|
iobuf_write (a, temp, nprefix+2);
|
2017-12-13 13:02:34 +01:00
|
|
|
|
|
|
|
cfx->short_blklen_warn = (blocksize < 16);
|
|
|
|
cfx->short_blklen_count = nprefix+2;
|
|
|
|
|
|
|
|
cfx->wrote_header = 1;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 11:56:28 +01:00
|
|
|
/*
|
|
|
|
* This filter is used to en/de-cipher data with a symmetric algorithm
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
int
|
2017-12-13 11:56:28 +01:00
|
|
|
cipher_filter (void *opaque, int control, iobuf_t a, byte *buf, size_t *ret_len)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2017-12-13 11:56:28 +01:00
|
|
|
cipher_filter_context_t *cfx = opaque;
|
|
|
|
size_t size = *ret_len;
|
|
|
|
int rc = 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2017-12-13 11:56:28 +01:00
|
|
|
if (control == IOBUFCTRL_UNDERFLOW) /* decrypt */
|
|
|
|
{
|
|
|
|
rc = -1; /* not yet used */
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2017-12-13 11:56:28 +01:00
|
|
|
else if (control == IOBUFCTRL_FLUSH) /* encrypt */
|
|
|
|
{
|
|
|
|
log_assert (a);
|
2017-12-13 13:02:34 +01:00
|
|
|
if (!cfx->wrote_header)
|
2017-12-13 11:56:28 +01:00
|
|
|
write_header (cfx, a);
|
|
|
|
if (cfx->mdc_hash)
|
|
|
|
gcry_md_write (cfx->mdc_hash, buf, size);
|
|
|
|
gcry_cipher_encrypt (cfx->cipher_hd, buf, size, NULL, 0);
|
2017-12-13 13:02:34 +01:00
|
|
|
if (cfx->short_blklen_warn)
|
|
|
|
{
|
|
|
|
cfx->short_blklen_count += size;
|
|
|
|
if (cfx->short_blklen_count > (150 * 1024 * 1024))
|
|
|
|
{
|
|
|
|
log_info ("WARNING: encrypting more than %d MiB with algorithm "
|
|
|
|
"%s should be avoided\n", 150,
|
|
|
|
openpgp_cipher_algo_name (cfx->dek->algo));
|
|
|
|
cfx->short_blklen_warn = 0; /* Don't show again. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 11:56:28 +01:00
|
|
|
rc = iobuf_write (a, buf, size);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2017-12-13 11:56:28 +01:00
|
|
|
else if (control == IOBUFCTRL_FREE)
|
|
|
|
{
|
|
|
|
if (cfx->mdc_hash)
|
|
|
|
{
|
|
|
|
byte *hash;
|
|
|
|
int hashlen = gcry_md_get_algo_dlen (gcry_md_get_algo(cfx->mdc_hash));
|
|
|
|
byte temp[22];
|
|
|
|
|
|
|
|
log_assert (hashlen == 20);
|
|
|
|
/* We must hash the prefix of the MDC packet here. */
|
|
|
|
temp[0] = 0xd3;
|
|
|
|
temp[1] = 0x14;
|
|
|
|
gcry_md_putc (cfx->mdc_hash, temp[0]);
|
|
|
|
gcry_md_putc (cfx->mdc_hash, temp[1]);
|
|
|
|
|
|
|
|
gcry_md_final (cfx->mdc_hash);
|
|
|
|
hash = gcry_md_read (cfx->mdc_hash, 0);
|
|
|
|
memcpy(temp+2, hash, 20);
|
|
|
|
gcry_cipher_encrypt (cfx->cipher_hd, temp, 22, NULL, 0);
|
|
|
|
gcry_md_close (cfx->mdc_hash); cfx->mdc_hash = NULL;
|
|
|
|
if (iobuf_write( a, temp, 22))
|
|
|
|
log_error ("writing MDC packet failed\n");
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2017-12-13 11:56:28 +01:00
|
|
|
|
|
|
|
gcry_cipher_close (cfx->cipher_hd);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2017-12-13 11:56:28 +01:00
|
|
|
else if (control == IOBUFCTRL_DESC)
|
|
|
|
{
|
|
|
|
mem2str (buf, "cipher_filter", *ret_len);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
2017-12-13 11:56:28 +01:00
|
|
|
|
|
|
|
return rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|