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