~ubuntu-branches/ubuntu/jaunty/gnupg2/jaunty-security

« back to all changes in this revision

Viewing changes to g10/cipher.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* cipher.c - En-/De-ciphering filter
2
 
 * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2003,
 
3
 *               2006 Free Software Foundation, Inc.
3
4
 *
4
5
 * This file is part of GnuPG.
5
6
 *
6
7
 * GnuPG is free software; you can redistribute it and/or modify
7
8
 * 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
 * the Free Software Foundation; either version 3 of the License, or
9
10
 * (at your option) any later version.
10
11
 *
11
12
 * GnuPG is distributed in the hope that it will be useful,
14
15
 * GNU General Public License for more details.
15
16
 *
16
17
 * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
19
 */
20
20
 
21
21
#include <config.h>
26
26
#include <assert.h>
27
27
 
28
28
#include "gpg.h"
29
 
#include "errors.h"
 
29
#include "status.h"
30
30
#include "iobuf.h"
31
 
#include "memory.h"
32
31
#include "util.h"
33
32
#include "filter.h"
34
33
#include "packet.h"
41
40
 
42
41
 
43
42
static void
44
 
write_header( cipher_filter_context_t *cfx, iobuf_t a )
 
43
write_header( cipher_filter_context_t *cfx, IOBUF a )
45
44
{
 
45
    gcry_error_t err;
46
46
    PACKET pkt;
47
47
    PKT_encrypted ed;
48
48
    byte temp[18];
49
49
    unsigned int blocksize;
50
50
    unsigned int nprefix;
51
 
    gpg_error_t rc;
52
51
 
53
 
    blocksize = gcry_cipher_get_algo_blklen ( cfx->dek->algo );
54
 
    if( blocksize < 8 || blocksize > 16 )
 
52
    blocksize = gcry_cipher_get_algo_blklen (cfx->dek->algo);
 
53
    if ( blocksize < 8 || blocksize > 16 )
55
54
        log_fatal("unsupported blocksize %u\n", blocksize );
56
55
 
57
56
    memset( &ed, 0, sizeof ed );
60
59
    ed.new_ctb = !ed.len && !RFC1991;
61
60
    if( cfx->dek->use_mdc ) {
62
61
        ed.mdc_method = DIGEST_ALGO_SHA1;
63
 
        gcry_md_open (&cfx->mdc_hash, GCRY_MD_SHA1, 0 );
 
62
        gcry_md_open (&cfx->mdc_hash, DIGEST_ALGO_SHA1, 0);
64
63
        if ( DBG_HASHING )
65
 
            gcry_md_start_debug ( cfx->mdc_hash, "creatmdc" );
 
64
            gcry_md_start_debug (cfx->mdc_hash, "creatmdc");
66
65
    }
67
66
 
68
67
    {
78
77
    if( build_packet( a, &pkt ))
79
78
        log_bug("build_packet(ENCR_DATA) failed\n");
80
79
    nprefix = blocksize;
81
 
    gcry_randomize ( temp, nprefix, GCRY_STRONG_RANDOM);
 
80
    gcry_randomize (temp, nprefix, GCRY_STRONG_RANDOM );
82
81
    temp[nprefix] = temp[nprefix-2];
83
82
    temp[nprefix+1] = temp[nprefix-1];
84
83
    print_cipher_algo_note( cfx->dek->algo );
85
 
    rc = gcry_cipher_open (&cfx->cipher_hd, 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
 
    if (rc) {
91
 
        /* we should never get an error here cause we already checked, that
92
 
         * the algorithm is available. */
 
84
    err = gcry_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
    if (err) {
 
91
        /* We should never get an error here cause we already checked,
 
92
         * that the algorithm is available.  */
93
93
        BUG();
94
94
    }
 
95
 
 
96
 
95
97
/*   log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/
96
98
    gcry_cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen );
97
99
    gcry_cipher_setiv( cfx->cipher_hd, NULL, 0 );
98
100
/*  log_hexdump( "prefix", temp, nprefix+2 ); */
99
 
    if( cfx->mdc_hash ) /* hash the "IV" */
100
 
        gcry_md_write( cfx->mdc_hash, temp, nprefix+2 );
101
 
    gcry_cipher_encrypt( cfx->cipher_hd, temp, nprefix+2, NULL, 0);
102
 
    gcry_cipher_sync( cfx->cipher_hd );
 
101
    if (cfx->mdc_hash) /* Hash the "IV". */
 
102
        gcry_md_write (cfx->mdc_hash, temp, nprefix+2 );
 
103
    gcry_cipher_encrypt (cfx->cipher_hd, temp, nprefix+2, NULL, 0);
 
104
    gcry_cipher_sync (cfx->cipher_hd);
103
105
    iobuf_write(a, temp, nprefix+2);
104
106
    cfx->header=1;
105
107
}
111
113
 */
112
114
int
113
115
cipher_filter( void *opaque, int control,
114
 
               iobuf_t a, byte *buf, size_t *ret_len)
 
116
               IOBUF a, byte *buf, size_t *ret_len)
115
117
{
116
118
    size_t size = *ret_len;
117
119
    cipher_filter_context_t *cfx = opaque;
125
127
        if( !cfx->header ) {
126
128
            write_header( cfx, a );
127
129
        }
128
 
        if( cfx->mdc_hash )
129
 
            gcry_md_write( cfx->mdc_hash, buf, size );
130
 
        gcry_cipher_encrypt( cfx->cipher_hd, buf, size, NULL, 0);
 
130
        if (cfx->mdc_hash)
 
131
            gcry_md_write (cfx->mdc_hash, buf, size);
 
132
        gcry_cipher_encrypt (cfx->cipher_hd, buf, size, NULL, 0);
131
133
        rc = iobuf_write( a, buf, size );
132
134
    }
133
135
    else if( control == IOBUFCTRL_FREE ) {
134
136
        if( cfx->mdc_hash ) {
135
137
            byte *hash;
136
 
            int hashlen = gcry_md_get_algo_dlen (gcry_md_get_algo (
137
 
                                                            cfx->mdc_hash));
 
138
            int hashlen = gcry_md_get_algo_dlen (gcry_md_get_algo
 
139
                                                 (cfx->mdc_hash));
138
140
            byte temp[22];
139
141
 
140
142
            assert( hashlen == 20 );
141
 
            /* we must hash the prefix of the MDC packet here */
 
143
            /* We must hash the prefix of the MDC packet here. */
142
144
            temp[0] = 0xd3;
143
145
            temp[1] = 0x14;
144
 
            gcry_md_putc ( cfx->mdc_hash, temp[0] );
145
 
            gcry_md_putc ( cfx->mdc_hash, temp[1] );
 
146
            gcry_md_putc (cfx->mdc_hash, temp[0]);
 
147
            gcry_md_putc (cfx->mdc_hash, temp[1]);
146
148
 
147
 
            gcry_md_final ( cfx->mdc_hash );
148
 
            hash = gcry_md_read ( cfx->mdc_hash, 0 );
 
149
            gcry_md_final (cfx->mdc_hash);
 
150
            hash = gcry_md_read (cfx->mdc_hash, 0);
149
151
            memcpy(temp+2, hash, 20);
150
 
            gcry_cipher_encrypt( cfx->cipher_hd, temp, 22, NULL, 0 );
151
 
            gcry_md_close ( cfx->mdc_hash ); cfx->mdc_hash = NULL;
152
 
            rc = iobuf_write( a, temp, 22 );
153
 
            if (rc)
 
152
            gcry_cipher_encrypt (cfx->cipher_hd, temp, 22, NULL, 0);
 
153
            gcry_md_close (cfx->mdc_hash); cfx->mdc_hash = NULL;
 
154
            if( iobuf_write( a, temp, 22 ) )
154
155
                log_error("writing MDC packet failed\n" );
155
156
        }
156
157
        gcry_cipher_close (cfx->cipher_hd);
160
161
    }
161
162
    return rc;
162
163
}
163
 
 
164
 
 
165