summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ectest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/ec/ectest.c')
-rw-r--r--src/lib/libcrypto/ec/ectest.c341
1 files changed, 93 insertions, 248 deletions
diff --git a/src/lib/libcrypto/ec/ectest.c b/src/lib/libcrypto/ec/ectest.c
index f107782de0..7509cb9c7c 100644
--- a/src/lib/libcrypto/ec/ectest.c
+++ b/src/lib/libcrypto/ec/ectest.c
@@ -94,7 +94,6 @@ 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>
98 97
99#if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12) 98#if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
100/* suppress "too big too optimize" warning */ 99/* suppress "too big too optimize" warning */
@@ -108,6 +107,10 @@ int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); retur
108 EXIT(1); \ 107 EXIT(1); \
109} while (0) 108} while (0)
110 109
110void prime_field_tests(void);
111void char2_field_tests(void);
112void internal_curve_test(void);
113
111#define TIMING_BASE_PT 0 114#define TIMING_BASE_PT 0
112#define TIMING_RAND_PT 1 115#define TIMING_RAND_PT 1
113#define TIMING_SIMUL 2 116#define TIMING_SIMUL 2
@@ -192,50 +195,7 @@ static void timings(EC_GROUP *group, int type, BN_CTX *ctx)
192 } 195 }
193#endif 196#endif
194 197
195/* test multiplication with group order, long and negative scalars */ 198void prime_field_tests()
196static 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
238static void prime_field_tests(void)
239 { 199 {
240 BN_CTX *ctx = NULL; 200 BN_CTX *ctx = NULL;
241 BIGNUM *p, *a, *b; 201 BIGNUM *p, *a, *b;
@@ -361,21 +321,21 @@ static void prime_field_tests(void)
361 if (len == 0) ABORT; 321 if (len == 0) ABORT;
362 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; 322 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
363 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; 323 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
364 fprintf(stdout, "Generator as octet string, compressed form:\n "); 324 fprintf(stdout, "Generator as octect string, compressed form:\n ");
365 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); 325 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
366 326
367 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx); 327 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
368 if (len == 0) ABORT; 328 if (len == 0) ABORT;
369 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; 329 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
370 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; 330 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
371 fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n "); 331 fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n ");
372 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); 332 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
373 333
374 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx); 334 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
375 if (len == 0) ABORT; 335 if (len == 0) ABORT;
376 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; 336 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
377 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; 337 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
378 fprintf(stdout, "\nGenerator as octet string, hybrid form:\n "); 338 fprintf(stdout, "\nGenerator as octect string, hybrid form:\n ");
379 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); 339 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
380 340
381 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT; 341 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
@@ -421,7 +381,17 @@ static void prime_field_tests(void)
421 if (EC_GROUP_get_degree(group) != 160) ABORT; 381 if (EC_GROUP_get_degree(group) != 160) ABORT;
422 fprintf(stdout, " ok\n"); 382 fprintf(stdout, " ok\n");
423 383
424 group_order_tests(group); 384 fprintf(stdout, "verify group order ...");
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");
425 395
426 if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; 396 if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
427 if (!EC_GROUP_copy(P_160, group)) ABORT; 397 if (!EC_GROUP_copy(P_160, group)) ABORT;
@@ -455,7 +425,17 @@ static void prime_field_tests(void)
455 if (EC_GROUP_get_degree(group) != 192) ABORT; 425 if (EC_GROUP_get_degree(group) != 192) ABORT;
456 fprintf(stdout, " ok\n"); 426 fprintf(stdout, " ok\n");
457 427
458 group_order_tests(group); 428 fprintf(stdout, "verify group order ...");
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");
459 439
460 if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; 440 if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
461 if (!EC_GROUP_copy(P_192, group)) ABORT; 441 if (!EC_GROUP_copy(P_192, group)) ABORT;
@@ -489,7 +469,17 @@ static void prime_field_tests(void)
489 if (EC_GROUP_get_degree(group) != 224) ABORT; 469 if (EC_GROUP_get_degree(group) != 224) ABORT;
490 fprintf(stdout, " ok\n"); 470 fprintf(stdout, " ok\n");
491 471
492 group_order_tests(group); 472 fprintf(stdout, "verify group order ...");
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");
493 483
494 if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; 484 if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
495 if (!EC_GROUP_copy(P_224, group)) ABORT; 485 if (!EC_GROUP_copy(P_224, group)) ABORT;
@@ -524,7 +514,17 @@ static void prime_field_tests(void)
524 if (EC_GROUP_get_degree(group) != 256) ABORT; 514 if (EC_GROUP_get_degree(group) != 256) ABORT;
525 fprintf(stdout, " ok\n"); 515 fprintf(stdout, " ok\n");
526 516
527 group_order_tests(group); 517 fprintf(stdout, "verify group order ...");
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,8 +563,18 @@ static void prime_field_tests(void)
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 group_order_tests(group); 567 fprintf(stdout, "verify group order ...");
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");
568 578
569 if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; 579 if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
570 if (!EC_GROUP_copy(P_384, group)) ABORT; 580 if (!EC_GROUP_copy(P_384, group)) ABORT;
@@ -609,8 +619,18 @@ static void prime_field_tests(void)
609 fprintf(stdout, "verify degree ..."); 619 fprintf(stdout, "verify degree ...");
610 if (EC_GROUP_get_degree(group) != 521) ABORT; 620 if (EC_GROUP_get_degree(group) != 521) ABORT;
611 fprintf(stdout, " ok\n"); 621 fprintf(stdout, " ok\n");
612 622
613 group_order_tests(group); 623 fprintf(stdout, "verify group order ...");
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");
614 634
615 if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; 635 if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
616 if (!EC_GROUP_copy(P_521, group)) ABORT; 636 if (!EC_GROUP_copy(P_521, group)) ABORT;
@@ -639,7 +659,6 @@ static void prime_field_tests(void)
639 points[2] = Q; 659 points[2] = Q;
640 points[3] = Q; 660 points[3] = Q;
641 661
642 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
643 if (!BN_add(y, z, BN_value_one())) ABORT; 662 if (!BN_add(y, z, BN_value_one())) ABORT;
644 if (BN_is_odd(y)) ABORT; 663 if (BN_is_odd(y)) ABORT;
645 if (!BN_rshift1(y, y)) ABORT; 664 if (!BN_rshift1(y, y)) ABORT;
@@ -773,14 +792,22 @@ static void prime_field_tests(void)
773 fprintf(stdout, "verify degree ..."); \ 792 fprintf(stdout, "verify degree ..."); \
774 if (EC_GROUP_get_degree(group) != _degree) ABORT; \ 793 if (EC_GROUP_get_degree(group) != _degree) ABORT; \
775 fprintf(stdout, " ok\n"); \ 794 fprintf(stdout, " ok\n"); \
776 group_order_tests(group); \ 795 fprintf(stdout, "verify group order ..."); \
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"); \
777 if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \ 806 if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
778 if (!EC_GROUP_copy(_variable, group)) ABORT; \ 807 if (!EC_GROUP_copy(_variable, group)) ABORT;
779 808
780#ifndef OPENSSL_NO_EC2M 809void char2_field_tests()
781 810 {
782static void char2_field_tests(void)
783 {
784 BN_CTX *ctx = NULL; 811 BN_CTX *ctx = NULL;
785 BIGNUM *p, *a, *b; 812 BIGNUM *p, *a, *b;
786 EC_GROUP *group; 813 EC_GROUP *group;
@@ -1212,9 +1239,8 @@ static void char2_field_tests(void)
1212 if (C2_B571) EC_GROUP_free(C2_B571); 1239 if (C2_B571) EC_GROUP_free(C2_B571);
1213 1240
1214 } 1241 }
1215#endif
1216 1242
1217static void internal_curve_test(void) 1243void internal_curve_test(void)
1218 { 1244 {
1219 EC_builtin_curve *curves = NULL; 1245 EC_builtin_curve *curves = NULL;
1220 size_t crv_len = 0, n = 0; 1246 size_t crv_len = 0, n = 0;
@@ -1261,189 +1287,13 @@ static void internal_curve_test(void)
1261 EC_GROUP_free(group); 1287 EC_GROUP_free(group);
1262 } 1288 }
1263 if (ok) 1289 if (ok)
1264 fprintf(stdout, " ok\n\n"); 1290 fprintf(stdout, " ok\n");
1265 else 1291 else
1266 { 1292 fprintf(stdout, " failed\n");
1267 fprintf(stdout, " failed\n\n");
1268 ABORT;
1269 }
1270 OPENSSL_free(curves); 1293 OPENSSL_free(curves);
1271 return; 1294 return;
1272 } 1295 }
1273 1296
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. */
1277struct 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
1287static 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
1333void 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
1436void 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
1447static const char rnd_seed[] = "string to make the random number generator think it has entropy"; 1297static const char rnd_seed[] = "string to make the random number generator think it has entropy";
1448 1298
1449int main(int argc, char *argv[]) 1299int main(int argc, char *argv[])
@@ -1467,12 +1317,7 @@ int main(int argc, char *argv[])
1467 1317
1468 prime_field_tests(); 1318 prime_field_tests();
1469 puts(""); 1319 puts("");
1470#ifndef OPENSSL_NO_EC2M
1471 char2_field_tests(); 1320 char2_field_tests();
1472#endif
1473#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1474 nistp_tests();
1475#endif
1476 /* test the internal curves */ 1321 /* test the internal curves */
1477 internal_curve_test(); 1322 internal_curve_test();
1478 1323