1 /* seskey.c - make sesssion keys etc.
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,
35 * Make a session key and put it into DEK
38 make_session_key( DEK *dek )
43 dek->keylen = cipher_get_keylen( dek->algo ) / 8;
45 chd = cipher_open( dek->algo, CIPHER_MODE_AUTO_CFB, 1 );
46 randomize_buffer( dek->key, dek->keylen, 1 );
47 for(i=0; i < 16; i++ ) {
48 rc = cipher_setkey( chd, dek->key, dek->keylen );
53 log_info(_("weak key created - retrying\n") );
54 /* Renew the session key until we get a non-weak key. */
55 randomize_buffer( dek->key, dek->keylen, 1 );
58 "cannot avoid weak key for symmetric cipher; tried %d times!\n"),
64 * Encode the session key. NBITS is the number of bits which should be used
65 * for packing the session key.
66 * returns: A mpi with the session key (caller must free)
69 encode_session_key( DEK *dek, unsigned nbits )
71 int nframe = (nbits+7) / 8;
78 /* the current limitation is that we can only use a session key
79 * whose length is a multiple of BITS_PER_MPI_LIMB
80 * I think we can live with that.
82 if( dek->keylen + 7 > nframe || !nframe )
83 log_bug("can't encode a %d bit key in a %d bits frame\n",
84 dek->keylen*8, nbits );
86 /* We encode the session key in this way:
88 * 0 2 RND(n bytes) 0 A DEK(k bytes) CSUM(2 bytes)
90 * (But how can we store the leading 0 - the external representaion
91 * of MPIs doesn't allow leading zeroes =:-)
93 * RND are non-zero random bytes.
94 * A is the cipher algorithm
95 * DEK is the encryption key (session key) length k depends on the
96 * cipher algorithm (20 is used with blowfish160).
97 * CSUM is the 16 bit checksum over the DEK
100 for( p = dek->key, i=0; i < dek->keylen; i++ )
103 frame = xmalloc_secure( nframe );
107 i = nframe - 6 - dek->keylen;
109 p = get_random_bits( i*8, 1, 1 );
110 /* replace zero bytes by new values */
115 /* count the zero bytes */
116 for(j=k=0; j < i; j++ )
120 break; /* okay: no zero bytes */
121 k += k/128 + 3; /* better get some more */
122 pp = get_random_bits( k*8, 1, 1);
123 for(j=0; j < i && k ;) {
131 memcpy( frame+n, p, i );
135 frame[n++] = dek->algo;
136 memcpy( frame+n, dek->key, dek->keylen ); n += dek->keylen;
137 frame[n++] = csum >>8;
139 assert( n == nframe );
140 a = mpi_alloc_secure( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB );
141 mpi_set_buffer( a, frame, nframe, 0 );
148 do_encode_md( MD_HANDLE md, int algo, size_t len, unsigned nbits,
149 const byte *asn, size_t asnlen )
151 int nframe = (nbits+7) / 8;
156 if( len + asnlen + 4 > nframe )
157 log_bug("can't encode a %d bit MD into a %d bits frame\n",
158 (int)(len*8), (int)nbits);
160 /* We encode the MD in this way:
162 * 0 1 PAD(n bytes) 0 ASN(asnlen bytes) MD(len bytes)
164 * PAD consists of FF bytes.
166 frame = md_is_secure(md)? xmalloc_secure( nframe ) : xmalloc( nframe );
169 frame[n++] = 1; /* block type */
170 i = nframe - len - asnlen -3 ;
172 memset( frame+n, 0xff, i ); n += i;
174 memcpy( frame+n, asn, asnlen ); n += asnlen;
175 memcpy( frame+n, md_read(md, algo), len ); n += len;
176 assert( n == nframe );
177 a = md_is_secure(md)?
178 mpi_alloc_secure( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB )
179 : mpi_alloc( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB );
180 mpi_set_buffer( a, frame, nframe, 0 );
183 /* Note that PGP before version 2.3 encoded the MD as:
185 * 0 1 MD(16 bytes) 0 PAD(n bytes) 1
187 * The MD is always 16 bytes here because it's always MD5. We do
188 * not support pre-v2.3 signatures, but I'm including this comment
189 * so the information is easily found in the future.
197 * Encode a message digest into an MPI.
198 * v3compathack is used to work around a bug in old GnuPG versions
199 * which did put the algo identifier inseatd of the block type 1 into
200 * the encoded value. Setting this flag forces the old behaviour.
203 encode_md_value( int pubkey_algo, MD_HANDLE md,
204 int hash_algo, unsigned nbits )
206 int algo = hash_algo? hash_algo : md_get_algo(md);
208 size_t asnlen, mdlen;
211 if( pubkey_algo == PUBKEY_ALGO_DSA ) {
212 mdlen = md_digest_length (hash_algo);
214 log_error (_("DSA requires the use of a 160 bit hash algorithm\n"));
218 frame = md_is_secure(md)? mpi_alloc_secure((md_digest_length(hash_algo)
219 +BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB )
220 : mpi_alloc((md_digest_length(hash_algo)
221 +BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB );
222 mpi_set_buffer( frame, md_read(md, hash_algo),
223 md_digest_length(hash_algo), 0 );
226 asn = md_asn_oid( algo, &asnlen, &mdlen );
227 frame = do_encode_md( md, algo, mdlen, nbits, asn, asnlen );