LCOV - code coverage report
Current view: top level - g10 - seskey.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 111 141 78.7 %
Date: 2015-11-05 17:10:59 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* seskey.c -  make sesssion keys etc.
       2             :  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
       3             :  *               2006, 2009, 2010 Free Software Foundation, Inc.
       4             :  *
       5             :  * This file is part of GnuPG.
       6             :  *
       7             :  * GnuPG is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the GNU General Public License as published by
       9             :  * the Free Software Foundation; either version 3 of the License, or
      10             :  * (at your option) any later version.
      11             :  *
      12             :  * GnuPG is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  * GNU General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU General Public License
      18             :  * along with this program; if not, see <http://www.gnu.org/licenses/>.
      19             :  */
      20             : 
      21             : #include <config.h>
      22             : #include <stdio.h>
      23             : #include <stdlib.h>
      24             : #include <string.h>
      25             : #include <assert.h>
      26             : 
      27             : #include "gpg.h"
      28             : #include "util.h"
      29             : #include "options.h"
      30             : #include "main.h"
      31             : #include "i18n.h"
      32             : 
      33             : 
      34             : /****************
      35             :  * Make a session key and put it into DEK
      36             :  */
      37             : void
      38         236 : make_session_key( DEK *dek )
      39             : {
      40             :     gcry_cipher_hd_t chd;
      41             :     int i, rc;
      42             : 
      43         236 :     dek->keylen = openpgp_cipher_get_algo_keylen (dek->algo);
      44             : 
      45         236 :     if (openpgp_cipher_open (&chd, dek->algo, GCRY_CIPHER_MODE_CFB,
      46             :                              (GCRY_CIPHER_SECURE
      47             :                               | (dek->algo >= 100 ?
      48             :                                  0 : GCRY_CIPHER_ENABLE_SYNC))) )
      49           0 :       BUG();
      50         236 :     gcry_randomize (dek->key, dek->keylen, GCRY_STRONG_RANDOM );
      51         236 :     for (i=0; i < 16; i++ )
      52             :       {
      53         236 :         rc = gcry_cipher_setkey (chd, dek->key, dek->keylen);
      54         236 :         if (!rc)
      55             :           {
      56         236 :             gcry_cipher_close (chd);
      57         236 :             return;
      58             :           }
      59           0 :         if (gpg_err_code (rc) != GPG_ERR_WEAK_KEY)
      60           0 :           BUG();
      61           0 :         log_info(_("weak key created - retrying\n") );
      62             :         /* Renew the session key until we get a non-weak key. */
      63           0 :         gcry_randomize (dek->key, dek->keylen, GCRY_STRONG_RANDOM);
      64             :       }
      65           0 :     log_fatal (_("cannot avoid weak key for symmetric cipher; "
      66             :                  "tried %d times!\n"), i);
      67             : }
      68             : 
      69             : 
      70             : /****************
      71             :  * Encode the session key. NBITS is the number of bits which should be used
      72             :  * for packing the session key.
      73             :  * returns: A mpi with the session key (caller must free)
      74             :  */
      75             : gcry_mpi_t
      76         236 : encode_session_key (int openpgp_pk_algo, DEK *dek, unsigned int nbits)
      77             : {
      78         236 :   size_t nframe = (nbits+7) / 8;
      79             :   byte *p;
      80             :   byte *frame;
      81             :   int i,n;
      82             :   u16 csum;
      83             :   gcry_mpi_t a;
      84             : 
      85         236 :   if (DBG_CRYPTO)
      86           0 :     log_debug ("encode_session_key: encoding %d byte DEK", dek->keylen);
      87             : 
      88         236 :   csum = 0;
      89        6076 :   for (p = dek->key, i=0; i < dek->keylen; i++)
      90        5840 :     csum += *p++;
      91             : 
      92             :   /* Shortcut for ECDH.  It's padding is minimal to simply make the
      93             :      output be a multiple of 8 bytes.  */
      94         236 :   if (openpgp_pk_algo == PUBKEY_ALGO_ECDH)
      95             :     {
      96             :       /* Pad to 8 byte granulatiry; the padding byte is the number of
      97             :        * padded bytes.
      98             :        *
      99             :        * A  DEK(k bytes)  CSUM(2 bytes) 0x 0x 0x 0x ... 0x
     100             :        *                                +---- x times ---+
     101             :        */
     102          42 :       nframe = (( 1 + dek->keylen + 2 /* The value so far is always odd. */
     103          21 :                   + 7 ) & (~7));
     104             : 
     105             :       /* alg+key+csum fit and the size is congruent to 8.  */
     106          21 :       assert (!(nframe%8) && nframe > 1 + dek->keylen + 2 );
     107             : 
     108          21 :       frame = xmalloc_secure (nframe);
     109          21 :       n = 0;
     110          21 :       frame[n++] = dek->algo;
     111          21 :       memcpy (frame+n, dek->key, dek->keylen);
     112          21 :       n += dek->keylen;
     113          21 :       frame[n++] = csum >> 8;
     114          21 :       frame[n++] = csum;
     115          21 :       i = nframe - n;         /* Number of padded bytes.  */
     116          21 :       memset (frame+n, i, i); /* Use it as the value of each padded byte.  */
     117          21 :       assert (n+i == nframe);
     118             : 
     119          21 :       if (DBG_CRYPTO)
     120           0 :         log_debug ("encode_session_key: "
     121             :                    "[%d] %02x  %02x %02x ...  %02x %02x %02x\n",
     122           0 :                    (int) nframe, frame[0], frame[1], frame[2],
     123           0 :                    frame[nframe-3], frame[nframe-2], frame[nframe-1]);
     124             : 
     125          21 :       if (gcry_mpi_scan (&a, GCRYMPI_FMT_USG, frame, nframe, &nframe))
     126           0 :         BUG();
     127          21 :       xfree(frame);
     128          21 :       return a;
     129             :     }
     130             : 
     131             :   /* The current limitation is that we can only use a session key
     132             :    * whose length is a multiple of BITS_PER_MPI_LIMB
     133             :    * I think we can live with that.
     134             :    */
     135         215 :   if (dek->keylen + 7 > nframe || !nframe)
     136           0 :     log_bug ("can't encode a %d bit key in a %d bits frame\n",
     137           0 :              dek->keylen*8, nbits );
     138             : 
     139             :   /* We encode the session key in this way:
     140             :    *
     141             :    *       0  2  RND(n bytes)  0  A  DEK(k bytes)  CSUM(2 bytes)
     142             :    *
     143             :    * (But how can we store the leading 0 - the external representaion
     144             :    *  of MPIs doesn't allow leading zeroes =:-)
     145             :    *
     146             :    * RND are non-zero random bytes.
     147             :    * A   is the cipher algorithm
     148             :    * DEK is the encryption key (session key) length k depends on the
     149             :    *       cipher algorithm (20 is used with blowfish160).
     150             :    * CSUM is the 16 bit checksum over the DEK
     151             :    */
     152             : 
     153         215 :   frame = xmalloc_secure( nframe );
     154         215 :   n = 0;
     155         215 :   frame[n++] = 0;
     156         215 :   frame[n++] = 2;
     157         215 :   i = nframe - 6 - dek->keylen;
     158         215 :   assert( i > 0 );
     159         215 :   p = gcry_random_bytes_secure (i, GCRY_STRONG_RANDOM);
     160             :   /* Replace zero bytes by new values.  */
     161             :   for (;;)
     162             :     {
     163             :       int j, k;
     164             :       byte *pp;
     165             : 
     166             :       /* Count the zero bytes. */
     167       22352 :       for (j=k=0; j < i; j++ )
     168       22088 :         if (!p[j])
     169          60 :           k++;
     170         264 :       if (!k)
     171         215 :         break; /* Okay: no zero bytes. */
     172          49 :       k += k/128 + 3; /* Better get some more. */
     173          49 :       pp = gcry_random_bytes_secure (k, GCRY_STRONG_RANDOM);
     174        4100 :       for (j=0; j < i && k ;)
     175             :         {
     176        4002 :           if (!p[j])
     177          60 :             p[j] = pp[--k];
     178        4002 :           if (p[j])
     179        4002 :             j++;
     180             :         }
     181          49 :       xfree (pp);
     182          49 :     }
     183         215 :   memcpy (frame+n, p, i);
     184         215 :   xfree (p);
     185         215 :   n += i;
     186         215 :   frame[n++] = 0;
     187         215 :   frame[n++] = dek->algo;
     188         215 :   memcpy (frame+n, dek->key, dek->keylen );
     189         215 :   n += dek->keylen;
     190         215 :   frame[n++] = csum >>8;
     191         215 :   frame[n++] = csum;
     192         215 :   assert (n == nframe);
     193         215 :   if (gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, n, &nframe))
     194           0 :     BUG();
     195         215 :   xfree (frame);
     196         215 :   return a;
     197             : }
     198             : 
     199             : 
     200             : static gcry_mpi_t
     201          36 : do_encode_md( gcry_md_hd_t md, int algo, size_t len, unsigned nbits,
     202             :               const byte *asn, size_t asnlen )
     203             : {
     204          36 :     size_t nframe = (nbits+7) / 8;
     205             :     byte *frame;
     206             :     int i,n;
     207             :     gcry_mpi_t a;
     208             : 
     209          36 :     if( len + asnlen + 4  > nframe )
     210           0 :       log_bug ("can't encode a %d bit MD into a %d bits frame, algo=%d\n",
     211             :                (int)(len*8), (int)nbits, algo);
     212             : 
     213             :     /* We encode the MD in this way:
     214             :      *
     215             :      *     0  1 PAD(n bytes)   0  ASN(asnlen bytes)  MD(len bytes)
     216             :      *
     217             :      * PAD consists of FF bytes.
     218             :      */
     219          36 :     frame = gcry_md_is_secure (md)? xmalloc_secure (nframe) : xmalloc (nframe);
     220          36 :     n = 0;
     221          36 :     frame[n++] = 0;
     222          36 :     frame[n++] = 1; /* block type */
     223          36 :     i = nframe - len - asnlen -3 ;
     224          36 :     assert( i > 1 );
     225          36 :     memset( frame+n, 0xff, i ); n += i;
     226          36 :     frame[n++] = 0;
     227          36 :     memcpy( frame+n, asn, asnlen ); n += asnlen;
     228          36 :     memcpy( frame+n, gcry_md_read (md, algo), len ); n += len;
     229          36 :     assert( n == nframe );
     230             : 
     231          36 :     if (gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, n, &nframe ))
     232           0 :         BUG();
     233          36 :     xfree(frame);
     234             : 
     235             :     /* Note that PGP before version 2.3 encoded the MD as:
     236             :      *
     237             :      *   0   1   MD(16 bytes)   0   PAD(n bytes)   1
     238             :      *
     239             :      * The MD is always 16 bytes here because it's always MD5.  We do
     240             :      * not support pre-v2.3 signatures, but I'm including this comment
     241             :      * so the information is easily found in the future.
     242             :      */
     243             : 
     244          36 :     return a;
     245             : }
     246             : 
     247             : 
     248             : /****************
     249             :  * Encode a message digest into an MPI.
     250             :  * If it's for a DSA signature, make sure that the hash is large
     251             :  * enough to fill up q.  If the hash is too big, take the leftmost
     252             :  * bits.
     253             :  */
     254             : gcry_mpi_t
     255         367 : encode_md_value (PKT_public_key *pk, gcry_md_hd_t md, int hash_algo)
     256             : {
     257             :   gcry_mpi_t frame;
     258             :   size_t mdlen;
     259             : 
     260         367 :   assert (hash_algo);
     261         367 :   assert (pk);
     262             : 
     263         367 :   if (pk->pubkey_algo == PUBKEY_ALGO_EDDSA)
     264             :     {
     265             :       /* EdDSA signs data of arbitrary length.  Thus no special
     266             :          treatment is required.  */
     267           0 :       frame = gcry_mpi_set_opaque_copy (NULL, gcry_md_read (md, hash_algo),
     268           0 :                                         8*gcry_md_get_algo_dlen (hash_algo));
     269             :     }
     270         367 :   else if (pk->pubkey_algo == PUBKEY_ALGO_DSA
     271          73 :            || pk->pubkey_algo == PUBKEY_ALGO_ECDSA)
     272         331 :     {
     273             :       /* It's a DSA signature, so find out the size of q.  */
     274             : 
     275         331 :       size_t qbits = gcry_mpi_get_nbits (pk->pkey[1]);
     276             : 
     277             :       /* pkey[1] is Q for ECDSA, which is an uncompressed point,
     278             :          i.e.  04 <x> <y>  */
     279         331 :       if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA)
     280          37 :         qbits = ecdsa_qbits_from_Q (qbits);
     281             : 
     282             :       /* Make sure it is a multiple of 8 bits. */
     283         331 :       if ((qbits%8))
     284             :         {
     285           0 :           log_error(_("DSA requires the hash length to be a"
     286             :                       " multiple of 8 bits\n"));
     287           0 :           return NULL;
     288             :         }
     289             : 
     290             :       /* Don't allow any q smaller than 160 bits.  This might need a
     291             :          revisit as the DSA2 design firms up, but for now, we don't
     292             :          want someone to issue signatures from a key with a 16-bit q
     293             :          or something like that, which would look correct but allow
     294             :          trivial forgeries.  Yes, I know this rules out using MD5 with
     295             :          DSA. ;) */
     296         331 :       if (qbits < 160)
     297             :         {
     298           0 :           log_error (_("%s key %s uses an unsafe (%zu bit) hash\n"),
     299           0 :                      openpgp_pk_algo_name (pk->pubkey_algo),
     300             :                      keystr_from_pk (pk), qbits);
     301           0 :           return NULL;
     302             :         }
     303             : 
     304             : 
     305             :       /* ECDSA 521 is special has it is larger than the largest hash
     306             :          we have (SHA-512).  Thus we chnage the size for further
     307             :          processing to 512.  */
     308         331 :       if (pk->pubkey_algo == PUBKEY_ALGO_ECDSA && qbits > 512)
     309          11 :         qbits = 512;
     310             : 
     311             :       /* Check if we're too short.  Too long is safe as we'll
     312             :          automatically left-truncate.  */
     313         331 :       mdlen = gcry_md_get_algo_dlen (hash_algo);
     314         331 :       if (mdlen < qbits/8)
     315             :         {
     316           0 :           log_error (_("%s key %s requires a %zu bit or larger hash "
     317             :                        "(hash is %s)\n"),
     318           0 :                      openpgp_pk_algo_name (pk->pubkey_algo),
     319             :                      keystr_from_pk (pk), qbits,
     320             :                      gcry_md_algo_name (hash_algo));
     321           0 :           return NULL;
     322             :         }
     323             : 
     324             :      /* Note that we do the truncation by passing QBITS/8 as length to
     325             :         mpi_scan.  */
     326         662 :       if (gcry_mpi_scan (&frame, GCRYMPI_FMT_USG,
     327         331 :                          gcry_md_read (md, hash_algo), qbits/8, NULL))
     328           0 :         BUG();
     329             :     }
     330             :   else
     331             :     {
     332             :       gpg_error_t rc;
     333             :       byte *asn;
     334             :       size_t asnlen;
     335             : 
     336          36 :       rc = gcry_md_algo_info (hash_algo, GCRYCTL_GET_ASNOID, NULL, &asnlen);
     337          36 :       if (rc)
     338           0 :         log_fatal ("can't get OID of digest algorithm %d: %s\n",
     339             :                    hash_algo, gpg_strerror (rc));
     340          36 :       asn = xtrymalloc (asnlen);
     341          36 :       if (!asn)
     342           0 :         return NULL;
     343          36 :       if ( gcry_md_algo_info (hash_algo, GCRYCTL_GET_ASNOID, asn, &asnlen) )
     344           0 :         BUG();
     345          36 :       frame = do_encode_md (md, hash_algo, gcry_md_get_algo_dlen (hash_algo),
     346             :                             gcry_mpi_get_nbits (pk->pkey[0]), asn, asnlen);
     347          36 :       xfree (asn);
     348             :     }
     349             : 
     350         367 :   return frame;
     351             : }

Generated by: LCOV version 1.11