1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4*e0c4386eSCy Schubert *
5*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
6*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
7*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
8*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
9*e0c4386eSCy Schubert */
10*e0c4386eSCy Schubert
11*e0c4386eSCy Schubert /*
12*e0c4386eSCy Schubert * EC_KEY low level APIs are deprecated for public use, but still ok for
13*e0c4386eSCy Schubert * internal use.
14*e0c4386eSCy Schubert */
15*e0c4386eSCy Schubert #include "internal/deprecated.h"
16*e0c4386eSCy Schubert
17*e0c4386eSCy Schubert #include <string.h>
18*e0c4386eSCy Schubert #include "internal/nelem.h"
19*e0c4386eSCy Schubert #include "testutil.h"
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubert #include <openssl/ec.h>
22*e0c4386eSCy Schubert #ifndef OPENSSL_NO_ENGINE
23*e0c4386eSCy Schubert # include <openssl/engine.h>
24*e0c4386eSCy Schubert #endif
25*e0c4386eSCy Schubert #include <openssl/err.h>
26*e0c4386eSCy Schubert #include <openssl/obj_mac.h>
27*e0c4386eSCy Schubert #include <openssl/objects.h>
28*e0c4386eSCy Schubert #include <openssl/rand.h>
29*e0c4386eSCy Schubert #include <openssl/bn.h>
30*e0c4386eSCy Schubert #include <openssl/opensslconf.h>
31*e0c4386eSCy Schubert #include <openssl/core_names.h>
32*e0c4386eSCy Schubert #include <openssl/param_build.h>
33*e0c4386eSCy Schubert #include <openssl/evp.h>
34*e0c4386eSCy Schubert
35*e0c4386eSCy Schubert static size_t crv_len = 0;
36*e0c4386eSCy Schubert static EC_builtin_curve *curves = NULL;
37*e0c4386eSCy Schubert
38*e0c4386eSCy Schubert /* test multiplication with group order, long and negative scalars */
group_order_tests(EC_GROUP * group)39*e0c4386eSCy Schubert static int group_order_tests(EC_GROUP *group)
40*e0c4386eSCy Schubert {
41*e0c4386eSCy Schubert BIGNUM *n1 = NULL, *n2 = NULL, *order = NULL;
42*e0c4386eSCy Schubert EC_POINT *P = NULL, *Q = NULL, *R = NULL, *S = NULL;
43*e0c4386eSCy Schubert const EC_POINT *G = NULL;
44*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
45*e0c4386eSCy Schubert int i = 0, r = 0;
46*e0c4386eSCy Schubert
47*e0c4386eSCy Schubert if (!TEST_ptr(n1 = BN_new())
48*e0c4386eSCy Schubert || !TEST_ptr(n2 = BN_new())
49*e0c4386eSCy Schubert || !TEST_ptr(order = BN_new())
50*e0c4386eSCy Schubert || !TEST_ptr(ctx = BN_CTX_new())
51*e0c4386eSCy Schubert || !TEST_ptr(G = EC_GROUP_get0_generator(group))
52*e0c4386eSCy Schubert || !TEST_ptr(P = EC_POINT_new(group))
53*e0c4386eSCy Schubert || !TEST_ptr(Q = EC_POINT_new(group))
54*e0c4386eSCy Schubert || !TEST_ptr(R = EC_POINT_new(group))
55*e0c4386eSCy Schubert || !TEST_ptr(S = EC_POINT_new(group)))
56*e0c4386eSCy Schubert goto err;
57*e0c4386eSCy Schubert
58*e0c4386eSCy Schubert if (!TEST_true(EC_GROUP_get_order(group, order, ctx))
59*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
60*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, Q))
61*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
62*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_precompute_mult(group, ctx))
63*e0c4386eSCy Schubert #endif
64*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
65*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, Q))
66*e0c4386eSCy Schubert || !TEST_true(EC_POINT_copy(P, G))
67*e0c4386eSCy Schubert || !TEST_true(BN_one(n1))
68*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))
69*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
70*e0c4386eSCy Schubert || !TEST_true(BN_sub(n1, order, n1))
71*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))
72*e0c4386eSCy Schubert || !TEST_true(EC_POINT_invert(group, Q, ctx))
73*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))
74*e0c4386eSCy Schubert goto err;
75*e0c4386eSCy Schubert
76*e0c4386eSCy Schubert for (i = 1; i <= 2; i++) {
77*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
78*e0c4386eSCy Schubert const BIGNUM *scalars[6];
79*e0c4386eSCy Schubert const EC_POINT *points[6];
80*e0c4386eSCy Schubert #endif
81*e0c4386eSCy Schubert
82*e0c4386eSCy Schubert if (!TEST_true(BN_set_word(n1, i))
83*e0c4386eSCy Schubert /*
84*e0c4386eSCy Schubert * If i == 1, P will be the predefined generator for which
85*e0c4386eSCy Schubert * EC_GROUP_precompute_mult has set up precomputation.
86*e0c4386eSCy Schubert */
87*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, P, n1, NULL, NULL, ctx))
88*e0c4386eSCy Schubert || (i == 1 && !TEST_int_eq(0, EC_POINT_cmp(group, P, G, ctx)))
89*e0c4386eSCy Schubert || !TEST_true(BN_one(n1))
90*e0c4386eSCy Schubert /* n1 = 1 - order */
91*e0c4386eSCy Schubert || !TEST_true(BN_sub(n1, n1, order))
92*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n1, ctx))
93*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
94*e0c4386eSCy Schubert
95*e0c4386eSCy Schubert /* n2 = 1 + order */
96*e0c4386eSCy Schubert || !TEST_true(BN_add(n2, order, BN_value_one()))
97*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
98*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
99*e0c4386eSCy Schubert
100*e0c4386eSCy Schubert /* n2 = (1 - order) * (1 + order) = 1 - order^2 */
101*e0c4386eSCy Schubert || !TEST_true(BN_mul(n2, n1, n2, ctx))
102*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
103*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))
104*e0c4386eSCy Schubert goto err;
105*e0c4386eSCy Schubert
106*e0c4386eSCy Schubert /* n2 = order^2 - 1 */
107*e0c4386eSCy Schubert BN_set_negative(n2, 0);
108*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
109*e0c4386eSCy Schubert /* Add P to verify the result. */
110*e0c4386eSCy Schubert || !TEST_true(EC_POINT_add(group, Q, Q, P, ctx))
111*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, Q))
112*e0c4386eSCy Schubert || !TEST_false(EC_POINT_is_at_infinity(group, P)))
113*e0c4386eSCy Schubert goto err;
114*e0c4386eSCy Schubert
115*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
116*e0c4386eSCy Schubert /* Exercise EC_POINTs_mul, including corner cases. */
117*e0c4386eSCy Schubert scalars[0] = scalars[1] = BN_value_one();
118*e0c4386eSCy Schubert points[0] = points[1] = P;
119*e0c4386eSCy Schubert
120*e0c4386eSCy Schubert if (!TEST_true(EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx))
121*e0c4386eSCy Schubert || !TEST_true(EC_POINT_dbl(group, S, points[0], ctx))
122*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, R, S, ctx)))
123*e0c4386eSCy Schubert goto err;
124*e0c4386eSCy Schubert
125*e0c4386eSCy Schubert scalars[0] = n1;
126*e0c4386eSCy Schubert points[0] = Q; /* => infinity */
127*e0c4386eSCy Schubert scalars[1] = n2;
128*e0c4386eSCy Schubert points[1] = P; /* => -P */
129*e0c4386eSCy Schubert scalars[2] = n1;
130*e0c4386eSCy Schubert points[2] = Q; /* => infinity */
131*e0c4386eSCy Schubert scalars[3] = n2;
132*e0c4386eSCy Schubert points[3] = Q; /* => infinity */
133*e0c4386eSCy Schubert scalars[4] = n1;
134*e0c4386eSCy Schubert points[4] = P; /* => P */
135*e0c4386eSCy Schubert scalars[5] = n2;
136*e0c4386eSCy Schubert points[5] = Q; /* => infinity */
137*e0c4386eSCy Schubert if (!TEST_true(EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))
138*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, P)))
139*e0c4386eSCy Schubert goto err;
140*e0c4386eSCy Schubert #endif
141*e0c4386eSCy Schubert }
142*e0c4386eSCy Schubert
143*e0c4386eSCy Schubert r = 1;
144*e0c4386eSCy Schubert err:
145*e0c4386eSCy Schubert if (r == 0 && i != 0)
146*e0c4386eSCy Schubert TEST_info(i == 1 ? "allowing precomputation" :
147*e0c4386eSCy Schubert "without precomputation");
148*e0c4386eSCy Schubert EC_POINT_free(P);
149*e0c4386eSCy Schubert EC_POINT_free(Q);
150*e0c4386eSCy Schubert EC_POINT_free(R);
151*e0c4386eSCy Schubert EC_POINT_free(S);
152*e0c4386eSCy Schubert BN_free(n1);
153*e0c4386eSCy Schubert BN_free(n2);
154*e0c4386eSCy Schubert BN_free(order);
155*e0c4386eSCy Schubert BN_CTX_free(ctx);
156*e0c4386eSCy Schubert return r;
157*e0c4386eSCy Schubert }
158*e0c4386eSCy Schubert
prime_field_tests(void)159*e0c4386eSCy Schubert static int prime_field_tests(void)
160*e0c4386eSCy Schubert {
161*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
162*e0c4386eSCy Schubert BIGNUM *p = NULL, *a = NULL, *b = NULL, *scalar3 = NULL;
163*e0c4386eSCy Schubert EC_GROUP *group = NULL;
164*e0c4386eSCy Schubert EC_POINT *P = NULL, *Q = NULL, *R = NULL;
165*e0c4386eSCy Schubert BIGNUM *x = NULL, *y = NULL, *z = NULL, *yplusone = NULL;
166*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
167*e0c4386eSCy Schubert const EC_POINT *points[4];
168*e0c4386eSCy Schubert const BIGNUM *scalars[4];
169*e0c4386eSCy Schubert #endif
170*e0c4386eSCy Schubert unsigned char buf[100];
171*e0c4386eSCy Schubert size_t len, r = 0;
172*e0c4386eSCy Schubert int k;
173*e0c4386eSCy Schubert
174*e0c4386eSCy Schubert if (!TEST_ptr(ctx = BN_CTX_new())
175*e0c4386eSCy Schubert || !TEST_ptr(p = BN_new())
176*e0c4386eSCy Schubert || !TEST_ptr(a = BN_new())
177*e0c4386eSCy Schubert || !TEST_ptr(b = BN_new())
178*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&p, "17"))
179*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&a, "1"))
180*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&b, "1"))
181*e0c4386eSCy Schubert || !TEST_ptr(group = EC_GROUP_new_curve_GFp(p, a, b, ctx))
182*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx)))
183*e0c4386eSCy Schubert goto err;
184*e0c4386eSCy Schubert
185*e0c4386eSCy Schubert TEST_info("Curve defined by Weierstrass equation");
186*e0c4386eSCy Schubert TEST_note(" y^2 = x^3 + a*x + b (mod p)");
187*e0c4386eSCy Schubert test_output_bignum("a", a);
188*e0c4386eSCy Schubert test_output_bignum("b", b);
189*e0c4386eSCy Schubert test_output_bignum("p", p);
190*e0c4386eSCy Schubert
191*e0c4386eSCy Schubert buf[0] = 0;
192*e0c4386eSCy Schubert if (!TEST_ptr(P = EC_POINT_new(group))
193*e0c4386eSCy Schubert || !TEST_ptr(Q = EC_POINT_new(group))
194*e0c4386eSCy Schubert || !TEST_ptr(R = EC_POINT_new(group))
195*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_to_infinity(group, P))
196*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, P))
197*e0c4386eSCy Schubert || !TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx))
198*e0c4386eSCy Schubert || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))
199*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, P))
200*e0c4386eSCy Schubert || !TEST_ptr(x = BN_new())
201*e0c4386eSCy Schubert || !TEST_ptr(y = BN_new())
202*e0c4386eSCy Schubert || !TEST_ptr(z = BN_new())
203*e0c4386eSCy Schubert || !TEST_ptr(yplusone = BN_new())
204*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&x, "D"))
205*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx)))
206*e0c4386eSCy Schubert goto err;
207*e0c4386eSCy Schubert
208*e0c4386eSCy Schubert if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) {
209*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_get_affine_coordinates(group, Q, x, y, ctx)))
210*e0c4386eSCy Schubert goto err;
211*e0c4386eSCy Schubert TEST_info("Point is not on curve");
212*e0c4386eSCy Schubert test_output_bignum("x", x);
213*e0c4386eSCy Schubert test_output_bignum("y", y);
214*e0c4386eSCy Schubert goto err;
215*e0c4386eSCy Schubert }
216*e0c4386eSCy Schubert
217*e0c4386eSCy Schubert TEST_note("A cyclic subgroup:");
218*e0c4386eSCy Schubert k = 100;
219*e0c4386eSCy Schubert do {
220*e0c4386eSCy Schubert if (!TEST_int_ne(k--, 0))
221*e0c4386eSCy Schubert goto err;
222*e0c4386eSCy Schubert
223*e0c4386eSCy Schubert if (EC_POINT_is_at_infinity(group, P)) {
224*e0c4386eSCy Schubert TEST_note(" point at infinity");
225*e0c4386eSCy Schubert } else {
226*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y,
227*e0c4386eSCy Schubert ctx)))
228*e0c4386eSCy Schubert goto err;
229*e0c4386eSCy Schubert
230*e0c4386eSCy Schubert test_output_bignum("x", x);
231*e0c4386eSCy Schubert test_output_bignum("y", y);
232*e0c4386eSCy Schubert }
233*e0c4386eSCy Schubert
234*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_copy(R, P))
235*e0c4386eSCy Schubert || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)))
236*e0c4386eSCy Schubert goto err;
237*e0c4386eSCy Schubert
238*e0c4386eSCy Schubert } while (!EC_POINT_is_at_infinity(group, P));
239*e0c4386eSCy Schubert
240*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx))
241*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, P)))
242*e0c4386eSCy Schubert goto err;
243*e0c4386eSCy Schubert
244*e0c4386eSCy Schubert len =
245*e0c4386eSCy Schubert EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
246*e0c4386eSCy Schubert sizeof(buf), ctx);
247*e0c4386eSCy Schubert if (!TEST_size_t_ne(len, 0)
248*e0c4386eSCy Schubert || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
249*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
250*e0c4386eSCy Schubert goto err;
251*e0c4386eSCy Schubert test_output_memory("Generator as octet string, compressed form:",
252*e0c4386eSCy Schubert buf, len);
253*e0c4386eSCy Schubert
254*e0c4386eSCy Schubert len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
255*e0c4386eSCy Schubert buf, sizeof(buf), ctx);
256*e0c4386eSCy Schubert if (!TEST_size_t_ne(len, 0)
257*e0c4386eSCy Schubert || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
258*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
259*e0c4386eSCy Schubert goto err;
260*e0c4386eSCy Schubert test_output_memory("Generator as octet string, uncompressed form:",
261*e0c4386eSCy Schubert buf, len);
262*e0c4386eSCy Schubert
263*e0c4386eSCy Schubert len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID,
264*e0c4386eSCy Schubert buf, sizeof(buf), ctx);
265*e0c4386eSCy Schubert if (!TEST_size_t_ne(len, 0)
266*e0c4386eSCy Schubert || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
267*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
268*e0c4386eSCy Schubert goto err;
269*e0c4386eSCy Schubert test_output_memory("Generator as octet string, hybrid form:",
270*e0c4386eSCy Schubert buf, len);
271*e0c4386eSCy Schubert
272*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_invert(group, P, ctx))
273*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
274*e0c4386eSCy Schubert
275*e0c4386eSCy Schubert /*
276*e0c4386eSCy Schubert * Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2,
277*e0c4386eSCy Schubert * 2000) -- not a NIST curve, but commonly used
278*e0c4386eSCy Schubert */
279*e0c4386eSCy Schubert
280*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&p, "FFFFFFFF"
281*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))
282*e0c4386eSCy Schubert || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
283*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&a, "FFFFFFFF"
284*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))
285*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&b, "1C97BEFC"
286*e0c4386eSCy Schubert "54BD7A8B65ACF89F81D4D4ADC565FA45"))
287*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
288*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&x, "4A96B568"
289*e0c4386eSCy Schubert "8EF573284664698968C38BB913CBFC82"))
290*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&y, "23a62855"
291*e0c4386eSCy Schubert "3168947d59dcc912042351377ac5fb32"))
292*e0c4386eSCy Schubert || !TEST_true(BN_add(yplusone, y, BN_value_one()))
293*e0c4386eSCy Schubert /*
294*e0c4386eSCy Schubert * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
295*e0c4386eSCy Schubert * and therefore setting the coordinates should fail.
296*e0c4386eSCy Schubert */
297*e0c4386eSCy Schubert || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
298*e0c4386eSCy Schubert ctx))
299*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
300*e0c4386eSCy Schubert || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
301*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&z, "0100000000"
302*e0c4386eSCy Schubert "000000000001F4C8F927AED3CA752257"))
303*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
304*e0c4386eSCy Schubert || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
305*e0c4386eSCy Schubert goto err;
306*e0c4386eSCy Schubert TEST_info("SEC2 curve secp160r1 -- Generator");
307*e0c4386eSCy Schubert test_output_bignum("x", x);
308*e0c4386eSCy Schubert test_output_bignum("y", y);
309*e0c4386eSCy Schubert /* G_y value taken from the standard: */
310*e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&z, "23a62855"
311*e0c4386eSCy Schubert "3168947d59dcc912042351377ac5fb32"))
312*e0c4386eSCy Schubert || !TEST_BN_eq(y, z)
313*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_degree(group), 160)
314*e0c4386eSCy Schubert || !group_order_tests(group)
315*e0c4386eSCy Schubert
316*e0c4386eSCy Schubert /* Curve P-192 (FIPS PUB 186-2, App. 6) */
317*e0c4386eSCy Schubert
318*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFF"
319*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))
320*e0c4386eSCy Schubert || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
321*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFF"
322*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))
323*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&b, "64210519E59C80E7"
324*e0c4386eSCy Schubert "0FA7E9AB72243049FEB8DEECC146B9B1"))
325*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
326*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&x, "188DA80EB03090F6"
327*e0c4386eSCy Schubert "7CBF20EB43A18800F4FF0AFD82FF1012"))
328*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
329*e0c4386eSCy Schubert || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
330*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFF"
331*e0c4386eSCy Schubert "FFFFFFFF99DEF836146BC9B1B4D22831"))
332*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
333*e0c4386eSCy Schubert || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
334*e0c4386eSCy Schubert goto err;
335*e0c4386eSCy Schubert
336*e0c4386eSCy Schubert TEST_info("NIST curve P-192 -- Generator");
337*e0c4386eSCy Schubert test_output_bignum("x", x);
338*e0c4386eSCy Schubert test_output_bignum("y", y);
339*e0c4386eSCy Schubert /* G_y value taken from the standard: */
340*e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&z, "07192B95FFC8DA78"
341*e0c4386eSCy Schubert "631011ED6B24CDD573F977A11E794811"))
342*e0c4386eSCy Schubert || !TEST_BN_eq(y, z)
343*e0c4386eSCy Schubert || !TEST_true(BN_add(yplusone, y, BN_value_one()))
344*e0c4386eSCy Schubert /*
345*e0c4386eSCy Schubert * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
346*e0c4386eSCy Schubert * and therefore setting the coordinates should fail.
347*e0c4386eSCy Schubert */
348*e0c4386eSCy Schubert || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
349*e0c4386eSCy Schubert ctx))
350*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_degree(group), 192)
351*e0c4386eSCy Schubert || !group_order_tests(group)
352*e0c4386eSCy Schubert
353*e0c4386eSCy Schubert /* Curve P-224 (FIPS PUB 186-2, App. 6) */
354*e0c4386eSCy Schubert
355*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFF"
356*e0c4386eSCy Schubert "FFFFFFFF000000000000000000000001"))
357*e0c4386eSCy Schubert || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
358*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFF"
359*e0c4386eSCy Schubert "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))
360*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&b, "B4050A850C04B3ABF5413256"
361*e0c4386eSCy Schubert "5044B0B7D7BFD8BA270B39432355FFB4"))
362*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
363*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B9"
364*e0c4386eSCy Schubert "4A03C1D356C21122343280D6115C1D21"))
365*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx))
366*e0c4386eSCy Schubert || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
367*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF"
368*e0c4386eSCy Schubert "FFFF16A2E0B8F03E13DD29455C5C2A3D"))
369*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
370*e0c4386eSCy Schubert || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
371*e0c4386eSCy Schubert goto err;
372*e0c4386eSCy Schubert
373*e0c4386eSCy Schubert TEST_info("NIST curve P-224 -- Generator");
374*e0c4386eSCy Schubert test_output_bignum("x", x);
375*e0c4386eSCy Schubert test_output_bignum("y", y);
376*e0c4386eSCy Schubert /* G_y value taken from the standard: */
377*e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6"
378*e0c4386eSCy Schubert "CD4375A05A07476444D5819985007E34"))
379*e0c4386eSCy Schubert || !TEST_BN_eq(y, z)
380*e0c4386eSCy Schubert || !TEST_true(BN_add(yplusone, y, BN_value_one()))
381*e0c4386eSCy Schubert /*
382*e0c4386eSCy Schubert * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
383*e0c4386eSCy Schubert * and therefore setting the coordinates should fail.
384*e0c4386eSCy Schubert */
385*e0c4386eSCy Schubert || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
386*e0c4386eSCy Schubert ctx))
387*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_degree(group), 224)
388*e0c4386eSCy Schubert || !group_order_tests(group)
389*e0c4386eSCy Schubert
390*e0c4386eSCy Schubert /* Curve P-256 (FIPS PUB 186-2, App. 6) */
391*e0c4386eSCy Schubert
392*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&p, "FFFFFFFF000000010000000000000000"
393*e0c4386eSCy Schubert "00000000FFFFFFFFFFFFFFFFFFFFFFFF"))
394*e0c4386eSCy Schubert || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
395*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&a, "FFFFFFFF000000010000000000000000"
396*e0c4386eSCy Schubert "00000000FFFFFFFFFFFFFFFFFFFFFFFC"))
397*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC"
398*e0c4386eSCy Schubert "651D06B0CC53B0F63BCE3C3E27D2604B"))
399*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
400*e0c4386eSCy Schubert
401*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F2"
402*e0c4386eSCy Schubert "77037D812DEB33A0F4A13945D898C296"))
403*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
404*e0c4386eSCy Schubert || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
405*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFF"
406*e0c4386eSCy Schubert "BCE6FAADA7179E84F3B9CAC2FC632551"))
407*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
408*e0c4386eSCy Schubert || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
409*e0c4386eSCy Schubert goto err;
410*e0c4386eSCy Schubert
411*e0c4386eSCy Schubert TEST_info("NIST curve P-256 -- Generator");
412*e0c4386eSCy Schubert test_output_bignum("x", x);
413*e0c4386eSCy Schubert test_output_bignum("y", y);
414*e0c4386eSCy Schubert /* G_y value taken from the standard: */
415*e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
416*e0c4386eSCy Schubert "2BCE33576B315ECECBB6406837BF51F5"))
417*e0c4386eSCy Schubert || !TEST_BN_eq(y, z)
418*e0c4386eSCy Schubert || !TEST_true(BN_add(yplusone, y, BN_value_one()))
419*e0c4386eSCy Schubert /*
420*e0c4386eSCy Schubert * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
421*e0c4386eSCy Schubert * and therefore setting the coordinates should fail.
422*e0c4386eSCy Schubert */
423*e0c4386eSCy Schubert || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
424*e0c4386eSCy Schubert ctx))
425*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_degree(group), 256)
426*e0c4386eSCy Schubert || !group_order_tests(group)
427*e0c4386eSCy Schubert
428*e0c4386eSCy Schubert /* Curve P-384 (FIPS PUB 186-2, App. 6) */
429*e0c4386eSCy Schubert
430*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
431*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
432*e0c4386eSCy Schubert "FFFFFFFF0000000000000000FFFFFFFF"))
433*e0c4386eSCy Schubert || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
434*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
435*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
436*e0c4386eSCy Schubert "FFFFFFFF0000000000000000FFFFFFFC"))
437*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19"
438*e0c4386eSCy Schubert "181D9C6EFE8141120314088F5013875A"
439*e0c4386eSCy Schubert "C656398D8A2ED19D2A85C8EDD3EC2AEF"))
440*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
441*e0c4386eSCy Schubert
442*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD74"
443*e0c4386eSCy Schubert "6E1D3B628BA79B9859F741E082542A38"
444*e0c4386eSCy Schubert "5502F25DBF55296C3A545E3872760AB7"))
445*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
446*e0c4386eSCy Schubert || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
447*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
448*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFC7634D81F4372DDF"
449*e0c4386eSCy Schubert "581A0DB248B0A77AECEC196ACCC52973"))
450*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
451*e0c4386eSCy Schubert || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
452*e0c4386eSCy Schubert goto err;
453*e0c4386eSCy Schubert
454*e0c4386eSCy Schubert TEST_info("NIST curve P-384 -- Generator");
455*e0c4386eSCy Schubert test_output_bignum("x", x);
456*e0c4386eSCy Schubert test_output_bignum("y", y);
457*e0c4386eSCy Schubert /* G_y value taken from the standard: */
458*e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29"
459*e0c4386eSCy Schubert "F8F41DBD289A147CE9DA3113B5F0B8C0"
460*e0c4386eSCy Schubert "0A60B1CE1D7E819D7A431D7C90EA0E5F"))
461*e0c4386eSCy Schubert || !TEST_BN_eq(y, z)
462*e0c4386eSCy Schubert || !TEST_true(BN_add(yplusone, y, BN_value_one()))
463*e0c4386eSCy Schubert /*
464*e0c4386eSCy Schubert * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
465*e0c4386eSCy Schubert * and therefore setting the coordinates should fail.
466*e0c4386eSCy Schubert */
467*e0c4386eSCy Schubert || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
468*e0c4386eSCy Schubert ctx))
469*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_degree(group), 384)
470*e0c4386eSCy Schubert || !group_order_tests(group)
471*e0c4386eSCy Schubert
472*e0c4386eSCy Schubert /* Curve P-521 (FIPS PUB 186-2, App. 6) */
473*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&p, "1FF"
474*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
475*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
476*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
477*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
478*e0c4386eSCy Schubert || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
479*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&a, "1FF"
480*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
481*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
482*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
483*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC"))
484*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&b, "051"
485*e0c4386eSCy Schubert "953EB9618E1C9A1F929A21A0B68540EE"
486*e0c4386eSCy Schubert "A2DA725B99B315F3B8B489918EF109E1"
487*e0c4386eSCy Schubert "56193951EC7E937B1652C0BD3BB1BF07"
488*e0c4386eSCy Schubert "3573DF883D2C34F1EF451FD46B503F00"))
489*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
490*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&x, "C6"
491*e0c4386eSCy Schubert "858E06B70404E9CD9E3ECB662395B442"
492*e0c4386eSCy Schubert "9C648139053FB521F828AF606B4D3DBA"
493*e0c4386eSCy Schubert "A14B5E77EFE75928FE1DC127A2FFA8DE"
494*e0c4386eSCy Schubert "3348B3C1856A429BF97E7E31C2E5BD66"))
495*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx))
496*e0c4386eSCy Schubert || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
497*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&z, "1FF"
498*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
499*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA"
500*e0c4386eSCy Schubert "51868783BF2F966B7FCC0148F709A5D0"
501*e0c4386eSCy Schubert "3BB5C9B8899C47AEBB6FB71E91386409"))
502*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
503*e0c4386eSCy Schubert || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
504*e0c4386eSCy Schubert goto err;
505*e0c4386eSCy Schubert
506*e0c4386eSCy Schubert TEST_info("NIST curve P-521 -- Generator");
507*e0c4386eSCy Schubert test_output_bignum("x", x);
508*e0c4386eSCy Schubert test_output_bignum("y", y);
509*e0c4386eSCy Schubert /* G_y value taken from the standard: */
510*e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&z, "118"
511*e0c4386eSCy Schubert "39296A789A3BC0045C8A5FB42C7D1BD9"
512*e0c4386eSCy Schubert "98F54449579B446817AFBD17273E662C"
513*e0c4386eSCy Schubert "97EE72995EF42640C550B9013FAD0761"
514*e0c4386eSCy Schubert "353C7086A272C24088BE94769FD16650"))
515*e0c4386eSCy Schubert || !TEST_BN_eq(y, z)
516*e0c4386eSCy Schubert || !TEST_true(BN_add(yplusone, y, BN_value_one()))
517*e0c4386eSCy Schubert /*
518*e0c4386eSCy Schubert * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
519*e0c4386eSCy Schubert * and therefore setting the coordinates should fail.
520*e0c4386eSCy Schubert */
521*e0c4386eSCy Schubert || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
522*e0c4386eSCy Schubert ctx))
523*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_degree(group), 521)
524*e0c4386eSCy Schubert || !group_order_tests(group)
525*e0c4386eSCy Schubert
526*e0c4386eSCy Schubert /* more tests using the last curve */
527*e0c4386eSCy Schubert
528*e0c4386eSCy Schubert /* Restore the point that got mangled in the (x, y + 1) test. */
529*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
530*e0c4386eSCy Schubert || !TEST_true(EC_POINT_copy(Q, P))
531*e0c4386eSCy Schubert || !TEST_false(EC_POINT_is_at_infinity(group, Q))
532*e0c4386eSCy Schubert || !TEST_true(EC_POINT_dbl(group, P, P, ctx))
533*e0c4386eSCy Schubert || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
534*e0c4386eSCy Schubert || !TEST_true(EC_POINT_invert(group, Q, ctx)) /* P = -2Q */
535*e0c4386eSCy Schubert || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))
536*e0c4386eSCy Schubert || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))
537*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, R)) /* R = P + 2Q */
538*e0c4386eSCy Schubert || !TEST_false(EC_POINT_is_at_infinity(group, Q)))
539*e0c4386eSCy Schubert goto err;
540*e0c4386eSCy Schubert
541*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
542*e0c4386eSCy Schubert TEST_note("combined multiplication ...");
543*e0c4386eSCy Schubert points[0] = Q;
544*e0c4386eSCy Schubert points[1] = Q;
545*e0c4386eSCy Schubert points[2] = Q;
546*e0c4386eSCy Schubert points[3] = Q;
547*e0c4386eSCy Schubert
548*e0c4386eSCy Schubert if (!TEST_true(EC_GROUP_get_order(group, z, ctx))
549*e0c4386eSCy Schubert || !TEST_true(BN_add(y, z, BN_value_one()))
550*e0c4386eSCy Schubert || !TEST_BN_even(y)
551*e0c4386eSCy Schubert || !TEST_true(BN_rshift1(y, y)))
552*e0c4386eSCy Schubert goto err;
553*e0c4386eSCy Schubert
554*e0c4386eSCy Schubert scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
555*e0c4386eSCy Schubert scalars[1] = y;
556*e0c4386eSCy Schubert
557*e0c4386eSCy Schubert /* z is still the group order */
558*e0c4386eSCy Schubert if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
559*e0c4386eSCy Schubert || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
560*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
561*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx))
562*e0c4386eSCy Schubert || !TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
563*e0c4386eSCy Schubert || !TEST_true(BN_add(z, z, y)))
564*e0c4386eSCy Schubert goto err;
565*e0c4386eSCy Schubert BN_set_negative(z, 1);
566*e0c4386eSCy Schubert scalars[0] = y;
567*e0c4386eSCy Schubert scalars[1] = z; /* z = -(order + y) */
568*e0c4386eSCy Schubert
569*e0c4386eSCy Schubert if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
570*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, P))
571*e0c4386eSCy Schubert || !TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))
572*e0c4386eSCy Schubert || !TEST_true(BN_add(z, x, y)))
573*e0c4386eSCy Schubert goto err;
574*e0c4386eSCy Schubert BN_set_negative(z, 1);
575*e0c4386eSCy Schubert scalars[0] = x;
576*e0c4386eSCy Schubert scalars[1] = y;
577*e0c4386eSCy Schubert scalars[2] = z; /* z = -(x+y) */
578*e0c4386eSCy Schubert
579*e0c4386eSCy Schubert if (!TEST_ptr(scalar3 = BN_new()))
580*e0c4386eSCy Schubert goto err;
581*e0c4386eSCy Schubert BN_zero(scalar3);
582*e0c4386eSCy Schubert scalars[3] = scalar3;
583*e0c4386eSCy Schubert
584*e0c4386eSCy Schubert if (!TEST_true(EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx))
585*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, P)))
586*e0c4386eSCy Schubert goto err;
587*e0c4386eSCy Schubert #endif
588*e0c4386eSCy Schubert TEST_note(" ok\n");
589*e0c4386eSCy Schubert r = 1;
590*e0c4386eSCy Schubert err:
591*e0c4386eSCy Schubert BN_CTX_free(ctx);
592*e0c4386eSCy Schubert BN_free(p);
593*e0c4386eSCy Schubert BN_free(a);
594*e0c4386eSCy Schubert BN_free(b);
595*e0c4386eSCy Schubert EC_GROUP_free(group);
596*e0c4386eSCy Schubert EC_POINT_free(P);
597*e0c4386eSCy Schubert EC_POINT_free(Q);
598*e0c4386eSCy Schubert EC_POINT_free(R);
599*e0c4386eSCy Schubert BN_free(x);
600*e0c4386eSCy Schubert BN_free(y);
601*e0c4386eSCy Schubert BN_free(z);
602*e0c4386eSCy Schubert BN_free(yplusone);
603*e0c4386eSCy Schubert BN_free(scalar3);
604*e0c4386eSCy Schubert return r;
605*e0c4386eSCy Schubert }
606*e0c4386eSCy Schubert
607*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
608*e0c4386eSCy Schubert
609*e0c4386eSCy Schubert static struct c2_curve_test {
610*e0c4386eSCy Schubert const char *name;
611*e0c4386eSCy Schubert const char *p;
612*e0c4386eSCy Schubert const char *a;
613*e0c4386eSCy Schubert const char *b;
614*e0c4386eSCy Schubert const char *x;
615*e0c4386eSCy Schubert const char *y;
616*e0c4386eSCy Schubert int ybit;
617*e0c4386eSCy Schubert const char *order;
618*e0c4386eSCy Schubert const char *cof;
619*e0c4386eSCy Schubert int degree;
620*e0c4386eSCy Schubert } char2_curve_tests[] = {
621*e0c4386eSCy Schubert /* Curve K-163 (FIPS PUB 186-2, App. 6) */
622*e0c4386eSCy Schubert {
623*e0c4386eSCy Schubert "NIST curve K-163",
624*e0c4386eSCy Schubert "0800000000000000000000000000000000000000C9",
625*e0c4386eSCy Schubert "1",
626*e0c4386eSCy Schubert "1",
627*e0c4386eSCy Schubert "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
628*e0c4386eSCy Schubert "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
629*e0c4386eSCy Schubert 1, "04000000000000000000020108A2E0CC0D99F8A5EF", "2", 163
630*e0c4386eSCy Schubert },
631*e0c4386eSCy Schubert /* Curve B-163 (FIPS PUB 186-2, App. 6) */
632*e0c4386eSCy Schubert {
633*e0c4386eSCy Schubert "NIST curve B-163",
634*e0c4386eSCy Schubert "0800000000000000000000000000000000000000C9",
635*e0c4386eSCy Schubert "1",
636*e0c4386eSCy Schubert "020A601907B8C953CA1481EB10512F78744A3205FD",
637*e0c4386eSCy Schubert "03F0EBA16286A2D57EA0991168D4994637E8343E36",
638*e0c4386eSCy Schubert "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
639*e0c4386eSCy Schubert 1, "040000000000000000000292FE77E70C12A4234C33", "2", 163
640*e0c4386eSCy Schubert },
641*e0c4386eSCy Schubert /* Curve K-233 (FIPS PUB 186-2, App. 6) */
642*e0c4386eSCy Schubert {
643*e0c4386eSCy Schubert "NIST curve K-233",
644*e0c4386eSCy Schubert "020000000000000000000000000000000000000004000000000000000001",
645*e0c4386eSCy Schubert "0",
646*e0c4386eSCy Schubert "1",
647*e0c4386eSCy Schubert "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
648*e0c4386eSCy Schubert "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
649*e0c4386eSCy Schubert 0,
650*e0c4386eSCy Schubert "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
651*e0c4386eSCy Schubert "4", 233
652*e0c4386eSCy Schubert },
653*e0c4386eSCy Schubert /* Curve B-233 (FIPS PUB 186-2, App. 6) */
654*e0c4386eSCy Schubert {
655*e0c4386eSCy Schubert "NIST curve B-233",
656*e0c4386eSCy Schubert "020000000000000000000000000000000000000004000000000000000001",
657*e0c4386eSCy Schubert "000000000000000000000000000000000000000000000000000000000001",
658*e0c4386eSCy Schubert "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
659*e0c4386eSCy Schubert "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
660*e0c4386eSCy Schubert "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
661*e0c4386eSCy Schubert 1,
662*e0c4386eSCy Schubert "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
663*e0c4386eSCy Schubert "2", 233
664*e0c4386eSCy Schubert },
665*e0c4386eSCy Schubert /* Curve K-283 (FIPS PUB 186-2, App. 6) */
666*e0c4386eSCy Schubert {
667*e0c4386eSCy Schubert "NIST curve K-283",
668*e0c4386eSCy Schubert "08000000"
669*e0c4386eSCy Schubert "00000000000000000000000000000000000000000000000000000000000010A1",
670*e0c4386eSCy Schubert "0",
671*e0c4386eSCy Schubert "1",
672*e0c4386eSCy Schubert "0503213F"
673*e0c4386eSCy Schubert "78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
674*e0c4386eSCy Schubert "01CCDA38"
675*e0c4386eSCy Schubert "0F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
676*e0c4386eSCy Schubert 0,
677*e0c4386eSCy Schubert "01FFFFFF"
678*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
679*e0c4386eSCy Schubert "4", 283
680*e0c4386eSCy Schubert },
681*e0c4386eSCy Schubert /* Curve B-283 (FIPS PUB 186-2, App. 6) */
682*e0c4386eSCy Schubert {
683*e0c4386eSCy Schubert "NIST curve B-283",
684*e0c4386eSCy Schubert "08000000"
685*e0c4386eSCy Schubert "00000000000000000000000000000000000000000000000000000000000010A1",
686*e0c4386eSCy Schubert "00000000"
687*e0c4386eSCy Schubert "0000000000000000000000000000000000000000000000000000000000000001",
688*e0c4386eSCy Schubert "027B680A"
689*e0c4386eSCy Schubert "C8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
690*e0c4386eSCy Schubert "05F93925"
691*e0c4386eSCy Schubert "8DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
692*e0c4386eSCy Schubert "03676854"
693*e0c4386eSCy Schubert "FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
694*e0c4386eSCy Schubert 1,
695*e0c4386eSCy Schubert "03FFFFFF"
696*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
697*e0c4386eSCy Schubert "2", 283
698*e0c4386eSCy Schubert },
699*e0c4386eSCy Schubert /* Curve K-409 (FIPS PUB 186-2, App. 6) */
700*e0c4386eSCy Schubert {
701*e0c4386eSCy Schubert "NIST curve K-409",
702*e0c4386eSCy Schubert "0200000000000000000000000000000000000000"
703*e0c4386eSCy Schubert "0000000000000000000000000000000000000000008000000000000000000001",
704*e0c4386eSCy Schubert "0",
705*e0c4386eSCy Schubert "1",
706*e0c4386eSCy Schubert "0060F05F658F49C1AD3AB1890F7184210EFD0987"
707*e0c4386eSCy Schubert "E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
708*e0c4386eSCy Schubert "01E369050B7C4E42ACBA1DACBF04299C3460782F"
709*e0c4386eSCy Schubert "918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
710*e0c4386eSCy Schubert 1,
711*e0c4386eSCy Schubert "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
712*e0c4386eSCy Schubert "FFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
713*e0c4386eSCy Schubert "4", 409
714*e0c4386eSCy Schubert },
715*e0c4386eSCy Schubert /* Curve B-409 (FIPS PUB 186-2, App. 6) */
716*e0c4386eSCy Schubert {
717*e0c4386eSCy Schubert "NIST curve B-409",
718*e0c4386eSCy Schubert "0200000000000000000000000000000000000000"
719*e0c4386eSCy Schubert "0000000000000000000000000000000000000000008000000000000000000001",
720*e0c4386eSCy Schubert "0000000000000000000000000000000000000000"
721*e0c4386eSCy Schubert "0000000000000000000000000000000000000000000000000000000000000001",
722*e0c4386eSCy Schubert "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422E"
723*e0c4386eSCy Schubert "F1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
724*e0c4386eSCy Schubert "015D4860D088DDB3496B0C6064756260441CDE4A"
725*e0c4386eSCy Schubert "F1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
726*e0c4386eSCy Schubert "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5"
727*e0c4386eSCy Schubert "A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
728*e0c4386eSCy Schubert 1,
729*e0c4386eSCy Schubert "0100000000000000000000000000000000000000"
730*e0c4386eSCy Schubert "00000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
731*e0c4386eSCy Schubert "2", 409
732*e0c4386eSCy Schubert },
733*e0c4386eSCy Schubert /* Curve K-571 (FIPS PUB 186-2, App. 6) */
734*e0c4386eSCy Schubert {
735*e0c4386eSCy Schubert "NIST curve K-571",
736*e0c4386eSCy Schubert "800000000000000"
737*e0c4386eSCy Schubert "0000000000000000000000000000000000000000000000000000000000000000"
738*e0c4386eSCy Schubert "0000000000000000000000000000000000000000000000000000000000000425",
739*e0c4386eSCy Schubert "0",
740*e0c4386eSCy Schubert "1",
741*e0c4386eSCy Schubert "026EB7A859923FBC"
742*e0c4386eSCy Schubert "82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E6"
743*e0c4386eSCy Schubert "47DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
744*e0c4386eSCy Schubert "0349DC807F4FBF37"
745*e0c4386eSCy Schubert "4F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA7"
746*e0c4386eSCy Schubert "4FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
747*e0c4386eSCy Schubert 0,
748*e0c4386eSCy Schubert "0200000000000000"
749*e0c4386eSCy Schubert "00000000000000000000000000000000000000000000000000000000131850E1"
750*e0c4386eSCy Schubert "F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
751*e0c4386eSCy Schubert "4", 571
752*e0c4386eSCy Schubert },
753*e0c4386eSCy Schubert /* Curve B-571 (FIPS PUB 186-2, App. 6) */
754*e0c4386eSCy Schubert {
755*e0c4386eSCy Schubert "NIST curve B-571",
756*e0c4386eSCy Schubert "800000000000000"
757*e0c4386eSCy Schubert "0000000000000000000000000000000000000000000000000000000000000000"
758*e0c4386eSCy Schubert "0000000000000000000000000000000000000000000000000000000000000425",
759*e0c4386eSCy Schubert "0000000000000000"
760*e0c4386eSCy Schubert "0000000000000000000000000000000000000000000000000000000000000000"
761*e0c4386eSCy Schubert "0000000000000000000000000000000000000000000000000000000000000001",
762*e0c4386eSCy Schubert "02F40E7E2221F295"
763*e0c4386eSCy Schubert "DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA5933"
764*e0c4386eSCy Schubert "2BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
765*e0c4386eSCy Schubert "0303001D34B85629"
766*e0c4386eSCy Schubert "6C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293"
767*e0c4386eSCy Schubert "CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
768*e0c4386eSCy Schubert "037BF27342DA639B"
769*e0c4386eSCy Schubert "6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A57"
770*e0c4386eSCy Schubert "6291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
771*e0c4386eSCy Schubert 1,
772*e0c4386eSCy Schubert "03FFFFFFFFFFFFFF"
773*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18"
774*e0c4386eSCy Schubert "FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
775*e0c4386eSCy Schubert "2", 571
776*e0c4386eSCy Schubert }
777*e0c4386eSCy Schubert };
778*e0c4386eSCy Schubert
char2_curve_test(int n)779*e0c4386eSCy Schubert static int char2_curve_test(int n)
780*e0c4386eSCy Schubert {
781*e0c4386eSCy Schubert int r = 0;
782*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
783*e0c4386eSCy Schubert BIGNUM *p = NULL, *a = NULL, *b = NULL;
784*e0c4386eSCy Schubert BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
785*e0c4386eSCy Schubert EC_GROUP *group = NULL;
786*e0c4386eSCy Schubert EC_POINT *P = NULL, *Q = NULL, *R = NULL;
787*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DEPRECATED_3_0
788*e0c4386eSCy Schubert const EC_POINT *points[3];
789*e0c4386eSCy Schubert const BIGNUM *scalars[3];
790*e0c4386eSCy Schubert # endif
791*e0c4386eSCy Schubert struct c2_curve_test *const test = char2_curve_tests + n;
792*e0c4386eSCy Schubert
793*e0c4386eSCy Schubert if (!TEST_ptr(ctx = BN_CTX_new())
794*e0c4386eSCy Schubert || !TEST_ptr(p = BN_new())
795*e0c4386eSCy Schubert || !TEST_ptr(a = BN_new())
796*e0c4386eSCy Schubert || !TEST_ptr(b = BN_new())
797*e0c4386eSCy Schubert || !TEST_ptr(x = BN_new())
798*e0c4386eSCy Schubert || !TEST_ptr(y = BN_new())
799*e0c4386eSCy Schubert || !TEST_ptr(z = BN_new())
800*e0c4386eSCy Schubert || !TEST_ptr(yplusone = BN_new())
801*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&p, test->p))
802*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&a, test->a))
803*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&b, test->b))
804*e0c4386eSCy Schubert || !TEST_true(group = EC_GROUP_new_curve_GF2m(p, a, b, ctx))
805*e0c4386eSCy Schubert || !TEST_ptr(P = EC_POINT_new(group))
806*e0c4386eSCy Schubert || !TEST_ptr(Q = EC_POINT_new(group))
807*e0c4386eSCy Schubert || !TEST_ptr(R = EC_POINT_new(group))
808*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&x, test->x))
809*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&y, test->y))
810*e0c4386eSCy Schubert || !TEST_true(BN_add(yplusone, y, BN_value_one())))
811*e0c4386eSCy Schubert goto err;
812*e0c4386eSCy Schubert
813*e0c4386eSCy Schubert /* Change test based on whether binary point compression is enabled or not. */
814*e0c4386eSCy Schubert # ifdef OPENSSL_EC_BIN_PT_COMP
815*e0c4386eSCy Schubert /*
816*e0c4386eSCy Schubert * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
817*e0c4386eSCy Schubert * and therefore setting the coordinates should fail.
818*e0c4386eSCy Schubert */
819*e0c4386eSCy Schubert if (!TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx))
820*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x,
821*e0c4386eSCy Schubert test->y_bit,
822*e0c4386eSCy Schubert ctx))
823*e0c4386eSCy Schubert || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
824*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&z, test->order))
825*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&cof, test->cof))
826*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(group, P, z, cof))
827*e0c4386eSCy Schubert || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
828*e0c4386eSCy Schubert goto err;
829*e0c4386eSCy Schubert TEST_info("%s -- Generator", test->name);
830*e0c4386eSCy Schubert test_output_bignum("x", x);
831*e0c4386eSCy Schubert test_output_bignum("y", y);
832*e0c4386eSCy Schubert /* G_y value taken from the standard: */
833*e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&z, test->y))
834*e0c4386eSCy Schubert || !TEST_BN_eq(y, z))
835*e0c4386eSCy Schubert goto err;
836*e0c4386eSCy Schubert # else
837*e0c4386eSCy Schubert /*
838*e0c4386eSCy Schubert * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
839*e0c4386eSCy Schubert * and therefore setting the coordinates should fail.
840*e0c4386eSCy Schubert */
841*e0c4386eSCy Schubert if (!TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx))
842*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
843*e0c4386eSCy Schubert || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
844*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&z, test->order))
845*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&cof, test->cof))
846*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(group, P, z, cof)))
847*e0c4386eSCy Schubert goto err;
848*e0c4386eSCy Schubert TEST_info("%s -- Generator:", test->name);
849*e0c4386eSCy Schubert test_output_bignum("x", x);
850*e0c4386eSCy Schubert test_output_bignum("y", y);
851*e0c4386eSCy Schubert # endif
852*e0c4386eSCy Schubert
853*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree)
854*e0c4386eSCy Schubert || !group_order_tests(group))
855*e0c4386eSCy Schubert goto err;
856*e0c4386eSCy Schubert
857*e0c4386eSCy Schubert /* more tests using the last curve */
858*e0c4386eSCy Schubert if (n == OSSL_NELEM(char2_curve_tests) - 1) {
859*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
860*e0c4386eSCy Schubert || !TEST_true(EC_POINT_copy(Q, P))
861*e0c4386eSCy Schubert || !TEST_false(EC_POINT_is_at_infinity(group, Q))
862*e0c4386eSCy Schubert || !TEST_true(EC_POINT_dbl(group, P, P, ctx))
863*e0c4386eSCy Schubert || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
864*e0c4386eSCy Schubert || !TEST_true(EC_POINT_invert(group, Q, ctx)) /* P = -2Q */
865*e0c4386eSCy Schubert || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))
866*e0c4386eSCy Schubert || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))
867*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, R)) /* R = P + 2Q */
868*e0c4386eSCy Schubert || !TEST_false(EC_POINT_is_at_infinity(group, Q)))
869*e0c4386eSCy Schubert goto err;
870*e0c4386eSCy Schubert
871*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DEPRECATED_3_0
872*e0c4386eSCy Schubert TEST_note("combined multiplication ...");
873*e0c4386eSCy Schubert points[0] = Q;
874*e0c4386eSCy Schubert points[1] = Q;
875*e0c4386eSCy Schubert points[2] = Q;
876*e0c4386eSCy Schubert
877*e0c4386eSCy Schubert if (!TEST_true(BN_add(y, z, BN_value_one()))
878*e0c4386eSCy Schubert || !TEST_BN_even(y)
879*e0c4386eSCy Schubert || !TEST_true(BN_rshift1(y, y)))
880*e0c4386eSCy Schubert goto err;
881*e0c4386eSCy Schubert scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
882*e0c4386eSCy Schubert scalars[1] = y;
883*e0c4386eSCy Schubert
884*e0c4386eSCy Schubert /* z is still the group order */
885*e0c4386eSCy Schubert if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
886*e0c4386eSCy Schubert || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
887*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
888*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx)))
889*e0c4386eSCy Schubert goto err;
890*e0c4386eSCy Schubert
891*e0c4386eSCy Schubert if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
892*e0c4386eSCy Schubert || !TEST_true(BN_add(z, z, y)))
893*e0c4386eSCy Schubert goto err;
894*e0c4386eSCy Schubert BN_set_negative(z, 1);
895*e0c4386eSCy Schubert scalars[0] = y;
896*e0c4386eSCy Schubert scalars[1] = z; /* z = -(order + y) */
897*e0c4386eSCy Schubert
898*e0c4386eSCy Schubert if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
899*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, P)))
900*e0c4386eSCy Schubert goto err;
901*e0c4386eSCy Schubert
902*e0c4386eSCy Schubert if (!TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))
903*e0c4386eSCy Schubert || !TEST_true(BN_add(z, x, y)))
904*e0c4386eSCy Schubert goto err;
905*e0c4386eSCy Schubert BN_set_negative(z, 1);
906*e0c4386eSCy Schubert scalars[0] = x;
907*e0c4386eSCy Schubert scalars[1] = y;
908*e0c4386eSCy Schubert scalars[2] = z; /* z = -(x+y) */
909*e0c4386eSCy Schubert
910*e0c4386eSCy Schubert if (!TEST_true(EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))
911*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, P)))
912*e0c4386eSCy Schubert goto err;
913*e0c4386eSCy Schubert # endif
914*e0c4386eSCy Schubert }
915*e0c4386eSCy Schubert
916*e0c4386eSCy Schubert r = 1;
917*e0c4386eSCy Schubert err:
918*e0c4386eSCy Schubert BN_CTX_free(ctx);
919*e0c4386eSCy Schubert BN_free(p);
920*e0c4386eSCy Schubert BN_free(a);
921*e0c4386eSCy Schubert BN_free(b);
922*e0c4386eSCy Schubert BN_free(x);
923*e0c4386eSCy Schubert BN_free(y);
924*e0c4386eSCy Schubert BN_free(z);
925*e0c4386eSCy Schubert BN_free(yplusone);
926*e0c4386eSCy Schubert BN_free(cof);
927*e0c4386eSCy Schubert EC_POINT_free(P);
928*e0c4386eSCy Schubert EC_POINT_free(Q);
929*e0c4386eSCy Schubert EC_POINT_free(R);
930*e0c4386eSCy Schubert EC_GROUP_free(group);
931*e0c4386eSCy Schubert return r;
932*e0c4386eSCy Schubert }
933*e0c4386eSCy Schubert
char2_field_tests(void)934*e0c4386eSCy Schubert static int char2_field_tests(void)
935*e0c4386eSCy Schubert {
936*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
937*e0c4386eSCy Schubert BIGNUM *p = NULL, *a = NULL, *b = NULL;
938*e0c4386eSCy Schubert EC_GROUP *group = NULL;
939*e0c4386eSCy Schubert EC_POINT *P = NULL, *Q = NULL, *R = NULL;
940*e0c4386eSCy Schubert BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
941*e0c4386eSCy Schubert unsigned char buf[100];
942*e0c4386eSCy Schubert size_t len;
943*e0c4386eSCy Schubert int k, r = 0;
944*e0c4386eSCy Schubert
945*e0c4386eSCy Schubert if (!TEST_ptr(ctx = BN_CTX_new())
946*e0c4386eSCy Schubert || !TEST_ptr(p = BN_new())
947*e0c4386eSCy Schubert || !TEST_ptr(a = BN_new())
948*e0c4386eSCy Schubert || !TEST_ptr(b = BN_new())
949*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&p, "13"))
950*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&a, "3"))
951*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&b, "1")))
952*e0c4386eSCy Schubert goto err;
953*e0c4386eSCy Schubert
954*e0c4386eSCy Schubert if (!TEST_ptr(group = EC_GROUP_new_curve_GF2m(p, a, b, ctx))
955*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx)))
956*e0c4386eSCy Schubert goto err;
957*e0c4386eSCy Schubert
958*e0c4386eSCy Schubert TEST_info("Curve defined by Weierstrass equation");
959*e0c4386eSCy Schubert TEST_note(" y^2 + x*y = x^3 + a*x^2 + b (mod p)");
960*e0c4386eSCy Schubert test_output_bignum("a", a);
961*e0c4386eSCy Schubert test_output_bignum("b", b);
962*e0c4386eSCy Schubert test_output_bignum("p", p);
963*e0c4386eSCy Schubert
964*e0c4386eSCy Schubert if (!TEST_ptr(P = EC_POINT_new(group))
965*e0c4386eSCy Schubert || !TEST_ptr(Q = EC_POINT_new(group))
966*e0c4386eSCy Schubert || !TEST_ptr(R = EC_POINT_new(group))
967*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_to_infinity(group, P))
968*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, P)))
969*e0c4386eSCy Schubert goto err;
970*e0c4386eSCy Schubert
971*e0c4386eSCy Schubert buf[0] = 0;
972*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx))
973*e0c4386eSCy Schubert || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))
974*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, P))
975*e0c4386eSCy Schubert || !TEST_ptr(x = BN_new())
976*e0c4386eSCy Schubert || !TEST_ptr(y = BN_new())
977*e0c4386eSCy Schubert || !TEST_ptr(z = BN_new())
978*e0c4386eSCy Schubert || !TEST_ptr(cof = BN_new())
979*e0c4386eSCy Schubert || !TEST_ptr(yplusone = BN_new())
980*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&x, "6"))
981*e0c4386eSCy Schubert /* Change test based on whether binary point compression is enabled or not. */
982*e0c4386eSCy Schubert # ifdef OPENSSL_EC_BIN_PT_COMP
983*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx))
984*e0c4386eSCy Schubert # else
985*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&y, "8"))
986*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_affine_coordinates(group, Q, x, y, ctx))
987*e0c4386eSCy Schubert # endif
988*e0c4386eSCy Schubert )
989*e0c4386eSCy Schubert goto err;
990*e0c4386eSCy Schubert if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) {
991*e0c4386eSCy Schubert /* Change test based on whether binary point compression is enabled or not. */
992*e0c4386eSCy Schubert # ifdef OPENSSL_EC_BIN_PT_COMP
993*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_get_affine_coordinates(group, Q, x, y, ctx)))
994*e0c4386eSCy Schubert goto err;
995*e0c4386eSCy Schubert # endif
996*e0c4386eSCy Schubert TEST_info("Point is not on curve");
997*e0c4386eSCy Schubert test_output_bignum("x", x);
998*e0c4386eSCy Schubert test_output_bignum("y", y);
999*e0c4386eSCy Schubert goto err;
1000*e0c4386eSCy Schubert }
1001*e0c4386eSCy Schubert
1002*e0c4386eSCy Schubert TEST_note("A cyclic subgroup:");
1003*e0c4386eSCy Schubert k = 100;
1004*e0c4386eSCy Schubert do {
1005*e0c4386eSCy Schubert if (!TEST_int_ne(k--, 0))
1006*e0c4386eSCy Schubert goto err;
1007*e0c4386eSCy Schubert
1008*e0c4386eSCy Schubert if (EC_POINT_is_at_infinity(group, P))
1009*e0c4386eSCy Schubert TEST_note(" point at infinity");
1010*e0c4386eSCy Schubert else {
1011*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y,
1012*e0c4386eSCy Schubert ctx)))
1013*e0c4386eSCy Schubert goto err;
1014*e0c4386eSCy Schubert
1015*e0c4386eSCy Schubert test_output_bignum("x", x);
1016*e0c4386eSCy Schubert test_output_bignum("y", y);
1017*e0c4386eSCy Schubert }
1018*e0c4386eSCy Schubert
1019*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_copy(R, P))
1020*e0c4386eSCy Schubert || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)))
1021*e0c4386eSCy Schubert goto err;
1022*e0c4386eSCy Schubert }
1023*e0c4386eSCy Schubert while (!EC_POINT_is_at_infinity(group, P));
1024*e0c4386eSCy Schubert
1025*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx))
1026*e0c4386eSCy Schubert || !TEST_true(EC_POINT_is_at_infinity(group, P)))
1027*e0c4386eSCy Schubert goto err;
1028*e0c4386eSCy Schubert
1029*e0c4386eSCy Schubert /* Change test based on whether binary point compression is enabled or not. */
1030*e0c4386eSCy Schubert # ifdef OPENSSL_EC_BIN_PT_COMP
1031*e0c4386eSCy Schubert len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED,
1032*e0c4386eSCy Schubert buf, sizeof(buf), ctx);
1033*e0c4386eSCy Schubert if (!TEST_size_t_ne(len, 0)
1034*e0c4386eSCy Schubert || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
1035*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
1036*e0c4386eSCy Schubert goto err;
1037*e0c4386eSCy Schubert test_output_memory("Generator as octet string, compressed form:",
1038*e0c4386eSCy Schubert buf, len);
1039*e0c4386eSCy Schubert # endif
1040*e0c4386eSCy Schubert
1041*e0c4386eSCy Schubert len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
1042*e0c4386eSCy Schubert buf, sizeof(buf), ctx);
1043*e0c4386eSCy Schubert if (!TEST_size_t_ne(len, 0)
1044*e0c4386eSCy Schubert || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
1045*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
1046*e0c4386eSCy Schubert goto err;
1047*e0c4386eSCy Schubert test_output_memory("Generator as octet string, uncompressed form:",
1048*e0c4386eSCy Schubert buf, len);
1049*e0c4386eSCy Schubert
1050*e0c4386eSCy Schubert /* Change test based on whether binary point compression is enabled or not. */
1051*e0c4386eSCy Schubert # ifdef OPENSSL_EC_BIN_PT_COMP
1052*e0c4386eSCy Schubert len =
1053*e0c4386eSCy Schubert EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof(buf),
1054*e0c4386eSCy Schubert ctx);
1055*e0c4386eSCy Schubert if (!TEST_size_t_ne(len, 0)
1056*e0c4386eSCy Schubert || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
1057*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
1058*e0c4386eSCy Schubert goto err;
1059*e0c4386eSCy Schubert test_output_memory("Generator as octet string, hybrid form:",
1060*e0c4386eSCy Schubert buf, len);
1061*e0c4386eSCy Schubert # endif
1062*e0c4386eSCy Schubert
1063*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_invert(group, P, ctx))
1064*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)))
1065*e0c4386eSCy Schubert goto err;
1066*e0c4386eSCy Schubert
1067*e0c4386eSCy Schubert TEST_note("\n");
1068*e0c4386eSCy Schubert
1069*e0c4386eSCy Schubert r = 1;
1070*e0c4386eSCy Schubert err:
1071*e0c4386eSCy Schubert BN_CTX_free(ctx);
1072*e0c4386eSCy Schubert BN_free(p);
1073*e0c4386eSCy Schubert BN_free(a);
1074*e0c4386eSCy Schubert BN_free(b);
1075*e0c4386eSCy Schubert EC_GROUP_free(group);
1076*e0c4386eSCy Schubert EC_POINT_free(P);
1077*e0c4386eSCy Schubert EC_POINT_free(Q);
1078*e0c4386eSCy Schubert EC_POINT_free(R);
1079*e0c4386eSCy Schubert BN_free(x);
1080*e0c4386eSCy Schubert BN_free(y);
1081*e0c4386eSCy Schubert BN_free(z);
1082*e0c4386eSCy Schubert BN_free(cof);
1083*e0c4386eSCy Schubert BN_free(yplusone);
1084*e0c4386eSCy Schubert return r;
1085*e0c4386eSCy Schubert }
1086*e0c4386eSCy Schubert
hybrid_point_encoding_test(void)1087*e0c4386eSCy Schubert static int hybrid_point_encoding_test(void)
1088*e0c4386eSCy Schubert {
1089*e0c4386eSCy Schubert BIGNUM *x = NULL, *y = NULL;
1090*e0c4386eSCy Schubert EC_GROUP *group = NULL;
1091*e0c4386eSCy Schubert EC_POINT *point = NULL;
1092*e0c4386eSCy Schubert unsigned char *buf = NULL;
1093*e0c4386eSCy Schubert size_t len;
1094*e0c4386eSCy Schubert int r = 0;
1095*e0c4386eSCy Schubert
1096*e0c4386eSCy Schubert if (!TEST_true(BN_dec2bn(&x, "0"))
1097*e0c4386eSCy Schubert || !TEST_true(BN_dec2bn(&y, "1"))
1098*e0c4386eSCy Schubert || !TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_sect571k1))
1099*e0c4386eSCy Schubert || !TEST_ptr(point = EC_POINT_new(group))
1100*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_affine_coordinates(group, point, x, y, NULL))
1101*e0c4386eSCy Schubert || !TEST_size_t_ne(0, (len = EC_POINT_point2oct(group,
1102*e0c4386eSCy Schubert point,
1103*e0c4386eSCy Schubert POINT_CONVERSION_HYBRID,
1104*e0c4386eSCy Schubert NULL,
1105*e0c4386eSCy Schubert 0,
1106*e0c4386eSCy Schubert NULL)))
1107*e0c4386eSCy Schubert || !TEST_ptr(buf = OPENSSL_malloc(len))
1108*e0c4386eSCy Schubert || !TEST_size_t_eq(len, EC_POINT_point2oct(group,
1109*e0c4386eSCy Schubert point,
1110*e0c4386eSCy Schubert POINT_CONVERSION_HYBRID,
1111*e0c4386eSCy Schubert buf,
1112*e0c4386eSCy Schubert len,
1113*e0c4386eSCy Schubert NULL)))
1114*e0c4386eSCy Schubert goto err;
1115*e0c4386eSCy Schubert
1116*e0c4386eSCy Schubert r = 1;
1117*e0c4386eSCy Schubert
1118*e0c4386eSCy Schubert /* buf contains a valid hybrid point, check that we can decode it. */
1119*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_oct2point(group, point, buf, len, NULL)))
1120*e0c4386eSCy Schubert r = 0;
1121*e0c4386eSCy Schubert
1122*e0c4386eSCy Schubert /* Flip the y_bit and verify that the invalid encoding is rejected. */
1123*e0c4386eSCy Schubert buf[0] ^= 1;
1124*e0c4386eSCy Schubert if (!TEST_false(EC_POINT_oct2point(group, point, buf, len, NULL)))
1125*e0c4386eSCy Schubert r = 0;
1126*e0c4386eSCy Schubert
1127*e0c4386eSCy Schubert err:
1128*e0c4386eSCy Schubert BN_free(x);
1129*e0c4386eSCy Schubert BN_free(y);
1130*e0c4386eSCy Schubert EC_GROUP_free(group);
1131*e0c4386eSCy Schubert EC_POINT_free(point);
1132*e0c4386eSCy Schubert OPENSSL_free(buf);
1133*e0c4386eSCy Schubert return r;
1134*e0c4386eSCy Schubert }
1135*e0c4386eSCy Schubert #endif
1136*e0c4386eSCy Schubert
internal_curve_test(int n)1137*e0c4386eSCy Schubert static int internal_curve_test(int n)
1138*e0c4386eSCy Schubert {
1139*e0c4386eSCy Schubert EC_GROUP *group = NULL;
1140*e0c4386eSCy Schubert int nid = curves[n].nid;
1141*e0c4386eSCy Schubert
1142*e0c4386eSCy Schubert if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {
1143*e0c4386eSCy Schubert TEST_info("EC_GROUP_new_curve_name() failed with curve %s\n",
1144*e0c4386eSCy Schubert OBJ_nid2sn(nid));
1145*e0c4386eSCy Schubert return 0;
1146*e0c4386eSCy Schubert }
1147*e0c4386eSCy Schubert if (!TEST_true(EC_GROUP_check(group, NULL))) {
1148*e0c4386eSCy Schubert TEST_info("EC_GROUP_check() failed with curve %s\n", OBJ_nid2sn(nid));
1149*e0c4386eSCy Schubert EC_GROUP_free(group);
1150*e0c4386eSCy Schubert return 0;
1151*e0c4386eSCy Schubert }
1152*e0c4386eSCy Schubert EC_GROUP_free(group);
1153*e0c4386eSCy Schubert return 1;
1154*e0c4386eSCy Schubert }
1155*e0c4386eSCy Schubert
internal_curve_test_method(int n)1156*e0c4386eSCy Schubert static int internal_curve_test_method(int n)
1157*e0c4386eSCy Schubert {
1158*e0c4386eSCy Schubert int r, nid = curves[n].nid;
1159*e0c4386eSCy Schubert EC_GROUP *group;
1160*e0c4386eSCy Schubert
1161*e0c4386eSCy Schubert if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {
1162*e0c4386eSCy Schubert TEST_info("Curve %s failed\n", OBJ_nid2sn(nid));
1163*e0c4386eSCy Schubert return 0;
1164*e0c4386eSCy Schubert }
1165*e0c4386eSCy Schubert r = group_order_tests(group);
1166*e0c4386eSCy Schubert EC_GROUP_free(group);
1167*e0c4386eSCy Schubert return r;
1168*e0c4386eSCy Schubert }
1169*e0c4386eSCy Schubert
group_field_test(void)1170*e0c4386eSCy Schubert static int group_field_test(void)
1171*e0c4386eSCy Schubert {
1172*e0c4386eSCy Schubert int r = 1;
1173*e0c4386eSCy Schubert BIGNUM *secp521r1_field = NULL;
1174*e0c4386eSCy Schubert BIGNUM *sect163r2_field = NULL;
1175*e0c4386eSCy Schubert EC_GROUP *secp521r1_group = NULL;
1176*e0c4386eSCy Schubert EC_GROUP *sect163r2_group = NULL;
1177*e0c4386eSCy Schubert
1178*e0c4386eSCy Schubert BN_hex2bn(&secp521r1_field,
1179*e0c4386eSCy Schubert "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
1180*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
1181*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
1182*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
1183*e0c4386eSCy Schubert "FFFF");
1184*e0c4386eSCy Schubert
1185*e0c4386eSCy Schubert
1186*e0c4386eSCy Schubert BN_hex2bn(§163r2_field,
1187*e0c4386eSCy Schubert "08000000000000000000000000000000"
1188*e0c4386eSCy Schubert "00000000C9");
1189*e0c4386eSCy Schubert
1190*e0c4386eSCy Schubert secp521r1_group = EC_GROUP_new_by_curve_name(NID_secp521r1);
1191*e0c4386eSCy Schubert if (BN_cmp(secp521r1_field, EC_GROUP_get0_field(secp521r1_group)))
1192*e0c4386eSCy Schubert r = 0;
1193*e0c4386eSCy Schubert
1194*e0c4386eSCy Schubert # ifndef OPENSSL_NO_EC2M
1195*e0c4386eSCy Schubert sect163r2_group = EC_GROUP_new_by_curve_name(NID_sect163r2);
1196*e0c4386eSCy Schubert if (BN_cmp(sect163r2_field, EC_GROUP_get0_field(sect163r2_group)))
1197*e0c4386eSCy Schubert r = 0;
1198*e0c4386eSCy Schubert # endif
1199*e0c4386eSCy Schubert
1200*e0c4386eSCy Schubert EC_GROUP_free(secp521r1_group);
1201*e0c4386eSCy Schubert EC_GROUP_free(sect163r2_group);
1202*e0c4386eSCy Schubert BN_free(secp521r1_field);
1203*e0c4386eSCy Schubert BN_free(sect163r2_field);
1204*e0c4386eSCy Schubert return r;
1205*e0c4386eSCy Schubert }
1206*e0c4386eSCy Schubert
1207*e0c4386eSCy Schubert /*
1208*e0c4386eSCy Schubert * nistp_test_params contains magic numbers for testing
1209*e0c4386eSCy Schubert * several NIST curves with characteristic > 3.
1210*e0c4386eSCy Schubert */
1211*e0c4386eSCy Schubert struct nistp_test_params {
1212*e0c4386eSCy Schubert const int nid;
1213*e0c4386eSCy Schubert int degree;
1214*e0c4386eSCy Schubert /*
1215*e0c4386eSCy Schubert * Qx, Qy and D are taken from
1216*e0c4386eSCy Schubert * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
1217*e0c4386eSCy Schubert * Otherwise, values are standard curve parameters from FIPS 180-3
1218*e0c4386eSCy Schubert */
1219*e0c4386eSCy Schubert const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
1220*e0c4386eSCy Schubert };
1221*e0c4386eSCy Schubert
1222*e0c4386eSCy Schubert static const struct nistp_test_params nistp_tests_params[] = {
1223*e0c4386eSCy Schubert {
1224*e0c4386eSCy Schubert /* P-224 */
1225*e0c4386eSCy Schubert NID_secp224r1,
1226*e0c4386eSCy Schubert 224,
1227*e0c4386eSCy Schubert /* p */
1228*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
1229*e0c4386eSCy Schubert /* a */
1230*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
1231*e0c4386eSCy Schubert /* b */
1232*e0c4386eSCy Schubert "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
1233*e0c4386eSCy Schubert /* Qx */
1234*e0c4386eSCy Schubert "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E",
1235*e0c4386eSCy Schubert /* Qy */
1236*e0c4386eSCy Schubert "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555",
1237*e0c4386eSCy Schubert /* Gx */
1238*e0c4386eSCy Schubert "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
1239*e0c4386eSCy Schubert /* Gy */
1240*e0c4386eSCy Schubert "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
1241*e0c4386eSCy Schubert /* order */
1242*e0c4386eSCy Schubert "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
1243*e0c4386eSCy Schubert /* d */
1244*e0c4386eSCy Schubert "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8",
1245*e0c4386eSCy Schubert },
1246*e0c4386eSCy Schubert {
1247*e0c4386eSCy Schubert /* P-256 */
1248*e0c4386eSCy Schubert NID_X9_62_prime256v1,
1249*e0c4386eSCy Schubert 256,
1250*e0c4386eSCy Schubert /* p */
1251*e0c4386eSCy Schubert "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
1252*e0c4386eSCy Schubert /* a */
1253*e0c4386eSCy Schubert "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
1254*e0c4386eSCy Schubert /* b */
1255*e0c4386eSCy Schubert "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
1256*e0c4386eSCy Schubert /* Qx */
1257*e0c4386eSCy Schubert "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19",
1258*e0c4386eSCy Schubert /* Qy */
1259*e0c4386eSCy Schubert "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09",
1260*e0c4386eSCy Schubert /* Gx */
1261*e0c4386eSCy Schubert "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
1262*e0c4386eSCy Schubert /* Gy */
1263*e0c4386eSCy Schubert "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
1264*e0c4386eSCy Schubert /* order */
1265*e0c4386eSCy Schubert "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
1266*e0c4386eSCy Schubert /* d */
1267*e0c4386eSCy Schubert "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96",
1268*e0c4386eSCy Schubert },
1269*e0c4386eSCy Schubert {
1270*e0c4386eSCy Schubert /* P-521 */
1271*e0c4386eSCy Schubert NID_secp521r1,
1272*e0c4386eSCy Schubert 521,
1273*e0c4386eSCy Schubert /* p */
1274*e0c4386eSCy Schubert "1ff"
1275*e0c4386eSCy Schubert "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
1276*e0c4386eSCy Schubert "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
1277*e0c4386eSCy Schubert /* a */
1278*e0c4386eSCy Schubert "1ff"
1279*e0c4386eSCy Schubert "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
1280*e0c4386eSCy Schubert "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
1281*e0c4386eSCy Schubert /* b */
1282*e0c4386eSCy Schubert "051"
1283*e0c4386eSCy Schubert "953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e1"
1284*e0c4386eSCy Schubert "56193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
1285*e0c4386eSCy Schubert /* Qx */
1286*e0c4386eSCy Schubert "0098"
1287*e0c4386eSCy Schubert "e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e"
1288*e0c4386eSCy Schubert "59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4",
1289*e0c4386eSCy Schubert /* Qy */
1290*e0c4386eSCy Schubert "0164"
1291*e0c4386eSCy Schubert "350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8"
1292*e0c4386eSCy Schubert "554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e",
1293*e0c4386eSCy Schubert /* Gx */
1294*e0c4386eSCy Schubert "c6"
1295*e0c4386eSCy Schubert "858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dba"
1296*e0c4386eSCy Schubert "a14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
1297*e0c4386eSCy Schubert /* Gy */
1298*e0c4386eSCy Schubert "118"
1299*e0c4386eSCy Schubert "39296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c"
1300*e0c4386eSCy Schubert "97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
1301*e0c4386eSCy Schubert /* order */
1302*e0c4386eSCy Schubert "1ff"
1303*e0c4386eSCy Schubert "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa"
1304*e0c4386eSCy Schubert "51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
1305*e0c4386eSCy Schubert /* d */
1306*e0c4386eSCy Schubert "0100"
1307*e0c4386eSCy Schubert "085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eee"
1308*e0c4386eSCy Schubert "df09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722",
1309*e0c4386eSCy Schubert },
1310*e0c4386eSCy Schubert };
1311*e0c4386eSCy Schubert
nistp_single_test(int idx)1312*e0c4386eSCy Schubert static int nistp_single_test(int idx)
1313*e0c4386eSCy Schubert {
1314*e0c4386eSCy Schubert const struct nistp_test_params *test = nistp_tests_params + idx;
1315*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
1316*e0c4386eSCy Schubert BIGNUM *p = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL;
1317*e0c4386eSCy Schubert BIGNUM *n = NULL, *m = NULL, *order = NULL, *yplusone = NULL;
1318*e0c4386eSCy Schubert EC_GROUP *NISTP = NULL;
1319*e0c4386eSCy Schubert EC_POINT *G = NULL, *P = NULL, *Q = NULL, *Q_CHECK = NULL;
1320*e0c4386eSCy Schubert int r = 0;
1321*e0c4386eSCy Schubert
1322*e0c4386eSCy Schubert TEST_note("NIST curve P-%d (optimised implementation):",
1323*e0c4386eSCy Schubert test->degree);
1324*e0c4386eSCy Schubert if (!TEST_ptr(ctx = BN_CTX_new())
1325*e0c4386eSCy Schubert || !TEST_ptr(p = BN_new())
1326*e0c4386eSCy Schubert || !TEST_ptr(a = BN_new())
1327*e0c4386eSCy Schubert || !TEST_ptr(b = BN_new())
1328*e0c4386eSCy Schubert || !TEST_ptr(x = BN_new())
1329*e0c4386eSCy Schubert || !TEST_ptr(y = BN_new())
1330*e0c4386eSCy Schubert || !TEST_ptr(m = BN_new())
1331*e0c4386eSCy Schubert || !TEST_ptr(n = BN_new())
1332*e0c4386eSCy Schubert || !TEST_ptr(order = BN_new())
1333*e0c4386eSCy Schubert || !TEST_ptr(yplusone = BN_new())
1334*e0c4386eSCy Schubert
1335*e0c4386eSCy Schubert || !TEST_ptr(NISTP = EC_GROUP_new_by_curve_name(test->nid))
1336*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&p, test->p))
1337*e0c4386eSCy Schubert || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
1338*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&a, test->a))
1339*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&b, test->b))
1340*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_curve(NISTP, p, a, b, ctx))
1341*e0c4386eSCy Schubert || !TEST_ptr(G = EC_POINT_new(NISTP))
1342*e0c4386eSCy Schubert || !TEST_ptr(P = EC_POINT_new(NISTP))
1343*e0c4386eSCy Schubert || !TEST_ptr(Q = EC_POINT_new(NISTP))
1344*e0c4386eSCy Schubert || !TEST_ptr(Q_CHECK = EC_POINT_new(NISTP))
1345*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&x, test->Qx))
1346*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&y, test->Qy))
1347*e0c4386eSCy Schubert || !TEST_true(BN_add(yplusone, y, BN_value_one()))
1348*e0c4386eSCy Schubert /*
1349*e0c4386eSCy Schubert * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
1350*e0c4386eSCy Schubert * and therefore setting the coordinates should fail.
1351*e0c4386eSCy Schubert */
1352*e0c4386eSCy Schubert || !TEST_false(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x,
1353*e0c4386eSCy Schubert yplusone, ctx))
1354*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x, y,
1355*e0c4386eSCy Schubert ctx))
1356*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&x, test->Gx))
1357*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&y, test->Gy))
1358*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, G, x, y, ctx))
1359*e0c4386eSCy Schubert || !TEST_true(BN_hex2bn(&order, test->order))
1360*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
1361*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_degree(NISTP), test->degree))
1362*e0c4386eSCy Schubert goto err;
1363*e0c4386eSCy Schubert
1364*e0c4386eSCy Schubert TEST_note("NIST test vectors ... ");
1365*e0c4386eSCy Schubert if (!TEST_true(BN_hex2bn(&n, test->d)))
1366*e0c4386eSCy Schubert goto err;
1367*e0c4386eSCy Schubert /* fixed point multiplication */
1368*e0c4386eSCy Schubert EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
1369*e0c4386eSCy Schubert if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
1370*e0c4386eSCy Schubert goto err;
1371*e0c4386eSCy Schubert /* random point multiplication */
1372*e0c4386eSCy Schubert EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
1373*e0c4386eSCy Schubert if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
1374*e0c4386eSCy Schubert
1375*e0c4386eSCy Schubert /* set generator to P = 2*G, where G is the standard generator */
1376*e0c4386eSCy Schubert || !TEST_true(EC_POINT_dbl(NISTP, P, G, ctx))
1377*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(NISTP, P, order, BN_value_one()))
1378*e0c4386eSCy Schubert /* set the scalar to m=n/2, where n is the NIST test scalar */
1379*e0c4386eSCy Schubert || !TEST_true(BN_rshift(m, n, 1)))
1380*e0c4386eSCy Schubert goto err;
1381*e0c4386eSCy Schubert
1382*e0c4386eSCy Schubert /* test the non-standard generator */
1383*e0c4386eSCy Schubert /* fixed point multiplication */
1384*e0c4386eSCy Schubert EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
1385*e0c4386eSCy Schubert if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
1386*e0c4386eSCy Schubert goto err;
1387*e0c4386eSCy Schubert /* random point multiplication */
1388*e0c4386eSCy Schubert EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
1389*e0c4386eSCy Schubert if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
1390*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DEPRECATED_3_0
1391*e0c4386eSCy Schubert /* We have not performed precomp so this should be false */
1392*e0c4386eSCy Schubert || !TEST_false(EC_GROUP_have_precompute_mult(NISTP))
1393*e0c4386eSCy Schubert /* now repeat all tests with precomputation */
1394*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_precompute_mult(NISTP, ctx))
1395*e0c4386eSCy Schubert #endif
1396*e0c4386eSCy Schubert )
1397*e0c4386eSCy Schubert goto err;
1398*e0c4386eSCy Schubert
1399*e0c4386eSCy Schubert /* fixed point multiplication */
1400*e0c4386eSCy Schubert EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
1401*e0c4386eSCy Schubert if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
1402*e0c4386eSCy Schubert goto err;
1403*e0c4386eSCy Schubert /* random point multiplication */
1404*e0c4386eSCy Schubert EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
1405*e0c4386eSCy Schubert if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
1406*e0c4386eSCy Schubert
1407*e0c4386eSCy Schubert /* reset generator */
1408*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one())))
1409*e0c4386eSCy Schubert goto err;
1410*e0c4386eSCy Schubert /* fixed point multiplication */
1411*e0c4386eSCy Schubert EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
1412*e0c4386eSCy Schubert if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
1413*e0c4386eSCy Schubert goto err;
1414*e0c4386eSCy Schubert /* random point multiplication */
1415*e0c4386eSCy Schubert EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
1416*e0c4386eSCy Schubert if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
1417*e0c4386eSCy Schubert goto err;
1418*e0c4386eSCy Schubert
1419*e0c4386eSCy Schubert /* regression test for felem_neg bug */
1420*e0c4386eSCy Schubert if (!TEST_true(BN_set_word(m, 32))
1421*e0c4386eSCy Schubert || !TEST_true(BN_set_word(n, 31))
1422*e0c4386eSCy Schubert || !TEST_true(EC_POINT_copy(P, G))
1423*e0c4386eSCy Schubert || !TEST_true(EC_POINT_invert(NISTP, P, ctx))
1424*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(NISTP, Q, m, P, n, ctx))
1425*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, G, ctx)))
1426*e0c4386eSCy Schubert goto err;
1427*e0c4386eSCy Schubert
1428*e0c4386eSCy Schubert r = 1;
1429*e0c4386eSCy Schubert err:
1430*e0c4386eSCy Schubert EC_GROUP_free(NISTP);
1431*e0c4386eSCy Schubert EC_POINT_free(G);
1432*e0c4386eSCy Schubert EC_POINT_free(P);
1433*e0c4386eSCy Schubert EC_POINT_free(Q);
1434*e0c4386eSCy Schubert EC_POINT_free(Q_CHECK);
1435*e0c4386eSCy Schubert BN_free(n);
1436*e0c4386eSCy Schubert BN_free(m);
1437*e0c4386eSCy Schubert BN_free(p);
1438*e0c4386eSCy Schubert BN_free(a);
1439*e0c4386eSCy Schubert BN_free(b);
1440*e0c4386eSCy Schubert BN_free(x);
1441*e0c4386eSCy Schubert BN_free(y);
1442*e0c4386eSCy Schubert BN_free(order);
1443*e0c4386eSCy Schubert BN_free(yplusone);
1444*e0c4386eSCy Schubert BN_CTX_free(ctx);
1445*e0c4386eSCy Schubert return r;
1446*e0c4386eSCy Schubert }
1447*e0c4386eSCy Schubert
1448*e0c4386eSCy Schubert static const unsigned char p521_named[] = {
1449*e0c4386eSCy Schubert 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23,
1450*e0c4386eSCy Schubert };
1451*e0c4386eSCy Schubert
1452*e0c4386eSCy Schubert static const unsigned char p521_explicit[] = {
1453*e0c4386eSCy Schubert 0x30, 0x82, 0x01, 0xc3, 0x02, 0x01, 0x01, 0x30, 0x4d, 0x06, 0x07, 0x2a,
1454*e0c4386eSCy Schubert 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x42, 0x01, 0xff, 0xff, 0xff,
1455*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1456*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1457*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1458*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1459*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1460*e0c4386eSCy Schubert 0xff, 0xff, 0x30, 0x81, 0x9f, 0x04, 0x42, 0x01, 0xff, 0xff, 0xff, 0xff,
1461*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1462*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1463*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1464*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1465*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1466*e0c4386eSCy Schubert 0xfc, 0x04, 0x42, 0x00, 0x51, 0x95, 0x3e, 0xb9, 0x61, 0x8e, 0x1c, 0x9a,
1467*e0c4386eSCy Schubert 0x1f, 0x92, 0x9a, 0x21, 0xa0, 0xb6, 0x85, 0x40, 0xee, 0xa2, 0xda, 0x72,
1468*e0c4386eSCy Schubert 0x5b, 0x99, 0xb3, 0x15, 0xf3, 0xb8, 0xb4, 0x89, 0x91, 0x8e, 0xf1, 0x09,
1469*e0c4386eSCy Schubert 0xe1, 0x56, 0x19, 0x39, 0x51, 0xec, 0x7e, 0x93, 0x7b, 0x16, 0x52, 0xc0,
1470*e0c4386eSCy Schubert 0xbd, 0x3b, 0xb1, 0xbf, 0x07, 0x35, 0x73, 0xdf, 0x88, 0x3d, 0x2c, 0x34,
1471*e0c4386eSCy Schubert 0xf1, 0xef, 0x45, 0x1f, 0xd4, 0x6b, 0x50, 0x3f, 0x00, 0x03, 0x15, 0x00,
1472*e0c4386eSCy Schubert 0xd0, 0x9e, 0x88, 0x00, 0x29, 0x1c, 0xb8, 0x53, 0x96, 0xcc, 0x67, 0x17,
1473*e0c4386eSCy Schubert 0x39, 0x32, 0x84, 0xaa, 0xa0, 0xda, 0x64, 0xba, 0x04, 0x81, 0x85, 0x04,
1474*e0c4386eSCy Schubert 0x00, 0xc6, 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04, 0xe9, 0xcd, 0x9e, 0x3e,
1475*e0c4386eSCy Schubert 0xcb, 0x66, 0x23, 0x95, 0xb4, 0x42, 0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f,
1476*e0c4386eSCy Schubert 0xb5, 0x21, 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d, 0x3d, 0xba, 0xa1, 0x4b,
1477*e0c4386eSCy Schubert 0x5e, 0x77, 0xef, 0xe7, 0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff,
1478*e0c4386eSCy Schubert 0xa8, 0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a, 0x42, 0x9b, 0xf9, 0x7e,
1479*e0c4386eSCy Schubert 0x7e, 0x31, 0xc2, 0xe5, 0xbd, 0x66, 0x01, 0x18, 0x39, 0x29, 0x6a, 0x78,
1480*e0c4386eSCy Schubert 0x9a, 0x3b, 0xc0, 0x04, 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d, 0x1b, 0xd9,
1481*e0c4386eSCy Schubert 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b, 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17,
1482*e0c4386eSCy Schubert 0x27, 0x3e, 0x66, 0x2c, 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4, 0x26, 0x40,
1483*e0c4386eSCy Schubert 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad, 0x07, 0x61, 0x35, 0x3c, 0x70, 0x86,
1484*e0c4386eSCy Schubert 0xa2, 0x72, 0xc2, 0x40, 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1, 0x66, 0x50,
1485*e0c4386eSCy Schubert 0x02, 0x42, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1486*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1487*e0c4386eSCy Schubert 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,
1488*e0c4386eSCy Schubert 0x51, 0x86, 0x87, 0x83, 0xbf, 0x2f, 0x96, 0x6b, 0x7f, 0xcc, 0x01, 0x48,
1489*e0c4386eSCy Schubert 0xf7, 0x09, 0xa5, 0xd0, 0x3b, 0xb5, 0xc9, 0xb8, 0x89, 0x9c, 0x47, 0xae,
1490*e0c4386eSCy Schubert 0xbb, 0x6f, 0xb7, 0x1e, 0x91, 0x38, 0x64, 0x09, 0x02, 0x01, 0x01,
1491*e0c4386eSCy Schubert };
1492*e0c4386eSCy Schubert
1493*e0c4386eSCy Schubert /*
1494*e0c4386eSCy Schubert * This test validates a named curve's group parameters using
1495*e0c4386eSCy Schubert * EC_GROUP_check_named_curve(). It also checks that modifying any of the
1496*e0c4386eSCy Schubert * group parameters results in the curve not being valid.
1497*e0c4386eSCy Schubert */
check_named_curve_test(int id)1498*e0c4386eSCy Schubert static int check_named_curve_test(int id)
1499*e0c4386eSCy Schubert {
1500*e0c4386eSCy Schubert int ret = 0, nid, field_nid, has_seed;
1501*e0c4386eSCy Schubert EC_GROUP *group = NULL, *gtest = NULL;
1502*e0c4386eSCy Schubert const EC_POINT *group_gen = NULL;
1503*e0c4386eSCy Schubert EC_POINT *other_gen = NULL;
1504*e0c4386eSCy Schubert BIGNUM *group_p = NULL, *group_a = NULL, *group_b = NULL;
1505*e0c4386eSCy Schubert BIGNUM *other_p = NULL, *other_a = NULL, *other_b = NULL;
1506*e0c4386eSCy Schubert BIGNUM *group_cofactor = NULL, *other_cofactor = NULL;
1507*e0c4386eSCy Schubert BIGNUM *other_order = NULL;
1508*e0c4386eSCy Schubert const BIGNUM *group_order = NULL;
1509*e0c4386eSCy Schubert BN_CTX *bn_ctx = NULL;
1510*e0c4386eSCy Schubert static const unsigned char invalid_seed[] = "THIS IS NOT A VALID SEED";
1511*e0c4386eSCy Schubert static size_t invalid_seed_len = sizeof(invalid_seed);
1512*e0c4386eSCy Schubert
1513*e0c4386eSCy Schubert /* Do some setup */
1514*e0c4386eSCy Schubert nid = curves[id].nid;
1515*e0c4386eSCy Schubert if (!TEST_ptr(bn_ctx = BN_CTX_new())
1516*e0c4386eSCy Schubert || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
1517*e0c4386eSCy Schubert || !TEST_ptr(gtest = EC_GROUP_dup(group))
1518*e0c4386eSCy Schubert || !TEST_ptr(group_p = BN_new())
1519*e0c4386eSCy Schubert || !TEST_ptr(group_a = BN_new())
1520*e0c4386eSCy Schubert || !TEST_ptr(group_b = BN_new())
1521*e0c4386eSCy Schubert || !TEST_ptr(group_cofactor = BN_new())
1522*e0c4386eSCy Schubert || !TEST_ptr(group_gen = EC_GROUP_get0_generator(group))
1523*e0c4386eSCy Schubert || !TEST_ptr(group_order = EC_GROUP_get0_order(group))
1524*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_get_cofactor(group, group_cofactor, NULL))
1525*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_get_curve(group, group_p, group_a, group_b, NULL))
1526*e0c4386eSCy Schubert || !TEST_ptr(other_gen = EC_POINT_dup(group_gen, group))
1527*e0c4386eSCy Schubert || !TEST_true(EC_POINT_add(group, other_gen, group_gen, group_gen, NULL))
1528*e0c4386eSCy Schubert || !TEST_ptr(other_order = BN_dup(group_order))
1529*e0c4386eSCy Schubert || !TEST_true(BN_add_word(other_order, 1))
1530*e0c4386eSCy Schubert || !TEST_ptr(other_a = BN_dup(group_a))
1531*e0c4386eSCy Schubert || !TEST_true(BN_add_word(other_a, 1))
1532*e0c4386eSCy Schubert || !TEST_ptr(other_b = BN_dup(group_b))
1533*e0c4386eSCy Schubert || !TEST_true(BN_add_word(other_b, 1))
1534*e0c4386eSCy Schubert || !TEST_ptr(other_cofactor = BN_dup(group_cofactor))
1535*e0c4386eSCy Schubert || !TEST_true(BN_add_word(other_cofactor, 1)))
1536*e0c4386eSCy Schubert goto err;
1537*e0c4386eSCy Schubert
1538*e0c4386eSCy Schubert /* Determine if the built-in curve has a seed field set */
1539*e0c4386eSCy Schubert has_seed = (EC_GROUP_get_seed_len(group) > 0);
1540*e0c4386eSCy Schubert field_nid = EC_GROUP_get_field_type(group);
1541*e0c4386eSCy Schubert if (field_nid == NID_X9_62_characteristic_two_field) {
1542*e0c4386eSCy Schubert if (!TEST_ptr(other_p = BN_dup(group_p))
1543*e0c4386eSCy Schubert || !TEST_true(BN_lshift1(other_p, other_p)))
1544*e0c4386eSCy Schubert goto err;
1545*e0c4386eSCy Schubert } else {
1546*e0c4386eSCy Schubert if (!TEST_ptr(other_p = BN_dup(group_p)))
1547*e0c4386eSCy Schubert goto err;
1548*e0c4386eSCy Schubert /*
1549*e0c4386eSCy Schubert * Just choosing any arbitrary prime does not work..
1550*e0c4386eSCy Schubert * Setting p via ec_GFp_nist_group_set_curve() needs the prime to be a
1551*e0c4386eSCy Schubert * nist prime. So only select one of these as an alternate prime.
1552*e0c4386eSCy Schubert */
1553*e0c4386eSCy Schubert if (!TEST_ptr(BN_copy(other_p,
1554*e0c4386eSCy Schubert BN_ucmp(BN_get0_nist_prime_192(), other_p) == 0 ?
1555*e0c4386eSCy Schubert BN_get0_nist_prime_256() :
1556*e0c4386eSCy Schubert BN_get0_nist_prime_192())))
1557*e0c4386eSCy Schubert goto err;
1558*e0c4386eSCy Schubert }
1559*e0c4386eSCy Schubert
1560*e0c4386eSCy Schubert /* Passes because this is a valid curve */
1561*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid)
1562*e0c4386eSCy Schubert /* Only NIST curves pass */
1563*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_check_named_curve(group, 1, NULL),
1564*e0c4386eSCy Schubert EC_curve_nid2nist(nid) != NULL ? nid : NID_undef))
1565*e0c4386eSCy Schubert goto err;
1566*e0c4386eSCy Schubert
1567*e0c4386eSCy Schubert /* Fail if the curve name doesn't match the parameters */
1568*e0c4386eSCy Schubert EC_GROUP_set_curve_name(group, nid + 1);
1569*e0c4386eSCy Schubert ERR_set_mark();
1570*e0c4386eSCy Schubert if (!TEST_int_le(EC_GROUP_check_named_curve(group, 0, NULL), 0))
1571*e0c4386eSCy Schubert goto err;
1572*e0c4386eSCy Schubert ERR_pop_to_mark();
1573*e0c4386eSCy Schubert
1574*e0c4386eSCy Schubert /* Restore curve name and ensure it's passing */
1575*e0c4386eSCy Schubert EC_GROUP_set_curve_name(group, nid);
1576*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid))
1577*e0c4386eSCy Schubert goto err;
1578*e0c4386eSCy Schubert
1579*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_set_seed(group, invalid_seed, invalid_seed_len),
1580*e0c4386eSCy Schubert invalid_seed_len))
1581*e0c4386eSCy Schubert goto err;
1582*e0c4386eSCy Schubert
1583*e0c4386eSCy Schubert if (has_seed) {
1584*e0c4386eSCy Schubert /*
1585*e0c4386eSCy Schubert * If the built-in curve has a seed and we set the seed to another value
1586*e0c4386eSCy Schubert * then it will fail the check.
1587*e0c4386eSCy Schubert */
1588*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), 0))
1589*e0c4386eSCy Schubert goto err;
1590*e0c4386eSCy Schubert } else {
1591*e0c4386eSCy Schubert /*
1592*e0c4386eSCy Schubert * If the built-in curve does not have a seed then setting the seed will
1593*e0c4386eSCy Schubert * pass the check (as the seed is optional).
1594*e0c4386eSCy Schubert */
1595*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid))
1596*e0c4386eSCy Schubert goto err;
1597*e0c4386eSCy Schubert }
1598*e0c4386eSCy Schubert /* Pass if the seed is unknown (as it is optional) */
1599*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_set_seed(group, NULL, 0), 1)
1600*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid))
1601*e0c4386eSCy Schubert goto err;
1602*e0c4386eSCy Schubert
1603*e0c4386eSCy Schubert /* Check that a duped group passes */
1604*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid))
1605*e0c4386eSCy Schubert goto err;
1606*e0c4386eSCy Schubert
1607*e0c4386eSCy Schubert /* check that changing any generator parameter fails */
1608*e0c4386eSCy Schubert if (!TEST_true(EC_GROUP_set_generator(gtest, other_gen, group_order,
1609*e0c4386eSCy Schubert group_cofactor))
1610*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)
1611*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, other_order,
1612*e0c4386eSCy Schubert group_cofactor))
1613*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)
1614*e0c4386eSCy Schubert /* The order is not an optional field, so this should fail */
1615*e0c4386eSCy Schubert || !TEST_false(EC_GROUP_set_generator(gtest, group_gen, NULL,
1616*e0c4386eSCy Schubert group_cofactor))
1617*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order,
1618*e0c4386eSCy Schubert other_cofactor))
1619*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)
1620*e0c4386eSCy Schubert /* Check that if the cofactor is not set then it still passes */
1621*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order,
1622*e0c4386eSCy Schubert NULL))
1623*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid)
1624*e0c4386eSCy Schubert /* check that restoring the generator passes */
1625*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order,
1626*e0c4386eSCy Schubert group_cofactor))
1627*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid))
1628*e0c4386eSCy Schubert goto err;
1629*e0c4386eSCy Schubert
1630*e0c4386eSCy Schubert /*
1631*e0c4386eSCy Schubert * check that changing any curve parameter fails
1632*e0c4386eSCy Schubert *
1633*e0c4386eSCy Schubert * Setting arbitrary p, a or b might fail for some EC_GROUPs
1634*e0c4386eSCy Schubert * depending on the internal EC_METHOD implementation, hence run
1635*e0c4386eSCy Schubert * these tests conditionally to the success of EC_GROUP_set_curve().
1636*e0c4386eSCy Schubert */
1637*e0c4386eSCy Schubert ERR_set_mark();
1638*e0c4386eSCy Schubert if (EC_GROUP_set_curve(gtest, other_p, group_a, group_b, NULL)) {
1639*e0c4386eSCy Schubert if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0))
1640*e0c4386eSCy Schubert goto err;
1641*e0c4386eSCy Schubert } else {
1642*e0c4386eSCy Schubert /* clear the error stack if EC_GROUP_set_curve() failed */
1643*e0c4386eSCy Schubert ERR_pop_to_mark();
1644*e0c4386eSCy Schubert ERR_set_mark();
1645*e0c4386eSCy Schubert }
1646*e0c4386eSCy Schubert if (EC_GROUP_set_curve(gtest, group_p, other_a, group_b, NULL)) {
1647*e0c4386eSCy Schubert if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0))
1648*e0c4386eSCy Schubert goto err;
1649*e0c4386eSCy Schubert } else {
1650*e0c4386eSCy Schubert /* clear the error stack if EC_GROUP_set_curve() failed */
1651*e0c4386eSCy Schubert ERR_pop_to_mark();
1652*e0c4386eSCy Schubert ERR_set_mark();
1653*e0c4386eSCy Schubert }
1654*e0c4386eSCy Schubert if (EC_GROUP_set_curve(gtest, group_p, group_a, other_b, NULL)) {
1655*e0c4386eSCy Schubert if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0))
1656*e0c4386eSCy Schubert goto err;
1657*e0c4386eSCy Schubert } else {
1658*e0c4386eSCy Schubert /* clear the error stack if EC_GROUP_set_curve() failed */
1659*e0c4386eSCy Schubert ERR_pop_to_mark();
1660*e0c4386eSCy Schubert ERR_set_mark();
1661*e0c4386eSCy Schubert }
1662*e0c4386eSCy Schubert ERR_pop_to_mark();
1663*e0c4386eSCy Schubert
1664*e0c4386eSCy Schubert /* Check that restoring the curve parameters passes */
1665*e0c4386eSCy Schubert if (!TEST_true(EC_GROUP_set_curve(gtest, group_p, group_a, group_b, NULL))
1666*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid))
1667*e0c4386eSCy Schubert goto err;
1668*e0c4386eSCy Schubert
1669*e0c4386eSCy Schubert ret = 1;
1670*e0c4386eSCy Schubert err:
1671*e0c4386eSCy Schubert BN_free(group_p);
1672*e0c4386eSCy Schubert BN_free(other_p);
1673*e0c4386eSCy Schubert BN_free(group_a);
1674*e0c4386eSCy Schubert BN_free(other_a);
1675*e0c4386eSCy Schubert BN_free(group_b);
1676*e0c4386eSCy Schubert BN_free(other_b);
1677*e0c4386eSCy Schubert BN_free(group_cofactor);
1678*e0c4386eSCy Schubert BN_free(other_cofactor);
1679*e0c4386eSCy Schubert BN_free(other_order);
1680*e0c4386eSCy Schubert EC_POINT_free(other_gen);
1681*e0c4386eSCy Schubert EC_GROUP_free(gtest);
1682*e0c4386eSCy Schubert EC_GROUP_free(group);
1683*e0c4386eSCy Schubert BN_CTX_free(bn_ctx);
1684*e0c4386eSCy Schubert return ret;
1685*e0c4386eSCy Schubert }
1686*e0c4386eSCy Schubert
1687*e0c4386eSCy Schubert /*
1688*e0c4386eSCy Schubert * This checks the lookup capability of EC_GROUP_check_named_curve()
1689*e0c4386eSCy Schubert * when the given group was created with explicit parameters.
1690*e0c4386eSCy Schubert *
1691*e0c4386eSCy Schubert * It is possible to retrieve an alternative alias that does not match
1692*e0c4386eSCy Schubert * the original nid in this case.
1693*e0c4386eSCy Schubert */
check_named_curve_lookup_test(int id)1694*e0c4386eSCy Schubert static int check_named_curve_lookup_test(int id)
1695*e0c4386eSCy Schubert {
1696*e0c4386eSCy Schubert int ret = 0, nid, rv = 0;
1697*e0c4386eSCy Schubert EC_GROUP *g = NULL , *ga = NULL;
1698*e0c4386eSCy Schubert ECPARAMETERS *p = NULL, *pa = NULL;
1699*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
1700*e0c4386eSCy Schubert
1701*e0c4386eSCy Schubert /* Do some setup */
1702*e0c4386eSCy Schubert nid = curves[id].nid;
1703*e0c4386eSCy Schubert if (!TEST_ptr(ctx = BN_CTX_new())
1704*e0c4386eSCy Schubert || !TEST_ptr(g = EC_GROUP_new_by_curve_name(nid))
1705*e0c4386eSCy Schubert || !TEST_ptr(p = EC_GROUP_get_ecparameters(g, NULL)))
1706*e0c4386eSCy Schubert goto err;
1707*e0c4386eSCy Schubert
1708*e0c4386eSCy Schubert /* replace with group from explicit parameters */
1709*e0c4386eSCy Schubert EC_GROUP_free(g);
1710*e0c4386eSCy Schubert if (!TEST_ptr(g = EC_GROUP_new_from_ecparameters(p)))
1711*e0c4386eSCy Schubert goto err;
1712*e0c4386eSCy Schubert
1713*e0c4386eSCy Schubert if (!TEST_int_gt(rv = EC_GROUP_check_named_curve(g, 0, NULL), 0))
1714*e0c4386eSCy Schubert goto err;
1715*e0c4386eSCy Schubert if (rv != nid) {
1716*e0c4386eSCy Schubert /*
1717*e0c4386eSCy Schubert * Found an alias:
1718*e0c4386eSCy Schubert * fail if the returned nid is not an alias of the original group.
1719*e0c4386eSCy Schubert *
1720*e0c4386eSCy Schubert * The comparison here is done by comparing two explicit
1721*e0c4386eSCy Schubert * parameter EC_GROUPs with EC_GROUP_cmp(), to ensure the
1722*e0c4386eSCy Schubert * comparison happens with unnamed EC_GROUPs using the same
1723*e0c4386eSCy Schubert * EC_METHODs.
1724*e0c4386eSCy Schubert */
1725*e0c4386eSCy Schubert if (!TEST_ptr(ga = EC_GROUP_new_by_curve_name(rv))
1726*e0c4386eSCy Schubert || !TEST_ptr(pa = EC_GROUP_get_ecparameters(ga, NULL)))
1727*e0c4386eSCy Schubert goto err;
1728*e0c4386eSCy Schubert
1729*e0c4386eSCy Schubert /* replace with group from explicit parameters, then compare */
1730*e0c4386eSCy Schubert EC_GROUP_free(ga);
1731*e0c4386eSCy Schubert if (!TEST_ptr(ga = EC_GROUP_new_from_ecparameters(pa))
1732*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_cmp(g, ga, ctx), 0))
1733*e0c4386eSCy Schubert goto err;
1734*e0c4386eSCy Schubert }
1735*e0c4386eSCy Schubert
1736*e0c4386eSCy Schubert ret = 1;
1737*e0c4386eSCy Schubert
1738*e0c4386eSCy Schubert err:
1739*e0c4386eSCy Schubert EC_GROUP_free(g);
1740*e0c4386eSCy Schubert EC_GROUP_free(ga);
1741*e0c4386eSCy Schubert ECPARAMETERS_free(p);
1742*e0c4386eSCy Schubert ECPARAMETERS_free(pa);
1743*e0c4386eSCy Schubert BN_CTX_free(ctx);
1744*e0c4386eSCy Schubert
1745*e0c4386eSCy Schubert return ret;
1746*e0c4386eSCy Schubert }
1747*e0c4386eSCy Schubert
1748*e0c4386eSCy Schubert /*
1749*e0c4386eSCy Schubert * Sometime we cannot compare nids for equality, as the built-in curve table
1750*e0c4386eSCy Schubert * includes aliases with different names for the same curve.
1751*e0c4386eSCy Schubert *
1752*e0c4386eSCy Schubert * This function returns TRUE (1) if the checked nids are identical, or if they
1753*e0c4386eSCy Schubert * alias to the same curve. FALSE (0) otherwise.
1754*e0c4386eSCy Schubert */
1755*e0c4386eSCy Schubert static ossl_inline
are_ec_nids_compatible(int n1d,int n2d)1756*e0c4386eSCy Schubert int are_ec_nids_compatible(int n1d, int n2d)
1757*e0c4386eSCy Schubert {
1758*e0c4386eSCy Schubert int ret = 0;
1759*e0c4386eSCy Schubert switch (n1d) {
1760*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
1761*e0c4386eSCy Schubert case NID_sect113r1:
1762*e0c4386eSCy Schubert case NID_wap_wsg_idm_ecid_wtls4:
1763*e0c4386eSCy Schubert ret = (n2d == NID_sect113r1 || n2d == NID_wap_wsg_idm_ecid_wtls4);
1764*e0c4386eSCy Schubert break;
1765*e0c4386eSCy Schubert case NID_sect163k1:
1766*e0c4386eSCy Schubert case NID_wap_wsg_idm_ecid_wtls3:
1767*e0c4386eSCy Schubert ret = (n2d == NID_sect163k1 || n2d == NID_wap_wsg_idm_ecid_wtls3);
1768*e0c4386eSCy Schubert break;
1769*e0c4386eSCy Schubert case NID_sect233k1:
1770*e0c4386eSCy Schubert case NID_wap_wsg_idm_ecid_wtls10:
1771*e0c4386eSCy Schubert ret = (n2d == NID_sect233k1 || n2d == NID_wap_wsg_idm_ecid_wtls10);
1772*e0c4386eSCy Schubert break;
1773*e0c4386eSCy Schubert case NID_sect233r1:
1774*e0c4386eSCy Schubert case NID_wap_wsg_idm_ecid_wtls11:
1775*e0c4386eSCy Schubert ret = (n2d == NID_sect233r1 || n2d == NID_wap_wsg_idm_ecid_wtls11);
1776*e0c4386eSCy Schubert break;
1777*e0c4386eSCy Schubert case NID_X9_62_c2pnb163v1:
1778*e0c4386eSCy Schubert case NID_wap_wsg_idm_ecid_wtls5:
1779*e0c4386eSCy Schubert ret = (n2d == NID_X9_62_c2pnb163v1
1780*e0c4386eSCy Schubert || n2d == NID_wap_wsg_idm_ecid_wtls5);
1781*e0c4386eSCy Schubert break;
1782*e0c4386eSCy Schubert #endif /* OPENSSL_NO_EC2M */
1783*e0c4386eSCy Schubert case NID_secp112r1:
1784*e0c4386eSCy Schubert case NID_wap_wsg_idm_ecid_wtls6:
1785*e0c4386eSCy Schubert ret = (n2d == NID_secp112r1 || n2d == NID_wap_wsg_idm_ecid_wtls6);
1786*e0c4386eSCy Schubert break;
1787*e0c4386eSCy Schubert case NID_secp160r2:
1788*e0c4386eSCy Schubert case NID_wap_wsg_idm_ecid_wtls7:
1789*e0c4386eSCy Schubert ret = (n2d == NID_secp160r2 || n2d == NID_wap_wsg_idm_ecid_wtls7);
1790*e0c4386eSCy Schubert break;
1791*e0c4386eSCy Schubert #ifdef OPENSSL_NO_EC_NISTP_64_GCC_128
1792*e0c4386eSCy Schubert case NID_secp224r1:
1793*e0c4386eSCy Schubert case NID_wap_wsg_idm_ecid_wtls12:
1794*e0c4386eSCy Schubert ret = (n2d == NID_secp224r1 || n2d == NID_wap_wsg_idm_ecid_wtls12);
1795*e0c4386eSCy Schubert break;
1796*e0c4386eSCy Schubert #else
1797*e0c4386eSCy Schubert /*
1798*e0c4386eSCy Schubert * For SEC P-224 we want to ensure that the SECP nid is returned, as
1799*e0c4386eSCy Schubert * that is associated with a specialized method.
1800*e0c4386eSCy Schubert */
1801*e0c4386eSCy Schubert case NID_wap_wsg_idm_ecid_wtls12:
1802*e0c4386eSCy Schubert ret = (n2d == NID_secp224r1);
1803*e0c4386eSCy Schubert break;
1804*e0c4386eSCy Schubert #endif /* def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
1805*e0c4386eSCy Schubert
1806*e0c4386eSCy Schubert default:
1807*e0c4386eSCy Schubert ret = (n1d == n2d);
1808*e0c4386eSCy Schubert }
1809*e0c4386eSCy Schubert return ret;
1810*e0c4386eSCy Schubert }
1811*e0c4386eSCy Schubert
1812*e0c4386eSCy Schubert /*
1813*e0c4386eSCy Schubert * This checks that EC_GROUP_bew_from_ecparameters() returns a "named"
1814*e0c4386eSCy Schubert * EC_GROUP for built-in curves.
1815*e0c4386eSCy Schubert *
1816*e0c4386eSCy Schubert * Note that it is possible to retrieve an alternative alias that does not match
1817*e0c4386eSCy Schubert * the original nid.
1818*e0c4386eSCy Schubert *
1819*e0c4386eSCy Schubert * Ensure that the OPENSSL_EC_EXPLICIT_CURVE ASN1 flag is set.
1820*e0c4386eSCy Schubert */
check_named_curve_from_ecparameters(int id)1821*e0c4386eSCy Schubert static int check_named_curve_from_ecparameters(int id)
1822*e0c4386eSCy Schubert {
1823*e0c4386eSCy Schubert int ret = 0, nid, tnid;
1824*e0c4386eSCy Schubert EC_GROUP *group = NULL, *tgroup = NULL, *tmpg = NULL;
1825*e0c4386eSCy Schubert const EC_POINT *group_gen = NULL;
1826*e0c4386eSCy Schubert EC_POINT *other_gen = NULL;
1827*e0c4386eSCy Schubert BIGNUM *group_cofactor = NULL, *other_cofactor = NULL;
1828*e0c4386eSCy Schubert BIGNUM *other_gen_x = NULL, *other_gen_y = NULL;
1829*e0c4386eSCy Schubert const BIGNUM *group_order = NULL;
1830*e0c4386eSCy Schubert BIGNUM *other_order = NULL;
1831*e0c4386eSCy Schubert BN_CTX *bn_ctx = NULL;
1832*e0c4386eSCy Schubert static const unsigned char invalid_seed[] = "THIS IS NOT A VALID SEED";
1833*e0c4386eSCy Schubert static size_t invalid_seed_len = sizeof(invalid_seed);
1834*e0c4386eSCy Schubert ECPARAMETERS *params = NULL, *other_params = NULL;
1835*e0c4386eSCy Schubert EC_GROUP *g_ary[8] = {NULL};
1836*e0c4386eSCy Schubert EC_GROUP **g_next = &g_ary[0];
1837*e0c4386eSCy Schubert ECPARAMETERS *p_ary[8] = {NULL};
1838*e0c4386eSCy Schubert ECPARAMETERS **p_next = &p_ary[0];
1839*e0c4386eSCy Schubert
1840*e0c4386eSCy Schubert /* Do some setup */
1841*e0c4386eSCy Schubert nid = curves[id].nid;
1842*e0c4386eSCy Schubert TEST_note("Curve %s", OBJ_nid2sn(nid));
1843*e0c4386eSCy Schubert if (!TEST_ptr(bn_ctx = BN_CTX_new()))
1844*e0c4386eSCy Schubert return ret;
1845*e0c4386eSCy Schubert BN_CTX_start(bn_ctx);
1846*e0c4386eSCy Schubert
1847*e0c4386eSCy Schubert if (/* Allocations */
1848*e0c4386eSCy Schubert !TEST_ptr(group_cofactor = BN_CTX_get(bn_ctx))
1849*e0c4386eSCy Schubert || !TEST_ptr(other_gen_x = BN_CTX_get(bn_ctx))
1850*e0c4386eSCy Schubert || !TEST_ptr(other_gen_y = BN_CTX_get(bn_ctx))
1851*e0c4386eSCy Schubert || !TEST_ptr(other_order = BN_CTX_get(bn_ctx))
1852*e0c4386eSCy Schubert || !TEST_ptr(other_cofactor = BN_CTX_get(bn_ctx))
1853*e0c4386eSCy Schubert /* Generate reference group and params */
1854*e0c4386eSCy Schubert || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
1855*e0c4386eSCy Schubert || !TEST_ptr(params = EC_GROUP_get_ecparameters(group, NULL))
1856*e0c4386eSCy Schubert || !TEST_ptr(group_gen = EC_GROUP_get0_generator(group))
1857*e0c4386eSCy Schubert || !TEST_ptr(group_order = EC_GROUP_get0_order(group))
1858*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_get_cofactor(group, group_cofactor, NULL))
1859*e0c4386eSCy Schubert /* compute `other_*` values */
1860*e0c4386eSCy Schubert || !TEST_ptr(tmpg = EC_GROUP_dup(group))
1861*e0c4386eSCy Schubert || !TEST_ptr(other_gen = EC_POINT_dup(group_gen, group))
1862*e0c4386eSCy Schubert || !TEST_true(EC_POINT_add(group, other_gen, group_gen, group_gen, NULL))
1863*e0c4386eSCy Schubert || !TEST_true(EC_POINT_get_affine_coordinates(group, other_gen,
1864*e0c4386eSCy Schubert other_gen_x, other_gen_y, bn_ctx))
1865*e0c4386eSCy Schubert || !TEST_true(BN_copy(other_order, group_order))
1866*e0c4386eSCy Schubert || !TEST_true(BN_add_word(other_order, 1))
1867*e0c4386eSCy Schubert || !TEST_true(BN_copy(other_cofactor, group_cofactor))
1868*e0c4386eSCy Schubert || !TEST_true(BN_add_word(other_cofactor, 1)))
1869*e0c4386eSCy Schubert goto err;
1870*e0c4386eSCy Schubert
1871*e0c4386eSCy Schubert EC_POINT_free(other_gen);
1872*e0c4386eSCy Schubert other_gen = NULL;
1873*e0c4386eSCy Schubert
1874*e0c4386eSCy Schubert if (!TEST_ptr(other_gen = EC_POINT_new(tmpg))
1875*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_affine_coordinates(tmpg, other_gen,
1876*e0c4386eSCy Schubert other_gen_x, other_gen_y,
1877*e0c4386eSCy Schubert bn_ctx)))
1878*e0c4386eSCy Schubert goto err;
1879*e0c4386eSCy Schubert
1880*e0c4386eSCy Schubert /*
1881*e0c4386eSCy Schubert * ###########################
1882*e0c4386eSCy Schubert * # Actual tests start here #
1883*e0c4386eSCy Schubert * ###########################
1884*e0c4386eSCy Schubert */
1885*e0c4386eSCy Schubert
1886*e0c4386eSCy Schubert /*
1887*e0c4386eSCy Schubert * Creating a group from built-in explicit parameters returns a
1888*e0c4386eSCy Schubert * "named" EC_GROUP
1889*e0c4386eSCy Schubert */
1890*e0c4386eSCy Schubert if (!TEST_ptr(tgroup = *g_next++ = EC_GROUP_new_from_ecparameters(params))
1891*e0c4386eSCy Schubert || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef))
1892*e0c4386eSCy Schubert goto err;
1893*e0c4386eSCy Schubert /*
1894*e0c4386eSCy Schubert * We cannot always guarantee the names match, as the built-in table
1895*e0c4386eSCy Schubert * contains aliases for the same curve with different names.
1896*e0c4386eSCy Schubert */
1897*e0c4386eSCy Schubert if (!TEST_true(are_ec_nids_compatible(nid, tnid))) {
1898*e0c4386eSCy Schubert TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid));
1899*e0c4386eSCy Schubert goto err;
1900*e0c4386eSCy Schubert }
1901*e0c4386eSCy Schubert /* Ensure that the OPENSSL_EC_EXPLICIT_CURVE ASN1 flag is set. */
1902*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup), OPENSSL_EC_EXPLICIT_CURVE))
1903*e0c4386eSCy Schubert goto err;
1904*e0c4386eSCy Schubert
1905*e0c4386eSCy Schubert /*
1906*e0c4386eSCy Schubert * An invalid seed in the parameters should be ignored: expect a "named"
1907*e0c4386eSCy Schubert * group.
1908*e0c4386eSCy Schubert */
1909*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_set_seed(tmpg, invalid_seed, invalid_seed_len),
1910*e0c4386eSCy Schubert invalid_seed_len)
1911*e0c4386eSCy Schubert || !TEST_ptr(other_params = *p_next++ =
1912*e0c4386eSCy Schubert EC_GROUP_get_ecparameters(tmpg, NULL))
1913*e0c4386eSCy Schubert || !TEST_ptr(tgroup = *g_next++ =
1914*e0c4386eSCy Schubert EC_GROUP_new_from_ecparameters(other_params))
1915*e0c4386eSCy Schubert || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
1916*e0c4386eSCy Schubert || !TEST_true(are_ec_nids_compatible(nid, tnid))
1917*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
1918*e0c4386eSCy Schubert OPENSSL_EC_EXPLICIT_CURVE)) {
1919*e0c4386eSCy Schubert TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid));
1920*e0c4386eSCy Schubert goto err;
1921*e0c4386eSCy Schubert }
1922*e0c4386eSCy Schubert
1923*e0c4386eSCy Schubert /*
1924*e0c4386eSCy Schubert * A null seed in the parameters should be ignored, as it is optional:
1925*e0c4386eSCy Schubert * expect a "named" group.
1926*e0c4386eSCy Schubert */
1927*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_set_seed(tmpg, NULL, 0), 1)
1928*e0c4386eSCy Schubert || !TEST_ptr(other_params = *p_next++ =
1929*e0c4386eSCy Schubert EC_GROUP_get_ecparameters(tmpg, NULL))
1930*e0c4386eSCy Schubert || !TEST_ptr(tgroup = *g_next++ =
1931*e0c4386eSCy Schubert EC_GROUP_new_from_ecparameters(other_params))
1932*e0c4386eSCy Schubert || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
1933*e0c4386eSCy Schubert || !TEST_true(are_ec_nids_compatible(nid, tnid))
1934*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
1935*e0c4386eSCy Schubert OPENSSL_EC_EXPLICIT_CURVE)) {
1936*e0c4386eSCy Schubert TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid));
1937*e0c4386eSCy Schubert goto err;
1938*e0c4386eSCy Schubert }
1939*e0c4386eSCy Schubert
1940*e0c4386eSCy Schubert /*
1941*e0c4386eSCy Schubert * Check that changing any of the generator parameters does not yield a
1942*e0c4386eSCy Schubert * match with the built-in curves
1943*e0c4386eSCy Schubert */
1944*e0c4386eSCy Schubert if (/* Other gen, same group order & cofactor */
1945*e0c4386eSCy Schubert !TEST_true(EC_GROUP_set_generator(tmpg, other_gen, group_order,
1946*e0c4386eSCy Schubert group_cofactor))
1947*e0c4386eSCy Schubert || !TEST_ptr(other_params = *p_next++ =
1948*e0c4386eSCy Schubert EC_GROUP_get_ecparameters(tmpg, NULL))
1949*e0c4386eSCy Schubert || !TEST_ptr(tgroup = *g_next++ =
1950*e0c4386eSCy Schubert EC_GROUP_new_from_ecparameters(other_params))
1951*e0c4386eSCy Schubert || !TEST_int_eq((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
1952*e0c4386eSCy Schubert /* Same gen & cofactor, different order */
1953*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, other_order,
1954*e0c4386eSCy Schubert group_cofactor))
1955*e0c4386eSCy Schubert || !TEST_ptr(other_params = *p_next++ =
1956*e0c4386eSCy Schubert EC_GROUP_get_ecparameters(tmpg, NULL))
1957*e0c4386eSCy Schubert || !TEST_ptr(tgroup = *g_next++ =
1958*e0c4386eSCy Schubert EC_GROUP_new_from_ecparameters(other_params))
1959*e0c4386eSCy Schubert || !TEST_int_eq((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
1960*e0c4386eSCy Schubert /* The order is not an optional field, so this should fail */
1961*e0c4386eSCy Schubert || !TEST_false(EC_GROUP_set_generator(tmpg, group_gen, NULL,
1962*e0c4386eSCy Schubert group_cofactor))
1963*e0c4386eSCy Schubert /* Check that a wrong cofactor is ignored, and we still match */
1964*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order,
1965*e0c4386eSCy Schubert other_cofactor))
1966*e0c4386eSCy Schubert || !TEST_ptr(other_params = *p_next++ =
1967*e0c4386eSCy Schubert EC_GROUP_get_ecparameters(tmpg, NULL))
1968*e0c4386eSCy Schubert || !TEST_ptr(tgroup = *g_next++ =
1969*e0c4386eSCy Schubert EC_GROUP_new_from_ecparameters(other_params))
1970*e0c4386eSCy Schubert || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
1971*e0c4386eSCy Schubert || !TEST_true(are_ec_nids_compatible(nid, tnid))
1972*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
1973*e0c4386eSCy Schubert OPENSSL_EC_EXPLICIT_CURVE)
1974*e0c4386eSCy Schubert /* Check that if the cofactor is not set then it still matches */
1975*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order,
1976*e0c4386eSCy Schubert NULL))
1977*e0c4386eSCy Schubert || !TEST_ptr(other_params = *p_next++ =
1978*e0c4386eSCy Schubert EC_GROUP_get_ecparameters(tmpg, NULL))
1979*e0c4386eSCy Schubert || !TEST_ptr(tgroup = *g_next++ =
1980*e0c4386eSCy Schubert EC_GROUP_new_from_ecparameters(other_params))
1981*e0c4386eSCy Schubert || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
1982*e0c4386eSCy Schubert || !TEST_true(are_ec_nids_compatible(nid, tnid))
1983*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
1984*e0c4386eSCy Schubert OPENSSL_EC_EXPLICIT_CURVE)
1985*e0c4386eSCy Schubert /* check that restoring the generator passes */
1986*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order,
1987*e0c4386eSCy Schubert group_cofactor))
1988*e0c4386eSCy Schubert || !TEST_ptr(other_params = *p_next++ =
1989*e0c4386eSCy Schubert EC_GROUP_get_ecparameters(tmpg, NULL))
1990*e0c4386eSCy Schubert || !TEST_ptr(tgroup = *g_next++ =
1991*e0c4386eSCy Schubert EC_GROUP_new_from_ecparameters(other_params))
1992*e0c4386eSCy Schubert || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
1993*e0c4386eSCy Schubert || !TEST_true(are_ec_nids_compatible(nid, tnid))
1994*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
1995*e0c4386eSCy Schubert OPENSSL_EC_EXPLICIT_CURVE))
1996*e0c4386eSCy Schubert goto err;
1997*e0c4386eSCy Schubert
1998*e0c4386eSCy Schubert ret = 1;
1999*e0c4386eSCy Schubert err:
2000*e0c4386eSCy Schubert for (g_next = &g_ary[0]; g_next < g_ary + OSSL_NELEM(g_ary); g_next++)
2001*e0c4386eSCy Schubert EC_GROUP_free(*g_next);
2002*e0c4386eSCy Schubert for (p_next = &p_ary[0]; p_next < p_ary + OSSL_NELEM(g_ary); p_next++)
2003*e0c4386eSCy Schubert ECPARAMETERS_free(*p_next);
2004*e0c4386eSCy Schubert ECPARAMETERS_free(params);
2005*e0c4386eSCy Schubert EC_POINT_free(other_gen);
2006*e0c4386eSCy Schubert EC_GROUP_free(tmpg);
2007*e0c4386eSCy Schubert EC_GROUP_free(group);
2008*e0c4386eSCy Schubert BN_CTX_end(bn_ctx);
2009*e0c4386eSCy Schubert BN_CTX_free(bn_ctx);
2010*e0c4386eSCy Schubert return ret;
2011*e0c4386eSCy Schubert }
2012*e0c4386eSCy Schubert
2013*e0c4386eSCy Schubert
parameter_test(void)2014*e0c4386eSCy Schubert static int parameter_test(void)
2015*e0c4386eSCy Schubert {
2016*e0c4386eSCy Schubert EC_GROUP *group = NULL, *group2 = NULL;
2017*e0c4386eSCy Schubert ECPARAMETERS *ecparameters = NULL;
2018*e0c4386eSCy Schubert unsigned char *buf = NULL;
2019*e0c4386eSCy Schubert int r = 0, len;
2020*e0c4386eSCy Schubert
2021*e0c4386eSCy Schubert if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp384r1))
2022*e0c4386eSCy Schubert || !TEST_ptr(ecparameters = EC_GROUP_get_ecparameters(group, NULL))
2023*e0c4386eSCy Schubert || !TEST_ptr(group2 = EC_GROUP_new_from_ecparameters(ecparameters))
2024*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_cmp(group, group2, NULL), 0))
2025*e0c4386eSCy Schubert goto err;
2026*e0c4386eSCy Schubert
2027*e0c4386eSCy Schubert EC_GROUP_free(group);
2028*e0c4386eSCy Schubert group = NULL;
2029*e0c4386eSCy Schubert
2030*e0c4386eSCy Schubert /* Test the named curve encoding, which should be default. */
2031*e0c4386eSCy Schubert if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp521r1))
2032*e0c4386eSCy Schubert || !TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0)
2033*e0c4386eSCy Schubert || !TEST_mem_eq(buf, len, p521_named, sizeof(p521_named)))
2034*e0c4386eSCy Schubert goto err;
2035*e0c4386eSCy Schubert
2036*e0c4386eSCy Schubert OPENSSL_free(buf);
2037*e0c4386eSCy Schubert buf = NULL;
2038*e0c4386eSCy Schubert
2039*e0c4386eSCy Schubert /*
2040*e0c4386eSCy Schubert * Test the explicit encoding. P-521 requires correctly zero-padding the
2041*e0c4386eSCy Schubert * curve coefficients.
2042*e0c4386eSCy Schubert */
2043*e0c4386eSCy Schubert EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
2044*e0c4386eSCy Schubert if (!TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0)
2045*e0c4386eSCy Schubert || !TEST_mem_eq(buf, len, p521_explicit, sizeof(p521_explicit)))
2046*e0c4386eSCy Schubert goto err;
2047*e0c4386eSCy Schubert
2048*e0c4386eSCy Schubert r = 1;
2049*e0c4386eSCy Schubert err:
2050*e0c4386eSCy Schubert EC_GROUP_free(group);
2051*e0c4386eSCy Schubert EC_GROUP_free(group2);
2052*e0c4386eSCy Schubert ECPARAMETERS_free(ecparameters);
2053*e0c4386eSCy Schubert OPENSSL_free(buf);
2054*e0c4386eSCy Schubert return r;
2055*e0c4386eSCy Schubert }
2056*e0c4386eSCy Schubert
2057*e0c4386eSCy Schubert /*-
2058*e0c4386eSCy Schubert * random 256-bit explicit parameters curve, cofactor absent
2059*e0c4386eSCy Schubert * order: 0x0c38d96a9f892b88772ec2e39614a82f4f (132 bit)
2060*e0c4386eSCy Schubert * cofactor: 0x12bc94785251297abfafddf1565100da (125 bit)
2061*e0c4386eSCy Schubert */
2062*e0c4386eSCy Schubert static const unsigned char params_cf_pass[] = {
2063*e0c4386eSCy Schubert 0x30, 0x81, 0xcd, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86,
2064*e0c4386eSCy Schubert 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xe5, 0x00, 0x1f, 0xc5,
2065*e0c4386eSCy Schubert 0xca, 0x71, 0x9d, 0x8e, 0xf7, 0x07, 0x4b, 0x48, 0x37, 0xf9, 0x33, 0x2d,
2066*e0c4386eSCy Schubert 0x71, 0xbf, 0x79, 0xe7, 0xdc, 0x91, 0xc2, 0xff, 0xb6, 0x7b, 0xc3, 0x93,
2067*e0c4386eSCy Schubert 0x44, 0x88, 0xe6, 0x91, 0x30, 0x44, 0x04, 0x20, 0xe5, 0x00, 0x1f, 0xc5,
2068*e0c4386eSCy Schubert 0xca, 0x71, 0x9d, 0x8e, 0xf7, 0x07, 0x4b, 0x48, 0x37, 0xf9, 0x33, 0x2d,
2069*e0c4386eSCy Schubert 0x71, 0xbf, 0x79, 0xe7, 0xdc, 0x91, 0xc2, 0xff, 0xb6, 0x7b, 0xc3, 0x93,
2070*e0c4386eSCy Schubert 0x44, 0x88, 0xe6, 0x8e, 0x04, 0x20, 0x18, 0x8c, 0x59, 0x57, 0xc4, 0xbc,
2071*e0c4386eSCy Schubert 0x85, 0x57, 0xc3, 0x66, 0x9f, 0x89, 0xd5, 0x92, 0x0d, 0x7e, 0x42, 0x27,
2072*e0c4386eSCy Schubert 0x07, 0x64, 0xaa, 0x26, 0xed, 0x89, 0xc4, 0x09, 0x05, 0x4d, 0xc7, 0x23,
2073*e0c4386eSCy Schubert 0x47, 0xda, 0x04, 0x41, 0x04, 0x1b, 0x6b, 0x41, 0x0b, 0xf9, 0xfb, 0x77,
2074*e0c4386eSCy Schubert 0xfd, 0x50, 0xb7, 0x3e, 0x23, 0xa3, 0xec, 0x9a, 0x3b, 0x09, 0x31, 0x6b,
2075*e0c4386eSCy Schubert 0xfa, 0xf6, 0xce, 0x1f, 0xff, 0xeb, 0x57, 0x93, 0x24, 0x70, 0xf3, 0xf4,
2076*e0c4386eSCy Schubert 0xba, 0x7e, 0xfa, 0x86, 0x6e, 0x19, 0x89, 0xe3, 0x55, 0x6d, 0x5a, 0xe9,
2077*e0c4386eSCy Schubert 0xc0, 0x3d, 0xbc, 0xfb, 0xaf, 0xad, 0xd4, 0x7e, 0xa6, 0xe5, 0xfa, 0x1a,
2078*e0c4386eSCy Schubert 0x58, 0x07, 0x9e, 0x8f, 0x0d, 0x3b, 0xf7, 0x38, 0xca, 0x02, 0x11, 0x0c,
2079*e0c4386eSCy Schubert 0x38, 0xd9, 0x6a, 0x9f, 0x89, 0x2b, 0x88, 0x77, 0x2e, 0xc2, 0xe3, 0x96,
2080*e0c4386eSCy Schubert 0x14, 0xa8, 0x2f, 0x4f
2081*e0c4386eSCy Schubert };
2082*e0c4386eSCy Schubert
2083*e0c4386eSCy Schubert /*-
2084*e0c4386eSCy Schubert * random 256-bit explicit parameters curve, cofactor absent
2085*e0c4386eSCy Schubert * order: 0x045a75c0c17228ebd9b169a10e34a22101 (131 bit)
2086*e0c4386eSCy Schubert * cofactor: 0x2e134b4ede82649f67a2e559d361e5fe (126 bit)
2087*e0c4386eSCy Schubert */
2088*e0c4386eSCy Schubert static const unsigned char params_cf_fail[] = {
2089*e0c4386eSCy Schubert 0x30, 0x81, 0xcd, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86,
2090*e0c4386eSCy Schubert 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xc8, 0x95, 0x27, 0x37,
2091*e0c4386eSCy Schubert 0xe8, 0xe1, 0xfd, 0xcc, 0xf9, 0x6e, 0x0c, 0xa6, 0x21, 0xc1, 0x7d, 0x6b,
2092*e0c4386eSCy Schubert 0x9d, 0x44, 0x42, 0xea, 0x73, 0x4e, 0x04, 0xb6, 0xac, 0x62, 0x50, 0xd0,
2093*e0c4386eSCy Schubert 0x33, 0xc2, 0xea, 0x13, 0x30, 0x44, 0x04, 0x20, 0xc8, 0x95, 0x27, 0x37,
2094*e0c4386eSCy Schubert 0xe8, 0xe1, 0xfd, 0xcc, 0xf9, 0x6e, 0x0c, 0xa6, 0x21, 0xc1, 0x7d, 0x6b,
2095*e0c4386eSCy Schubert 0x9d, 0x44, 0x42, 0xea, 0x73, 0x4e, 0x04, 0xb6, 0xac, 0x62, 0x50, 0xd0,
2096*e0c4386eSCy Schubert 0x33, 0xc2, 0xea, 0x10, 0x04, 0x20, 0xbf, 0xa6, 0xa8, 0x05, 0x1d, 0x09,
2097*e0c4386eSCy Schubert 0xac, 0x70, 0x39, 0xbb, 0x4d, 0xb2, 0x90, 0x8a, 0x15, 0x41, 0x14, 0x1d,
2098*e0c4386eSCy Schubert 0x11, 0x86, 0x9f, 0x13, 0xa2, 0x63, 0x1a, 0xda, 0x95, 0x22, 0x4d, 0x02,
2099*e0c4386eSCy Schubert 0x15, 0x0a, 0x04, 0x41, 0x04, 0xaf, 0x16, 0x71, 0xf9, 0xc4, 0xc8, 0x59,
2100*e0c4386eSCy Schubert 0x1d, 0xa3, 0x6f, 0xe7, 0xc3, 0x57, 0xa1, 0xfa, 0x9f, 0x49, 0x7c, 0x11,
2101*e0c4386eSCy Schubert 0x27, 0x05, 0xa0, 0x7f, 0xff, 0xf9, 0xe0, 0xe7, 0x92, 0xdd, 0x9c, 0x24,
2102*e0c4386eSCy Schubert 0x8e, 0xc7, 0xb9, 0x52, 0x71, 0x3f, 0xbc, 0x7f, 0x6a, 0x9f, 0x35, 0x70,
2103*e0c4386eSCy Schubert 0xe1, 0x27, 0xd5, 0x35, 0x8a, 0x13, 0xfa, 0xa8, 0x33, 0x3e, 0xd4, 0x73,
2104*e0c4386eSCy Schubert 0x1c, 0x14, 0x58, 0x9e, 0xc7, 0x0a, 0x87, 0x65, 0x8d, 0x02, 0x11, 0x04,
2105*e0c4386eSCy Schubert 0x5a, 0x75, 0xc0, 0xc1, 0x72, 0x28, 0xeb, 0xd9, 0xb1, 0x69, 0xa1, 0x0e,
2106*e0c4386eSCy Schubert 0x34, 0xa2, 0x21, 0x01
2107*e0c4386eSCy Schubert };
2108*e0c4386eSCy Schubert
2109*e0c4386eSCy Schubert /*-
2110*e0c4386eSCy Schubert * Test two random 256-bit explicit parameters curves with absent cofactor.
2111*e0c4386eSCy Schubert * The two curves are chosen to roughly straddle the bounds at which the lib
2112*e0c4386eSCy Schubert * can compute the cofactor automatically, roughly 4*sqrt(p). So test that:
2113*e0c4386eSCy Schubert *
2114*e0c4386eSCy Schubert * - params_cf_pass: order is sufficiently close to p to compute cofactor
2115*e0c4386eSCy Schubert * - params_cf_fail: order is too far away from p to compute cofactor
2116*e0c4386eSCy Schubert *
2117*e0c4386eSCy Schubert * For standards-compliant curves, cofactor is chosen as small as possible.
2118*e0c4386eSCy Schubert * So you can see neither of these curves are fit for cryptographic use.
2119*e0c4386eSCy Schubert *
2120*e0c4386eSCy Schubert * Some standards even mandate an upper bound on the cofactor, e.g. SECG1 v2:
2121*e0c4386eSCy Schubert * h <= 2**(t/8) where t is the security level of the curve, for which the lib
2122*e0c4386eSCy Schubert * will always succeed in computing the cofactor. Neither of these curves
2123*e0c4386eSCy Schubert * conform to that -- this is just robustness testing.
2124*e0c4386eSCy Schubert */
cofactor_range_test(void)2125*e0c4386eSCy Schubert static int cofactor_range_test(void)
2126*e0c4386eSCy Schubert {
2127*e0c4386eSCy Schubert EC_GROUP *group = NULL;
2128*e0c4386eSCy Schubert BIGNUM *cf = NULL;
2129*e0c4386eSCy Schubert int ret = 0;
2130*e0c4386eSCy Schubert const unsigned char *b1 = (const unsigned char *)params_cf_fail;
2131*e0c4386eSCy Schubert const unsigned char *b2 = (const unsigned char *)params_cf_pass;
2132*e0c4386eSCy Schubert
2133*e0c4386eSCy Schubert if (!TEST_ptr(group = d2i_ECPKParameters(NULL, &b1, sizeof(params_cf_fail)))
2134*e0c4386eSCy Schubert || !TEST_BN_eq_zero(EC_GROUP_get0_cofactor(group))
2135*e0c4386eSCy Schubert || !TEST_ptr(group = d2i_ECPKParameters(&group, &b2,
2136*e0c4386eSCy Schubert sizeof(params_cf_pass)))
2137*e0c4386eSCy Schubert || !TEST_int_gt(BN_hex2bn(&cf, "12bc94785251297abfafddf1565100da"), 0)
2138*e0c4386eSCy Schubert || !TEST_BN_eq(cf, EC_GROUP_get0_cofactor(group)))
2139*e0c4386eSCy Schubert goto err;
2140*e0c4386eSCy Schubert ret = 1;
2141*e0c4386eSCy Schubert err:
2142*e0c4386eSCy Schubert BN_free(cf);
2143*e0c4386eSCy Schubert EC_GROUP_free(group);
2144*e0c4386eSCy Schubert return ret;
2145*e0c4386eSCy Schubert }
2146*e0c4386eSCy Schubert
2147*e0c4386eSCy Schubert /*-
2148*e0c4386eSCy Schubert * For named curves, test that:
2149*e0c4386eSCy Schubert * - the lib correctly computes the cofactor if passed a NULL or zero cofactor
2150*e0c4386eSCy Schubert * - a nonsensical cofactor throws an error (negative test)
2151*e0c4386eSCy Schubert * - nonsensical orders throw errors (negative tests)
2152*e0c4386eSCy Schubert */
cardinality_test(int n)2153*e0c4386eSCy Schubert static int cardinality_test(int n)
2154*e0c4386eSCy Schubert {
2155*e0c4386eSCy Schubert int ret = 0, is_binary = 0;
2156*e0c4386eSCy Schubert int nid = curves[n].nid;
2157*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
2158*e0c4386eSCy Schubert EC_GROUP *g1 = NULL, *g2 = NULL;
2159*e0c4386eSCy Schubert EC_POINT *g2_gen = NULL;
2160*e0c4386eSCy Schubert BIGNUM *g1_p = NULL, *g1_a = NULL, *g1_b = NULL, *g1_x = NULL, *g1_y = NULL,
2161*e0c4386eSCy Schubert *g1_order = NULL, *g1_cf = NULL, *g2_cf = NULL;
2162*e0c4386eSCy Schubert
2163*e0c4386eSCy Schubert TEST_info("Curve %s cardinality test", OBJ_nid2sn(nid));
2164*e0c4386eSCy Schubert
2165*e0c4386eSCy Schubert if (!TEST_ptr(ctx = BN_CTX_new())
2166*e0c4386eSCy Schubert || !TEST_ptr(g1 = EC_GROUP_new_by_curve_name(nid))) {
2167*e0c4386eSCy Schubert BN_CTX_free(ctx);
2168*e0c4386eSCy Schubert return 0;
2169*e0c4386eSCy Schubert }
2170*e0c4386eSCy Schubert
2171*e0c4386eSCy Schubert is_binary = (EC_GROUP_get_field_type(g1) == NID_X9_62_characteristic_two_field);
2172*e0c4386eSCy Schubert
2173*e0c4386eSCy Schubert BN_CTX_start(ctx);
2174*e0c4386eSCy Schubert g1_p = BN_CTX_get(ctx);
2175*e0c4386eSCy Schubert g1_a = BN_CTX_get(ctx);
2176*e0c4386eSCy Schubert g1_b = BN_CTX_get(ctx);
2177*e0c4386eSCy Schubert g1_x = BN_CTX_get(ctx);
2178*e0c4386eSCy Schubert g1_y = BN_CTX_get(ctx);
2179*e0c4386eSCy Schubert g1_order = BN_CTX_get(ctx);
2180*e0c4386eSCy Schubert g1_cf = BN_CTX_get(ctx);
2181*e0c4386eSCy Schubert
2182*e0c4386eSCy Schubert if (!TEST_ptr(g2_cf = BN_CTX_get(ctx))
2183*e0c4386eSCy Schubert /* pull out the explicit curve parameters */
2184*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_get_curve(g1, g1_p, g1_a, g1_b, ctx))
2185*e0c4386eSCy Schubert || !TEST_true(EC_POINT_get_affine_coordinates(g1,
2186*e0c4386eSCy Schubert EC_GROUP_get0_generator(g1), g1_x, g1_y, ctx))
2187*e0c4386eSCy Schubert || !TEST_true(BN_copy(g1_order, EC_GROUP_get0_order(g1)))
2188*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_get_cofactor(g1, g1_cf, ctx))
2189*e0c4386eSCy Schubert /* construct g2 manually with g1 parameters */
2190*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
2191*e0c4386eSCy Schubert || !TEST_ptr(g2 = (is_binary) ?
2192*e0c4386eSCy Schubert EC_GROUP_new_curve_GF2m(g1_p, g1_a, g1_b, ctx) :
2193*e0c4386eSCy Schubert EC_GROUP_new_curve_GFp(g1_p, g1_a, g1_b, ctx))
2194*e0c4386eSCy Schubert #else
2195*e0c4386eSCy Schubert || !TEST_int_eq(0, is_binary)
2196*e0c4386eSCy Schubert || !TEST_ptr(g2 = EC_GROUP_new_curve_GFp(g1_p, g1_a, g1_b, ctx))
2197*e0c4386eSCy Schubert #endif
2198*e0c4386eSCy Schubert || !TEST_ptr(g2_gen = EC_POINT_new(g2))
2199*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_affine_coordinates(g2, g2_gen, g1_x, g1_y, ctx))
2200*e0c4386eSCy Schubert /* pass NULL cofactor: lib should compute it */
2201*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
2202*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_get_cofactor(g2, g2_cf, ctx))
2203*e0c4386eSCy Schubert || !TEST_BN_eq(g1_cf, g2_cf)
2204*e0c4386eSCy Schubert /* pass zero cofactor: lib should compute it */
2205*e0c4386eSCy Schubert || !TEST_true(BN_set_word(g2_cf, 0))
2206*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(g2, g2_gen, g1_order, g2_cf))
2207*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_get_cofactor(g2, g2_cf, ctx))
2208*e0c4386eSCy Schubert || !TEST_BN_eq(g1_cf, g2_cf)
2209*e0c4386eSCy Schubert /* negative test for invalid cofactor */
2210*e0c4386eSCy Schubert || !TEST_true(BN_set_word(g2_cf, 0))
2211*e0c4386eSCy Schubert || !TEST_true(BN_sub(g2_cf, g2_cf, BN_value_one()))
2212*e0c4386eSCy Schubert || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, g2_cf))
2213*e0c4386eSCy Schubert /* negative test for NULL order */
2214*e0c4386eSCy Schubert || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, NULL, NULL))
2215*e0c4386eSCy Schubert /* negative test for zero order */
2216*e0c4386eSCy Schubert || !TEST_true(BN_set_word(g1_order, 0))
2217*e0c4386eSCy Schubert || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
2218*e0c4386eSCy Schubert /* negative test for negative order */
2219*e0c4386eSCy Schubert || !TEST_true(BN_set_word(g2_cf, 0))
2220*e0c4386eSCy Schubert || !TEST_true(BN_sub(g2_cf, g2_cf, BN_value_one()))
2221*e0c4386eSCy Schubert || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
2222*e0c4386eSCy Schubert /* negative test for too large order */
2223*e0c4386eSCy Schubert || !TEST_true(BN_lshift(g1_order, g1_p, 2))
2224*e0c4386eSCy Schubert || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL)))
2225*e0c4386eSCy Schubert goto err;
2226*e0c4386eSCy Schubert ret = 1;
2227*e0c4386eSCy Schubert err:
2228*e0c4386eSCy Schubert EC_POINT_free(g2_gen);
2229*e0c4386eSCy Schubert EC_GROUP_free(g1);
2230*e0c4386eSCy Schubert EC_GROUP_free(g2);
2231*e0c4386eSCy Schubert BN_CTX_end(ctx);
2232*e0c4386eSCy Schubert BN_CTX_free(ctx);
2233*e0c4386eSCy Schubert return ret;
2234*e0c4386eSCy Schubert }
2235*e0c4386eSCy Schubert
check_ec_key_field_public_range_test(int id)2236*e0c4386eSCy Schubert static int check_ec_key_field_public_range_test(int id)
2237*e0c4386eSCy Schubert {
2238*e0c4386eSCy Schubert int ret = 0, type = 0;
2239*e0c4386eSCy Schubert const EC_POINT *pub = NULL;
2240*e0c4386eSCy Schubert const EC_GROUP *group = NULL;
2241*e0c4386eSCy Schubert const BIGNUM *field = NULL;
2242*e0c4386eSCy Schubert BIGNUM *x = NULL, *y = NULL;
2243*e0c4386eSCy Schubert EC_KEY *key = NULL;
2244*e0c4386eSCy Schubert
2245*e0c4386eSCy Schubert if (!TEST_ptr(x = BN_new())
2246*e0c4386eSCy Schubert || !TEST_ptr(y = BN_new())
2247*e0c4386eSCy Schubert || !TEST_ptr(key = EC_KEY_new_by_curve_name(curves[id].nid))
2248*e0c4386eSCy Schubert || !TEST_ptr(group = EC_KEY_get0_group(key))
2249*e0c4386eSCy Schubert || !TEST_ptr(field = EC_GROUP_get0_field(group))
2250*e0c4386eSCy Schubert || !TEST_int_gt(EC_KEY_generate_key(key), 0)
2251*e0c4386eSCy Schubert || !TEST_int_gt(EC_KEY_check_key(key), 0)
2252*e0c4386eSCy Schubert || !TEST_ptr(pub = EC_KEY_get0_public_key(key))
2253*e0c4386eSCy Schubert || !TEST_int_gt(EC_POINT_get_affine_coordinates(group, pub, x, y,
2254*e0c4386eSCy Schubert NULL), 0))
2255*e0c4386eSCy Schubert goto err;
2256*e0c4386eSCy Schubert
2257*e0c4386eSCy Schubert /*
2258*e0c4386eSCy Schubert * Make the public point out of range by adding the field (which will still
2259*e0c4386eSCy Schubert * be the same point on the curve). The add is different for char2 fields.
2260*e0c4386eSCy Schubert */
2261*e0c4386eSCy Schubert type = EC_GROUP_get_field_type(group);
2262*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
2263*e0c4386eSCy Schubert if (type == NID_X9_62_characteristic_two_field) {
2264*e0c4386eSCy Schubert /* test for binary curves */
2265*e0c4386eSCy Schubert if (!TEST_true(BN_GF2m_add(x, x, field)))
2266*e0c4386eSCy Schubert goto err;
2267*e0c4386eSCy Schubert } else
2268*e0c4386eSCy Schubert #endif
2269*e0c4386eSCy Schubert if (type == NID_X9_62_prime_field) {
2270*e0c4386eSCy Schubert /* test for prime curves */
2271*e0c4386eSCy Schubert if (!TEST_true(BN_add(x, x, field)))
2272*e0c4386eSCy Schubert goto err;
2273*e0c4386eSCy Schubert } else {
2274*e0c4386eSCy Schubert /* this should never happen */
2275*e0c4386eSCy Schubert TEST_error("Unsupported EC_METHOD field_type");
2276*e0c4386eSCy Schubert goto err;
2277*e0c4386eSCy Schubert }
2278*e0c4386eSCy Schubert if (!TEST_int_le(EC_KEY_set_public_key_affine_coordinates(key, x, y), 0))
2279*e0c4386eSCy Schubert goto err;
2280*e0c4386eSCy Schubert
2281*e0c4386eSCy Schubert ret = 1;
2282*e0c4386eSCy Schubert err:
2283*e0c4386eSCy Schubert BN_free(x);
2284*e0c4386eSCy Schubert BN_free(y);
2285*e0c4386eSCy Schubert EC_KEY_free(key);
2286*e0c4386eSCy Schubert return ret;
2287*e0c4386eSCy Schubert }
2288*e0c4386eSCy Schubert
2289*e0c4386eSCy Schubert /*
2290*e0c4386eSCy Schubert * Helper for ec_point_hex2point_test
2291*e0c4386eSCy Schubert *
2292*e0c4386eSCy Schubert * Self-tests EC_POINT_point2hex() against EC_POINT_hex2point() for the given
2293*e0c4386eSCy Schubert * (group,P) pair.
2294*e0c4386eSCy Schubert *
2295*e0c4386eSCy Schubert * If P is NULL use point at infinity.
2296*e0c4386eSCy Schubert */
2297*e0c4386eSCy Schubert static ossl_inline
ec_point_hex2point_test_helper(const EC_GROUP * group,const EC_POINT * P,point_conversion_form_t form,BN_CTX * bnctx)2298*e0c4386eSCy Schubert int ec_point_hex2point_test_helper(const EC_GROUP *group, const EC_POINT *P,
2299*e0c4386eSCy Schubert point_conversion_form_t form,
2300*e0c4386eSCy Schubert BN_CTX *bnctx)
2301*e0c4386eSCy Schubert {
2302*e0c4386eSCy Schubert int ret = 0;
2303*e0c4386eSCy Schubert EC_POINT *Q = NULL, *Pinf = NULL;
2304*e0c4386eSCy Schubert char *hex = NULL;
2305*e0c4386eSCy Schubert
2306*e0c4386eSCy Schubert if (P == NULL) {
2307*e0c4386eSCy Schubert /* If P is NULL use point at infinity. */
2308*e0c4386eSCy Schubert if (!TEST_ptr(Pinf = EC_POINT_new(group))
2309*e0c4386eSCy Schubert || !TEST_true(EC_POINT_set_to_infinity(group, Pinf)))
2310*e0c4386eSCy Schubert goto err;
2311*e0c4386eSCy Schubert P = Pinf;
2312*e0c4386eSCy Schubert }
2313*e0c4386eSCy Schubert
2314*e0c4386eSCy Schubert if (!TEST_ptr(hex = EC_POINT_point2hex(group, P, form, bnctx))
2315*e0c4386eSCy Schubert || !TEST_ptr(Q = EC_POINT_hex2point(group, hex, NULL, bnctx))
2316*e0c4386eSCy Schubert || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, bnctx)))
2317*e0c4386eSCy Schubert goto err;
2318*e0c4386eSCy Schubert
2319*e0c4386eSCy Schubert /*
2320*e0c4386eSCy Schubert * The next check is most likely superfluous, as EC_POINT_cmp should already
2321*e0c4386eSCy Schubert * cover this.
2322*e0c4386eSCy Schubert * Nonetheless it increases the test coverage for EC_POINT_is_at_infinity,
2323*e0c4386eSCy Schubert * so we include it anyway!
2324*e0c4386eSCy Schubert */
2325*e0c4386eSCy Schubert if (Pinf != NULL
2326*e0c4386eSCy Schubert && !TEST_true(EC_POINT_is_at_infinity(group, Q)))
2327*e0c4386eSCy Schubert goto err;
2328*e0c4386eSCy Schubert
2329*e0c4386eSCy Schubert ret = 1;
2330*e0c4386eSCy Schubert
2331*e0c4386eSCy Schubert err:
2332*e0c4386eSCy Schubert EC_POINT_free(Pinf);
2333*e0c4386eSCy Schubert OPENSSL_free(hex);
2334*e0c4386eSCy Schubert EC_POINT_free(Q);
2335*e0c4386eSCy Schubert
2336*e0c4386eSCy Schubert return ret;
2337*e0c4386eSCy Schubert }
2338*e0c4386eSCy Schubert
2339*e0c4386eSCy Schubert /*
2340*e0c4386eSCy Schubert * This test self-validates EC_POINT_hex2point() and EC_POINT_point2hex()
2341*e0c4386eSCy Schubert */
ec_point_hex2point_test(int id)2342*e0c4386eSCy Schubert static int ec_point_hex2point_test(int id)
2343*e0c4386eSCy Schubert {
2344*e0c4386eSCy Schubert int ret = 0, nid;
2345*e0c4386eSCy Schubert EC_GROUP *group = NULL;
2346*e0c4386eSCy Schubert const EC_POINT *G = NULL;
2347*e0c4386eSCy Schubert EC_POINT *P = NULL;
2348*e0c4386eSCy Schubert BN_CTX * bnctx = NULL;
2349*e0c4386eSCy Schubert
2350*e0c4386eSCy Schubert /* Do some setup */
2351*e0c4386eSCy Schubert nid = curves[id].nid;
2352*e0c4386eSCy Schubert if (!TEST_ptr(bnctx = BN_CTX_new())
2353*e0c4386eSCy Schubert || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
2354*e0c4386eSCy Schubert || !TEST_ptr(G = EC_GROUP_get0_generator(group))
2355*e0c4386eSCy Schubert || !TEST_ptr(P = EC_POINT_dup(G, group)))
2356*e0c4386eSCy Schubert goto err;
2357*e0c4386eSCy Schubert
2358*e0c4386eSCy Schubert if (!TEST_true(ec_point_hex2point_test_helper(group, P,
2359*e0c4386eSCy Schubert POINT_CONVERSION_COMPRESSED,
2360*e0c4386eSCy Schubert bnctx))
2361*e0c4386eSCy Schubert || !TEST_true(ec_point_hex2point_test_helper(group, NULL,
2362*e0c4386eSCy Schubert POINT_CONVERSION_COMPRESSED,
2363*e0c4386eSCy Schubert bnctx))
2364*e0c4386eSCy Schubert || !TEST_true(ec_point_hex2point_test_helper(group, P,
2365*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2366*e0c4386eSCy Schubert bnctx))
2367*e0c4386eSCy Schubert || !TEST_true(ec_point_hex2point_test_helper(group, NULL,
2368*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2369*e0c4386eSCy Schubert bnctx))
2370*e0c4386eSCy Schubert || !TEST_true(ec_point_hex2point_test_helper(group, P,
2371*e0c4386eSCy Schubert POINT_CONVERSION_HYBRID,
2372*e0c4386eSCy Schubert bnctx))
2373*e0c4386eSCy Schubert || !TEST_true(ec_point_hex2point_test_helper(group, NULL,
2374*e0c4386eSCy Schubert POINT_CONVERSION_HYBRID,
2375*e0c4386eSCy Schubert bnctx)))
2376*e0c4386eSCy Schubert goto err;
2377*e0c4386eSCy Schubert
2378*e0c4386eSCy Schubert ret = 1;
2379*e0c4386eSCy Schubert
2380*e0c4386eSCy Schubert err:
2381*e0c4386eSCy Schubert EC_POINT_free(P);
2382*e0c4386eSCy Schubert EC_GROUP_free(group);
2383*e0c4386eSCy Schubert BN_CTX_free(bnctx);
2384*e0c4386eSCy Schubert
2385*e0c4386eSCy Schubert return ret;
2386*e0c4386eSCy Schubert }
2387*e0c4386eSCy Schubert
do_test_custom_explicit_fromdata(EC_GROUP * group,BN_CTX * ctx,unsigned char * gen,int gen_size)2388*e0c4386eSCy Schubert static int do_test_custom_explicit_fromdata(EC_GROUP *group, BN_CTX *ctx,
2389*e0c4386eSCy Schubert unsigned char *gen, int gen_size)
2390*e0c4386eSCy Schubert {
2391*e0c4386eSCy Schubert int ret = 0, i_out;
2392*e0c4386eSCy Schubert EVP_PKEY_CTX *pctx = NULL;
2393*e0c4386eSCy Schubert EVP_PKEY *pkeyparam = NULL;
2394*e0c4386eSCy Schubert OSSL_PARAM_BLD *bld = NULL;
2395*e0c4386eSCy Schubert const char *field_name;
2396*e0c4386eSCy Schubert OSSL_PARAM *params = NULL;
2397*e0c4386eSCy Schubert const OSSL_PARAM *gettable;
2398*e0c4386eSCy Schubert BIGNUM *p, *a, *b;
2399*e0c4386eSCy Schubert BIGNUM *p_out = NULL, *a_out = NULL, *b_out = NULL;
2400*e0c4386eSCy Schubert BIGNUM *order_out = NULL, *cofactor_out = NULL;
2401*e0c4386eSCy Schubert char name[80];
2402*e0c4386eSCy Schubert unsigned char buf[1024];
2403*e0c4386eSCy Schubert size_t buf_len, name_len;
2404*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
2405*e0c4386eSCy Schubert unsigned int k1 = 0, k2 = 0, k3 = 0;
2406*e0c4386eSCy Schubert const char *basis_name = NULL;
2407*e0c4386eSCy Schubert #endif
2408*e0c4386eSCy Schubert
2409*e0c4386eSCy Schubert p = BN_CTX_get(ctx);
2410*e0c4386eSCy Schubert a = BN_CTX_get(ctx);
2411*e0c4386eSCy Schubert b = BN_CTX_get(ctx);
2412*e0c4386eSCy Schubert
2413*e0c4386eSCy Schubert if (!TEST_ptr(b)
2414*e0c4386eSCy Schubert || !TEST_ptr(bld = OSSL_PARAM_BLD_new()))
2415*e0c4386eSCy Schubert goto err;
2416*e0c4386eSCy Schubert
2417*e0c4386eSCy Schubert if (EC_GROUP_get_field_type(group) == NID_X9_62_prime_field) {
2418*e0c4386eSCy Schubert field_name = SN_X9_62_prime_field;
2419*e0c4386eSCy Schubert } else {
2420*e0c4386eSCy Schubert field_name = SN_X9_62_characteristic_two_field;
2421*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
2422*e0c4386eSCy Schubert if (EC_GROUP_get_basis_type(group) == NID_X9_62_tpBasis) {
2423*e0c4386eSCy Schubert basis_name = SN_X9_62_tpBasis;
2424*e0c4386eSCy Schubert if (!TEST_true(EC_GROUP_get_trinomial_basis(group, &k1)))
2425*e0c4386eSCy Schubert goto err;
2426*e0c4386eSCy Schubert } else {
2427*e0c4386eSCy Schubert basis_name = SN_X9_62_ppBasis;
2428*e0c4386eSCy Schubert if (!TEST_true(EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)))
2429*e0c4386eSCy Schubert goto err;
2430*e0c4386eSCy Schubert }
2431*e0c4386eSCy Schubert #endif /* OPENSSL_NO_EC2M */
2432*e0c4386eSCy Schubert }
2433*e0c4386eSCy Schubert if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx))
2434*e0c4386eSCy Schubert || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
2435*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_FIELD_TYPE, field_name, 0))
2436*e0c4386eSCy Schubert || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, p))
2437*e0c4386eSCy Schubert || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a))
2438*e0c4386eSCy Schubert || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b)))
2439*e0c4386eSCy Schubert goto err;
2440*e0c4386eSCy Schubert
2441*e0c4386eSCy Schubert if (EC_GROUP_get0_seed(group) != NULL) {
2442*e0c4386eSCy Schubert if (!TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
2443*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_SEED, EC_GROUP_get0_seed(group),
2444*e0c4386eSCy Schubert EC_GROUP_get_seed_len(group))))
2445*e0c4386eSCy Schubert goto err;
2446*e0c4386eSCy Schubert }
2447*e0c4386eSCy Schubert if (EC_GROUP_get0_cofactor(group) != NULL) {
2448*e0c4386eSCy Schubert if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
2449*e0c4386eSCy Schubert EC_GROUP_get0_cofactor(group))))
2450*e0c4386eSCy Schubert goto err;
2451*e0c4386eSCy Schubert }
2452*e0c4386eSCy Schubert
2453*e0c4386eSCy Schubert if (!TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
2454*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_GENERATOR, gen, gen_size))
2455*e0c4386eSCy Schubert || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER,
2456*e0c4386eSCy Schubert EC_GROUP_get0_order(group))))
2457*e0c4386eSCy Schubert goto err;
2458*e0c4386eSCy Schubert
2459*e0c4386eSCy Schubert if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
2460*e0c4386eSCy Schubert || !TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL))
2461*e0c4386eSCy Schubert || !TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
2462*e0c4386eSCy Schubert || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkeyparam,
2463*e0c4386eSCy Schubert EVP_PKEY_KEY_PARAMETERS, params), 0))
2464*e0c4386eSCy Schubert goto err;
2465*e0c4386eSCy Schubert
2466*e0c4386eSCy Schubert /*- Check that all the set values are retrievable -*/
2467*e0c4386eSCy Schubert
2468*e0c4386eSCy Schubert /* There should be no match to a group name since the generator changed */
2469*e0c4386eSCy Schubert if (!TEST_false(EVP_PKEY_get_utf8_string_param(pkeyparam,
2470*e0c4386eSCy Schubert OSSL_PKEY_PARAM_GROUP_NAME, name, sizeof(name),
2471*e0c4386eSCy Schubert &name_len)))
2472*e0c4386eSCy Schubert goto err;
2473*e0c4386eSCy Schubert
2474*e0c4386eSCy Schubert /* The encoding should be explicit as it has no group */
2475*e0c4386eSCy Schubert if (!TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam,
2476*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_ENCODING,
2477*e0c4386eSCy Schubert name, sizeof(name), &name_len))
2478*e0c4386eSCy Schubert || !TEST_str_eq(name, OSSL_PKEY_EC_ENCODING_EXPLICIT))
2479*e0c4386eSCy Schubert goto err;
2480*e0c4386eSCy Schubert
2481*e0c4386eSCy Schubert if (!TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam,
2482*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_FIELD_TYPE, name, sizeof(name),
2483*e0c4386eSCy Schubert &name_len))
2484*e0c4386eSCy Schubert || !TEST_str_eq(name, field_name))
2485*e0c4386eSCy Schubert goto err;
2486*e0c4386eSCy Schubert
2487*e0c4386eSCy Schubert if (!TEST_true(EVP_PKEY_get_octet_string_param(pkeyparam,
2488*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_GENERATOR, buf, sizeof(buf), &buf_len))
2489*e0c4386eSCy Schubert || !TEST_mem_eq(buf, (int)buf_len, gen, gen_size))
2490*e0c4386eSCy Schubert goto err;
2491*e0c4386eSCy Schubert
2492*e0c4386eSCy Schubert if (!TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_P, &p_out))
2493*e0c4386eSCy Schubert || !TEST_BN_eq(p_out, p)
2494*e0c4386eSCy Schubert || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_A,
2495*e0c4386eSCy Schubert &a_out))
2496*e0c4386eSCy Schubert || !TEST_BN_eq(a_out, a)
2497*e0c4386eSCy Schubert || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_B,
2498*e0c4386eSCy Schubert &b_out))
2499*e0c4386eSCy Schubert || !TEST_BN_eq(b_out, b)
2500*e0c4386eSCy Schubert || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_ORDER,
2501*e0c4386eSCy Schubert &order_out))
2502*e0c4386eSCy Schubert || !TEST_BN_eq(order_out, EC_GROUP_get0_order(group)))
2503*e0c4386eSCy Schubert goto err;
2504*e0c4386eSCy Schubert
2505*e0c4386eSCy Schubert if (EC_GROUP_get0_cofactor(group) != NULL) {
2506*e0c4386eSCy Schubert if (!TEST_true(EVP_PKEY_get_bn_param(pkeyparam,
2507*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_COFACTOR, &cofactor_out))
2508*e0c4386eSCy Schubert || !TEST_BN_eq(cofactor_out, EC_GROUP_get0_cofactor(group)))
2509*e0c4386eSCy Schubert goto err;
2510*e0c4386eSCy Schubert }
2511*e0c4386eSCy Schubert if (EC_GROUP_get0_seed(group) != NULL) {
2512*e0c4386eSCy Schubert if (!TEST_true(EVP_PKEY_get_octet_string_param(pkeyparam,
2513*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_SEED, buf, sizeof(buf), &buf_len))
2514*e0c4386eSCy Schubert || !TEST_mem_eq(buf, buf_len, EC_GROUP_get0_seed(group),
2515*e0c4386eSCy Schubert EC_GROUP_get_seed_len(group)))
2516*e0c4386eSCy Schubert goto err;
2517*e0c4386eSCy Schubert }
2518*e0c4386eSCy Schubert
2519*e0c4386eSCy Schubert if (EC_GROUP_get_field_type(group) == NID_X9_62_prime_field) {
2520*e0c4386eSCy Schubert /* No extra fields should be set for a prime field */
2521*e0c4386eSCy Schubert if (!TEST_false(EVP_PKEY_get_int_param(pkeyparam,
2522*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_M, &i_out))
2523*e0c4386eSCy Schubert || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
2524*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out))
2525*e0c4386eSCy Schubert || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
2526*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out))
2527*e0c4386eSCy Schubert || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
2528*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out))
2529*e0c4386eSCy Schubert || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
2530*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out))
2531*e0c4386eSCy Schubert || !TEST_false(EVP_PKEY_get_utf8_string_param(pkeyparam,
2532*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_TYPE, name, sizeof(name),
2533*e0c4386eSCy Schubert &name_len)))
2534*e0c4386eSCy Schubert goto err;
2535*e0c4386eSCy Schubert } else {
2536*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
2537*e0c4386eSCy Schubert if (!TEST_true(EVP_PKEY_get_int_param(pkeyparam,
2538*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_M, &i_out))
2539*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_degree(group), i_out)
2540*e0c4386eSCy Schubert || !TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam,
2541*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_TYPE, name, sizeof(name),
2542*e0c4386eSCy Schubert &name_len))
2543*e0c4386eSCy Schubert || !TEST_str_eq(name, basis_name))
2544*e0c4386eSCy Schubert goto err;
2545*e0c4386eSCy Schubert
2546*e0c4386eSCy Schubert if (EC_GROUP_get_basis_type(group) == NID_X9_62_tpBasis) {
2547*e0c4386eSCy Schubert if (!TEST_true(EVP_PKEY_get_int_param(pkeyparam,
2548*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out))
2549*e0c4386eSCy Schubert || !TEST_int_eq(k1, i_out)
2550*e0c4386eSCy Schubert || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
2551*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out))
2552*e0c4386eSCy Schubert || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
2553*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out))
2554*e0c4386eSCy Schubert || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
2555*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out)))
2556*e0c4386eSCy Schubert goto err;
2557*e0c4386eSCy Schubert } else {
2558*e0c4386eSCy Schubert if (!TEST_false(EVP_PKEY_get_int_param(pkeyparam,
2559*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out))
2560*e0c4386eSCy Schubert || !TEST_true(EVP_PKEY_get_int_param(pkeyparam,
2561*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out))
2562*e0c4386eSCy Schubert || !TEST_int_eq(k1, i_out)
2563*e0c4386eSCy Schubert || !TEST_true(EVP_PKEY_get_int_param(pkeyparam,
2564*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out))
2565*e0c4386eSCy Schubert || !TEST_int_eq(k2, i_out)
2566*e0c4386eSCy Schubert || !TEST_true(EVP_PKEY_get_int_param(pkeyparam,
2567*e0c4386eSCy Schubert OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out))
2568*e0c4386eSCy Schubert || !TEST_int_eq(k3, i_out))
2569*e0c4386eSCy Schubert goto err;
2570*e0c4386eSCy Schubert }
2571*e0c4386eSCy Schubert #endif /* OPENSSL_NO_EC2M */
2572*e0c4386eSCy Schubert }
2573*e0c4386eSCy Schubert if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pkeyparam))
2574*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_GROUP_NAME))
2575*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_ENCODING))
2576*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_FIELD_TYPE))
2577*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_P))
2578*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_A))
2579*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_B))
2580*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_GENERATOR))
2581*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_ORDER))
2582*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_COFACTOR))
2583*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_SEED))
2584*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
2585*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_M))
2586*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_TYPE))
2587*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS))
2588*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K1))
2589*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K2))
2590*e0c4386eSCy Schubert || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K3))
2591*e0c4386eSCy Schubert #endif
2592*e0c4386eSCy Schubert )
2593*e0c4386eSCy Schubert goto err;
2594*e0c4386eSCy Schubert ret = 1;
2595*e0c4386eSCy Schubert err:
2596*e0c4386eSCy Schubert BN_free(order_out);
2597*e0c4386eSCy Schubert BN_free(cofactor_out);
2598*e0c4386eSCy Schubert BN_free(a_out);
2599*e0c4386eSCy Schubert BN_free(b_out);
2600*e0c4386eSCy Schubert BN_free(p_out);
2601*e0c4386eSCy Schubert OSSL_PARAM_free(params);
2602*e0c4386eSCy Schubert OSSL_PARAM_BLD_free(bld);
2603*e0c4386eSCy Schubert EVP_PKEY_free(pkeyparam);
2604*e0c4386eSCy Schubert EVP_PKEY_CTX_free(pctx);
2605*e0c4386eSCy Schubert return ret;
2606*e0c4386eSCy Schubert }
2607*e0c4386eSCy Schubert
2608*e0c4386eSCy Schubert /*
2609*e0c4386eSCy Schubert * check the EC_METHOD respects the supplied EC_GROUP_set_generator G
2610*e0c4386eSCy Schubert */
custom_generator_test(int id)2611*e0c4386eSCy Schubert static int custom_generator_test(int id)
2612*e0c4386eSCy Schubert {
2613*e0c4386eSCy Schubert int ret = 0, nid, bsize;
2614*e0c4386eSCy Schubert EC_GROUP *group = NULL;
2615*e0c4386eSCy Schubert EC_POINT *G2 = NULL, *Q1 = NULL, *Q2 = NULL;
2616*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
2617*e0c4386eSCy Schubert BIGNUM *k = NULL;
2618*e0c4386eSCy Schubert unsigned char *b1 = NULL, *b2 = NULL;
2619*e0c4386eSCy Schubert
2620*e0c4386eSCy Schubert /* Do some setup */
2621*e0c4386eSCy Schubert nid = curves[id].nid;
2622*e0c4386eSCy Schubert TEST_note("Curve %s", OBJ_nid2sn(nid));
2623*e0c4386eSCy Schubert if (!TEST_ptr(ctx = BN_CTX_new()))
2624*e0c4386eSCy Schubert return 0;
2625*e0c4386eSCy Schubert
2626*e0c4386eSCy Schubert BN_CTX_start(ctx);
2627*e0c4386eSCy Schubert
2628*e0c4386eSCy Schubert if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid)))
2629*e0c4386eSCy Schubert goto err;
2630*e0c4386eSCy Schubert
2631*e0c4386eSCy Schubert /* expected byte length of encoded points */
2632*e0c4386eSCy Schubert bsize = (EC_GROUP_get_degree(group) + 7) / 8;
2633*e0c4386eSCy Schubert bsize = 1 + 2 * bsize; /* UNCOMPRESSED_POINT format */
2634*e0c4386eSCy Schubert
2635*e0c4386eSCy Schubert if (!TEST_ptr(k = BN_CTX_get(ctx))
2636*e0c4386eSCy Schubert /* fetch a testing scalar k != 0,1 */
2637*e0c4386eSCy Schubert || !TEST_true(BN_rand(k, EC_GROUP_order_bits(group) - 1,
2638*e0c4386eSCy Schubert BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
2639*e0c4386eSCy Schubert /* make k even */
2640*e0c4386eSCy Schubert || !TEST_true(BN_clear_bit(k, 0))
2641*e0c4386eSCy Schubert || !TEST_ptr(G2 = EC_POINT_new(group))
2642*e0c4386eSCy Schubert || !TEST_ptr(Q1 = EC_POINT_new(group))
2643*e0c4386eSCy Schubert /* Q1 := kG */
2644*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, Q1, k, NULL, NULL, ctx))
2645*e0c4386eSCy Schubert /* pull out the bytes of that */
2646*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
2647*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED, NULL,
2648*e0c4386eSCy Schubert 0, ctx), bsize)
2649*e0c4386eSCy Schubert || !TEST_ptr(b1 = OPENSSL_malloc(bsize))
2650*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
2651*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED, b1,
2652*e0c4386eSCy Schubert bsize, ctx), bsize)
2653*e0c4386eSCy Schubert /* new generator is G2 := 2G */
2654*e0c4386eSCy Schubert || !TEST_true(EC_POINT_dbl(group, G2, EC_GROUP_get0_generator(group),
2655*e0c4386eSCy Schubert ctx))
2656*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(group, G2,
2657*e0c4386eSCy Schubert EC_GROUP_get0_order(group),
2658*e0c4386eSCy Schubert EC_GROUP_get0_cofactor(group)))
2659*e0c4386eSCy Schubert || !TEST_ptr(Q2 = EC_POINT_new(group))
2660*e0c4386eSCy Schubert || !TEST_true(BN_rshift1(k, k))
2661*e0c4386eSCy Schubert /* Q2 := k/2 G2 */
2662*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, Q2, k, NULL, NULL, ctx))
2663*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(group, Q2,
2664*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED, NULL,
2665*e0c4386eSCy Schubert 0, ctx), bsize)
2666*e0c4386eSCy Schubert || !TEST_ptr(b2 = OPENSSL_malloc(bsize))
2667*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(group, Q2,
2668*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED, b2,
2669*e0c4386eSCy Schubert bsize, ctx), bsize)
2670*e0c4386eSCy Schubert /* Q1 = kG = k/2 G2 = Q2 should hold */
2671*e0c4386eSCy Schubert || !TEST_mem_eq(b1, bsize, b2, bsize))
2672*e0c4386eSCy Schubert goto err;
2673*e0c4386eSCy Schubert
2674*e0c4386eSCy Schubert if (!do_test_custom_explicit_fromdata(group, ctx, b1, bsize))
2675*e0c4386eSCy Schubert goto err;
2676*e0c4386eSCy Schubert
2677*e0c4386eSCy Schubert ret = 1;
2678*e0c4386eSCy Schubert
2679*e0c4386eSCy Schubert err:
2680*e0c4386eSCy Schubert EC_POINT_free(Q1);
2681*e0c4386eSCy Schubert EC_POINT_free(Q2);
2682*e0c4386eSCy Schubert EC_POINT_free(G2);
2683*e0c4386eSCy Schubert EC_GROUP_free(group);
2684*e0c4386eSCy Schubert BN_CTX_end(ctx);
2685*e0c4386eSCy Schubert BN_CTX_free(ctx);
2686*e0c4386eSCy Schubert OPENSSL_free(b1);
2687*e0c4386eSCy Schubert OPENSSL_free(b2);
2688*e0c4386eSCy Schubert
2689*e0c4386eSCy Schubert return ret;
2690*e0c4386eSCy Schubert }
2691*e0c4386eSCy Schubert
2692*e0c4386eSCy Schubert /*
2693*e0c4386eSCy Schubert * check creation of curves from explicit params through the public API
2694*e0c4386eSCy Schubert */
custom_params_test(int id)2695*e0c4386eSCy Schubert static int custom_params_test(int id)
2696*e0c4386eSCy Schubert {
2697*e0c4386eSCy Schubert int ret = 0, nid, bsize;
2698*e0c4386eSCy Schubert const char *curve_name = NULL;
2699*e0c4386eSCy Schubert EC_GROUP *group = NULL, *altgroup = NULL;
2700*e0c4386eSCy Schubert EC_POINT *G2 = NULL, *Q1 = NULL, *Q2 = NULL;
2701*e0c4386eSCy Schubert const EC_POINT *Q = NULL;
2702*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
2703*e0c4386eSCy Schubert BIGNUM *k = NULL;
2704*e0c4386eSCy Schubert unsigned char *buf1 = NULL, *buf2 = NULL;
2705*e0c4386eSCy Schubert const BIGNUM *z = NULL, *cof = NULL, *priv1 = NULL;
2706*e0c4386eSCy Schubert BIGNUM *p = NULL, *a = NULL, *b = NULL;
2707*e0c4386eSCy Schubert int is_prime = 0;
2708*e0c4386eSCy Schubert EC_KEY *eckey1 = NULL, *eckey2 = NULL;
2709*e0c4386eSCy Schubert EVP_PKEY *pkey1 = NULL, *pkey2 = NULL;
2710*e0c4386eSCy Schubert EVP_PKEY_CTX *pctx1 = NULL, *pctx2 = NULL;
2711*e0c4386eSCy Schubert size_t sslen, t;
2712*e0c4386eSCy Schubert unsigned char *pub1 = NULL , *pub2 = NULL;
2713*e0c4386eSCy Schubert OSSL_PARAM_BLD *param_bld = NULL;
2714*e0c4386eSCy Schubert OSSL_PARAM *params1 = NULL, *params2 = NULL;
2715*e0c4386eSCy Schubert
2716*e0c4386eSCy Schubert /* Do some setup */
2717*e0c4386eSCy Schubert nid = curves[id].nid;
2718*e0c4386eSCy Schubert curve_name = OBJ_nid2sn(nid);
2719*e0c4386eSCy Schubert TEST_note("Curve %s", curve_name);
2720*e0c4386eSCy Schubert
2721*e0c4386eSCy Schubert if (nid == NID_sm2)
2722*e0c4386eSCy Schubert return TEST_skip("custom params not supported with SM2");
2723*e0c4386eSCy Schubert
2724*e0c4386eSCy Schubert if (!TEST_ptr(ctx = BN_CTX_new()))
2725*e0c4386eSCy Schubert return 0;
2726*e0c4386eSCy Schubert
2727*e0c4386eSCy Schubert BN_CTX_start(ctx);
2728*e0c4386eSCy Schubert if (!TEST_ptr(p = BN_CTX_get(ctx))
2729*e0c4386eSCy Schubert || !TEST_ptr(a = BN_CTX_get(ctx))
2730*e0c4386eSCy Schubert || !TEST_ptr(b = BN_CTX_get(ctx))
2731*e0c4386eSCy Schubert || !TEST_ptr(k = BN_CTX_get(ctx)))
2732*e0c4386eSCy Schubert goto err;
2733*e0c4386eSCy Schubert
2734*e0c4386eSCy Schubert if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid)))
2735*e0c4386eSCy Schubert goto err;
2736*e0c4386eSCy Schubert
2737*e0c4386eSCy Schubert is_prime = EC_GROUP_get_field_type(group) == NID_X9_62_prime_field;
2738*e0c4386eSCy Schubert #ifdef OPENSSL_NO_EC2M
2739*e0c4386eSCy Schubert if (!is_prime) {
2740*e0c4386eSCy Schubert ret = TEST_skip("binary curves not supported in this build");
2741*e0c4386eSCy Schubert goto err;
2742*e0c4386eSCy Schubert }
2743*e0c4386eSCy Schubert #endif
2744*e0c4386eSCy Schubert
2745*e0c4386eSCy Schubert /* expected byte length of encoded points */
2746*e0c4386eSCy Schubert bsize = (EC_GROUP_get_degree(group) + 7) / 8;
2747*e0c4386eSCy Schubert bsize = 1 + 2 * bsize; /* UNCOMPRESSED_POINT format */
2748*e0c4386eSCy Schubert
2749*e0c4386eSCy Schubert /* extract parameters from built-in curve */
2750*e0c4386eSCy Schubert if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx))
2751*e0c4386eSCy Schubert || !TEST_ptr(G2 = EC_POINT_new(group))
2752*e0c4386eSCy Schubert /* new generator is G2 := 2G */
2753*e0c4386eSCy Schubert || !TEST_true(EC_POINT_dbl(group, G2,
2754*e0c4386eSCy Schubert EC_GROUP_get0_generator(group), ctx))
2755*e0c4386eSCy Schubert /* pull out the bytes of that */
2756*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(group, G2,
2757*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2758*e0c4386eSCy Schubert NULL, 0, ctx), bsize)
2759*e0c4386eSCy Schubert || !TEST_ptr(buf1 = OPENSSL_malloc(bsize))
2760*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(group, G2,
2761*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2762*e0c4386eSCy Schubert buf1, bsize, ctx), bsize)
2763*e0c4386eSCy Schubert || !TEST_ptr(z = EC_GROUP_get0_order(group))
2764*e0c4386eSCy Schubert || !TEST_ptr(cof = EC_GROUP_get0_cofactor(group))
2765*e0c4386eSCy Schubert )
2766*e0c4386eSCy Schubert goto err;
2767*e0c4386eSCy Schubert
2768*e0c4386eSCy Schubert /* create a new group using same params (but different generator) */
2769*e0c4386eSCy Schubert if (is_prime) {
2770*e0c4386eSCy Schubert if (!TEST_ptr(altgroup = EC_GROUP_new_curve_GFp(p, a, b, ctx)))
2771*e0c4386eSCy Schubert goto err;
2772*e0c4386eSCy Schubert }
2773*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
2774*e0c4386eSCy Schubert else {
2775*e0c4386eSCy Schubert if (!TEST_ptr(altgroup = EC_GROUP_new_curve_GF2m(p, a, b, ctx)))
2776*e0c4386eSCy Schubert goto err;
2777*e0c4386eSCy Schubert }
2778*e0c4386eSCy Schubert #endif
2779*e0c4386eSCy Schubert
2780*e0c4386eSCy Schubert /* set 2*G as the generator of altgroup */
2781*e0c4386eSCy Schubert EC_POINT_free(G2); /* discard G2 as it refers to the original group */
2782*e0c4386eSCy Schubert if (!TEST_ptr(G2 = EC_POINT_new(altgroup))
2783*e0c4386eSCy Schubert || !TEST_true(EC_POINT_oct2point(altgroup, G2, buf1, bsize, ctx))
2784*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_is_on_curve(altgroup, G2, ctx), 1)
2785*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_generator(altgroup, G2, z, cof))
2786*e0c4386eSCy Schubert )
2787*e0c4386eSCy Schubert goto err;
2788*e0c4386eSCy Schubert
2789*e0c4386eSCy Schubert /* verify math checks out */
2790*e0c4386eSCy Schubert if (/* allocate temporary points on group and altgroup */
2791*e0c4386eSCy Schubert !TEST_ptr(Q1 = EC_POINT_new(group))
2792*e0c4386eSCy Schubert || !TEST_ptr(Q2 = EC_POINT_new(altgroup))
2793*e0c4386eSCy Schubert /* fetch a testing scalar k != 0,1 */
2794*e0c4386eSCy Schubert || !TEST_true(BN_rand(k, EC_GROUP_order_bits(group) - 1,
2795*e0c4386eSCy Schubert BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
2796*e0c4386eSCy Schubert /* make k even */
2797*e0c4386eSCy Schubert || !TEST_true(BN_clear_bit(k, 0))
2798*e0c4386eSCy Schubert /* Q1 := kG on group */
2799*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(group, Q1, k, NULL, NULL, ctx))
2800*e0c4386eSCy Schubert /* pull out the bytes of that */
2801*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
2802*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2803*e0c4386eSCy Schubert NULL, 0, ctx), bsize)
2804*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
2805*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2806*e0c4386eSCy Schubert buf1, bsize, ctx), bsize)
2807*e0c4386eSCy Schubert /* k := k/2 */
2808*e0c4386eSCy Schubert || !TEST_true(BN_rshift1(k, k))
2809*e0c4386eSCy Schubert /* Q2 := k/2 G2 on altgroup */
2810*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(altgroup, Q2, k, NULL, NULL, ctx))
2811*e0c4386eSCy Schubert /* pull out the bytes of that */
2812*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q2,
2813*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2814*e0c4386eSCy Schubert NULL, 0, ctx), bsize)
2815*e0c4386eSCy Schubert || !TEST_ptr(buf2 = OPENSSL_malloc(bsize))
2816*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q2,
2817*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2818*e0c4386eSCy Schubert buf2, bsize, ctx), bsize)
2819*e0c4386eSCy Schubert /* Q1 = kG = k/2 G2 = Q2 should hold */
2820*e0c4386eSCy Schubert || !TEST_mem_eq(buf1, bsize, buf2, bsize))
2821*e0c4386eSCy Schubert goto err;
2822*e0c4386eSCy Schubert
2823*e0c4386eSCy Schubert /* create two `EC_KEY`s on altgroup */
2824*e0c4386eSCy Schubert if (!TEST_ptr(eckey1 = EC_KEY_new())
2825*e0c4386eSCy Schubert || !TEST_true(EC_KEY_set_group(eckey1, altgroup))
2826*e0c4386eSCy Schubert || !TEST_true(EC_KEY_generate_key(eckey1))
2827*e0c4386eSCy Schubert || !TEST_ptr(eckey2 = EC_KEY_new())
2828*e0c4386eSCy Schubert || !TEST_true(EC_KEY_set_group(eckey2, altgroup))
2829*e0c4386eSCy Schubert || !TEST_true(EC_KEY_generate_key(eckey2)))
2830*e0c4386eSCy Schubert goto err;
2831*e0c4386eSCy Schubert
2832*e0c4386eSCy Schubert /* retrieve priv1 for later */
2833*e0c4386eSCy Schubert if (!TEST_ptr(priv1 = EC_KEY_get0_private_key(eckey1)))
2834*e0c4386eSCy Schubert goto err;
2835*e0c4386eSCy Schubert
2836*e0c4386eSCy Schubert /*
2837*e0c4386eSCy Schubert * retrieve bytes for pub1 for later
2838*e0c4386eSCy Schubert *
2839*e0c4386eSCy Schubert * We compute the pub key in the original group as we will later use it to
2840*e0c4386eSCy Schubert * define a provider key in the built-in group.
2841*e0c4386eSCy Schubert */
2842*e0c4386eSCy Schubert if (!TEST_true(EC_POINT_mul(group, Q1, priv1, NULL, NULL, ctx))
2843*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
2844*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2845*e0c4386eSCy Schubert NULL, 0, ctx), bsize)
2846*e0c4386eSCy Schubert || !TEST_ptr(pub1 = OPENSSL_malloc(bsize))
2847*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
2848*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2849*e0c4386eSCy Schubert pub1, bsize, ctx), bsize))
2850*e0c4386eSCy Schubert goto err;
2851*e0c4386eSCy Schubert
2852*e0c4386eSCy Schubert /* retrieve bytes for pub2 for later */
2853*e0c4386eSCy Schubert if (!TEST_ptr(Q = EC_KEY_get0_public_key(eckey2))
2854*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q,
2855*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2856*e0c4386eSCy Schubert NULL, 0, ctx), bsize)
2857*e0c4386eSCy Schubert || !TEST_ptr(pub2 = OPENSSL_malloc(bsize))
2858*e0c4386eSCy Schubert || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q,
2859*e0c4386eSCy Schubert POINT_CONVERSION_UNCOMPRESSED,
2860*e0c4386eSCy Schubert pub2, bsize, ctx), bsize))
2861*e0c4386eSCy Schubert goto err;
2862*e0c4386eSCy Schubert
2863*e0c4386eSCy Schubert /* create two `EVP_PKEY`s from the `EC_KEY`s */
2864*e0c4386eSCy Schubert if(!TEST_ptr(pkey1 = EVP_PKEY_new())
2865*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_assign_EC_KEY(pkey1, eckey1), 1))
2866*e0c4386eSCy Schubert goto err;
2867*e0c4386eSCy Schubert eckey1 = NULL; /* ownership passed to pkey1 */
2868*e0c4386eSCy Schubert if(!TEST_ptr(pkey2 = EVP_PKEY_new())
2869*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_assign_EC_KEY(pkey2, eckey2), 1))
2870*e0c4386eSCy Schubert goto err;
2871*e0c4386eSCy Schubert eckey2 = NULL; /* ownership passed to pkey2 */
2872*e0c4386eSCy Schubert
2873*e0c4386eSCy Schubert /* Compute keyexchange in both directions */
2874*e0c4386eSCy Schubert if (!TEST_ptr(pctx1 = EVP_PKEY_CTX_new(pkey1, NULL))
2875*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive_init(pctx1), 1)
2876*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx1, pkey2), 1)
2877*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive(pctx1, NULL, &sslen), 1)
2878*e0c4386eSCy Schubert || !TEST_int_gt(bsize, sslen)
2879*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive(pctx1, buf1, &sslen), 1))
2880*e0c4386eSCy Schubert goto err;
2881*e0c4386eSCy Schubert if (!TEST_ptr(pctx2 = EVP_PKEY_CTX_new(pkey2, NULL))
2882*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive_init(pctx2), 1)
2883*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx2, pkey1), 1)
2884*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive(pctx2, NULL, &t), 1)
2885*e0c4386eSCy Schubert || !TEST_int_gt(bsize, t)
2886*e0c4386eSCy Schubert || !TEST_int_le(sslen, t)
2887*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive(pctx2, buf2, &t), 1))
2888*e0c4386eSCy Schubert goto err;
2889*e0c4386eSCy Schubert
2890*e0c4386eSCy Schubert /* Both sides should expect the same shared secret */
2891*e0c4386eSCy Schubert if (!TEST_mem_eq(buf1, sslen, buf2, t))
2892*e0c4386eSCy Schubert goto err;
2893*e0c4386eSCy Schubert
2894*e0c4386eSCy Schubert /* Build parameters for provider-native keys */
2895*e0c4386eSCy Schubert if (!TEST_ptr(param_bld = OSSL_PARAM_BLD_new())
2896*e0c4386eSCy Schubert || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(param_bld,
2897*e0c4386eSCy Schubert OSSL_PKEY_PARAM_GROUP_NAME,
2898*e0c4386eSCy Schubert curve_name, 0))
2899*e0c4386eSCy Schubert || !TEST_true(OSSL_PARAM_BLD_push_octet_string(param_bld,
2900*e0c4386eSCy Schubert OSSL_PKEY_PARAM_PUB_KEY,
2901*e0c4386eSCy Schubert pub1, bsize))
2902*e0c4386eSCy Schubert || !TEST_true(OSSL_PARAM_BLD_push_BN(param_bld,
2903*e0c4386eSCy Schubert OSSL_PKEY_PARAM_PRIV_KEY,
2904*e0c4386eSCy Schubert priv1))
2905*e0c4386eSCy Schubert || !TEST_ptr(params1 = OSSL_PARAM_BLD_to_param(param_bld)))
2906*e0c4386eSCy Schubert goto err;
2907*e0c4386eSCy Schubert
2908*e0c4386eSCy Schubert OSSL_PARAM_BLD_free(param_bld);
2909*e0c4386eSCy Schubert if (!TEST_ptr(param_bld = OSSL_PARAM_BLD_new())
2910*e0c4386eSCy Schubert || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(param_bld,
2911*e0c4386eSCy Schubert OSSL_PKEY_PARAM_GROUP_NAME,
2912*e0c4386eSCy Schubert curve_name, 0))
2913*e0c4386eSCy Schubert || !TEST_true(OSSL_PARAM_BLD_push_octet_string(param_bld,
2914*e0c4386eSCy Schubert OSSL_PKEY_PARAM_PUB_KEY,
2915*e0c4386eSCy Schubert pub2, bsize))
2916*e0c4386eSCy Schubert || !TEST_ptr(params2 = OSSL_PARAM_BLD_to_param(param_bld)))
2917*e0c4386eSCy Schubert goto err;
2918*e0c4386eSCy Schubert
2919*e0c4386eSCy Schubert /* create two new provider-native `EVP_PKEY`s */
2920*e0c4386eSCy Schubert EVP_PKEY_CTX_free(pctx2);
2921*e0c4386eSCy Schubert if (!TEST_ptr(pctx2 = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL))
2922*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx2), 1)
2923*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_fromdata(pctx2, &pkey1, EVP_PKEY_KEYPAIR,
2924*e0c4386eSCy Schubert params1), 1)
2925*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_fromdata(pctx2, &pkey2, EVP_PKEY_PUBLIC_KEY,
2926*e0c4386eSCy Schubert params2), 1))
2927*e0c4386eSCy Schubert goto err;
2928*e0c4386eSCy Schubert
2929*e0c4386eSCy Schubert /* compute keyexchange once more using the provider keys */
2930*e0c4386eSCy Schubert EVP_PKEY_CTX_free(pctx1);
2931*e0c4386eSCy Schubert if (!TEST_ptr(pctx1 = EVP_PKEY_CTX_new(pkey1, NULL))
2932*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive_init(pctx1), 1)
2933*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx1, pkey2), 1)
2934*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive(pctx1, NULL, &t), 1)
2935*e0c4386eSCy Schubert || !TEST_int_gt(bsize, t)
2936*e0c4386eSCy Schubert || !TEST_int_le(sslen, t)
2937*e0c4386eSCy Schubert || !TEST_int_eq(EVP_PKEY_derive(pctx1, buf1, &t), 1)
2938*e0c4386eSCy Schubert /* compare with previous result */
2939*e0c4386eSCy Schubert || !TEST_mem_eq(buf1, t, buf2, sslen))
2940*e0c4386eSCy Schubert goto err;
2941*e0c4386eSCy Schubert
2942*e0c4386eSCy Schubert ret = 1;
2943*e0c4386eSCy Schubert
2944*e0c4386eSCy Schubert err:
2945*e0c4386eSCy Schubert BN_CTX_end(ctx);
2946*e0c4386eSCy Schubert BN_CTX_free(ctx);
2947*e0c4386eSCy Schubert OSSL_PARAM_BLD_free(param_bld);
2948*e0c4386eSCy Schubert OSSL_PARAM_free(params1);
2949*e0c4386eSCy Schubert OSSL_PARAM_free(params2);
2950*e0c4386eSCy Schubert EC_POINT_free(Q1);
2951*e0c4386eSCy Schubert EC_POINT_free(Q2);
2952*e0c4386eSCy Schubert EC_POINT_free(G2);
2953*e0c4386eSCy Schubert EC_GROUP_free(group);
2954*e0c4386eSCy Schubert EC_GROUP_free(altgroup);
2955*e0c4386eSCy Schubert OPENSSL_free(buf1);
2956*e0c4386eSCy Schubert OPENSSL_free(buf2);
2957*e0c4386eSCy Schubert OPENSSL_free(pub1);
2958*e0c4386eSCy Schubert OPENSSL_free(pub2);
2959*e0c4386eSCy Schubert EC_KEY_free(eckey1);
2960*e0c4386eSCy Schubert EC_KEY_free(eckey2);
2961*e0c4386eSCy Schubert EVP_PKEY_free(pkey1);
2962*e0c4386eSCy Schubert EVP_PKEY_free(pkey2);
2963*e0c4386eSCy Schubert EVP_PKEY_CTX_free(pctx1);
2964*e0c4386eSCy Schubert EVP_PKEY_CTX_free(pctx2);
2965*e0c4386eSCy Schubert
2966*e0c4386eSCy Schubert return ret;
2967*e0c4386eSCy Schubert }
2968*e0c4386eSCy Schubert
ec_d2i_publickey_test(void)2969*e0c4386eSCy Schubert static int ec_d2i_publickey_test(void)
2970*e0c4386eSCy Schubert {
2971*e0c4386eSCy Schubert unsigned char buf[1000];
2972*e0c4386eSCy Schubert unsigned char *pubkey_enc = buf;
2973*e0c4386eSCy Schubert const unsigned char *pk_enc = pubkey_enc;
2974*e0c4386eSCy Schubert EVP_PKEY *gen_key = NULL, *decoded_key = NULL;
2975*e0c4386eSCy Schubert EVP_PKEY_CTX *pctx = NULL;
2976*e0c4386eSCy Schubert int pklen, ret = 0;
2977*e0c4386eSCy Schubert OSSL_PARAM params[2];
2978*e0c4386eSCy Schubert
2979*e0c4386eSCy Schubert if (!TEST_ptr(gen_key = EVP_EC_gen("P-256")))
2980*e0c4386eSCy Schubert goto err;
2981*e0c4386eSCy Schubert
2982*e0c4386eSCy Schubert if (!TEST_int_gt(pklen = i2d_PublicKey(gen_key, &pubkey_enc), 0))
2983*e0c4386eSCy Schubert goto err;
2984*e0c4386eSCy Schubert
2985*e0c4386eSCy Schubert params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
2986*e0c4386eSCy Schubert "P-256", 0);
2987*e0c4386eSCy Schubert params[1] = OSSL_PARAM_construct_end();
2988*e0c4386eSCy Schubert
2989*e0c4386eSCy Schubert if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL))
2990*e0c4386eSCy Schubert || !TEST_true(EVP_PKEY_fromdata_init(pctx))
2991*e0c4386eSCy Schubert || !TEST_true(EVP_PKEY_fromdata(pctx, &decoded_key,
2992*e0c4386eSCy Schubert OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
2993*e0c4386eSCy Schubert params))
2994*e0c4386eSCy Schubert || !TEST_ptr(decoded_key)
2995*e0c4386eSCy Schubert || !TEST_ptr(decoded_key = d2i_PublicKey(EVP_PKEY_EC, &decoded_key,
2996*e0c4386eSCy Schubert &pk_enc, pklen)))
2997*e0c4386eSCy Schubert goto err;
2998*e0c4386eSCy Schubert
2999*e0c4386eSCy Schubert if (!TEST_true(EVP_PKEY_eq(gen_key, decoded_key)))
3000*e0c4386eSCy Schubert goto err;
3001*e0c4386eSCy Schubert ret = 1;
3002*e0c4386eSCy Schubert
3003*e0c4386eSCy Schubert err:
3004*e0c4386eSCy Schubert EVP_PKEY_CTX_free(pctx);
3005*e0c4386eSCy Schubert EVP_PKEY_free(gen_key);
3006*e0c4386eSCy Schubert EVP_PKEY_free(decoded_key);
3007*e0c4386eSCy Schubert return ret;
3008*e0c4386eSCy Schubert }
3009*e0c4386eSCy Schubert
setup_tests(void)3010*e0c4386eSCy Schubert int setup_tests(void)
3011*e0c4386eSCy Schubert {
3012*e0c4386eSCy Schubert crv_len = EC_get_builtin_curves(NULL, 0);
3013*e0c4386eSCy Schubert if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
3014*e0c4386eSCy Schubert || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
3015*e0c4386eSCy Schubert return 0;
3016*e0c4386eSCy Schubert
3017*e0c4386eSCy Schubert ADD_TEST(parameter_test);
3018*e0c4386eSCy Schubert ADD_TEST(cofactor_range_test);
3019*e0c4386eSCy Schubert ADD_ALL_TESTS(cardinality_test, crv_len);
3020*e0c4386eSCy Schubert ADD_TEST(prime_field_tests);
3021*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
3022*e0c4386eSCy Schubert ADD_TEST(hybrid_point_encoding_test);
3023*e0c4386eSCy Schubert ADD_TEST(char2_field_tests);
3024*e0c4386eSCy Schubert ADD_ALL_TESTS(char2_curve_test, OSSL_NELEM(char2_curve_tests));
3025*e0c4386eSCy Schubert #endif
3026*e0c4386eSCy Schubert ADD_ALL_TESTS(nistp_single_test, OSSL_NELEM(nistp_tests_params));
3027*e0c4386eSCy Schubert ADD_ALL_TESTS(internal_curve_test, crv_len);
3028*e0c4386eSCy Schubert ADD_ALL_TESTS(internal_curve_test_method, crv_len);
3029*e0c4386eSCy Schubert ADD_TEST(group_field_test);
3030*e0c4386eSCy Schubert ADD_ALL_TESTS(check_named_curve_test, crv_len);
3031*e0c4386eSCy Schubert ADD_ALL_TESTS(check_named_curve_lookup_test, crv_len);
3032*e0c4386eSCy Schubert ADD_ALL_TESTS(check_ec_key_field_public_range_test, crv_len);
3033*e0c4386eSCy Schubert ADD_ALL_TESTS(check_named_curve_from_ecparameters, crv_len);
3034*e0c4386eSCy Schubert ADD_ALL_TESTS(ec_point_hex2point_test, crv_len);
3035*e0c4386eSCy Schubert ADD_ALL_TESTS(custom_generator_test, crv_len);
3036*e0c4386eSCy Schubert ADD_ALL_TESTS(custom_params_test, crv_len);
3037*e0c4386eSCy Schubert ADD_TEST(ec_d2i_publickey_test);
3038*e0c4386eSCy Schubert return 1;
3039*e0c4386eSCy Schubert }
3040*e0c4386eSCy Schubert
cleanup_tests(void)3041*e0c4386eSCy Schubert void cleanup_tests(void)
3042*e0c4386eSCy Schubert {
3043*e0c4386eSCy Schubert OPENSSL_free(curves);
3044*e0c4386eSCy Schubert }
3045