aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_ecc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/crypto_ecc.c')
-rw-r--r--src/util/crypto_ecc.c58
1 files changed, 29 insertions, 29 deletions
diff --git a/src/util/crypto_ecc.c b/src/util/crypto_ecc.c
index 144cfbd45..34edec3de 100644
--- a/src/util/crypto_ecc.c
+++ b/src/util/crypto_ecc.c
@@ -27,14 +27,14 @@
27#include <gcrypt.h> 27#include <gcrypt.h>
28#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
29 29
30#define EXTRA_CHECKS ALLOW_EXTRA_CHECKS 30#define EXTRA_CHECKS ALLOW_EXTRA_CHECKS
31 31
32/** 32/**
33 * Name of the curve we are using. Note that we have hard-coded 33 * Name of the curve we are using. Note that we have hard-coded
34 * structs that use 256 bits, so using a bigger curve will require 34 * structs that use 256 bits, so using a bigger curve will require
35 * changes that break stuff badly. The name of the curve given here 35 * changes that break stuff badly. The name of the curve given here
36 * must be agreed by all peers and be supported by libgcrypt. 36 * must be agreed by all peers and be supported by libgcrypt.
37 * 37 *
38 * NOTE: this will change to Curve25519 before GNUnet 0.10.0. 38 * NOTE: this will change to Curve25519 before GNUnet 0.10.0.
39 */ 39 */
40#define CURVE "NIST P-256" 40#define CURVE "NIST P-256"
@@ -73,13 +73,13 @@ key_from_sexp (gcry_mpi_t * array, gcry_sexp_t sexp, const char *topname,
73 unsigned int idx; 73 unsigned int idx;
74 74
75 list = gcry_sexp_find_token (sexp, topname, 0); 75 list = gcry_sexp_find_token (sexp, topname, 0);
76 if (! list) 76 if (! list)
77 return 1; 77 return 1;
78 l2 = gcry_sexp_cadr (list); 78 l2 = gcry_sexp_cadr (list);
79 gcry_sexp_release (list); 79 gcry_sexp_release (list);
80 list = l2; 80 list = l2;
81 if (! list) 81 if (! list)
82 return 2; 82 return 2;
83 83
84 idx = 0; 84 idx = 0;
85 for (s = elems; *s; s++, idx++) 85 for (s = elems; *s; s++, idx++)
@@ -136,7 +136,7 @@ adjust (unsigned char *buf,
136 136
137/** 137/**
138 * Output the given MPI value to the given buffer. 138 * Output the given MPI value to the given buffer.
139 * 139 *
140 * @param buf where to output to 140 * @param buf where to output to
141 * @param size number of bytes in @a buf 141 * @param size number of bytes in @a buf
142 * @param val value to write to @a buf 142 * @param val value to write to @a buf
@@ -171,7 +171,7 @@ mpi_scan (gcry_mpi_t *result,
171 int rc; 171 int rc;
172 172
173 if (0 != (rc = gcry_mpi_scan (result, 173 if (0 != (rc = gcry_mpi_scan (result,
174 GCRYMPI_FMT_USG, 174 GCRYMPI_FMT_USG,
175 data, size, &size))) 175 data, size, &size)))
176 { 176 {
177 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc); 177 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
@@ -203,7 +203,7 @@ decode_private_key (const struct GNUNET_CRYPTO_EccPrivateKey *priv)
203 gcry_mpi_release (d); 203 gcry_mpi_release (d);
204 if (0 != rc) 204 if (0 != rc)
205 { 205 {
206 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc); 206 LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_sexp_build", rc);
207 GNUNET_assert (0); 207 GNUNET_assert (0);
208 } 208 }
209#if EXTRA_CHECKS 209#if EXTRA_CHECKS
@@ -224,7 +224,7 @@ decode_private_key (const struct GNUNET_CRYPTO_EccPrivateKey *priv)
224 * @param q point on curve 224 * @param q point on curve
225 * @param pub public key struct to initialize 225 * @param pub public key struct to initialize
226 * @param ctx context to use for ECC operations 226 * @param ctx context to use for ECC operations
227 */ 227 */
228static void 228static void
229point_to_public_sign_key (gcry_mpi_point_t q, 229point_to_public_sign_key (gcry_mpi_point_t q,
230 gcry_ctx_t ctx, 230 gcry_ctx_t ctx,
@@ -232,7 +232,7 @@ point_to_public_sign_key (gcry_mpi_point_t q,
232{ 232{
233 gcry_mpi_t q_x; 233 gcry_mpi_t q_x;
234 gcry_mpi_t q_y; 234 gcry_mpi_t q_y;
235 235
236 q_x = gcry_mpi_new (256); 236 q_x = gcry_mpi_new (256);
237 q_y = gcry_mpi_new (256); 237 q_y = gcry_mpi_new (256);
238 if (gcry_mpi_ec_get_affine (q_x, q_y, q, ctx)) 238 if (gcry_mpi_ec_get_affine (q_x, q_y, q, ctx))
@@ -255,7 +255,7 @@ point_to_public_sign_key (gcry_mpi_point_t q,
255 * @param q point on curve 255 * @param q point on curve
256 * @param pub public key struct to initialize 256 * @param pub public key struct to initialize
257 * @param ctx context to use for ECC operations 257 * @param ctx context to use for ECC operations
258 */ 258 */
259static void 259static void
260point_to_public_encrypt_key (gcry_mpi_point_t q, 260point_to_public_encrypt_key (gcry_mpi_point_t q,
261 gcry_ctx_t ctx, 261 gcry_ctx_t ctx,
@@ -263,7 +263,7 @@ point_to_public_encrypt_key (gcry_mpi_point_t q,
263{ 263{
264 gcry_mpi_t q_x; 264 gcry_mpi_t q_x;
265 gcry_mpi_t q_y; 265 gcry_mpi_t q_y;
266 266
267 q_x = gcry_mpi_new (256); 267 q_x = gcry_mpi_new (256);
268 q_y = gcry_mpi_new (256); 268 q_y = gcry_mpi_new (256);
269 if (gcry_mpi_ec_get_affine (q_x, q_y, q, ctx)) 269 if (gcry_mpi_ec_get_affine (q_x, q_y, q, ctx))
@@ -346,9 +346,9 @@ GNUNET_CRYPTO_ecc_public_sign_key_to_string (const struct GNUNET_CRYPTO_EccPubli
346 keylen += 5 - keylen % 5; 346 keylen += 5 - keylen % 5;
347 keylen /= 5; 347 keylen /= 5;
348 pubkeybuf = GNUNET_malloc (keylen + 1); 348 pubkeybuf = GNUNET_malloc (keylen + 1);
349 end = GNUNET_STRINGS_data_to_string ((unsigned char *) pub, 349 end = GNUNET_STRINGS_data_to_string ((unsigned char *) pub,
350 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey), 350 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
351 pubkeybuf, 351 pubkeybuf,
352 keylen); 352 keylen);
353 if (NULL == end) 353 if (NULL == end)
354 { 354 {
@@ -369,7 +369,7 @@ GNUNET_CRYPTO_ecc_public_sign_key_to_string (const struct GNUNET_CRYPTO_EccPubli
369 * @return #GNUNET_OK on success 369 * @return #GNUNET_OK on success
370 */ 370 */
371int 371int
372GNUNET_CRYPTO_ecc_public_sign_key_from_string (const char *enc, 372GNUNET_CRYPTO_ecc_public_sign_key_from_string (const char *enc,
373 size_t enclen, 373 size_t enclen,
374 struct GNUNET_CRYPTO_EccPublicSignKey *pub) 374 struct GNUNET_CRYPTO_EccPublicSignKey *pub)
375{ 375{
@@ -408,7 +408,7 @@ decode_public_sign_key (const struct GNUNET_CRYPTO_EccPublicSignKey *pub)
408 mpi_scan (&q_x, pub->q_x, sizeof (pub->q_x)); 408 mpi_scan (&q_x, pub->q_x, sizeof (pub->q_x));
409 mpi_scan (&q_y, pub->q_y, sizeof (pub->q_y)); 409 mpi_scan (&q_y, pub->q_y, sizeof (pub->q_y));
410 q = gcry_mpi_point_new (256); 410 q = gcry_mpi_point_new (256);
411 gcry_mpi_point_set (q, q_x, q_y, GCRYMPI_CONST_ONE); 411 gcry_mpi_point_set (q, q_x, q_y, GCRYMPI_CONST_ONE);
412 gcry_mpi_release (q_x); 412 gcry_mpi_release (q_x);
413 gcry_mpi_release (q_y); 413 gcry_mpi_release (q_y);
414 414
@@ -426,7 +426,7 @@ decode_public_sign_key (const struct GNUNET_CRYPTO_EccPublicSignKey *pub)
426 426
427/** 427/**
428 * @ingroup crypto 428 * @ingroup crypto
429 * Clear memory that was used to store a private key. 429 * Clear memory that was used to store a private key.
430 * 430 *
431 * @param pk location of the key 431 * @param pk location of the key
432 */ 432 */
@@ -503,8 +503,8 @@ GNUNET_CRYPTO_ecc_key_get_anonymous ()
503 503
504 if (once) 504 if (once)
505 return &anonymous; 505 return &anonymous;
506 mpi_print (anonymous.d, 506 mpi_print (anonymous.d,
507 sizeof (anonymous.d), 507 sizeof (anonymous.d),
508 GCRYMPI_CONST_ONE); 508 GCRYMPI_CONST_ONE);
509 once = 1; 509 once = 1;
510 return &anonymous; 510 return &anonymous;
@@ -700,7 +700,7 @@ GNUNET_CRYPTO_ecc_key_create_from_configuration (const struct GNUNET_CONFIGURATI
700 struct GNUNET_CRYPTO_EccPrivateKey *priv; 700 struct GNUNET_CRYPTO_EccPrivateKey *priv;
701 char *fn; 701 char *fn;
702 702
703 if (GNUNET_OK != 703 if (GNUNET_OK !=
704 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn)) 704 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn))
705 return NULL; 705 return NULL;
706 priv = GNUNET_CRYPTO_ecc_key_create_from_file (fn); 706 priv = GNUNET_CRYPTO_ecc_key_create_from_file (fn);
@@ -865,7 +865,7 @@ GNUNET_CRYPTO_ecc_verify (uint32_t purpose,
865 /* build s-expression for signature */ 865 /* build s-expression for signature */
866 mpi_scan (&r, sig->r, sizeof (sig->r)); 866 mpi_scan (&r, sig->r, sizeof (sig->r));
867 mpi_scan (&s, sig->s, sizeof (sig->s)); 867 mpi_scan (&s, sig->s, sizeof (sig->s));
868 if (0 != (rc = gcry_sexp_build (&sig_sexpr, NULL, 868 if (0 != (rc = gcry_sexp_build (&sig_sexpr, NULL,
869 "(sig-val(ecdsa(r %m)(s %m)))", 869 "(sig-val(ecdsa(r %m)(s %m)))",
870 r, s))) 870 r, s)))
871 { 871 {
@@ -917,7 +917,7 @@ decode_public_encrypt_key (const struct GNUNET_CRYPTO_EccPublicEncryptKey *pub)
917 mpi_scan (&q_x, pub->q_x, sizeof (pub->q_x)); 917 mpi_scan (&q_x, pub->q_x, sizeof (pub->q_x));
918 mpi_scan (&q_y, pub->q_y, sizeof (pub->q_y)); 918 mpi_scan (&q_y, pub->q_y, sizeof (pub->q_y));
919 q = gcry_mpi_point_new (256); 919 q = gcry_mpi_point_new (256);
920 gcry_mpi_point_set (q, q_x, q_y, GCRYMPI_CONST_ONE); 920 gcry_mpi_point_set (q, q_x, q_y, GCRYMPI_CONST_ONE);
921 gcry_mpi_release (q_x); 921 gcry_mpi_release (q_x);
922 gcry_mpi_release (q_y); 922 gcry_mpi_release (q_y);
923 923
@@ -945,7 +945,7 @@ int
945GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *priv, 945GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
946 const struct GNUNET_CRYPTO_EccPublicEncryptKey *pub, 946 const struct GNUNET_CRYPTO_EccPublicEncryptKey *pub,
947 struct GNUNET_HashCode *key_material) 947 struct GNUNET_HashCode *key_material)
948{ 948{
949 gcry_mpi_point_t result; 949 gcry_mpi_point_t result;
950 gcry_mpi_point_t q; 950 gcry_mpi_point_t q;
951 gcry_mpi_t d; 951 gcry_mpi_t d;
@@ -993,7 +993,7 @@ GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
993 993
994 994
995/** 995/**
996 * Derive the 'h' value for key derivation, where 996 * Derive the 'h' value for key derivation, where
997 * 'h = H(l,P)'. 997 * 'h = H(l,P)'.
998 * 998 *
999 * @param pub public key for deriviation 999 * @param pub public key for deriviation
@@ -1001,8 +1001,8 @@ GNUNET_CRYPTO_ecc_ecdh (const struct GNUNET_CRYPTO_EccPrivateKey *priv,
1001 * @param context additional context to use for HKDF of 'h'; 1001 * @param context additional context to use for HKDF of 'h';
1002 * typically the name of the subsystem/application 1002 * typically the name of the subsystem/application
1003 * @return h value 1003 * @return h value
1004 */ 1004 */
1005static gcry_mpi_t 1005static gcry_mpi_t
1006derive_h (const struct GNUNET_CRYPTO_EccPublicSignKey *pub, 1006derive_h (const struct GNUNET_CRYPTO_EccPublicSignKey *pub,
1007 const char *label, 1007 const char *label,
1008 const char *context) 1008 const char *context)
@@ -1090,7 +1090,7 @@ GNUNET_CRYPTO_ecc_public_key_derive (const struct GNUNET_CRYPTO_EccPublicSignKey
1090 gcry_mpi_point_t v; 1090 gcry_mpi_point_t v;
1091 1091
1092 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE)); 1092 GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
1093 1093
1094 /* obtain point 'q' from original public key */ 1094 /* obtain point 'q' from original public key */
1095 mpi_scan (&q_x, pub->q_x, sizeof (pub->q_x)); 1095 mpi_scan (&q_x, pub->q_x, sizeof (pub->q_x));
1096 mpi_scan (&q_y, pub->q_y, sizeof (pub->q_y)); 1096 mpi_scan (&q_y, pub->q_y, sizeof (pub->q_y));