1 /* cipher.c - En-/De-ciphering filter
2 * Copyright (C) 1998, 1999, 2000, 2001, 2003,
3 * 2006, 2009 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 3 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, see <https://www.gnu.org/licenses/>.
38 #define MIN_PARTIAL_SIZE 512
42 write_header( cipher_filter_context_t *cfx, IOBUF a )
48 unsigned int blocksize;
51 blocksize = openpgp_cipher_get_algo_blklen (cfx->dek->algo);
52 if ( blocksize < 8 || blocksize > 16 )
53 log_fatal("unsupported blocksize %u\n", blocksize );
55 memset( &ed, 0, sizeof ed );
56 ed.len = cfx->datalen;
57 ed.extralen = blocksize+2;
59 if( cfx->dek->use_mdc ) {
60 ed.mdc_method = DIGEST_ALGO_SHA1;
61 gcry_md_open (&cfx->mdc_hash, DIGEST_ALGO_SHA1, 0);
63 gcry_md_debug (cfx->mdc_hash, "creatmdc");
69 sprintf (buf, "%d %d", ed.mdc_method, cfx->dek->algo);
70 write_status_text (STATUS_BEGIN_ENCRYPTION, buf);
74 pkt.pkttype = cfx->dek->use_mdc? PKT_ENCRYPTED_MDC : PKT_ENCRYPTED;
75 pkt.pkt.encrypted = &ed;
76 if( build_packet( a, &pkt ))
77 log_bug("build_packet(ENCR_DATA) failed\n");
79 gcry_randomize (temp, nprefix, GCRY_STRONG_RANDOM );
80 temp[nprefix] = temp[nprefix-2];
81 temp[nprefix+1] = temp[nprefix-1];
82 print_cipher_algo_note( cfx->dek->algo );
83 err = openpgp_cipher_open (&cfx->cipher_hd,
87 | ((cfx->dek->use_mdc || cfx->dek->algo >= 100)?
88 0 : GCRY_CIPHER_ENABLE_SYNC)));
90 /* We should never get an error here cause we already checked,
91 * that the algorithm is available. */
96 /* log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/
97 gcry_cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen );
98 gcry_cipher_setiv( cfx->cipher_hd, NULL, 0 );
99 /* log_hexdump( "prefix", temp, nprefix+2 ); */
100 if (cfx->mdc_hash) /* Hash the "IV". */
101 gcry_md_write (cfx->mdc_hash, temp, nprefix+2 );
102 gcry_cipher_encrypt (cfx->cipher_hd, temp, nprefix+2, NULL, 0);
103 gcry_cipher_sync (cfx->cipher_hd);
104 iobuf_write(a, temp, nprefix+2);
111 * This filter is used to en/de-cipher data with a conventional algorithm
114 cipher_filter( void *opaque, int control,
115 IOBUF a, byte *buf, size_t *ret_len)
117 size_t size = *ret_len;
118 cipher_filter_context_t *cfx = opaque;
121 if( control == IOBUFCTRL_UNDERFLOW ) { /* decrypt */
122 rc = -1; /* not yet used */
124 else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */
127 write_header( cfx, a );
130 gcry_md_write (cfx->mdc_hash, buf, size);
131 gcry_cipher_encrypt (cfx->cipher_hd, buf, size, NULL, 0);
132 rc = iobuf_write( a, buf, size );
134 else if( control == IOBUFCTRL_FREE ) {
135 if( cfx->mdc_hash ) {
137 int hashlen = gcry_md_get_algo_dlen (gcry_md_get_algo
141 log_assert( hashlen == 20 );
142 /* We must hash the prefix of the MDC packet here. */
145 gcry_md_putc (cfx->mdc_hash, temp[0]);
146 gcry_md_putc (cfx->mdc_hash, temp[1]);
148 gcry_md_final (cfx->mdc_hash);
149 hash = gcry_md_read (cfx->mdc_hash, 0);
150 memcpy(temp+2, hash, 20);
151 gcry_cipher_encrypt (cfx->cipher_hd, temp, 22, NULL, 0);
152 gcry_md_close (cfx->mdc_hash); cfx->mdc_hash = NULL;
153 if( iobuf_write( a, temp, 22 ) )
154 log_error("writing MDC packet failed\n" );
156 gcry_cipher_close (cfx->cipher_hd);
158 else if( control == IOBUFCTRL_DESC ) {
159 mem2str (buf, "cipher_filter", *ret_len);