Line data Source code
1 : /* cipher.c - En-/De-ciphering filter
2 : * Copyright (C) 1998, 1999, 2000, 2001, 2003,
3 : * 2006, 2009 Free Software Foundation, Inc.
4 : *
5 : * This file is part of GnuPG.
6 : *
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 3 of the License, or
10 : * (at your option) any later version.
11 : *
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.
16 : *
17 : * You should have received a copy of the GNU General Public License
18 : * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include <config.h>
22 : #include <stdio.h>
23 : #include <stdlib.h>
24 : #include <string.h>
25 : #include <errno.h>
26 : #include <assert.h>
27 :
28 : #include "gpg.h"
29 : #include "status.h"
30 : #include "iobuf.h"
31 : #include "util.h"
32 : #include "filter.h"
33 : #include "packet.h"
34 : #include "options.h"
35 : #include "main.h"
36 : #include "status.h"
37 :
38 :
39 : #define MIN_PARTIAL_SIZE 512
40 :
41 :
42 : static void
43 454 : write_header( cipher_filter_context_t *cfx, IOBUF a )
44 : {
45 : gcry_error_t err;
46 : PACKET pkt;
47 : PKT_encrypted ed;
48 : byte temp[18];
49 : unsigned int blocksize;
50 : unsigned int nprefix;
51 :
52 454 : blocksize = openpgp_cipher_get_algo_blklen (cfx->dek->algo);
53 454 : if ( blocksize < 8 || blocksize > 16 )
54 0 : log_fatal("unsupported blocksize %u\n", blocksize );
55 :
56 454 : memset( &ed, 0, sizeof ed );
57 454 : ed.len = cfx->datalen;
58 454 : ed.extralen = blocksize+2;
59 454 : ed.new_ctb = !ed.len;
60 454 : if( cfx->dek->use_mdc ) {
61 402 : ed.mdc_method = DIGEST_ALGO_SHA1;
62 402 : gcry_md_open (&cfx->mdc_hash, DIGEST_ALGO_SHA1, 0);
63 402 : if ( DBG_HASHING )
64 0 : gcry_md_debug (cfx->mdc_hash, "creatmdc");
65 : }
66 :
67 : {
68 : char buf[20];
69 :
70 454 : sprintf (buf, "%d %d", ed.mdc_method, cfx->dek->algo);
71 454 : write_status_text (STATUS_BEGIN_ENCRYPTION, buf);
72 : }
73 :
74 454 : init_packet( &pkt );
75 454 : pkt.pkttype = cfx->dek->use_mdc? PKT_ENCRYPTED_MDC : PKT_ENCRYPTED;
76 454 : pkt.pkt.encrypted = &ed;
77 454 : if( build_packet( a, &pkt ))
78 0 : log_bug("build_packet(ENCR_DATA) failed\n");
79 454 : nprefix = blocksize;
80 454 : gcry_randomize (temp, nprefix, GCRY_STRONG_RANDOM );
81 454 : temp[nprefix] = temp[nprefix-2];
82 454 : temp[nprefix+1] = temp[nprefix-1];
83 454 : print_cipher_algo_note( cfx->dek->algo );
84 454 : err = openpgp_cipher_open (&cfx->cipher_hd,
85 : cfx->dek->algo,
86 : GCRY_CIPHER_MODE_CFB,
87 : (GCRY_CIPHER_SECURE
88 : | ((cfx->dek->use_mdc || cfx->dek->algo >= 100)?
89 : 0 : GCRY_CIPHER_ENABLE_SYNC)));
90 454 : if (err) {
91 : /* We should never get an error here cause we already checked,
92 : * that the algorithm is available. */
93 0 : BUG();
94 : }
95 :
96 :
97 : /* log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/
98 454 : gcry_cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen );
99 454 : gcry_cipher_setiv( cfx->cipher_hd, NULL, 0 );
100 : /* log_hexdump( "prefix", temp, nprefix+2 ); */
101 454 : if (cfx->mdc_hash) /* Hash the "IV". */
102 402 : gcry_md_write (cfx->mdc_hash, temp, nprefix+2 );
103 454 : gcry_cipher_encrypt (cfx->cipher_hd, temp, nprefix+2, NULL, 0);
104 454 : gcry_cipher_sync (cfx->cipher_hd);
105 454 : iobuf_write(a, temp, nprefix+2);
106 454 : cfx->header=1;
107 454 : }
108 :
109 :
110 :
111 : /****************
112 : * This filter is used to en/de-cipher data with a conventional algorithm
113 : */
114 : int
115 2378 : cipher_filter( void *opaque, int control,
116 : IOBUF a, byte *buf, size_t *ret_len)
117 : {
118 2378 : size_t size = *ret_len;
119 2378 : cipher_filter_context_t *cfx = opaque;
120 2378 : int rc=0;
121 :
122 2378 : if( control == IOBUFCTRL_UNDERFLOW ) { /* decrypt */
123 0 : rc = -1; /* not yet used */
124 : }
125 2378 : else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */
126 1470 : assert(a);
127 1470 : if( !cfx->header ) {
128 454 : write_header( cfx, a );
129 : }
130 1470 : if (cfx->mdc_hash)
131 1261 : gcry_md_write (cfx->mdc_hash, buf, size);
132 1470 : gcry_cipher_encrypt (cfx->cipher_hd, buf, size, NULL, 0);
133 1470 : rc = iobuf_write( a, buf, size );
134 : }
135 908 : else if( control == IOBUFCTRL_FREE ) {
136 454 : if( cfx->mdc_hash ) {
137 : byte *hash;
138 402 : int hashlen = gcry_md_get_algo_dlen (gcry_md_get_algo
139 : (cfx->mdc_hash));
140 : byte temp[22];
141 :
142 402 : assert( hashlen == 20 );
143 : /* We must hash the prefix of the MDC packet here. */
144 402 : temp[0] = 0xd3;
145 402 : temp[1] = 0x14;
146 402 : gcry_md_putc (cfx->mdc_hash, temp[0]);
147 402 : gcry_md_putc (cfx->mdc_hash, temp[1]);
148 :
149 402 : gcry_md_final (cfx->mdc_hash);
150 402 : hash = gcry_md_read (cfx->mdc_hash, 0);
151 402 : memcpy(temp+2, hash, 20);
152 402 : gcry_cipher_encrypt (cfx->cipher_hd, temp, 22, NULL, 0);
153 402 : gcry_md_close (cfx->mdc_hash); cfx->mdc_hash = NULL;
154 402 : if( iobuf_write( a, temp, 22 ) )
155 0 : log_error("writing MDC packet failed\n" );
156 : }
157 454 : gcry_cipher_close (cfx->cipher_hd);
158 : }
159 454 : else if( control == IOBUFCTRL_DESC ) {
160 0 : *(char**)buf = "cipher_filter";
161 : }
162 2378 : return rc;
163 : }
|