2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/crypto_ecc.c
23 * @brief public key cryptography (ECC) with libgcrypt
24 * @author Christian Grothoff
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_strings_lib.h"
30 #include "benchmark.h"
32 #define EXTRA_CHECKS 0
35 * Name of the curve we are using. Note that we have hard-coded
36 * structs that use 256 bits, so using a bigger curve will require
37 * changes that break stuff badly. The name of the curve given here
38 * must be agreed by all peers and be supported by libgcrypt.
40 #define CURVE "Ed25519"
42 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
44 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
46 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
49 * Log an error message at log-level 'level' that indicates
50 * a failure of the command 'cmd' with the message given
51 * by gcry_strerror(rc).
53 #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)
57 * Extract values from an S-expression.
59 * @param array where to store the result(s)
60 * @param sexp S-expression to parse
61 * @param topname top-level name in the S-expression that is of interest
62 * @param elems names of the elements to extract
63 * @return 0 on success
66 key_from_sexp (gcry_mpi_t * array,
77 list = gcry_sexp_find_token (sexp, topname, 0);
80 l2 = gcry_sexp_cadr (list);
81 gcry_sexp_release (list);
87 for (s = elems; *s; s++, idx++)
89 l2 = gcry_sexp_find_token (list, s, 1);
92 for (i = 0; i < idx; i++)
97 gcry_sexp_release (list);
98 return 3; /* required parameter not found */
100 array[idx] = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
101 gcry_sexp_release (l2);
104 for (i = 0; i < idx; i++)
106 gcry_free (array[i]);
109 gcry_sexp_release (list);
110 return 4; /* required parameter is invalid */
113 gcry_sexp_release (list);
119 * Convert the given private key from the network format to the
120 * S-expression that can be used by libgcrypt.
122 * @param priv private key to decode
123 * @return NULL on error
126 decode_private_ecdsa_key (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv)
131 rc = gcry_sexp_build (&result, NULL,
132 "(private-key(ecc(curve \"" CURVE "\")"
134 (int) sizeof (priv->d), priv->d);
137 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
141 if (0 != (rc = gcry_pk_testkey (result)))
143 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
152 * Convert the given private key from the network format to the
153 * S-expression that can be used by libgcrypt.
155 * @param priv private key to decode
156 * @return NULL on error
159 decode_private_eddsa_key (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
164 rc = gcry_sexp_build (&result, NULL,
165 "(private-key(ecc(curve \"" CURVE "\")"
166 "(flags eddsa)(d %b)))",
167 (int)sizeof (priv->d), priv->d);
170 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
174 if (0 != (rc = gcry_pk_testkey (result)))
176 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
185 * Convert the given private key from the network format to the
186 * S-expression that can be used by libgcrypt.
188 * @param priv private key to decode
189 * @return NULL on error
192 decode_private_ecdhe_key (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv)
197 rc = gcry_sexp_build (&result, NULL,
198 "(private-key(ecc(curve \"" CURVE "\")"
200 (int)sizeof (priv->d), priv->d);
203 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
207 if (0 != (rc = gcry_pk_testkey (result)))
209 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
218 * Extract the public key for the given private key.
220 * @param priv the private key
221 * @param pub where to write the public key
224 GNUNET_CRYPTO_ecdsa_key_get_public (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
225 struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
231 BENCHMARK_START (ecdsa_key_get_public);
233 sexp = decode_private_ecdsa_key (priv);
234 GNUNET_assert (NULL != sexp);
235 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, sexp, NULL));
236 gcry_sexp_release (sexp);
237 q = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
238 GNUNET_assert (NULL != q);
239 GNUNET_CRYPTO_mpi_print_unsigned (pub->q_y, sizeof (pub->q_y), q);
240 gcry_mpi_release (q);
241 gcry_ctx_release (ctx);
243 BENCHMARK_END (ecdsa_key_get_public);
248 * Extract the public key for the given private key.
250 * @param priv the private key
251 * @param pub where to write the public key
254 GNUNET_CRYPTO_eddsa_key_get_public (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
255 struct GNUNET_CRYPTO_EddsaPublicKey *pub)
261 BENCHMARK_START (eddsa_key_get_public);
263 sexp = decode_private_eddsa_key (priv);
264 GNUNET_assert (NULL != sexp);
265 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, sexp, NULL));
266 gcry_sexp_release (sexp);
267 q = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
269 GNUNET_CRYPTO_mpi_print_unsigned (pub->q_y, sizeof (pub->q_y), q);
270 gcry_mpi_release (q);
271 gcry_ctx_release (ctx);
273 BENCHMARK_END (eddsa_key_get_public);
278 * Extract the public key for the given private key.
280 * @param priv the private key
281 * @param pub where to write the public key
284 GNUNET_CRYPTO_ecdhe_key_get_public (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
285 struct GNUNET_CRYPTO_EcdhePublicKey *pub)
291 BENCHMARK_START (ecdhe_key_get_public);
293 sexp = decode_private_ecdhe_key (priv);
294 GNUNET_assert (NULL != sexp);
295 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, sexp, NULL));
296 gcry_sexp_release (sexp);
297 q = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
299 GNUNET_CRYPTO_mpi_print_unsigned (pub->q_y, sizeof (pub->q_y), q);
300 gcry_mpi_release (q);
301 gcry_ctx_release (ctx);
303 BENCHMARK_END (ecdhe_key_get_public);
308 * Convert a public key to a string.
310 * @param pub key to convert
311 * @return string representing @a pub
314 GNUNET_CRYPTO_ecdsa_public_key_to_string (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
317 size_t keylen = (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
321 keylen += 5 - keylen % 5;
323 pubkeybuf = GNUNET_malloc (keylen + 1);
324 end = GNUNET_STRINGS_data_to_string ((unsigned char *) pub,
325 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
330 GNUNET_free (pubkeybuf);
339 * Convert a public key to a string.
341 * @param pub key to convert
342 * @return string representing @a pub
345 GNUNET_CRYPTO_eddsa_public_key_to_string (const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
348 size_t keylen = (sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)) * 8;
352 keylen += 5 - keylen % 5;
354 pubkeybuf = GNUNET_malloc (keylen + 1);
355 end = GNUNET_STRINGS_data_to_string ((unsigned char *) pub,
356 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey),
361 GNUNET_free (pubkeybuf);
370 * Convert a private key to a string.
372 * @param priv key to convert
373 * @return string representing @a pub
376 GNUNET_CRYPTO_eddsa_private_key_to_string (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
379 size_t keylen = (sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)) * 8;
383 keylen += 5 - keylen % 5;
385 privkeybuf = GNUNET_malloc (keylen + 1);
386 end = GNUNET_STRINGS_data_to_string ((unsigned char *) priv,
387 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
392 GNUNET_free (privkeybuf);
401 * Convert a string representing a public key to a public key.
403 * @param enc encoded public key
404 * @param enclen number of bytes in @a enc (without 0-terminator)
405 * @param pub where to store the public key
406 * @return #GNUNET_OK on success
409 GNUNET_CRYPTO_ecdsa_public_key_from_string (const char *enc,
411 struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
413 size_t keylen = (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
416 keylen += 5 - keylen % 5;
418 if (enclen != keylen)
419 return GNUNET_SYSERR;
422 GNUNET_STRINGS_string_to_data (enc, enclen,
424 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)))
425 return GNUNET_SYSERR;
431 * Convert a string representing a public key to a public key.
433 * @param enc encoded public key
434 * @param enclen number of bytes in @a enc (without 0-terminator)
435 * @param pub where to store the public key
436 * @return #GNUNET_OK on success
439 GNUNET_CRYPTO_eddsa_public_key_from_string (const char *enc,
441 struct GNUNET_CRYPTO_EddsaPublicKey *pub)
443 size_t keylen = (sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)) * 8;
446 keylen += 5 - keylen % 5;
448 if (enclen != keylen)
449 return GNUNET_SYSERR;
452 GNUNET_STRINGS_string_to_data (enc, enclen,
454 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)))
455 return GNUNET_SYSERR;
461 * Convert a string representing a private key to a private key.
463 * @param enc encoded public key
464 * @param enclen number of bytes in @a enc (without 0-terminator)
465 * @param priv where to store the private key
466 * @return #GNUNET_OK on success
469 GNUNET_CRYPTO_eddsa_private_key_from_string (const char *enc,
471 struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
473 size_t keylen = (sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)) * 8;
476 keylen += 5 - keylen % 5;
478 if (enclen != keylen)
479 return GNUNET_SYSERR;
482 GNUNET_STRINGS_string_to_data (enc, enclen,
484 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
485 return GNUNET_SYSERR;
488 check_eddsa_key (priv))
500 * Clear memory that was used to store a private key.
502 * @param pk location of the key
505 GNUNET_CRYPTO_ecdhe_key_clear (struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
507 memset (pk, 0, sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
513 * Clear memory that was used to store a private key.
515 * @param pk location of the key
518 GNUNET_CRYPTO_ecdsa_key_clear (struct GNUNET_CRYPTO_EcdsaPrivateKey *pk)
520 memset (pk, 0, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
526 * Clear memory that was used to store a private key.
528 * @param pk location of the key
531 GNUNET_CRYPTO_eddsa_key_clear (struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
533 memset (pk, 0, sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
538 * Create a new private key. Caller must free return value.
540 * @return fresh private key
542 struct GNUNET_CRYPTO_EcdhePrivateKey *
543 GNUNET_CRYPTO_ecdhe_key_create ()
545 struct GNUNET_CRYPTO_EcdhePrivateKey *priv;
547 priv = GNUNET_new (struct GNUNET_CRYPTO_EcdhePrivateKey);
549 GNUNET_CRYPTO_ecdhe_key_create2 (priv))
560 * Create a new private key. Clear with #GNUNET_CRYPTO_ecdhe_key_clear().
562 * @param[out] pk set to fresh private key;
563 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
566 GNUNET_CRYPTO_ecdhe_key_create2 (struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
568 gcry_sexp_t priv_sexp;
569 gcry_sexp_t s_keyparam;
573 BENCHMARK_START (ecdhe_key_create);
575 /* NOTE: For libgcrypt >= 1.7, we do not need the 'eddsa' flag here,
576 but should also be harmless. For libgcrypt < 1.7, using 'eddsa'
577 disables an expensive key testing routine. We do not want to run
578 the expensive check for ECDHE, as we generate TONS of keys to
579 use for a very short time. */
580 if (0 != (rc = gcry_sexp_build (&s_keyparam, NULL,
581 "(genkey(ecc(curve \"" CURVE "\")"
582 "(flags eddsa no-keytest)))")))
584 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
585 return GNUNET_SYSERR;
587 if (0 != (rc = gcry_pk_genkey (&priv_sexp, s_keyparam)))
589 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc);
590 gcry_sexp_release (s_keyparam);
591 return GNUNET_SYSERR;
593 gcry_sexp_release (s_keyparam);
595 if (0 != (rc = gcry_pk_testkey (priv_sexp)))
597 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
598 gcry_sexp_release (priv_sexp);
599 return GNUNET_SYSERR;
602 if (0 != (rc = key_from_sexp (&d, priv_sexp, "private-key", "d")))
604 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc);
605 gcry_sexp_release (priv_sexp);
606 return GNUNET_SYSERR;
608 gcry_sexp_release (priv_sexp);
609 GNUNET_CRYPTO_mpi_print_unsigned (pk->d, sizeof (pk->d), d);
610 gcry_mpi_release (d);
612 BENCHMARK_END (ecdhe_key_create);
619 * Create a new private key. Caller must free return value.
621 * @return fresh private key
623 struct GNUNET_CRYPTO_EcdsaPrivateKey *
624 GNUNET_CRYPTO_ecdsa_key_create ()
626 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
627 gcry_sexp_t priv_sexp;
628 gcry_sexp_t s_keyparam;
632 BENCHMARK_START (ecdsa_key_create);
634 if (0 != (rc = gcry_sexp_build (&s_keyparam, NULL,
635 "(genkey(ecc(curve \"" CURVE "\")"
638 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
641 if (0 != (rc = gcry_pk_genkey (&priv_sexp, s_keyparam)))
643 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc);
644 gcry_sexp_release (s_keyparam);
647 gcry_sexp_release (s_keyparam);
649 if (0 != (rc = gcry_pk_testkey (priv_sexp)))
651 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
652 gcry_sexp_release (priv_sexp);
656 if (0 != (rc = key_from_sexp (&d, priv_sexp, "private-key", "d")))
658 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc);
659 gcry_sexp_release (priv_sexp);
662 gcry_sexp_release (priv_sexp);
663 priv = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
664 GNUNET_CRYPTO_mpi_print_unsigned (priv->d, sizeof (priv->d), d);
665 gcry_mpi_release (d);
667 BENCHMARK_END (ecdsa_key_create);
673 * Create a new private key. Caller must free return value.
675 * @return fresh private key
677 struct GNUNET_CRYPTO_EddsaPrivateKey *
678 GNUNET_CRYPTO_eddsa_key_create ()
680 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
681 gcry_sexp_t priv_sexp;
682 gcry_sexp_t s_keyparam;
686 BENCHMARK_START (eddsa_key_create);
691 if (0 != (rc = gcry_sexp_build (&s_keyparam, NULL,
692 "(genkey(ecc(curve \"" CURVE "\")"
695 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
698 if (0 != (rc = gcry_pk_genkey (&priv_sexp, s_keyparam)))
700 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_genkey", rc);
701 gcry_sexp_release (s_keyparam);
704 gcry_sexp_release (s_keyparam);
706 if (0 != (rc = gcry_pk_testkey (priv_sexp)))
708 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_pk_testkey", rc);
709 gcry_sexp_release (priv_sexp);
713 if (0 != (rc = key_from_sexp (&d, priv_sexp, "private-key", "d")))
715 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "key_from_sexp", rc);
716 gcry_sexp_release (priv_sexp);
719 gcry_sexp_release (priv_sexp);
720 priv = GNUNET_new (struct GNUNET_CRYPTO_EddsaPrivateKey);
721 GNUNET_CRYPTO_mpi_print_unsigned (priv->d, sizeof (priv->d), d);
722 gcry_mpi_release (d);
726 check_eddsa_key (priv))
734 BENCHMARK_END (eddsa_key_create);
741 * Get the shared private key we use for anonymous users.
743 * @return "anonymous" private key
745 const struct GNUNET_CRYPTO_EcdsaPrivateKey *
746 GNUNET_CRYPTO_ecdsa_key_get_anonymous ()
749 * 'anonymous' pseudonym (global static, d=1, public key = G
752 static struct GNUNET_CRYPTO_EcdsaPrivateKey anonymous;
757 GNUNET_CRYPTO_mpi_print_unsigned (anonymous.d,
758 sizeof (anonymous.d),
766 * Compare two Peer Identities.
768 * @param first first peer identity
769 * @param second second peer identity
770 * @return bigger than 0 if first > second,
771 * 0 if they are the same
772 * smaller than 0 if second > first
775 GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
776 const struct GNUNET_PeerIdentity *second)
778 return memcmp (first, second, sizeof (struct GNUNET_PeerIdentity));
783 * Convert the data specified in the given purpose argument to an
784 * S-expression suitable for signature operations.
786 * @param purpose data to convert
787 * @return converted s-expression
790 data_to_eddsa_value (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
796 struct GNUNET_HashCode hc;
798 GNUNET_CRYPTO_hash (purpose,
799 ntohl (purpose->size),
801 if (0 != (rc = gcry_sexp_build (&data, NULL,
802 "(data(flags eddsa)(hash-algo %s)(value %b))",
807 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
813 GNUNET_CRYPTO_hash (purpose, ntohl (purpose->size), &hc);
814 if (0 != (rc = gcry_sexp_build (&data, NULL,
815 "(data(flags eddsa)(hash-algo %s)(value %b))",
817 ntohl (purpose->size),
820 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
831 * Convert the data specified in the given purpose argument to an
832 * S-expression suitable for signature operations.
834 * @param purpose data to convert
835 * @return converted s-expression
838 data_to_ecdsa_value (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
844 struct GNUNET_HashCode hc;
846 GNUNET_CRYPTO_hash (purpose,
847 ntohl (purpose->size),
849 if (0 != (rc = gcry_sexp_build (&data, NULL,
850 "(data(flags rfc6979)(hash %s %b))",
852 (int)sizeof (hc), &hc)))
854 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
860 if (0 != (rc = gcry_sexp_build (&data, NULL,
861 "(data(flags rfc6979)(hash %s %b))",
863 ntohl (purpose->size),
866 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
877 * Sign a given block.
879 * @param priv private key to use for the signing
880 * @param purpose what to sign (size, purpose)
881 * @param sig where to write the signature
882 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
885 GNUNET_CRYPTO_ecdsa_sign (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
886 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
887 struct GNUNET_CRYPTO_EcdsaSignature *sig)
889 gcry_sexp_t priv_sexp;
890 gcry_sexp_t sig_sexp;
895 BENCHMARK_START (ecdsa_sign);
897 priv_sexp = decode_private_ecdsa_key (priv);
898 data = data_to_ecdsa_value (purpose);
899 if (0 != (rc = gcry_pk_sign (&sig_sexp, data, priv_sexp)))
901 LOG (GNUNET_ERROR_TYPE_WARNING,
902 _("ECC signing failed at %s:%d: %s\n"), __FILE__,
903 __LINE__, gcry_strerror (rc));
904 gcry_sexp_release (data);
905 gcry_sexp_release (priv_sexp);
906 return GNUNET_SYSERR;
908 gcry_sexp_release (priv_sexp);
909 gcry_sexp_release (data);
911 /* extract 'r' and 's' values from sexpression 'sig_sexp' and store in
913 if (0 != (rc = key_from_sexp (rs, sig_sexp, "sig-val", "rs")))
916 gcry_sexp_release (sig_sexp);
917 return GNUNET_SYSERR;
919 gcry_sexp_release (sig_sexp);
920 GNUNET_CRYPTO_mpi_print_unsigned (sig->r,
923 GNUNET_CRYPTO_mpi_print_unsigned (sig->s,
926 gcry_mpi_release (rs[0]);
927 gcry_mpi_release (rs[1]);
929 BENCHMARK_END (ecdsa_sign);
936 * Sign a given block.
938 * @param priv private key to use for the signing
939 * @param purpose what to sign (size, purpose)
940 * @param sig where to write the signature
941 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
944 GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
945 const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
946 struct GNUNET_CRYPTO_EddsaSignature *sig)
948 gcry_sexp_t priv_sexp;
949 gcry_sexp_t sig_sexp;
954 BENCHMARK_START (eddsa_sign);
956 priv_sexp = decode_private_eddsa_key (priv);
957 data = data_to_eddsa_value (purpose);
958 if (0 != (rc = gcry_pk_sign (&sig_sexp, data, priv_sexp)))
960 LOG (GNUNET_ERROR_TYPE_WARNING,
961 _("EdDSA signing failed at %s:%d: %s\n"), __FILE__,
962 __LINE__, gcry_strerror (rc));
963 gcry_sexp_release (data);
964 gcry_sexp_release (priv_sexp);
965 return GNUNET_SYSERR;
967 gcry_sexp_release (priv_sexp);
968 gcry_sexp_release (data);
970 /* extract 'r' and 's' values from sexpression 'sig_sexp' and store in
972 if (0 != (rc = key_from_sexp (rs, sig_sexp, "sig-val", "rs")))
975 gcry_sexp_release (sig_sexp);
976 return GNUNET_SYSERR;
978 gcry_sexp_release (sig_sexp);
979 GNUNET_CRYPTO_mpi_print_unsigned (sig->r, sizeof (sig->r), rs[0]);
980 GNUNET_CRYPTO_mpi_print_unsigned (sig->s, sizeof (sig->s), rs[1]);
981 gcry_mpi_release (rs[0]);
982 gcry_mpi_release (rs[1]);
984 BENCHMARK_END (eddsa_sign);
993 * @param purpose what is the purpose that the signature should have?
994 * @param validate block to validate (size, purpose, data)
995 * @param sig signature that is being validated
996 * @param pub public key of the signer
997 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1000 GNUNET_CRYPTO_ecdsa_verify (uint32_t purpose,
1001 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1002 const struct GNUNET_CRYPTO_EcdsaSignature *sig,
1003 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub)
1006 gcry_sexp_t sig_sexpr;
1007 gcry_sexp_t pub_sexpr;
1010 BENCHMARK_START (ecdsa_verify);
1012 if (purpose != ntohl (validate->purpose))
1013 return GNUNET_SYSERR; /* purpose mismatch */
1015 /* build s-expression for signature */
1016 if (0 != (rc = gcry_sexp_build (&sig_sexpr, NULL,
1017 "(sig-val(ecdsa(r %b)(s %b)))",
1018 (int) sizeof (sig->r), sig->r,
1019 (int) sizeof (sig->s), sig->s)))
1021 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
1022 return GNUNET_SYSERR;
1024 data = data_to_ecdsa_value (validate);
1025 if (0 != (rc = gcry_sexp_build (&pub_sexpr, NULL,
1026 "(public-key(ecc(curve " CURVE ")(q %b)))",
1027 (int) sizeof (pub->q_y), pub->q_y)))
1029 gcry_sexp_release (data);
1030 gcry_sexp_release (sig_sexpr);
1031 return GNUNET_SYSERR;
1033 rc = gcry_pk_verify (sig_sexpr, data, pub_sexpr);
1034 gcry_sexp_release (pub_sexpr);
1035 gcry_sexp_release (data);
1036 gcry_sexp_release (sig_sexpr);
1039 LOG (GNUNET_ERROR_TYPE_INFO,
1040 _("ECDSA signature verification failed at %s:%d: %s\n"), __FILE__,
1041 __LINE__, gcry_strerror (rc));
1042 BENCHMARK_END (ecdsa_verify);
1043 return GNUNET_SYSERR;
1045 BENCHMARK_END (ecdsa_verify);
1054 * @param purpose what is the purpose that the signature should have?
1055 * @param validate block to validate (size, purpose, data)
1056 * @param sig signature that is being validated
1057 * @param pub public key of the signer
1058 * @returns #GNUNET_OK if ok, #GNUNET_SYSERR if invalid
1061 GNUNET_CRYPTO_eddsa_verify (uint32_t purpose,
1062 const struct GNUNET_CRYPTO_EccSignaturePurpose *validate,
1063 const struct GNUNET_CRYPTO_EddsaSignature *sig,
1064 const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
1067 gcry_sexp_t sig_sexpr;
1068 gcry_sexp_t pub_sexpr;
1071 BENCHMARK_START (eddsa_verify);
1073 if (purpose != ntohl (validate->purpose))
1074 return GNUNET_SYSERR; /* purpose mismatch */
1076 /* build s-expression for signature */
1077 if (0 != (rc = gcry_sexp_build (&sig_sexpr, NULL,
1078 "(sig-val(eddsa(r %b)(s %b)))",
1079 (int)sizeof (sig->r), sig->r,
1080 (int)sizeof (sig->s), sig->s)))
1082 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
1083 return GNUNET_SYSERR;
1085 data = data_to_eddsa_value (validate);
1086 if (0 != (rc = gcry_sexp_build (&pub_sexpr, NULL,
1087 "(public-key(ecc(curve " CURVE ")(flags eddsa)(q %b)))",
1088 (int)sizeof (pub->q_y), pub->q_y)))
1090 gcry_sexp_release (data);
1091 gcry_sexp_release (sig_sexpr);
1092 return GNUNET_SYSERR;
1094 rc = gcry_pk_verify (sig_sexpr, data, pub_sexpr);
1095 gcry_sexp_release (pub_sexpr);
1096 gcry_sexp_release (data);
1097 gcry_sexp_release (sig_sexpr);
1100 LOG (GNUNET_ERROR_TYPE_INFO,
1101 _("EdDSA signature verification failed at %s:%d: %s\n"), __FILE__,
1102 __LINE__, gcry_strerror (rc));
1103 BENCHMARK_END (eddsa_verify);
1104 return GNUNET_SYSERR;
1106 BENCHMARK_END (eddsa_verify);
1112 * Derive key material from a public and a private ECDHE key.
1114 * @param priv private key to use for the ECDH (x)
1115 * @param pub public key to use for the ECDH (yG)
1116 * @param key_material where to write the key material (xyG)
1117 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1120 GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1121 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1122 struct GNUNET_HashCode *key_material)
1124 gcry_mpi_point_t result;
1128 gcry_sexp_t pub_sexpr;
1129 gcry_mpi_t result_x;
1130 unsigned char xbuf[256 / 8];
1133 BENCHMARK_START (ecc_ecdh);
1135 /* first, extract the q = dP value from the public key */
1136 if (0 != gcry_sexp_build (&pub_sexpr, NULL,
1137 "(public-key(ecc(curve " CURVE ")(q %b)))",
1138 (int)sizeof (pub->q_y), pub->q_y))
1139 return GNUNET_SYSERR;
1140 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, pub_sexpr, NULL));
1141 gcry_sexp_release (pub_sexpr);
1142 q = gcry_mpi_ec_get_point ("q", ctx, 0);
1144 /* second, extract the d value from our private key */
1145 GNUNET_CRYPTO_mpi_scan_unsigned (&d, priv->d, sizeof (priv->d));
1147 /* then call the 'multiply' function, to compute the product */
1148 result = gcry_mpi_point_new (0);
1149 gcry_mpi_ec_mul (result, d, q, ctx);
1150 gcry_mpi_point_release (q);
1151 gcry_mpi_release (d);
1153 /* finally, convert point to string for hashing */
1154 result_x = gcry_mpi_new (256);
1155 if (gcry_mpi_ec_get_affine (result_x, NULL, result, ctx))
1157 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "get_affine failed", 0);
1158 gcry_mpi_point_release (result);
1159 gcry_ctx_release (ctx);
1160 return GNUNET_SYSERR;
1162 gcry_mpi_point_release (result);
1163 gcry_ctx_release (ctx);
1165 rsize = sizeof (xbuf);
1166 GNUNET_assert (! gcry_mpi_get_flag (result_x, GCRYMPI_FLAG_OPAQUE));
1167 /* result_x can be negative here, so we do not use 'GNUNET_CRYPTO_mpi_print_unsigned'
1168 as that does not include the sign bit; x should be a 255-bit
1169 value, so with the sign it should fit snugly into the 256-bit
1172 gcry_mpi_print (GCRYMPI_FMT_STD, xbuf, rsize, &rsize,
1174 GNUNET_CRYPTO_hash (xbuf,
1177 gcry_mpi_release (result_x);
1178 BENCHMARK_END (ecc_ecdh);
1184 * Derive the 'h' value for key derivation, where
1187 * @param pub public key for deriviation
1188 * @param label label for deriviation
1189 * @param context additional context to use for HKDF of 'h';
1190 * typically the name of the subsystem/application
1194 derive_h (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1196 const char *context)
1199 struct GNUNET_HashCode hc;
1200 static const char *const salt = "key-derivation";
1202 GNUNET_CRYPTO_kdf (&hc, sizeof (hc),
1203 salt, strlen (salt),
1205 label, strlen (label),
1206 context, strlen (context),
1208 GNUNET_CRYPTO_mpi_scan_unsigned (&h,
1209 (unsigned char *) &hc,
1216 * Derive a private key from a given private key and a label.
1217 * Essentially calculates a private key 'd = H(l,P) * x mod n'
1218 * where n is the size of the ECC group and P is the public
1219 * key associated with the private key 'd'.
1221 * @param priv original private key
1222 * @param label label to use for key deriviation
1223 * @param context additional context to use for HKDF of 'h';
1224 * typically the name of the subsystem/application
1225 * @return derived private key
1227 struct GNUNET_CRYPTO_EcdsaPrivateKey *
1228 GNUNET_CRYPTO_ecdsa_private_key_derive (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1230 const char *context)
1232 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
1233 struct GNUNET_CRYPTO_EcdsaPrivateKey *ret;
1240 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
1242 n = gcry_mpi_ec_get_mpi ("n", ctx, 1);
1243 GNUNET_CRYPTO_ecdsa_key_get_public (priv, &pub);
1245 h = derive_h (&pub, label, context);
1246 GNUNET_CRYPTO_mpi_scan_unsigned (&x,
1249 d = gcry_mpi_new (256);
1250 gcry_mpi_mulm (d, h, x, n);
1251 gcry_mpi_release (h);
1252 gcry_mpi_release (x);
1253 gcry_mpi_release (n);
1254 gcry_ctx_release (ctx);
1255 ret = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
1256 GNUNET_CRYPTO_mpi_print_unsigned (ret->d, sizeof (ret->d), d);
1257 gcry_mpi_release (d);
1263 * Derive a public key from a given public key and a label.
1264 * Essentially calculates a public key 'V = H(l,P) * P'.
1266 * @param pub original public key
1267 * @param label label to use for key derivation
1268 * @param context additional context to use for HKDF of 'h';
1269 * typically the name of the subsystem/application
1270 * @param result where to write the derived public key
1273 GNUNET_CRYPTO_ecdsa_public_key_derive (const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1275 const char *context,
1276 struct GNUNET_CRYPTO_EcdsaPublicKey *result)
1286 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
1288 /* obtain point 'q' from original public key. The provided 'q' is
1289 compressed thus we first store it in the context and then get it
1290 back as a (decompresssed) point. */
1291 q_y = gcry_mpi_set_opaque_copy (NULL, pub->q_y, 8*sizeof (pub->q_y));
1292 GNUNET_assert (NULL != q_y);
1293 GNUNET_assert (0 == gcry_mpi_ec_set_mpi ("q", q_y, ctx));
1294 gcry_mpi_release (q_y);
1295 q = gcry_mpi_ec_get_point ("q", ctx, 0);
1298 /* calculate h_mod_n = h % n */
1299 h = derive_h (pub, label, context);
1300 n = gcry_mpi_ec_get_mpi ("n", ctx, 1);
1301 h_mod_n = gcry_mpi_new (256);
1302 gcry_mpi_mod (h_mod_n, h, n);
1303 /* calculate v = h_mod_n * q */
1304 v = gcry_mpi_point_new (0);
1305 gcry_mpi_ec_mul (v, h_mod_n, q, ctx);
1306 gcry_mpi_release (h_mod_n);
1307 gcry_mpi_release (h);
1308 gcry_mpi_release (n);
1309 gcry_mpi_point_release (q);
1311 /* convert point 'v' to public key that we return */
1312 GNUNET_assert (0 == gcry_mpi_ec_set_point ("q", v, ctx));
1313 gcry_mpi_point_release (v);
1314 q_y = gcry_mpi_ec_get_mpi ("q@eddsa", ctx, 0);
1315 GNUNET_assert (q_y);
1316 GNUNET_CRYPTO_mpi_print_unsigned (result->q_y,
1317 sizeof (result->q_y),
1319 gcry_mpi_release (q_y);
1320 gcry_ctx_release (ctx);
1325 * Reverse the sequence of the bytes in @a buffer
1327 * @param[in|out] buffer buffer to invert
1328 * @param length number of bytes in @a buffer
1331 reverse_buffer (unsigned char *buffer,
1337 for (i=0; i < length/2; i++)
1340 buffer[i] = buffer[length-1-i];
1341 buffer[length-1-i] = tmp;
1347 * Convert the secret @a d of an EdDSA key to the
1348 * value that is actually used in the EdDSA computation.
1350 * @param d secret input
1351 * @return value used for the calculation in EdDSA
1354 eddsa_d_to_a (gcry_mpi_t d)
1356 unsigned char rawmpi[32]; /* 256-bit value */
1358 unsigned char digest[64]; /* 512-bit hash value */
1359 gcry_buffer_t hvec[2];
1363 b = 256 / 8; /* number of bytes in `d` */
1365 /* Note that we clear DIGEST so we can use it as input to left pad
1366 the key with zeroes for hashing. */
1367 memset (digest, 0, sizeof digest);
1368 memset (hvec, 0, sizeof hvec);
1369 rawmpilen = sizeof (rawmpi);
1371 gcry_mpi_print (GCRYMPI_FMT_USG,
1372 rawmpi, rawmpilen, &rawmpilen,
1374 hvec[0].data = digest;
1376 hvec[0].len = b > rawmpilen ? (b - rawmpilen) : 0;
1377 hvec[1].data = rawmpi;
1379 hvec[1].len = rawmpilen;
1381 gcry_md_hash_buffers (GCRY_MD_SHA512,
1385 /* Compute the A value. */
1386 reverse_buffer (digest, 32); /* Only the first half of the hash. */
1387 digest[0] = (digest[0] & 0x7f) | 0x40;
1390 GNUNET_CRYPTO_mpi_scan_unsigned (&a,
1398 * Take point from ECDH and convert it to key material.
1400 * @param result point from ECDH
1401 * @param ctx ECC context
1402 * @param key_material[out] set to derived key material
1403 * @return #GNUNET_OK on success
1406 point_to_hash (gcry_mpi_point_t result,
1408 struct GNUNET_HashCode *key_material)
1410 gcry_mpi_t result_x;
1411 unsigned char xbuf[256 / 8];
1414 /* finally, convert point to string for hashing */
1415 result_x = gcry_mpi_new (256);
1416 if (gcry_mpi_ec_get_affine (result_x, NULL, result, ctx))
1418 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "get_affine failed", 0);
1419 return GNUNET_SYSERR;
1422 rsize = sizeof (xbuf);
1423 GNUNET_assert (! gcry_mpi_get_flag (result_x, GCRYMPI_FLAG_OPAQUE));
1424 /* result_x can be negative here, so we do not use 'GNUNET_CRYPTO_mpi_print_unsigned'
1425 as that does not include the sign bit; x should be a 255-bit
1426 value, so with the sign it should fit snugly into the 256-bit
1429 gcry_mpi_print (GCRYMPI_FMT_STD, xbuf, rsize, &rsize,
1431 GNUNET_CRYPTO_hash (xbuf,
1434 gcry_mpi_release (result_x);
1441 * Derive key material from a ECDH public key and a private EdDSA key.
1442 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1444 * @param priv private key from EdDSA to use for the ECDH (x)
1445 * @param pub public key to use for the ECDH (yG)
1446 * @param key_material where to write the key material H(h(x)yG)
1447 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1450 GNUNET_CRYPTO_eddsa_ecdh (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
1451 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1452 struct GNUNET_HashCode *key_material)
1454 gcry_mpi_point_t result;
1459 gcry_sexp_t pub_sexpr;
1462 BENCHMARK_START (eddsa_ecdh);
1464 /* first, extract the q = dP value from the public key */
1465 if (0 != gcry_sexp_build (&pub_sexpr, NULL,
1466 "(public-key(ecc(curve " CURVE ")(q %b)))",
1467 (int)sizeof (pub->q_y), pub->q_y))
1468 return GNUNET_SYSERR;
1469 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, pub_sexpr, NULL));
1470 gcry_sexp_release (pub_sexpr);
1471 q = gcry_mpi_ec_get_point ("q", ctx, 0);
1473 /* second, extract the d value from our private key */
1474 GNUNET_CRYPTO_mpi_scan_unsigned (&d, priv->d, sizeof (priv->d));
1476 /* NOW, because this is EdDSA, HASH 'd' first! */
1477 a = eddsa_d_to_a (d);
1478 gcry_mpi_release (d);
1480 /* then call the 'multiply' function, to compute the product */
1481 result = gcry_mpi_point_new (0);
1482 gcry_mpi_ec_mul (result, a, q, ctx);
1483 gcry_mpi_point_release (q);
1484 gcry_mpi_release (a);
1486 ret = point_to_hash (result,
1489 gcry_mpi_point_release (result);
1490 gcry_ctx_release (ctx);
1491 BENCHMARK_END (eddsa_ecdh);
1498 * Derive key material from a ECDH public key and a private ECDSA key.
1499 * Dual to #GNUNET_CRRYPTO_ecdh_eddsa.
1501 * @param priv private key from ECDSA to use for the ECDH (x)
1502 * @param pub public key to use for the ECDH (yG)
1503 * @param key_material where to write the key material H(h(x)yG)
1504 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1507 GNUNET_CRYPTO_ecdsa_ecdh (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv,
1508 const struct GNUNET_CRYPTO_EcdhePublicKey *pub,
1509 struct GNUNET_HashCode *key_material)
1511 gcry_mpi_point_t result;
1515 gcry_sexp_t pub_sexpr;
1518 BENCHMARK_START (ecdsa_ecdh);
1520 /* first, extract the q = dP value from the public key */
1521 if (0 != gcry_sexp_build (&pub_sexpr, NULL,
1522 "(public-key(ecc(curve " CURVE ")(q %b)))",
1523 (int)sizeof (pub->q_y), pub->q_y))
1524 return GNUNET_SYSERR;
1525 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, pub_sexpr, NULL));
1526 gcry_sexp_release (pub_sexpr);
1527 q = gcry_mpi_ec_get_point ("q", ctx, 0);
1529 /* second, extract the d value from our private key */
1530 GNUNET_CRYPTO_mpi_scan_unsigned (&d, priv->d, sizeof (priv->d));
1532 /* then call the 'multiply' function, to compute the product */
1533 result = gcry_mpi_point_new (0);
1534 gcry_mpi_ec_mul (result, d, q, ctx);
1535 gcry_mpi_point_release (q);
1536 gcry_mpi_release (d);
1538 /* finally, convert point to string for hashing */
1539 ret = point_to_hash (result,
1542 gcry_mpi_point_release (result);
1543 gcry_ctx_release (ctx);
1544 BENCHMARK_END (ecdsa_ecdh);
1552 * Derive key material from a EdDSA public key and a private ECDH key.
1553 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1555 * @param priv private key to use for the ECDH (y)
1556 * @param pub public key from EdDSA to use for the ECDH (X=h(x)G)
1557 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1558 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1561 GNUNET_CRYPTO_ecdh_eddsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1562 const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
1563 struct GNUNET_HashCode *key_material)
1565 gcry_mpi_point_t result;
1569 gcry_sexp_t pub_sexpr;
1572 BENCHMARK_START (ecdh_eddsa);
1574 /* first, extract the q = dP value from the public key */
1575 if (0 != gcry_sexp_build (&pub_sexpr, NULL,
1576 "(public-key(ecc(curve " CURVE ")(q %b)))",
1577 (int)sizeof (pub->q_y), pub->q_y))
1578 return GNUNET_SYSERR;
1579 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, pub_sexpr, NULL));
1580 gcry_sexp_release (pub_sexpr);
1581 q = gcry_mpi_ec_get_point ("q", ctx, 0);
1583 /* second, extract the d value from our private key */
1584 GNUNET_CRYPTO_mpi_scan_unsigned (&d, priv->d, sizeof (priv->d));
1586 /* then call the 'multiply' function, to compute the product */
1587 result = gcry_mpi_point_new (0);
1588 gcry_mpi_ec_mul (result, d, q, ctx);
1589 gcry_mpi_point_release (q);
1590 gcry_mpi_release (d);
1592 /* finally, convert point to string for hashing */
1593 ret = point_to_hash (result,
1596 gcry_mpi_point_release (result);
1597 gcry_ctx_release (ctx);
1598 BENCHMARK_END (ecdh_eddsa);
1604 * Derive key material from a ECDSA public key and a private ECDH key.
1605 * Dual to #GNUNET_CRRYPTO_eddsa_ecdh.
1607 * @param priv private key to use for the ECDH (y)
1608 * @param pub public key from ECDSA to use for the ECDH (X=h(x)G)
1609 * @param key_material where to write the key material H(yX)=H(h(x)yG)
1610 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
1613 GNUNET_CRYPTO_ecdh_ecdsa (const struct GNUNET_CRYPTO_EcdhePrivateKey *priv,
1614 const struct GNUNET_CRYPTO_EcdsaPublicKey *pub,
1615 struct GNUNET_HashCode *key_material)
1617 return GNUNET_CRYPTO_ecdh_eddsa (priv,
1618 (const struct GNUNET_CRYPTO_EddsaPublicKey *)pub,
1622 /* end of crypto_ecc.c */