1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-11-10 21:38:50 +01:00
gnupg/g10/encr-data.c

135 lines
3.5 KiB
C
Raw Normal View History

1997-11-18 15:06:00 +01:00
/* encr-data.c - process an encrypted data packet
1998-02-24 19:50:46 +01:00
* Copyright (C) 1998 Free Software Foundation, Inc.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +01:00
* This file is part of GNUPG.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +01:00
* GNUPG is free software; you can redistribute it and/or modify
1997-11-18 15:06:00 +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-18 15:06:00 +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 <assert.h>
#include "util.h"
#include "memory.h"
#include "packet.h"
#include "mpi.h"
#include "cipher.h"
1998-04-04 22:16:55 +02:00
#include "options.h"
1997-11-18 15:06:00 +01:00
static int decode_filter( void *opaque, int control, IOBUF a,
byte *buf, size_t *ret_len);
typedef struct {
1998-04-07 20:16:10 +02:00
CIPHER_HANDLE cipher_hd;
1997-11-18 15:06:00 +01:00
} decode_filter_ctx_t;
/****************
* Decrypt the data, specified by ED with the key DEK.
*/
int
1997-12-01 11:33:23 +01:00
decrypt_data( PKT_encrypted *ed, DEK *dek )
1997-11-18 15:06:00 +01:00
{
decode_filter_ctx_t dfx;
byte *p;
1998-04-07 20:16:10 +02:00
int rc, c, i;
1998-07-29 21:35:05 +02:00
byte temp[32];
unsigned blocksize;
1997-11-18 15:06:00 +01:00
1998-04-04 22:16:55 +02:00
if( opt.verbose ) {
const char *s = cipher_algo_to_string( dek->algo );
if( s )
log_info("%s encrypted data\n", s );
else
log_info("encrypted with unknown algorithm %d\n", dek->algo );
}
1998-04-07 20:16:10 +02:00
if( (rc=check_cipher_algo(dek->algo)) )
return rc;
1998-07-29 21:35:05 +02:00
blocksize = cipher_get_blocksize(dek->algo);
if( !blocksize || blocksize > 16 )
log_fatal("unsupported blocksize %u\n", blocksize );
if( ed->len && ed->len < (blocksize+2) )
1997-11-18 15:06:00 +01:00
log_bug("Nanu\n"); /* oops: found a bug */
1998-04-07 20:16:10 +02:00
dfx.cipher_hd = cipher_open( dek->algo, CIPHER_MODE_AUTO_CFB, 1 );
cipher_setkey( dfx.cipher_hd, dek->key, dek->keylen );
cipher_setiv( dfx.cipher_hd, NULL );
1997-11-18 15:06:00 +01:00
if( ed->len ) {
iobuf_set_limit( ed->buf, ed->len );
1998-07-29 21:35:05 +02:00
for(i=0; i < (blocksize+2) && ed->len; i++, ed->len-- )
1997-11-18 15:06:00 +01:00
temp[i] = iobuf_get(ed->buf);
}
else {
1998-07-29 21:35:05 +02:00
for(i=0; i < (blocksize+2); i++ )
1997-11-18 15:06:00 +01:00
if( (c=iobuf_get(ed->buf)) == -1 )
break;
else
temp[i] = c;
}
1998-07-29 21:35:05 +02:00
cipher_decrypt( dfx.cipher_hd, temp, temp, blocksize+2);
1998-04-07 20:16:10 +02:00
cipher_sync( dfx.cipher_hd );
1997-11-18 15:06:00 +01:00
p = temp;
1998-07-29 21:35:05 +02:00
if( p[blocksize-2] != p[blocksize] || p[blocksize-1] != p[blocksize+1] ) {
1998-04-07 20:16:10 +02:00
cipher_close(dfx.cipher_hd);
1997-11-18 15:06:00 +01:00
return G10ERR_BAD_KEY;
}
iobuf_push_filter( ed->buf, decode_filter, &dfx );
proc_packets(ed->buf);
iobuf_pop_filter( ed->buf, decode_filter, &dfx );
if( ed->len )
iobuf_set_limit( ed->buf, 0 ); /* disable the readlimit */
else
iobuf_clear_eof( ed->buf );
ed->buf = NULL;
1998-04-07 20:16:10 +02:00
cipher_close(dfx.cipher_hd);
1997-11-18 15:06:00 +01:00
return 0;
}
static int
decode_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len)
{
decode_filter_ctx_t *fc = opaque;
size_t n, size = *ret_len;
int rc = 0;
int c;
if( control == IOBUFCTRL_UNDERFLOW ) {
assert(a);
for(n=0; n < size; n++ ) {
if( (c = iobuf_get(a)) == -1 )
break;
buf[n] = c;
}
1998-04-07 20:16:10 +02:00
if( n )
cipher_decrypt( fc->cipher_hd, buf, buf, n);
1997-11-18 15:06:00 +01:00
else
rc = -1; /* eof */
*ret_len = n;
}
else if( control == IOBUFCTRL_DESC ) {
*(char**)buf = "decode_filter";
}
return rc;
}