1 /* $OpenBSD: ectest.c,v 1.19 2023/04/18 15:28:17 tb Exp $ */ 2 /* crypto/ec/ectest.c */ 3 /* 4 * Originally written by Bodo Moeller for the OpenSSL project. 5 */ 6 /* ==================================================================== 7 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. All advertising materials mentioning features or use of this 22 * software must display the following acknowledgment: 23 * "This product includes software developed by the OpenSSL Project 24 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 25 * 26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27 * endorse or promote products derived from this software without 28 * prior written permission. For written permission, please contact 29 * openssl-core@openssl.org. 30 * 31 * 5. Products derived from this software may not be called "OpenSSL" 32 * nor may "OpenSSL" appear in their names without prior written 33 * permission of the OpenSSL Project. 34 * 35 * 6. Redistributions of any form whatsoever must retain the following 36 * acknowledgment: 37 * "This product includes software developed by the OpenSSL Project 38 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 39 * 40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51 * OF THE POSSIBILITY OF SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This product includes cryptographic software written by Eric Young 55 * (eay@cryptsoft.com). This product includes software written by Tim 56 * Hudson (tjh@cryptsoft.com). 57 * 58 */ 59 /* ==================================================================== 60 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 61 * 62 * Portions of the attached software ("Contribution") are developed by 63 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. 64 * 65 * The Contribution is licensed pursuant to the OpenSSL open source 66 * license provided above. 67 * 68 * The elliptic curve binary polynomial software is originally written by 69 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. 70 * 71 */ 72 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <time.h> 77 78 #include <openssl/ec.h> 79 #ifndef OPENSSL_NO_ENGINE 80 #include <openssl/engine.h> 81 #endif 82 #include <openssl/err.h> 83 #include <openssl/obj_mac.h> 84 #include <openssl/objects.h> 85 #include <openssl/bn.h> 86 #include <openssl/opensslconf.h> 87 88 #define ABORT do { \ 89 fflush(stdout); \ 90 fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \ 91 ERR_print_errors_fp(stderr); \ 92 exit(1); \ 93 } while (0) 94 95 #define TIMING_BASE_PT 0 96 #define TIMING_RAND_PT 1 97 #define TIMING_SIMUL 2 98 99 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, 100 const EC_POINT *point, BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx); 101 102 /* test multiplication with group order, long and negative scalars */ 103 static void 104 group_order_tests(EC_GROUP *group) 105 { 106 BIGNUM *n1, *n2, *order; 107 EC_POINT *P = EC_POINT_new(group); 108 EC_POINT *Q = EC_POINT_new(group); 109 BN_CTX *ctx; 110 111 if ((ctx = BN_CTX_new()) == NULL) 112 ABORT; 113 114 if ((n1 = BN_new()) == NULL) 115 ABORT; 116 if ((n2 = BN_new()) == NULL) 117 ABORT; 118 if ((order = BN_new()) == NULL) 119 ABORT; 120 fprintf(stdout, "verify group order ..."); 121 fflush(stdout); 122 if (!EC_GROUP_get_order(group, order, ctx)) 123 ABORT; 124 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) 125 ABORT; 126 if (!EC_POINT_is_at_infinity(group, Q)) 127 ABORT; 128 fprintf(stdout, "."); 129 fflush(stdout); 130 if (!EC_GROUP_precompute_mult(group, ctx)) 131 ABORT; 132 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) 133 ABORT; 134 if (!EC_POINT_is_at_infinity(group, Q)) 135 ABORT; 136 fprintf(stdout, " ok\n"); 137 fprintf(stdout, "long/negative scalar tests ... "); 138 /* XXX - switch back to BN_one() after next bump. */ 139 if (!BN_set_word(n1, 1)) 140 ABORT; 141 /* n1 = 1 - order */ 142 if (!BN_sub(n1, n1, order)) 143 ABORT; 144 if (!EC_POINT_mul(group, Q, NULL, P, n1, ctx)) 145 ABORT; 146 if (0 != EC_POINT_cmp(group, Q, P, ctx)) 147 ABORT; 148 /* n2 = 1 + order */ 149 if (!BN_add(n2, order, BN_value_one())) 150 ABORT; 151 if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) 152 ABORT; 153 if (0 != EC_POINT_cmp(group, Q, P, ctx)) 154 ABORT; 155 /* n2 = (1 - order) * (1 + order) */ 156 if (!BN_mul(n2, n1, n2, ctx)) 157 ABORT; 158 if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) 159 ABORT; 160 if (0 != EC_POINT_cmp(group, Q, P, ctx)) 161 ABORT; 162 fprintf(stdout, "ok\n"); 163 EC_POINT_free(P); 164 EC_POINT_free(Q); 165 BN_free(n1); 166 BN_free(n2); 167 BN_free(order); 168 BN_CTX_free(ctx); 169 } 170 171 static void 172 prime_field_tests(void) 173 { 174 BN_CTX *ctx = NULL; 175 BIGNUM *p, *a, *b; 176 EC_GROUP *group; 177 EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL; 178 EC_POINT *P, *Q, *R; 179 BIGNUM *x, *y, *z; 180 unsigned char buf[100]; 181 size_t i, len; 182 int k; 183 184 ctx = BN_CTX_new(); 185 if (!ctx) 186 ABORT; 187 188 p = BN_new(); 189 a = BN_new(); 190 b = BN_new(); 191 if (!p || !a || !b) 192 ABORT; 193 194 if (!BN_hex2bn(&p, "17")) 195 ABORT; 196 if (!BN_hex2bn(&a, "1")) 197 ABORT; 198 if (!BN_hex2bn(&b, "1")) 199 ABORT; 200 201 group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp 202 * so that the library gets to choose the EC_METHOD */ 203 if (!group) 204 ABORT; 205 206 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 207 ABORT; 208 209 { 210 EC_GROUP *tmp; 211 tmp = EC_GROUP_new(EC_GROUP_method_of(group)); 212 if (!tmp) 213 ABORT; 214 if (!EC_GROUP_copy(tmp, group)) 215 ABORT; 216 EC_GROUP_free(group); 217 group = tmp; 218 } 219 220 if (!EC_GROUP_get_curve(group, p, a, b, ctx)) 221 ABORT; 222 223 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x"); 224 BN_print_fp(stdout, p); 225 fprintf(stdout, ")\n a = 0x"); 226 BN_print_fp(stdout, a); 227 fprintf(stdout, "\n b = 0x"); 228 BN_print_fp(stdout, b); 229 fprintf(stdout, "\n"); 230 231 P = EC_POINT_new(group); 232 Q = EC_POINT_new(group); 233 R = EC_POINT_new(group); 234 if (!P || !Q || !R) 235 ABORT; 236 237 if (!EC_POINT_set_to_infinity(group, P)) 238 ABORT; 239 if (!EC_POINT_is_at_infinity(group, P)) 240 ABORT; 241 242 buf[0] = 0; 243 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) 244 ABORT; 245 246 if (!EC_POINT_add(group, P, P, Q, ctx)) 247 ABORT; 248 if (!EC_POINT_is_at_infinity(group, P)) 249 ABORT; 250 251 x = BN_new(); 252 y = BN_new(); 253 z = BN_new(); 254 if (!x || !y || !z) 255 ABORT; 256 257 if (!BN_hex2bn(&x, "D")) 258 ABORT; 259 if (!EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx)) 260 ABORT; 261 if (!EC_POINT_is_on_curve(group, Q, ctx)) { 262 if (!EC_POINT_get_affine_coordinates(group, Q, x, y, ctx)) 263 ABORT; 264 fprintf(stderr, "Point is not on curve: x = 0x"); 265 BN_print_fp(stderr, x); 266 fprintf(stderr, ", y = 0x"); 267 BN_print_fp(stderr, y); 268 fprintf(stderr, "\n"); 269 ABORT; 270 } 271 272 fprintf(stdout, "A cyclic subgroup:\n"); 273 k = 100; 274 do { 275 if (k-- == 0) 276 ABORT; 277 278 if (EC_POINT_is_at_infinity(group, P)) 279 fprintf(stdout, " point at infinity\n"); 280 else { 281 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 282 ABORT; 283 284 fprintf(stdout, " x = 0x"); 285 BN_print_fp(stdout, x); 286 fprintf(stdout, ", y = 0x"); 287 BN_print_fp(stdout, y); 288 fprintf(stdout, "\n"); 289 } 290 291 if (!EC_POINT_copy(R, P)) 292 ABORT; 293 if (!EC_POINT_add(group, P, P, Q, ctx)) 294 ABORT; 295 } while (!EC_POINT_is_at_infinity(group, P)); 296 297 if (!EC_POINT_add(group, P, Q, R, ctx)) 298 ABORT; 299 if (!EC_POINT_is_at_infinity(group, P)) 300 ABORT; 301 302 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx); 303 if (len == 0) 304 ABORT; 305 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) 306 ABORT; 307 if (0 != EC_POINT_cmp(group, P, Q, ctx)) 308 ABORT; 309 fprintf(stdout, "Generator as octet string, compressed form:\n "); 310 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); 311 312 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx); 313 if (len == 0) 314 ABORT; 315 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) 316 ABORT; 317 if (0 != EC_POINT_cmp(group, P, Q, ctx)) 318 ABORT; 319 fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n "); 320 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); 321 322 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx); 323 if (len == 0) 324 ABORT; 325 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) 326 ABORT; 327 if (0 != EC_POINT_cmp(group, P, Q, ctx)) 328 ABORT; 329 fprintf(stdout, "\nGenerator as octet string, hybrid form:\n "); 330 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); 331 332 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) 333 ABORT; 334 fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x"); 335 BN_print_fp(stdout, x); 336 fprintf(stdout, ", Y = 0x"); 337 BN_print_fp(stdout, y); 338 fprintf(stdout, ", Z = 0x"); 339 BN_print_fp(stdout, z); 340 fprintf(stdout, "\n"); 341 342 if (!EC_POINT_invert(group, P, ctx)) 343 ABORT; 344 if (0 != EC_POINT_cmp(group, P, R, ctx)) 345 ABORT; 346 347 348 /* Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2, 2000) 349 * -- not a NIST curve, but commonly used */ 350 351 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF")) 352 ABORT; 353 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 354 ABORT; 355 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC")) 356 ABORT; 357 if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45")) 358 ABORT; 359 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 360 ABORT; 361 362 if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82")) 363 ABORT; 364 if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32")) 365 ABORT; 366 if (!EC_POINT_set_affine_coordinates(group, P, x, y, ctx)) 367 ABORT; 368 if (!EC_POINT_is_on_curve(group, P, ctx)) 369 ABORT; 370 if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257")) 371 ABORT; 372 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 373 ABORT; 374 375 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 376 ABORT; 377 fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x"); 378 BN_print_fp(stdout, x); 379 fprintf(stdout, "\n y = 0x"); 380 BN_print_fp(stdout, y); 381 fprintf(stdout, "\n"); 382 /* G_y value taken from the standard: */ 383 if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32")) 384 ABORT; 385 if (0 != BN_cmp(y, z)) 386 ABORT; 387 388 fprintf(stdout, "verify degree ..."); 389 if (EC_GROUP_get_degree(group) != 160) 390 ABORT; 391 fprintf(stdout, " ok\n"); 392 393 group_order_tests(group); 394 395 if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) 396 ABORT; 397 if (!EC_GROUP_copy(P_160, group)) 398 ABORT; 399 400 401 /* Curve P-192 (FIPS PUB 186-2, App. 6) */ 402 403 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) 404 ABORT; 405 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 406 ABORT; 407 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) 408 ABORT; 409 if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) 410 ABORT; 411 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 412 ABORT; 413 414 if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) 415 ABORT; 416 if (!EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx)) 417 ABORT; 418 if (!EC_POINT_is_on_curve(group, P, ctx)) 419 ABORT; 420 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) 421 ABORT; 422 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 423 ABORT; 424 425 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 426 ABORT; 427 fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x"); 428 BN_print_fp(stdout, x); 429 fprintf(stdout, "\n y = 0x"); 430 BN_print_fp(stdout, y); 431 fprintf(stdout, "\n"); 432 /* G_y value taken from the standard: */ 433 if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) 434 ABORT; 435 if (0 != BN_cmp(y, z)) 436 ABORT; 437 438 fprintf(stdout, "verify degree ..."); 439 if (EC_GROUP_get_degree(group) != 192) 440 ABORT; 441 fprintf(stdout, " ok\n"); 442 443 group_order_tests(group); 444 445 if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) 446 ABORT; 447 if (!EC_GROUP_copy(P_192, group)) 448 ABORT; 449 450 451 /* Curve P-224 (FIPS PUB 186-2, App. 6) */ 452 453 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) 454 ABORT; 455 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 456 ABORT; 457 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) 458 ABORT; 459 if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) 460 ABORT; 461 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 462 ABORT; 463 464 if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) 465 ABORT; 466 if (!EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx)) 467 ABORT; 468 if (!EC_POINT_is_on_curve(group, P, ctx)) 469 ABORT; 470 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) 471 ABORT; 472 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 473 ABORT; 474 475 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 476 ABORT; 477 fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x"); 478 BN_print_fp(stdout, x); 479 fprintf(stdout, "\n y = 0x"); 480 BN_print_fp(stdout, y); 481 fprintf(stdout, "\n"); 482 /* G_y value taken from the standard: */ 483 if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) 484 ABORT; 485 if (0 != BN_cmp(y, z)) 486 ABORT; 487 488 fprintf(stdout, "verify degree ..."); 489 if (EC_GROUP_get_degree(group) != 224) 490 ABORT; 491 fprintf(stdout, " ok\n"); 492 493 group_order_tests(group); 494 495 if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) 496 ABORT; 497 if (!EC_GROUP_copy(P_224, group)) 498 ABORT; 499 500 501 /* Curve P-256 (FIPS PUB 186-2, App. 6) */ 502 503 if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) 504 ABORT; 505 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 506 ABORT; 507 if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) 508 ABORT; 509 if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) 510 ABORT; 511 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 512 ABORT; 513 514 if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) 515 ABORT; 516 if (!EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx)) 517 ABORT; 518 if (!EC_POINT_is_on_curve(group, P, ctx)) 519 ABORT; 520 if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E" 521 "84F3B9CAC2FC632551")) ABORT; 522 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 523 ABORT; 524 525 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 526 ABORT; 527 fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x"); 528 BN_print_fp(stdout, x); 529 fprintf(stdout, "\n y = 0x"); 530 BN_print_fp(stdout, y); 531 fprintf(stdout, "\n"); 532 /* G_y value taken from the standard: */ 533 if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) 534 ABORT; 535 if (0 != BN_cmp(y, z)) 536 ABORT; 537 538 fprintf(stdout, "verify degree ..."); 539 if (EC_GROUP_get_degree(group) != 256) 540 ABORT; 541 fprintf(stdout, " ok\n"); 542 543 group_order_tests(group); 544 545 if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) 546 ABORT; 547 if (!EC_GROUP_copy(P_256, group)) 548 ABORT; 549 550 551 /* Curve P-384 (FIPS PUB 186-2, App. 6) */ 552 553 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 554 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT; 555 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 556 ABORT; 557 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 558 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT; 559 if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141" 560 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT; 561 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 562 ABORT; 563 564 if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B" 565 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT; 566 if (!EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx)) 567 ABORT; 568 if (!EC_POINT_is_on_curve(group, P, ctx)) 569 ABORT; 570 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 571 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT; 572 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 573 ABORT; 574 575 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 576 ABORT; 577 fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x"); 578 BN_print_fp(stdout, x); 579 fprintf(stdout, "\n y = 0x"); 580 BN_print_fp(stdout, y); 581 fprintf(stdout, "\n"); 582 /* G_y value taken from the standard: */ 583 if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14" 584 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT; 585 if (0 != BN_cmp(y, z)) 586 ABORT; 587 588 fprintf(stdout, "verify degree ..."); 589 if (EC_GROUP_get_degree(group) != 384) 590 ABORT; 591 fprintf(stdout, " ok\n"); 592 593 group_order_tests(group); 594 595 if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) 596 ABORT; 597 if (!EC_GROUP_copy(P_384, group)) 598 ABORT; 599 600 601 /* Curve P-521 (FIPS PUB 186-2, App. 6) */ 602 603 if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 604 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 605 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT; 606 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 607 ABORT; 608 if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 609 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 610 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT; 611 if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B" 612 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573" 613 "DF883D2C34F1EF451FD46B503F00")) ABORT; 614 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 615 ABORT; 616 617 if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F" 618 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B" 619 "3C1856A429BF97E7E31C2E5BD66")) ABORT; 620 if (!EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx)) 621 ABORT; 622 if (!EC_POINT_is_on_curve(group, P, ctx)) 623 ABORT; 624 if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 625 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5" 626 "C9B8899C47AEBB6FB71E91386409")) ABORT; 627 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 628 ABORT; 629 630 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 631 ABORT; 632 fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x"); 633 BN_print_fp(stdout, x); 634 fprintf(stdout, "\n y = 0x"); 635 BN_print_fp(stdout, y); 636 fprintf(stdout, "\n"); 637 /* G_y value taken from the standard: */ 638 if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579" 639 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C" 640 "7086A272C24088BE94769FD16650")) ABORT; 641 if (0 != BN_cmp(y, z)) 642 ABORT; 643 644 fprintf(stdout, "verify degree ..."); 645 if (EC_GROUP_get_degree(group) != 521) 646 ABORT; 647 fprintf(stdout, " ok\n"); 648 649 group_order_tests(group); 650 651 if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) 652 ABORT; 653 if (!EC_GROUP_copy(P_521, group)) 654 ABORT; 655 656 657 /* more tests using the last curve */ 658 fprintf(stdout, "infinity tests ..."); 659 fflush(stdout); 660 if (!EC_POINT_copy(Q, P)) 661 ABORT; 662 if (EC_POINT_is_at_infinity(group, Q)) 663 ABORT; 664 /* P := 2P */ 665 if (!EC_POINT_dbl(group, P, P, ctx)) 666 ABORT; 667 if (!EC_POINT_is_on_curve(group, P, ctx)) 668 ABORT; 669 /* Q := -P */ 670 if (!EC_POINT_invert(group, Q, ctx)) 671 ABORT; 672 /* R := 2P - P = P */ 673 if (!EC_POINT_add(group, R, P, Q, ctx)) 674 ABORT; 675 /* R := R + Q = P - P = infty */ 676 if (!EC_POINT_add(group, R, R, Q, ctx)) 677 ABORT; 678 if (!EC_POINT_is_at_infinity(group, R)) 679 ABORT; 680 fprintf(stdout, " ok\n\n"); 681 682 if (ctx) 683 BN_CTX_free(ctx); 684 BN_free(p); 685 BN_free(a); 686 BN_free(b); 687 EC_GROUP_free(group); 688 EC_POINT_free(P); 689 EC_POINT_free(Q); 690 EC_POINT_free(R); 691 BN_free(x); 692 BN_free(y); 693 BN_free(z); 694 695 if (P_160) 696 EC_GROUP_free(P_160); 697 if (P_192) 698 EC_GROUP_free(P_192); 699 if (P_224) 700 EC_GROUP_free(P_224); 701 if (P_256) 702 EC_GROUP_free(P_256); 703 if (P_384) 704 EC_GROUP_free(P_384); 705 if (P_521) 706 EC_GROUP_free(P_521); 707 708 } 709 710 static void 711 internal_curve_test(void) 712 { 713 EC_builtin_curve *curves = NULL; 714 size_t crv_len = 0, n = 0; 715 int ok = 1; 716 717 crv_len = EC_get_builtin_curves(NULL, 0); 718 719 curves = reallocarray(NULL, sizeof(EC_builtin_curve), crv_len); 720 721 if (curves == NULL) 722 return; 723 724 if (!EC_get_builtin_curves(curves, crv_len)) { 725 free(curves); 726 return; 727 } 728 729 fprintf(stdout, "testing internal curves: "); 730 731 for (n = 0; n < crv_len; n++) { 732 EC_GROUP *group = NULL; 733 int nid = curves[n].nid; 734 if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL) { 735 ok = 0; 736 fprintf(stdout, "\nEC_GROUP_new_curve_name() failed with" 737 " curve %s\n", OBJ_nid2sn(nid)); 738 /* try next curve */ 739 continue; 740 } 741 if (!EC_GROUP_check(group, NULL)) { 742 ok = 0; 743 fprintf(stdout, "\nEC_GROUP_check() failed with" 744 " curve %s\n", OBJ_nid2sn(nid)); 745 EC_GROUP_free(group); 746 /* try the next curve */ 747 continue; 748 } 749 fprintf(stdout, "."); 750 fflush(stdout); 751 EC_GROUP_free(group); 752 } 753 if (ok) 754 fprintf(stdout, " ok\n\n"); 755 else { 756 fprintf(stdout, " failed\n\n"); 757 ABORT; 758 } 759 free(curves); 760 return; 761 } 762 763 int 764 main(int argc, char *argv[]) 765 { 766 ERR_load_crypto_strings(); 767 768 prime_field_tests(); 769 puts(""); 770 /* test the internal curves */ 771 internal_curve_test(); 772 773 #ifndef OPENSSL_NO_ENGINE 774 ENGINE_cleanup(); 775 #endif 776 CRYPTO_cleanup_all_ex_data(); 777 ERR_free_strings(); 778 ERR_remove_thread_state(NULL); 779 CRYPTO_mem_leaks_fp(stderr); 780 781 return 0; 782 } 783