1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert /*
11*e0c4386eSCy Schubert * Low level APIs are deprecated for public use, but still ok for internal use.
12*e0c4386eSCy Schubert */
13*e0c4386eSCy Schubert #include "internal/deprecated.h"
14*e0c4386eSCy Schubert
15*e0c4386eSCy Schubert #include "internal/nelem.h"
16*e0c4386eSCy Schubert #include "testutil.h"
17*e0c4386eSCy Schubert #include <openssl/ec.h>
18*e0c4386eSCy Schubert #include "ec_local.h"
19*e0c4386eSCy Schubert #include <openssl/objects.h>
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubert static size_t crv_len = 0;
22*e0c4386eSCy Schubert static EC_builtin_curve *curves = NULL;
23*e0c4386eSCy Schubert
24*e0c4386eSCy Schubert /* sanity checks field_inv function pointer in EC_METHOD */
group_field_tests(const EC_GROUP * group,BN_CTX * ctx)25*e0c4386eSCy Schubert static int group_field_tests(const EC_GROUP *group, BN_CTX *ctx)
26*e0c4386eSCy Schubert {
27*e0c4386eSCy Schubert BIGNUM *a = NULL, *b = NULL, *c = NULL;
28*e0c4386eSCy Schubert int ret = 0;
29*e0c4386eSCy Schubert
30*e0c4386eSCy Schubert if (group->meth->field_inv == NULL || group->meth->field_mul == NULL)
31*e0c4386eSCy Schubert return 1;
32*e0c4386eSCy Schubert
33*e0c4386eSCy Schubert BN_CTX_start(ctx);
34*e0c4386eSCy Schubert a = BN_CTX_get(ctx);
35*e0c4386eSCy Schubert b = BN_CTX_get(ctx);
36*e0c4386eSCy Schubert if (!TEST_ptr(c = BN_CTX_get(ctx))
37*e0c4386eSCy Schubert /* 1/1 = 1 */
38*e0c4386eSCy Schubert || !TEST_true(group->meth->field_inv(group, b, BN_value_one(), ctx))
39*e0c4386eSCy Schubert || !TEST_true(BN_is_one(b))
40*e0c4386eSCy Schubert /* (1/a)*a = 1 */
41*e0c4386eSCy Schubert || !TEST_true(BN_rand(a, BN_num_bits(group->field) - 1,
42*e0c4386eSCy Schubert BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
43*e0c4386eSCy Schubert || !TEST_true(group->meth->field_inv(group, b, a, ctx))
44*e0c4386eSCy Schubert || (group->meth->field_encode &&
45*e0c4386eSCy Schubert !TEST_true(group->meth->field_encode(group, a, a, ctx)))
46*e0c4386eSCy Schubert || (group->meth->field_encode &&
47*e0c4386eSCy Schubert !TEST_true(group->meth->field_encode(group, b, b, ctx)))
48*e0c4386eSCy Schubert || !TEST_true(group->meth->field_mul(group, c, a, b, ctx))
49*e0c4386eSCy Schubert || (group->meth->field_decode &&
50*e0c4386eSCy Schubert !TEST_true(group->meth->field_decode(group, c, c, ctx)))
51*e0c4386eSCy Schubert || !TEST_true(BN_is_one(c)))
52*e0c4386eSCy Schubert goto err;
53*e0c4386eSCy Schubert
54*e0c4386eSCy Schubert /* 1/0 = error */
55*e0c4386eSCy Schubert BN_zero(a);
56*e0c4386eSCy Schubert if (!TEST_false(group->meth->field_inv(group, b, a, ctx))
57*e0c4386eSCy Schubert || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)
58*e0c4386eSCy Schubert || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) ==
59*e0c4386eSCy Schubert EC_R_CANNOT_INVERT)
60*e0c4386eSCy Schubert /* 1/p = error */
61*e0c4386eSCy Schubert || !TEST_false(group->meth->field_inv(group, b, group->field, ctx))
62*e0c4386eSCy Schubert || !TEST_true(ERR_GET_LIB(ERR_peek_last_error()) == ERR_LIB_EC)
63*e0c4386eSCy Schubert || !TEST_true(ERR_GET_REASON(ERR_peek_last_error()) ==
64*e0c4386eSCy Schubert EC_R_CANNOT_INVERT))
65*e0c4386eSCy Schubert goto err;
66*e0c4386eSCy Schubert
67*e0c4386eSCy Schubert ERR_clear_error();
68*e0c4386eSCy Schubert ret = 1;
69*e0c4386eSCy Schubert err:
70*e0c4386eSCy Schubert BN_CTX_end(ctx);
71*e0c4386eSCy Schubert return ret;
72*e0c4386eSCy Schubert }
73*e0c4386eSCy Schubert
74*e0c4386eSCy Schubert /* wrapper for group_field_tests for explicit curve params and EC_METHOD */
field_tests(const EC_METHOD * meth,const unsigned char * params,int len)75*e0c4386eSCy Schubert static int field_tests(const EC_METHOD *meth, const unsigned char *params,
76*e0c4386eSCy Schubert int len)
77*e0c4386eSCy Schubert {
78*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
79*e0c4386eSCy Schubert BIGNUM *p = NULL, *a = NULL, *b = NULL;
80*e0c4386eSCy Schubert EC_GROUP *group = NULL;
81*e0c4386eSCy Schubert int ret = 0;
82*e0c4386eSCy Schubert
83*e0c4386eSCy Schubert if (!TEST_ptr(ctx = BN_CTX_new()))
84*e0c4386eSCy Schubert return 0;
85*e0c4386eSCy Schubert
86*e0c4386eSCy Schubert BN_CTX_start(ctx);
87*e0c4386eSCy Schubert p = BN_CTX_get(ctx);
88*e0c4386eSCy Schubert a = BN_CTX_get(ctx);
89*e0c4386eSCy Schubert if (!TEST_ptr(b = BN_CTX_get(ctx))
90*e0c4386eSCy Schubert || !TEST_ptr(group = EC_GROUP_new(meth))
91*e0c4386eSCy Schubert || !TEST_true(BN_bin2bn(params, len, p))
92*e0c4386eSCy Schubert || !TEST_true(BN_bin2bn(params + len, len, a))
93*e0c4386eSCy Schubert || !TEST_true(BN_bin2bn(params + 2 * len, len, b))
94*e0c4386eSCy Schubert || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
95*e0c4386eSCy Schubert || !group_field_tests(group, ctx))
96*e0c4386eSCy Schubert goto err;
97*e0c4386eSCy Schubert ret = 1;
98*e0c4386eSCy Schubert
99*e0c4386eSCy Schubert err:
100*e0c4386eSCy Schubert BN_CTX_end(ctx);
101*e0c4386eSCy Schubert BN_CTX_free(ctx);
102*e0c4386eSCy Schubert if (group != NULL)
103*e0c4386eSCy Schubert EC_GROUP_free(group);
104*e0c4386eSCy Schubert return ret;
105*e0c4386eSCy Schubert }
106*e0c4386eSCy Schubert
107*e0c4386eSCy Schubert /* NIST prime curve P-256 */
108*e0c4386eSCy Schubert static const unsigned char params_p256[] = {
109*e0c4386eSCy Schubert /* p */
110*e0c4386eSCy Schubert 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
111*e0c4386eSCy Schubert 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
112*e0c4386eSCy Schubert 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
113*e0c4386eSCy Schubert /* a */
114*e0c4386eSCy Schubert 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
115*e0c4386eSCy Schubert 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
116*e0c4386eSCy Schubert 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC,
117*e0c4386eSCy Schubert /* b */
118*e0c4386eSCy Schubert 0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7, 0xB3, 0xEB, 0xBD, 0x55,
119*e0c4386eSCy Schubert 0x76, 0x98, 0x86, 0xBC, 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
120*e0c4386eSCy Schubert 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B
121*e0c4386eSCy Schubert };
122*e0c4386eSCy Schubert
123*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
124*e0c4386eSCy Schubert /* NIST binary curve B-283 */
125*e0c4386eSCy Schubert static const unsigned char params_b283[] = {
126*e0c4386eSCy Schubert /* p */
127*e0c4386eSCy Schubert 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128*e0c4386eSCy Schubert 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129*e0c4386eSCy Schubert 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xA1,
130*e0c4386eSCy Schubert /* a */
131*e0c4386eSCy Schubert 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132*e0c4386eSCy Schubert 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133*e0c4386eSCy Schubert 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
134*e0c4386eSCy Schubert /* b */
135*e0c4386eSCy Schubert 0x02, 0x7B, 0x68, 0x0A, 0xC8, 0xB8, 0x59, 0x6D, 0xA5, 0xA4, 0xAF, 0x8A,
136*e0c4386eSCy Schubert 0x19, 0xA0, 0x30, 0x3F, 0xCA, 0x97, 0xFD, 0x76, 0x45, 0x30, 0x9F, 0xA2,
137*e0c4386eSCy Schubert 0xA5, 0x81, 0x48, 0x5A, 0xF6, 0x26, 0x3E, 0x31, 0x3B, 0x79, 0xA2, 0xF5
138*e0c4386eSCy Schubert };
139*e0c4386eSCy Schubert #endif
140*e0c4386eSCy Schubert
141*e0c4386eSCy Schubert /* test EC_GFp_simple_method directly */
field_tests_ecp_simple(void)142*e0c4386eSCy Schubert static int field_tests_ecp_simple(void)
143*e0c4386eSCy Schubert {
144*e0c4386eSCy Schubert TEST_info("Testing EC_GFp_simple_method()\n");
145*e0c4386eSCy Schubert return field_tests(EC_GFp_simple_method(), params_p256,
146*e0c4386eSCy Schubert sizeof(params_p256) / 3);
147*e0c4386eSCy Schubert }
148*e0c4386eSCy Schubert
149*e0c4386eSCy Schubert /* test EC_GFp_mont_method directly */
field_tests_ecp_mont(void)150*e0c4386eSCy Schubert static int field_tests_ecp_mont(void)
151*e0c4386eSCy Schubert {
152*e0c4386eSCy Schubert TEST_info("Testing EC_GFp_mont_method()\n");
153*e0c4386eSCy Schubert return field_tests(EC_GFp_mont_method(), params_p256,
154*e0c4386eSCy Schubert sizeof(params_p256) / 3);
155*e0c4386eSCy Schubert }
156*e0c4386eSCy Schubert
157*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
158*e0c4386eSCy Schubert /* test EC_GF2m_simple_method directly */
field_tests_ec2_simple(void)159*e0c4386eSCy Schubert static int field_tests_ec2_simple(void)
160*e0c4386eSCy Schubert {
161*e0c4386eSCy Schubert TEST_info("Testing EC_GF2m_simple_method()\n");
162*e0c4386eSCy Schubert return field_tests(EC_GF2m_simple_method(), params_b283,
163*e0c4386eSCy Schubert sizeof(params_b283) / 3);
164*e0c4386eSCy Schubert }
165*e0c4386eSCy Schubert #endif
166*e0c4386eSCy Schubert
167*e0c4386eSCy Schubert /* test default method for a named curve */
field_tests_default(int n)168*e0c4386eSCy Schubert static int field_tests_default(int n)
169*e0c4386eSCy Schubert {
170*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
171*e0c4386eSCy Schubert EC_GROUP *group = NULL;
172*e0c4386eSCy Schubert int nid = curves[n].nid;
173*e0c4386eSCy Schubert int ret = 0;
174*e0c4386eSCy Schubert
175*e0c4386eSCy Schubert TEST_info("Testing curve %s\n", OBJ_nid2sn(nid));
176*e0c4386eSCy Schubert
177*e0c4386eSCy Schubert if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
178*e0c4386eSCy Schubert || !TEST_ptr(ctx = BN_CTX_new())
179*e0c4386eSCy Schubert || !group_field_tests(group, ctx))
180*e0c4386eSCy Schubert goto err;
181*e0c4386eSCy Schubert
182*e0c4386eSCy Schubert ret = 1;
183*e0c4386eSCy Schubert err:
184*e0c4386eSCy Schubert if (group != NULL)
185*e0c4386eSCy Schubert EC_GROUP_free(group);
186*e0c4386eSCy Schubert if (ctx != NULL)
187*e0c4386eSCy Schubert BN_CTX_free(ctx);
188*e0c4386eSCy Schubert return ret;
189*e0c4386eSCy Schubert }
190*e0c4386eSCy Schubert
191*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
192*e0c4386eSCy Schubert /*
193*e0c4386eSCy Schubert * Tests a point known to cause an incorrect underflow in an old version of
194*e0c4386eSCy Schubert * ecp_nist521.c
195*e0c4386eSCy Schubert */
underflow_test(void)196*e0c4386eSCy Schubert static int underflow_test(void)
197*e0c4386eSCy Schubert {
198*e0c4386eSCy Schubert BN_CTX *ctx = NULL;
199*e0c4386eSCy Schubert EC_GROUP *grp = NULL;
200*e0c4386eSCy Schubert EC_POINT *P = NULL, *Q = NULL, *R = NULL;
201*e0c4386eSCy Schubert BIGNUM *x1 = NULL, *y1 = NULL, *z1 = NULL, *x2 = NULL, *y2 = NULL;
202*e0c4386eSCy Schubert BIGNUM *k = NULL;
203*e0c4386eSCy Schubert int testresult = 0;
204*e0c4386eSCy Schubert const char *x1str =
205*e0c4386eSCy Schubert "1534f0077fffffe87e9adcfe000000000000000000003e05a21d2400002e031b1f4"
206*e0c4386eSCy Schubert "b80000c6fafa4f3c1288798d624a247b5e2ffffffffffffffefe099241900004";
207*e0c4386eSCy Schubert const char *p521m1 =
208*e0c4386eSCy Schubert "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
209*e0c4386eSCy Schubert "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe";
210*e0c4386eSCy Schubert
211*e0c4386eSCy Schubert ctx = BN_CTX_new();
212*e0c4386eSCy Schubert if (!TEST_ptr(ctx))
213*e0c4386eSCy Schubert return 0;
214*e0c4386eSCy Schubert
215*e0c4386eSCy Schubert BN_CTX_start(ctx);
216*e0c4386eSCy Schubert x1 = BN_CTX_get(ctx);
217*e0c4386eSCy Schubert y1 = BN_CTX_get(ctx);
218*e0c4386eSCy Schubert z1 = BN_CTX_get(ctx);
219*e0c4386eSCy Schubert x2 = BN_CTX_get(ctx);
220*e0c4386eSCy Schubert y2 = BN_CTX_get(ctx);
221*e0c4386eSCy Schubert k = BN_CTX_get(ctx);
222*e0c4386eSCy Schubert if (!TEST_ptr(k))
223*e0c4386eSCy Schubert goto err;
224*e0c4386eSCy Schubert
225*e0c4386eSCy Schubert grp = EC_GROUP_new_by_curve_name(NID_secp521r1);
226*e0c4386eSCy Schubert P = EC_POINT_new(grp);
227*e0c4386eSCy Schubert Q = EC_POINT_new(grp);
228*e0c4386eSCy Schubert R = EC_POINT_new(grp);
229*e0c4386eSCy Schubert if (!TEST_ptr(grp) || !TEST_ptr(P) || !TEST_ptr(Q) || !TEST_ptr(R))
230*e0c4386eSCy Schubert goto err;
231*e0c4386eSCy Schubert
232*e0c4386eSCy Schubert if (!TEST_int_gt(BN_hex2bn(&x1, x1str), 0)
233*e0c4386eSCy Schubert || !TEST_int_gt(BN_hex2bn(&y1, p521m1), 0)
234*e0c4386eSCy Schubert || !TEST_int_gt(BN_hex2bn(&z1, p521m1), 0)
235*e0c4386eSCy Schubert || !TEST_int_gt(BN_hex2bn(&k, "02"), 0)
236*e0c4386eSCy Schubert || !TEST_true(ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(grp, P, x1,
237*e0c4386eSCy Schubert y1, z1, ctx))
238*e0c4386eSCy Schubert || !TEST_true(EC_POINT_mul(grp, Q, NULL, P, k, ctx))
239*e0c4386eSCy Schubert || !TEST_true(EC_POINT_get_affine_coordinates(grp, Q, x1, y1, ctx))
240*e0c4386eSCy Schubert || !TEST_true(EC_POINT_dbl(grp, R, P, ctx))
241*e0c4386eSCy Schubert || !TEST_true(EC_POINT_get_affine_coordinates(grp, R, x2, y2, ctx)))
242*e0c4386eSCy Schubert goto err;
243*e0c4386eSCy Schubert
244*e0c4386eSCy Schubert if (!TEST_int_eq(BN_cmp(x1, x2), 0)
245*e0c4386eSCy Schubert || !TEST_int_eq(BN_cmp(y1, y2), 0))
246*e0c4386eSCy Schubert goto err;
247*e0c4386eSCy Schubert
248*e0c4386eSCy Schubert testresult = 1;
249*e0c4386eSCy Schubert
250*e0c4386eSCy Schubert err:
251*e0c4386eSCy Schubert BN_CTX_end(ctx);
252*e0c4386eSCy Schubert EC_POINT_free(P);
253*e0c4386eSCy Schubert EC_POINT_free(Q);
254*e0c4386eSCy Schubert EC_POINT_free(R);
255*e0c4386eSCy Schubert EC_GROUP_free(grp);
256*e0c4386eSCy Schubert BN_CTX_free(ctx);
257*e0c4386eSCy Schubert
258*e0c4386eSCy Schubert return testresult;
259*e0c4386eSCy Schubert }
260*e0c4386eSCy Schubert #endif
261*e0c4386eSCy Schubert
262*e0c4386eSCy Schubert /*
263*e0c4386eSCy Schubert * Tests behavior of the EC_KEY_set_private_key
264*e0c4386eSCy Schubert */
set_private_key(void)265*e0c4386eSCy Schubert static int set_private_key(void)
266*e0c4386eSCy Schubert {
267*e0c4386eSCy Schubert EC_KEY *key = NULL, *aux_key = NULL;
268*e0c4386eSCy Schubert int testresult = 0;
269*e0c4386eSCy Schubert
270*e0c4386eSCy Schubert key = EC_KEY_new_by_curve_name(NID_secp224r1);
271*e0c4386eSCy Schubert aux_key = EC_KEY_new_by_curve_name(NID_secp224r1);
272*e0c4386eSCy Schubert if (!TEST_ptr(key)
273*e0c4386eSCy Schubert || !TEST_ptr(aux_key)
274*e0c4386eSCy Schubert || !TEST_int_eq(EC_KEY_generate_key(key), 1)
275*e0c4386eSCy Schubert || !TEST_int_eq(EC_KEY_generate_key(aux_key), 1))
276*e0c4386eSCy Schubert goto err;
277*e0c4386eSCy Schubert
278*e0c4386eSCy Schubert /* Test setting a valid private key */
279*e0c4386eSCy Schubert if (!TEST_int_eq(EC_KEY_set_private_key(key, aux_key->priv_key), 1))
280*e0c4386eSCy Schubert goto err;
281*e0c4386eSCy Schubert
282*e0c4386eSCy Schubert /* Test compliance with legacy behavior for NULL private keys */
283*e0c4386eSCy Schubert if (!TEST_int_eq(EC_KEY_set_private_key(key, NULL), 0)
284*e0c4386eSCy Schubert || !TEST_ptr_null(key->priv_key))
285*e0c4386eSCy Schubert goto err;
286*e0c4386eSCy Schubert
287*e0c4386eSCy Schubert testresult = 1;
288*e0c4386eSCy Schubert
289*e0c4386eSCy Schubert err:
290*e0c4386eSCy Schubert EC_KEY_free(key);
291*e0c4386eSCy Schubert EC_KEY_free(aux_key);
292*e0c4386eSCy Schubert return testresult;
293*e0c4386eSCy Schubert }
294*e0c4386eSCy Schubert
295*e0c4386eSCy Schubert /*
296*e0c4386eSCy Schubert * Tests behavior of the decoded_from_explicit_params flag and API
297*e0c4386eSCy Schubert */
decoded_flag_test(void)298*e0c4386eSCy Schubert static int decoded_flag_test(void)
299*e0c4386eSCy Schubert {
300*e0c4386eSCy Schubert EC_GROUP *grp;
301*e0c4386eSCy Schubert EC_GROUP *grp_copy = NULL;
302*e0c4386eSCy Schubert ECPARAMETERS *ecparams = NULL;
303*e0c4386eSCy Schubert ECPKPARAMETERS *ecpkparams = NULL;
304*e0c4386eSCy Schubert EC_KEY *key = NULL;
305*e0c4386eSCy Schubert unsigned char *encodedparams = NULL;
306*e0c4386eSCy Schubert const unsigned char *encp;
307*e0c4386eSCy Schubert int encodedlen;
308*e0c4386eSCy Schubert int testresult = 0;
309*e0c4386eSCy Schubert
310*e0c4386eSCy Schubert /* Test EC_GROUP_new not setting the flag */
311*e0c4386eSCy Schubert grp = EC_GROUP_new(EC_GFp_simple_method());
312*e0c4386eSCy Schubert if (!TEST_ptr(grp)
313*e0c4386eSCy Schubert || !TEST_int_eq(grp->decoded_from_explicit_params, 0))
314*e0c4386eSCy Schubert goto err;
315*e0c4386eSCy Schubert EC_GROUP_free(grp);
316*e0c4386eSCy Schubert
317*e0c4386eSCy Schubert /* Test EC_GROUP_new_by_curve_name not setting the flag */
318*e0c4386eSCy Schubert grp = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
319*e0c4386eSCy Schubert if (!TEST_ptr(grp)
320*e0c4386eSCy Schubert || !TEST_int_eq(grp->decoded_from_explicit_params, 0))
321*e0c4386eSCy Schubert goto err;
322*e0c4386eSCy Schubert
323*e0c4386eSCy Schubert /* Test EC_GROUP_new_from_ecparameters not setting the flag */
324*e0c4386eSCy Schubert if (!TEST_ptr(ecparams = EC_GROUP_get_ecparameters(grp, NULL))
325*e0c4386eSCy Schubert || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecparameters(ecparams))
326*e0c4386eSCy Schubert || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0))
327*e0c4386eSCy Schubert goto err;
328*e0c4386eSCy Schubert EC_GROUP_free(grp_copy);
329*e0c4386eSCy Schubert grp_copy = NULL;
330*e0c4386eSCy Schubert ECPARAMETERS_free(ecparams);
331*e0c4386eSCy Schubert ecparams = NULL;
332*e0c4386eSCy Schubert
333*e0c4386eSCy Schubert /* Test EC_GROUP_new_from_ecpkparameters not setting the flag */
334*e0c4386eSCy Schubert if (!TEST_int_eq(EC_GROUP_get_asn1_flag(grp), OPENSSL_EC_NAMED_CURVE)
335*e0c4386eSCy Schubert || !TEST_ptr(ecpkparams = EC_GROUP_get_ecpkparameters(grp, NULL))
336*e0c4386eSCy Schubert || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecpkparameters(ecpkparams))
337*e0c4386eSCy Schubert || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0)
338*e0c4386eSCy Schubert || !TEST_ptr(key = EC_KEY_new())
339*e0c4386eSCy Schubert /* Test EC_KEY_decoded_from_explicit_params on key without a group */
340*e0c4386eSCy Schubert || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), -1)
341*e0c4386eSCy Schubert || !TEST_int_eq(EC_KEY_set_group(key, grp_copy), 1)
342*e0c4386eSCy Schubert /* Test EC_KEY_decoded_from_explicit_params negative case */
343*e0c4386eSCy Schubert || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), 0))
344*e0c4386eSCy Schubert goto err;
345*e0c4386eSCy Schubert EC_GROUP_free(grp_copy);
346*e0c4386eSCy Schubert grp_copy = NULL;
347*e0c4386eSCy Schubert ECPKPARAMETERS_free(ecpkparams);
348*e0c4386eSCy Schubert ecpkparams = NULL;
349*e0c4386eSCy Schubert
350*e0c4386eSCy Schubert /* Test d2i_ECPKParameters with named params not setting the flag */
351*e0c4386eSCy Schubert if (!TEST_int_gt(encodedlen = i2d_ECPKParameters(grp, &encodedparams), 0)
352*e0c4386eSCy Schubert || !TEST_ptr(encp = encodedparams)
353*e0c4386eSCy Schubert || !TEST_ptr(grp_copy = d2i_ECPKParameters(NULL, &encp, encodedlen))
354*e0c4386eSCy Schubert || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0))
355*e0c4386eSCy Schubert goto err;
356*e0c4386eSCy Schubert EC_GROUP_free(grp_copy);
357*e0c4386eSCy Schubert grp_copy = NULL;
358*e0c4386eSCy Schubert OPENSSL_free(encodedparams);
359*e0c4386eSCy Schubert encodedparams = NULL;
360*e0c4386eSCy Schubert
361*e0c4386eSCy Schubert /* Asn1 flag stays set to explicit with EC_GROUP_new_from_ecpkparameters */
362*e0c4386eSCy Schubert EC_GROUP_set_asn1_flag(grp, OPENSSL_EC_EXPLICIT_CURVE);
363*e0c4386eSCy Schubert if (!TEST_ptr(ecpkparams = EC_GROUP_get_ecpkparameters(grp, NULL))
364*e0c4386eSCy Schubert || !TEST_ptr(grp_copy = EC_GROUP_new_from_ecpkparameters(ecpkparams))
365*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_asn1_flag(grp_copy), OPENSSL_EC_EXPLICIT_CURVE)
366*e0c4386eSCy Schubert || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 0))
367*e0c4386eSCy Schubert goto err;
368*e0c4386eSCy Schubert EC_GROUP_free(grp_copy);
369*e0c4386eSCy Schubert grp_copy = NULL;
370*e0c4386eSCy Schubert
371*e0c4386eSCy Schubert /* Test d2i_ECPKParameters with explicit params setting the flag */
372*e0c4386eSCy Schubert if (!TEST_int_gt(encodedlen = i2d_ECPKParameters(grp, &encodedparams), 0)
373*e0c4386eSCy Schubert || !TEST_ptr(encp = encodedparams)
374*e0c4386eSCy Schubert || !TEST_ptr(grp_copy = d2i_ECPKParameters(NULL, &encp, encodedlen))
375*e0c4386eSCy Schubert || !TEST_int_eq(EC_GROUP_get_asn1_flag(grp_copy), OPENSSL_EC_EXPLICIT_CURVE)
376*e0c4386eSCy Schubert || !TEST_int_eq(grp_copy->decoded_from_explicit_params, 1)
377*e0c4386eSCy Schubert || !TEST_int_eq(EC_KEY_set_group(key, grp_copy), 1)
378*e0c4386eSCy Schubert /* Test EC_KEY_decoded_from_explicit_params positive case */
379*e0c4386eSCy Schubert || !TEST_int_eq(EC_KEY_decoded_from_explicit_params(key), 1))
380*e0c4386eSCy Schubert goto err;
381*e0c4386eSCy Schubert
382*e0c4386eSCy Schubert testresult = 1;
383*e0c4386eSCy Schubert
384*e0c4386eSCy Schubert err:
385*e0c4386eSCy Schubert EC_KEY_free(key);
386*e0c4386eSCy Schubert EC_GROUP_free(grp);
387*e0c4386eSCy Schubert EC_GROUP_free(grp_copy);
388*e0c4386eSCy Schubert ECPARAMETERS_free(ecparams);
389*e0c4386eSCy Schubert ECPKPARAMETERS_free(ecpkparams);
390*e0c4386eSCy Schubert OPENSSL_free(encodedparams);
391*e0c4386eSCy Schubert
392*e0c4386eSCy Schubert return testresult;
393*e0c4386eSCy Schubert }
394*e0c4386eSCy Schubert
395*e0c4386eSCy Schubert static
ecpkparams_i2d2i_test(int n)396*e0c4386eSCy Schubert int ecpkparams_i2d2i_test(int n)
397*e0c4386eSCy Schubert {
398*e0c4386eSCy Schubert EC_GROUP *g1 = NULL, *g2 = NULL;
399*e0c4386eSCy Schubert FILE *fp = NULL;
400*e0c4386eSCy Schubert int nid = curves[n].nid;
401*e0c4386eSCy Schubert int testresult = 0;
402*e0c4386eSCy Schubert
403*e0c4386eSCy Schubert /* create group */
404*e0c4386eSCy Schubert if (!TEST_ptr(g1 = EC_GROUP_new_by_curve_name(nid)))
405*e0c4386eSCy Schubert goto end;
406*e0c4386eSCy Schubert
407*e0c4386eSCy Schubert /* encode params to file */
408*e0c4386eSCy Schubert if (!TEST_ptr(fp = fopen("params.der", "wb"))
409*e0c4386eSCy Schubert || !TEST_true(i2d_ECPKParameters_fp(fp, g1)))
410*e0c4386eSCy Schubert goto end;
411*e0c4386eSCy Schubert
412*e0c4386eSCy Schubert /* flush and close file */
413*e0c4386eSCy Schubert if (!TEST_int_eq(fclose(fp), 0)) {
414*e0c4386eSCy Schubert fp = NULL;
415*e0c4386eSCy Schubert goto end;
416*e0c4386eSCy Schubert }
417*e0c4386eSCy Schubert fp = NULL;
418*e0c4386eSCy Schubert
419*e0c4386eSCy Schubert /* decode params from file */
420*e0c4386eSCy Schubert if (!TEST_ptr(fp = fopen("params.der", "rb"))
421*e0c4386eSCy Schubert || !TEST_ptr(g2 = d2i_ECPKParameters_fp(fp, NULL)))
422*e0c4386eSCy Schubert goto end;
423*e0c4386eSCy Schubert
424*e0c4386eSCy Schubert testresult = 1; /* PASS */
425*e0c4386eSCy Schubert
426*e0c4386eSCy Schubert end:
427*e0c4386eSCy Schubert if (fp != NULL)
428*e0c4386eSCy Schubert fclose(fp);
429*e0c4386eSCy Schubert
430*e0c4386eSCy Schubert EC_GROUP_free(g1);
431*e0c4386eSCy Schubert EC_GROUP_free(g2);
432*e0c4386eSCy Schubert
433*e0c4386eSCy Schubert return testresult;
434*e0c4386eSCy Schubert }
435*e0c4386eSCy Schubert
setup_tests(void)436*e0c4386eSCy Schubert int setup_tests(void)
437*e0c4386eSCy Schubert {
438*e0c4386eSCy Schubert crv_len = EC_get_builtin_curves(NULL, 0);
439*e0c4386eSCy Schubert if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
440*e0c4386eSCy Schubert || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
441*e0c4386eSCy Schubert return 0;
442*e0c4386eSCy Schubert
443*e0c4386eSCy Schubert ADD_TEST(field_tests_ecp_simple);
444*e0c4386eSCy Schubert ADD_TEST(field_tests_ecp_mont);
445*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC2M
446*e0c4386eSCy Schubert ADD_TEST(field_tests_ec2_simple);
447*e0c4386eSCy Schubert #endif
448*e0c4386eSCy Schubert ADD_ALL_TESTS(field_tests_default, crv_len);
449*e0c4386eSCy Schubert #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
450*e0c4386eSCy Schubert ADD_TEST(underflow_test);
451*e0c4386eSCy Schubert #endif
452*e0c4386eSCy Schubert ADD_TEST(set_private_key);
453*e0c4386eSCy Schubert ADD_TEST(decoded_flag_test);
454*e0c4386eSCy Schubert ADD_ALL_TESTS(ecpkparams_i2d2i_test, crv_len);
455*e0c4386eSCy Schubert
456*e0c4386eSCy Schubert return 1;
457*e0c4386eSCy Schubert }
458*e0c4386eSCy Schubert
cleanup_tests(void)459*e0c4386eSCy Schubert void cleanup_tests(void)
460*e0c4386eSCy Schubert {
461*e0c4386eSCy Schubert OPENSSL_free(curves);
462*e0c4386eSCy Schubert }
463