1 /* cipher.c - En-/De-ciphering filter
2 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
40 #define MIN_PARTIAL_SIZE 512
44 write_header( cipher_filter_context_t *cfx, IOBUF a )
52 blocksize = cipher_get_blocksize( cfx->dek->algo );
53 if( blocksize < 8 || blocksize > 16 )
54 log_fatal("unsupported blocksize %u\n", blocksize );
56 memset( &ed, 0, sizeof ed );
57 ed.len = cfx->datalen;
58 ed.extralen = blocksize+2;
59 ed.new_ctb = !ed.len && !RFC1991;
60 if( cfx->dek->use_mdc ) {
61 ed.mdc_method = DIGEST_ALGO_SHA1;
62 cfx->mdc_hash = md_open( DIGEST_ALGO_SHA1, 0 );
64 md_start_debug( cfx->mdc_hash, "creatmdc" );
70 sprintf (buf, "%d %d", ed.mdc_method, cfx->dek->algo);
71 write_status_text (STATUS_BEGIN_ENCRYPTION, buf);
75 pkt.pkttype = cfx->dek->use_mdc? PKT_ENCRYPTED_MDC : PKT_ENCRYPTED;
76 pkt.pkt.encrypted = &ed;
77 if( build_packet( a, &pkt ))
78 log_bug("build_packet(ENCR_DATA) failed\n");
80 randomize_buffer( temp, nprefix, 1 );
81 temp[nprefix] = temp[nprefix-2];
82 temp[nprefix+1] = temp[nprefix-1];
83 print_cipher_algo_note( cfx->dek->algo );
84 cfx->cipher_hd = cipher_open( cfx->dek->algo,
85 cfx->dek->use_mdc? CIPHER_MODE_CFB
86 : CIPHER_MODE_AUTO_CFB, 1 );
87 /* log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/
88 cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen );
89 cipher_setiv( cfx->cipher_hd, NULL, 0 );
90 /* log_hexdump( "prefix", temp, nprefix+2 ); */
91 if( cfx->mdc_hash ) /* hash the "IV" */
92 md_write( cfx->mdc_hash, temp, nprefix+2 );
93 cipher_encrypt( cfx->cipher_hd, temp, temp, nprefix+2);
94 cipher_sync( cfx->cipher_hd );
95 iobuf_write(a, temp, nprefix+2);
102 * This filter is used to en/de-cipher data with a conventional algorithm
105 cipher_filter( void *opaque, int control,
106 IOBUF a, byte *buf, size_t *ret_len)
108 size_t size = *ret_len;
109 cipher_filter_context_t *cfx = opaque;
112 if( control == IOBUFCTRL_UNDERFLOW ) { /* decrypt */
113 rc = -1; /* not yet used */
115 else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */
118 write_header( cfx, a );
121 md_write( cfx->mdc_hash, buf, size );
122 cipher_encrypt( cfx->cipher_hd, buf, buf, size);
123 if( iobuf_write( a, buf, size ) )
124 rc = G10ERR_WRITE_FILE;
126 else if( control == IOBUFCTRL_FREE ) {
127 if( cfx->mdc_hash ) {
129 int hashlen = md_digest_length( md_get_algo( cfx->mdc_hash ) );
132 assert( hashlen == 20 );
133 /* we must hash the prefix of the MDC packet here */
136 md_putc( cfx->mdc_hash, temp[0] );
137 md_putc( cfx->mdc_hash, temp[1] );
139 md_final( cfx->mdc_hash );
140 hash = md_read( cfx->mdc_hash, 0 );
141 memcpy(temp+2, hash, 20);
142 cipher_encrypt( cfx->cipher_hd, temp, temp, 22 );
143 md_close( cfx->mdc_hash ); cfx->mdc_hash = NULL;
144 if( iobuf_write( a, temp, 22 ) )
145 log_error("writing MDC packet failed\n" );
147 cipher_close(cfx->cipher_hd);
149 else if( control == IOBUFCTRL_DESC ) {
150 *(char**)buf = "cipher_filter";