LCOV - code coverage report
Current view: top level - g10 - ecdh.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 115 198 58.1 %
Date: 2016-09-12 12:29:17 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /* ecdh.c - ECDH public key operations used in public key glue code
       2             :  *      Copyright (C) 2010, 2011 Free Software Foundation, Inc.
       3             :  *
       4             :  * This file is part of GnuPG.
       5             :  *
       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 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      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.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License
      17             :  * along with this program; if not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include <config.h>
      21             : #include <stdio.h>
      22             : #include <stdlib.h>
      23             : #include <string.h>
      24             : #include <errno.h>
      25             : 
      26             : #include "gpg.h"
      27             : #include "util.h"
      28             : #include "pkglue.h"
      29             : #include "main.h"
      30             : #include "options.h"
      31             : 
      32             : /* A table with the default KEK parameters used by GnuPG.  */
      33             : static const struct
      34             : {
      35             :   unsigned int qbits;
      36             :   int openpgp_hash_id;   /* KEK digest algorithm. */
      37             :   int openpgp_cipher_id; /* KEK cipher algorithm. */
      38             : } kek_params_table[] =
      39             :   /* Note: Must be sorted by ascending values for QBITS.  */
      40             :   {
      41             :     { 256, DIGEST_ALGO_SHA256, CIPHER_ALGO_AES    },
      42             :     { 384, DIGEST_ALGO_SHA384, CIPHER_ALGO_AES256 },
      43             : 
      44             :     /* Note: 528 is 521 rounded to the 8 bit boundary */
      45             :     { 528, DIGEST_ALGO_SHA512, CIPHER_ALGO_AES256 }
      46             :   };
      47             : 
      48             : 
      49             : 
      50             : /* Return KEK parameters as an opaque MPI The caller must free the
      51             :    returned value.  Returns NULL and sets ERRNO on error.  */
      52             : gcry_mpi_t
      53           0 : pk_ecdh_default_params (unsigned int qbits)
      54             : {
      55             :   byte *kek_params;
      56             :   int i;
      57             : 
      58           0 :   kek_params = xtrymalloc (4);
      59           0 :   if (!kek_params)
      60           0 :     return NULL;
      61           0 :   kek_params[0] = 3; /* Number of bytes to follow. */
      62           0 :   kek_params[1] = 1; /* Version for KDF+AESWRAP.   */
      63             : 
      64             :   /* Search for matching KEK parameter.  Defaults to the strongest
      65             :      possible choices.  Performance is not an issue here, only
      66             :      interoperability.  */
      67           0 :   for (i=0; i < DIM (kek_params_table); i++)
      68             :     {
      69           0 :       if (kek_params_table[i].qbits >= qbits
      70           0 :           || i+1 == DIM (kek_params_table))
      71             :         {
      72           0 :           kek_params[2] = kek_params_table[i].openpgp_hash_id;
      73           0 :           kek_params[3] = kek_params_table[i].openpgp_cipher_id;
      74           0 :           break;
      75             :         }
      76             :     }
      77           0 :   log_assert (i < DIM (kek_params_table));
      78           0 :   if (DBG_CRYPTO)
      79           0 :     log_printhex ("ECDH KEK params are", kek_params, sizeof(kek_params) );
      80             : 
      81           0 :   return gcry_mpi_set_opaque (NULL, kek_params, 4 * 8);
      82             : }
      83             : 
      84             : 
      85             : /* Encrypts/decrypts DATA using a key derived from the ECC shared
      86             :    point SHARED_MPI using the FIPS SP 800-56A compliant method
      87             :    key_derivation+key_wrapping.  If IS_ENCRYPT is true the function
      88             :    encrypts; if false, it decrypts.  PKEY is the public key and PK_FP
      89             :    the fingerprint of this public key.  On success the result is
      90             :    stored at R_RESULT; on failure NULL is stored at R_RESULT and an
      91             :    error code returned.  */
      92             : gpg_error_t
      93          48 : pk_ecdh_encrypt_with_shared_point (int is_encrypt, gcry_mpi_t shared_mpi,
      94             :                                    const byte pk_fp[MAX_FINGERPRINT_LEN],
      95             :                                    gcry_mpi_t data, gcry_mpi_t *pkey,
      96             :                                    gcry_mpi_t *r_result)
      97             : {
      98             :   gpg_error_t err;
      99             :   byte *secret_x;
     100             :   int secret_x_size;
     101             :   unsigned int nbits;
     102             :   const unsigned char *kek_params;
     103             :   size_t kek_params_size;
     104             :   int kdf_hash_algo;
     105             :   int kdf_encr_algo;
     106             :   unsigned char message[256];
     107             :   size_t message_size;
     108             : 
     109          48 :   *r_result = NULL;
     110             : 
     111          48 :   nbits = pubkey_nbits (PUBKEY_ALGO_ECDH, pkey);
     112          48 :   if (!nbits)
     113           0 :     return gpg_error (GPG_ERR_TOO_SHORT);
     114             : 
     115             :   {
     116             :     size_t nbytes;
     117             : 
     118             :     /* Extract x component of the shared point: this is the actual
     119             :        shared secret. */
     120          48 :     nbytes = (mpi_get_nbits (pkey[1] /* public point */)+7)/8;
     121          48 :     secret_x = xtrymalloc_secure (nbytes);
     122          48 :     if (!secret_x)
     123           0 :       return gpg_error_from_syserror ();
     124             : 
     125          48 :     err = gcry_mpi_print (GCRYMPI_FMT_USG, secret_x, nbytes,
     126             :                           &nbytes, shared_mpi);
     127          48 :     if (err)
     128             :       {
     129           0 :         xfree (secret_x);
     130           0 :         log_error ("ECDH ephemeral export of shared point failed: %s\n",
     131             :                    gpg_strerror (err));
     132           0 :         return err;
     133             :       }
     134             : 
     135          48 :     secret_x_size = (nbits+7)/8;
     136          48 :     log_assert (nbytes >= secret_x_size);
     137          48 :     if ((nbytes & 1))
     138             :       /* Remove the "04" prefix of non-compressed format.  */
     139          48 :       memmove (secret_x, secret_x+1, secret_x_size);
     140          48 :     if (nbytes - secret_x_size)
     141          48 :       memset (secret_x+secret_x_size, 0, nbytes-secret_x_size);
     142             : 
     143          48 :     if (DBG_CRYPTO)
     144           0 :       log_printhex ("ECDH shared secret X is:", secret_x, secret_x_size );
     145             :   }
     146             : 
     147             :   /*** We have now the shared secret bytes in secret_x. ***/
     148             : 
     149             :   /* At this point we are done with PK encryption and the rest of the
     150             :    * function uses symmetric key encryption techniques to protect the
     151             :    * input DATA.  The following two sections will simply replace
     152             :    * current secret_x with a value derived from it.  This will become
     153             :    * a KEK.
     154             :    */
     155          48 :   if (!gcry_mpi_get_flag (pkey[2], GCRYMPI_FLAG_OPAQUE))
     156             :     {
     157           0 :       xfree (secret_x);
     158           0 :       return gpg_error (GPG_ERR_BUG);
     159             :     }
     160          48 :   kek_params = gcry_mpi_get_opaque (pkey[2], &nbits);
     161          48 :   kek_params_size = (nbits+7)/8;
     162             : 
     163          48 :   if (DBG_CRYPTO)
     164           0 :     log_printhex ("ecdh KDF params:", kek_params, kek_params_size);
     165             : 
     166             :   /* Expect 4 bytes  03 01 hash_alg symm_alg.  */
     167          48 :   if (kek_params_size != 4 || kek_params[0] != 3 || kek_params[1] != 1)
     168             :     {
     169           0 :       xfree (secret_x);
     170           0 :       return gpg_error (GPG_ERR_BAD_PUBKEY);
     171             :     }
     172             : 
     173          48 :   kdf_hash_algo = kek_params[2];
     174          48 :   kdf_encr_algo = kek_params[3];
     175             : 
     176          48 :   if (DBG_CRYPTO)
     177           0 :     log_debug ("ecdh KDF algorithms %s+%s with aeswrap\n",
     178             :                openpgp_md_algo_name (kdf_hash_algo),
     179             :                openpgp_cipher_algo_name (kdf_encr_algo));
     180             : 
     181          48 :   if (kdf_hash_algo != GCRY_MD_SHA256
     182          30 :       && kdf_hash_algo != GCRY_MD_SHA384
     183          15 :       && kdf_hash_algo != GCRY_MD_SHA512)
     184             :     {
     185           0 :       xfree (secret_x);
     186           0 :       return gpg_error (GPG_ERR_BAD_PUBKEY);
     187             :     }
     188          48 :   if (kdf_encr_algo != CIPHER_ALGO_AES
     189          30 :       && kdf_encr_algo != CIPHER_ALGO_AES192
     190          30 :       && kdf_encr_algo != CIPHER_ALGO_AES256)
     191             :     {
     192           0 :       xfree (secret_x);
     193           0 :       return gpg_error (GPG_ERR_BAD_PUBKEY);
     194             :     }
     195             : 
     196             :   /* Build kdf_params.  */
     197             :   {
     198             :     IOBUF obuf;
     199             : 
     200          48 :     obuf = iobuf_temp();
     201             :     /* variable-length field 1, curve name OID */
     202          48 :     err = gpg_mpi_write_nohdr (obuf, pkey[0]);
     203             :     /* fixed-length field 2 */
     204          48 :     iobuf_put (obuf, PUBKEY_ALGO_ECDH);
     205             :     /* variable-length field 3, KDF params */
     206          48 :     err = (err ? err : gpg_mpi_write_nohdr (obuf, pkey[2]));
     207             :     /* fixed-length field 4 */
     208          48 :     iobuf_write (obuf, "Anonymous Sender    ", 20);
     209             :     /* fixed-length field 5, recipient fp */
     210          48 :     iobuf_write (obuf, pk_fp, 20);
     211             : 
     212          48 :     message_size = iobuf_temp_to_buffer (obuf, message, sizeof message);
     213          48 :     iobuf_close (obuf);
     214          48 :     if (err)
     215             :       {
     216           0 :         xfree (secret_x);
     217           0 :         return err;
     218             :       }
     219             : 
     220          48 :     if(DBG_CRYPTO)
     221           0 :       log_printhex ("ecdh KDF message params are:", message, message_size);
     222             :   }
     223             : 
     224             :   /* Derive a KEK (key wrapping key) using MESSAGE and SECRET_X. */
     225             :   {
     226             :     gcry_md_hd_t h;
     227             :     int old_size;
     228             : 
     229          48 :     err = gcry_md_open (&h, kdf_hash_algo, 0);
     230          48 :     if (err)
     231             :       {
     232           0 :         log_error ("gcry_md_open failed for kdf_hash_algo %d: %s",
     233             :                    kdf_hash_algo, gpg_strerror (err));
     234           0 :         xfree (secret_x);
     235           0 :         return err;
     236             :       }
     237          48 :     gcry_md_write(h, "\x00\x00\x00\x01", 4);      /* counter = 1 */
     238          48 :     gcry_md_write(h, secret_x, secret_x_size);    /* x of the point X */
     239          48 :     gcry_md_write(h, message, message_size);/* KDF parameters */
     240             : 
     241          48 :     gcry_md_final (h);
     242             : 
     243          48 :     log_assert( gcry_md_get_algo_dlen (kdf_hash_algo) >= 32 );
     244             : 
     245          48 :     memcpy (secret_x, gcry_md_read (h, kdf_hash_algo),
     246          48 :             gcry_md_get_algo_dlen (kdf_hash_algo));
     247          48 :     gcry_md_close (h);
     248             : 
     249          48 :     old_size = secret_x_size;
     250          48 :     log_assert( old_size >= gcry_cipher_get_algo_keylen( kdf_encr_algo ) );
     251          48 :     secret_x_size = gcry_cipher_get_algo_keylen( kdf_encr_algo );
     252          48 :     log_assert( secret_x_size <= gcry_md_get_algo_dlen (kdf_hash_algo) );
     253             : 
     254             :     /* We could have allocated more, so clean the tail before returning.  */
     255          48 :     memset (secret_x+secret_x_size, 0, old_size - secret_x_size);
     256          48 :     if (DBG_CRYPTO)
     257           0 :       log_printhex ("ecdh KEK is:", secret_x, secret_x_size );
     258             :   }
     259             : 
     260             :   /* And, finally, aeswrap with key secret_x.  */
     261             :   {
     262             :     gcry_cipher_hd_t hd;
     263             :     size_t nbytes;
     264             : 
     265             :     byte *data_buf;
     266             :     int data_buf_size;
     267             : 
     268             :     gcry_mpi_t result;
     269             : 
     270          48 :     err = gcry_cipher_open (&hd, kdf_encr_algo, GCRY_CIPHER_MODE_AESWRAP, 0);
     271          48 :     if (err)
     272             :       {
     273           0 :         log_error ("ecdh failed to initialize AESWRAP: %s\n",
     274             :                    gpg_strerror (err));
     275           0 :         xfree (secret_x);
     276           0 :         return err;
     277             :       }
     278             : 
     279          48 :     err = gcry_cipher_setkey (hd, secret_x, secret_x_size);
     280          48 :     xfree (secret_x);
     281          48 :     secret_x = NULL;
     282          48 :     if (err)
     283             :       {
     284           0 :         gcry_cipher_close (hd);
     285           0 :         log_error ("ecdh failed in gcry_cipher_setkey: %s\n",
     286             :                    gpg_strerror (err));
     287           0 :         return err;
     288             :       }
     289             : 
     290          48 :     data_buf_size = (gcry_mpi_get_nbits(data)+7)/8;
     291          48 :     if ((data_buf_size & 7) != (is_encrypt ? 0 : 1))
     292             :       {
     293           0 :         log_error ("can't use a shared secret of %d bytes for ecdh\n",
     294             :                    data_buf_size);
     295           0 :         return gpg_error (GPG_ERR_BAD_DATA);
     296             :       }
     297             : 
     298          48 :     data_buf = xtrymalloc_secure( 1 + 2*data_buf_size + 8);
     299          48 :     if (!data_buf)
     300             :       {
     301           0 :         err = gpg_error_from_syserror ();
     302           0 :         gcry_cipher_close (hd);
     303           0 :         return err;
     304             :       }
     305             : 
     306          48 :     if (is_encrypt)
     307             :       {
     308          24 :         byte *in = data_buf+1+data_buf_size+8;
     309             : 
     310             :         /* Write data MPI into the end of data_buf. data_buf is size
     311             :            aeswrap data.  */
     312          24 :         err = gcry_mpi_print (GCRYMPI_FMT_USG, in,
     313             :                              data_buf_size, &nbytes, data/*in*/);
     314          24 :         if (err)
     315             :           {
     316           0 :             log_error ("ecdh failed to export DEK: %s\n", gpg_strerror (err));
     317           0 :             gcry_cipher_close (hd);
     318           0 :             xfree (data_buf);
     319           0 :             return err;
     320             :           }
     321             : 
     322          24 :         if (DBG_CRYPTO)
     323           0 :           log_printhex ("ecdh encrypting  :", in, data_buf_size );
     324             : 
     325          24 :         err = gcry_cipher_encrypt (hd, data_buf+1, data_buf_size+8,
     326             :                                    in, data_buf_size);
     327          24 :         memset (in, 0, data_buf_size);
     328          24 :         gcry_cipher_close (hd);
     329          24 :         if (err)
     330             :           {
     331           0 :             log_error ("ecdh failed in gcry_cipher_encrypt: %s\n",
     332             :                        gpg_strerror (err));
     333           0 :             xfree (data_buf);
     334           0 :             return err;
     335             :           }
     336          24 :         data_buf[0] = data_buf_size+8;
     337             : 
     338          24 :         if (DBG_CRYPTO)
     339           0 :           log_printhex ("ecdh encrypted to:", data_buf+1, data_buf[0] );
     340             : 
     341          24 :         result = gcry_mpi_set_opaque (NULL, data_buf, 8 * (1+data_buf[0]));
     342          24 :         if (!result)
     343             :           {
     344           0 :             err = gpg_error_from_syserror ();
     345           0 :             xfree (data_buf);
     346           0 :             log_error ("ecdh failed to create an MPI: %s\n",
     347             :                        gpg_strerror (err));
     348           0 :             return err;
     349             :           }
     350             : 
     351          24 :         *r_result = result;
     352             :       }
     353             :     else
     354             :       {
     355             :         byte *in;
     356             :         const void *p;
     357             : 
     358          24 :         p = gcry_mpi_get_opaque (data, &nbits);
     359          24 :         nbytes = (nbits+7)/8;
     360          24 :         if (!p || nbytes > data_buf_size || !nbytes)
     361             :           {
     362           0 :             xfree (data_buf);
     363           0 :             return gpg_error (GPG_ERR_BAD_MPI);
     364             :           }
     365          24 :         memcpy (data_buf, p, nbytes);
     366          24 :         if (data_buf[0] != nbytes-1)
     367             :           {
     368           0 :             log_error ("ecdh inconsistent size\n");
     369           0 :             xfree (data_buf);
     370           0 :             return gpg_error (GPG_ERR_BAD_MPI);
     371             :           }
     372          24 :         in = data_buf+data_buf_size;
     373          24 :         data_buf_size = data_buf[0];
     374             : 
     375          24 :         if (DBG_CRYPTO)
     376           0 :           log_printhex ("ecdh decrypting :", data_buf+1, data_buf_size);
     377             : 
     378          24 :         err = gcry_cipher_decrypt (hd, in, data_buf_size, data_buf+1,
     379             :                                    data_buf_size);
     380          24 :         gcry_cipher_close (hd);
     381          24 :         if (err)
     382             :           {
     383           0 :             log_error ("ecdh failed in gcry_cipher_decrypt: %s\n",
     384             :                        gpg_strerror (err));
     385           0 :             xfree (data_buf);
     386           0 :             return err;
     387             :           }
     388             : 
     389          24 :         data_buf_size -= 8;
     390             : 
     391          24 :         if (DBG_CRYPTO)
     392           0 :           log_printhex ("ecdh decrypted to :", in, data_buf_size);
     393             : 
     394             :         /* Padding is removed later.  */
     395             :         /* if (in[data_buf_size-1] > 8 ) */
     396             :         /*   { */
     397             :         /*     log_error ("ecdh failed at decryption: invalid padding." */
     398             :         /*                " 0x%02x > 8\n", in[data_buf_size-1] ); */
     399             :         /*     return gpg_error (GPG_ERR_BAD_KEY); */
     400             :         /*   } */
     401             : 
     402          24 :         err = gcry_mpi_scan (&result, GCRYMPI_FMT_USG, in, data_buf_size, NULL);
     403          24 :         xfree (data_buf);
     404          24 :         if (err)
     405             :           {
     406           0 :             log_error ("ecdh failed to create a plain text MPI: %s\n",
     407             :                        gpg_strerror (err));
     408           0 :             return err;
     409             :           }
     410             : 
     411          24 :         *r_result = result;
     412             :       }
     413             :   }
     414             : 
     415          48 :   return err;
     416             : }
     417             : 
     418             : 
     419             : static gcry_mpi_t
     420          24 : gen_k (unsigned nbits)
     421             : {
     422             :   gcry_mpi_t k;
     423             : 
     424          24 :   k = gcry_mpi_snew (nbits);
     425          24 :   if (DBG_CRYPTO)
     426           0 :     log_debug ("choosing a random k of %u bits\n", nbits);
     427             : 
     428          24 :   gcry_mpi_randomize (k, nbits-1, GCRY_STRONG_RANDOM);
     429             : 
     430          24 :   if (DBG_CRYPTO)
     431             :     {
     432             :       unsigned char *buffer;
     433           0 :       if (gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buffer, NULL, k))
     434           0 :         BUG ();
     435           0 :       log_debug ("ephemeral scalar MPI #0: %s\n", buffer);
     436           0 :       gcry_free (buffer);
     437             :     }
     438             : 
     439          24 :   return k;
     440             : }
     441             : 
     442             : 
     443             : /* Generate an ephemeral key for the public ECDH key in PKEY.  On
     444             :    success the generated key is stored at R_K; on failure NULL is
     445             :    stored at R_K and an error code returned.  */
     446             : gpg_error_t
     447          24 : pk_ecdh_generate_ephemeral_key (gcry_mpi_t *pkey, gcry_mpi_t *r_k)
     448             : {
     449             :   unsigned int nbits;
     450             :   gcry_mpi_t k;
     451             : 
     452          24 :   *r_k = NULL;
     453             : 
     454          24 :   nbits = pubkey_nbits (PUBKEY_ALGO_ECDH, pkey);
     455          24 :   if (!nbits)
     456           0 :     return gpg_error (GPG_ERR_TOO_SHORT);
     457          24 :   k = gen_k (nbits);
     458          24 :   if (!k)
     459           0 :     BUG ();
     460             : 
     461          24 :   *r_k = k;
     462          24 :   return 0;
     463             : }
     464             : 
     465             : 
     466             : 
     467             : /* Perform ECDH decryption.   */
     468             : int
     469          24 : pk_ecdh_decrypt (gcry_mpi_t * result, const byte sk_fp[MAX_FINGERPRINT_LEN],
     470             :                  gcry_mpi_t data, gcry_mpi_t shared, gcry_mpi_t * skey)
     471             : {
     472          24 :   if (!data)
     473           0 :     return gpg_error (GPG_ERR_BAD_MPI);
     474          24 :   return pk_ecdh_encrypt_with_shared_point (0 /*=decryption*/, shared,
     475             :                                             sk_fp, data/*encr data as an MPI*/,
     476             :                                             skey, result);
     477             : }

Generated by: LCOV version 1.11