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