2003-06-05 09:14:21 +02:00
|
|
|
/* cipher.c - En-/De-ciphering filter
|
2006-04-19 13:26:11 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2003,
|
Add full Camellia support.
* configure.ac: Remove Camellia restriction.
* gpg.c (main), misc.c (openpgp_cipher_test_algo): Remove Camellia
restriction.
* misc.c (map_cipher_openpgp_to_gcry), main.h: Add macros for
openpgp_cipher_open, openpgp_cipher_get_algo_keylen, and
openpgp_cipher_get_algo_blklen to wrap around the corresponding gcry_*
functions, but pass the algorithm number through
map_cipher_openpgp_to_gcry. This is needed in case the gcry algorithm
number doesn't match the OpenPGP number (c.f. Camellia).
* encr-data.c, pubkey-enc.c, mainproc.c, cipher.c, encode.c, seskey.c,
passphrase.c, seckey-cert.c: Use new openpgp_cipher_* macros here.
2009-06-05 16:11:03 +02:00
|
|
|
* 2006, 2009 Free Software Foundation, Inc.
|
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
|
2007-07-04 21:49:40 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
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"
|
2007-11-19 17:03:50 +01:00
|
|
|
#include "status.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
#include "iobuf.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "status.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define MIN_PARTIAL_SIZE 512
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2006-04-19 13:26:11 +02:00
|
|
|
write_header( cipher_filter_context_t *cfx, IOBUF a )
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2006-12-06 11:16:50 +01:00
|
|
|
gcry_error_t err;
|
2003-06-05 09:14:21 +02:00
|
|
|
PACKET pkt;
|
|
|
|
PKT_encrypted ed;
|
|
|
|
byte temp[18];
|
2003-06-18 21:56:13 +02:00
|
|
|
unsigned int blocksize;
|
|
|
|
unsigned int nprefix;
|
2003-06-05 09:14:21 +02:00
|
|
|
|
Add full Camellia support.
* configure.ac: Remove Camellia restriction.
* gpg.c (main), misc.c (openpgp_cipher_test_algo): Remove Camellia
restriction.
* misc.c (map_cipher_openpgp_to_gcry), main.h: Add macros for
openpgp_cipher_open, openpgp_cipher_get_algo_keylen, and
openpgp_cipher_get_algo_blklen to wrap around the corresponding gcry_*
functions, but pass the algorithm number through
map_cipher_openpgp_to_gcry. This is needed in case the gcry algorithm
number doesn't match the OpenPGP number (c.f. Camellia).
* encr-data.c, pubkey-enc.c, mainproc.c, cipher.c, encode.c, seskey.c,
passphrase.c, seckey-cert.c: Use new openpgp_cipher_* macros here.
2009-06-05 16:11:03 +02:00
|
|
|
blocksize = openpgp_cipher_get_algo_blklen (cfx->dek->algo);
|
2006-04-19 13:26:11 +02:00
|
|
|
if ( blocksize < 8 || blocksize > 16 )
|
2003-06-05 09:14:21 +02:00
|
|
|
log_fatal("unsupported blocksize %u\n", blocksize );
|
|
|
|
|
|
|
|
memset( &ed, 0, sizeof ed );
|
|
|
|
ed.len = cfx->datalen;
|
|
|
|
ed.extralen = blocksize+2;
|
2014-08-12 10:36:30 +02:00
|
|
|
ed.new_ctb = !ed.len;
|
2003-06-05 09:14:21 +02:00
|
|
|
if( cfx->dek->use_mdc ) {
|
|
|
|
ed.mdc_method = DIGEST_ALGO_SHA1;
|
2006-04-19 13:26:11 +02:00
|
|
|
gcry_md_open (&cfx->mdc_hash, DIGEST_ALGO_SHA1, 0);
|
2003-06-05 09:14:21 +02:00
|
|
|
if ( DBG_HASHING )
|
2011-09-20 09:54:27 +02:00
|
|
|
gcry_md_debug (cfx->mdc_hash, "creatmdc");
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char buf[20];
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
sprintf (buf, "%d %d", ed.mdc_method, cfx->dek->algo);
|
|
|
|
write_status_text (STATUS_BEGIN_ENCRYPTION, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2006-04-19 13:26:11 +02:00
|
|
|
gcry_randomize (temp, nprefix, GCRY_STRONG_RANDOM );
|
2003-06-05 09:14:21 +02:00
|
|
|
temp[nprefix] = temp[nprefix-2];
|
|
|
|
temp[nprefix+1] = temp[nprefix-1];
|
|
|
|
print_cipher_algo_note( cfx->dek->algo );
|
2011-02-04 12:57:53 +01:00
|
|
|
err = openpgp_cipher_open (&cfx->cipher_hd,
|
Add full Camellia support.
* configure.ac: Remove Camellia restriction.
* gpg.c (main), misc.c (openpgp_cipher_test_algo): Remove Camellia
restriction.
* misc.c (map_cipher_openpgp_to_gcry), main.h: Add macros for
openpgp_cipher_open, openpgp_cipher_get_algo_keylen, and
openpgp_cipher_get_algo_blklen to wrap around the corresponding gcry_*
functions, but pass the algorithm number through
map_cipher_openpgp_to_gcry. This is needed in case the gcry algorithm
number doesn't match the OpenPGP number (c.f. Camellia).
* encr-data.c, pubkey-enc.c, mainproc.c, cipher.c, encode.c, seskey.c,
passphrase.c, seckey-cert.c: Use new openpgp_cipher_* macros here.
2009-06-05 16:11:03 +02:00
|
|
|
cfx->dek->algo,
|
|
|
|
GCRY_CIPHER_MODE_CFB,
|
|
|
|
(GCRY_CIPHER_SECURE
|
|
|
|
| ((cfx->dek->use_mdc || cfx->dek->algo >= 100)?
|
|
|
|
0 : GCRY_CIPHER_ENABLE_SYNC)));
|
2006-04-19 15:24:36 +02:00
|
|
|
if (err) {
|
2006-04-19 13:26:11 +02:00
|
|
|
/* We should never get an error here cause we already checked,
|
|
|
|
* that the algorithm is available. */
|
2003-06-18 21:56:13 +02:00
|
|
|
BUG();
|
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
/* log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen );
|
|
|
|
gcry_cipher_setiv( cfx->cipher_hd, NULL, 0 );
|
2003-06-05 09:14:21 +02:00
|
|
|
/* log_hexdump( "prefix", temp, nprefix+2 ); */
|
2006-04-19 13:26:11 +02:00
|
|
|
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);
|
2003-06-05 09:14:21 +02:00
|
|
|
iobuf_write(a, temp, nprefix+2);
|
|
|
|
cfx->header=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* This filter is used to en/de-cipher data with a conventional algorithm
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
cipher_filter( void *opaque, int control,
|
2006-04-19 13:26:11 +02:00
|
|
|
IOBUF a, byte *buf, size_t *ret_len)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
|
|
|
size_t size = *ret_len;
|
|
|
|
cipher_filter_context_t *cfx = opaque;
|
|
|
|
int rc=0;
|
|
|
|
|
|
|
|
if( control == IOBUFCTRL_UNDERFLOW ) { /* decrypt */
|
|
|
|
rc = -1; /* not yet used */
|
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */
|
2016-04-29 11:05:24 +02:00
|
|
|
log_assert(a);
|
2003-06-05 09:14:21 +02:00
|
|
|
if( !cfx->header ) {
|
|
|
|
write_header( cfx, a );
|
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
if (cfx->mdc_hash)
|
|
|
|
gcry_md_write (cfx->mdc_hash, buf, size);
|
|
|
|
gcry_cipher_encrypt (cfx->cipher_hd, buf, size, NULL, 0);
|
2003-06-18 21:56:13 +02:00
|
|
|
rc = iobuf_write( a, buf, size );
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_FREE ) {
|
|
|
|
if( cfx->mdc_hash ) {
|
|
|
|
byte *hash;
|
2006-04-19 13:26:11 +02:00
|
|
|
int hashlen = gcry_md_get_algo_dlen (gcry_md_get_algo
|
|
|
|
(cfx->mdc_hash));
|
2003-06-05 09:14:21 +02:00
|
|
|
byte temp[22];
|
|
|
|
|
2016-04-29 11:05:24 +02:00
|
|
|
log_assert( hashlen == 20 );
|
2006-10-02 13:54:35 +02:00
|
|
|
/* We must hash the prefix of the MDC packet here. */
|
2003-06-05 09:14:21 +02:00
|
|
|
temp[0] = 0xd3;
|
|
|
|
temp[1] = 0x14;
|
2006-04-19 13:26:11 +02:00
|
|
|
gcry_md_putc (cfx->mdc_hash, temp[0]);
|
|
|
|
gcry_md_putc (cfx->mdc_hash, temp[1]);
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
gcry_md_final (cfx->mdc_hash);
|
|
|
|
hash = gcry_md_read (cfx->mdc_hash, 0);
|
2003-06-05 09:14:21 +02:00
|
|
|
memcpy(temp+2, hash, 20);
|
2006-04-19 13:26:11 +02:00
|
|
|
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 ) )
|
2003-06-05 09:14:21 +02:00
|
|
|
log_error("writing MDC packet failed\n" );
|
|
|
|
}
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_cipher_close (cfx->cipher_hd);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_DESC ) {
|
common: Fix iobuf API of filter function for alignment.
* common/iobuf.h (IOBUFCTRL_DESC): Change the call semantics.
* common/iobuf.c (iobuf_desc): Add the second argument DESC.
(print_chain, iobuf_close, do_open, iobuf_sockopen, iobuf_ioctl)
(iobuf_push_filter2, pop_filter, iobuf_write_temp): Change calls
of iobuf_desc.
(file_filter, file_es_filter, sock_filter, block_filter): Fill the
description.
* common/t-iobuf.c (every_other_filter, double_filter): Likewise.
* g10/armor.c, g10/cipher.c, g10/compress-bz2.c, g10/compress.c,
g10/decrypt-data.c, g10/encrypt.c, g10/mdfilter.c, g10/progress.c,
g10/textfilter.c: Likewise.
--
Newer GCC warns against possible alignment difference of pointers.
This change can silence those warnings.
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
2016-01-12 02:32:20 +01:00
|
|
|
mem2str (buf, "cipher_filter", *ret_len);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|