diff options
Diffstat (limited to 'src/util/gnunet-ecc.c')
-rw-r--r-- | src/util/gnunet-ecc.c | 60 |
1 files changed, 20 insertions, 40 deletions
diff --git a/src/util/gnunet-ecc.c b/src/util/gnunet-ecc.c index b19fc1998..d7f1dd58d 100644 --- a/src/util/gnunet-ecc.c +++ b/src/util/gnunet-ecc.c | |||
@@ -65,38 +65,12 @@ struct GNUNET_CRYPTO_EccPrivateKey | |||
65 | 65 | ||
66 | 66 | ||
67 | /** | 67 | /** |
68 | * Create a new private key. Caller must free return value. | ||
69 | * | ||
70 | * @return fresh private key | ||
71 | */ | ||
72 | static struct GNUNET_CRYPTO_EccPrivateKey * | ||
73 | ecc_key_create () | ||
74 | { | ||
75 | struct GNUNET_CRYPTO_EccPrivateKey *ret; | ||
76 | gcry_sexp_t s_key; | ||
77 | gcry_sexp_t s_keyparam; | ||
78 | |||
79 | GNUNET_assert (0 == | ||
80 | gcry_sexp_build (&s_keyparam, NULL, | ||
81 | "(genkey(ecc(nbits %d)(ecc-use-e 3:257)))", | ||
82 | 2048)); | ||
83 | GNUNET_assert (0 == gcry_pk_genkey (&s_key, s_keyparam)); | ||
84 | gcry_sexp_release (s_keyparam); | ||
85 | #if EXTRA_CHECKS | ||
86 | GNUNET_assert (0 == gcry_pk_testkey (s_key)); | ||
87 | #endif | ||
88 | ret = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EccPrivateKey)); | ||
89 | ret->sexp = s_key; | ||
90 | return ret; | ||
91 | } | ||
92 | |||
93 | |||
94 | /** | ||
95 | * Create a flat file with a large number of key pairs for testing. | 68 | * Create a flat file with a large number of key pairs for testing. |
96 | */ | 69 | */ |
97 | static void | 70 | static void |
98 | create_keys (const char *fn) | 71 | create_keys (const char *fn) |
99 | { | 72 | { |
73 | static char pad[GNUNET_TESTING_HOSTKEYFILESIZE]; | ||
100 | FILE *f; | 74 | FILE *f; |
101 | struct GNUNET_CRYPTO_EccPrivateKey *pk; | 75 | struct GNUNET_CRYPTO_EccPrivateKey *pk; |
102 | struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc; | 76 | struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *enc; |
@@ -116,28 +90,34 @@ create_keys (const char *fn) | |||
116 | { | 90 | { |
117 | fprintf (stderr, | 91 | fprintf (stderr, |
118 | "."); | 92 | "."); |
119 | if (NULL == (pk = ecc_key_create ())) | 93 | if (NULL == (pk = GNUNET_CRYPTO_ecc_key_create ())) |
120 | { | 94 | { |
121 | GNUNET_break (0); | 95 | GNUNET_break (0); |
122 | break; | 96 | break; |
123 | } | 97 | } |
124 | enc = GNUNET_CRYPTO_ecc_encode_key (pk); | 98 | enc = GNUNET_CRYPTO_ecc_encode_key (pk); |
125 | if (htons (enc->size) != fwrite (enc, 1, htons (enc->size), f)) | 99 | GNUNET_assert (ntohs (enc->size) <= GNUNET_TESTING_HOSTKEYFILESIZE); |
126 | { | 100 | if ( (ntohs (enc->size) != fwrite (enc, 1, ntohs (enc->size), f)) || |
127 | fprintf (stderr, | 101 | (GNUNET_TESTING_HOSTKEYFILESIZE - ntohs (enc->size) |
128 | _("\nFailed to write to `%s': %s\n"), | 102 | != fwrite (pad, 1, GNUNET_TESTING_HOSTKEYFILESIZE - ntohs (enc->size), f)) ) |
129 | fn, | 103 | { |
130 | STRERROR (errno)); | 104 | fprintf (stderr, |
131 | GNUNET_CRYPTO_ecc_key_free (pk); | 105 | _("\nFailed to write to `%s': %s\n"), |
132 | GNUNET_free (enc); | 106 | fn, |
133 | break; | 107 | STRERROR (errno)); |
134 | } | 108 | GNUNET_CRYPTO_ecc_key_free (pk); |
109 | GNUNET_free (enc); | ||
110 | break; | ||
111 | } | ||
135 | GNUNET_CRYPTO_ecc_key_free (pk); | 112 | GNUNET_CRYPTO_ecc_key_free (pk); |
136 | GNUNET_free (enc); | 113 | GNUNET_free (enc); |
137 | } | 114 | } |
138 | if (0 == make_keys) | 115 | if (UINT_MAX == make_keys) |
116 | fprintf (stderr, | ||
117 | _("\nFinished!\n")); | ||
118 | else | ||
139 | fprintf (stderr, | 119 | fprintf (stderr, |
140 | _("Finished!\n")); | 120 | _("\nError, %u keys not generated\n"), make_keys); |
141 | fclose (f); | 121 | fclose (f); |
142 | } | 122 | } |
143 | 123 | ||