1 /* ecdh.c - ECDH public key operations used in public key glue code
2 * Copyright (C) 2010, 2011 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
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.
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.
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/>.
33 /* A table with the default KEK parameters used by GnuPG. */
37 int openpgp_hash_id; /* KEK digest algorithm. */
38 int openpgp_cipher_id; /* KEK cipher algorithm. */
39 } kek_params_table[] =
40 /* Note: Must be sorted by ascending values for QBITS. */
42 { 256, DIGEST_ALGO_SHA256, CIPHER_ALGO_AES },
43 { 384, DIGEST_ALGO_SHA384, CIPHER_ALGO_AES256 },
45 /* Note: 528 is 521 rounded to the 8 bit boundary */
46 { 528, DIGEST_ALGO_SHA512, CIPHER_ALGO_AES256 }
51 /* Return KEK parameters as an opaque MPI The caller must free the
52 returned value. Returns NULL and sets ERRNO on error. */
54 pk_ecdh_default_params (unsigned int qbits)
59 kek_params = xtrymalloc (4);
62 kek_params[0] = 3; /* Number of bytes to follow. */
63 kek_params[1] = 1; /* Version for KDF+AESWRAP. */
65 /* Search for matching KEK parameter. Defaults to the strongest
66 possible choices. Performance is not an issue here, only
68 for (i=0; i < DIM (kek_params_table); i++)
70 if (kek_params_table[i].qbits >= qbits
71 || i+1 == DIM (kek_params_table))
73 kek_params[2] = kek_params_table[i].openpgp_hash_id;
74 kek_params[3] = kek_params_table[i].openpgp_cipher_id;
78 assert (i < DIM (kek_params_table));
80 log_printhex ("ECDH KEK params are", kek_params, sizeof(kek_params) );
82 return gcry_mpi_set_opaque (NULL, kek_params, 4 * 8);
86 /* Encrypts/decrypts DATA using a key derived from the ECC shared
87 point SHARED_MPI using the FIPS SP 800-56A compliant method
88 key_derivation+key_wrapping. If IS_ENCRYPT is true the function
89 encrypts; if false, it decrypts. PKEY is the public key and PK_FP
90 the fingerprint of this public key. On success the result is
91 stored at R_RESULT; on failure NULL is stored at R_RESULT and an
92 error code returned. */
94 pk_ecdh_encrypt_with_shared_point (int is_encrypt, gcry_mpi_t shared_mpi,
95 const byte pk_fp[MAX_FINGERPRINT_LEN],
96 gcry_mpi_t data, gcry_mpi_t *pkey,
103 const unsigned char *kek_params;
104 size_t kek_params_size;
107 unsigned char message[256];
112 nbits = pubkey_nbits (PUBKEY_ALGO_ECDH, pkey);
114 return gpg_error (GPG_ERR_TOO_SHORT);
119 /* Extract x component of the shared point: this is the actual
121 nbytes = (mpi_get_nbits (pkey[1] /* public point */)+7)/8;
122 secret_x = xtrymalloc_secure (nbytes);
124 return gpg_error_from_syserror ();
126 err = gcry_mpi_print (GCRYMPI_FMT_USG, secret_x, nbytes,
127 &nbytes, shared_mpi);
131 log_error ("ECDH ephemeral export of shared point failed: %s\n",
136 secret_x_size = (nbits+7)/8;
137 assert (nbytes > secret_x_size);
138 memmove (secret_x, secret_x+1, secret_x_size);
139 memset (secret_x+secret_x_size, 0, nbytes-secret_x_size);
142 log_printhex ("ECDH shared secret X is:", secret_x, secret_x_size );
145 /*** We have now the shared secret bytes in secret_x. ***/
147 /* At this point we are done with PK encryption and the rest of the
148 * function uses symmetric key encryption techniques to protect the
149 * input DATA. The following two sections will simply replace
150 * current secret_x with a value derived from it. This will become
153 if (!gcry_mpi_get_flag (pkey[2], GCRYMPI_FLAG_OPAQUE))
156 return gpg_error (GPG_ERR_BUG);
158 kek_params = gcry_mpi_get_opaque (pkey[2], &nbits);
159 kek_params_size = (nbits+7)/8;
162 log_printhex ("ecdh KDF params:", kek_params, kek_params_size);
164 /* Expect 4 bytes 03 01 hash_alg symm_alg. */
165 if (kek_params_size != 4 || kek_params[0] != 3 || kek_params[1] != 1)
168 return gpg_error (GPG_ERR_BAD_PUBKEY);
171 kdf_hash_algo = kek_params[2];
172 kdf_encr_algo = kek_params[3];
175 log_debug ("ecdh KDF algorithms %s+%s with aeswrap\n",
176 openpgp_md_algo_name (kdf_hash_algo),
177 openpgp_cipher_algo_name (kdf_encr_algo));
179 if (kdf_hash_algo != GCRY_MD_SHA256
180 && kdf_hash_algo != GCRY_MD_SHA384
181 && kdf_hash_algo != GCRY_MD_SHA512)
184 return gpg_error (GPG_ERR_BAD_PUBKEY);
186 if (kdf_encr_algo != CIPHER_ALGO_AES
187 && kdf_encr_algo != CIPHER_ALGO_AES192
188 && kdf_encr_algo != CIPHER_ALGO_AES256)
191 return gpg_error (GPG_ERR_BAD_PUBKEY);
194 /* Build kdf_params. */
199 /* variable-length field 1, curve name OID */
200 err = gpg_mpi_write_nohdr (obuf, pkey[0]);
201 /* fixed-length field 2 */
202 iobuf_put (obuf, PUBKEY_ALGO_ECDH);
203 /* variable-length field 3, KDF params */
204 err = (err ? err : gpg_mpi_write_nohdr (obuf, pkey[2]));
205 /* fixed-length field 4 */
206 iobuf_write (obuf, "Anonymous Sender ", 20);
207 /* fixed-length field 5, recipient fp */
208 iobuf_write (obuf, pk_fp, 20);
210 message_size = iobuf_temp_to_buffer (obuf, message, sizeof message);
219 log_printhex ("ecdh KDF message params are:", message, message_size);
222 /* Derive a KEK (key wrapping key) using MESSAGE and SECRET_X. */
227 err = gcry_md_open (&h, kdf_hash_algo, 0);
230 log_error ("gcry_md_open failed for kdf_hash_algo %d: %s",
231 kdf_hash_algo, gpg_strerror (err));
235 gcry_md_write(h, "\x00\x00\x00\x01", 4); /* counter = 1 */
236 gcry_md_write(h, secret_x, secret_x_size); /* x of the point X */
237 gcry_md_write(h, message, message_size);/* KDF parameters */
241 assert( gcry_md_get_algo_dlen (kdf_hash_algo) >= 32 );
243 memcpy (secret_x, gcry_md_read (h, kdf_hash_algo),
244 gcry_md_get_algo_dlen (kdf_hash_algo));
247 old_size = secret_x_size;
248 assert( old_size >= gcry_cipher_get_algo_keylen( kdf_encr_algo ) );
249 secret_x_size = gcry_cipher_get_algo_keylen( kdf_encr_algo );
250 assert( secret_x_size <= gcry_md_get_algo_dlen (kdf_hash_algo) );
252 /* We could have allocated more, so clean the tail before returning. */
253 memset (secret_x+secret_x_size, 0, old_size - secret_x_size);
255 log_printhex ("ecdh KEK is:", secret_x, secret_x_size );
258 /* And, finally, aeswrap with key secret_x. */
268 err = gcry_cipher_open (&hd, kdf_encr_algo, GCRY_CIPHER_MODE_AESWRAP, 0);
271 log_error ("ecdh failed to initialize AESWRAP: %s\n",
277 err = gcry_cipher_setkey (hd, secret_x, secret_x_size);
282 gcry_cipher_close (hd);
283 log_error ("ecdh failed in gcry_cipher_setkey: %s\n",
288 data_buf_size = (gcry_mpi_get_nbits(data)+7)/8;
289 if ((data_buf_size & 7) != (is_encrypt ? 0 : 1))
291 log_error ("can't use a shared secret of %d bytes for ecdh\n",
293 return gpg_error (GPG_ERR_BAD_DATA);
296 data_buf = xtrymalloc_secure( 1 + 2*data_buf_size + 8);
299 err = gpg_error_from_syserror ();
300 gcry_cipher_close (hd);
306 byte *in = data_buf+1+data_buf_size+8;
308 /* Write data MPI into the end of data_buf. data_buf is size
310 err = gcry_mpi_print (GCRYMPI_FMT_USG, in,
311 data_buf_size, &nbytes, data/*in*/);
314 log_error ("ecdh failed to export DEK: %s\n", gpg_strerror (err));
315 gcry_cipher_close (hd);
321 log_printhex ("ecdh encrypting :", in, data_buf_size );
323 err = gcry_cipher_encrypt (hd, data_buf+1, data_buf_size+8,
325 memset (in, 0, data_buf_size);
326 gcry_cipher_close (hd);
329 log_error ("ecdh failed in gcry_cipher_encrypt: %s\n",
334 data_buf[0] = data_buf_size+8;
337 log_printhex ("ecdh encrypted to:", data_buf+1, data_buf[0] );
339 result = gcry_mpi_set_opaque (NULL, data_buf, 8 * (1+data_buf[0]));
342 err = gpg_error_from_syserror ();
344 log_error ("ecdh failed to create an MPI: %s\n",
356 p = gcry_mpi_get_opaque (data, &nbits);
357 nbytes = (nbits+7)/8;
358 if (!p || nbytes > data_buf_size || !nbytes)
361 return gpg_error (GPG_ERR_BAD_MPI);
363 memcpy (data_buf, p, nbytes);
364 if (data_buf[0] != nbytes-1)
366 log_error ("ecdh inconsistent size\n");
368 return gpg_error (GPG_ERR_BAD_MPI);
370 in = data_buf+data_buf_size;
371 data_buf_size = data_buf[0];
374 log_printhex ("ecdh decrypting :", data_buf+1, data_buf_size);
376 err = gcry_cipher_decrypt (hd, in, data_buf_size, data_buf+1,
378 gcry_cipher_close (hd);
381 log_error ("ecdh failed in gcry_cipher_decrypt: %s\n",
390 log_printhex ("ecdh decrypted to :", in, data_buf_size);
392 /* Padding is removed later. */
393 /* if (in[data_buf_size-1] > 8 ) */
395 /* log_error ("ecdh failed at decryption: invalid padding." */
396 /* " 0x%02x > 8\n", in[data_buf_size-1] ); */
397 /* return gpg_error (GPG_ERR_BAD_KEY); */
400 err = gcry_mpi_scan (&result, GCRYMPI_FMT_USG, in, data_buf_size, NULL);
404 log_error ("ecdh failed to create a plain text MPI: %s\n",
418 gen_k (unsigned nbits)
422 k = gcry_mpi_snew (nbits);
424 log_debug ("choosing a random k of %u bits\n", nbits);
426 gcry_mpi_randomize (k, nbits-1, GCRY_STRONG_RANDOM);
430 unsigned char *buffer;
431 if (gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buffer, NULL, k))
433 log_debug ("ephemeral scalar MPI #0: %s\n", buffer);
441 /* Generate an ephemeral key for the public ECDH key in PKEY. On
442 success the generated key is stored at R_K; on failure NULL is
443 stored at R_K and an error code returned. */
445 pk_ecdh_generate_ephemeral_key (gcry_mpi_t *pkey, gcry_mpi_t *r_k)
452 nbits = pubkey_nbits (PUBKEY_ALGO_ECDH, pkey);
454 return gpg_error (GPG_ERR_TOO_SHORT);
465 /* Perform ECDH decryption. */
467 pk_ecdh_decrypt (gcry_mpi_t * result, const byte sk_fp[MAX_FINGERPRINT_LEN],
468 gcry_mpi_t data, gcry_mpi_t shared, gcry_mpi_t * skey)
471 return gpg_error (GPG_ERR_BAD_MPI);
472 return pk_ecdh_encrypt_with_shared_point (0 /*=decryption*/, shared,
473 sk_fp, data/*encr data as an MPI*/,