Tizen 2.1 base
[framework/security/gnupg.git] / g10 / cipher.c
1 /* cipher.c - En-/De-ciphering filter
2  * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
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,
19  * USA.
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
28
29 #include "errors.h"
30 #include "iobuf.h"
31 #include "memory.h"
32 #include "util.h"
33 #include "filter.h"
34 #include "packet.h"
35 #include "options.h"
36 #include "main.h"
37 #include "status.h"
38
39
40 #define MIN_PARTIAL_SIZE 512
41
42
43 static void
44 write_header( cipher_filter_context_t *cfx, IOBUF a )
45 {
46     PACKET pkt;
47     PKT_encrypted ed;
48     byte temp[18];
49     unsigned blocksize;
50     unsigned nprefix;
51
52     blocksize = cipher_get_blocksize( cfx->dek->algo );
53     if( blocksize < 8 || blocksize > 16 )
54         log_fatal("unsupported blocksize %u\n", blocksize );
55
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 );
63         if ( DBG_HASHING )
64             md_start_debug( cfx->mdc_hash, "creatmdc" );
65     }
66
67     {
68         char buf[20];
69         
70         sprintf (buf, "%d %d", ed.mdc_method, cfx->dek->algo);
71         write_status_text (STATUS_BEGIN_ENCRYPTION, buf);
72     }
73
74     init_packet( &pkt );
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");
79     nprefix = blocksize;
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);
96     cfx->header=1;
97 }
98
99
100
101 /****************
102  * This filter is used to en/de-cipher data with a conventional algorithm
103  */
104 int
105 cipher_filter( void *opaque, int control,
106                IOBUF a, byte *buf, size_t *ret_len)
107 {
108     size_t size = *ret_len;
109     cipher_filter_context_t *cfx = opaque;
110     int rc=0;
111
112     if( control == IOBUFCTRL_UNDERFLOW ) { /* decrypt */
113         rc = -1; /* not yet used */
114     }
115     else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */
116         assert(a);
117         if( !cfx->header ) {
118             write_header( cfx, a );
119         }
120         if( cfx->mdc_hash )
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;
125     }
126     else if( control == IOBUFCTRL_FREE ) {
127         if( cfx->mdc_hash ) {
128             byte *hash;
129             int hashlen = md_digest_length( md_get_algo( cfx->mdc_hash ) );
130             byte temp[22];
131
132             assert( hashlen == 20 );
133             /* we must hash the prefix of the MDC packet here */
134             temp[0] = 0xd3;
135             temp[1] = 0x14;
136             md_putc( cfx->mdc_hash, temp[0] );
137             md_putc( cfx->mdc_hash, temp[1] );
138
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" );
146         }
147         cipher_close(cfx->cipher_hd);
148     }
149     else if( control == IOBUFCTRL_DESC ) {
150         *(char**)buf = "cipher_filter";
151     }
152     return rc;
153 }