2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009, 2012 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 || 1
33 #define CURVE "NIST P-521"
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 (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,
219 (unsigned char*) pub,
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 */
266 // FIXME: is this key expected to pass pk_testkey?
269 if (0 != (rc = gcry_pk_testkey (result)))
271 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
272 gcry_sexp_release (result);
282 * Encode the private key in a format suitable for
283 * storing it into a file.
285 * @param key key to encode
286 * @return encoding of the private key.
287 * The first 4 bytes give the size of the array, as usual.
289 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *
290 GNUNET_CRYPTO_ecc_encode_key (const struct GNUNET_CRYPTO_EccPrivateKey *key)
292 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *retval;
298 if (0 != gcry_pk_testkey (key->sexp))
304 size = gcry_sexp_sprint (key->sexp,
305 GCRYSEXP_FMT_DEFAULT,
306 &buf[2], sizeof (buf) - sizeof (uint16_t));
312 GNUNET_assert (size < 65536 - sizeof (uint16_t));
313 be = htons ((uint16_t) size + (sizeof (be)));
314 memcpy (buf, &be, sizeof (be));
316 retval = GNUNET_malloc (size);
317 memcpy (retval, buf, size);
323 * Decode the private key from the file-format back
324 * to the "normal", internal format.
326 * @param buf the buffer where the private key data is stored
327 * @param len the length of the data in 'buffer'
328 * @return NULL on error
330 struct GNUNET_CRYPTO_EccPrivateKey *
331 GNUNET_CRYPTO_ecc_decode_key (const char *buf,
334 struct GNUNET_CRYPTO_EccPrivateKey *ret;
340 if (len < sizeof (uint16_t))
342 memcpy (&be, buf, sizeof (be));
343 if (len != ntohs (be))
345 if (0 != (rc = gcry_sexp_sscan (&sexp,
348 len - sizeof (uint16_t))))
350 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_scan", rc);
353 if (0 != (rc = gcry_pk_testkey (sexp)))
355 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
358 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccPrivateKey));
365 * Create a new private key. Caller must free return value.
367 * @return fresh private key
369 static struct GNUNET_CRYPTO_EccPrivateKey *
372 struct GNUNET_CRYPTO_EccPrivateKey *ret;
374 gcry_sexp_t s_keyparam;
377 if (0 != (rc = gcry_sexp_build (&s_keyparam, NULL,
378 "(genkey(ecdsa(curve \"" CURVE "\")))")))
380 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
383 if (0 != (rc = gcry_pk_genkey (&s_key, s_keyparam)))
385 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc);
386 gcry_sexp_release (s_keyparam);
389 gcry_sexp_release (s_keyparam);
391 if (0 != (rc = gcry_pk_testkey (s_key)))
393 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
394 gcry_sexp_release (s_key);
398 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccPrivateKey));
405 * Try to read the private key from the given file.
407 * @param filename file to read the key from
408 * @return NULL on error
410 static struct GNUNET_CRYPTO_EccPrivateKey *
411 try_read_key (const char *filename)
413 struct GNUNET_CRYPTO_EccPrivateKey *ret;
414 struct GNUNET_DISK_FileHandle *fd;
417 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
420 /* key file exists already, read it! */
421 if (NULL == (fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
422 GNUNET_DISK_PERM_NONE)))
424 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
427 if (GNUNET_OK != (GNUNET_DISK_file_handle_size (fd, &fs)))
429 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "stat", filename);
430 (void) GNUNET_DISK_file_close (fd);
435 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
440 LOG (GNUNET_ERROR_TYPE_ERROR,
441 _("File `%s' does not contain a valid private key (too long, %llu bytes). Deleting it.\n"),
443 (unsigned long long) fs);
444 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
445 if (0 != UNLINK (filename))
446 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
452 GNUNET_break (fs == GNUNET_DISK_file_read (fd, enc, fs));
453 if (NULL == (ret = GNUNET_CRYPTO_ecc_decode_key ((char *) enc, fs)))
455 LOG (GNUNET_ERROR_TYPE_ERROR,
456 _("File `%s' does not contain a valid private key (failed decode, %llu bytes). Deleting it.\n"),
458 (unsigned long long) fs);
459 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
460 if (0 != UNLINK (filename))
461 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
465 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
471 * Wait for a short time (we're trying to lock a file or want
472 * to give another process a shot at finishing a disk write, etc.).
473 * Sleeps for 100ms (as that should be long enough for virtually all
474 * modern systems to context switch and allow another process to do
480 struct GNUNET_TIME_Relative timeout;
482 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
483 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
488 * Create a new private key by reading it from a file. If the
489 * files does not exist, create a new key and write it to the
490 * file. Caller must free return value. Note that this function
491 * can not guarantee that another process might not be trying
492 * the same operation on the same file at the same time.
493 * If the contents of the file
494 * are invalid the old file is deleted and a fresh key is
497 * @return new private key, NULL on error (for example,
500 struct GNUNET_CRYPTO_EccPrivateKey *
501 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename)
503 struct GNUNET_CRYPTO_EccPrivateKey *ret;
504 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
506 struct GNUNET_DISK_FileHandle *fd;
510 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
511 struct GNUNET_PeerIdentity pid;
513 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
515 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
517 fd = GNUNET_DISK_file_open (filename,
518 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
519 | GNUNET_DISK_OPEN_FAILIFEXISTS,
520 GNUNET_DISK_PERM_USER_READ |
521 GNUNET_DISK_PERM_USER_WRITE);
526 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
528 /* must exist but not be accessible, fail for good! */
529 if (0 != ACCESS (filename, R_OK))
530 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
532 GNUNET_break (0); /* what is going on!? */
537 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
543 GNUNET_DISK_file_lock (fd, 0,
544 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded),
551 LOG (GNUNET_ERROR_TYPE_ERROR,
552 _("Could not acquire lock on file `%s': %s...\n"), filename,
556 LOG (GNUNET_ERROR_TYPE_INFO,
557 _("Creating a new private key. This may take a while.\n"));
558 ret = ecc_key_create ();
559 GNUNET_assert (ret != NULL);
560 enc = GNUNET_CRYPTO_ecc_encode_key (ret);
561 GNUNET_assert (enc != NULL);
562 GNUNET_assert (ntohs (enc->size) ==
563 GNUNET_DISK_file_write (fd, enc, ntohs (enc->size)));
566 GNUNET_DISK_file_sync (fd);
568 GNUNET_DISK_file_unlock (fd, 0,
569 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
570 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
571 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
572 GNUNET_CRYPTO_ecc_key_get_public (ret, &pub);
573 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
576 /* key file exists already, read it! */
577 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
578 GNUNET_DISK_PERM_NONE);
581 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
588 GNUNET_DISK_file_lock (fd, 0,
589 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded),
595 LOG (GNUNET_ERROR_TYPE_ERROR,
596 _("Could not acquire lock on file `%s': %s...\n"), filename,
598 LOG (GNUNET_ERROR_TYPE_ERROR,
600 ("This may be ok if someone is currently generating a private key.\n"));
605 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
607 /* eh, what!? File we opened is now gone!? */
608 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
610 GNUNET_DISK_file_unlock (fd, 0,
611 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
612 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
613 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
617 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
619 if (fs < sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded))
621 /* maybe we got the read lock before the key generating
622 * process had a chance to get the write lock; give it up! */
624 GNUNET_DISK_file_unlock (fd, 0,
625 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
626 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
629 LOG (GNUNET_ERROR_TYPE_ERROR,
631 ("When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
632 filename, (unsigned int) fs,
633 (unsigned int) sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded));
634 LOG (GNUNET_ERROR_TYPE_ERROR,
636 ("This may be ok if someone is currently generating a key.\n"));
638 short_wait (); /* wait a bit longer! */
643 enc = GNUNET_malloc (fs);
644 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, enc, fs));
645 len = ntohs (enc->size);
648 (NULL == (ret = GNUNET_CRYPTO_ecc_decode_key ((char *) enc, len))))
650 LOG (GNUNET_ERROR_TYPE_ERROR,
651 _("File `%s' does not contain a valid private key. Deleting it.\n"),
653 if (0 != UNLINK (filename))
655 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
660 GNUNET_DISK_file_unlock (fd, 0,
661 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
662 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
663 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
666 GNUNET_CRYPTO_ecc_key_get_public (ret, &pub);
667 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
674 * Handle to cancel private key generation and state for the
675 * key generation operation.
677 struct GNUNET_CRYPTO_EccKeyGenerationContext
681 * Continuation to call upon completion.
683 GNUNET_CRYPTO_EccKeyCallback cont;
686 * Closure for 'cont'.
696 * Handle to the helper process which does the key generation.
698 struct GNUNET_OS_Process *gnunet_ecc;
701 * Handle to 'stdout' of gnunet-ecc. We 'read' on stdout to detect
702 * process termination (instead of messing with SIGCHLD).
704 struct GNUNET_DISK_PipeHandle *gnunet_ecc_out;
707 * Location where we store the private key if it already existed.
708 * (if this is used, 'filename', 'gnunet_ecc' and 'gnunet_ecc_out' will
711 struct GNUNET_CRYPTO_EccPrivateKey *pk;
714 * Task reading from 'gnunet_ecc_out' to wait for process termination.
716 GNUNET_SCHEDULER_TaskIdentifier read_task;
722 * Abort ECC key generation.
724 * @param gc key generation context to abort
727 GNUNET_CRYPTO_ecc_key_create_stop (struct GNUNET_CRYPTO_EccKeyGenerationContext *gc)
729 if (GNUNET_SCHEDULER_NO_TASK != gc->read_task)
731 GNUNET_SCHEDULER_cancel (gc->read_task);
732 gc->read_task = GNUNET_SCHEDULER_NO_TASK;
734 if (NULL != gc->gnunet_ecc)
736 (void) GNUNET_OS_process_kill (gc->gnunet_ecc, SIGKILL);
737 GNUNET_break (GNUNET_OK ==
738 GNUNET_OS_process_wait (gc->gnunet_ecc));
739 GNUNET_OS_process_destroy (gc->gnunet_ecc);
740 GNUNET_DISK_pipe_close (gc->gnunet_ecc_out);
743 if (NULL != gc->filename)
745 if (0 != UNLINK (gc->filename))
746 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", gc->filename);
747 GNUNET_free (gc->filename);
750 GNUNET_CRYPTO_ecc_key_free (gc->pk);
756 * Task called upon shutdown or process termination of 'gnunet-ecc' during
757 * ECC key generation. Check where we are and perform the appropriate
760 * @param cls the 'struct GNUNET_CRYPTO_EccKeyGenerationContext'
761 * @param tc scheduler context
764 check_key_generation_completion (void *cls,
765 const struct GNUNET_SCHEDULER_TaskContext *tc)
767 struct GNUNET_CRYPTO_EccKeyGenerationContext *gc = cls;
768 struct GNUNET_CRYPTO_EccPrivateKey *pk;
770 gc->read_task = GNUNET_SCHEDULER_NO_TASK;
771 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
773 gc->cont (gc->cont_cls, NULL, _("interrupted by shutdown"));
774 GNUNET_CRYPTO_ecc_key_create_stop (gc);
777 GNUNET_assert (GNUNET_OK ==
778 GNUNET_OS_process_wait (gc->gnunet_ecc));
779 GNUNET_OS_process_destroy (gc->gnunet_ecc);
780 gc->gnunet_ecc = NULL;
781 if (NULL == (pk = try_read_key (gc->filename)))
784 gc->cont (gc->cont_cls, NULL, _("gnunet-ecc failed"));
785 GNUNET_CRYPTO_ecc_key_create_stop (gc);
788 gc->cont (gc->cont_cls, pk, NULL);
789 GNUNET_DISK_pipe_close (gc->gnunet_ecc_out);
790 GNUNET_free (gc->filename);
796 * Return the private ECC key which already existed on disk
797 * (asynchronously) to the caller.
799 * @param cls the 'struct GNUNET_CRYPTO_EccKeyGenerationContext'
800 * @param tc scheduler context (unused)
803 async_return_key (void *cls,
804 const struct GNUNET_SCHEDULER_TaskContext *tc)
806 struct GNUNET_CRYPTO_EccKeyGenerationContext *gc = cls;
808 gc->cont (gc->cont_cls,
816 * Create a new private key by reading it from a file. If the files
817 * does not exist, create a new key and write it to the file. If the
818 * contents of the file are invalid the old file is deleted and a
819 * fresh key is created.
821 * @param filename name of file to use for storage
822 * @param cont function to call when done (or on errors)
823 * @param cont_cls closure for 'cont'
824 * @return handle to abort operation, NULL on fatal errors (cont will not be called if NULL is returned)
826 struct GNUNET_CRYPTO_EccKeyGenerationContext *
827 GNUNET_CRYPTO_ecc_key_create_start (const char *filename,
828 GNUNET_CRYPTO_EccKeyCallback cont,
831 struct GNUNET_CRYPTO_EccKeyGenerationContext *gc;
832 struct GNUNET_CRYPTO_EccPrivateKey *pk;
833 const char *weak_random;
835 if (NULL != (pk = try_read_key (filename)))
837 /* quick happy ending: key already exists! */
838 gc = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccKeyGenerationContext));
841 gc->cont_cls = cont_cls;
842 gc->read_task = GNUNET_SCHEDULER_add_now (&async_return_key,
846 gc = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccKeyGenerationContext));
847 gc->filename = GNUNET_strdup (filename);
849 gc->cont_cls = cont_cls;
850 gc->gnunet_ecc_out = GNUNET_DISK_pipe (GNUNET_NO,
854 if (NULL == gc->gnunet_ecc_out)
856 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "pipe");
857 GNUNET_free (gc->filename);
863 GNUNET_CRYPTO_random_is_weak ())
865 gc->gnunet_ecc = GNUNET_OS_start_process (GNUNET_NO,
866 GNUNET_OS_INHERIT_STD_ERR,
874 if (NULL == gc->gnunet_ecc)
876 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fork");
877 GNUNET_DISK_pipe_close (gc->gnunet_ecc_out);
878 GNUNET_free (gc->filename);
882 GNUNET_assert (GNUNET_OK ==
883 GNUNET_DISK_pipe_close_end (gc->gnunet_ecc_out,
884 GNUNET_DISK_PIPE_END_WRITE));
885 gc->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
886 GNUNET_DISK_pipe_handle (gc->gnunet_ecc_out,
887 GNUNET_DISK_PIPE_END_READ),
888 &check_key_generation_completion,
895 * Setup a key file for a peer given the name of the
896 * configuration file (!). This function is used so that
897 * at a later point code can be certain that reading a
898 * key is fast (for example in time-dependent testcases).
900 * @param cfg_name name of the configuration file to use
903 GNUNET_CRYPTO_ecc_setup_key (const char *cfg_name)
905 struct GNUNET_CONFIGURATION_Handle *cfg;
906 struct GNUNET_CRYPTO_EccPrivateKey *pk;
909 cfg = GNUNET_CONFIGURATION_create ();
910 (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
912 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn))
914 pk = GNUNET_CRYPTO_ecc_key_create_from_file (fn);
916 GNUNET_CRYPTO_ecc_key_free (pk);
919 GNUNET_CONFIGURATION_destroy (cfg);
924 * Convert the data specified in the given purpose argument to an
925 * S-expression suitable for signature operations.
927 * @param purpose data to convert
928 * @return converted s-expression
931 data_to_pkcs1 (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
933 struct GNUNET_CRYPTO_ShortHashCode hc;
937 GNUNET_CRYPTO_short_hash (purpose, ntohl (purpose->size), &hc);
938 #define FORMATSTRING "(4:data(5:flags3:raw)(5:value32:01234567890123456789012345678901))"
939 #define FORMATSTRING2 "(4:data(4:hash6:sha25632:01234567890123456789012345678901))"
940 bufSize = strlen (FORMATSTRING) + 1;
944 memcpy (buff, FORMATSTRING, bufSize);
948 ("01234567890123456789012345678901))")
949 - 1], &hc, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
950 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
958 * Sign a given block.
960 * @param key private key to use for the signing
961 * @param purpose what to sign (size, purpose)
962 * @param sig where to write the signature
963 * @return GNUNET_SYSERR on error, GNUNET_OK on success
966 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *key,
967 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
968 struct GNUNET_CRYPTO_EccSignature *sig)
975 data = data_to_pkcs1 (purpose);
976 if (0 != (rc = gcry_pk_sign (&result, data, key->sexp)))
978 LOG (GNUNET_ERROR_TYPE_WARNING,
979 _("ECC signing failed at %s:%d: %s\n"), __FILE__,
980 __LINE__, gcry_strerror (rc));
982 gcry_sexp_release (data);
983 ssize = gcry_sexp_sprint (result,
984 GCRYSEXP_FMT_DEFAULT,
986 GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH);
990 return GNUNET_SYSERR;
992 sig->size = htons ((uint16_t) (ssize + sizeof (uint16_t)));
993 /* padd with zeros */
994 memset (&sig->sexpr[ssize], 0, GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH - ssize);
995 gcry_sexp_release (result);
1003 * @param purpose what is the purpose that the signature should have?
1004 * @param validate block to validate (size, purpose, data)
1005 * @param sig signature that is being validated
1006 * @param publicKey public key of the signer
1007 * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
1010 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
1011 const struct GNUNET_CRYPTO_EccSignaturePurpose
1013 const struct GNUNET_CRYPTO_EccSignature *sig,
1014 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
1018 gcry_sexp_t sigdata;
1024 if (purpose != ntohl (validate->purpose))
1025 return GNUNET_SYSERR; /* purpose mismatch */
1026 size = ntohs (sig->size);
1027 if ( (size < sizeof (uint16_t)) ||
1028 (size > GNUNET_CRYPTO_ECC_SIGNATURE_DATA_ENCODING_LENGTH - sizeof (uint16_t)) )
1029 return GNUNET_SYSERR; /* size out of range */
1030 data = data_to_pkcs1 (validate);
1032 gcry_sexp_sscan (&sigdata, &erroff,
1033 sig->sexpr, size - sizeof (uint16_t)));
1034 if (! (psexp = decode_public_key (publicKey)))
1036 gcry_sexp_release (data);
1037 gcry_sexp_release (sigdata);
1038 return GNUNET_SYSERR;
1040 rc = gcry_pk_verify (sigdata, data, psexp);
1041 gcry_sexp_release (psexp);
1042 gcry_sexp_release (data);
1043 gcry_sexp_release (sigdata);
1046 LOG (GNUNET_ERROR_TYPE_WARNING,
1047 _("ECC signature verification failed at %s:%d: %s\n"), __FILE__,
1048 __LINE__, gcry_strerror (rc));
1049 return GNUNET_SYSERR;
1055 /* end of crypto_ecc.c */