1997-11-23 16:38:27 +01:00
|
|
|
/* cipher.c - En-/De-ciphering filter
|
2000-07-14 19:34:53 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
|
1997-11-23 16:38:27 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* This file is part of GnuPG.
|
1997-11-23 16:38:27 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-23 16:38:27 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-23 16:38:27 +01:00
|
|
|
* 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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
1999-10-26 14:14:37 +02:00
|
|
|
#include <gcrypt.h>
|
1997-11-23 16:38:27 +01:00
|
|
|
#include "errors.h"
|
|
|
|
#include "iobuf.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "options.h"
|
1999-01-09 18:59:58 +01:00
|
|
|
#include "main.h"
|
2000-07-14 19:34:53 +02:00
|
|
|
#include "status.h"
|
1997-11-23 16:38:27 +01:00
|
|
|
|
|
|
|
|
1998-07-02 21:31:46 +02:00
|
|
|
#define MIN_PARTIAL_SIZE 512
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
write_header( cipher_filter_context_t *cfx, IOBUF a )
|
|
|
|
{
|
|
|
|
PACKET pkt;
|
|
|
|
PKT_encrypted ed;
|
1998-07-29 21:35:05 +02:00
|
|
|
byte temp[18];
|
2000-07-14 19:34:53 +02:00
|
|
|
unsigned int blocksize;
|
|
|
|
unsigned int nprefix;
|
1999-09-18 12:17:16 +02:00
|
|
|
int rc;
|
2000-07-14 19:34:53 +02:00
|
|
|
int use_mdc = opt.force_mdc;
|
|
|
|
|
|
|
|
blocksize = gcry_cipher_get_algo_blklen( cfx->dek->algo );
|
|
|
|
if( blocksize < 8 || blocksize > 16 )
|
|
|
|
log_fatal("unsupported blocksize %u\n", blocksize );
|
|
|
|
if( blocksize != 8 )
|
|
|
|
use_mdc = 1; /* enable it for all modern ciphers */
|
|
|
|
if( opt.rfc2440 )
|
|
|
|
use_mdc = 0; /* override - rfc2440 does not know about MDC */
|
1998-07-02 21:31:46 +02:00
|
|
|
|
|
|
|
memset( &ed, 0, sizeof ed );
|
|
|
|
ed.len = cfx->datalen;
|
1998-07-06 12:23:57 +02:00
|
|
|
ed.new_ctb = !ed.len && !opt.rfc1991;
|
1999-05-17 22:03:24 +02:00
|
|
|
if( use_mdc ) {
|
1999-12-08 22:03:03 +01:00
|
|
|
ed.mdc_method = GCRY_MD_SHA1;
|
|
|
|
cfx->mdc_hash = gcry_md_open( GCRY_MD_SHA1, 0 );
|
2000-07-14 19:34:53 +02:00
|
|
|
if( !cfx->mdc_hash )
|
|
|
|
BUG();
|
|
|
|
if ( DBG_HASHING )
|
|
|
|
gcry_md_start_debug( cfx->mdc_hash, "creatmdc" );
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
1998-07-02 21:31:46 +02:00
|
|
|
init_packet( &pkt );
|
1999-05-17 22:03:24 +02:00
|
|
|
pkt.pkttype = use_mdc? PKT_ENCRYPTED_MDC : PKT_ENCRYPTED;
|
1998-07-02 21:31:46 +02:00
|
|
|
pkt.pkt.encrypted = &ed;
|
|
|
|
if( build_packet( a, &pkt ))
|
|
|
|
log_bug("build_packet(ENCR_DATA) failed\n");
|
1999-04-09 12:34:44 +02:00
|
|
|
nprefix = blocksize;
|
1999-11-13 17:43:23 +01:00
|
|
|
gcry_randomize( temp, nprefix, GCRY_STRONG_RANDOM );
|
1999-04-07 20:58:34 +02:00
|
|
|
temp[nprefix] = temp[nprefix-2];
|
|
|
|
temp[nprefix+1] = temp[nprefix-1];
|
1998-12-10 20:20:47 +01:00
|
|
|
print_cipher_algo_note( cfx->dek->algo );
|
1999-10-26 14:14:37 +02:00
|
|
|
if( !(cfx->cipher_hd = gcry_cipher_open( cfx->dek->algo,
|
|
|
|
GCRY_CIPHER_MODE_CFB,
|
|
|
|
GCRY_CIPHER_SECURE
|
2000-07-14 19:34:53 +02:00
|
|
|
| ((use_mdc || cfx->dek->algo >= 100) ?
|
1999-10-26 14:14:37 +02:00
|
|
|
0 : GCRY_CIPHER_ENABLE_SYNC)))
|
|
|
|
) {
|
1999-09-18 12:17:16 +02:00
|
|
|
/* we should never get an error here cause we already checked, that
|
|
|
|
* the algorithm is available. */
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
2000-07-14 19:34:53 +02:00
|
|
|
|
1999-04-18 10:18:52 +02:00
|
|
|
/* log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/
|
1999-09-18 12:17:16 +02:00
|
|
|
rc = gcry_cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen );
|
|
|
|
if( !rc )
|
|
|
|
rc = gcry_cipher_setiv( cfx->cipher_hd, NULL, 0 );
|
|
|
|
if( rc )
|
|
|
|
log_fatal("set key or IV failed: %s\n", gcry_strerror(rc) );
|
1999-04-18 10:18:52 +02:00
|
|
|
/* log_hexdump( "prefix", temp, nprefix+2 ); */
|
1999-05-17 22:03:24 +02:00
|
|
|
if( cfx->mdc_hash )
|
1999-09-18 12:17:16 +02:00
|
|
|
gcry_md_write( cfx->mdc_hash, temp, nprefix+2 );
|
1999-10-26 14:14:37 +02:00
|
|
|
rc = gcry_cipher_encrypt( cfx->cipher_hd, temp, nprefix+2, NULL, 0 );
|
1999-09-18 12:17:16 +02:00
|
|
|
if( !rc )
|
|
|
|
rc = gcry_cipher_sync( cfx->cipher_hd );
|
|
|
|
if( rc )
|
|
|
|
log_fatal("encrypt failed: %s\n", gcry_strerror(rc) );
|
1999-04-07 20:58:34 +02:00
|
|
|
iobuf_write(a, temp, nprefix+2);
|
1998-07-02 21:31:46 +02:00
|
|
|
cfx->header=1;
|
2000-07-14 19:34:53 +02:00
|
|
|
|
1998-07-02 21:31:46 +02:00
|
|
|
}
|
1997-11-23 16:38:27 +01:00
|
|
|
|
|
|
|
|
1999-05-17 22:03:24 +02:00
|
|
|
|
1997-11-23 16:38:27 +01:00
|
|
|
/****************
|
1998-04-14 19:51:16 +02:00
|
|
|
* This filter is used to en/de-cipher data with a conventional algorithm
|
1997-11-23 16:38:27 +01:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
cipher_filter( void *opaque, int control,
|
|
|
|
IOBUF a, byte *buf, size_t *ret_len)
|
|
|
|
{
|
|
|
|
size_t size = *ret_len;
|
|
|
|
cipher_filter_context_t *cfx = opaque;
|
|
|
|
int rc=0;
|
|
|
|
|
1998-03-19 16:27:29 +01:00
|
|
|
if( control == IOBUFCTRL_UNDERFLOW ) { /* decrypt */
|
1998-04-02 12:30:03 +02:00
|
|
|
rc = -1; /* not yet used */
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
1998-03-19 16:27:29 +01:00
|
|
|
else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */
|
1997-11-23 16:38:27 +01:00
|
|
|
assert(a);
|
|
|
|
if( !cfx->header ) {
|
2000-07-14 19:34:53 +02:00
|
|
|
write_status( STATUS_BEGIN_ENCRYPTION );
|
1998-07-02 21:31:46 +02:00
|
|
|
write_header( cfx, a );
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
1999-05-17 22:03:24 +02:00
|
|
|
if( cfx->mdc_hash )
|
1999-09-18 12:17:16 +02:00
|
|
|
gcry_md_write( cfx->mdc_hash, buf, size );
|
|
|
|
rc = gcry_cipher_encrypt( cfx->cipher_hd, buf, size, NULL, 0);
|
|
|
|
if( rc )
|
|
|
|
log_fatal("encrypt failed: %s\n", gcry_strerror(rc) );
|
1997-11-23 16:38:27 +01:00
|
|
|
if( iobuf_write( a, buf, size ) )
|
2000-01-27 17:50:45 +01:00
|
|
|
rc = GPGERR_WRITE_FILE;
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_FREE ) {
|
1999-05-17 22:03:24 +02:00
|
|
|
if( cfx->mdc_hash ) {
|
|
|
|
byte *hash;
|
1999-09-18 12:17:16 +02:00
|
|
|
int hashlen = gcry_md_get_algo_dlen( gcry_md_get_algo( cfx->mdc_hash ) );
|
2000-07-14 19:34:53 +02:00
|
|
|
byte temp[22];
|
|
|
|
|
|
|
|
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] );
|
|
|
|
|
1999-09-18 12:17:16 +02:00
|
|
|
hash = gcry_md_read( cfx->mdc_hash, 0 );
|
2000-07-14 19:34:53 +02:00
|
|
|
memcpy(temp+2, hash, 20);
|
|
|
|
rc = gcry_cipher_encrypt( cfx->cipher_hd, temp, 22, NULL, 0 );
|
1999-09-18 12:17:16 +02:00
|
|
|
if( rc )
|
|
|
|
log_fatal("encrypt failed: %s\n", gcry_strerror(rc) );
|
|
|
|
gcry_md_close( cfx->mdc_hash ); cfx->mdc_hash = NULL;
|
2000-07-14 19:34:53 +02:00
|
|
|
if( iobuf_write( a, temp, 22 ) )
|
|
|
|
log_error("writing MDC packet failed\n" );
|
1999-05-17 22:03:24 +02:00
|
|
|
}
|
1999-09-18 12:17:16 +02:00
|
|
|
gcry_cipher_close(cfx->cipher_hd);
|
2000-07-14 19:34:53 +02:00
|
|
|
write_status( STATUS_END_ENCRYPTION );
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_DESC ) {
|
|
|
|
*(char**)buf = "cipher_filter";
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|