summaryrefslogtreecommitdiffstats
path: root/g10/encr-data.c
blob: 1dd6e0e7723d5c9e4bc710190cd7551c71cef4f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* encr-data.c -  process an encrypted data packet
 *	Copyright (C) 1998, 1999 Free Software Foundation, Inc.
 *
 * 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
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * 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 <gcrypt.h>
#include "util.h"
#include "memory.h"
#include "packet.h"
#include "mpi.h"
#include "cipher.h"
#include "options.h"
#include "i18n.h"


static int decode_filter( void *opaque, int control, IOBUF a,
					byte *buf, size_t *ret_len);
static int mdc_decode_filter( void *opaque, int control, IOBUF a,
					byte *buf, size_t *ret_len);

typedef struct {
    GCRY_CIPHER_HD cipher_hd;
    GCRY_MD_HD mdc_hash;
    char defer[20];
    int  defer_filled;
    int  eof_seen;
} decode_filter_ctx_t;


/****************
 * Decrypt the data, specified by ED with the key DEK.
 */
int
decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
{
    decode_filter_ctx_t dfx;
    byte *p;
    int rc=0, c, i;
    byte temp[32];
    int blocksize;
    unsigned nprefix;

    memset( &dfx, 0, sizeof dfx );
    if( gcry_cipher_test_algo( dek->algo ) ) {
	if( opt.verbose )
	    log_info(_("encrypted with unknown algorithm %d\n"), dek->algo );
	rc = G10ERR_CIPHER_ALGO;
	goto leave;
    }
    if( opt.verbose )
	log_info(_("%s encrypted data\n"), gcry_cipher_algo_name( dek->algo ) );

    blocksize = gcry_cipher_get_algo_blklen( dek->algo );
    if( blocksize < 1 || blocksize > 16 )
	log_fatal("unsupported blocksize %u\n", blocksize );
    nprefix = blocksize;
    if( ed->len && ed->len < (nprefix+2) )
	BUG();

    if( ed->mdc_method ) {
	dfx.mdc_hash = gcry_md_open( ed->mdc_method, 0 );
	if( !dfx.mdc_hash )
	    BUG();
    }
    if( !(dfx.cipher_hd = gcry_cipher_open( dek->algo,
				      GCRY_CIPHER_MODE_CFB,
				      GCRY_CIPHER_SECURE
				      | (dek->algo >= 100 ?
					   0 : GCRY_CIPHER_ENABLE_SYNC) ))
				    ) {
	/* we should never get an error here cause we already checked, that
	 * the algorithm is available. What about a flag to let the function
	 * die in this case? */
	BUG();
    }


/* log_hexdump( "thekey", dek->key, dek->keylen );*/
    rc = gcry_cipher_setkey( dfx.cipher_hd, dek->key, dek->keylen );
    if( rc == GCRYERR_WEAK_KEY ) {
	log_info(_("WARNING: message was encrypted with "
		    "a weak key in the symmetric cipher.\n"));
	rc = 0;
    }
    else if( rc ) {
	log_error("key setup failed: %s\n", gcry_strerror(rc) );
	rc = map_gcry_rc(rc);
	goto leave;
    }

    gcry_cipher_setiv( dfx.cipher_hd, NULL, 0 );

    if( ed->len ) {
	for(i=0; i < (nprefix+2) && ed->len; i++, ed->len-- ) {
	    if( (c=iobuf_get(ed->buf)) == -1 )
		break;
	    else
		temp[i] = c;
	}
    }
    else {
	for(i=0; i < (nprefix+2); i++ )
	    if( (c=iobuf_get(ed->buf)) == -1 )
		break;
	    else
		temp[i] = c;
    }
    gcry_cipher_decrypt( dfx.cipher_hd, temp, nprefix+2, NULL, 0 );
    if( dfx.mdc_hash )
	gcry_md_write( dfx.mdc_hash, temp, nprefix+2 );
    gcry_cipher_sync( dfx.cipher_hd );
    p = temp;
/* log_hexdump( "prefix", temp, nprefix+2 ); */
    if( p[nprefix-2] != p[nprefix] || p[nprefix-1] != p[nprefix+1] ) {
	rc = G10ERR_BAD_KEY;
	goto leave;
    }
    if( ed->mdc_method )
	iobuf_push_filter( ed->buf, mdc_decode_filter, &dfx );
    else
	iobuf_push_filter( ed->buf, decode_filter, &dfx );
    proc_packets( procctx, ed->buf);
    ed->buf = NULL;
    if( ed->mdc_method && dfx.eof_seen == 2 )
	rc = G10ERR_INVALID_PACKET;
    else if( ed->mdc_method ) { /* check the mdc */
	int datalen = gcry_md_get_algo_dlen( ed->mdc_method );
	if( datalen != 20
	    || memcmp(gcry_md_read( dfx.mdc_hash, 0 ), dfx.defer, datalen) )
	    rc = G10ERR_BAD_SIGN;
	log_hexdump("MDC calculated:", gcry_md_read( dfx.mdc_hash, 0), datalen);
	log_hexdump("MDC message   :", dfx.defer, 20);
    }
  leave:
    gcry_cipher_close(dfx.cipher_hd);
    gcry_md_close( dfx.mdc_hash );
    return rc;
}


/* I think we should merge this with cipher_filter */
static int
mdc_decode_filter( void *opaque, int control, IOBUF a,
					      byte *buf, size_t *ret_len)
{
    decode_filter_ctx_t *dfx = opaque;
    size_t n, size = *ret_len;
    int rc = 0;
    int c;

    if( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen ) {
	*ret_len = 0;
	rc = -1;
    }
    else if( control == IOBUFCTRL_UNDERFLOW ) {
	assert(a);
	assert( size > 40 );

	/* get at least 20 bytes and put it somewhere ahead in the buffer */
	for(n=20; n < 40 ; n++ ) {
	    if( (c = iobuf_get(a)) == -1 )
		break;
	    buf[n] = c;
	}
	if( n == 40 ) {
	    /* we have enough stuff - flush the deferred stuff */
	    /* (we have asserted that the buffer is large enough */
	    if( !dfx->defer_filled ) /* the first time */
		memcpy(buf, buf+20, 20 );
	    else
		memcpy(buf, dfx->defer, 20 );
	    /* now fill up */
	    for(; n < size; n++ ) {
		if( (c = iobuf_get(a)) == -1 )
		    break;
		buf[n] = c;
	    }
	    /* move the last 20 bytes back to the defer buffer */
	    /* (okay, we are wasting 20 bytes of supplied buffer) */
	    n -= 20;
	    memcpy( dfx->defer, buf+n, 20 );
	    dfx->defer_filled = 1;
	}
	else if( !dfx->defer_filled ) { /* eof seen buf empty defer */
	    /* this is very bad because there is an incomplete hash */
	    n -= 20;
	    memcpy(buf, buf+20, n );
	    dfx->eof_seen = 2; /* eof with incomplete hash */
	}
	else { /* eof seen */
	    memcpy(buf, dfx->defer, 20 );
	    n -= 20;
	    memcpy( dfx->defer, buf+n, 20 );
	    dfx->eof_seen = 1; /* normal eof */
	}

	if( n ) {
	    gcry_cipher_decrypt( dfx->cipher_hd, buf, n, NULL, 0);
	    gcry_md_write( dfx->mdc_hash, buf, n );
	}
	else {
	    assert( dfx->eof_seen );
	    rc = -1; /* eof */
	}
	*ret_len = n;
    }
    else if( control == IOBUFCTRL_DESC ) {
	*(char**)buf = "mdc_decode_filter";
    }
    return rc;
}

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;

    if( control == IOBUFCTRL_UNDERFLOW ) {
	assert(a);
	n = iobuf_read( a, buf, size );
	if( n == -1 ) n = 0;
	if( n )
	    gcry_cipher_decrypt( fc->cipher_hd, buf, n, NULL, 0);
	else
	    rc = -1; /* eof */
	*ret_len = n;
    }
    else if( control == IOBUFCTRL_DESC ) {
	*(char**)buf = "decode_filter";
    }
    return rc;
}