2 * Copyright 2001-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
13 * Portions of the attached software ("Contribution") are developed by
14 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
16 * The Contribution is licensed pursuant to the OpenSSL open source
17 * license provided above.
19 * The elliptic curve binary polynomial software is originally written by
20 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
28 # include <openssl/ec.h>
29 # ifndef OPENSSL_NO_ENGINE
30 # include <openssl/engine.h>
32 # include <openssl/err.h>
33 # include <openssl/obj_mac.h>
34 # include <openssl/objects.h>
35 # include <openssl/rand.h>
36 # include <openssl/bn.h>
37 # include <openssl/opensslconf.h>
39 # if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
40 /* suppress "too big too optimize" warning */
41 # pragma warning(disable:4959)
44 static size_t crv_len
= 0;
45 static EC_builtin_curve
*curves
= NULL
;
47 /* test multiplication with group order, long and negative scalars */
48 static int group_order_tests(EC_GROUP
*group
)
50 BIGNUM
*n1
= NULL
, *n2
= NULL
, *order
= NULL
;
51 EC_POINT
*P
= NULL
, *Q
= NULL
, *R
= NULL
, *S
= NULL
;
55 if (!TEST_ptr(n1
= BN_new())
56 || !TEST_ptr(n2
= BN_new())
57 || !TEST_ptr(order
= BN_new())
58 || !TEST_ptr(ctx
= BN_CTX_new())
59 || !TEST_ptr(P
= EC_POINT_new(group
))
60 || !TEST_ptr(Q
= EC_POINT_new(group
))
61 || !TEST_ptr(R
= EC_POINT_new(group
))
62 || !TEST_ptr(S
= EC_POINT_new(group
)))
65 if (!TEST_true(EC_GROUP_get_order(group
, order
, ctx
))
66 || !TEST_true(EC_POINT_mul(group
, Q
, order
, NULL
, NULL
, ctx
))
67 || !TEST_true(EC_POINT_is_at_infinity(group
, Q
))
68 || !TEST_true(EC_GROUP_precompute_mult(group
, ctx
))
69 || !TEST_true(EC_POINT_mul(group
, Q
, order
, NULL
, NULL
, ctx
))
70 || !TEST_true(EC_POINT_is_at_infinity(group
, Q
)))
73 for (i
= 1; i
<= 2; i
++) {
74 const BIGNUM
*scalars
[6];
75 const EC_POINT
*points
[6];
77 if (!TEST_true(BN_set_word(n1
, i
))
79 * If i == 1, P will be the predefined generator for which
80 * EC_GROUP_precompute_mult has set up precomputation.
82 || !TEST_true(EC_POINT_mul(group
, P
, n1
, NULL
, NULL
, ctx
))
83 || !TEST_true(BN_one(n1
))
85 || !TEST_true(BN_sub(n1
, n1
, order
))
86 || !TEST_true(EC_POINT_mul(group
, Q
, NULL
, P
, n1
, ctx
))
87 || !TEST_int_eq(0, EC_POINT_cmp(group
, Q
, P
, ctx
))
90 || !TEST_true(BN_add(n2
, order
, BN_value_one()))
91 || !TEST_true(EC_POINT_mul(group
, Q
, NULL
, P
, n2
, ctx
))
92 || !TEST_int_eq(0, EC_POINT_cmp(group
, Q
, P
, ctx
))
94 /* n2 = (1 - order) * (1 + order) = 1 - order^2 */
95 || !TEST_true(BN_mul(n2
, n1
, n2
, ctx
))
96 || !TEST_true(EC_POINT_mul(group
, Q
, NULL
, P
, n2
, ctx
))
97 || !TEST_int_eq(0, EC_POINT_cmp(group
, Q
, P
, ctx
)))
100 /* n2 = order^2 - 1 */
101 BN_set_negative(n2
, 0);
102 if (!TEST_true(EC_POINT_mul(group
, Q
, NULL
, P
, n2
, ctx
))
103 /* Add P to verify the result. */
104 || !TEST_true(EC_POINT_add(group
, Q
, Q
, P
, ctx
))
105 || !TEST_true(EC_POINT_is_at_infinity(group
, Q
))
107 /* Exercise EC_POINTs_mul, including corner cases. */
108 || !TEST_false(EC_POINT_is_at_infinity(group
, P
)))
111 scalars
[0] = scalars
[1] = BN_value_one();
112 points
[0] = points
[1] = P
;
114 if (!TEST_true(EC_POINTs_mul(group
, R
, NULL
, 2, points
, scalars
, ctx
))
115 || !TEST_true(EC_POINT_dbl(group
, S
, points
[0], ctx
))
116 || !TEST_int_eq(0, EC_POINT_cmp(group
, R
, S
, ctx
)))
120 points
[0] = Q
; /* => infinity */
122 points
[1] = P
; /* => -P */
124 points
[2] = Q
; /* => infinity */
126 points
[3] = Q
; /* => infinity */
128 points
[4] = P
; /* => P */
130 points
[5] = Q
; /* => infinity */
131 if (!TEST_true(EC_POINTs_mul(group
, P
, NULL
, 6, points
, scalars
, ctx
))
132 || !TEST_true(EC_POINT_is_at_infinity(group
, P
)))
138 if (r
== 0 && i
!= 0)
139 TEST_info(i
== 1 ? "allowing precomputation" :
140 "without precomputation");
152 static int prime_field_tests(void)
155 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
, *scalar3
= NULL
;
156 EC_GROUP
*group
= NULL
, *tmp
= NULL
;
157 EC_GROUP
*P_160
= NULL
, *P_192
= NULL
, *P_224
= NULL
,
158 *P_256
= NULL
, *P_384
= NULL
, *P_521
= NULL
;
159 EC_POINT
*P
= NULL
, *Q
= NULL
, *R
= NULL
;
160 BIGNUM
*x
= NULL
, *y
= NULL
, *z
= NULL
, *yplusone
= NULL
;
161 const EC_POINT
*points
[4];
162 const BIGNUM
*scalars
[4];
163 unsigned char buf
[100];
164 size_t i
, len
, r
= 0;
167 if (!TEST_ptr(ctx
= BN_CTX_new())
168 || !TEST_ptr(p
= BN_new())
169 || !TEST_ptr(a
= BN_new())
170 || !TEST_ptr(b
= BN_new())
171 || !TEST_true(BN_hex2bn(&p
, "17"))
172 || !TEST_true(BN_hex2bn(&a
, "1"))
173 || !TEST_true(BN_hex2bn(&b
, "1"))
175 * applications should use EC_GROUP_new_curve_GFp so
176 * that the library gets to choose the EC_METHOD
178 || !TEST_ptr(group
= EC_GROUP_new(EC_GFp_mont_method()))
179 || !TEST_true(EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
))
180 || !TEST_ptr(tmp
= EC_GROUP_new(EC_GROUP_method_of(group
)))
181 || !TEST_true(EC_GROUP_copy(tmp
, group
)))
183 EC_GROUP_free(group
);
187 if (!TEST_true(EC_GROUP_get_curve_GFp(group
, p
, a
, b
, ctx
)))
191 "Curve defined by Weierstrass equation\n"
192 " y^2 = x^3 + a*x + b (mod 0x");
193 BN_print(bio_out
, p
);
194 BIO_printf(bio_out
, ")\n a = 0x");
195 BN_print(bio_out
, a
);
196 BIO_printf(bio_out
, "\n b = 0x");
197 BN_print(bio_out
, b
);
198 BIO_printf(bio_out
, "\n");
201 if (!TEST_ptr(P
= EC_POINT_new(group
))
202 || !TEST_ptr(Q
= EC_POINT_new(group
))
203 || !TEST_ptr(R
= EC_POINT_new(group
))
204 || !TEST_true(EC_POINT_set_to_infinity(group
, P
))
205 || !TEST_true(EC_POINT_is_at_infinity(group
, P
))
206 || !TEST_true(EC_POINT_oct2point(group
, Q
, buf
, 1, ctx
))
207 || !TEST_true(EC_POINT_add(group
, P
, P
, Q
, ctx
))
208 || !TEST_true(EC_POINT_is_at_infinity(group
, P
))
209 || !TEST_ptr(x
= BN_new())
210 || !TEST_ptr(y
= BN_new())
211 || !TEST_ptr(z
= BN_new())
212 || !TEST_ptr(yplusone
= BN_new())
213 || !TEST_true(BN_hex2bn(&x
, "D"))
214 || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group
, Q
, x
, 1,
218 if (!TEST_int_gt(EC_POINT_is_on_curve(group
, Q
, ctx
), 0)) {
219 if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group
, Q
, x
, y
,
222 BIO_printf(bio_err
, "Point is not on curve: x = 0x");
223 BN_print_fp(stderr
, x
);
224 BIO_printf(bio_err
, ", y = 0x");
225 BN_print_fp(stderr
, y
);
226 BIO_printf(bio_err
, "\n");
230 BIO_printf(bio_out
, "A cyclic subgroup:\n");
233 if (!TEST_int_ne(k
--, 0))
236 if (EC_POINT_is_at_infinity(group
, P
)) {
237 BIO_printf(bio_out
, " point at infinity\n");
239 if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
,
243 BIO_printf(bio_out
, " x = 0x");
244 BN_print(bio_out
, x
);
245 BIO_printf(bio_out
, ", y = 0x");
246 BN_print(bio_out
, y
);
247 BIO_printf(bio_out
, "\n");
250 if (!TEST_true(EC_POINT_copy(R
, P
))
251 || !TEST_true(EC_POINT_add(group
, P
, P
, Q
, ctx
)))
254 } while (!EC_POINT_is_at_infinity(group
, P
));
256 if (!TEST_true(EC_POINT_add(group
, P
, Q
, R
, ctx
))
257 || !TEST_true(EC_POINT_is_at_infinity(group
, P
)))
261 EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_COMPRESSED
, buf
,
263 if (!TEST_size_t_ne(len
, 0)
264 || !TEST_true(EC_POINT_oct2point(group
, P
, buf
, len
, ctx
))
265 || !TEST_int_eq(0, EC_POINT_cmp(group
, P
, Q
, ctx
)))
267 BIO_printf(bio_out
, "Generator as octet string, compressed form:\n ");
268 for (i
= 0; i
< len
; i
++)
269 BIO_printf(bio_out
, "%02X", buf
[i
]);
271 len
= EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_UNCOMPRESSED
,
272 buf
, sizeof buf
, ctx
);
273 if (!TEST_size_t_ne(len
, 0)
274 || !TEST_true(EC_POINT_oct2point(group
, P
, buf
, len
, ctx
))
275 || !TEST_int_eq(0, EC_POINT_cmp(group
, P
, Q
, ctx
)))
277 BIO_printf(bio_out
, "\nGenerator as octet string, uncompressed form:\n"
279 for (i
= 0; i
< len
; i
++)
280 BIO_printf(bio_out
, "%02X", buf
[i
]);
282 len
= EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_HYBRID
,
283 buf
, sizeof buf
, ctx
);
284 if (!TEST_size_t_ne(len
, 0)
285 || !TEST_true(EC_POINT_oct2point(group
, P
, buf
, len
, ctx
))
286 || !TEST_int_eq(0, EC_POINT_cmp(group
, P
, Q
, ctx
)))
288 BIO_printf(bio_out
, "\nGenerator as octet string, hybrid form:\n ");
289 for (i
= 0; i
< len
; i
++)
290 BIO_printf(bio_out
, "%02X", buf
[i
]);
292 if (!TEST_true(EC_POINT_get_Jprojective_coordinates_GFp(group
, R
, x
, y
, z
,
296 "\nA representation of the inverse of that generator in\n"
297 "Jacobian projective coordinates:\n"
299 BN_print(bio_out
, x
);
300 BIO_printf(bio_out
, ", Y = 0x");
301 BN_print(bio_out
, y
);
302 BIO_printf(bio_out
, ", Z = 0x");
303 BN_print(bio_out
, z
);
304 BIO_printf(bio_out
, "\n");
306 if (!TEST_true(EC_POINT_invert(group
, P
, ctx
))
307 || !TEST_int_eq(0, EC_POINT_cmp(group
, P
, R
, ctx
))
310 * Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2,
311 * 2000) -- not a NIST curve, but commonly used
314 || !TEST_true(BN_hex2bn(&p
, "FFFFFFFF"
315 "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))
316 || !TEST_int_eq(1, BN_is_prime_ex(p
, BN_prime_checks
, ctx
, NULL
))
317 || !TEST_true(BN_hex2bn(&a
, "FFFFFFFF"
318 "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))
319 || !TEST_true(BN_hex2bn(&b
, "1C97BEFC"
320 "54BD7A8B65ACF89F81D4D4ADC565FA45"))
321 || !TEST_true(EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
))
322 || !TEST_true(BN_hex2bn(&x
, "4A96B568"
323 "8EF573284664698968C38BB913CBFC82"))
324 || !TEST_true(BN_hex2bn(&y
, "23a62855"
325 "3168947d59dcc912042351377ac5fb32"))
326 || !TEST_true(BN_add(yplusone
, y
, BN_value_one()))
328 * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
329 * and therefore setting the coordinates should fail.
331 || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group
, P
, x
,
333 || !TEST_true(EC_POINT_set_affine_coordinates_GFp(group
, P
, x
, y
, ctx
))
334 || !TEST_int_gt(EC_POINT_is_on_curve(group
, P
, ctx
), 0)
335 || !TEST_true(BN_hex2bn(&z
, "0100000000"
336 "000000000001F4C8F927AED3CA752257"))
337 || !TEST_true(EC_GROUP_set_generator(group
, P
, z
, BN_value_one()))
338 || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)))
340 BIO_printf(bio_out
, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x");
341 BN_print(bio_out
, x
);
342 BIO_printf(bio_out
, "\n y = 0x");
343 BN_print(bio_out
, y
);
344 BIO_printf(bio_out
, "\n");
345 /* G_y value taken from the standard: */
346 if (!TEST_true(BN_hex2bn(&z
, "23a62855"
347 "3168947d59dcc912042351377ac5fb32"))
348 || !TEST_int_eq(0, BN_cmp(y
, z
))
349 || !TEST_int_eq(EC_GROUP_get_degree(group
), 160)
350 || !group_order_tests(group
)
351 || !TEST_ptr(P_160
= EC_GROUP_new(EC_GROUP_method_of(group
)))
352 || !TEST_true(EC_GROUP_copy(P_160
, group
))
354 /* Curve P-192 (FIPS PUB 186-2, App. 6) */
356 || !TEST_true(BN_hex2bn(&p
, "FFFFFFFFFFFFFFFF"
357 "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))
358 || !TEST_int_eq(1, BN_is_prime_ex(p
, BN_prime_checks
, ctx
, NULL
))
359 || !TEST_true(BN_hex2bn(&a
, "FFFFFFFFFFFFFFFF"
360 "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))
361 || !TEST_true(BN_hex2bn(&b
, "64210519E59C80E7"
362 "0FA7E9AB72243049FEB8DEECC146B9B1"))
363 || !TEST_true(EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
))
364 || !TEST_true(BN_hex2bn(&x
, "188DA80EB03090F6"
365 "7CBF20EB43A18800F4FF0AFD82FF1012"))
366 || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group
, P
, x
, 1,
368 || !TEST_int_gt(EC_POINT_is_on_curve(group
, P
, ctx
), 0)
369 || !TEST_true(BN_hex2bn(&z
, "FFFFFFFFFFFFFFFF"
370 "FFFFFFFF99DEF836146BC9B1B4D22831"))
371 || !TEST_true(EC_GROUP_set_generator(group
, P
, z
, BN_value_one()))
372 || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)))
375 BIO_printf(bio_out
, "\nNIST curve P-192 -- Generator:\n x = 0x");
376 BN_print(bio_out
, x
);
377 BIO_printf(bio_out
, "\n y = 0x");
378 BN_print(bio_out
, y
);
379 BIO_printf(bio_out
, "\n");
380 /* G_y value taken from the standard: */
381 if (!TEST_true(BN_hex2bn(&z
, "07192B95FFC8DA78"
382 "631011ED6B24CDD573F977A11E794811"))
383 || !TEST_int_eq(0, BN_cmp(y
, z
))
384 || !TEST_true(BN_add(yplusone
, y
, BN_value_one()))
386 * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
387 * and therefore setting the coordinates should fail.
389 || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group
, P
, x
,
391 || !TEST_int_eq(EC_GROUP_get_degree(group
), 192)
392 || !group_order_tests(group
)
393 || !TEST_ptr(P_192
= EC_GROUP_new(EC_GROUP_method_of(group
)))
394 || !TEST_true(EC_GROUP_copy(P_192
, group
))
396 /* Curve P-224 (FIPS PUB 186-2, App. 6) */
398 || !TEST_true(BN_hex2bn(&p
, "FFFFFFFFFFFFFFFFFFFFFFFF"
399 "FFFFFFFF000000000000000000000001"))
400 || !TEST_int_eq(1, BN_is_prime_ex(p
, BN_prime_checks
, ctx
, NULL
))
401 || !TEST_true(BN_hex2bn(&a
, "FFFFFFFFFFFFFFFFFFFFFFFF"
402 "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))
403 || !TEST_true(BN_hex2bn(&b
, "B4050A850C04B3ABF5413256"
404 "5044B0B7D7BFD8BA270B39432355FFB4"))
405 || !TEST_true(EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
))
406 || !TEST_true(BN_hex2bn(&x
, "B70E0CBD6BB4BF7F321390B9"
407 "4A03C1D356C21122343280D6115C1D21"))
408 || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group
, P
, x
, 0,
410 || !TEST_int_gt(EC_POINT_is_on_curve(group
, P
, ctx
), 0)
411 || !TEST_true(BN_hex2bn(&z
, "FFFFFFFFFFFFFFFFFFFFFFFF"
412 "FFFF16A2E0B8F03E13DD29455C5C2A3D"))
413 || !TEST_true(EC_GROUP_set_generator(group
, P
, z
, BN_value_one()))
414 || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)))
417 BIO_printf(bio_out
, "\nNIST curve P-224 -- Generator:\n x = 0x");
418 BN_print(bio_out
, x
);
419 BIO_printf(bio_out
, "\n y = 0x");
420 BN_print(bio_out
, y
);
421 BIO_printf(bio_out
, "\n");
422 /* G_y value taken from the standard: */
423 if (!TEST_true(BN_hex2bn(&z
, "BD376388B5F723FB4C22DFE6"
424 "CD4375A05A07476444D5819985007E34"))
425 || !TEST_int_eq(0, BN_cmp(y
, z
))
426 || !TEST_true(BN_add(yplusone
, y
, BN_value_one()))
428 * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
429 * and therefore setting the coordinates should fail.
431 || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group
, P
, x
,
433 || !TEST_int_eq(EC_GROUP_get_degree(group
), 224)
434 || !group_order_tests(group
)
435 || !TEST_ptr(P_224
= EC_GROUP_new(EC_GROUP_method_of(group
)))
436 || !TEST_true(EC_GROUP_copy(P_224
, group
))
438 /* Curve P-256 (FIPS PUB 186-2, App. 6) */
440 || !TEST_true(BN_hex2bn(&p
, "FFFFFFFF000000010000000000000000"
441 "00000000FFFFFFFFFFFFFFFFFFFFFFFF"))
442 || !TEST_int_eq(1, BN_is_prime_ex(p
, BN_prime_checks
, ctx
, NULL
))
443 || !TEST_true(BN_hex2bn(&a
, "FFFFFFFF000000010000000000000000"
444 "00000000FFFFFFFFFFFFFFFFFFFFFFFC"))
445 || !TEST_true(BN_hex2bn(&b
, "5AC635D8AA3A93E7B3EBBD55769886BC"
446 "651D06B0CC53B0F63BCE3C3E27D2604B"))
447 || !TEST_true(EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
))
449 || !TEST_true(BN_hex2bn(&x
, "6B17D1F2E12C4247F8BCE6E563A440F2"
450 "77037D812DEB33A0F4A13945D898C296"))
451 || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group
, P
, x
, 1,
453 || !TEST_int_gt(EC_POINT_is_on_curve(group
, P
, ctx
), 0)
454 || !TEST_true(BN_hex2bn(&z
, "FFFFFFFF00000000FFFFFFFFFFFFFFFF"
455 "BCE6FAADA7179E84F3B9CAC2FC632551"))
456 || !TEST_true(EC_GROUP_set_generator(group
, P
, z
, BN_value_one()))
457 || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)))
460 BIO_printf(bio_out
, "\nNIST curve P-256 -- Generator:\n x = 0x");
461 BN_print(bio_out
, x
);
462 BIO_printf(bio_out
, "\n y = 0x");
463 BN_print(bio_out
, y
);
464 BIO_printf(bio_out
, "\n");
465 /* G_y value taken from the standard: */
466 if (!TEST_true(BN_hex2bn(&z
, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
467 "2BCE33576B315ECECBB6406837BF51F5"))
468 || !TEST_int_eq(0, BN_cmp(y
, z
))
469 || !TEST_true(BN_add(yplusone
, y
, BN_value_one()))
471 * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
472 * and therefore setting the coordinates should fail.
474 || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group
, P
, x
,
476 || !TEST_int_eq(EC_GROUP_get_degree(group
), 256)
477 || !group_order_tests(group
)
478 || !TEST_ptr(P_256
= EC_GROUP_new(EC_GROUP_method_of(group
)))
479 || !TEST_true(EC_GROUP_copy(P_256
, group
))
481 /* Curve P-384 (FIPS PUB 186-2, App. 6) */
483 || !TEST_true(BN_hex2bn(&p
, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
484 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
485 "FFFFFFFF0000000000000000FFFFFFFF"))
486 || !TEST_int_eq(1, BN_is_prime_ex(p
, BN_prime_checks
, ctx
, NULL
))
487 || !TEST_true(BN_hex2bn(&a
, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
488 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
489 "FFFFFFFF0000000000000000FFFFFFFC"))
490 || !TEST_true(BN_hex2bn(&b
, "B3312FA7E23EE7E4988E056BE3F82D19"
491 "181D9C6EFE8141120314088F5013875A"
492 "C656398D8A2ED19D2A85C8EDD3EC2AEF"))
493 || !TEST_true(EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
))
495 || !TEST_true(BN_hex2bn(&x
, "AA87CA22BE8B05378EB1C71EF320AD74"
496 "6E1D3B628BA79B9859F741E082542A38"
497 "5502F25DBF55296C3A545E3872760AB7"))
498 || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group
, P
, x
, 1,
500 || !TEST_int_gt(EC_POINT_is_on_curve(group
, P
, ctx
), 0)
501 || !TEST_true(BN_hex2bn(&z
, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
502 "FFFFFFFFFFFFFFFFC7634D81F4372DDF"
503 "581A0DB248B0A77AECEC196ACCC52973"))
504 || !TEST_true(EC_GROUP_set_generator(group
, P
, z
, BN_value_one()))
505 || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)))
508 BIO_printf(bio_out
, "\nNIST curve P-384 -- Generator:\n x = 0x");
509 BN_print(bio_out
, x
);
510 BIO_printf(bio_out
, "\n y = 0x");
511 BN_print(bio_out
, y
);
512 BIO_printf(bio_out
, "\n");
513 /* G_y value taken from the standard: */
514 if (!TEST_true(BN_hex2bn(&z
, "3617DE4A96262C6F5D9E98BF9292DC29"
515 "F8F41DBD289A147CE9DA3113B5F0B8C0"
516 "0A60B1CE1D7E819D7A431D7C90EA0E5F"))
517 || !TEST_int_eq(0, BN_cmp(y
, z
))
518 || !TEST_true(BN_add(yplusone
, y
, BN_value_one()))
520 * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
521 * and therefore setting the coordinates should fail.
523 || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group
, P
, x
,
525 || !TEST_int_eq(EC_GROUP_get_degree(group
), 384)
526 || !group_order_tests(group
)
527 || !TEST_ptr(P_384
= EC_GROUP_new(EC_GROUP_method_of(group
)))
528 || !TEST_true(EC_GROUP_copy(P_384
, group
))
530 /* Curve P-521 (FIPS PUB 186-2, App. 6) */
531 || !TEST_true(BN_hex2bn(&p
, "1FF"
532 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
533 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
534 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
535 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
536 || !TEST_int_eq(1, BN_is_prime_ex(p
, BN_prime_checks
, ctx
, NULL
))
537 || !TEST_true(BN_hex2bn(&a
, "1FF"
538 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
539 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
540 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
541 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC"))
542 || !TEST_true(BN_hex2bn(&b
, "051"
543 "953EB9618E1C9A1F929A21A0B68540EE"
544 "A2DA725B99B315F3B8B489918EF109E1"
545 "56193951EC7E937B1652C0BD3BB1BF07"
546 "3573DF883D2C34F1EF451FD46B503F00"))
547 || !TEST_true(EC_GROUP_set_curve_GFp(group
, p
, a
, b
, ctx
))
548 || !TEST_true(BN_hex2bn(&x
, "C6"
549 "858E06B70404E9CD9E3ECB662395B442"
550 "9C648139053FB521F828AF606B4D3DBA"
551 "A14B5E77EFE75928FE1DC127A2FFA8DE"
552 "3348B3C1856A429BF97E7E31C2E5BD66"))
553 || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group
, P
, x
, 0,
555 || !TEST_int_gt(EC_POINT_is_on_curve(group
, P
, ctx
), 0)
556 || !TEST_true(BN_hex2bn(&z
, "1FF"
557 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
558 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA"
559 "51868783BF2F966B7FCC0148F709A5D0"
560 "3BB5C9B8899C47AEBB6FB71E91386409"))
561 || !TEST_true(EC_GROUP_set_generator(group
, P
, z
, BN_value_one()))
562 || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group
, P
, x
, y
, ctx
)))
565 BIO_printf(bio_out
, "\nNIST curve P-521 -- Generator:\n x = 0x");
566 BN_print(bio_out
, x
);
567 BIO_printf(bio_out
, "\n y = 0x");
568 BN_print(bio_out
, y
);
569 BIO_printf(bio_out
, "\n");
570 /* G_y value taken from the standard: */
571 if (!TEST_true(BN_hex2bn(&z
, "118"
572 "39296A789A3BC0045C8A5FB42C7D1BD9"
573 "98F54449579B446817AFBD17273E662C"
574 "97EE72995EF42640C550B9013FAD0761"
575 "353C7086A272C24088BE94769FD16650"))
576 || !TEST_int_eq(0, BN_cmp(y
, z
))
577 || !TEST_true(BN_add(yplusone
, y
, BN_value_one()))
579 * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
580 * and therefore setting the coordinates should fail.
582 || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group
, P
, x
,
584 || !TEST_int_eq(EC_GROUP_get_degree(group
), 521)
585 || !group_order_tests(group
)
586 || !TEST_ptr(P_521
= EC_GROUP_new(EC_GROUP_method_of(group
)))
587 || !TEST_true(EC_GROUP_copy(P_521
, group
))
589 /* more tests using the last curve */
591 /* Restore the point that got mangled in the (x, y + 1) test. */
592 || !TEST_true(EC_POINT_set_affine_coordinates_GFp(group
, P
, x
, y
, ctx
))
593 || !TEST_true(EC_POINT_copy(Q
, P
))
594 || !TEST_false(EC_POINT_is_at_infinity(group
, Q
))
595 || !TEST_true(EC_POINT_dbl(group
, P
, P
, ctx
))
596 || !TEST_int_gt(EC_POINT_is_on_curve(group
, P
, ctx
), 0)
597 || !TEST_true(EC_POINT_invert(group
, Q
, ctx
)) /* P = -2Q */
598 || !TEST_true(EC_POINT_add(group
, R
, P
, Q
, ctx
))
599 || !TEST_true(EC_POINT_add(group
, R
, R
, Q
, ctx
))
600 || !TEST_true(EC_POINT_is_at_infinity(group
, R
)) /* R = P + 2Q */
601 || !TEST_false(EC_POINT_is_at_infinity(group
, Q
)))
608 if (!TEST_true(EC_GROUP_get_order(group
, z
, ctx
))
609 || !TEST_true(BN_add(y
, z
, BN_value_one()))
610 || !TEST_false(BN_is_odd(y
))
611 || !TEST_true(BN_rshift1(y
, y
)))
613 scalars
[0] = y
; /* (group order + 1)/2, so y*Q + y*Q = Q */
616 BIO_printf(bio_out
, "combined multiplication ...");
618 /* z is still the group order */
619 if (!TEST_true(EC_POINTs_mul(group
, P
, NULL
, 2, points
, scalars
, ctx
))
620 || !TEST_true(EC_POINTs_mul(group
, R
, z
, 2, points
, scalars
, ctx
))
621 || !TEST_int_eq(0, EC_POINT_cmp(group
, P
, R
, ctx
))
622 || !TEST_int_eq(0, EC_POINT_cmp(group
, R
, Q
, ctx
))
623 || !TEST_true(BN_pseudo_rand(y
, BN_num_bits(y
), 0, 0))
624 || !TEST_true(BN_add(z
, z
, y
)))
626 BN_set_negative(z
, 1);
628 scalars
[1] = z
; /* z = -(order + y) */
630 if (!TEST_true(EC_POINTs_mul(group
, P
, NULL
, 2, points
, scalars
, ctx
))
631 || !TEST_true(EC_POINT_is_at_infinity(group
, P
))
632 || !TEST_true(BN_pseudo_rand(x
, BN_num_bits(y
) - 1, 0, 0))
633 || !TEST_true(BN_add(z
, x
, y
)))
635 BN_set_negative(z
, 1);
638 scalars
[2] = z
; /* z = -(x+y) */
640 if (!TEST_ptr(scalar3
= BN_new()))
643 scalars
[3] = scalar3
;
645 if (!TEST_true(EC_POINTs_mul(group
, P
, NULL
, 4, points
, scalars
, ctx
))
646 || !TEST_true(EC_POINT_is_at_infinity(group
, P
)))
649 BIO_printf(bio_out
, " ok\n\n");
658 EC_GROUP_free(group
);
669 EC_GROUP_free(P_160
);
670 EC_GROUP_free(P_192
);
671 EC_GROUP_free(P_224
);
672 EC_GROUP_free(P_256
);
673 EC_GROUP_free(P_384
);
674 EC_GROUP_free(P_521
);
678 # ifndef OPENSSL_NO_EC2M
680 static struct c2_curve_test
{
691 } char2_curve_tests
[] = {
692 /* Curve K-163 (FIPS PUB 186-2, App. 6) */
695 "0800000000000000000000000000000000000000C9",
698 "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
699 "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
700 1, "04000000000000000000020108A2E0CC0D99F8A5EF", "2", 163
702 /* Curve B-163 (FIPS PUB 186-2, App. 6) */
705 "0800000000000000000000000000000000000000C9",
707 "020A601907B8C953CA1481EB10512F78744A3205FD",
708 "03F0EBA16286A2D57EA0991168D4994637E8343E36",
709 "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
710 1, "040000000000000000000292FE77E70C12A4234C33", "2", 163
712 /* Curve K-233 (FIPS PUB 186-2, App. 6) */
715 "020000000000000000000000000000000000000004000000000000000001",
718 "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
719 "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
721 "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
724 /* Curve B-233 (FIPS PUB 186-2, App. 6) */
727 "020000000000000000000000000000000000000004000000000000000001",
728 "000000000000000000000000000000000000000000000000000000000001",
729 "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
730 "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
731 "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
733 "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
736 /* Curve K-283 (FIPS PUB 186-2, App. 6) */
740 "00000000000000000000000000000000000000000000000000000000000010A1",
744 "78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
746 "0F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
749 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
752 /* Curve B-283 (FIPS PUB 186-2, App. 6) */
756 "00000000000000000000000000000000000000000000000000000000000010A1",
758 "0000000000000000000000000000000000000000000000000000000000000001",
760 "C8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
762 "8DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
764 "FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
767 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
770 /* Curve K-409 (FIPS PUB 186-2, App. 6) */
773 "0200000000000000000000000000000000000000"
774 "0000000000000000000000000000000000000000008000000000000000000001",
777 "0060F05F658F49C1AD3AB1890F7184210EFD0987"
778 "E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
779 "01E369050B7C4E42ACBA1DACBF04299C3460782F"
780 "918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
782 "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
783 "FFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
786 /* Curve B-409 (FIPS PUB 186-2, App. 6) */
789 "0200000000000000000000000000000000000000"
790 "0000000000000000000000000000000000000000008000000000000000000001",
791 "0000000000000000000000000000000000000000"
792 "0000000000000000000000000000000000000000000000000000000000000001",
793 "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422E"
794 "F1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
795 "015D4860D088DDB3496B0C6064756260441CDE4A"
796 "F1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
797 "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5"
798 "A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
800 "0100000000000000000000000000000000000000"
801 "00000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
804 /* Curve K-571 (FIPS PUB 186-2, App. 6) */
808 "0000000000000000000000000000000000000000000000000000000000000000"
809 "0000000000000000000000000000000000000000000000000000000000000425",
813 "82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E6"
814 "47DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
816 "4F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA7"
817 "4FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
820 "00000000000000000000000000000000000000000000000000000000131850E1"
821 "F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
824 /* Curve B-571 (FIPS PUB 186-2, App. 6) */
828 "0000000000000000000000000000000000000000000000000000000000000000"
829 "0000000000000000000000000000000000000000000000000000000000000425",
831 "0000000000000000000000000000000000000000000000000000000000000000"
832 "0000000000000000000000000000000000000000000000000000000000000001",
834 "DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA5933"
835 "2BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
837 "6C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293"
838 "CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
840 "6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A57"
841 "6291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
844 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18"
845 "FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
850 static int char2_curve_test(int n
)
854 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
;
855 BIGNUM
*x
= NULL
, *y
= NULL
, *z
= NULL
, *cof
= NULL
, *yplusone
= NULL
;
856 EC_GROUP
*group
= NULL
, *variable
= NULL
;
857 EC_POINT
*P
= NULL
, *Q
= NULL
, *R
= NULL
;
858 const EC_POINT
*points
[3];
859 const BIGNUM
*scalars
[3];
860 struct c2_curve_test
*const test
= char2_curve_tests
+ n
;
862 if (!TEST_ptr(ctx
= BN_CTX_new())
863 || !TEST_ptr(p
= BN_new())
864 || !TEST_ptr(a
= BN_new())
865 || !TEST_ptr(b
= BN_new())
866 || !TEST_ptr(x
= BN_new())
867 || !TEST_ptr(y
= BN_new())
868 || !TEST_ptr(z
= BN_new())
869 || !TEST_ptr(yplusone
= BN_new())
870 || !TEST_true(BN_hex2bn(&p
, test
->p
))
871 || !TEST_true(BN_hex2bn(&a
, test
->a
))
872 || !TEST_true(BN_hex2bn(&b
, test
->b
))
873 || !TEST_true(group
= EC_GROUP_new(EC_GF2m_simple_method()))
874 || !TEST_true(EC_GROUP_set_curve_GF2m(group
, p
, a
, b
, ctx
))
875 || !TEST_ptr(P
= EC_POINT_new(group
))
876 || !TEST_ptr(Q
= EC_POINT_new(group
))
877 || !TEST_ptr(R
= EC_POINT_new(group
))
878 || !TEST_true(BN_hex2bn(&x
, test
->x
))
879 || !TEST_true(BN_hex2bn(&y
, test
->y
))
880 || !TEST_true(BN_add(yplusone
, y
, BN_value_one())))
883 /* Change test based on whether binary point compression is enabled or not. */
884 # ifdef OPENSSL_EC_BIN_PT_COMP
886 * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
887 * and therefore setting the coordinates should fail.
889 if (!TEST_false(EC_POINT_set_affine_coordinates_GF2m(group
, P
, x
, yplusone
,
891 || !TEST_true(EC_POINT_set_compressed_coordinates_GF2m(group
, P
, x
,
894 || !TEST_int_gt(EC_POINT_is_on_curve(group
, P
, ctx
), 0)
895 || !TEST_true(BN_hex2bn(&z
, test
->order
))
896 || !TEST_true(BN_hex2bn(&cof
, test
->cof
))
897 || !TEST_true(EC_GROUP_set_generator(group
, P
, z
, cof
))
898 || !TEST_true(EC_POINT_get_affine_coordinates_GF2m(group
, P
, x
, y
,
901 BIO_printf(bio_out
, "\n%s -- Generator:\n x = 0x", test
->name
);
902 BN_print(bio_out
, x
);
903 BIO_printf(bio_out
, "\n y = 0x");
904 BN_print(bio_out
, y
);
905 BIO_printf(bio_out
, "\n");
906 /* G_y value taken from the standard: */
907 if (!TEST_true(BN_hex2bn(&z
, test
->y
))
908 || !TEST_int_eq(0, BN_cmp(y
, z
)))
912 * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
913 * and therefore setting the coordinates should fail.
915 if (!TEST_false(EC_POINT_set_affine_coordinates_GF2m(group
, P
, x
, yplusone
,
917 || !TEST_true(EC_POINT_set_affine_coordinates_GF2m(group
, P
, x
, y
, ctx
))
918 || !TEST_int_gt(EC_POINT_is_on_curve(group
, P
, ctx
), 0)
919 || !TEST_true(BN_hex2bn(&z
, test
->order
))
920 || !TEST_true(BN_hex2bn(&cof
, test
->cof
))
921 || !TEST_true(EC_GROUP_set_generator(group
, P
, z
, cof
)))
923 BIO_printf(bio_out
, "\n%s -- Generator:\n x = 0x", test
->name
); \
924 BN_print(bio_out
, x
); \
925 BIO_printf(bio_out
, "\n y = 0x"); \
926 BN_print(bio_out
, y
); \
927 BIO_printf(bio_out
, "\n");
930 if (!TEST_int_eq(EC_GROUP_get_degree(group
), test
->degree
)
931 || !group_order_tests(group
)
932 || !TEST_ptr(variable
= EC_GROUP_new(EC_GROUP_method_of(group
)))
933 || !TEST_true(EC_GROUP_copy(variable
, group
)))
936 /* more tests using the last curve */
937 if (n
== OSSL_NELEM(char2_curve_tests
) - 1) {
938 if (!TEST_true(EC_POINT_set_affine_coordinates_GF2m(group
, P
, x
, y
,
940 || !TEST_true(EC_POINT_copy(Q
, P
))
941 || !TEST_false(EC_POINT_is_at_infinity(group
, Q
))
942 || !TEST_true(EC_POINT_dbl(group
, P
, P
, ctx
))
943 || !TEST_int_gt(EC_POINT_is_on_curve(group
, P
, ctx
), 0)
944 || !TEST_true(EC_POINT_invert(group
, Q
, ctx
)) /* P = -2Q */
945 || !TEST_true(EC_POINT_add(group
, R
, P
, Q
, ctx
))
946 || !TEST_true(EC_POINT_add(group
, R
, R
, Q
, ctx
))
947 || !TEST_true(EC_POINT_is_at_infinity(group
, R
)) /* R = P + 2Q */
948 || !TEST_false(EC_POINT_is_at_infinity(group
, Q
)))
955 if (!TEST_true(BN_add(y
, z
, BN_value_one()))
956 || !TEST_false(BN_is_odd(y
))
957 || !TEST_true(BN_rshift1(y
, y
)))
959 scalars
[0] = y
; /* (group order + 1)/2, so y*Q + y*Q = Q */
962 BIO_printf(bio_out
, "combined multiplication ...");
964 /* z is still the group order */
965 if (!TEST_true(EC_POINTs_mul(group
, P
, NULL
, 2, points
, scalars
, ctx
))
966 || !TEST_true(EC_POINTs_mul(group
, R
, z
, 2, points
, scalars
, ctx
))
967 || !TEST_int_eq(0, EC_POINT_cmp(group
, P
, R
, ctx
))
968 || !TEST_int_eq(0, EC_POINT_cmp(group
, R
, Q
, ctx
)))
971 if (!TEST_true(BN_pseudo_rand(y
, BN_num_bits(y
), 0, 0))
972 || !TEST_true(BN_add(z
, z
, y
)))
974 BN_set_negative(z
, 1);
976 scalars
[1] = z
; /* z = -(order + y) */
978 if (!TEST_true(EC_POINTs_mul(group
, P
, NULL
, 2, points
, scalars
, ctx
))
979 || !TEST_true(EC_POINT_is_at_infinity(group
, P
)))
982 if (!TEST_true(BN_pseudo_rand(x
, BN_num_bits(y
) - 1, 0, 0))
983 || !TEST_true(BN_add(z
, x
, y
)))
985 BN_set_negative(z
, 1);
988 scalars
[2] = z
; /* z = -(x+y) */
990 if (!TEST_true(EC_POINTs_mul(group
, P
, NULL
, 3, points
, scalars
, ctx
))
991 || !TEST_true(EC_POINT_is_at_infinity(group
, P
)))
1009 EC_GROUP_free(group
);
1010 EC_GROUP_free(variable
);
1014 static int char2_field_tests(void)
1017 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
;
1018 EC_GROUP
*group
= NULL
, *tmp
= NULL
;
1019 EC_POINT
*P
= NULL
, *Q
= NULL
, *R
= NULL
;
1020 BIGNUM
*x
= NULL
, *y
= NULL
, *z
= NULL
, *cof
= NULL
, *yplusone
= NULL
;
1021 unsigned char buf
[100];
1025 if (!TEST_ptr(ctx
= BN_CTX_new())
1026 || !TEST_ptr(p
= BN_new())
1027 || !TEST_ptr(a
= BN_new())
1028 || !TEST_ptr(b
= BN_new())
1029 || !TEST_true(BN_hex2bn(&p
, "13"))
1030 || !TEST_true(BN_hex2bn(&a
, "3"))
1031 || !TEST_true(BN_hex2bn(&b
, "1")))
1034 group
= EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use
1035 * EC_GROUP_new_curve_GF2m
1036 * so that the library gets
1037 * to choose the EC_METHOD */
1038 if (!TEST_ptr(group
)
1039 || !TEST_true(EC_GROUP_set_curve_GF2m(group
, p
, a
, b
, ctx
))
1040 || !TEST_ptr(tmp
= EC_GROUP_new(EC_GROUP_method_of(group
)))
1041 || !TEST_true(EC_GROUP_copy(tmp
, group
)))
1043 EC_GROUP_free(group
);
1047 if (!TEST_true(EC_GROUP_get_curve_GF2m(group
, p
, a
, b
, ctx
)))
1051 "Curve defined by Weierstrass equation\n"
1052 " y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
1053 BN_print(bio_out
, p
);
1054 BIO_printf(bio_out
, ")\n a = 0x");
1055 BN_print(bio_out
, a
);
1056 BIO_printf(bio_out
, "\n b = 0x");
1057 BN_print(bio_out
, b
);
1058 BIO_printf(bio_out
, "\n(0x... means binary polynomial)\n");
1060 if (!TEST_ptr(P
= EC_POINT_new(group
))
1061 || !TEST_ptr(Q
= EC_POINT_new(group
))
1062 || !TEST_ptr(R
= EC_POINT_new(group
))
1063 || !TEST_true(EC_POINT_set_to_infinity(group
, P
))
1064 || !TEST_true(EC_POINT_is_at_infinity(group
, P
)))
1068 if (!TEST_true(EC_POINT_oct2point(group
, Q
, buf
, 1, ctx
))
1069 || !TEST_true(EC_POINT_add(group
, P
, P
, Q
, ctx
))
1070 || !TEST_true(EC_POINT_is_at_infinity(group
, P
))
1071 || !TEST_ptr(x
= BN_new())
1072 || !TEST_ptr(y
= BN_new())
1073 || !TEST_ptr(z
= BN_new())
1074 || !TEST_ptr(cof
= BN_new())
1075 || !TEST_ptr(yplusone
= BN_new())
1076 || !TEST_true(BN_hex2bn(&x
, "6"))
1077 /* Change test based on whether binary point compression is enabled or not. */
1078 # ifdef OPENSSL_EC_BIN_PT_COMP
1079 || !TEST_true(EC_POINT_set_compressed_coordinates_GF2m(group
, Q
, x
, 1,
1082 || !TEST_true(BN_hex2bn(&y
, "8"))
1083 || !TEST_true(EC_POINT_set_affine_coordinates_GF2m(group
, Q
, x
, y
, ctx
))
1087 if (!TEST_int_gt(EC_POINT_is_on_curve(group
, Q
, ctx
), 0)) {
1088 /* Change test based on whether binary point compression is enabled or not. */
1089 # ifdef OPENSSL_EC_BIN_PT_COMP
1090 if (!TEST_true(EC_POINT_get_affine_coordinates_GF2m(group
, Q
, x
, y
,
1094 BIO_printf(bio_err
, "Point is not on curve: x = 0x");
1095 BN_print_fp(stderr
, x
);
1096 BIO_printf(bio_err
, ", y = 0x");
1097 BN_print_fp(stderr
, y
);
1098 BIO_printf(bio_err
, "\n");
1102 BIO_printf(bio_out
, "A cyclic subgroup:\n");
1105 if (!TEST_int_ne(k
--, 0))
1108 if (EC_POINT_is_at_infinity(group
, P
))
1109 BIO_printf(bio_out
, " point at infinity\n");
1111 if (!TEST_true(EC_POINT_get_affine_coordinates_GF2m(group
, P
, x
, y
,
1115 BIO_printf(bio_out
, " x = 0x");
1116 BN_print(bio_out
, x
);
1117 BIO_printf(bio_out
, ", y = 0x");
1118 BN_print(bio_out
, y
);
1119 BIO_printf(bio_out
, "\n");
1122 if (!TEST_true(EC_POINT_copy(R
, P
))
1123 || !TEST_true(EC_POINT_add(group
, P
, P
, Q
, ctx
)))
1126 while (!EC_POINT_is_at_infinity(group
, P
));
1128 if (!TEST_true(EC_POINT_add(group
, P
, Q
, R
, ctx
))
1129 || !TEST_true(EC_POINT_is_at_infinity(group
, P
)))
1132 /* Change test based on whether binary point compression is enabled or not. */
1133 # ifdef OPENSSL_EC_BIN_PT_COMP
1134 len
= EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_COMPRESSED
,
1135 buf
, sizeof buf
, ctx
);
1136 if (!TEST_size_t_ne(len
, 0)
1137 || !TEST_true(EC_POINT_oct2point(group
, P
, buf
, len
, ctx
))
1138 || !TEST_int_eq(0, EC_POINT_cmp(group
, P
, Q
, ctx
)))
1140 BIO_printf(bio_out
, "Generator as octet string, compressed form:\n ");
1141 for (i
= 0; i
< len
; i
++)
1142 BIO_printf(bio_out
, "%02X", buf
[i
]);
1145 len
= EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_UNCOMPRESSED
,
1146 buf
, sizeof buf
, ctx
);
1147 if (!TEST_size_t_ne(len
, 0)
1148 || !TEST_true(EC_POINT_oct2point(group
, P
, buf
, len
, ctx
))
1149 || !TEST_int_eq(0, EC_POINT_cmp(group
, P
, Q
, ctx
)))
1151 BIO_printf(bio_out
, "\nGenerator as octet string, uncompressed form:\n"
1153 for (i
= 0; i
< len
; i
++)
1154 BIO_printf(bio_out
, "%02X", buf
[i
]);
1156 /* Change test based on whether binary point compression is enabled or not. */
1157 # ifdef OPENSSL_EC_BIN_PT_COMP
1159 EC_POINT_point2oct(group
, Q
, POINT_CONVERSION_HYBRID
, buf
, sizeof buf
,
1161 if (!TEST_size_t_ne(len
, 0)
1162 || !TEST_true(EC_POINT_oct2point(group
, P
, buf
, len
, ctx
))
1163 || !TEST_int_eq(0, EC_POINT_cmp(group
, P
, Q
, ctx
)))
1165 BIO_printf(bio_out
, "\nGenerator as octet string, hybrid form:\n ");
1166 for (i
= 0; i
< len
; i
++)
1167 BIO_printf(bio_out
, "%02X", buf
[i
]);
1169 BIO_printf(bio_out
, "\n");
1171 if (!TEST_true(EC_POINT_invert(group
, P
, ctx
))
1172 || !TEST_int_eq(0, EC_POINT_cmp(group
, P
, R
, ctx
)))
1178 BIO_printf(bio_out
, "\n\n");
1186 EC_GROUP_free(group
);
1200 static int internal_curve_test(int n
)
1202 EC_GROUP
*group
= NULL
;
1203 int nid
= curves
[n
].nid
;
1205 if (!TEST_ptr(group
= EC_GROUP_new_by_curve_name(nid
))) {
1206 TEST_info("EC_GROUP_new_curve_name() failed with curve %s\n",
1210 if (!TEST_true(EC_GROUP_check(group
, NULL
))) {
1211 TEST_info("EC_GROUP_check() failed with curve %s\n", OBJ_nid2sn(nid
));
1212 EC_GROUP_free(group
);
1215 EC_GROUP_free(group
);
1219 static int internal_curve_test_method(int n
)
1221 int r
, nid
= curves
[n
].nid
;
1225 * Skip for X25519 because low level operations such as EC_POINT_mul()
1226 * are not supported for this curve
1228 if (nid
== NID_X25519
)
1230 if (!TEST_ptr(group
= EC_GROUP_new_by_curve_name(nid
))) {
1231 TEST_info("Curve %s failed\n", OBJ_nid2sn(nid
));
1234 r
= group_order_tests(group
);
1235 EC_GROUP_free(group
);
1239 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1241 * nistp_test_params contains magic numbers for testing our optimized
1242 * implementations of several NIST curves with characteristic > 3.
1244 struct nistp_test_params
{
1245 const EC_METHOD
*(*meth
) ();
1248 * Qx, Qy and D are taken from
1249 * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
1250 * Otherwise, values are standard curve parameters from FIPS 180-3
1252 const char *p
, *a
, *b
, *Qx
, *Qy
, *Gx
, *Gy
, *order
, *d
;
1255 static const struct nistp_test_params nistp_tests_params
[] = {
1258 EC_GFp_nistp224_method
,
1261 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
1263 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
1265 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
1267 "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E",
1269 "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555",
1271 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
1273 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
1275 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
1277 "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8",
1281 EC_GFp_nistp256_method
,
1284 "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
1286 "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
1288 "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
1290 "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19",
1292 "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09",
1294 "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
1296 "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
1298 "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
1300 "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96",
1304 EC_GFp_nistp521_method
,
1308 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
1309 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
1312 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
1313 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
1316 "953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e1"
1317 "56193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
1320 "e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e"
1321 "59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4",
1324 "350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8"
1325 "554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e",
1328 "858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dba"
1329 "a14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
1332 "39296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c"
1333 "97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
1336 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa"
1337 "51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
1340 "085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eee"
1341 "df09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722",
1345 static int nistp_single_test(int idx
)
1347 const struct nistp_test_params
*test
= nistp_tests_params
+ idx
;
1349 BIGNUM
*p
= NULL
, *a
= NULL
, *b
= NULL
, *x
= NULL
, *y
= NULL
;
1350 BIGNUM
*n
= NULL
, *m
= NULL
, *order
= NULL
, *yplusone
= NULL
;
1351 EC_GROUP
*NISTP
= NULL
;
1352 EC_POINT
*G
= NULL
, *P
= NULL
, *Q
= NULL
, *Q_CHECK
= NULL
;
1355 BIO_printf(bio_out
, "\nNIST curve P-%d (optimised implementation):\n",
1357 if (!TEST_ptr(ctx
= BN_CTX_new())
1358 || !TEST_ptr(p
= BN_new())
1359 || !TEST_ptr(a
= BN_new())
1360 || !TEST_ptr(b
= BN_new())
1361 || !TEST_ptr(x
= BN_new())
1362 || !TEST_ptr(y
= BN_new())
1363 || !TEST_ptr(m
= BN_new())
1364 || !TEST_ptr(n
= BN_new())
1365 || !TEST_ptr(order
= BN_new())
1366 || !TEST_ptr(yplusone
= BN_new())
1368 || !TEST_ptr(NISTP
= EC_GROUP_new(test
->meth()))
1369 || !TEST_true(BN_hex2bn(&p
, test
->p
))
1370 || !TEST_int_eq(1, BN_is_prime_ex(p
, BN_prime_checks
, ctx
, NULL
))
1371 || !TEST_true(BN_hex2bn(&a
, test
->a
))
1372 || !TEST_true(BN_hex2bn(&b
, test
->b
))
1373 || !TEST_true(EC_GROUP_set_curve_GFp(NISTP
, p
, a
, b
, ctx
))
1374 || !TEST_ptr(G
= EC_POINT_new(NISTP
))
1375 || !TEST_ptr(P
= EC_POINT_new(NISTP
))
1376 || !TEST_ptr(Q
= EC_POINT_new(NISTP
))
1377 || !TEST_ptr(Q_CHECK
= EC_POINT_new(NISTP
))
1378 || !TEST_true(BN_hex2bn(&x
, test
->Qx
))
1379 || !TEST_true(BN_hex2bn(&y
, test
->Qy
))
1380 || !TEST_true(BN_add(yplusone
, y
, BN_value_one()))
1382 * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
1383 * and therefore setting the coordinates should fail.
1385 || !TEST_false(EC_POINT_set_affine_coordinates_GFp(NISTP
, Q_CHECK
, x
,
1387 || !TEST_true(EC_POINT_set_affine_coordinates_GFp(NISTP
, Q_CHECK
, x
, y
,
1389 || !TEST_true(BN_hex2bn(&x
, test
->Gx
))
1390 || !TEST_true(BN_hex2bn(&y
, test
->Gy
))
1391 || !TEST_true(EC_POINT_set_affine_coordinates_GFp(NISTP
, G
, x
, y
, ctx
))
1392 || !TEST_true(BN_hex2bn(&order
, test
->order
))
1393 || !TEST_true(EC_GROUP_set_generator(NISTP
, G
, order
, BN_value_one()))
1394 || !TEST_int_eq(EC_GROUP_get_degree(NISTP
), test
->degree
))
1397 BIO_printf(bio_out
, "NIST test vectors ... ");
1398 if (!TEST_true(BN_hex2bn(&n
, test
->d
)))
1400 /* fixed point multiplication */
1401 EC_POINT_mul(NISTP
, Q
, n
, NULL
, NULL
, ctx
);
1402 if (!TEST_int_eq(0, EC_POINT_cmp(NISTP
, Q
, Q_CHECK
, ctx
)))
1404 /* random point multiplication */
1405 EC_POINT_mul(NISTP
, Q
, NULL
, G
, n
, ctx
);
1406 if (!TEST_int_eq(0, EC_POINT_cmp(NISTP
, Q
, Q_CHECK
, ctx
))
1408 /* set generator to P = 2*G, where G is the standard generator */
1409 || !TEST_true(EC_POINT_dbl(NISTP
, P
, G
, ctx
))
1410 || !TEST_true(EC_GROUP_set_generator(NISTP
, P
, order
, BN_value_one()))
1411 /* set the scalar to m=n/2, where n is the NIST test scalar */
1412 || !TEST_true(BN_rshift(m
, n
, 1)))
1415 /* test the non-standard generator */
1416 /* fixed point multiplication */
1417 EC_POINT_mul(NISTP
, Q
, m
, NULL
, NULL
, ctx
);
1418 if (!TEST_int_eq(0, EC_POINT_cmp(NISTP
, Q
, Q_CHECK
, ctx
)))
1420 /* random point multiplication */
1421 EC_POINT_mul(NISTP
, Q
, NULL
, P
, m
, ctx
);
1422 if (!TEST_int_eq(0, EC_POINT_cmp(NISTP
, Q
, Q_CHECK
, ctx
))
1425 * We have not performed precomputation so have_precompute mult should be
1428 || !TEST_false(EC_GROUP_have_precompute_mult(NISTP
))
1430 /* now repeat all tests with precomputation */
1431 || !TEST_true(EC_GROUP_precompute_mult(NISTP
, ctx
))
1432 || !TEST_true(EC_GROUP_have_precompute_mult(NISTP
)))
1435 /* fixed point multiplication */
1436 EC_POINT_mul(NISTP
, Q
, m
, NULL
, NULL
, ctx
);
1437 if (!TEST_int_eq(0, EC_POINT_cmp(NISTP
, Q
, Q_CHECK
, ctx
)))
1439 /* random point multiplication */
1440 EC_POINT_mul(NISTP
, Q
, NULL
, P
, m
, ctx
);
1441 if (!TEST_int_eq(0, EC_POINT_cmp(NISTP
, Q
, Q_CHECK
, ctx
))
1443 /* reset generator */
1444 || !TEST_true(EC_GROUP_set_generator(NISTP
, G
, order
, BN_value_one())))
1446 /* fixed point multiplication */
1447 EC_POINT_mul(NISTP
, Q
, n
, NULL
, NULL
, ctx
);
1448 if (!TEST_int_eq(0, EC_POINT_cmp(NISTP
, Q
, Q_CHECK
, ctx
)))
1450 /* random point multiplication */
1451 EC_POINT_mul(NISTP
, Q
, NULL
, G
, n
, ctx
);
1452 if (!TEST_int_eq(0, EC_POINT_cmp(NISTP
, Q
, Q_CHECK
, ctx
)))
1455 r
= group_order_tests(NISTP
);
1457 EC_GROUP_free(NISTP
);
1461 EC_POINT_free(Q_CHECK
);
1476 static int parameter_test(void)
1478 EC_GROUP
*group
= NULL
, *group2
= NULL
;
1479 ECPARAMETERS
*ecparameters
= NULL
;
1482 r
= TEST_ptr(group
= EC_GROUP_new_by_curve_name(NID_secp112r1
))
1483 && TEST_ptr(ecparameters
= EC_GROUP_get_ecparameters(group
, NULL
))
1484 && TEST_ptr(group2
= EC_GROUP_new_from_ecparameters(ecparameters
))
1485 && TEST_int_eq(EC_GROUP_cmp(group
, group2
, NULL
), 0);
1487 EC_GROUP_free(group
);
1488 EC_GROUP_free(group2
);
1489 ECPARAMETERS_free(ecparameters
);
1493 static const char rnd_seed
[] =
1494 "string to make the random number generator think it has entropy";
1497 int test_main(int argc
, char *argv
[])
1499 int result
= EXIT_SUCCESS
;
1500 #ifndef OPENSSL_NO_EC
1502 crv_len
= EC_get_builtin_curves(NULL
, 0);
1503 if (!TEST_ptr(curves
= OPENSSL_malloc(sizeof(*curves
) * crv_len
))
1504 || !TEST_true(EC_get_builtin_curves(curves
, crv_len
)))
1505 return EXIT_FAILURE
;
1507 RAND_seed(rnd_seed
, sizeof rnd_seed
); /* or BN_generate_prime may fail */
1509 ADD_TEST(parameter_test
);
1510 ADD_TEST(prime_field_tests
);
1511 # ifndef OPENSSL_NO_EC2M
1512 ADD_TEST(char2_field_tests
);
1513 ADD_ALL_TESTS(char2_curve_test
, OSSL_NELEM(char2_curve_tests
));
1515 # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1516 ADD_ALL_TESTS(nistp_single_test
, OSSL_NELEM(nistp_tests_params
));
1518 ADD_ALL_TESTS(internal_curve_test
, crv_len
);
1519 ADD_ALL_TESTS(internal_curve_test_method
, crv_len
);
1521 result
= run_tests(argv
[0]);
1522 OPENSSL_free(curves
);