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
26 * This is just a first, completely untested, draft hack for future ECC support.
28 * - declare public API in gnunet_crypto_lib (move some structs, etc.)
29 * - implement gnunet-ecc binary
30 * - convert existing RSA testcases
31 * - adjust encoding length and other parameters
36 #include "gnunet_common.h"
37 #include "gnunet_util_lib.h"
39 #define EXTRA_CHECKS ALLOW_EXTRA_CHECKS
41 #define CURVE "NIST P-521"
43 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
45 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
47 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
51 * FIXME: what is an acceptable value here?
53 #define GNUNET_CRYPTO_ECC_DATA_ENCODING_LENGTH 64
56 * Length of the q-point (Q = dP) in the public key.
57 * FIXME: double-check that this is right.
59 #define GNUNET_CRYPTO_ECC_PUBLIC_KEY_LENGTH 64
63 * @brief an ECC signature
65 struct GNUNET_CRYPTO_EccSignature
67 unsigned char sig[GNUNET_CRYPTO_ECC_DATA_ENCODING_LENGTH];
71 GNUNET_NETWORK_STRUCT_BEGIN
74 * @brief header of what an ECC signature signs
75 * this must be followed by "size - 8" bytes of
76 * the actual signed data
78 struct GNUNET_CRYPTO_EccSignaturePurpose
81 * How many bytes does this signature sign?
82 * (including this purpose header); in network
85 uint32_t size GNUNET_PACKED;
88 * What does this signature vouch for? This
89 * must contain a GNUNET_SIGNATURE_PURPOSE_XXX
90 * constant (from gnunet_signatures.h). In
93 uint32_t purpose GNUNET_PACKED;
99 * Public ECC key (always for NIST P-521) encoded in a format suitable
100 * for network transmission.
102 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
105 * Size of the encoding, in network byte order.
110 * Actual length of the q-point binary encoding.
115 * 0-padded q-point in binary encoding (GCRYPT_MPI_FMT_USG).
117 unsigned char key[GNUNET_CRYPTO_ECC_PUBLIC_KEY_LENGTH];
121 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded
124 * Overall size of the private key.
129 * Size of the q and d components of the private key.
130 * Note that the other parameters are from NIST P-521.
137 * ECC Encrypted data.
139 struct GNUNET_CRYPTO_EccEncryptedData
141 unsigned char encoding[GNUNET_CRYPTO_ECC_DATA_ENCODING_LENGTH];
144 GNUNET_NETWORK_STRUCT_END
149 * The private information of an ECC private key.
151 struct GNUNET_CRYPTO_EccPrivateKey
155 * Libgcrypt S-expression for the ECC key.
162 * Function called upon completion of 'GNUNET_CRYPTO_ecc_key_create_async'.
165 * @param pk NULL on error, otherwise the private key (which must be free'd by the callee)
166 * @param emsg NULL on success, otherwise an error message
168 typedef void (*GNUNET_CRYPTO_EccKeyCallback)(void *cls,
169 struct GNUNET_CRYPTO_EccPrivateKey *pk,
175 * Log an error message at log-level 'level' that indicates
176 * a failure of the command 'cmd' with the message given
177 * by gcry_strerror(rc).
179 #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);
183 * If target != size, move target bytes to the
184 * end of the size-sized buffer and zero out the
185 * first target-size bytes.
187 * @param buf original buffer
188 * @param size number of bytes in the buffer
189 * @param target target size of the buffer
192 adjust (unsigned char *buf, size_t size, size_t target)
196 memmove (&buf[target - size], buf, size);
197 memset (buf, 0, target - size);
203 * Free memory occupied by hostkey
204 * @param privatekey pointer to the memory to free
207 GNUNET_CRYPTO_ecc_key_free (struct GNUNET_CRYPTO_EccPrivateKey *privatekey)
209 gcry_sexp_release (privatekey->sexp);
210 GNUNET_free (privatekey);
215 * Extract values from an S-expression.
217 * @param array where to store the result(s)
218 * @param sexp S-expression to parse
219 * @param topname top-level name in the S-expression that is of interest
220 * @param elems names of the elements to extract
221 * @return 0 on success
224 key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname,
233 list = gcry_sexp_find_token (sexp, topname, 0);
236 l2 = gcry_sexp_cadr (list);
237 gcry_sexp_release (list);
243 for (s = elems; *s; s++, idx++)
245 l2 = gcry_sexp_find_token (list, s, 1);
248 for (i = 0; i < idx; i++)
250 gcry_free (array[i]);
253 gcry_sexp_release (list);
254 return 3; /* required parameter not found */
256 array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
257 gcry_sexp_release (l2);
260 for (i = 0; i < idx; i++)
262 gcry_free (array[i]);
265 gcry_sexp_release (list);
266 return 4; /* required parameter is invalid */
269 gcry_sexp_release (list);
275 * Extract the public key for the given private key.
277 * @param priv the private key
278 * @param pub where to write the public key
281 GNUNET_CRYPTO_ecc_key_get_public (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
282 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub)
288 rc = key_from_sexp (&skey, priv->sexp, "public-key", "q");
290 rc = key_from_sexp (&skey, priv->sexp, "private-key", "q");
292 rc = key_from_sexp (&skey, priv->sexp, "ecc", "q");
293 GNUNET_assert (0 == rc);
294 pub->size = htons (sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded));
295 size = GNUNET_CRYPTO_ECC_DATA_ENCODING_LENGTH;
297 gcry_mpi_print (GCRYMPI_FMT_USG, pub->key, size, &size,
299 pub->len = htons (size);
300 adjust (&pub->key[0], size, GNUNET_CRYPTO_ECC_DATA_ENCODING_LENGTH);
301 gcry_mpi_release (skey);
306 * Convert a public key to a string.
308 * @param pub key to convert
309 * @return string representing 'pub'
312 GNUNET_CRYPTO_ecc_public_key_to_string (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub)
315 size_t keylen = (sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded)) * 8;
319 keylen += 5 - keylen % 5;
321 pubkeybuf = GNUNET_malloc (keylen + 1);
322 end = GNUNET_STRINGS_data_to_string ((unsigned char *) pub,
323 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
328 GNUNET_free (pubkeybuf);
337 * Convert a string representing a public key to a public key.
339 * @param enc encoded public key
340 * @param enclen number of bytes in enc (without 0-terminator)
341 * @param pub where to store the public key
342 * @return GNUNET_OK on success
345 GNUNET_CRYPTO_ecc_public_key_from_string (const char *enc,
347 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *pub)
349 size_t keylen = (sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded)) * 8;
352 keylen += 5 - keylen % 5;
354 if (enclen != keylen)
355 return GNUNET_SYSERR;
357 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, enclen,
358 (unsigned char*) pub,
359 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded)))
360 return GNUNET_SYSERR;
361 if ( (ntohs (pub->size) != sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded)) ||
362 (ntohs (pub->len) > GNUNET_CRYPTO_ECC_DATA_ENCODING_LENGTH) )
363 return GNUNET_SYSERR;
369 * Convert the given public key from the network format to the
370 * S-expression that can be used by libgcrypt.
372 * @param publicKey public key to decode
373 * @return NULL on error
376 decode_public_key (const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *publicKey)
384 if (ntohs (publicKey->len) > GNUNET_CRYPTO_ECC_DATA_ENCODING_LENGTH)
389 size = ntohs (publicKey->size);
390 if (0 != (rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG, publicKey->key, size, &size)))
392 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
395 rc = gcry_sexp_build (&result, &erroff, "(public-key(ecc((curve \"" CURVE "\")(q %m)))", q);
396 gcry_mpi_release (q);
399 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); /* erroff gives more info */
407 * Encode the private key in a format suitable for
408 * storing it into a file.
410 * @returns encoding of the private key.
411 * The first 4 bytes give the size of the array, as usual.
413 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *
414 GNUNET_CRYPTO_ecc_encode_key (const struct GNUNET_CRYPTO_EccPrivateKey *hostkey)
416 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *retval;
426 if (gcry_pk_testkey (hostkey->sexp))
433 memset (pkv, 0, sizeof (gcry_mpi_t) * 2);
434 rc = key_from_sexp (pkv, hostkey->sexp, "private-key", "qd");
436 rc = key_from_sexp (pkv, hostkey->sexp, "ecc", "qd");
437 GNUNET_assert (0 == rc);
438 size = sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded);
444 gcry_mpi_aprint (GCRYMPI_FMT_USG,
445 (unsigned char **) &pbu[i], &sizes[i],
455 GNUNET_assert (size < 65536);
456 retval = GNUNET_malloc (size);
457 retval->size = htons (size);
461 retval->sizes[i] = htons (sizes[0]);
462 memcpy (&((char *) (&retval[1]))[off], pbu[i], sizes[i]);
465 gcry_mpi_release (pkv[i]);
474 * Decode the private key from the file-format back
475 * to the "normal", internal format.
477 * @param buf the buffer where the private key data is stored
478 * @param len the length of the data in 'buffer'
479 * @return NULL on error
481 struct GNUNET_CRYPTO_EccPrivateKey *
482 GNUNET_CRYPTO_ecc_decode_key (const char *buf, uint16_t len)
484 struct GNUNET_CRYPTO_EccPrivateKey *ret;
485 const struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *encoding =
486 (const struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *) buf;
496 enc_len = ntohs (encoding->size);
500 size = ntohs (encoding->sizes[0]);
501 rc = gcry_mpi_scan (&q, GCRYMPI_FMT_USG,
502 &((const unsigned char *) (&encoding[1]))[pos], size,
504 pos += ntohs (encoding->sizes[0]);
507 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
510 size = ntohs (encoding->sizes[1]);
511 rc = gcry_mpi_scan (&d, GCRYMPI_FMT_USG,
512 &((const unsigned char *) (&encoding[1]))[pos], size,
514 pos += ntohs (encoding->sizes[1]);
517 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
518 gcry_mpi_release (d);
521 rc = gcry_sexp_build (&res, &erroff,
522 "(private-key(ecc(q %m)(d %m)(curve \"" CURVE "\")))",
524 gcry_mpi_release (q);
525 gcry_mpi_release (d);
527 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
529 if (0 != (rc = gcry_pk_testkey (res)))
531 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
535 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccPrivateKey));
542 * Create a new private key. Caller must free return value.
544 * @return fresh private key
546 static struct GNUNET_CRYPTO_EccPrivateKey *
549 struct GNUNET_CRYPTO_EccPrivateKey *ret;
551 gcry_sexp_t s_keyparam;
554 gcry_sexp_build (&s_keyparam, NULL,
555 "(genkey(ecc(curve \"" CURVE "\")))"));
556 GNUNET_assert (0 == gcry_pk_genkey (&s_key, s_keyparam));
557 gcry_sexp_release (s_keyparam);
559 GNUNET_assert (0 == gcry_pk_testkey (s_key));
561 ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccPrivateKey));
568 * Try to read the private key from the given file.
570 * @param filename file to read the key from
571 * @return NULL on error
573 static struct GNUNET_CRYPTO_EccPrivateKey *
574 try_read_key (const char *filename)
576 struct GNUNET_CRYPTO_EccPrivateKey *ret;
577 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
578 struct GNUNET_DISK_FileHandle *fd;
582 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
585 /* hostkey file exists already, read it! */
586 if (NULL == (fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
587 GNUNET_DISK_PERM_NONE)))
589 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
592 if (GNUNET_OK != (GNUNET_DISK_file_handle_size (fd, &fs)))
594 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "stat", filename);
595 (void) GNUNET_DISK_file_close (fd);
600 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
605 LOG (GNUNET_ERROR_TYPE_ERROR,
606 _("File `%s' does not contain a valid private key (too long, %llu bytes). Deleting it.\n"),
608 (unsigned long long) fs);
609 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
610 if (0 != UNLINK (filename))
611 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
615 enc = GNUNET_malloc (fs);
616 GNUNET_break (fs == GNUNET_DISK_file_read (fd, enc, fs));
617 len = ntohs (enc->size);
620 (NULL == (ret = GNUNET_CRYPTO_ecc_decode_key ((char *) enc, len))))
622 LOG (GNUNET_ERROR_TYPE_ERROR,
623 _("File `%s' does not contain a valid private key (failed decode, %llu bytes). Deleting it.\n"),
625 (unsigned long long) fs);
626 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
627 if (0 != UNLINK (filename))
628 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
634 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
640 * Wait for a short time (we're trying to lock a file or want
641 * to give another process a shot at finishing a disk write, etc.).
642 * Sleeps for 100ms (as that should be long enough for virtually all
643 * modern systems to context switch and allow another process to do
649 struct GNUNET_TIME_Relative timeout;
651 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
652 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
657 * Create a new private key by reading it from a file. If the
658 * files does not exist, create a new key and write it to the
659 * file. Caller must free return value. Note that this function
660 * can not guarantee that another process might not be trying
661 * the same operation on the same file at the same time.
662 * If the contents of the file
663 * are invalid the old file is deleted and a fresh key is
666 * @return new private key, NULL on error (for example,
669 struct GNUNET_CRYPTO_EccPrivateKey *
670 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename)
672 struct GNUNET_CRYPTO_EccPrivateKey *ret;
673 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc;
675 struct GNUNET_DISK_FileHandle *fd;
679 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pub;
680 struct GNUNET_PeerIdentity pid;
682 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
684 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
686 fd = GNUNET_DISK_file_open (filename,
687 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
688 | GNUNET_DISK_OPEN_FAILIFEXISTS,
689 GNUNET_DISK_PERM_USER_READ |
690 GNUNET_DISK_PERM_USER_WRITE);
695 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
697 /* must exist but not be accessible, fail for good! */
698 if (0 != ACCESS (filename, R_OK))
699 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
701 GNUNET_break (0); /* what is going on!? */
706 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
712 GNUNET_DISK_file_lock (fd, 0,
713 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded),
720 LOG (GNUNET_ERROR_TYPE_ERROR,
721 _("Could not acquire lock on file `%s': %s...\n"), filename,
725 LOG (GNUNET_ERROR_TYPE_INFO,
726 _("Creating a new private key. This may take a while.\n"));
727 ret = ecc_key_create ();
728 GNUNET_assert (ret != NULL);
729 enc = GNUNET_CRYPTO_ecc_encode_key (ret);
730 GNUNET_assert (enc != NULL);
731 GNUNET_assert (ntohs (enc->size) ==
732 GNUNET_DISK_file_write (fd, enc, ntohs (enc->size)));
735 GNUNET_DISK_file_sync (fd);
737 GNUNET_DISK_file_unlock (fd, 0,
738 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
739 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
740 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
741 GNUNET_CRYPTO_ecc_key_get_public (ret, &pub);
742 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
743 LOG (GNUNET_ERROR_TYPE_INFO,
744 _("I am host `%s'. Stored new private key in `%s'.\n"),
745 GNUNET_i2s (&pid), filename);
748 /* hostkey file exists already, read it! */
749 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
750 GNUNET_DISK_PERM_NONE);
753 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
760 GNUNET_DISK_file_lock (fd, 0,
761 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded),
767 LOG (GNUNET_ERROR_TYPE_ERROR,
768 _("Could not acquire lock on file `%s': %s...\n"), filename,
770 LOG (GNUNET_ERROR_TYPE_ERROR,
772 ("This may be ok if someone is currently generating a hostkey.\n"));
777 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
779 /* eh, what!? File we opened is now gone!? */
780 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
782 GNUNET_DISK_file_unlock (fd, 0,
783 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
784 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
785 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
789 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
791 if (fs < sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded))
793 /* maybe we got the read lock before the hostkey generating
794 * process had a chance to get the write lock; give it up! */
796 GNUNET_DISK_file_unlock (fd, 0,
797 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
798 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
801 LOG (GNUNET_ERROR_TYPE_ERROR,
803 ("When trying to read hostkey file `%s' I found %u bytes but I need at least %u.\n"),
804 filename, (unsigned int) fs,
805 (unsigned int) sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded));
806 LOG (GNUNET_ERROR_TYPE_ERROR,
808 ("This may be ok if someone is currently generating a hostkey.\n"));
810 short_wait (); /* wait a bit longer! */
815 enc = GNUNET_malloc (fs);
816 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, enc, fs));
817 len = ntohs (enc->size);
820 (NULL == (ret = GNUNET_CRYPTO_ecc_decode_key ((char *) enc, len))))
822 LOG (GNUNET_ERROR_TYPE_ERROR,
823 _("File `%s' does not contain a valid private key. Deleting it.\n"),
825 if (0 != UNLINK (filename))
827 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
832 GNUNET_DISK_file_unlock (fd, 0,
833 sizeof (struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded)))
834 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
835 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
838 GNUNET_CRYPTO_ecc_key_get_public (ret, &pub);
839 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
840 LOG (GNUNET_ERROR_TYPE_INFO,
841 _("I am host `%s'. Read private key from `%s'.\n"), GNUNET_i2s (&pid),
849 * Handle to cancel private key generation and state for the
850 * key generation operation.
852 struct GNUNET_CRYPTO_EccKeyGenerationContext
856 * Continuation to call upon completion.
858 GNUNET_CRYPTO_EccKeyCallback cont;
861 * Closure for 'cont'.
871 * Handle to the helper process which does the key generation.
873 struct GNUNET_OS_Process *gnunet_ecc;
876 * Handle to 'stdout' of gnunet-ecc. We 'read' on stdout to detect
877 * process termination (instead of messing with SIGCHLD).
879 struct GNUNET_DISK_PipeHandle *gnunet_ecc_out;
882 * Location where we store the private key if it already existed.
883 * (if this is used, 'filename', 'gnunet_ecc' and 'gnunet_ecc_out' will
886 struct GNUNET_CRYPTO_EccPrivateKey *pk;
889 * Task reading from 'gnunet_ecc_out' to wait for process termination.
891 GNUNET_SCHEDULER_TaskIdentifier read_task;
897 * Abort ECC key generation.
899 * @param gc key generation context to abort
902 GNUNET_CRYPTO_ecc_key_create_stop (struct GNUNET_CRYPTO_EccKeyGenerationContext *gc)
904 if (GNUNET_SCHEDULER_NO_TASK != gc->read_task)
906 GNUNET_SCHEDULER_cancel (gc->read_task);
907 gc->read_task = GNUNET_SCHEDULER_NO_TASK;
909 if (NULL != gc->gnunet_ecc)
911 (void) GNUNET_OS_process_kill (gc->gnunet_ecc, SIGKILL);
912 GNUNET_break (GNUNET_OK ==
913 GNUNET_OS_process_wait (gc->gnunet_ecc));
914 GNUNET_OS_process_destroy (gc->gnunet_ecc);
915 GNUNET_DISK_pipe_close (gc->gnunet_ecc_out);
918 if (NULL != gc->filename)
920 if (0 != UNLINK (gc->filename))
921 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", gc->filename);
922 GNUNET_free (gc->filename);
925 GNUNET_CRYPTO_ecc_key_free (gc->pk);
931 * Task called upon shutdown or process termination of 'gnunet-ecc' during
932 * ECC key generation. Check where we are and perform the appropriate
935 * @param cls the 'struct GNUNET_CRYPTO_EccKeyGenerationContext'
936 * @param tc scheduler context
939 check_key_generation_completion (void *cls,
940 const struct GNUNET_SCHEDULER_TaskContext *tc)
942 struct GNUNET_CRYPTO_EccKeyGenerationContext *gc = cls;
943 struct GNUNET_CRYPTO_EccPrivateKey *pk;
945 gc->read_task = GNUNET_SCHEDULER_NO_TASK;
946 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
948 gc->cont (gc->cont_cls, NULL, _("interrupted by shutdown"));
949 GNUNET_CRYPTO_ecc_key_create_stop (gc);
952 GNUNET_assert (GNUNET_OK ==
953 GNUNET_OS_process_wait (gc->gnunet_ecc));
954 GNUNET_OS_process_destroy (gc->gnunet_ecc);
955 gc->gnunet_ecc = NULL;
956 if (NULL == (pk = try_read_key (gc->filename)))
959 gc->cont (gc->cont_cls, NULL, _("gnunet-ecc failed"));
960 GNUNET_CRYPTO_ecc_key_create_stop (gc);
963 gc->cont (gc->cont_cls, pk, NULL);
964 GNUNET_DISK_pipe_close (gc->gnunet_ecc_out);
965 GNUNET_free (gc->filename);
971 * Return the private ECC key which already existed on disk
972 * (asynchronously) to the caller.
974 * @param cls the 'struct GNUNET_CRYPTO_EccKeyGenerationContext'
975 * @param tc scheduler context (unused)
978 async_return_key (void *cls,
979 const struct GNUNET_SCHEDULER_TaskContext *tc)
981 struct GNUNET_CRYPTO_EccKeyGenerationContext *gc = cls;
983 gc->cont (gc->cont_cls,
991 * Create a new private key by reading it from a file. If the files
992 * does not exist, create a new key and write it to the file. If the
993 * contents of the file are invalid the old file is deleted and a
994 * fresh key is created.
996 * @param filename name of file to use for storage
997 * @param cont function to call when done (or on errors)
998 * @param cont_cls closure for 'cont'
999 * @return handle to abort operation, NULL on fatal errors (cont will not be called if NULL is returned)
1001 struct GNUNET_CRYPTO_EccKeyGenerationContext *
1002 GNUNET_CRYPTO_ecc_key_create_start (const char *filename,
1003 GNUNET_CRYPTO_EccKeyCallback cont,
1006 struct GNUNET_CRYPTO_EccKeyGenerationContext *gc;
1007 struct GNUNET_CRYPTO_EccPrivateKey *pk;
1008 const char *weak_random;
1010 if (NULL != (pk = try_read_key (filename)))
1012 /* quick happy ending: key already exists! */
1013 gc = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccKeyGenerationContext));
1016 gc->cont_cls = cont_cls;
1017 gc->read_task = GNUNET_SCHEDULER_add_now (&async_return_key,
1021 gc = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccKeyGenerationContext));
1022 gc->filename = GNUNET_strdup (filename);
1024 gc->cont_cls = cont_cls;
1025 gc->gnunet_ecc_out = GNUNET_DISK_pipe (GNUNET_NO,
1029 if (NULL == gc->gnunet_ecc_out)
1031 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "pipe");
1032 GNUNET_free (gc->filename);
1038 GNUNET_CRYPTO_random_is_weak ())
1040 gc->gnunet_ecc = GNUNET_OS_start_process (GNUNET_NO,
1041 GNUNET_OS_INHERIT_STD_ERR,
1049 if (NULL == gc->gnunet_ecc)
1051 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "fork");
1052 GNUNET_DISK_pipe_close (gc->gnunet_ecc_out);
1053 GNUNET_free (gc->filename);
1057 GNUNET_assert (GNUNET_OK ==
1058 GNUNET_DISK_pipe_close_end (gc->gnunet_ecc_out,
1059 GNUNET_DISK_PIPE_END_WRITE));
1060 gc->read_task = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1061 GNUNET_DISK_pipe_handle (gc->gnunet_ecc_out,
1062 GNUNET_DISK_PIPE_END_READ),
1063 &check_key_generation_completion,
1070 * Setup a hostkey file for a peer given the name of the
1071 * configuration file (!). This function is used so that
1072 * at a later point code can be certain that reading a
1073 * hostkey is fast (for example in time-dependent testcases).
1075 * @param cfg_name name of the configuration file to use
1078 GNUNET_CRYPTO_ecc_setup_hostkey (const char *cfg_name)
1080 struct GNUNET_CONFIGURATION_Handle *cfg;
1081 struct GNUNET_CRYPTO_EccPrivateKey *pk;
1084 cfg = GNUNET_CONFIGURATION_create ();
1085 (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
1087 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY", &fn))
1089 pk = GNUNET_CRYPTO_ecc_key_create_from_file (fn);
1091 GNUNET_CRYPTO_ecc_key_free (pk);
1094 GNUNET_CONFIGURATION_destroy (cfg);
1099 * Encrypt a block with the public key of another host that uses the
1102 * @param block the block to encrypt
1103 * @param size the size of block
1104 * @param publicKey the encoded public key used to encrypt
1105 * @param target where to store the encrypted block
1106 * @returns GNUNET_SYSERR on error, GNUNET_OK if ok
1109 GNUNET_CRYPTO_ecc_encrypt (const void *block, size_t size,
1110 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
1112 struct GNUNET_CRYPTO_EccEncryptedData *target)
1122 GNUNET_assert (size <= sizeof (struct GNUNET_HashCode));
1123 if (! (psexp = decode_public_key (publicKey)))
1124 return GNUNET_SYSERR;
1127 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, block, isize, &isize));
1129 gcry_sexp_build (&data, &erroff,
1130 "(data (flags pkcs1)(value %m))", val));
1131 gcry_mpi_release (val);
1132 GNUNET_assert (0 == gcry_pk_encrypt (&result, data, psexp));
1133 gcry_sexp_release (data);
1134 gcry_sexp_release (psexp);
1136 GNUNET_assert (0 == key_from_sexp (&rval, result, "ecc", "a"));
1137 gcry_sexp_release (result);
1138 isize = sizeof (struct GNUNET_CRYPTO_EccEncryptedData);
1140 gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) target,
1141 isize, &isize, rval));
1142 gcry_mpi_release (rval);
1143 adjust (&target->encoding[0], isize,
1144 sizeof (struct GNUNET_CRYPTO_EccEncryptedData));
1150 * Decrypt a given block with the hostkey.
1152 * @param key the key with which to decrypt this block
1153 * @param block the data to decrypt, encoded as returned by encrypt
1154 * @param result pointer to a location where the result can be stored
1155 * @param max the maximum number of bits to store for the result, if
1156 * the decrypted block is bigger, an error is returned
1157 * @return the size of the decrypted block, -1 on error
1160 GNUNET_CRYPTO_ecc_decrypt (const struct GNUNET_CRYPTO_EccPrivateKey * key,
1161 const struct GNUNET_CRYPTO_EccEncryptedData * block,
1162 void *result, size_t max)
1164 gcry_sexp_t resultsexp;
1169 unsigned char *endp;
1173 GNUNET_assert (0 == gcry_pk_testkey (key->sexp));
1175 size = sizeof (struct GNUNET_CRYPTO_EccEncryptedData);
1177 gcry_mpi_scan (&val, GCRYMPI_FMT_USG, &block->encoding[0],
1180 gcry_sexp_build (&data, &erroff, "(enc-val(flags)(ecc(a %m)))",
1182 gcry_mpi_release (val);
1183 GNUNET_assert (0 == gcry_pk_decrypt (&resultsexp, data, key->sexp));
1184 gcry_sexp_release (data);
1185 /* resultsexp has format "(value %m)" */
1186 GNUNET_assert (NULL !=
1187 (val = gcry_sexp_nth_mpi (resultsexp, 1, GCRYMPI_FMT_USG)));
1188 gcry_sexp_release (resultsexp);
1189 size = max + GNUNET_CRYPTO_ECC_DATA_ENCODING_LENGTH * 2;
1190 tmp = GNUNET_malloc (size);
1191 GNUNET_assert (0 == gcry_mpi_print (GCRYMPI_FMT_USG, tmp, size, &size, val));
1192 gcry_mpi_release (val);
1194 endp += (size - max);
1196 memcpy (result, endp, size);
1203 * Sign a given block.
1205 * @param key private key to use for the signing
1206 * @param purpose what to sign (size, purpose)
1207 * @param sig where to write the signature
1208 * @return GNUNET_SYSERR on error, GNUNET_OK on success
1211 GNUNET_CRYPTO_ecc_sign (const struct GNUNET_CRYPTO_EccPrivateKey *key,
1212 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
1213 struct GNUNET_CRYPTO_EccSignature *sig)
1219 struct GNUNET_HashCode hc;
1223 GNUNET_CRYPTO_hash (purpose, ntohl (purpose->size), &hc);
1224 #define FORMATSTRING "(4:data(5:flags5:pkcs1)(4:hash6:sha51264:0123456789012345678901234567890123456789012345678901234567890123))"
1225 bufSize = strlen (FORMATSTRING) + 1;
1226 buff = GNUNET_malloc (bufSize);
1227 memcpy (buff, FORMATSTRING, bufSize);
1231 ("0123456789012345678901234567890123456789012345678901234567890123))")
1232 - 1], &hc, sizeof (struct GNUNET_HashCode));
1233 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
1235 GNUNET_assert (0 == gcry_pk_sign (&result, data, key->sexp));
1236 gcry_sexp_release (data);
1237 GNUNET_assert (0 == key_from_sexp (&rval, result, "ecc", "s"));
1238 gcry_sexp_release (result);
1239 ssize = sizeof (struct GNUNET_CRYPTO_EccSignature);
1241 gcry_mpi_print (GCRYMPI_FMT_USG, (unsigned char *) sig, ssize,
1243 gcry_mpi_release (rval);
1244 adjust (sig->sig, ssize, sizeof (struct GNUNET_CRYPTO_EccSignature));
1252 * @param purpose what is the purpose that the signature should have?
1253 * @param validate block to validate (size, purpose, data)
1254 * @param sig signature that is being validated
1255 * @param publicKey public key of the signer
1256 * @returns GNUNET_OK if ok, GNUNET_SYSERR if invalid
1259 GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
1260 const struct GNUNET_CRYPTO_EccSignaturePurpose
1262 const struct GNUNET_CRYPTO_EccSignature *sig,
1263 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
1267 gcry_sexp_t sigdata;
1271 struct GNUNET_HashCode hc;
1277 if (purpose != ntohl (validate->purpose))
1278 return GNUNET_SYSERR; /* purpose mismatch */
1279 GNUNET_CRYPTO_hash (validate, ntohl (validate->size), &hc);
1280 size = sizeof (struct GNUNET_CRYPTO_EccSignature);
1282 gcry_mpi_scan (&val, GCRYMPI_FMT_USG,
1283 (const unsigned char *) sig, size, &size));
1285 gcry_sexp_build (&sigdata, &erroff, "(sig-val(ecc(s %m)))",
1287 gcry_mpi_release (val);
1288 bufSize = strlen (FORMATSTRING) + 1;
1289 buff = GNUNET_malloc (bufSize);
1290 memcpy (buff, FORMATSTRING, bufSize);
1292 [strlen (FORMATSTRING) -
1294 ("0123456789012345678901234567890123456789012345678901234567890123))")],
1295 &hc, sizeof (struct GNUNET_HashCode));
1296 GNUNET_assert (0 == gcry_sexp_new (&data, buff, bufSize, 0));
1298 if (! (psexp = decode_public_key (publicKey)))
1300 gcry_sexp_release (data);
1301 gcry_sexp_release (sigdata);
1302 return GNUNET_SYSERR;
1304 rc = gcry_pk_verify (sigdata, data, psexp);
1305 gcry_sexp_release (psexp);
1306 gcry_sexp_release (data);
1307 gcry_sexp_release (sigdata);
1310 LOG (GNUNET_ERROR_TYPE_WARNING,
1311 _("ECC signature verification failed at %s:%d: %s\n"), __FILE__,
1312 __LINE__, gcry_strerror (rc));
1313 return GNUNET_SYSERR;
1319 /* end of crypto_ecc.c */