2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/crypto_ecc.c
23 * @brief public key cryptography (ECC) with libgcrypt
24 * @author Christian Grothoff
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
31 #define EXTRA_CHECKS ALLOW_EXTRA_CHECKS
33 #define CURVE "NIST P-256"
35 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
39 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
42 * Log an error message at log-level 'level' that indicates
43 * a failure of the command 'cmd' with the message given
44 * by gcry_strerror(rc).
46 #define LOG_GCRY(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0);
50 * The private information of an ECC private key.
52 struct GNUNET_CRYPTO_EccPrivateKey
56 * Libgcrypt S-expression for the ECC key.
63 * Free memory occupied by ECC key
65 * @param privatekey pointer to the memory to free
68 GNUNET_CRYPTO_ecc_key_free (struct GNUNET_CRYPTO_EccPrivateKey *privatekey)
70 gcry_sexp_release (privatekey->sexp);
71 GNUNET_free (privatekey);
76 * Extract values from an S-expression.
78 * @param array where to store the result(s)
79 * @param sexp S-expression to parse
80 * @param topname top-level name in the S-expression that is of interest
81 * @param elems names of the elements to extract
82 * @return 0 on success
85 key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname,
94 list = gcry_sexp_find_token (sexp, topname, 0);
97 l2 = gcry_sexp_cadr (list);
98 gcry_sexp_release (list);
104 for (s = elems; *s; s++, idx++)
106 l2 = gcry_sexp_find_token (list, s, 1);
109 for (i = 0; i < idx; i++)
111 gcry_free (array[i]);
114 gcry_sexp_release (list);
115 return 3; /* required parameter not found */
117 array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
118 gcry_sexp_release (l2);
121 for (i = 0; i < idx; i++)
123 gcry_free (array[i]);
126 gcry_sexp_release (list);
127 return 4; /* required parameter is invalid */
130 gcry_sexp_release (list);
136 * Extract the public key for the given private key.
138 * @param priv the private key
139 * @param pub where to write the public key
142 GNUNET_CRYPTO_ecc_key_get_public (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
143 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub)
149 memset (pub, 0, sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded));
150 rc = key_from_sexp (&skey, priv->sexp, "public-key", "q");
152 rc = key_from_sexp (&skey, priv->sexp, "private-key", "q");
154 rc = key_from_sexp (&skey, priv->sexp, "ecc", "q");
155 GNUNET_assert (0 == rc);
156 pub->size = htons (sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded));
157 size = GNUNET_CRYPTO_ECC_MAX_PUBLIC_KEY_LENGTH;
159 gcry_mpi_print (GCRYMPI_FMT_USG, pub->key, size, &size,
161 pub->len = htons (size);
162 gcry_mpi_release (skey);
167 * Convert a public key to a string.
169 * @param pub key to convert
170 * @return string representing 'pub'
173 GNUNET_CRYPTO_ecc_public_key_to_string (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub)
176 size_t keylen = (sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded)) * 8;
180 keylen += 5 - keylen % 5;
182 pubkeybuf = GNUNET_malloc (keylen + 1);
183 end = GNUNET_STRINGS_data_to_string ((unsigned char *) pub,
184 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
189 GNUNET_free (pubkeybuf);
198 * Convert a string representing a public key to a public key.
200 * @param enc encoded public key
201 * @param enclen number of bytes in enc (without 0-terminator)
202 * @param pub where to store the public key
203 * @return GNUNET_OK on success
206 GNUNET_CRYPTO_ecc_public_key_from_string (const char *enc,
208 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub)
210 size_t keylen = (sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded)) * 8;
213 keylen += 5 - keylen % 5;
215 if (enclen != keylen)
216 return GNUNET_SYSERR;
218 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, enclen,
220 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded)))
221 return GNUNET_SYSERR;
222 if ( (ntohs (pub->size) != sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded)) ||
223 (ntohs (pub->len) > GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH) )
224 return GNUNET_SYSERR;
230 * Convert the given public key from the network format to the
231 * S-expression that can be used by libgcrypt.
233 * @param publicKey public key to decode
234 * @return NULL on error
237 decode_public_key (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *publicKey)
245 if (ntohs (publicKey->len) > GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH)
250 size = ntohs (publicKey->len);
251 if (0 != (rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG, publicKey->key, size, &size)))
253 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
257 rc = gcry_sexp_build (&result, &erroff,
258 "(public-key(ecdsa(curve \"" CURVE "\")(q %m)))",
260 gcry_mpi_release (q);
263 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); /* erroff gives more info */
267 if (0 != (rc = gcry_pk_testkey (result)))
269 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
270 gcry_sexp_release (result);
279 * Encode the private key in a format suitable for
280 * storing it into a file.
282 * @param key key to encode
283 * @return encoding of the private key.
284 * The first 4 bytes give the size of the array, as usual.
286 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *
287 GNUNET_CRYPTO_ecc_encode_key (const struct GNUNET_CRYPTO_EccPrivateKey *key)
289 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *retval;
295 if (0 != gcry_pk_testkey (key->sexp))
301 size = gcry_sexp_sprint (key->sexp,
302 GCRYSEXP_FMT_DEFAULT,
303 &buf[2], sizeof (buf) - sizeof (uint16_t));
309 GNUNET_assert (size < 65536 - sizeof (uint16_t));
310 be = htons ((uint16_t) size + (sizeof (be)));
311 memcpy (buf, &be, sizeof (be));
313 retval = GNUNET_malloc (size);
314 memcpy (retval, buf, size);
320 * Decode the private key from the file-format back
321 * to the "normal", internal format.
323 * @param buf the buffer where the private key data is stored
324 * @param len the length of the data in 'buffer'
325 * @param validate GNUNET_YES to validate that the key is well-formed,
326 * GNUNET_NO if the key comes from a totally trusted source
327 * and validation is considered too expensive
328 * @return NULL on error
330 struct GNUNET_CRYPTO_EccPrivateKey *
331 GNUNET_CRYPTO_ecc_decode_key (const char *buf,
335 struct GNUNET_CRYPTO_EccPrivateKey *ret;
341 if (len < sizeof (uint16_t))
343 memcpy (&be, buf, sizeof (be));
344 if (len < ntohs (be))
347 if (0 != (rc = gcry_sexp_sscan (&sexp,
350 len - sizeof (uint16_t))))
352 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_scan", rc);
355 if ( (GNUNET_YES == validate) &&
356 (0 != (rc = gcry_pk_testkey (sexp))) )
358 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
361 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccPrivateKey));
368 * Create a new private key. Caller must free return value.
370 * @return fresh private key
372 struct GNUNET_CRYPTO_EccPrivateKey *
373 GNUNET_CRYPTO_ecc_key_create ()
375 struct GNUNET_CRYPTO_EccPrivateKey *ret;
377 gcry_sexp_t s_keyparam;
380 if (0 != (rc = gcry_sexp_build (&s_keyparam, NULL,
381 "(genkey(ecdsa(curve \"" CURVE "\")))")))
383 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
386 if (0 != (rc = gcry_pk_genkey (&s_key, s_keyparam)))
388 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc);
389 gcry_sexp_release (s_keyparam);
392 gcry_sexp_release (s_keyparam);
394 if (0 != (rc = gcry_pk_testkey (s_key)))
396 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
397 gcry_sexp_release (s_key);
401 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccPrivateKey));
408 * Wait for a short time (we're trying to lock a file or want
409 * to give another process a shot at finishing a disk write, etc.).
410 * Sleeps for 100ms (as that should be long enough for virtually all
411 * modern systems to context switch and allow another process to do
417 struct GNUNET_TIME_Relative timeout;
419 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
420 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
425 * Create a new private key by reading it from a file. If the
426 * files does not exist, create a new key and write it to the
427 * file. Caller must free return value. Note that this function
428 * can not guarantee that another process might not be trying
429 * the same operation on the same file at the same time.
430 * If the contents of the file
431 * are invalid the old file is deleted and a fresh key is
434 * @param filename name of file to use to store the key
435 * @return new private key, NULL on error (for example,
438 struct GNUNET_CRYPTO_EccPrivateKey *
439 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename)
441 struct GNUNET_CRYPTO_EccPrivateKey *ret;
442 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
444 struct GNUNET_DISK_FileHandle *fd;
448 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
449 struct GNUNET_PeerIdentity pid;
451 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
453 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
455 fd = GNUNET_DISK_file_open (filename,
456 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
457 | GNUNET_DISK_OPEN_FAILIFEXISTS,
458 GNUNET_DISK_PERM_USER_READ |
459 GNUNET_DISK_PERM_USER_WRITE);
464 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
466 /* must exist but not be accessible, fail for good! */
467 if (0 != ACCESS (filename, R_OK))
468 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
470 GNUNET_break (0); /* what is going on!? */
475 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
481 GNUNET_DISK_file_lock (fd, 0,
482 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded),
489 LOG (GNUNET_ERROR_TYPE_ERROR,
490 _("Could not acquire lock on file `%s': %s...\n"), filename,
494 LOG (GNUNET_ERROR_TYPE_INFO,
495 _("Creating a new private key. This may take a while.\n"));
496 ret = GNUNET_CRYPTO_ecc_key_create ();
497 GNUNET_assert (ret != NULL);
498 enc = GNUNET_CRYPTO_ecc_encode_key (ret);
499 GNUNET_assert (enc != NULL);
500 GNUNET_assert (ntohs (enc->size) ==
501 GNUNET_DISK_file_write (fd, enc, ntohs (enc->size)));
504 GNUNET_DISK_file_sync (fd);
506 GNUNET_DISK_file_unlock (fd, 0,
507 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
508 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
509 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
510 GNUNET_CRYPTO_ecc_key_get_public (ret, &pub);
511 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
514 /* key file exists already, read it! */
515 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
516 GNUNET_DISK_PERM_NONE);
519 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
526 GNUNET_DISK_file_lock (fd, 0,
527 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded),
533 LOG (GNUNET_ERROR_TYPE_ERROR,
534 _("Could not acquire lock on file `%s': %s...\n"), filename,
536 LOG (GNUNET_ERROR_TYPE_ERROR,
538 ("This may be ok if someone is currently generating a private key.\n"));
543 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
545 /* eh, what!? File we opened is now gone!? */
546 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
548 GNUNET_DISK_file_unlock (fd, 0,
549 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
550 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
551 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
555 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
557 if (fs < sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded))
559 /* maybe we got the read lock before the key generating
560 * process had a chance to get the write lock; give it up! */
562 GNUNET_DISK_file_unlock (fd, 0,
563 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
564 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
567 LOG (GNUNET_ERROR_TYPE_ERROR,
569 ("When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
570 filename, (unsigned int) fs,
571 (unsigned int) sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded));
572 LOG (GNUNET_ERROR_TYPE_ERROR,
574 ("This may be ok if someone is currently generating a key.\n"));
576 short_wait (); /* wait a bit longer! */
581 enc = GNUNET_malloc (fs);
582 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, enc, fs));
583 len = ntohs (enc->size);
586 (NULL == (ret = GNUNET_CRYPTO_ecc_decode_key ((char *) enc, len, GNUNET_YES))))
588 LOG (GNUNET_ERROR_TYPE_ERROR,
589 _("File `%s' does not contain a valid private key. Deleting it.\n"),
591 if (0 != UNLINK (filename))
593 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
598 GNUNET_DISK_file_unlock (fd, 0,
599 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
600 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
601 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
604 GNUNET_CRYPTO_ecc_key_get_public (ret, &pub);
605 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
612 * Create a new private key by reading our peer's key from
613 * the file specified in the configuration.
615 * @return new private key, NULL on error (for example,
618 struct GNUNET_CRYPTO_EccPrivateKey *
619 GNUNET_CRYPTO_ecc_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
621 struct GNUNET_CRYPTO_EccPrivateKey *pk;
625 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn))
627 pk = GNUNET_CRYPTO_ecc_key_create_from_file (fn);
634 * Setup a key file for a peer given the name of the
635 * configuration file (!). This function is used so that
636 * at a later point code can be certain that reading a
637 * key is fast (for example in time-dependent testcases).
639 * @param cfg_name name of the configuration file to use
642 GNUNET_CRYPTO_ecc_setup_key (const char *cfg_name)
644 struct GNUNET_CONFIGURATION_Handle *cfg;
645 struct GNUNET_CRYPTO_EccPrivateKey *pk;
647 cfg = GNUNET_CONFIGURATION_create ();
648 (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
649 pk = GNUNET_CRYPTO_ecc_key_create_from_configuration (cfg);
651 GNUNET_CRYPTO_ecc_key_free (pk);
652 GNUNET_CONFIGURATION_destroy (cfg);
657 * Retrieve the identity of the host's peer.
659 * @param cfg configuration to use
660 * @param dst pointer to where to write the peer identity
661 * @return GNUNET_OK on success, GNUNET_SYSERR if the identity
662 * could not be retrieved
665 GNUNET_CRYPTO_get_host_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
666 struct GNUNET_PeerIdentity *dst)
668 struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
669 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
671 if (NULL == (my_private_key = GNUNET_CRYPTO_ecc_key_create_from_configuration (cfg)))
673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
674 _("Could not load peer's private key\n"));
675 return GNUNET_SYSERR;
677 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
678 GNUNET_CRYPTO_ecc_key_free (my_private_key);
679 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &dst->hashPubKey);
685 * Convert the data specified in the given purpose argument to an
686 * S-expression suitable for signature operations.
688 * @param purpose data to convert
689 * @return converted s-expression
692 data_to_pkcs1 (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
694 struct GNUNET_CRYPTO_ShortHashCode hc;
698 GNUNET_CRYPTO_short_hash (purpose, ntohl (purpose->size), &hc);
699 #define FORMATSTRING "(4:data(5:flags3:raw)(5:value32:01234567890123456789012345678901))"
700 bufSize = strlen (FORMATSTRING) + 1;
704 memcpy (buff, FORMATSTRING, bufSize);
708 ("01234567890123456789012345678901))")
709 - 1], &hc, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
710 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
718 * Sign a given block.
720 * @param key private key to use for the signing
721 * @param purpose what to sign (size, purpose)
722 * @param sig where to write the signature
723 * @return GNUNET_SYSERR on error, GNUNET_OK on success
726 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *key,
727 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
728 struct GNUNET_CRYPTO_EccSignature *sig)
735 data = data_to_pkcs1 (purpose);
736 if (0 != (rc = gcry_pk_sign (&result, data, key->sexp)))
738 LOG (GNUNET_ERROR_TYPE_WARNING,
739 _("ECC signing failed at %s:%d: %s\n"), __FILE__,
740 __LINE__, gcry_strerror (rc));
741 gcry_sexp_release (data);
742 return GNUNET_SYSERR;
744 gcry_sexp_release (data);
745 ssize = gcry_sexp_sprint (result,
746 GCRYSEXP_FMT_DEFAULT,
748 GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH);
752 return GNUNET_SYSERR;
754 sig->size = htons ((uint16_t) (ssize + sizeof (uint16_t)));
755 /* padd with zeros */
756 memset (&sig->sexpr[ssize], 0, GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH - ssize);
757 gcry_sexp_release (result);
765 * @param purpose what is the purpose that the signature should have?
766 * @param validate block to validate (size, purpose, data)
767 * @param sig signature that is being validated
768 * @param publicKey public key of the signer
769 * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
772 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
773 const struct GNUNET_CRYPTO_EccSignaturePurpose
775 const struct GNUNET_CRYPTO_EccSignature *sig,
776 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
786 if (purpose != ntohl (validate->purpose))
787 return GNUNET_SYSERR; /* purpose mismatch */
788 size = ntohs (sig->size);
789 if ( (size < sizeof (uint16_t)) ||
790 (size > GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH - sizeof (uint16_t)) )
791 return GNUNET_SYSERR; /* size out of range */
792 data = data_to_pkcs1 (validate);
794 gcry_sexp_sscan (&sigdata, &erroff,
795 sig->sexpr, size - sizeof (uint16_t)));
796 if (! (psexp = decode_public_key (publicKey)))
798 gcry_sexp_release (data);
799 gcry_sexp_release (sigdata);
800 return GNUNET_SYSERR;
802 rc = gcry_pk_verify (sigdata, data, psexp);
803 gcry_sexp_release (psexp);
804 gcry_sexp_release (data);
805 gcry_sexp_release (sigdata);
808 LOG (GNUNET_ERROR_TYPE_WARNING,
809 _("ECC signature verification failed at %s:%d: %s\n"), __FILE__,
810 __LINE__, gcry_strerror (rc));
811 return GNUNET_SYSERR;
818 * Derive key material from a public and a private ECC key.
820 * @param key private key to use for the ECDH (x)
821 * @param pub public key to use for the ECDY (yG)
822 * @param key_material where to write the key material (xyG)
823 * @return GNUNET_SYSERR on error, GNUNET_OK on success
826 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *key,
827 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub,
828 struct GNUNET_HashCode *key_material)
833 unsigned char sdata_buf[2048]; /* big enough to print dh-shared-secret as S-expression */
834 gcry_mpi_point_t result;
842 /* first, extract the q = dP value from the public key */
843 if (! (psexp = decode_public_key (pub)))
844 return GNUNET_SYSERR;
845 if (0 != (rc = gcry_mpi_ec_new (&ctx, psexp, NULL)))
847 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_ec_new", rc); /* erroff gives more info */
848 return GNUNET_SYSERR;
850 gcry_sexp_release (psexp);
851 q = gcry_mpi_ec_get_point ("q", ctx, 0);
852 gcry_ctx_release (ctx);
854 /* second, extract the d value from our private key */
855 rc = key_from_sexp (&d, key->sexp, "private-key", "d");
857 rc = key_from_sexp (&d, key->sexp, "ecc", "d");
861 gcry_mpi_point_release (q);
862 return GNUNET_SYSERR;
865 /* create a new context for definitively the correct curve;
866 theoretically the 'public_key' might not use the right curve */
867 if (0 != (rc = gcry_mpi_ec_new (&ctx, NULL, "NIST P-256")))
869 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_ec_new", rc); /* erroff gives more info */
870 gcry_mpi_release (d);
871 gcry_mpi_point_release (q);
872 return GNUNET_SYSERR;
875 /* then call the 'multiply' function, to compute the product */
876 GNUNET_assert (NULL != ctx);
877 result = gcry_mpi_point_new (0);
878 gcry_mpi_ec_mul (result, d, q, ctx);
879 gcry_mpi_point_release (q);
880 gcry_mpi_release (d);
882 /* finally, convert point to string for hashing */
883 result_x = gcry_mpi_new (256);
884 result_y = gcry_mpi_new (256);
885 if (gcry_mpi_ec_get_affine (result_x, result_y, result, ctx))
887 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "get_affine failed", 0);
888 gcry_mpi_point_release (result);
889 gcry_ctx_release (ctx);
890 return GNUNET_SYSERR;
892 gcry_mpi_point_release (result);
893 gcry_ctx_release (ctx);
894 if (0 != (rc = gcry_sexp_build (&psexp, &erroff,
895 "(dh-shared-secret (x %m)(y %m))",
899 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); /* erroff gives more info */
900 gcry_mpi_release (result_x);
901 gcry_mpi_release (result_y);
902 return GNUNET_SYSERR;
904 gcry_mpi_release (result_x);
905 gcry_mpi_release (result_y);
906 slen = gcry_sexp_sprint (psexp, GCRYSEXP_FMT_DEFAULT, sdata_buf, sizeof (sdata_buf));
907 GNUNET_assert (0 != slen);
908 gcry_sexp_release (psexp);
909 /* finally, get a string of the resulting S-expression and hash it to generate the key material */
910 GNUNET_CRYPTO_hash (sdata_buf, slen, key_material);
915 /* end of crypto_ecc.c */