1997-11-23 16:38:27 +01:00
|
|
|
/* cipher.c - En-/De-ciphering filter
|
1998-02-24 19:50:46 +01:00
|
|
|
* Copyright (C) 1998 Free Software Foundation, Inc.
|
1997-11-23 16:38:27 +01:00
|
|
|
*
|
1998-02-24 19:50:46 +01:00
|
|
|
* This file is part of GNUPG.
|
1997-11-23 16:38:27 +01:00
|
|
|
*
|
1998-02-24 19:50:46 +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-02-24 19:50:46 +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>
|
|
|
|
|
|
|
|
#include "errors.h"
|
|
|
|
#include "iobuf.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "options.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
1997-12-31 13:32:54 +01:00
|
|
|
* This filter is used to en/de-cipher data with a conventinal 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 ) {
|
|
|
|
PACKET pkt;
|
1997-12-01 11:33:23 +01:00
|
|
|
PKT_encrypted ed;
|
1997-11-23 16:38:27 +01:00
|
|
|
byte temp[10];
|
|
|
|
|
|
|
|
memset( &ed, 0, sizeof ed );
|
|
|
|
ed.len = cfx->datalen;
|
|
|
|
init_packet( &pkt );
|
1997-12-01 11:33:23 +01:00
|
|
|
pkt.pkttype = PKT_ENCRYPTED;
|
|
|
|
pkt.pkt.encrypted = &ed;
|
1997-11-23 16:38:27 +01:00
|
|
|
if( build_packet( a, &pkt ))
|
|
|
|
log_bug("build_packet(ENCR_DATA) failed\n");
|
|
|
|
randomize_buffer( temp, 8, 1 );
|
|
|
|
temp[8] = temp[6];
|
|
|
|
temp[9] = temp[7];
|
1998-01-07 21:47:46 +01:00
|
|
|
if( cfx->dek->algo == CIPHER_ALGO_BLOWFISH
|
|
|
|
|| cfx->dek->algo == CIPHER_ALGO_BLOWFISH128 ) {
|
1997-11-23 16:38:27 +01:00
|
|
|
cfx->bf_ctx = m_alloc_secure( sizeof *cfx->bf_ctx );
|
|
|
|
blowfish_setkey( cfx->bf_ctx, cfx->dek->key, cfx->dek->keylen );
|
|
|
|
blowfish_setiv( cfx->bf_ctx, NULL );
|
|
|
|
blowfish_encode_cfb( cfx->bf_ctx, temp, temp, 10);
|
|
|
|
}
|
1998-04-04 22:16:55 +02:00
|
|
|
else if( cfx->dek->algo == CIPHER_ALGO_CAST ) {
|
|
|
|
cfx->cast5_ctx = m_alloc_secure( sizeof *cfx->cast5_ctx );
|
|
|
|
cast5_setkey( cfx->cast5_ctx, cfx->dek->key, cfx->dek->keylen );
|
|
|
|
cast5_setiv( cfx->cast5_ctx, NULL );
|
|
|
|
cast5_encode_cfb( cfx->cast5_ctx, temp, temp, 10);
|
|
|
|
cast5_sync_cfb( cfx->cast5_ctx );
|
|
|
|
}
|
1997-11-23 16:38:27 +01:00
|
|
|
else
|
|
|
|
log_bug("no cipher algo %d\n", cfx->dek->algo);
|
|
|
|
|
|
|
|
iobuf_write(a, temp, 10);
|
|
|
|
cfx->header=1;
|
|
|
|
}
|
|
|
|
|
1998-01-07 21:47:46 +01:00
|
|
|
if( cfx->dek->algo == CIPHER_ALGO_BLOWFISH
|
|
|
|
|| cfx->dek->algo == CIPHER_ALGO_BLOWFISH128 )
|
1997-11-23 16:38:27 +01:00
|
|
|
blowfish_encode_cfb( cfx->bf_ctx, buf, buf, size);
|
1998-04-04 22:16:55 +02:00
|
|
|
else if( cfx->dek->algo == CIPHER_ALGO_CAST )
|
|
|
|
cast5_encode_cfb( cfx->cast5_ctx, buf, buf, size);
|
|
|
|
|
1997-11-23 16:38:27 +01:00
|
|
|
if( iobuf_write( a, buf, size ) )
|
|
|
|
rc = G10ERR_WRITE_FILE;
|
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_FREE ) {
|
1998-01-07 21:47:46 +01:00
|
|
|
if( cfx->dek->algo == CIPHER_ALGO_BLOWFISH
|
|
|
|
|| cfx->dek->algo == CIPHER_ALGO_BLOWFISH128 )
|
1997-11-23 16:38:27 +01:00
|
|
|
m_free(cfx->bf_ctx);
|
1998-04-04 22:16:55 +02:00
|
|
|
else if( cfx->dek->algo == CIPHER_ALGO_CAST )
|
|
|
|
m_free(cfx->cast5_ctx);
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_DESC ) {
|
|
|
|
*(char**)buf = "cipher_filter";
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|