summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortb <>2024-11-01 17:08:46 +0000
committertb <>2024-11-01 17:08:46 +0000
commitb738db22fb51fab83f26e9bf9e64a8558e39ee9d (patch)
tree787ed0f15c495162a95eac4331e09cb56d2f78c7
parenteefcb54263e82502e70326344efe768bb90296a8 (diff)
downloadopenbsd-b738db22fb51fab83f26e9bf9e64a8558e39ee9d.tar.gz
openbsd-b738db22fb51fab83f26e9bf9e64a8558e39ee9d.tar.bz2
openbsd-b738db22fb51fab83f26e9bf9e64a8558e39ee9d.zip
Also exercise the "simple" version of the builtin curves, not only "mont"
-rw-r--r--src/regress/lib/libcrypto/ec/ec_asn1_test.c151
1 files changed, 124 insertions, 27 deletions
diff --git a/src/regress/lib/libcrypto/ec/ec_asn1_test.c b/src/regress/lib/libcrypto/ec/ec_asn1_test.c
index 54d99a7bba..8b35bbaa80 100644
--- a/src/regress/lib/libcrypto/ec/ec_asn1_test.c
+++ b/src/regress/lib/libcrypto/ec/ec_asn1_test.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_asn1_test.c,v 1.23 2024/10/29 13:19:22 tb Exp $ */ 1/* $OpenBSD: ec_asn1_test.c,v 1.24 2024/11/01 17:08:46 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org>
4 * Copyright (c) 2024 Theo Buehler <tb@openbsd.org> 4 * Copyright (c) 2024 Theo Buehler <tb@openbsd.org>
@@ -184,7 +184,7 @@ ec_group_pkparameters_test(const char *label, int nid, int asn1_flag,
184 if ((bio_mem = BIO_new(BIO_s_mem())) == NULL) 184 if ((bio_mem = BIO_new(BIO_s_mem())) == NULL)
185 errx(1, "BIO_new failed for BIO_s_mem"); 185 errx(1, "BIO_new failed for BIO_s_mem");
186 186
187 if ((len = i2d_ECPKParameters_bio(bio_mem, group_a)) < 0) { 187 if (i2d_ECPKParameters_bio(bio_mem, group_a) < 0) {
188 fprintf(stderr, "FAIL: i2d_ECPKParameters_bio failed\n"); 188 fprintf(stderr, "FAIL: i2d_ECPKParameters_bio failed\n");
189 goto done; 189 goto done;
190 } 190 }
@@ -212,7 +212,7 @@ ec_group_pkparameters_test(const char *label, int nid, int asn1_flag,
212 EC_GROUP_free(group_b); 212 EC_GROUP_free(group_b);
213 free(out); 213 free(out);
214 214
215 return (failure); 215 return failure;
216} 216}
217 217
218static int 218static int
@@ -242,12 +242,72 @@ ec_group_pkparameters_correct_padding_test(void)
242 sizeof(ec_secp256k1_pkparameters_parameters)); 242 sizeof(ec_secp256k1_pkparameters_parameters));
243} 243}
244 244
245static EC_GROUP *
246ec_group_simple_from_builtin(const EC_GROUP *group, int nid, BN_CTX *ctx)
247{
248 EC_GROUP *simple_group;
249 BIGNUM *p, *a, *b, *x, *y, *order, *cofactor;
250 const EC_POINT *generator;
251 EC_POINT *simple_generator = NULL;
252
253 BN_CTX_start(ctx);
254
255 if ((p = BN_CTX_get(ctx)) == NULL)
256 errx(1, "BN_CTX_get");
257 if ((a = BN_CTX_get(ctx)) == NULL)
258 errx(1, "BN_CTX_get");
259 if ((b = BN_CTX_get(ctx)) == NULL)
260 errx(1, "BN_CTX_get");
261
262 if ((x = BN_CTX_get(ctx)) == NULL)
263 errx(1, "BN_CTX_get");
264 if ((y = BN_CTX_get(ctx)) == NULL)
265 errx(1, "BN_CTX_get");
266
267 if ((order = BN_CTX_get(ctx)) == NULL)
268 errx(1, "BN_CTX_get");
269 if ((cofactor = BN_CTX_get(ctx)) == NULL)
270 errx(1, "BN_CTX_get");
271
272 if (!EC_GROUP_get_curve(group, p, a, b, ctx))
273 errx(1, "EC_GROUP_get_curve");
274 if (!EC_GROUP_get_order(group, order, ctx))
275 errx(1, "EC_GROUP_get_order");
276 if (!EC_GROUP_get_cofactor(group, cofactor, ctx))
277 errx(1, "EC_GROUP_get_cofactor");
278 if ((generator = EC_GROUP_get0_generator(group)) == NULL)
279 errx(1, "EC_GROUP_get0_generator");
280 if (!EC_POINT_get_affine_coordinates(group, generator, x, y, ctx))
281 errx(1, "EC_POINT_get_affine_coordinates");
282
283 if ((simple_group = EC_GROUP_new(EC_GFp_simple_method())) == NULL)
284 errx(1, "EC_GROUP_new");
285 if (!EC_GROUP_set_curve(simple_group, p, a, b, ctx))
286 errx(1, "EC_GROUP_set_curve");
287 EC_GROUP_set_curve_name(simple_group, nid);
288
289 if ((simple_generator = EC_POINT_new(simple_group)) == NULL)
290 errx(1, "EC_POINT_new");
291 if (!EC_POINT_set_affine_coordinates(simple_group, simple_generator,
292 x, y, ctx))
293 errx(1, "EC_POINT_set_affine_coordinates");
294 if (!EC_GROUP_set_generator(simple_group, simple_generator, order,
295 cofactor))
296 errx(1, "EC_GROUP_set_generator");
297
298 BN_CTX_end(ctx);
299
300 EC_POINT_free(simple_generator);
301
302 return simple_group;
303}
304
245static int 305static int
246ec_group_roundtrip_curve(const EC_GROUP *group, const char *descr, int nid) 306ec_group_roundtrip_curve(const EC_GROUP *group, const char *descr, int nid)
247{ 307{
248 EC_GROUP *new_group = NULL; 308 EC_GROUP *new_group = NULL;
249 unsigned char *der = NULL; 309 unsigned char *der = NULL, *new_der = NULL;
250 int der_len; 310 int der_len, new_der_len;
251 const unsigned char *p; 311 const unsigned char *p;
252 int failed = 1; 312 int failed = 1;
253 313
@@ -259,10 +319,21 @@ ec_group_roundtrip_curve(const EC_GROUP *group, const char *descr, int nid)
259 if ((new_group = d2i_ECPKParameters(NULL, &p, der_len)) == NULL) 319 if ((new_group = d2i_ECPKParameters(NULL, &p, der_len)) == NULL)
260 errx(1, "failed to deserialize %s %d", descr, nid); 320 errx(1, "failed to deserialize %s %d", descr, nid);
261 321
262 if (EC_GROUP_cmp(group, new_group, NULL) != 0) { 322 new_der = NULL;
263 fprintf(stderr, "FAIL: %s %d groups mismatch\n", descr, nid); 323 if ((new_der_len = i2d_ECPKParameters(new_group, &new_der)) <= 0)
324 errx(1, "failed to serialize new %s %d", descr, nid);
325
326 if (compare_data(__func__, der, der_len, new_der, new_der_len) == -1) {
327 fprintf(stderr, "FAIL: new and old der for %s %d\n", descr, nid);
264 goto err; 328 goto err;
265 } 329 }
330
331 if (EC_GROUP_method_of(group) == EC_GFp_mont_method()) {
332 if (EC_GROUP_cmp(group, new_group, NULL) != 0) {
333 fprintf(stderr, "FAIL: %s %d groups mismatch\n", descr, nid);
334 goto err;
335 }
336 }
266 if (EC_GROUP_get_asn1_flag(group) != EC_GROUP_get_asn1_flag(new_group)) { 337 if (EC_GROUP_get_asn1_flag(group) != EC_GROUP_get_asn1_flag(new_group)) {
267 fprintf(stderr, "FAIL: %s %d asn1_flag %x != %x\n", descr, nid, 338 fprintf(stderr, "FAIL: %s %d asn1_flag %x != %x\n", descr, nid,
268 EC_GROUP_get_asn1_flag(group), 339 EC_GROUP_get_asn1_flag(group),
@@ -287,45 +358,67 @@ ec_group_roundtrip_curve(const EC_GROUP *group, const char *descr, int nid)
287} 358}
288 359
289static int 360static int
290ec_group_roundtrip_builtin_curve(const EC_builtin_curve *curve) 361ec_group_roundtrip_group(EC_GROUP *group, int nid)
291{ 362{
292 EC_GROUP *group = NULL;
293 int failed = 1; 363 int failed = 1;
294 364
295 if ((group = EC_GROUP_new_by_curve_name(curve->nid)) == NULL)
296 errx(1, "failed to instantiate curve %d", curve->nid);
297
298 if (!EC_GROUP_check(group, NULL)) {
299 fprintf(stderr, "FAIL: EC_GROUP_check(%d) failed\n", curve->nid);
300 goto err;
301 }
302
303 if (EC_GROUP_get_asn1_flag(group) != OPENSSL_EC_NAMED_CURVE) { 365 if (EC_GROUP_get_asn1_flag(group) != OPENSSL_EC_NAMED_CURVE) {
304 fprintf(stderr, "FAIL: ASN.1 flag not set for %d\n", curve->nid); 366 fprintf(stderr, "FAIL: ASN.1 flag not set for %d\n", nid);
305 goto err; 367 goto err;
306 } 368 }
307 if (EC_GROUP_get_point_conversion_form(group) != 369 if (EC_GROUP_get_point_conversion_form(group) !=
308 POINT_CONVERSION_UNCOMPRESSED) { 370 POINT_CONVERSION_UNCOMPRESSED) {
309 fprintf(stderr, "FAIL: %d has point conversion form %02x\n", 371 fprintf(stderr, "FAIL: %d has point conversion form %02x\n",
310 curve->nid, EC_GROUP_get_point_conversion_form(group)); 372 nid, EC_GROUP_get_point_conversion_form(group));
311 goto err; 373 goto err;
312 } 374 }
313 375
314 failed = 0; 376 failed = 0;
315 377
316 failed |= ec_group_roundtrip_curve(group, "named", curve->nid); 378 failed |= ec_group_roundtrip_curve(group, "named", nid);
317 379
318 EC_GROUP_set_asn1_flag(group, 0); 380 EC_GROUP_set_asn1_flag(group, 0);
319 failed |= ec_group_roundtrip_curve(group, "explicit", curve->nid); 381 failed |= ec_group_roundtrip_curve(group, "explicit", nid);
320 382
321 EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_COMPRESSED); 383 EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_COMPRESSED);
322 failed |= ec_group_roundtrip_curve(group, "compressed", curve->nid); 384 failed |= ec_group_roundtrip_curve(group, "compressed", nid);
323 385
324 EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_HYBRID); 386 EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_HYBRID);
325 failed |= ec_group_roundtrip_curve(group, "hybrid", curve->nid); 387 failed |= ec_group_roundtrip_curve(group, "hybrid", nid);
388
389 err:
390 return failed;
391}
392
393static int
394ec_group_roundtrip_builtin_curve(const EC_builtin_curve *curve, BN_CTX *ctx)
395{
396 EC_GROUP *group = NULL, *simple_group = NULL;
397 int failed = 0;
398
399 if ((group = EC_GROUP_new_by_curve_name(curve->nid)) == NULL)
400 errx(1, "failed to instantiate curve %d", curve->nid);
401
402 if (!EC_GROUP_check(group, NULL)) {
403 fprintf(stderr, "FAIL: EC_GROUP_check(%d) failed\n", curve->nid);
404 goto err;
405 }
406
407 if ((simple_group = ec_group_simple_from_builtin(group, curve->nid,
408 ctx)) == NULL)
409 errx(1, "failed to instantiate simple group %d", curve->nid);
410
411 if (!EC_GROUP_check(group, NULL)) {
412 fprintf(stderr, "FAIL: EC_GROUP_check(%d) failed\n", curve->nid);
413 goto err;
414 }
415
416 failed |= ec_group_roundtrip_group(group, curve->nid);
417 failed |= ec_group_roundtrip_group(simple_group, curve->nid);
326 418
327 err: 419 err:
328 EC_GROUP_free(group); 420 EC_GROUP_free(group);
421 EC_GROUP_free(simple_group);
329 422
330 return failed; 423 return failed;
331} 424}
@@ -333,19 +426,24 @@ ec_group_roundtrip_builtin_curve(const EC_builtin_curve *curve)
333static int 426static int
334ec_group_roundtrip_builtin_curves(void) 427ec_group_roundtrip_builtin_curves(void)
335{ 428{
429 BN_CTX *ctx = NULL;
336 EC_builtin_curve *all_curves = NULL; 430 EC_builtin_curve *all_curves = NULL;
337 size_t curve_id, ncurves; 431 size_t curve_id, ncurves;
338 int failed = 0; 432 int failed = 0;
339 433
434 if ((ctx = BN_CTX_new()) == NULL)
435 errx(1, "BN_CTX_new");
436
340 ncurves = EC_get_builtin_curves(NULL, 0); 437 ncurves = EC_get_builtin_curves(NULL, 0);
341 if ((all_curves = calloc(ncurves, sizeof(*all_curves))) == NULL) 438 if ((all_curves = calloc(ncurves, sizeof(*all_curves))) == NULL)
342 err(1, "calloc builtin curves"); 439 err(1, "calloc builtin curves");
343 EC_get_builtin_curves(all_curves, ncurves); 440 EC_get_builtin_curves(all_curves, ncurves);
344 441
345 for (curve_id = 0; curve_id < ncurves; curve_id++) 442 for (curve_id = 0; curve_id < ncurves; curve_id++)
346 failed |= ec_group_roundtrip_builtin_curve(&all_curves[curve_id]); 443 failed |= ec_group_roundtrip_builtin_curve(&all_curves[curve_id], ctx);
347 444
348 free(all_curves); 445 free(all_curves);
446 BN_CTX_free(ctx);
349 447
350 return failed; 448 return failed;
351} 449}
@@ -787,7 +885,6 @@ ec_group_non_builtin_curve(const struct curve *curve, const EC_METHOD *method,
787 unsigned char *der = NULL; 885 unsigned char *der = NULL;
788 long error; 886 long error;
789 int der_len = 0; 887 int der_len = 0;
790 int nid;
791 int failed = 1; 888 int failed = 1;
792 889
793 ERR_clear_error(); 890 ERR_clear_error();
@@ -796,7 +893,7 @@ ec_group_non_builtin_curve(const struct curve *curve, const EC_METHOD *method,
796 if ((group = ec_group_new(curve, method, ctx)) == NULL) 893 if ((group = ec_group_new(curve, method, ctx)) == NULL)
797 goto err; 894 goto err;
798 895
799 if ((nid = EC_GROUP_get_curve_name(group)) == NID_undef) { 896 if (EC_GROUP_get_curve_name(group) == NID_undef) {
800 fprintf(stderr, "FAIL: no curve name set for %s\n", curve->descr); 897 fprintf(stderr, "FAIL: no curve name set for %s\n", curve->descr);
801 goto err; 898 goto err;
802 } 899 }
@@ -3014,5 +3111,5 @@ main(int argc, char **argv)
3014 failed |= ec_group_non_builtin_curves(); 3111 failed |= ec_group_non_builtin_curves();
3015 failed |= ec_group_check_private_keys(); 3112 failed |= ec_group_check_private_keys();
3016 3113
3017 return (failed); 3114 return failed;
3018} 3115}