1997-11-23 15:38:27 +00:00
|
|
|
/* cipher.c - En-/De-ciphering filter
|
2002-06-29 13:46:34 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
1997-11-23 15:38:27 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* This file is part of GnuPG.
|
1997-11-23 15:38:27 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-23 15:38:27 +00: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 12:41:40 +00:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-23 15:38:27 +00: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>
|
|
|
|
|
|
|
|
#include "errors.h"
|
|
|
|
#include "iobuf.h"
|
2002-06-29 13:46:34 +00:00
|
|
|
#include "memory.h"
|
1997-11-23 15:38:27 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "options.h"
|
1999-01-09 17:59:58 +00:00
|
|
|
#include "main.h"
|
2000-07-14 17:34:53 +00:00
|
|
|
#include "status.h"
|
1997-11-23 15:38:27 +00:00
|
|
|
|
|
|
|
|
1998-07-02 19:31:46 +00: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 19:35:05 +00:00
|
|
|
byte temp[18];
|
2002-06-29 13:46:34 +00:00
|
|
|
unsigned blocksize;
|
|
|
|
unsigned nprefix;
|
|
|
|
int use_mdc;
|
2000-07-14 17:34:53 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
blocksize = cipher_get_blocksize( cfx->dek->algo );
|
2000-07-14 17:34:53 +00:00
|
|
|
if( blocksize < 8 || blocksize > 16 )
|
|
|
|
log_fatal("unsupported blocksize %u\n", blocksize );
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
use_mdc = cfx->dek->use_mdc;
|
|
|
|
|
2000-07-14 17:34:53 +00:00
|
|
|
if( blocksize != 8 )
|
2002-06-29 13:46:34 +00:00
|
|
|
use_mdc = 1; /* Hack: enable it for all modern ciphers */
|
|
|
|
/* Note: We should remove this hack as soon as a reasonable number of keys
|
|
|
|
are carrying the MDC flag. But always keep the hack for conventional
|
|
|
|
encryption */
|
|
|
|
|
|
|
|
if (opt.force_mdc)
|
|
|
|
use_mdc = 1;
|
|
|
|
|
|
|
|
if( opt.rfc2440 || opt.rfc1991 || opt.disable_mdc )
|
2000-07-14 17:34:53 +00:00
|
|
|
use_mdc = 0; /* override - rfc2440 does not know about MDC */
|
1998-07-02 19:31:46 +00:00
|
|
|
|
|
|
|
memset( &ed, 0, sizeof ed );
|
|
|
|
ed.len = cfx->datalen;
|
2002-06-29 13:46:34 +00:00
|
|
|
ed.extralen = blocksize+2;
|
1998-07-06 10:23:57 +00:00
|
|
|
ed.new_ctb = !ed.len && !opt.rfc1991;
|
1999-05-17 20:03:24 +00:00
|
|
|
if( use_mdc ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
ed.mdc_method = DIGEST_ALGO_SHA1;
|
|
|
|
cfx->mdc_hash = md_open( DIGEST_ALGO_SHA1, 0 );
|
2000-07-14 17:34:53 +00:00
|
|
|
if ( DBG_HASHING )
|
2002-06-29 13:46:34 +00:00
|
|
|
md_start_debug( cfx->mdc_hash, "creatmdc" );
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char buf[20];
|
|
|
|
|
|
|
|
sprintf (buf, "%d %d", ed.mdc_method, cfx->dek->algo);
|
|
|
|
write_status_text (STATUS_BEGIN_ENCRYPTION, buf);
|
1999-05-17 20:03:24 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
|
1998-07-02 19:31:46 +00:00
|
|
|
init_packet( &pkt );
|
1999-05-17 20:03:24 +00:00
|
|
|
pkt.pkttype = use_mdc? PKT_ENCRYPTED_MDC : PKT_ENCRYPTED;
|
1998-07-02 19:31:46 +00:00
|
|
|
pkt.pkt.encrypted = &ed;
|
|
|
|
if( build_packet( a, &pkt ))
|
|
|
|
log_bug("build_packet(ENCR_DATA) failed\n");
|
1999-04-09 10:34:44 +00:00
|
|
|
nprefix = blocksize;
|
2002-06-29 13:46:34 +00:00
|
|
|
randomize_buffer( temp, nprefix, 1 );
|
1999-04-07 18:58:34 +00:00
|
|
|
temp[nprefix] = temp[nprefix-2];
|
|
|
|
temp[nprefix+1] = temp[nprefix-1];
|
1998-12-10 19:20:47 +00:00
|
|
|
print_cipher_algo_note( cfx->dek->algo );
|
2002-06-29 13:46:34 +00:00
|
|
|
cfx->cipher_hd = cipher_open( cfx->dek->algo,
|
|
|
|
use_mdc? CIPHER_MODE_CFB
|
|
|
|
: CIPHER_MODE_AUTO_CFB, 1 );
|
1999-04-18 08:18:52 +00:00
|
|
|
/* log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/
|
2002-06-29 13:46:34 +00:00
|
|
|
cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen );
|
|
|
|
cipher_setiv( cfx->cipher_hd, NULL, 0 );
|
1999-04-18 08:18:52 +00:00
|
|
|
/* log_hexdump( "prefix", temp, nprefix+2 ); */
|
2002-06-29 13:46:34 +00:00
|
|
|
if( cfx->mdc_hash ) /* hash the "IV" */
|
|
|
|
md_write( cfx->mdc_hash, temp, nprefix+2 );
|
|
|
|
cipher_encrypt( cfx->cipher_hd, temp, temp, nprefix+2);
|
|
|
|
cipher_sync( cfx->cipher_hd );
|
1999-04-07 18:58:34 +00:00
|
|
|
iobuf_write(a, temp, nprefix+2);
|
1998-07-02 19:31:46 +00:00
|
|
|
cfx->header=1;
|
|
|
|
}
|
1997-11-23 15:38:27 +00:00
|
|
|
|
|
|
|
|
1999-05-17 20:03:24 +00:00
|
|
|
|
1997-11-23 15:38:27 +00:00
|
|
|
/****************
|
1998-04-14 17:51:16 +00:00
|
|
|
* This filter is used to en/de-cipher data with a conventional algorithm
|
1997-11-23 15:38:27 +00: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 15:27:29 +00:00
|
|
|
if( control == IOBUFCTRL_UNDERFLOW ) { /* decrypt */
|
1998-04-02 10:30:03 +00:00
|
|
|
rc = -1; /* not yet used */
|
1997-11-23 15:38:27 +00:00
|
|
|
}
|
1998-03-19 15:27:29 +00:00
|
|
|
else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */
|
1997-11-23 15:38:27 +00:00
|
|
|
assert(a);
|
|
|
|
if( !cfx->header ) {
|
1998-07-02 19:31:46 +00:00
|
|
|
write_header( cfx, a );
|
1997-11-23 15:38:27 +00:00
|
|
|
}
|
1999-05-17 20:03:24 +00:00
|
|
|
if( cfx->mdc_hash )
|
2002-06-29 13:46:34 +00:00
|
|
|
md_write( cfx->mdc_hash, buf, size );
|
|
|
|
cipher_encrypt( cfx->cipher_hd, buf, buf, size);
|
1997-11-23 15:38:27 +00:00
|
|
|
if( iobuf_write( a, buf, size ) )
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = G10ERR_WRITE_FILE;
|
1997-11-23 15:38:27 +00:00
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_FREE ) {
|
1999-05-17 20:03:24 +00:00
|
|
|
if( cfx->mdc_hash ) {
|
|
|
|
byte *hash;
|
2002-06-29 13:46:34 +00:00
|
|
|
int hashlen = md_digest_length( md_get_algo( cfx->mdc_hash ) );
|
2000-07-14 17:34:53 +00:00
|
|
|
byte temp[22];
|
|
|
|
|
|
|
|
assert( hashlen == 20 );
|
|
|
|
/* we must hash the prefix of the MDC packet here */
|
|
|
|
temp[0] = 0xd3;
|
|
|
|
temp[1] = 0x14;
|
2002-06-29 13:46:34 +00:00
|
|
|
md_putc( cfx->mdc_hash, temp[0] );
|
|
|
|
md_putc( cfx->mdc_hash, temp[1] );
|
2000-07-14 17:34:53 +00:00
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
md_final( cfx->mdc_hash );
|
|
|
|
hash = md_read( cfx->mdc_hash, 0 );
|
2000-07-14 17:34:53 +00:00
|
|
|
memcpy(temp+2, hash, 20);
|
2002-06-29 13:46:34 +00:00
|
|
|
cipher_encrypt( cfx->cipher_hd, temp, temp, 22 );
|
|
|
|
md_close( cfx->mdc_hash ); cfx->mdc_hash = NULL;
|
2000-07-14 17:34:53 +00:00
|
|
|
if( iobuf_write( a, temp, 22 ) )
|
|
|
|
log_error("writing MDC packet failed\n" );
|
1999-05-17 20:03:24 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
cipher_close(cfx->cipher_hd);
|
1997-11-23 15:38:27 +00:00
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_DESC ) {
|
|
|
|
*(char**)buf = "cipher_filter";
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|