diff options
author | Markus Teich <markus.teich@stusta.mhn.de> | 2016-06-12 20:52:22 +0200 |
---|---|---|
committer | Markus Teich <markus.teich@stusta.mhn.de> | 2016-06-12 20:52:22 +0200 |
commit | 62b87e57a7f7042d27fe0a80b9194aeae0c14a50 (patch) | |
tree | 961a43363dbca413e4b1e65b367c0ffd553cfaf0 /crypto.c | |
parent | 5957a777076d014b17aada25afe0991397edbacc (diff) | |
download | libbrandt-62b87e57a7f7042d27fe0a80b9194aeae0c14a50.tar.gz libbrandt-62b87e57a7f7042d27fe0a80b9194aeae0c14a50.zip |
add tests for key generation
Diffstat (limited to 'crypto.c')
-rw-r--r-- | crypto.c | 190 |
1 files changed, 149 insertions, 41 deletions
@@ -26,6 +26,28 @@ | |||
26 | 26 | ||
27 | #define CURVE "Ed25519" | 27 | #define CURVE "Ed25519" |
28 | 28 | ||
29 | struct brandt_ec_skey { | ||
30 | unsigned char d[256 / 8]; | ||
31 | }; | ||
32 | |||
33 | struct brandt_ec_pkey { | ||
34 | unsigned char q_y[256 / 8]; | ||
35 | }; | ||
36 | |||
37 | gcry_mpi_point_t ec_gen; | ||
38 | gcry_ctx_t ec_ctx; | ||
39 | |||
40 | void | ||
41 | brandt_crypto_init () | ||
42 | { | ||
43 | gcry_error_t rc; | ||
44 | |||
45 | rc = gcry_mpi_ec_new (&ec_ctx, NULL, CURVE); | ||
46 | brandt_assert_gpgerr (rc); | ||
47 | ec_gen = gcry_mpi_ec_get_point ("g", ec_ctx, 0); | ||
48 | brandt_assert (NULL != ec_gen); | ||
49 | } | ||
50 | |||
29 | /* --- RANDOM --- */ | 51 | /* --- RANDOM --- */ |
30 | 52 | ||
31 | void | 53 | void |
@@ -132,28 +154,31 @@ brandt_mpi_scan_unsigned (gcry_mpi_t *result, const void *data, size_t size) | |||
132 | brandt_assert_gpgerr (rc); | 154 | brandt_assert_gpgerr (rc); |
133 | } | 155 | } |
134 | 156 | ||
135 | /* --- ECDHE --- */ | 157 | /* |
136 | 158 | gcry_mpi_point_t | |
137 | /** | 159 | deserialize_point(const struct brandt_point* data, const int len) |
138 | * Convert the given private key from the network format to the | ||
139 | * S-expression that can be used by libgcrypt. | ||
140 | * | ||
141 | * @param priv private key to decode | ||
142 | * @return NULL on error | ||
143 | */ | ||
144 | static gcry_sexp_t | ||
145 | decode_private_ecdhe_key (const struct brandt_dhe_skey *priv) | ||
146 | { | 160 | { |
147 | gcry_sexp_t result; | 161 | gcry_sexp_t s; |
162 | gcry_ctx_t ctx; | ||
163 | gcry_mpi_point_t ret; | ||
148 | gcry_error_t rc; | 164 | gcry_error_t rc; |
149 | 165 | ||
150 | rc = gcry_sexp_build (&result, NULL, | 166 | rc = gcry_sexp_build(&s, NULL, "(public-key(ecc(curve " CURVE ")(q %b)))", |
151 | "(private-key(ecc(curve \"" CURVE "\")" | 167 | len, data); |
152 | "(d %b)))", | 168 | brandt_assert_gpgerr(rc); |
153 | (int)sizeof (priv->d), priv->d); | 169 | |
154 | brandt_assert_gpgerr (rc); | 170 | rc = gcry_mpi_ec_new(&ctx, s, NULL); |
155 | return result; | 171 | brandt_assert_gpgerr(rc); |
172 | gcry_sexp_release(s); | ||
173 | |||
174 | ret = gcry_mpi_ec_get_point("q", ctx, 0); | ||
175 | brandt_assert(ret); | ||
176 | gcry_ctx_release(ctx); | ||
177 | return ret; | ||
156 | } | 178 | } |
179 | */ | ||
180 | |||
181 | /* --- EC --- */ | ||
157 | 182 | ||
158 | /** | 183 | /** |
159 | * Extract values from an S-expression. | 184 | * Extract values from an S-expression. |
@@ -213,30 +238,81 @@ key_from_sexp (gcry_mpi_t *array, gcry_sexp_t sexp, const char *topname, | |||
213 | return 0; | 238 | return 0; |
214 | } | 239 | } |
215 | 240 | ||
216 | /** | ||
217 | * Create a new private key. | ||
218 | * | ||
219 | * @param priv where to write the private key | ||
220 | */ | ||
221 | void | 241 | void |
222 | brandt_ecdhe_key_create (struct brandt_dhe_skey *priv) | 242 | brandt_ec_skey_create (gcry_mpi_t* skey) |
223 | { | 243 | { |
224 | gcry_sexp_t priv_sexp; | ||
225 | gcry_sexp_t s_keyparam; | 244 | gcry_sexp_t s_keyparam; |
245 | gcry_sexp_t priv_sexp; | ||
226 | gcry_mpi_t d; | 246 | gcry_mpi_t d; |
227 | gcry_error_t rc; | 247 | gcry_error_t rc; |
228 | 248 | ||
229 | rc = gcry_sexp_build (&s_keyparam, NULL, "(genkey(ecc(curve \"" CURVE "\")" | 249 | rc = gcry_sexp_build (&s_keyparam, NULL, "(genkey(ecc(curve \"" CURVE "\")" |
230 | "(flags)))") | 250 | "(flags)))"); |
231 | brandt_assert_gpgerr (rc); | 251 | brandt_assert_gpgerr (rc); |
232 | rc = gcry_pk_genkey (&priv_sexp, s_keyparam) | 252 | rc = gcry_pk_genkey (&priv_sexp, s_keyparam); |
233 | brandt_assert_gpgerr (rc); | 253 | brandt_assert_gpgerr (rc); |
234 | gcry_sexp_release (s_keyparam); | 254 | gcry_sexp_release (s_keyparam); |
235 | rc = key_from_sexp (&d, priv_sexp, "private-key", "d") | 255 | rc = key_from_sexp (skey, priv_sexp, "private-key", "d"); |
236 | brandt_assert_gpgerr (rc); | 256 | brandt_assert_gpgerr (rc); |
237 | gcry_sexp_release (priv_sexp); | 257 | gcry_sexp_release (priv_sexp); |
238 | brandt_mpi_print_unsigned (priv->d, sizeof (priv->d), d); | 258 | } |
239 | gcry_mpi_release (d); | 259 | |
260 | |||
261 | void | ||
262 | brandt_ec_pkey_compute (gcry_mpi_point_t* pkey, const gcry_mpi_t skey) | ||
263 | { | ||
264 | |||
265 | } | ||
266 | |||
267 | |||
268 | void | ||
269 | brandt_ec_keypair_create (gcry_mpi_point_t* pkey, gcry_mpi_t* skey) | ||
270 | { | ||
271 | gcry_error_t rc; | ||
272 | gcry_sexp_t s_keyparam; | ||
273 | gcry_sexp_t priv_sexp; | ||
274 | gcry_ctx_t ctx; | ||
275 | |||
276 | rc = gcry_sexp_build (&s_keyparam, NULL, "(genkey(ecc(curve \"" CURVE "\")" | ||
277 | "(flags)))"); | ||
278 | brandt_assert_gpgerr (rc); | ||
279 | |||
280 | rc = gcry_pk_genkey (&priv_sexp, s_keyparam); | ||
281 | brandt_assert_gpgerr (rc); | ||
282 | gcry_sexp_release (s_keyparam); | ||
283 | |||
284 | rc = key_from_sexp (skey, priv_sexp, "private-key", "d"); | ||
285 | brandt_assert_gpgerr (rc); | ||
286 | |||
287 | rc = gcry_mpi_ec_new (&ctx, priv_sexp, NULL); | ||
288 | brandt_assert_gpgerr (rc); | ||
289 | gcry_sexp_release (priv_sexp); | ||
290 | |||
291 | *pkey = gcry_mpi_ec_get_point("q", ctx, 0); | ||
292 | brandt_assert (NULL != *pkey); | ||
293 | gcry_ctx_release (ctx); | ||
294 | } | ||
295 | |||
296 | |||
297 | /** | ||
298 | * Convert the given private key from the network format to the | ||
299 | * S-expression that can be used by libgcrypt. | ||
300 | * | ||
301 | * @param priv private key to decode | ||
302 | * @return NULL on error | ||
303 | */ | ||
304 | static gcry_sexp_t | ||
305 | decode_private_ecdhe_key (const struct brandt_ec_skey *priv) | ||
306 | { | ||
307 | gcry_sexp_t result; | ||
308 | gcry_error_t rc; | ||
309 | |||
310 | rc = gcry_sexp_build (&result, NULL, | ||
311 | "(private-key(ecc(curve \"" CURVE "\")" | ||
312 | "(d %b)))", | ||
313 | (int)sizeof (priv->d), priv->d); | ||
314 | brandt_assert_gpgerr (rc); | ||
315 | return result; | ||
240 | } | 316 | } |
241 | 317 | ||
242 | /** | 318 | /** |
@@ -246,8 +322,8 @@ brandt_ecdhe_key_create (struct brandt_dhe_skey *priv) | |||
246 | * @param pub where to write the public key | 322 | * @param pub where to write the public key |
247 | */ | 323 | */ |
248 | void | 324 | void |
249 | brandt_ecdhe_key_get_public (const struct brandt_dhe_skey *priv, | 325 | brandt_ecdhe_key_get_public (const struct brandt_ec_skey *priv, |
250 | struct brandt_dhe_pkey *pub) | 326 | struct brandt_ec_pkey *pub) |
251 | { | 327 | { |
252 | gcry_sexp_t sexp; | 328 | gcry_sexp_t sexp; |
253 | gcry_ctx_t ctx; | 329 | gcry_ctx_t ctx; |
@@ -275,8 +351,8 @@ brandt_ecdhe_key_get_public (const struct brandt_dhe_skey *priv, | |||
275 | * @return 0 on error, 1 on success | 351 | * @return 0 on error, 1 on success |
276 | */ | 352 | */ |
277 | int | 353 | int |
278 | brandt_ecdhe (const struct brandt_dhe_skey *priv, | 354 | brandt_ecdhe (const struct brandt_ec_skey *priv, |
279 | const struct brandt_dhe_pkey *pub, | 355 | const struct brandt_ec_pkey *pub, |
280 | struct brandt_hash_code *key_material) | 356 | struct brandt_hash_code *key_material) |
281 | { | 357 | { |
282 | gcry_error_t rc; | 358 | gcry_error_t rc; |
@@ -331,13 +407,45 @@ brandt_ecdhe (const struct brandt_dhe_skey *priv, | |||
331 | } | 407 | } |
332 | 408 | ||
333 | /** | 409 | /** |
334 | * @ingroup crypto | ||
335 | * Clear memory that was used to store a private key. | 410 | * Clear memory that was used to store a private key. |
336 | * | 411 | * |
337 | * @param pk location of the key | 412 | * @param skey location of the key |
338 | */ | 413 | */ |
339 | void | 414 | void |
340 | brandt_ecdhe_key_clear (struct brandt_dhe_skey *pk) | 415 | brandt_ec_key_clear (struct brandt_ec_skey *skey) |
341 | { | 416 | { |
342 | memset (pk, 0, sizeof (struct brandt_dhe_skey)); | 417 | memset (skey, 0, sizeof (struct brandt_ec_skey)); |
343 | } | 418 | } |
419 | |||
420 | /** | ||
421 | * Generate a random value mod n. | ||
422 | * | ||
423 | * @param edc ECC context | ||
424 | * @return random value mod n. | ||
425 | */ | ||
426 | //gcry_mpi_t | ||
427 | //GNUNET_CRYPTO_ecc_random_mod_n (struct GNUNET_CRYPTO_EccDlogContext *edc) | ||
428 | //{ | ||
429 | // gcry_mpi_t n; | ||
430 | // unsigned int highbit; | ||
431 | // gcry_mpi_t r; | ||
432 | // | ||
433 | // n = gcry_mpi_ec_get_mpi ("n", edc->ctx, 1); | ||
434 | // | ||
435 | // /* check public key for number of bits, bail out if key is all zeros */ | ||
436 | // highbit = 256; /* Curve25519 */ | ||
437 | // while ( (! gcry_mpi_test_bit (n, highbit)) && | ||
438 | // (0 != highbit) ) | ||
439 | // highbit--; | ||
440 | // GNUNET_assert (0 != highbit); | ||
441 | // /* generate fact < n (without bias) */ | ||
442 | // GNUNET_assert (NULL != (r = gcry_mpi_new (0))); | ||
443 | // do { | ||
444 | // gcry_mpi_randomize (r, | ||
445 | // highbit + 1, | ||
446 | // GCRY_STRONG_RANDOM); | ||
447 | // } | ||
448 | // while (gcry_mpi_cmp (r, n) >= 0); | ||
449 | // gcry_mpi_release (n); | ||
450 | // return r; | ||
451 | //} | ||