* gpg.texi (GPG Configuration Options): Make http_proxy option
[gnupg.git] / g10 / cipher.c
blob802c019058b101bb4149fc649ce029d81fe470a1
1 /* cipher.c - En-/De-ciphering filter
2 * Copyright (C) 1998, 1999, 2000, 2001, 2003,
3 * 2006 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <assert.h>
30 #include "gpg.h"
31 #include "errors.h"
32 #include "iobuf.h"
33 #include "util.h"
34 #include "filter.h"
35 #include "packet.h"
36 #include "options.h"
37 #include "main.h"
38 #include "status.h"
41 #define MIN_PARTIAL_SIZE 512
44 static void
45 write_header( cipher_filter_context_t *cfx, IOBUF a )
47 gcry_error_t err;
48 PACKET pkt;
49 PKT_encrypted ed;
50 byte temp[18];
51 unsigned int blocksize;
52 unsigned int nprefix;
54 blocksize = gcry_cipher_get_algo_blklen (cfx->dek->algo);
55 if ( blocksize < 8 || blocksize > 16 )
56 log_fatal("unsupported blocksize %u\n", blocksize );
58 memset( &ed, 0, sizeof ed );
59 ed.len = cfx->datalen;
60 ed.extralen = blocksize+2;
61 ed.new_ctb = !ed.len && !RFC1991;
62 if( cfx->dek->use_mdc ) {
63 ed.mdc_method = DIGEST_ALGO_SHA1;
64 gcry_md_open (&cfx->mdc_hash, DIGEST_ALGO_SHA1, 0);
65 if ( DBG_HASHING )
66 gcry_md_start_debug (cfx->mdc_hash, "creatmdc");
70 char buf[20];
72 sprintf (buf, "%d %d", ed.mdc_method, cfx->dek->algo);
73 write_status_text (STATUS_BEGIN_ENCRYPTION, buf);
76 init_packet( &pkt );
77 pkt.pkttype = cfx->dek->use_mdc? PKT_ENCRYPTED_MDC : PKT_ENCRYPTED;
78 pkt.pkt.encrypted = &ed;
79 if( build_packet( a, &pkt ))
80 log_bug("build_packet(ENCR_DATA) failed\n");
81 nprefix = blocksize;
82 gcry_randomize (temp, nprefix, GCRY_STRONG_RANDOM );
83 temp[nprefix] = temp[nprefix-2];
84 temp[nprefix+1] = temp[nprefix-1];
85 print_cipher_algo_note( cfx->dek->algo );
86 err = gcry_cipher_open (&cfx->cipher_hd,
87 cfx->dek->algo,
88 GCRY_CIPHER_MODE_CFB,
89 (GCRY_CIPHER_SECURE
90 | ((cfx->dek->use_mdc || cfx->dek->algo >= 100)?
91 0 : GCRY_CIPHER_ENABLE_SYNC)));
92 if (err) {
93 /* We should never get an error here cause we already checked,
94 * that the algorithm is available. */
95 BUG();
99 /* log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/
100 gcry_cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen );
101 gcry_cipher_setiv( cfx->cipher_hd, NULL, 0 );
102 /* log_hexdump( "prefix", temp, nprefix+2 ); */
103 if (cfx->mdc_hash) /* Hash the "IV". */
104 gcry_md_write (cfx->mdc_hash, temp, nprefix+2 );
105 gcry_cipher_encrypt (cfx->cipher_hd, temp, nprefix+2, NULL, 0);
106 gcry_cipher_sync (cfx->cipher_hd);
107 iobuf_write(a, temp, nprefix+2);
108 cfx->header=1;
113 /****************
114 * This filter is used to en/de-cipher data with a conventional algorithm
117 cipher_filter( void *opaque, int control,
118 IOBUF a, byte *buf, size_t *ret_len)
120 size_t size = *ret_len;
121 cipher_filter_context_t *cfx = opaque;
122 int rc=0;
124 if( control == IOBUFCTRL_UNDERFLOW ) { /* decrypt */
125 rc = -1; /* not yet used */
127 else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */
128 assert(a);
129 if( !cfx->header ) {
130 write_header( cfx, a );
132 if (cfx->mdc_hash)
133 gcry_md_write (cfx->mdc_hash, buf, size);
134 gcry_cipher_encrypt (cfx->cipher_hd, buf, size, NULL, 0);
135 rc = iobuf_write( a, buf, size );
137 else if( control == IOBUFCTRL_FREE ) {
138 if( cfx->mdc_hash ) {
139 byte *hash;
140 int hashlen = gcry_md_get_algo_dlen (gcry_md_get_algo
141 (cfx->mdc_hash));
142 byte temp[22];
144 assert( hashlen == 20 );
145 /* We must hash the prefix of the MDC packet here. */
146 temp[0] = 0xd3;
147 temp[1] = 0x14;
148 gcry_md_putc (cfx->mdc_hash, temp[0]);
149 gcry_md_putc (cfx->mdc_hash, temp[1]);
151 gcry_md_final (cfx->mdc_hash);
152 hash = gcry_md_read (cfx->mdc_hash, 0);
153 memcpy(temp+2, hash, 20);
154 gcry_cipher_encrypt (cfx->cipher_hd, temp, 22, NULL, 0);
155 gcry_md_close (cfx->mdc_hash); cfx->mdc_hash = NULL;
156 if( iobuf_write( a, temp, 22 ) )
157 log_error("writing MDC packet failed\n" );
159 gcry_cipher_close (cfx->cipher_hd);
161 else if( control == IOBUFCTRL_DESC ) {
162 *(char**)buf = "cipher_filter";
164 return rc;