1997-11-24 11:04:11 +00:00
|
|
|
/* seskey.c - make sesssion keys etc.
|
2000-07-14 17:34:53 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* This file is part of GnuPG.
|
1997-11-18 14:06:00 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-18 14:06:00 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-18 14:06:00 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
1999-10-26 12:14:37 +00:00
|
|
|
|
|
|
|
#include <gcrypt.h>
|
1997-11-18 14:06:00 +00:00
|
|
|
#include "util.h"
|
1997-11-24 22:24:04 +00:00
|
|
|
#include "main.h"
|
1998-09-14 15:49:56 +00:00
|
|
|
#include "i18n.h"
|
1997-11-18 14:06:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Make a session key and put it into DEK
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
make_session_key( DEK *dek )
|
|
|
|
{
|
1999-10-26 12:14:37 +00:00
|
|
|
GCRY_CIPHER_HD chd;
|
1998-09-14 15:49:56 +00:00
|
|
|
int i, rc;
|
|
|
|
|
1999-10-26 12:14:37 +00:00
|
|
|
dek->keylen = gcry_cipher_get_algo_keylen( dek->algo );
|
|
|
|
|
|
|
|
if( !(chd = gcry_cipher_open( dek->algo, GCRY_CIPHER_MODE_CFB,
|
|
|
|
GCRY_CIPHER_SECURE
|
|
|
|
| (dek->algo >= 100 ?
|
|
|
|
0 : GCRY_CIPHER_ENABLE_SYNC) ))
|
|
|
|
) {
|
|
|
|
BUG();
|
|
|
|
}
|
1998-09-14 15:49:56 +00:00
|
|
|
|
1999-11-13 16:43:23 +00:00
|
|
|
gcry_randomize( dek->key, dek->keylen, GCRY_STRONG_RANDOM );
|
1998-09-14 15:49:56 +00:00
|
|
|
for(i=0; i < 16; i++ ) {
|
1999-10-26 12:14:37 +00:00
|
|
|
rc = gcry_cipher_setkey( chd, dek->key, dek->keylen );
|
1998-09-14 15:49:56 +00:00
|
|
|
if( !rc ) {
|
1999-10-26 12:14:37 +00:00
|
|
|
gcry_cipher_close( chd );
|
1998-09-14 15:49:56 +00:00
|
|
|
return;
|
|
|
|
}
|
1999-10-26 12:14:37 +00:00
|
|
|
if( rc != GCRYERR_WEAK_KEY )
|
|
|
|
BUG();
|
1998-09-14 15:49:56 +00:00
|
|
|
log_info(_("weak key created - retrying\n") );
|
|
|
|
/* Renew the session key until we get a non-weak key. */
|
1999-11-13 16:43:23 +00:00
|
|
|
gcry_randomize( dek->key, dek->keylen, GCRY_STRONG_RANDOM );
|
1998-09-14 15:49:56 +00:00
|
|
|
}
|
|
|
|
log_fatal(_(
|
|
|
|
"cannot avoid weak key for symmetric cipher; tried %d times!\n"),
|
|
|
|
i);
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Encode the session key. NBITS is the number of bits which should be used
|
1997-12-16 19:15:09 +00:00
|
|
|
* for packing the session key.
|
1997-11-18 14:06:00 +00:00
|
|
|
* returns: A mpi with the session key (caller must free)
|
|
|
|
*/
|
|
|
|
MPI
|
|
|
|
encode_session_key( DEK *dek, unsigned nbits )
|
|
|
|
{
|
|
|
|
int nframe = (nbits+7) / 8;
|
|
|
|
byte *p;
|
1998-02-12 14:39:08 +00:00
|
|
|
byte *frame;
|
1998-08-07 08:53:38 +00:00
|
|
|
int i,n;
|
1997-11-18 14:06:00 +00:00
|
|
|
u16 csum;
|
1998-02-12 14:39:08 +00:00
|
|
|
MPI a;
|
1997-11-18 14:06:00 +00:00
|
|
|
|
1998-04-14 17:51:16 +00:00
|
|
|
/* the current limitation is that we can only use a session key
|
|
|
|
* whose length is a multiple of BITS_PER_MPI_LIMB
|
1997-11-18 14:06:00 +00:00
|
|
|
* I think we can live with that.
|
|
|
|
*/
|
1998-02-12 14:39:08 +00:00
|
|
|
if( dek->keylen + 7 > nframe || !nframe )
|
1997-11-18 14:06:00 +00:00
|
|
|
log_bug("can't encode a %d bit key in a %d bits frame\n",
|
|
|
|
dek->keylen*8, nbits );
|
|
|
|
|
|
|
|
/* We encode the session key in this way:
|
|
|
|
*
|
|
|
|
* 0 2 RND(n bytes) 0 A DEK(k bytes) CSUM(2 bytes)
|
|
|
|
*
|
1998-04-07 18:16:10 +00:00
|
|
|
* (But how can we store the leading 0 - the external representaion
|
1998-04-14 17:51:16 +00:00
|
|
|
* of MPIs doesn't allow leading zeroes =:-)
|
1998-04-07 18:16:10 +00:00
|
|
|
*
|
1997-11-18 14:06:00 +00:00
|
|
|
* RND are non-zero random bytes.
|
1997-11-24 22:24:04 +00:00
|
|
|
* A is the cipher algorithm
|
1997-11-18 14:06:00 +00:00
|
|
|
* DEK is the encryption key (session key) length k depends on the
|
1998-04-30 14:06:01 +00:00
|
|
|
* cipher algorithm (20 is used with blowfish160).
|
1997-11-18 14:06:00 +00:00
|
|
|
* CSUM is the 16 bit checksum over the DEK
|
|
|
|
*/
|
|
|
|
csum = 0;
|
|
|
|
for( p = dek->key, i=0; i < dek->keylen; i++ )
|
|
|
|
csum += *p++;
|
1998-02-12 14:39:08 +00:00
|
|
|
|
2000-01-24 11:55:49 +00:00
|
|
|
frame = gcry_xmalloc_secure( nframe );
|
1998-02-12 14:39:08 +00:00
|
|
|
n = 0;
|
|
|
|
frame[n++] = 0;
|
|
|
|
frame[n++] = 2;
|
|
|
|
i = nframe - 6 - dek->keylen;
|
|
|
|
assert( i > 0 );
|
1999-11-13 16:43:23 +00:00
|
|
|
p = gcry_random_bytes_secure( i, GCRY_STRONG_RANDOM );
|
1998-08-08 19:27:00 +00:00
|
|
|
/* replace zero bytes by new values */
|
|
|
|
for(;;) {
|
|
|
|
int j, k;
|
|
|
|
byte *pp;
|
|
|
|
|
|
|
|
/* count the zero bytes */
|
|
|
|
for(j=k=0; j < i; j++ )
|
|
|
|
if( !p[j] )
|
|
|
|
k++;
|
|
|
|
if( !k )
|
|
|
|
break; /* okay: no zero bytes */
|
|
|
|
k += k/128; /* better get some more */
|
1999-11-13 16:43:23 +00:00
|
|
|
pp = gcry_random_bytes_secure( k, GCRY_STRONG_RANDOM);
|
1998-08-08 19:27:00 +00:00
|
|
|
for(j=0; j < i && k ; j++ )
|
|
|
|
if( !p[j] )
|
|
|
|
p[j] = pp[--k];
|
2000-01-24 11:55:49 +00:00
|
|
|
gcry_free(pp);
|
1998-08-08 19:27:00 +00:00
|
|
|
}
|
1998-08-07 08:53:38 +00:00
|
|
|
memcpy( frame+n, p, i );
|
2000-01-24 11:55:49 +00:00
|
|
|
gcry_free(p);
|
1998-08-07 08:53:38 +00:00
|
|
|
n += i;
|
1998-02-12 14:39:08 +00:00
|
|
|
frame[n++] = 0;
|
|
|
|
frame[n++] = dek->algo;
|
|
|
|
memcpy( frame+n, dek->key, dek->keylen ); n += dek->keylen;
|
|
|
|
frame[n++] = csum >>8;
|
|
|
|
frame[n++] = csum;
|
1997-11-18 14:06:00 +00:00
|
|
|
assert( n == nframe );
|
2000-01-24 11:55:49 +00:00
|
|
|
if( gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, &nframe ) )
|
|
|
|
BUG();
|
|
|
|
gcry_free(frame);
|
|
|
|
|
1998-02-12 14:39:08 +00:00
|
|
|
return a;
|
1997-11-18 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
1997-11-24 11:04:11 +00:00
|
|
|
|
1998-02-11 23:22:09 +00:00
|
|
|
static MPI
|
1999-10-26 12:14:37 +00:00
|
|
|
do_encode_md( GCRY_MD_HD md, int algo, size_t len, unsigned nbits,
|
2000-07-14 17:34:53 +00:00
|
|
|
const byte *asn, size_t asnlen, int v3compathack )
|
1997-12-31 12:32:54 +00:00
|
|
|
{
|
|
|
|
int nframe = (nbits+7) / 8;
|
1998-02-12 14:39:08 +00:00
|
|
|
byte *frame;
|
1998-01-16 21:15:24 +00:00
|
|
|
int i,n;
|
1998-02-12 14:39:08 +00:00
|
|
|
MPI a;
|
1997-12-31 12:32:54 +00:00
|
|
|
|
1998-02-12 14:39:08 +00:00
|
|
|
if( len + asnlen + 4 > nframe )
|
1998-02-16 20:05:02 +00:00
|
|
|
log_bug("can't encode a %d bit MD into a %d bits frame\n",
|
|
|
|
(int)(len*8), (int)nbits);
|
1997-12-31 12:32:54 +00:00
|
|
|
|
|
|
|
/* We encode the MD in this way:
|
|
|
|
*
|
2000-07-14 17:34:53 +00:00
|
|
|
* 0 1 PAD(n bytes) 0 ASN(asnlen bytes) MD(len bytes)
|
1997-12-31 12:32:54 +00:00
|
|
|
*
|
|
|
|
* PAD consists of FF bytes.
|
|
|
|
*/
|
2000-01-24 11:55:49 +00:00
|
|
|
frame = gcry_md_is_secure(md)? gcry_xmalloc_secure( nframe )
|
|
|
|
: gcry_xmalloc( nframe );
|
1997-12-31 12:32:54 +00:00
|
|
|
n = 0;
|
1998-02-12 14:39:08 +00:00
|
|
|
frame[n++] = 0;
|
2000-07-14 17:34:53 +00:00
|
|
|
frame[n++] = v3compathack? algo : 1; /* block type */
|
1998-02-12 14:39:08 +00:00
|
|
|
i = nframe - len - asnlen -3 ;
|
|
|
|
assert( i > 1 );
|
|
|
|
memset( frame+n, 0xff, i ); n += i;
|
|
|
|
frame[n++] = 0;
|
|
|
|
memcpy( frame+n, asn, asnlen ); n += asnlen;
|
1999-10-26 12:14:37 +00:00
|
|
|
memcpy( frame+n, gcry_md_read(md, algo), len ); n += len;
|
1997-11-24 11:04:11 +00:00
|
|
|
assert( n == nframe );
|
2000-01-24 11:55:49 +00:00
|
|
|
if( gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, &nframe ) )
|
|
|
|
BUG();
|
|
|
|
gcry_free(frame);
|
1998-02-12 14:39:08 +00:00
|
|
|
return a;
|
1997-11-24 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
1997-11-24 22:24:04 +00:00
|
|
|
|
2000-07-14 17:34:53 +00:00
|
|
|
/****************
|
|
|
|
* Encode a message digest into an MPI.
|
|
|
|
* v3compathack is used to work around a bug in old GnuPG versions
|
|
|
|
* which did put the algo identifier inseatd of the block type 1 into
|
|
|
|
* the encoded value. setting this vare force the old behaviour.
|
|
|
|
*/
|
1997-12-01 10:33:23 +00:00
|
|
|
MPI
|
2000-07-14 17:34:53 +00:00
|
|
|
encode_md_value( int pubkey_algo, GCRY_MD_HD md, int hash_algo,
|
|
|
|
unsigned nbits, int v3compathack )
|
1997-12-01 10:33:23 +00:00
|
|
|
{
|
1999-10-26 12:14:37 +00:00
|
|
|
int algo = hash_algo? hash_algo : gcry_md_get_algo(md);
|
1998-06-15 15:41:04 +00:00
|
|
|
MPI frame;
|
|
|
|
|
1999-11-13 16:43:23 +00:00
|
|
|
if( pubkey_algo == GCRY_PK_DSA ) {
|
2000-01-24 11:55:49 +00:00
|
|
|
size_t n = gcry_md_get_algo_dlen(hash_algo);
|
|
|
|
if( gcry_mpi_scan( &frame, GCRYMPI_FMT_USG,
|
|
|
|
gcry_md_read(md, hash_algo), &n ) )
|
|
|
|
BUG();
|
1998-06-15 15:41:04 +00:00
|
|
|
}
|
|
|
|
else {
|
1999-10-26 12:14:37 +00:00
|
|
|
byte *asn;
|
|
|
|
size_t asnlen;
|
|
|
|
|
|
|
|
if( gcry_md_algo_info( algo, GCRYCTL_GET_ASNOID, NULL, &asnlen ) )
|
|
|
|
log_fatal("can't get OID of algo %d: %s\n",
|
|
|
|
algo, gcry_strerror(-1));
|
2000-01-24 11:55:49 +00:00
|
|
|
asn = gcry_xmalloc( asnlen );
|
1999-10-26 12:14:37 +00:00
|
|
|
if( gcry_md_algo_info( algo, GCRYCTL_GET_ASNOID, asn, &asnlen ) )
|
|
|
|
BUG();
|
|
|
|
frame = do_encode_md( md, algo, gcry_md_get_algo_dlen( algo ),
|
2000-07-14 17:34:53 +00:00
|
|
|
nbits, asn, asnlen, v3compathack );
|
2000-01-24 11:55:49 +00:00
|
|
|
gcry_free( asn );
|
1998-06-15 15:41:04 +00:00
|
|
|
}
|
|
|
|
return frame;
|
1997-12-01 10:33:23 +00:00
|
|
|
}
|
|
|
|
|
2000-07-14 17:34:53 +00:00
|
|
|
|