gnupg/g10/seskey.c

157 lines
4.3 KiB
C
Raw Normal View History

1997-11-24 12:04:11 +01:00
/* seskey.c - make sesssion keys etc.
1998-02-24 19:50:46 +01:00
* Copyright (C) 1998 Free Software Foundation, Inc.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +01:00
* This file is part of GNUPG.
1997-11-18 15:06:00 +01:00
*
1998-02-24 19:50:46 +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-02-24 19:50:46 +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
* 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>
#include "util.h"
#include "cipher.h"
#include "mpi.h"
1997-11-24 23:24:04 +01:00
#include "main.h"
1997-11-18 15:06:00 +01:00
/****************
* Make a session key and put it into DEK
*/
void
make_session_key( DEK *dek )
{
1998-04-30 16:06:01 +02:00
dek->keylen = cipher_get_keylen( dek->algo ) / 8;
randomize_buffer( dek->key, dek->keylen, 1 );
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)
*/
MPI
encode_session_key( DEK *dek, unsigned nbits )
{
int nframe = (nbits+7) / 8;
byte *p;
1998-02-12 15:39:08 +01:00
byte *frame;
1997-11-18 15:06:00 +01:00
int i,n,c;
u16 csum;
1998-02-12 15:39:08 +01:00
MPI a;
1997-11-18 15:06:00 +01:00
1998-04-14 19:51:16 +02: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 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
frame = m_alloc_secure( nframe );
n = 0;
frame[n++] = 0;
frame[n++] = 2;
i = nframe - 6 - dek->keylen;
assert( i > 0 );
for( ; i ; i-- ) {
1997-11-18 15:06:00 +01:00
while( !(c = get_random_byte(1)) )
;
1998-02-12 15:39:08 +01:00
frame[n++] = c;
1997-11-18 15:06:00 +01:00
}
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;
1997-11-18 15:06:00 +01:00
assert( n == nframe );
1998-02-12 15:39:08 +01:00
a = mpi_alloc_secure( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB );
mpi_set_buffer( a, frame, nframe, 0 );
m_free(frame);
return a;
1997-11-18 15:06:00 +01:00
}
1997-11-24 12:04:11 +01:00
1998-02-12 00:22:09 +01:00
static MPI
1998-02-12 15:39:08 +01:00
do_encode_md( MD_HANDLE md, int algo, size_t len, unsigned nbits,
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;
1998-02-12 15:39:08 +01:00
MPI 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:
*
1998-02-12 15:39:08 +01:00
* 0 A PAD(n bytes) 0 ASN(asnlen bytes) MD(len bytes)
1997-12-31 13:32:54 +01:00
*
* PAD consists of FF bytes.
*/
1998-02-12 15:39:08 +01:00
frame = md_is_secure(md)? m_alloc_secure( nframe ) : m_alloc( nframe );
1997-12-31 13:32:54 +01:00
n = 0;
1998-02-12 15:39:08 +01:00
frame[n++] = 0;
frame[n++] = algo;
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;
memcpy( frame+n, md_read(md, algo), len ); n += len;
1997-11-24 12:04:11 +01:00
assert( n == nframe );
1998-02-12 15:39:08 +01:00
a = md_is_secure(md)?
mpi_alloc_secure( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB )
: mpi_alloc( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB );
mpi_set_buffer( a, frame, nframe, 0 );
m_free(frame);
return a;
1997-11-24 12:04:11 +01:00
}
1997-11-24 23:24:04 +01:00
1997-12-01 11:33:23 +01:00
MPI
1998-05-04 20:49:26 +02:00
encode_md_value( MD_HANDLE md, int hash_algo, unsigned nbits )
1997-12-01 11:33:23 +01:00
{
1998-05-04 20:49:26 +02:00
int algo = hash_algo? hash_algo : md_get_algo(md);
1998-02-12 15:39:08 +01:00
const byte *asn;
size_t asnlen, mdlen;
asn = md_asn_oid( algo, &asnlen, &mdlen );
return do_encode_md( md, algo, mdlen, nbits, asn, asnlen );
1997-12-01 11:33:23 +01:00
}