diff options
| author | djm <> | 2012-10-13 21:23:58 +0000 |
|---|---|---|
| committer | djm <> | 2012-10-13 21:23:58 +0000 |
| commit | adb31f466163d59ad933cf9d299ce6c610f1cff3 (patch) | |
| tree | cfb338ea736543eb88bd6c3f56f007ecb05dce51 /src/lib/libcrypto/ec/ectest.c | |
| parent | 767ddad28eb81a703656eed39ed362c83ac9e20a (diff) | |
| download | openbsd-adb31f466163d59ad933cf9d299ce6c610f1cff3.tar.gz openbsd-adb31f466163d59ad933cf9d299ce6c610f1cff3.tar.bz2 openbsd-adb31f466163d59ad933cf9d299ce6c610f1cff3.zip | |
import OpenSSL-1.0.1c
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/ec/ectest.c | 341 |
1 files changed, 248 insertions, 93 deletions
diff --git a/src/lib/libcrypto/ec/ectest.c b/src/lib/libcrypto/ec/ectest.c index 7509cb9c7c..f107782de0 100644 --- a/src/lib/libcrypto/ec/ectest.c +++ b/src/lib/libcrypto/ec/ectest.c | |||
| @@ -94,6 +94,7 @@ int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); retur | |||
| 94 | #include <openssl/objects.h> | 94 | #include <openssl/objects.h> |
| 95 | #include <openssl/rand.h> | 95 | #include <openssl/rand.h> |
| 96 | #include <openssl/bn.h> | 96 | #include <openssl/bn.h> |
| 97 | #include <openssl/opensslconf.h> | ||
| 97 | 98 | ||
| 98 | #if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12) | 99 | #if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12) |
| 99 | /* suppress "too big too optimize" warning */ | 100 | /* suppress "too big too optimize" warning */ |
| @@ -107,10 +108,6 @@ int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); retur | |||
| 107 | EXIT(1); \ | 108 | EXIT(1); \ |
| 108 | } while (0) | 109 | } while (0) |
| 109 | 110 | ||
| 110 | void prime_field_tests(void); | ||
| 111 | void char2_field_tests(void); | ||
| 112 | void internal_curve_test(void); | ||
| 113 | |||
| 114 | #define TIMING_BASE_PT 0 | 111 | #define TIMING_BASE_PT 0 |
| 115 | #define TIMING_RAND_PT 1 | 112 | #define TIMING_RAND_PT 1 |
| 116 | #define TIMING_SIMUL 2 | 113 | #define TIMING_SIMUL 2 |
| @@ -195,7 +192,50 @@ static void timings(EC_GROUP *group, int type, BN_CTX *ctx) | |||
| 195 | } | 192 | } |
| 196 | #endif | 193 | #endif |
| 197 | 194 | ||
| 198 | void prime_field_tests() | 195 | /* test multiplication with group order, long and negative scalars */ |
| 196 | static void group_order_tests(EC_GROUP *group) | ||
| 197 | { | ||
| 198 | BIGNUM *n1, *n2, *order; | ||
| 199 | EC_POINT *P = EC_POINT_new(group); | ||
| 200 | EC_POINT *Q = EC_POINT_new(group); | ||
| 201 | BN_CTX *ctx = BN_CTX_new(); | ||
| 202 | |||
| 203 | n1 = BN_new(); n2 = BN_new(); order = BN_new(); | ||
| 204 | fprintf(stdout, "verify group order ..."); | ||
| 205 | fflush(stdout); | ||
| 206 | if (!EC_GROUP_get_order(group, order, ctx)) ABORT; | ||
| 207 | if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) ABORT; | ||
| 208 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 209 | fprintf(stdout, "."); | ||
| 210 | fflush(stdout); | ||
| 211 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 212 | if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) ABORT; | ||
| 213 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 214 | fprintf(stdout, " ok\n"); | ||
| 215 | fprintf(stdout, "long/negative scalar tests ... "); | ||
| 216 | if (!BN_one(n1)) ABORT; | ||
| 217 | /* n1 = 1 - order */ | ||
| 218 | if (!BN_sub(n1, n1, order)) ABORT; | ||
| 219 | if(!EC_POINT_mul(group, Q, NULL, P, n1, ctx)) ABORT; | ||
| 220 | if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT; | ||
| 221 | /* n2 = 1 + order */ | ||
| 222 | if (!BN_add(n2, order, BN_value_one())) ABORT; | ||
| 223 | if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT; | ||
| 224 | if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT; | ||
| 225 | /* n2 = (1 - order) * (1 + order) */ | ||
| 226 | if (!BN_mul(n2, n1, n2, ctx)) ABORT; | ||
| 227 | if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT; | ||
| 228 | if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT; | ||
| 229 | fprintf(stdout, "ok\n"); | ||
| 230 | EC_POINT_free(P); | ||
| 231 | EC_POINT_free(Q); | ||
| 232 | BN_free(n1); | ||
| 233 | BN_free(n2); | ||
| 234 | BN_free(order); | ||
| 235 | BN_CTX_free(ctx); | ||
| 236 | } | ||
| 237 | |||
| 238 | static void prime_field_tests(void) | ||
| 199 | { | 239 | { |
| 200 | BN_CTX *ctx = NULL; | 240 | BN_CTX *ctx = NULL; |
| 201 | BIGNUM *p, *a, *b; | 241 | BIGNUM *p, *a, *b; |
| @@ -321,21 +361,21 @@ void prime_field_tests() | |||
| 321 | if (len == 0) ABORT; | 361 | if (len == 0) ABORT; |
| 322 | if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; | 362 | if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; |
| 323 | if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; | 363 | if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; |
| 324 | fprintf(stdout, "Generator as octect string, compressed form:\n "); | 364 | fprintf(stdout, "Generator as octet string, compressed form:\n "); |
| 325 | for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); | 365 | for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); |
| 326 | 366 | ||
| 327 | len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx); | 367 | len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx); |
| 328 | if (len == 0) ABORT; | 368 | if (len == 0) ABORT; |
| 329 | if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; | 369 | if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; |
| 330 | if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; | 370 | if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; |
| 331 | fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n "); | 371 | fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n "); |
| 332 | for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); | 372 | for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); |
| 333 | 373 | ||
| 334 | len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx); | 374 | len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx); |
| 335 | if (len == 0) ABORT; | 375 | if (len == 0) ABORT; |
| 336 | if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; | 376 | if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; |
| 337 | if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; | 377 | if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; |
| 338 | fprintf(stdout, "\nGenerator as octect string, hybrid form:\n "); | 378 | fprintf(stdout, "\nGenerator as octet string, hybrid form:\n "); |
| 339 | for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); | 379 | for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); |
| 340 | 380 | ||
| 341 | if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT; | 381 | if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT; |
| @@ -381,17 +421,7 @@ void prime_field_tests() | |||
| 381 | if (EC_GROUP_get_degree(group) != 160) ABORT; | 421 | if (EC_GROUP_get_degree(group) != 160) ABORT; |
| 382 | fprintf(stdout, " ok\n"); | 422 | fprintf(stdout, " ok\n"); |
| 383 | 423 | ||
| 384 | fprintf(stdout, "verify group order ..."); | 424 | group_order_tests(group); |
| 385 | fflush(stdout); | ||
| 386 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 387 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 388 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 389 | fprintf(stdout, "."); | ||
| 390 | fflush(stdout); | ||
| 391 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 392 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 393 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 394 | fprintf(stdout, " ok\n"); | ||
| 395 | 425 | ||
| 396 | if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; | 426 | if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; |
| 397 | if (!EC_GROUP_copy(P_160, group)) ABORT; | 427 | if (!EC_GROUP_copy(P_160, group)) ABORT; |
| @@ -425,17 +455,7 @@ void prime_field_tests() | |||
| 425 | if (EC_GROUP_get_degree(group) != 192) ABORT; | 455 | if (EC_GROUP_get_degree(group) != 192) ABORT; |
| 426 | fprintf(stdout, " ok\n"); | 456 | fprintf(stdout, " ok\n"); |
| 427 | 457 | ||
| 428 | fprintf(stdout, "verify group order ..."); | 458 | group_order_tests(group); |
| 429 | fflush(stdout); | ||
| 430 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 431 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 432 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 433 | fprintf(stdout, "."); | ||
| 434 | fflush(stdout); | ||
| 435 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 436 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 437 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 438 | fprintf(stdout, " ok\n"); | ||
| 439 | 459 | ||
| 440 | if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; | 460 | if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; |
| 441 | if (!EC_GROUP_copy(P_192, group)) ABORT; | 461 | if (!EC_GROUP_copy(P_192, group)) ABORT; |
| @@ -469,17 +489,7 @@ void prime_field_tests() | |||
| 469 | if (EC_GROUP_get_degree(group) != 224) ABORT; | 489 | if (EC_GROUP_get_degree(group) != 224) ABORT; |
| 470 | fprintf(stdout, " ok\n"); | 490 | fprintf(stdout, " ok\n"); |
| 471 | 491 | ||
| 472 | fprintf(stdout, "verify group order ..."); | 492 | group_order_tests(group); |
| 473 | fflush(stdout); | ||
| 474 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 475 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 476 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 477 | fprintf(stdout, "."); | ||
| 478 | fflush(stdout); | ||
| 479 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 480 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 481 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 482 | fprintf(stdout, " ok\n"); | ||
| 483 | 493 | ||
| 484 | if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; | 494 | if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; |
| 485 | if (!EC_GROUP_copy(P_224, group)) ABORT; | 495 | if (!EC_GROUP_copy(P_224, group)) ABORT; |
| @@ -514,17 +524,7 @@ void prime_field_tests() | |||
| 514 | if (EC_GROUP_get_degree(group) != 256) ABORT; | 524 | if (EC_GROUP_get_degree(group) != 256) ABORT; |
| 515 | fprintf(stdout, " ok\n"); | 525 | fprintf(stdout, " ok\n"); |
| 516 | 526 | ||
| 517 | fprintf(stdout, "verify group order ..."); | 527 | group_order_tests(group); |
| 518 | fflush(stdout); | ||
| 519 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 520 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 521 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 522 | fprintf(stdout, "."); | ||
| 523 | fflush(stdout); | ||
| 524 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 525 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 526 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 527 | fprintf(stdout, " ok\n"); | ||
| 528 | 528 | ||
| 529 | if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; | 529 | if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; |
| 530 | if (!EC_GROUP_copy(P_256, group)) ABORT; | 530 | if (!EC_GROUP_copy(P_256, group)) ABORT; |
| @@ -563,18 +563,8 @@ void prime_field_tests() | |||
| 563 | fprintf(stdout, "verify degree ..."); | 563 | fprintf(stdout, "verify degree ..."); |
| 564 | if (EC_GROUP_get_degree(group) != 384) ABORT; | 564 | if (EC_GROUP_get_degree(group) != 384) ABORT; |
| 565 | fprintf(stdout, " ok\n"); | 565 | fprintf(stdout, " ok\n"); |
| 566 | 566 | ||
| 567 | fprintf(stdout, "verify group order ..."); | 567 | group_order_tests(group); |
| 568 | fflush(stdout); | ||
| 569 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 570 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 571 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 572 | fprintf(stdout, "."); | ||
| 573 | fflush(stdout); | ||
| 574 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 575 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 576 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 577 | fprintf(stdout, " ok\n"); | ||
| 578 | 568 | ||
| 579 | if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; | 569 | if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; |
| 580 | if (!EC_GROUP_copy(P_384, group)) ABORT; | 570 | if (!EC_GROUP_copy(P_384, group)) ABORT; |
| @@ -619,18 +609,8 @@ void prime_field_tests() | |||
| 619 | fprintf(stdout, "verify degree ..."); | 609 | fprintf(stdout, "verify degree ..."); |
| 620 | if (EC_GROUP_get_degree(group) != 521) ABORT; | 610 | if (EC_GROUP_get_degree(group) != 521) ABORT; |
| 621 | fprintf(stdout, " ok\n"); | 611 | fprintf(stdout, " ok\n"); |
| 622 | 612 | ||
| 623 | fprintf(stdout, "verify group order ..."); | 613 | group_order_tests(group); |
| 624 | fflush(stdout); | ||
| 625 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 626 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 627 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 628 | fprintf(stdout, "."); | ||
| 629 | fflush(stdout); | ||
| 630 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 631 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 632 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 633 | fprintf(stdout, " ok\n"); | ||
| 634 | 614 | ||
| 635 | if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; | 615 | if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; |
| 636 | if (!EC_GROUP_copy(P_521, group)) ABORT; | 616 | if (!EC_GROUP_copy(P_521, group)) ABORT; |
| @@ -659,6 +639,7 @@ void prime_field_tests() | |||
| 659 | points[2] = Q; | 639 | points[2] = Q; |
| 660 | points[3] = Q; | 640 | points[3] = Q; |
| 661 | 641 | ||
| 642 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 662 | if (!BN_add(y, z, BN_value_one())) ABORT; | 643 | if (!BN_add(y, z, BN_value_one())) ABORT; |
| 663 | if (BN_is_odd(y)) ABORT; | 644 | if (BN_is_odd(y)) ABORT; |
| 664 | if (!BN_rshift1(y, y)) ABORT; | 645 | if (!BN_rshift1(y, y)) ABORT; |
| @@ -792,22 +773,14 @@ void prime_field_tests() | |||
| 792 | fprintf(stdout, "verify degree ..."); \ | 773 | fprintf(stdout, "verify degree ..."); \ |
| 793 | if (EC_GROUP_get_degree(group) != _degree) ABORT; \ | 774 | if (EC_GROUP_get_degree(group) != _degree) ABORT; \ |
| 794 | fprintf(stdout, " ok\n"); \ | 775 | fprintf(stdout, " ok\n"); \ |
| 795 | fprintf(stdout, "verify group order ..."); \ | 776 | group_order_tests(group); \ |
| 796 | fflush(stdout); \ | ||
| 797 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; \ | ||
| 798 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \ | ||
| 799 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \ | ||
| 800 | fprintf(stdout, "."); \ | ||
| 801 | fflush(stdout); \ | ||
| 802 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; \ | ||
| 803 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \ | ||
| 804 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \ | ||
| 805 | fprintf(stdout, " ok\n"); \ | ||
| 806 | if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \ | 777 | if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \ |
| 807 | if (!EC_GROUP_copy(_variable, group)) ABORT; | 778 | if (!EC_GROUP_copy(_variable, group)) ABORT; \ |
| 808 | 779 | ||
| 809 | void char2_field_tests() | 780 | #ifndef OPENSSL_NO_EC2M |
| 810 | { | 781 | |
| 782 | static void char2_field_tests(void) | ||
| 783 | { | ||
| 811 | BN_CTX *ctx = NULL; | 784 | BN_CTX *ctx = NULL; |
| 812 | BIGNUM *p, *a, *b; | 785 | BIGNUM *p, *a, *b; |
| 813 | EC_GROUP *group; | 786 | EC_GROUP *group; |
| @@ -1239,8 +1212,9 @@ void char2_field_tests() | |||
| 1239 | if (C2_B571) EC_GROUP_free(C2_B571); | 1212 | if (C2_B571) EC_GROUP_free(C2_B571); |
| 1240 | 1213 | ||
| 1241 | } | 1214 | } |
| 1215 | #endif | ||
| 1242 | 1216 | ||
| 1243 | void internal_curve_test(void) | 1217 | static void internal_curve_test(void) |
| 1244 | { | 1218 | { |
| 1245 | EC_builtin_curve *curves = NULL; | 1219 | EC_builtin_curve *curves = NULL; |
| 1246 | size_t crv_len = 0, n = 0; | 1220 | size_t crv_len = 0, n = 0; |
| @@ -1287,13 +1261,189 @@ void internal_curve_test(void) | |||
| 1287 | EC_GROUP_free(group); | 1261 | EC_GROUP_free(group); |
| 1288 | } | 1262 | } |
| 1289 | if (ok) | 1263 | if (ok) |
| 1290 | fprintf(stdout, " ok\n"); | 1264 | fprintf(stdout, " ok\n\n"); |
| 1291 | else | 1265 | else |
| 1292 | fprintf(stdout, " failed\n"); | 1266 | { |
| 1267 | fprintf(stdout, " failed\n\n"); | ||
| 1268 | ABORT; | ||
| 1269 | } | ||
| 1293 | OPENSSL_free(curves); | 1270 | OPENSSL_free(curves); |
| 1294 | return; | 1271 | return; |
| 1295 | } | 1272 | } |
| 1296 | 1273 | ||
| 1274 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 | ||
| 1275 | /* nistp_test_params contains magic numbers for testing our optimized | ||
| 1276 | * implementations of several NIST curves with characteristic > 3. */ | ||
| 1277 | struct nistp_test_params | ||
| 1278 | { | ||
| 1279 | const EC_METHOD* (*meth) (); | ||
| 1280 | int degree; | ||
| 1281 | /* Qx, Qy and D are taken from | ||
| 1282 | * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf | ||
| 1283 | * Otherwise, values are standard curve parameters from FIPS 180-3 */ | ||
| 1284 | const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d; | ||
| 1285 | }; | ||
| 1286 | |||
| 1287 | static const struct nistp_test_params nistp_tests_params[] = | ||
| 1288 | { | ||
| 1289 | { | ||
| 1290 | /* P-224 */ | ||
| 1291 | EC_GFp_nistp224_method, | ||
| 1292 | 224, | ||
| 1293 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", /* p */ | ||
| 1294 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", /* a */ | ||
| 1295 | "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4", /* b */ | ||
| 1296 | "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E", /* Qx */ | ||
| 1297 | "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555", /* Qy */ | ||
| 1298 | "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21", /* Gx */ | ||
| 1299 | "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34", /* Gy */ | ||
| 1300 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D", /* order */ | ||
| 1301 | "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8", /* d */ | ||
| 1302 | }, | ||
| 1303 | { | ||
| 1304 | /* P-256 */ | ||
| 1305 | EC_GFp_nistp256_method, | ||
| 1306 | 256, | ||
| 1307 | "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", /* p */ | ||
| 1308 | "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", /* a */ | ||
| 1309 | "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", /* b */ | ||
| 1310 | "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19", /* Qx */ | ||
| 1311 | "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09", /* Qy */ | ||
| 1312 | "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", /* Gx */ | ||
| 1313 | "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", /* Gy */ | ||
| 1314 | "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", /* order */ | ||
| 1315 | "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96", /* d */ | ||
| 1316 | }, | ||
| 1317 | { | ||
| 1318 | /* P-521 */ | ||
| 1319 | EC_GFp_nistp521_method, | ||
| 1320 | 521, | ||
| 1321 | "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", /* p */ | ||
| 1322 | "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc", /* a */ | ||
| 1323 | "051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", /* b */ | ||
| 1324 | "0098e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4", /* Qx */ | ||
| 1325 | "0164350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e", /* Qy */ | ||
| 1326 | "c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", /* Gx */ | ||
| 1327 | "11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", /* Gy */ | ||
| 1328 | "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409", /* order */ | ||
| 1329 | "0100085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eeedf09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722", /* d */ | ||
| 1330 | }, | ||
| 1331 | }; | ||
| 1332 | |||
| 1333 | void nistp_single_test(const struct nistp_test_params *test) | ||
| 1334 | { | ||
| 1335 | BN_CTX *ctx; | ||
| 1336 | BIGNUM *p, *a, *b, *x, *y, *n, *m, *order; | ||
| 1337 | EC_GROUP *NISTP; | ||
| 1338 | EC_POINT *G, *P, *Q, *Q_CHECK; | ||
| 1339 | |||
| 1340 | fprintf(stdout, "\nNIST curve P-%d (optimised implementation):\n", test->degree); | ||
| 1341 | ctx = BN_CTX_new(); | ||
| 1342 | p = BN_new(); | ||
| 1343 | a = BN_new(); | ||
| 1344 | b = BN_new(); | ||
| 1345 | x = BN_new(); y = BN_new(); | ||
| 1346 | m = BN_new(); n = BN_new(); order = BN_new(); | ||
| 1347 | |||
| 1348 | NISTP = EC_GROUP_new(test->meth()); | ||
| 1349 | if(!NISTP) ABORT; | ||
| 1350 | if (!BN_hex2bn(&p, test->p)) ABORT; | ||
| 1351 | if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT; | ||
| 1352 | if (!BN_hex2bn(&a, test->a)) ABORT; | ||
| 1353 | if (!BN_hex2bn(&b, test->b)) ABORT; | ||
| 1354 | if (!EC_GROUP_set_curve_GFp(NISTP, p, a, b, ctx)) ABORT; | ||
| 1355 | G = EC_POINT_new(NISTP); | ||
| 1356 | P = EC_POINT_new(NISTP); | ||
| 1357 | Q = EC_POINT_new(NISTP); | ||
| 1358 | Q_CHECK = EC_POINT_new(NISTP); | ||
| 1359 | if(!BN_hex2bn(&x, test->Qx)) ABORT; | ||
| 1360 | if(!BN_hex2bn(&y, test->Qy)) ABORT; | ||
| 1361 | if(!EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, y, ctx)) ABORT; | ||
| 1362 | if (!BN_hex2bn(&x, test->Gx)) ABORT; | ||
| 1363 | if (!BN_hex2bn(&y, test->Gy)) ABORT; | ||
| 1364 | if (!EC_POINT_set_affine_coordinates_GFp(NISTP, G, x, y, ctx)) ABORT; | ||
| 1365 | if (!BN_hex2bn(&order, test->order)) ABORT; | ||
| 1366 | if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) ABORT; | ||
| 1367 | |||
| 1368 | fprintf(stdout, "verify degree ... "); | ||
| 1369 | if (EC_GROUP_get_degree(NISTP) != test->degree) ABORT; | ||
| 1370 | fprintf(stdout, "ok\n"); | ||
| 1371 | |||
| 1372 | fprintf(stdout, "NIST test vectors ... "); | ||
| 1373 | if (!BN_hex2bn(&n, test->d)) ABORT; | ||
| 1374 | /* fixed point multiplication */ | ||
| 1375 | EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx); | ||
| 1376 | if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT; | ||
| 1377 | /* random point multiplication */ | ||
| 1378 | EC_POINT_mul(NISTP, Q, NULL, G, n, ctx); | ||
| 1379 | if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT; | ||
| 1380 | |||
| 1381 | /* set generator to P = 2*G, where G is the standard generator */ | ||
| 1382 | if (!EC_POINT_dbl(NISTP, P, G, ctx)) ABORT; | ||
| 1383 | if (!EC_GROUP_set_generator(NISTP, P, order, BN_value_one())) ABORT; | ||
| 1384 | /* set the scalar to m=n/2, where n is the NIST test scalar */ | ||
| 1385 | if (!BN_rshift(m, n, 1)) ABORT; | ||
| 1386 | |||
| 1387 | /* test the non-standard generator */ | ||
| 1388 | /* fixed point multiplication */ | ||
| 1389 | EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx); | ||
| 1390 | if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT; | ||
| 1391 | /* random point multiplication */ | ||
| 1392 | EC_POINT_mul(NISTP, Q, NULL, P, m, ctx); | ||
| 1393 | if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT; | ||
| 1394 | |||
| 1395 | /* now repeat all tests with precomputation */ | ||
| 1396 | if (!EC_GROUP_precompute_mult(NISTP, ctx)) ABORT; | ||
| 1397 | |||
| 1398 | /* fixed point multiplication */ | ||
| 1399 | EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx); | ||
| 1400 | if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT; | ||
| 1401 | /* random point multiplication */ | ||
| 1402 | EC_POINT_mul(NISTP, Q, NULL, P, m, ctx); | ||
| 1403 | if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT; | ||
| 1404 | |||
| 1405 | /* reset generator */ | ||
| 1406 | if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) ABORT; | ||
| 1407 | /* fixed point multiplication */ | ||
| 1408 | EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx); | ||
| 1409 | if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT; | ||
| 1410 | /* random point multiplication */ | ||
| 1411 | EC_POINT_mul(NISTP, Q, NULL, G, n, ctx); | ||
| 1412 | if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT; | ||
| 1413 | |||
| 1414 | fprintf(stdout, "ok\n"); | ||
| 1415 | group_order_tests(NISTP); | ||
| 1416 | #if 0 | ||
| 1417 | timings(NISTP, TIMING_BASE_PT, ctx); | ||
| 1418 | timings(NISTP, TIMING_RAND_PT, ctx); | ||
| 1419 | #endif | ||
| 1420 | EC_GROUP_free(NISTP); | ||
| 1421 | EC_POINT_free(G); | ||
| 1422 | EC_POINT_free(P); | ||
| 1423 | EC_POINT_free(Q); | ||
| 1424 | EC_POINT_free(Q_CHECK); | ||
| 1425 | BN_free(n); | ||
| 1426 | BN_free(m); | ||
| 1427 | BN_free(p); | ||
| 1428 | BN_free(a); | ||
| 1429 | BN_free(b); | ||
| 1430 | BN_free(x); | ||
| 1431 | BN_free(y); | ||
| 1432 | BN_free(order); | ||
| 1433 | BN_CTX_free(ctx); | ||
| 1434 | } | ||
| 1435 | |||
| 1436 | void nistp_tests() | ||
| 1437 | { | ||
| 1438 | unsigned i; | ||
| 1439 | |||
| 1440 | for (i = 0; i < sizeof(nistp_tests_params) / sizeof(struct nistp_test_params); i++) | ||
| 1441 | { | ||
| 1442 | nistp_single_test(&nistp_tests_params[i]); | ||
| 1443 | } | ||
| 1444 | } | ||
| 1445 | #endif | ||
| 1446 | |||
| 1297 | static const char rnd_seed[] = "string to make the random number generator think it has entropy"; | 1447 | static const char rnd_seed[] = "string to make the random number generator think it has entropy"; |
| 1298 | 1448 | ||
| 1299 | int main(int argc, char *argv[]) | 1449 | int main(int argc, char *argv[]) |
| @@ -1317,7 +1467,12 @@ int main(int argc, char *argv[]) | |||
| 1317 | 1467 | ||
| 1318 | prime_field_tests(); | 1468 | prime_field_tests(); |
| 1319 | puts(""); | 1469 | puts(""); |
| 1470 | #ifndef OPENSSL_NO_EC2M | ||
| 1320 | char2_field_tests(); | 1471 | char2_field_tests(); |
| 1472 | #endif | ||
| 1473 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 | ||
| 1474 | nistp_tests(); | ||
| 1475 | #endif | ||
| 1321 | /* test the internal curves */ | 1476 | /* test the internal curves */ |
| 1322 | internal_curve_test(); | 1477 | internal_curve_test(); |
| 1323 | 1478 | ||
