1997-11-24 12:04:11 +01:00
|
|
|
/* seskey.c - make sesssion keys etc.
|
2006-04-19 13:26:11 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
|
|
|
* 2006 Free Software Foundation, Inc.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* This file is part of GnuPG.
|
1997-11-18 15:06:00 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-18 15:06:00 +01: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 13:41:40 +01:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-18 15:06:00 +01: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
|
2006-04-19 13:26:11 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
* USA.
|
1997-11-18 15:06:00 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2003-06-18 21:56:13 +02:00
|
|
|
|
|
|
|
#include "gpg.h"
|
1997-11-18 15:06:00 +01:00
|
|
|
#include "util.h"
|
2002-06-29 15:46:34 +02:00
|
|
|
#include "cipher.h"
|
1997-11-24 23:24:04 +01:00
|
|
|
#include "main.h"
|
1998-09-14 17:49:56 +02:00
|
|
|
#include "i18n.h"
|
2006-04-19 13:26:11 +02:00
|
|
|
|
1997-11-18 15:06:00 +01:00
|
|
|
|
|
|
|
/****************
|
|
|
|
* Make a session key and put it into DEK
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
make_session_key( DEK *dek )
|
|
|
|
{
|
2006-04-19 13:26:11 +02:00
|
|
|
gcry_cipher_hd_t chd;
|
|
|
|
int i, rc;
|
2003-06-18 21:56:13 +02:00
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
dek->keylen = gcry_cipher_get_algo_keylen (dek->algo);
|
2003-06-18 21:56:13 +02:00
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
if (gcry_cipher_open (&chd, dek->algo, GCRY_CIPHER_MODE_CFB,
|
2003-06-18 21:56:13 +02:00
|
|
|
(GCRY_CIPHER_SECURE
|
|
|
|
| (dek->algo >= 100 ?
|
|
|
|
0 : GCRY_CIPHER_ENABLE_SYNC))) )
|
2006-04-19 13:26:11 +02:00
|
|
|
BUG();
|
|
|
|
gcry_randomize (dek->key, dek->keylen, GCRY_STRONG_RANDOM );
|
|
|
|
for (i=0; i < 16; i++ )
|
|
|
|
{
|
|
|
|
rc = gcry_cipher_setkey (chd, dek->key, dek->keylen);
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
gcry_cipher_close (chd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (gpg_err_code (rc) != GPG_ERR_WEAK_KEY)
|
|
|
|
BUG();
|
|
|
|
log_info(_("weak key created - retrying\n") );
|
|
|
|
/* Renew the session key until we get a non-weak key. */
|
|
|
|
gcry_randomize (dek->key, dek->keylen, GCRY_STRONG_RANDOM);
|
|
|
|
}
|
|
|
|
log_fatal (_("cannot avoid weak key for symmetric cipher; "
|
|
|
|
"tried %d times!\n"), i);
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Encode the session key. NBITS is the number of bits which should be used
|
1997-12-16 20:15:09 +01:00
|
|
|
* for packing the session key.
|
1997-11-18 15:06:00 +01:00
|
|
|
* returns: A mpi with the session key (caller must free)
|
|
|
|
*/
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_mpi_t
|
|
|
|
encode_session_key (DEK *dek, unsigned int nbits)
|
1997-11-18 15:06:00 +01:00
|
|
|
{
|
|
|
|
int nframe = (nbits+7) / 8;
|
|
|
|
byte *p;
|
1998-02-12 15:39:08 +01:00
|
|
|
byte *frame;
|
1998-08-07 10:53:38 +02:00
|
|
|
int i,n;
|
1997-11-18 15:06:00 +01:00
|
|
|
u16 csum;
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_mpi_t a;
|
1997-11-18 15:06:00 +01:00
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
/* The current limitation is that we can only use a session key
|
1998-04-14 19:51:16 +02:00
|
|
|
* whose length is a multiple of BITS_PER_MPI_LIMB
|
1997-11-18 15:06:00 +01:00
|
|
|
* I think we can live with that.
|
|
|
|
*/
|
1998-02-12 15:39:08 +01:00
|
|
|
if( dek->keylen + 7 > nframe || !nframe )
|
1997-11-18 15:06:00 +01: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 20:16:10 +02:00
|
|
|
* (But how can we store the leading 0 - the external representaion
|
1998-04-14 19:51:16 +02:00
|
|
|
* of MPIs doesn't allow leading zeroes =:-)
|
1998-04-07 20:16:10 +02:00
|
|
|
*
|
1997-11-18 15:06:00 +01:00
|
|
|
* RND are non-zero random bytes.
|
1997-11-24 23:24:04 +01:00
|
|
|
* A is the cipher algorithm
|
1997-11-18 15:06:00 +01:00
|
|
|
* DEK is the encryption key (session key) length k depends on the
|
1998-04-30 16:06:01 +02:00
|
|
|
* cipher algorithm (20 is used with blowfish160).
|
1997-11-18 15:06:00 +01: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 15:39:08 +01:00
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
frame = xmalloc_secure( nframe );
|
1998-02-12 15:39:08 +01:00
|
|
|
n = 0;
|
|
|
|
frame[n++] = 0;
|
|
|
|
frame[n++] = 2;
|
|
|
|
i = nframe - 6 - dek->keylen;
|
|
|
|
assert( i > 0 );
|
2003-06-18 21:56:13 +02:00
|
|
|
p = gcry_random_bytes_secure (i, GCRY_STRONG_RANDOM);
|
2006-04-19 13:26:11 +02:00
|
|
|
/* Replace zero bytes by new values. */
|
1998-08-08 21:27:00 +02:00
|
|
|
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 */
|
2006-04-19 13:26:11 +02:00
|
|
|
k += k/128 + 3; /* better get some more */
|
|
|
|
pp = gcry_random_bytes_secure (k, GCRY_STRONG_RANDOM);
|
|
|
|
for(j=0; j < i && k ;) {
|
1998-08-08 21:27:00 +02:00
|
|
|
if( !p[j] )
|
|
|
|
p[j] = pp[--k];
|
2006-04-19 13:26:11 +02:00
|
|
|
if (p[j])
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
xfree(pp);
|
1998-08-08 21:27:00 +02:00
|
|
|
}
|
1998-08-07 10:53:38 +02:00
|
|
|
memcpy( frame+n, p, i );
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(p);
|
1998-08-07 10:53:38 +02:00
|
|
|
n += i;
|
1998-02-12 15:39:08 +01: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;
|
2006-04-19 13:26:11 +02:00
|
|
|
assert( n == nframe );
|
2003-07-28 10:59:18 +02:00
|
|
|
if (gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, n, &nframe))
|
2003-06-18 21:56:13 +02:00
|
|
|
BUG();
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(frame);
|
1998-02-12 15:39:08 +01:00
|
|
|
return a;
|
1997-11-18 15:06:00 +01:00
|
|
|
}
|
|
|
|
|
1997-11-24 12:04:11 +01:00
|
|
|
|
2003-06-18 21:56:13 +02:00
|
|
|
static gcry_mpi_t
|
|
|
|
do_encode_md( gcry_md_hd_t md, int algo, size_t len, unsigned nbits,
|
2006-04-19 13:26:11 +02:00
|
|
|
const byte *asn, size_t asnlen )
|
1997-12-31 13:32:54 +01:00
|
|
|
{
|
|
|
|
int nframe = (nbits+7) / 8;
|
1998-02-12 15:39:08 +01:00
|
|
|
byte *frame;
|
1998-01-16 22:15:24 +01:00
|
|
|
int i,n;
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_mpi_t a;
|
1997-12-31 13:32:54 +01:00
|
|
|
|
1998-02-12 15:39:08 +01:00
|
|
|
if( len + asnlen + 4 > nframe )
|
1998-02-16 21:05:02 +01:00
|
|
|
log_bug("can't encode a %d bit MD into a %d bits frame\n",
|
|
|
|
(int)(len*8), (int)nbits);
|
1997-12-31 13:32:54 +01:00
|
|
|
|
|
|
|
/* We encode the MD in this way:
|
|
|
|
*
|
2006-04-19 13:26:11 +02:00
|
|
|
* 0 1 PAD(n bytes) 0 ASN(asnlen bytes) MD(len bytes)
|
1997-12-31 13:32:54 +01:00
|
|
|
*
|
|
|
|
* PAD consists of FF bytes.
|
|
|
|
*/
|
2006-04-19 13:26:11 +02:00
|
|
|
frame = gcry_md_is_secure (md)? xmalloc_secure (nframe) : xmalloc (nframe);
|
1997-12-31 13:32:54 +01:00
|
|
|
n = 0;
|
1998-02-12 15:39:08 +01:00
|
|
|
frame[n++] = 0;
|
2006-04-19 13:26:11 +02:00
|
|
|
frame[n++] = 1; /* block type */
|
1998-02-12 15:39:08 +01: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;
|
2003-06-18 21:56:13 +02:00
|
|
|
memcpy( frame+n, gcry_md_read (md, algo), len ); n += len;
|
1997-11-24 12:04:11 +01:00
|
|
|
assert( n == nframe );
|
2006-04-19 13:26:11 +02:00
|
|
|
|
2003-07-28 10:59:18 +02:00
|
|
|
if (gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, n, &nframe ))
|
2003-06-18 21:56:13 +02:00
|
|
|
BUG();
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(frame);
|
|
|
|
|
|
|
|
/* Note that PGP before version 2.3 encoded the MD as:
|
|
|
|
*
|
|
|
|
* 0 1 MD(16 bytes) 0 PAD(n bytes) 1
|
|
|
|
*
|
|
|
|
* The MD is always 16 bytes here because it's always MD5. We do
|
|
|
|
* not support pre-v2.3 signatures, but I'm including this comment
|
|
|
|
* so the information is easily found in the future.
|
|
|
|
*/
|
|
|
|
|
1998-02-12 15:39:08 +01:00
|
|
|
return a;
|
1997-11-24 12:04:11 +01:00
|
|
|
}
|
|
|
|
|
1997-11-24 23:24:04 +01:00
|
|
|
|
2000-07-14 19:34:53 +02:00
|
|
|
/****************
|
|
|
|
* Encode a message digest into an MPI.
|
2006-04-19 13:26:11 +02:00
|
|
|
* If it's for a DSA signature, make sure that the hash is large
|
|
|
|
* enough to fill up q. If the hash is too big, take the leftmost
|
|
|
|
* bits.
|
2000-07-14 19:34:53 +02:00
|
|
|
*/
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_mpi_t
|
2006-04-19 13:26:11 +02:00
|
|
|
encode_md_value (PKT_public_key *pk, PKT_secret_key *sk,
|
|
|
|
gcry_md_hd_t md, int hash_algo)
|
1997-12-01 11:33:23 +01:00
|
|
|
{
|
2003-06-18 21:56:13 +02:00
|
|
|
gcry_mpi_t frame;
|
2006-04-19 13:26:11 +02:00
|
|
|
|
|
|
|
assert(hash_algo);
|
|
|
|
assert(pk || sk);
|
|
|
|
|
|
|
|
if((pk?pk->pubkey_algo:sk->pubkey_algo) == GCRY_PK_DSA)
|
2003-06-18 21:56:13 +02:00
|
|
|
{
|
2006-04-19 13:26:11 +02:00
|
|
|
/* It's a DSA signature, so find out the size of q. */
|
|
|
|
|
2006-06-30 11:42:08 +02:00
|
|
|
size_t qbytes = gcry_mpi_get_nbits (pk?pk->pkey[1]:sk->skey[1]);
|
2006-04-19 13:26:11 +02:00
|
|
|
|
|
|
|
/* Make sure it is a multiple of 8 bits. */
|
|
|
|
|
|
|
|
if(qbytes%8)
|
|
|
|
{
|
|
|
|
log_error(_("DSA requires the hash length to be a"
|
|
|
|
" multiple of 8 bits\n"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't allow any q smaller than 160 bits. This might need a
|
|
|
|
revisit as the DSA2 design firms up, but for now, we don't
|
|
|
|
want someone to issue signatures from a key with a 16-bit q
|
|
|
|
or something like that, which would look correct but allow
|
|
|
|
trivial forgeries. Yes, I know this rules out using MD5 with
|
|
|
|
DSA. ;) */
|
|
|
|
|
|
|
|
if(qbytes<160)
|
|
|
|
{
|
|
|
|
log_error(_("DSA key %s uses an unsafe (%u bit) hash\n"),
|
|
|
|
pk?keystr_from_pk(pk):keystr_from_sk(sk),qbytes);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
qbytes/=8;
|
|
|
|
|
|
|
|
/* Check if we're too short. Too long is safe as we'll
|
|
|
|
automatically left-truncate. */
|
|
|
|
|
|
|
|
if(gcry_md_get_algo_dlen (hash_algo) < qbytes)
|
|
|
|
{
|
|
|
|
log_error(_("DSA key %s requires a %u bit or larger hash\n"),
|
|
|
|
pk?keystr_from_pk(pk):keystr_from_sk(sk),qbytes*8);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gcry_mpi_scan (&frame, GCRYMPI_FMT_USG,
|
2006-06-30 11:42:08 +02:00
|
|
|
gcry_md_read (md, hash_algo), qbytes, &qbytes))
|
2003-06-18 21:56:13 +02:00
|
|
|
BUG();
|
1998-06-15 17:41:04 +02:00
|
|
|
}
|
2003-06-18 21:56:13 +02:00
|
|
|
else
|
|
|
|
{
|
2003-07-01 10:34:45 +02:00
|
|
|
gpg_error_t rc;
|
2003-06-18 21:56:13 +02:00
|
|
|
byte *asn;
|
|
|
|
size_t asnlen;
|
2006-04-19 13:26:11 +02:00
|
|
|
|
2006-10-05 13:06:42 +02:00
|
|
|
rc = gcry_md_test_algo (hash_algo);
|
|
|
|
if (!rc)
|
|
|
|
rc = gcry_md_algo_info (hash_algo, GCRYCTL_GET_ASNOID, NULL, &asnlen);
|
2003-07-01 10:34:45 +02:00
|
|
|
if (rc)
|
2006-04-19 13:26:11 +02:00
|
|
|
log_fatal ("can't get OID of algo %d: %s\n",
|
|
|
|
hash_algo, gpg_strerror (rc));
|
2003-06-18 21:56:13 +02:00
|
|
|
asn = xmalloc (asnlen);
|
2006-04-19 13:26:11 +02:00
|
|
|
if ( gcry_md_algo_info (hash_algo, GCRYCTL_GET_ASNOID, asn, &asnlen) )
|
2003-06-18 21:56:13 +02:00
|
|
|
BUG();
|
2006-04-19 13:26:11 +02:00
|
|
|
frame = do_encode_md (md, hash_algo, gcry_md_get_algo_dlen (hash_algo),
|
|
|
|
gcry_mpi_get_nbits (pk?pk->pkey[0]:sk->skey[0]),
|
|
|
|
asn, asnlen);
|
2003-06-18 21:56:13 +02:00
|
|
|
xfree (asn);
|
1998-06-15 17:41:04 +02:00
|
|
|
}
|
2006-04-19 13:26:11 +02:00
|
|
|
|
2003-06-18 21:56:13 +02:00
|
|
|
return frame;
|
1997-12-01 11:33:23 +01:00
|
|
|
}
|