1 /* $OpenBSD: ectest.c,v 1.26 2025/01/06 10:43:26 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 #include <openssl/err.h> 79 #include <openssl/obj_mac.h> 80 #include <openssl/objects.h> 81 #include <openssl/bn.h> 82 #include <openssl/opensslconf.h> 83 84 #define ABORT do { \ 85 fflush(stdout); \ 86 fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \ 87 ERR_print_errors_fp(stderr); \ 88 exit(1); \ 89 } while (0) 90 91 #define TIMING_BASE_PT 0 92 #define TIMING_RAND_PT 1 93 #define TIMING_SIMUL 2 94 95 /* test multiplication with group order, long and negative scalars */ 96 static void 97 group_order_tests(EC_GROUP *group) 98 { 99 BIGNUM *n1, *n2, *order; 100 EC_POINT *P = EC_POINT_new(group); 101 EC_POINT *Q = EC_POINT_new(group); 102 BN_CTX *ctx; 103 104 if ((ctx = BN_CTX_new()) == NULL) 105 ABORT; 106 107 if ((n1 = BN_new()) == NULL) 108 ABORT; 109 if ((n2 = BN_new()) == NULL) 110 ABORT; 111 if ((order = BN_new()) == NULL) 112 ABORT; 113 fprintf(stdout, "verify group order ..."); 114 fflush(stdout); 115 if (!EC_GROUP_get_order(group, order, ctx)) 116 ABORT; 117 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) 118 ABORT; 119 if (!EC_POINT_is_at_infinity(group, Q)) 120 ABORT; 121 fprintf(stdout, "."); 122 fflush(stdout); 123 if (!EC_GROUP_precompute_mult(group, ctx)) 124 ABORT; 125 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) 126 ABORT; 127 if (!EC_POINT_is_at_infinity(group, Q)) 128 ABORT; 129 fprintf(stdout, " ok\n"); 130 fprintf(stdout, "long/negative scalar tests ... "); 131 /* XXX - switch back to BN_one() after next bump. */ 132 if (!BN_set_word(n1, 1)) 133 ABORT; 134 /* n1 = 1 - order */ 135 if (!BN_sub(n1, n1, order)) 136 ABORT; 137 if (!EC_POINT_mul(group, Q, NULL, P, n1, ctx)) 138 ABORT; 139 if (0 != EC_POINT_cmp(group, Q, P, ctx)) 140 ABORT; 141 /* n2 = 1 + order */ 142 if (!BN_add(n2, order, BN_value_one())) 143 ABORT; 144 if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) 145 ABORT; 146 if (0 != EC_POINT_cmp(group, Q, P, ctx)) 147 ABORT; 148 /* n2 = (1 - order) * (1 + order) */ 149 if (!BN_mul(n2, n1, n2, ctx)) 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 fprintf(stdout, "ok\n"); 156 EC_POINT_free(P); 157 EC_POINT_free(Q); 158 BN_free(n1); 159 BN_free(n2); 160 BN_free(order); 161 BN_CTX_free(ctx); 162 } 163 164 static void 165 prime_field_tests(void) 166 { 167 BN_CTX *ctx = NULL; 168 BIGNUM *p, *a, *b; 169 EC_GROUP *group; 170 EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL; 171 EC_POINT *P, *Q, *R; 172 BIGNUM *x, *y, *z; 173 unsigned char buf[100]; 174 size_t i, len; 175 int k; 176 177 ctx = BN_CTX_new(); 178 if (!ctx) 179 ABORT; 180 181 p = BN_new(); 182 a = BN_new(); 183 b = BN_new(); 184 if (!p || !a || !b) 185 ABORT; 186 187 if (!BN_hex2bn(&p, "17")) 188 ABORT; 189 if (!BN_hex2bn(&a, "1")) 190 ABORT; 191 if (!BN_hex2bn(&b, "1")) 192 ABORT; 193 194 group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp 195 * so that the library gets to choose the EC_METHOD */ 196 if (!group) 197 ABORT; 198 199 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 200 ABORT; 201 202 { 203 EC_GROUP *tmp; 204 tmp = EC_GROUP_new(EC_GROUP_method_of(group)); 205 if (!tmp) 206 ABORT; 207 if (!EC_GROUP_copy(tmp, group)) 208 ABORT; 209 EC_GROUP_free(group); 210 group = tmp; 211 } 212 213 if (!EC_GROUP_get_curve(group, p, a, b, ctx)) 214 ABORT; 215 216 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x"); 217 BN_print_fp(stdout, p); 218 fprintf(stdout, ")\n a = 0x"); 219 BN_print_fp(stdout, a); 220 fprintf(stdout, "\n b = 0x"); 221 BN_print_fp(stdout, b); 222 fprintf(stdout, "\n"); 223 224 P = EC_POINT_new(group); 225 Q = EC_POINT_new(group); 226 R = EC_POINT_new(group); 227 if (!P || !Q || !R) 228 ABORT; 229 230 if (!EC_POINT_set_to_infinity(group, P)) 231 ABORT; 232 if (!EC_POINT_is_at_infinity(group, P)) 233 ABORT; 234 235 buf[0] = 0; 236 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) 237 ABORT; 238 239 if (!EC_POINT_add(group, P, P, Q, ctx)) 240 ABORT; 241 if (!EC_POINT_is_at_infinity(group, P)) 242 ABORT; 243 244 x = BN_new(); 245 y = BN_new(); 246 z = BN_new(); 247 if (!x || !y || !z) 248 ABORT; 249 250 if (!BN_hex2bn(&x, "D")) 251 ABORT; 252 if (!EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx)) 253 ABORT; 254 if (EC_POINT_is_on_curve(group, Q, ctx) <= 0) { 255 if (!EC_POINT_get_affine_coordinates(group, Q, x, y, ctx)) 256 ABORT; 257 fprintf(stderr, "Point is not on curve: x = 0x"); 258 BN_print_fp(stderr, x); 259 fprintf(stderr, ", y = 0x"); 260 BN_print_fp(stderr, y); 261 fprintf(stderr, "\n"); 262 ABORT; 263 } 264 265 fprintf(stdout, "A cyclic subgroup:\n"); 266 k = 100; 267 do { 268 if (k-- == 0) 269 ABORT; 270 271 if (EC_POINT_is_at_infinity(group, P)) 272 fprintf(stdout, " point at infinity\n"); 273 else { 274 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 275 ABORT; 276 277 fprintf(stdout, " x = 0x"); 278 BN_print_fp(stdout, x); 279 fprintf(stdout, ", y = 0x"); 280 BN_print_fp(stdout, y); 281 fprintf(stdout, "\n"); 282 } 283 284 if (!EC_POINT_copy(R, P)) 285 ABORT; 286 if (!EC_POINT_add(group, P, P, Q, ctx)) 287 ABORT; 288 } while (!EC_POINT_is_at_infinity(group, P)); 289 290 if (!EC_POINT_add(group, P, Q, R, ctx)) 291 ABORT; 292 if (!EC_POINT_is_at_infinity(group, P)) 293 ABORT; 294 295 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx); 296 if (len == 0) 297 ABORT; 298 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) 299 ABORT; 300 if (0 != EC_POINT_cmp(group, P, Q, ctx)) 301 ABORT; 302 fprintf(stdout, "Generator as octet string, compressed form:\n "); 303 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); 304 305 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx); 306 if (len == 0) 307 ABORT; 308 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) 309 ABORT; 310 if (0 != EC_POINT_cmp(group, P, Q, ctx)) 311 ABORT; 312 fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n "); 313 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); 314 315 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx); 316 if (len == 0) 317 ABORT; 318 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) 319 ABORT; 320 if (0 != EC_POINT_cmp(group, P, Q, ctx)) 321 ABORT; 322 fprintf(stdout, "\nGenerator as octet string, hybrid form:\n "); 323 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); 324 325 if (!EC_POINT_get_affine_coordinates(group, R, x, y, ctx)) 326 ABORT; 327 fprintf(stdout, "\nThe inverse of that generator:\n X = 0x"); 328 BN_print_fp(stdout, x); 329 fprintf(stdout, ", Y = 0x"); 330 BN_print_fp(stdout, y); 331 fprintf(stdout, "\n"); 332 333 if (!EC_POINT_invert(group, P, ctx)) 334 ABORT; 335 if (0 != EC_POINT_cmp(group, P, R, ctx)) 336 ABORT; 337 338 339 /* Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2, 2000) 340 * -- not a NIST curve, but commonly used */ 341 342 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF")) 343 ABORT; 344 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 345 ABORT; 346 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC")) 347 ABORT; 348 if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45")) 349 ABORT; 350 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 351 ABORT; 352 353 if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82")) 354 ABORT; 355 if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32")) 356 ABORT; 357 if (!EC_POINT_set_affine_coordinates(group, P, x, y, ctx)) 358 ABORT; 359 if (EC_POINT_is_on_curve(group, P, ctx) <= 0) 360 ABORT; 361 if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257")) 362 ABORT; 363 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 364 ABORT; 365 366 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 367 ABORT; 368 fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x"); 369 BN_print_fp(stdout, x); 370 fprintf(stdout, "\n y = 0x"); 371 BN_print_fp(stdout, y); 372 fprintf(stdout, "\n"); 373 /* G_y value taken from the standard: */ 374 if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32")) 375 ABORT; 376 if (0 != BN_cmp(y, z)) 377 ABORT; 378 379 fprintf(stdout, "verify degree ..."); 380 if (EC_GROUP_get_degree(group) != 160) 381 ABORT; 382 fprintf(stdout, " ok\n"); 383 384 group_order_tests(group); 385 386 if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) 387 ABORT; 388 if (!EC_GROUP_copy(P_160, group)) 389 ABORT; 390 391 392 /* Curve P-192 (FIPS PUB 186-2, App. 6) */ 393 394 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) 395 ABORT; 396 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 397 ABORT; 398 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) 399 ABORT; 400 if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) 401 ABORT; 402 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 403 ABORT; 404 405 if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) 406 ABORT; 407 if (!EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx)) 408 ABORT; 409 if (EC_POINT_is_on_curve(group, P, ctx) <= 0) 410 ABORT; 411 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) 412 ABORT; 413 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 414 ABORT; 415 416 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 417 ABORT; 418 fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x"); 419 BN_print_fp(stdout, x); 420 fprintf(stdout, "\n y = 0x"); 421 BN_print_fp(stdout, y); 422 fprintf(stdout, "\n"); 423 /* G_y value taken from the standard: */ 424 if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) 425 ABORT; 426 if (0 != BN_cmp(y, z)) 427 ABORT; 428 429 fprintf(stdout, "verify degree ..."); 430 if (EC_GROUP_get_degree(group) != 192) 431 ABORT; 432 fprintf(stdout, " ok\n"); 433 434 group_order_tests(group); 435 436 if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) 437 ABORT; 438 if (!EC_GROUP_copy(P_192, group)) 439 ABORT; 440 441 442 /* Curve P-224 (FIPS PUB 186-2, App. 6) */ 443 444 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) 445 ABORT; 446 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 447 ABORT; 448 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) 449 ABORT; 450 if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) 451 ABORT; 452 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 453 ABORT; 454 455 if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) 456 ABORT; 457 if (!EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx)) 458 ABORT; 459 if (EC_POINT_is_on_curve(group, P, ctx) <= 0) 460 ABORT; 461 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) 462 ABORT; 463 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 464 ABORT; 465 466 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 467 ABORT; 468 fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x"); 469 BN_print_fp(stdout, x); 470 fprintf(stdout, "\n y = 0x"); 471 BN_print_fp(stdout, y); 472 fprintf(stdout, "\n"); 473 /* G_y value taken from the standard: */ 474 if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) 475 ABORT; 476 if (0 != BN_cmp(y, z)) 477 ABORT; 478 479 fprintf(stdout, "verify degree ..."); 480 if (EC_GROUP_get_degree(group) != 224) 481 ABORT; 482 fprintf(stdout, " ok\n"); 483 484 group_order_tests(group); 485 486 if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) 487 ABORT; 488 if (!EC_GROUP_copy(P_224, group)) 489 ABORT; 490 491 492 /* Curve P-256 (FIPS PUB 186-2, App. 6) */ 493 494 if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) 495 ABORT; 496 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 497 ABORT; 498 if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) 499 ABORT; 500 if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) 501 ABORT; 502 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 503 ABORT; 504 505 if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) 506 ABORT; 507 if (!EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx)) 508 ABORT; 509 if (EC_POINT_is_on_curve(group, P, ctx) <= 0) 510 ABORT; 511 if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E" 512 "84F3B9CAC2FC632551")) ABORT; 513 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 514 ABORT; 515 516 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 517 ABORT; 518 fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x"); 519 BN_print_fp(stdout, x); 520 fprintf(stdout, "\n y = 0x"); 521 BN_print_fp(stdout, y); 522 fprintf(stdout, "\n"); 523 /* G_y value taken from the standard: */ 524 if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) 525 ABORT; 526 if (0 != BN_cmp(y, z)) 527 ABORT; 528 529 fprintf(stdout, "verify degree ..."); 530 if (EC_GROUP_get_degree(group) != 256) 531 ABORT; 532 fprintf(stdout, " ok\n"); 533 534 group_order_tests(group); 535 536 if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) 537 ABORT; 538 if (!EC_GROUP_copy(P_256, group)) 539 ABORT; 540 541 542 /* Curve P-384 (FIPS PUB 186-2, App. 6) */ 543 544 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 545 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT; 546 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 547 ABORT; 548 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 549 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT; 550 if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141" 551 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT; 552 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 553 ABORT; 554 555 if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B" 556 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT; 557 if (!EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx)) 558 ABORT; 559 if (EC_POINT_is_on_curve(group, P, ctx) <= 0) 560 ABORT; 561 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 562 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT; 563 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 564 ABORT; 565 566 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 567 ABORT; 568 fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x"); 569 BN_print_fp(stdout, x); 570 fprintf(stdout, "\n y = 0x"); 571 BN_print_fp(stdout, y); 572 fprintf(stdout, "\n"); 573 /* G_y value taken from the standard: */ 574 if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14" 575 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT; 576 if (0 != BN_cmp(y, z)) 577 ABORT; 578 579 fprintf(stdout, "verify degree ..."); 580 if (EC_GROUP_get_degree(group) != 384) 581 ABORT; 582 fprintf(stdout, " ok\n"); 583 584 group_order_tests(group); 585 586 if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) 587 ABORT; 588 if (!EC_GROUP_copy(P_384, group)) 589 ABORT; 590 591 592 /* Curve P-521 (FIPS PUB 186-2, App. 6) */ 593 594 if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 595 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 596 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT; 597 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) 598 ABORT; 599 if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 600 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 601 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT; 602 if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B" 603 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573" 604 "DF883D2C34F1EF451FD46B503F00")) ABORT; 605 if (!EC_GROUP_set_curve(group, p, a, b, ctx)) 606 ABORT; 607 608 if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F" 609 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B" 610 "3C1856A429BF97E7E31C2E5BD66")) ABORT; 611 if (!EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx)) 612 ABORT; 613 if (EC_POINT_is_on_curve(group, P, ctx) <= 0) 614 ABORT; 615 if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" 616 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5" 617 "C9B8899C47AEBB6FB71E91386409")) ABORT; 618 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) 619 ABORT; 620 621 if (!EC_POINT_get_affine_coordinates(group, P, x, y, ctx)) 622 ABORT; 623 fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x"); 624 BN_print_fp(stdout, x); 625 fprintf(stdout, "\n y = 0x"); 626 BN_print_fp(stdout, y); 627 fprintf(stdout, "\n"); 628 /* G_y value taken from the standard: */ 629 if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579" 630 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C" 631 "7086A272C24088BE94769FD16650")) ABORT; 632 if (0 != BN_cmp(y, z)) 633 ABORT; 634 635 fprintf(stdout, "verify degree ..."); 636 if (EC_GROUP_get_degree(group) != 521) 637 ABORT; 638 fprintf(stdout, " ok\n"); 639 640 group_order_tests(group); 641 642 if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) 643 ABORT; 644 if (!EC_GROUP_copy(P_521, group)) 645 ABORT; 646 647 648 /* more tests using the last curve */ 649 fprintf(stdout, "infinity tests ..."); 650 fflush(stdout); 651 if (!EC_POINT_copy(Q, P)) 652 ABORT; 653 if (EC_POINT_is_at_infinity(group, Q)) 654 ABORT; 655 /* P := 2P */ 656 if (!EC_POINT_dbl(group, P, P, ctx)) 657 ABORT; 658 if (EC_POINT_is_on_curve(group, P, ctx) <= 0) 659 ABORT; 660 /* Q := -P */ 661 if (!EC_POINT_invert(group, Q, ctx)) 662 ABORT; 663 /* R := 2P - P = P */ 664 if (!EC_POINT_add(group, R, P, Q, ctx)) 665 ABORT; 666 /* R := R + Q = P - P = infty */ 667 if (!EC_POINT_add(group, R, R, Q, ctx)) 668 ABORT; 669 if (!EC_POINT_is_at_infinity(group, R)) 670 ABORT; 671 fprintf(stdout, " ok\n\n"); 672 673 if (ctx) 674 BN_CTX_free(ctx); 675 BN_free(p); 676 BN_free(a); 677 BN_free(b); 678 EC_GROUP_free(group); 679 EC_POINT_free(P); 680 EC_POINT_free(Q); 681 EC_POINT_free(R); 682 BN_free(x); 683 BN_free(y); 684 BN_free(z); 685 686 if (P_160) 687 EC_GROUP_free(P_160); 688 if (P_192) 689 EC_GROUP_free(P_192); 690 if (P_224) 691 EC_GROUP_free(P_224); 692 if (P_256) 693 EC_GROUP_free(P_256); 694 if (P_384) 695 EC_GROUP_free(P_384); 696 if (P_521) 697 EC_GROUP_free(P_521); 698 699 } 700 701 int 702 main(int argc, char *argv[]) 703 { 704 ERR_load_crypto_strings(); 705 706 prime_field_tests(); 707 708 CRYPTO_cleanup_all_ex_data(); 709 ERR_free_strings(); 710 ERR_remove_thread_state(NULL); 711 712 return 0; 713 } 714