]>
Commit | Line | Data |
---|---|---|
35b73a1f | 1 | /* |
2db85ac9 | 2 | * Copyright 2001-2017 The OpenSSL Project Authors. All Rights Reserved. |
62763f68 | 3 | * |
440e5d80 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
62763f68 | 8 | */ |
440e5d80 | 9 | |
7793f30e BM |
10 | /* ==================================================================== |
11 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | |
12 | * | |
0f113f3e | 13 | * Portions of the attached software ("Contribution") are developed by |
7793f30e BM |
14 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
15 | * | |
16 | * The Contribution is licensed pursuant to the OpenSSL open source | |
17 | * license provided above. | |
18 | * | |
0f113f3e | 19 | * The elliptic curve binary polynomial software is originally written by |
7793f30e BM |
20 | * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories. |
21 | * | |
22 | */ | |
62763f68 | 23 | |
2f0ca54c | 24 | #include "e_os.h" |
2db85ac9 | 25 | #include "testutil.h" |
2f0ca54c | 26 | |
2db85ac9 | 27 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
28 | # include <openssl/ec.h> |
29 | # ifndef OPENSSL_NO_ENGINE | |
30 | # include <openssl/engine.h> | |
31 | # endif | |
32 | # include <openssl/err.h> | |
33 | # include <openssl/obj_mac.h> | |
34 | # include <openssl/objects.h> | |
35 | # include <openssl/rand.h> | |
36 | # include <openssl/bn.h> | |
37 | # include <openssl/opensslconf.h> | |
38 | ||
39 | # if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12) | |
0491e058 | 40 | /* suppress "too big too optimize" warning */ |
0f113f3e MC |
41 | # pragma warning(disable:4959) |
42 | # endif | |
43 | ||
2db85ac9 P |
44 | static size_t crv_len = 0; |
45 | static EC_builtin_curve *curves = NULL; | |
652ae06b | 46 | |
04daec86 | 47 | /* test multiplication with group order, long and negative scalars */ |
2db85ac9 | 48 | static int group_order_tests(EC_GROUP *group) |
0f113f3e | 49 | { |
2db85ac9 P |
50 | BIGNUM *n1 = NULL, *n2 = NULL, *order = NULL; |
51 | EC_POINT *P = NULL, *Q = NULL, *R = NULL, *S = NULL; | |
52 | BN_CTX *ctx = NULL; | |
53 | int i = 0, r = 0; | |
54 | ||
55 | if (!TEST_ptr(n1 = BN_new()) | |
56 | || !TEST_ptr(n2 = BN_new()) | |
57 | || !TEST_ptr(order = BN_new()) | |
58 | || !TEST_ptr(ctx = BN_CTX_new()) | |
59 | || !TEST_ptr(P = EC_POINT_new(group)) | |
60 | || !TEST_ptr(Q = EC_POINT_new(group)) | |
61 | || !TEST_ptr(R = EC_POINT_new(group)) | |
62 | || !TEST_ptr(S = EC_POINT_new(group))) | |
63 | goto err; | |
64 | ||
65 | if (!TEST_true(EC_GROUP_get_order(group, order, ctx)) | |
66 | || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) | |
67 | || !TEST_true(EC_POINT_is_at_infinity(group, Q)) | |
68 | || !TEST_true(EC_GROUP_precompute_mult(group, ctx)) | |
69 | || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) | |
70 | || !TEST_true(EC_POINT_is_at_infinity(group, Q))) | |
71 | goto err; | |
72 | ||
0f113f3e MC |
73 | for (i = 1; i <= 2; i++) { |
74 | const BIGNUM *scalars[6]; | |
75 | const EC_POINT *points[6]; | |
76 | ||
2db85ac9 P |
77 | if (!TEST_true(BN_set_word(n1, i)) |
78 | /* | |
79 | * If i == 1, P will be the predefined generator for which | |
80 | * EC_GROUP_precompute_mult has set up precomputation. | |
81 | */ | |
82 | || !TEST_true(EC_POINT_mul(group, P, n1, NULL, NULL, ctx)) | |
83 | || !TEST_true(BN_one(n1)) | |
84 | /* n1 = 1 - order */ | |
85 | || !TEST_true(BN_sub(n1, n1, order)) | |
86 | || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n1, ctx)) | |
87 | || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)) | |
88 | ||
89 | /* n2 = 1 + order */ | |
90 | || !TEST_true(BN_add(n2, order, BN_value_one())) | |
91 | || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx)) | |
92 | || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)) | |
93 | ||
94 | /* n2 = (1 - order) * (1 + order) = 1 - order^2 */ | |
95 | || !TEST_true(BN_mul(n2, n1, n2, ctx)) | |
96 | || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx)) | |
97 | || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))) | |
98 | goto err; | |
0f113f3e MC |
99 | |
100 | /* n2 = order^2 - 1 */ | |
101 | BN_set_negative(n2, 0); | |
2db85ac9 P |
102 | if (!TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx)) |
103 | /* Add P to verify the result. */ | |
104 | || !TEST_true(EC_POINT_add(group, Q, Q, P, ctx)) | |
105 | || !TEST_true(EC_POINT_is_at_infinity(group, Q)) | |
106 | ||
107 | /* Exercise EC_POINTs_mul, including corner cases. */ | |
108 | || !TEST_false(EC_POINT_is_at_infinity(group, P))) | |
109 | goto err; | |
50e34aab AP |
110 | |
111 | scalars[0] = scalars[1] = BN_value_one(); | |
112 | points[0] = points[1] = P; | |
113 | ||
2db85ac9 P |
114 | if (!TEST_true(EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx)) |
115 | || !TEST_true(EC_POINT_dbl(group, S, points[0], ctx)) | |
116 | || !TEST_int_eq(0, EC_POINT_cmp(group, R, S, ctx))) | |
117 | goto err; | |
50e34aab | 118 | |
0f113f3e MC |
119 | scalars[0] = n1; |
120 | points[0] = Q; /* => infinity */ | |
121 | scalars[1] = n2; | |
122 | points[1] = P; /* => -P */ | |
123 | scalars[2] = n1; | |
124 | points[2] = Q; /* => infinity */ | |
125 | scalars[3] = n2; | |
126 | points[3] = Q; /* => infinity */ | |
127 | scalars[4] = n1; | |
128 | points[4] = P; /* => P */ | |
129 | scalars[5] = n2; | |
130 | points[5] = Q; /* => infinity */ | |
2db85ac9 P |
131 | if (!TEST_true(EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx)) |
132 | || !TEST_true(EC_POINT_is_at_infinity(group, P))) | |
133 | goto err; | |
0f113f3e | 134 | } |
0f113f3e | 135 | |
2db85ac9 P |
136 | r = 1; |
137 | err: | |
138 | if (r == 0 && i != 0) | |
139 | TEST_info(i == 1 ? "allowing precomputation" : | |
140 | "without precomputation"); | |
0f113f3e MC |
141 | EC_POINT_free(P); |
142 | EC_POINT_free(Q); | |
50e34aab AP |
143 | EC_POINT_free(R); |
144 | EC_POINT_free(S); | |
0f113f3e MC |
145 | BN_free(n1); |
146 | BN_free(n2); | |
147 | BN_free(order); | |
148 | BN_CTX_free(ctx); | |
2db85ac9 | 149 | return r; |
0f113f3e | 150 | } |
04daec86 | 151 | |
2db85ac9 | 152 | static int prime_field_tests(void) |
0f113f3e MC |
153 | { |
154 | BN_CTX *ctx = NULL; | |
2db85ac9 P |
155 | BIGNUM *p = NULL, *a = NULL, *b = NULL, *scalar3 = NULL; |
156 | EC_GROUP *group = NULL, *tmp = NULL; | |
157 | EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, | |
158 | *P_256 = NULL, *P_384 = NULL, *P_521 = NULL; | |
159 | EC_POINT *P = NULL, *Q = NULL, *R = NULL; | |
160 | BIGNUM *x = NULL, *y = NULL, *z = NULL, *yplusone = NULL; | |
161 | const EC_POINT *points[4]; | |
162 | const BIGNUM *scalars[4]; | |
0f113f3e | 163 | unsigned char buf[100]; |
2db85ac9 | 164 | size_t i, len, r = 0; |
0f113f3e MC |
165 | int k; |
166 | ||
2db85ac9 P |
167 | if (!TEST_ptr(ctx = BN_CTX_new()) |
168 | || !TEST_ptr(p = BN_new()) | |
169 | || !TEST_ptr(a = BN_new()) | |
170 | || !TEST_ptr(b = BN_new()) | |
171 | || !TEST_true(BN_hex2bn(&p, "17")) | |
172 | || !TEST_true(BN_hex2bn(&a, "1")) | |
173 | || !TEST_true(BN_hex2bn(&b, "1")) | |
174 | /* | |
175 | * applications should use EC_GROUP_new_curve_GFp so | |
176 | * that the library gets to choose the EC_METHOD | |
177 | */ | |
178 | || !TEST_ptr(group = EC_GROUP_new(EC_GFp_mont_method())) | |
179 | || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) | |
180 | || !TEST_ptr(tmp = EC_GROUP_new(EC_GROUP_method_of(group))) | |
181 | || !TEST_true(EC_GROUP_copy(tmp, group))) | |
182 | goto err; | |
183 | EC_GROUP_free(group); | |
184 | group = tmp; | |
185 | tmp = NULL; | |
186 | ||
187 | if (!TEST_true(EC_GROUP_get_curve_GFp(group, p, a, b, ctx))) | |
188 | goto err; | |
189 | ||
190 | BIO_printf(bio_out, | |
191 | "Curve defined by Weierstrass equation\n" | |
192 | " y^2 = x^3 + a*x + b (mod 0x"); | |
193 | BN_print(bio_out, p); | |
194 | BIO_printf(bio_out, ")\n a = 0x"); | |
195 | BN_print(bio_out, a); | |
196 | BIO_printf(bio_out, "\n b = 0x"); | |
197 | BN_print(bio_out, b); | |
198 | BIO_printf(bio_out, "\n"); | |
0f113f3e MC |
199 | |
200 | buf[0] = 0; | |
2db85ac9 P |
201 | if (!TEST_ptr(P = EC_POINT_new(group)) |
202 | || !TEST_ptr(Q = EC_POINT_new(group)) | |
203 | || !TEST_ptr(R = EC_POINT_new(group)) | |
204 | || !TEST_true(EC_POINT_set_to_infinity(group, P)) | |
205 | || !TEST_true(EC_POINT_is_at_infinity(group, P)) | |
206 | || !TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx)) | |
207 | || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)) | |
208 | || !TEST_true(EC_POINT_is_at_infinity(group, P)) | |
209 | || !TEST_ptr(x = BN_new()) | |
210 | || !TEST_ptr(y = BN_new()) | |
211 | || !TEST_ptr(z = BN_new()) | |
212 | || !TEST_ptr(yplusone = BN_new()) | |
213 | || !TEST_true(BN_hex2bn(&x, "D")) | |
214 | || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, | |
215 | ctx))) | |
216 | goto err; | |
217 | ||
218 | if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) { | |
219 | if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, | |
220 | ctx))) | |
221 | goto err; | |
222 | BIO_printf(bio_err, "Point is not on curve: x = 0x"); | |
0f113f3e | 223 | BN_print_fp(stderr, x); |
2db85ac9 | 224 | BIO_printf(bio_err, ", y = 0x"); |
0f113f3e | 225 | BN_print_fp(stderr, y); |
2db85ac9 P |
226 | BIO_printf(bio_err, "\n"); |
227 | goto err; | |
0f113f3e MC |
228 | } |
229 | ||
2db85ac9 | 230 | BIO_printf(bio_out, "A cyclic subgroup:\n"); |
0f113f3e MC |
231 | k = 100; |
232 | do { | |
2db85ac9 P |
233 | if (!TEST_int_ne(k--, 0)) |
234 | goto err; | |
235 | ||
236 | if (EC_POINT_is_at_infinity(group, P)) { | |
237 | BIO_printf(bio_out, " point at infinity\n"); | |
238 | } else { | |
239 | if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, | |
240 | ctx))) | |
241 | goto err; | |
242 | ||
243 | BIO_printf(bio_out, " x = 0x"); | |
244 | BN_print(bio_out, x); | |
245 | BIO_printf(bio_out, ", y = 0x"); | |
246 | BN_print(bio_out, y); | |
247 | BIO_printf(bio_out, "\n"); | |
0f113f3e MC |
248 | } |
249 | ||
2db85ac9 P |
250 | if (!TEST_true(EC_POINT_copy(R, P)) |
251 | || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))) | |
252 | goto err; | |
0f113f3e | 253 | |
2db85ac9 | 254 | } while (!EC_POINT_is_at_infinity(group, P)); |
0f113f3e | 255 | |
2db85ac9 P |
256 | if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx)) |
257 | || !TEST_true(EC_POINT_is_at_infinity(group, P))) | |
258 | goto err; | |
0f113f3e MC |
259 | |
260 | len = | |
261 | EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, | |
262 | sizeof buf, ctx); | |
2db85ac9 P |
263 | if (!TEST_size_t_ne(len, 0) |
264 | || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) | |
265 | || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) | |
266 | goto err; | |
267 | BIO_printf(bio_out, "Generator as octet string, compressed form:\n "); | |
0f113f3e | 268 | for (i = 0; i < len; i++) |
2db85ac9 P |
269 | BIO_printf(bio_out, "%02X", buf[i]); |
270 | ||
271 | len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, | |
272 | buf, sizeof buf, ctx); | |
273 | if (!TEST_size_t_ne(len, 0) | |
274 | || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) | |
275 | || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) | |
276 | goto err; | |
277 | BIO_printf(bio_out, "\nGenerator as octet string, uncompressed form:\n" | |
278 | " "); | |
0f113f3e | 279 | for (i = 0; i < len; i++) |
2db85ac9 P |
280 | BIO_printf(bio_out, "%02X", buf[i]); |
281 | ||
282 | len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, | |
283 | buf, sizeof buf, ctx); | |
284 | if (!TEST_size_t_ne(len, 0) | |
285 | || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) | |
286 | || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) | |
287 | goto err; | |
288 | BIO_printf(bio_out, "\nGenerator as octet string, hybrid form:\n "); | |
0f113f3e | 289 | for (i = 0; i < len; i++) |
2db85ac9 P |
290 | BIO_printf(bio_out, "%02X", buf[i]); |
291 | ||
292 | if (!TEST_true(EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, | |
293 | ctx))) | |
294 | goto err; | |
295 | BIO_printf(bio_out, | |
296 | "\nA representation of the inverse of that generator in\n" | |
297 | "Jacobian projective coordinates:\n" | |
298 | " X = 0x"); | |
299 | BN_print(bio_out, x); | |
300 | BIO_printf(bio_out, ", Y = 0x"); | |
301 | BN_print(bio_out, y); | |
302 | BIO_printf(bio_out, ", Z = 0x"); | |
303 | BN_print(bio_out, z); | |
304 | BIO_printf(bio_out, "\n"); | |
305 | ||
306 | if (!TEST_true(EC_POINT_invert(group, P, ctx)) | |
307 | || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)) | |
0f113f3e MC |
308 | |
309 | /* | |
310 | * Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2, | |
311 | * 2000) -- not a NIST curve, but commonly used | |
312 | */ | |
313 | ||
2db85ac9 P |
314 | || !TEST_true(BN_hex2bn(&p, "FFFFFFFF" |
315 | "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF")) | |
316 | || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) | |
317 | || !TEST_true(BN_hex2bn(&a, "FFFFFFFF" | |
318 | "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC")) | |
319 | || !TEST_true(BN_hex2bn(&b, "1C97BEFC" | |
320 | "54BD7A8B65ACF89F81D4D4ADC565FA45")) | |
321 | || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) | |
322 | || !TEST_true(BN_hex2bn(&x, "4A96B568" | |
323 | "8EF573284664698968C38BB913CBFC82")) | |
324 | || !TEST_true(BN_hex2bn(&y, "23a62855" | |
325 | "3168947d59dcc912042351377ac5fb32")) | |
326 | || !TEST_true(BN_add(yplusone, y, BN_value_one())) | |
1e2012b7 EK |
327 | /* |
328 | * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, | |
329 | * and therefore setting the coordinates should fail. | |
330 | */ | |
2db85ac9 P |
331 | || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x, |
332 | yplusone, ctx)) | |
333 | || !TEST_true(EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx)) | |
334 | || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) | |
335 | || !TEST_true(BN_hex2bn(&z, "0100000000" | |
336 | "000000000001F4C8F927AED3CA752257")) | |
337 | || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) | |
338 | || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))) | |
339 | goto err; | |
340 | BIO_printf(bio_out, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x"); | |
341 | BN_print(bio_out, x); | |
342 | BIO_printf(bio_out, "\n y = 0x"); | |
343 | BN_print(bio_out, y); | |
344 | BIO_printf(bio_out, "\n"); | |
0f113f3e | 345 | /* G_y value taken from the standard: */ |
2db85ac9 P |
346 | if (!TEST_true(BN_hex2bn(&z, "23a62855" |
347 | "3168947d59dcc912042351377ac5fb32")) | |
dc352c19 | 348 | || !TEST_BN_eq(y, z) |
2db85ac9 P |
349 | || !TEST_int_eq(EC_GROUP_get_degree(group), 160) |
350 | || !group_order_tests(group) | |
351 | || !TEST_ptr(P_160 = EC_GROUP_new(EC_GROUP_method_of(group))) | |
352 | || !TEST_true(EC_GROUP_copy(P_160, group)) | |
0f113f3e MC |
353 | |
354 | /* Curve P-192 (FIPS PUB 186-2, App. 6) */ | |
355 | ||
2db85ac9 P |
356 | || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFF" |
357 | "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) | |
358 | || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) | |
359 | || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFF" | |
360 | "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) | |
361 | || !TEST_true(BN_hex2bn(&b, "64210519E59C80E7" | |
362 | "0FA7E9AB72243049FEB8DEECC146B9B1")) | |
363 | || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) | |
364 | || !TEST_true(BN_hex2bn(&x, "188DA80EB03090F6" | |
365 | "7CBF20EB43A18800F4FF0AFD82FF1012")) | |
366 | || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, | |
367 | ctx)) | |
368 | || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) | |
369 | || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFF" | |
370 | "FFFFFFFF99DEF836146BC9B1B4D22831")) | |
371 | || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) | |
372 | || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))) | |
373 | goto err; | |
374 | ||
375 | BIO_printf(bio_out, "\nNIST curve P-192 -- Generator:\n x = 0x"); | |
376 | BN_print(bio_out, x); | |
377 | BIO_printf(bio_out, "\n y = 0x"); | |
378 | BN_print(bio_out, y); | |
379 | BIO_printf(bio_out, "\n"); | |
0f113f3e | 380 | /* G_y value taken from the standard: */ |
2db85ac9 P |
381 | if (!TEST_true(BN_hex2bn(&z, "07192B95FFC8DA78" |
382 | "631011ED6B24CDD573F977A11E794811")) | |
dc352c19 | 383 | || !TEST_BN_eq(y, z) |
2db85ac9 | 384 | || !TEST_true(BN_add(yplusone, y, BN_value_one())) |
1e2012b7 EK |
385 | /* |
386 | * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, | |
387 | * and therefore setting the coordinates should fail. | |
388 | */ | |
2db85ac9 P |
389 | || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x, |
390 | yplusone, ctx)) | |
391 | || !TEST_int_eq(EC_GROUP_get_degree(group), 192) | |
392 | || !group_order_tests(group) | |
393 | || !TEST_ptr(P_192 = EC_GROUP_new(EC_GROUP_method_of(group))) | |
394 | || !TEST_true(EC_GROUP_copy(P_192, group)) | |
0f113f3e MC |
395 | |
396 | /* Curve P-224 (FIPS PUB 186-2, App. 6) */ | |
397 | ||
2db85ac9 P |
398 | || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFF" |
399 | "FFFFFFFF000000000000000000000001")) | |
400 | || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) | |
401 | || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFF" | |
402 | "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) | |
403 | || !TEST_true(BN_hex2bn(&b, "B4050A850C04B3ABF5413256" | |
404 | "5044B0B7D7BFD8BA270B39432355FFB4")) | |
405 | || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) | |
406 | || !TEST_true(BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B9" | |
407 | "4A03C1D356C21122343280D6115C1D21")) | |
408 | || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, | |
409 | ctx)) | |
410 | || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) | |
411 | || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF" | |
412 | "FFFF16A2E0B8F03E13DD29455C5C2A3D")) | |
413 | || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) | |
414 | || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))) | |
415 | goto err; | |
416 | ||
417 | BIO_printf(bio_out, "\nNIST curve P-224 -- Generator:\n x = 0x"); | |
418 | BN_print(bio_out, x); | |
419 | BIO_printf(bio_out, "\n y = 0x"); | |
420 | BN_print(bio_out, y); | |
421 | BIO_printf(bio_out, "\n"); | |
0f113f3e | 422 | /* G_y value taken from the standard: */ |
2db85ac9 P |
423 | if (!TEST_true(BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6" |
424 | "CD4375A05A07476444D5819985007E34")) | |
dc352c19 | 425 | || !TEST_BN_eq(y, z) |
2db85ac9 | 426 | || !TEST_true(BN_add(yplusone, y, BN_value_one())) |
1e2012b7 EK |
427 | /* |
428 | * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, | |
429 | * and therefore setting the coordinates should fail. | |
430 | */ | |
2db85ac9 P |
431 | || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x, |
432 | yplusone, ctx)) | |
433 | || !TEST_int_eq(EC_GROUP_get_degree(group), 224) | |
434 | || !group_order_tests(group) | |
435 | || !TEST_ptr(P_224 = EC_GROUP_new(EC_GROUP_method_of(group))) | |
436 | || !TEST_true(EC_GROUP_copy(P_224, group)) | |
0f113f3e MC |
437 | |
438 | /* Curve P-256 (FIPS PUB 186-2, App. 6) */ | |
439 | ||
2db85ac9 P |
440 | || !TEST_true(BN_hex2bn(&p, "FFFFFFFF000000010000000000000000" |
441 | "00000000FFFFFFFFFFFFFFFFFFFFFFFF")) | |
442 | || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) | |
443 | || !TEST_true(BN_hex2bn(&a, "FFFFFFFF000000010000000000000000" | |
444 | "00000000FFFFFFFFFFFFFFFFFFFFFFFC")) | |
445 | || !TEST_true(BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC" | |
446 | "651D06B0CC53B0F63BCE3C3E27D2604B")) | |
447 | || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) | |
448 | ||
449 | || !TEST_true(BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F2" | |
450 | "77037D812DEB33A0F4A13945D898C296")) | |
451 | || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, | |
452 | ctx)) | |
453 | || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) | |
454 | || !TEST_true(BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFF" | |
455 | "BCE6FAADA7179E84F3B9CAC2FC632551")) | |
456 | || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) | |
457 | || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))) | |
458 | goto err; | |
459 | ||
460 | BIO_printf(bio_out, "\nNIST curve P-256 -- Generator:\n x = 0x"); | |
461 | BN_print(bio_out, x); | |
462 | BIO_printf(bio_out, "\n y = 0x"); | |
463 | BN_print(bio_out, y); | |
464 | BIO_printf(bio_out, "\n"); | |
0f113f3e | 465 | /* G_y value taken from the standard: */ |
2db85ac9 P |
466 | if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16" |
467 | "2BCE33576B315ECECBB6406837BF51F5")) | |
dc352c19 | 468 | || !TEST_BN_eq(y, z) |
2db85ac9 | 469 | || !TEST_true(BN_add(yplusone, y, BN_value_one())) |
1e2012b7 EK |
470 | /* |
471 | * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, | |
472 | * and therefore setting the coordinates should fail. | |
473 | */ | |
2db85ac9 P |
474 | || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x, |
475 | yplusone, ctx)) | |
476 | || !TEST_int_eq(EC_GROUP_get_degree(group), 256) | |
477 | || !group_order_tests(group) | |
478 | || !TEST_ptr(P_256 = EC_GROUP_new(EC_GROUP_method_of(group))) | |
479 | || !TEST_true(EC_GROUP_copy(P_256, group)) | |
0f113f3e MC |
480 | |
481 | /* Curve P-384 (FIPS PUB 186-2, App. 6) */ | |
482 | ||
2db85ac9 P |
483 | || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" |
484 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" | |
485 | "FFFFFFFF0000000000000000FFFFFFFF")) | |
486 | || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) | |
487 | || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | |
488 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" | |
489 | "FFFFFFFF0000000000000000FFFFFFFC")) | |
490 | || !TEST_true(BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19" | |
491 | "181D9C6EFE8141120314088F5013875A" | |
492 | "C656398D8A2ED19D2A85C8EDD3EC2AEF")) | |
493 | || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) | |
494 | ||
495 | || !TEST_true(BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD74" | |
496 | "6E1D3B628BA79B9859F741E082542A38" | |
497 | "5502F25DBF55296C3A545E3872760AB7")) | |
498 | || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, | |
499 | ctx)) | |
500 | || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) | |
501 | || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | |
502 | "FFFFFFFFFFFFFFFFC7634D81F4372DDF" | |
503 | "581A0DB248B0A77AECEC196ACCC52973")) | |
504 | || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) | |
505 | || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))) | |
506 | goto err; | |
507 | ||
508 | BIO_printf(bio_out, "\nNIST curve P-384 -- Generator:\n x = 0x"); | |
509 | BN_print(bio_out, x); | |
510 | BIO_printf(bio_out, "\n y = 0x"); | |
511 | BN_print(bio_out, y); | |
512 | BIO_printf(bio_out, "\n"); | |
0f113f3e | 513 | /* G_y value taken from the standard: */ |
2db85ac9 P |
514 | if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29" |
515 | "F8F41DBD289A147CE9DA3113B5F0B8C0" | |
516 | "0A60B1CE1D7E819D7A431D7C90EA0E5F")) | |
dc352c19 | 517 | || !TEST_BN_eq(y, z) |
2db85ac9 | 518 | || !TEST_true(BN_add(yplusone, y, BN_value_one())) |
1e2012b7 EK |
519 | /* |
520 | * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, | |
521 | * and therefore setting the coordinates should fail. | |
522 | */ | |
2db85ac9 P |
523 | || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x, |
524 | yplusone, ctx)) | |
525 | || !TEST_int_eq(EC_GROUP_get_degree(group), 384) | |
526 | || !group_order_tests(group) | |
527 | || !TEST_ptr(P_384 = EC_GROUP_new(EC_GROUP_method_of(group))) | |
528 | || !TEST_true(EC_GROUP_copy(P_384, group)) | |
0f113f3e MC |
529 | |
530 | /* Curve P-521 (FIPS PUB 186-2, App. 6) */ | |
2db85ac9 P |
531 | || !TEST_true(BN_hex2bn(&p, "1FF" |
532 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | |
533 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | |
534 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | |
535 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")) | |
536 | || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) | |
537 | || !TEST_true(BN_hex2bn(&a, "1FF" | |
538 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | |
539 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | |
540 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | |
541 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC")) | |
542 | || !TEST_true(BN_hex2bn(&b, "051" | |
543 | "953EB9618E1C9A1F929A21A0B68540EE" | |
544 | "A2DA725B99B315F3B8B489918EF109E1" | |
545 | "56193951EC7E937B1652C0BD3BB1BF07" | |
546 | "3573DF883D2C34F1EF451FD46B503F00")) | |
547 | || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) | |
548 | || !TEST_true(BN_hex2bn(&x, "C6" | |
549 | "858E06B70404E9CD9E3ECB662395B442" | |
550 | "9C648139053FB521F828AF606B4D3DBA" | |
551 | "A14B5E77EFE75928FE1DC127A2FFA8DE" | |
552 | "3348B3C1856A429BF97E7E31C2E5BD66")) | |
553 | || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, | |
554 | ctx)) | |
555 | || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) | |
556 | || !TEST_true(BN_hex2bn(&z, "1FF" | |
557 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | |
558 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA" | |
559 | "51868783BF2F966B7FCC0148F709A5D0" | |
560 | "3BB5C9B8899C47AEBB6FB71E91386409")) | |
561 | || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one())) | |
562 | || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))) | |
563 | goto err; | |
564 | ||
565 | BIO_printf(bio_out, "\nNIST curve P-521 -- Generator:\n x = 0x"); | |
566 | BN_print(bio_out, x); | |
567 | BIO_printf(bio_out, "\n y = 0x"); | |
568 | BN_print(bio_out, y); | |
569 | BIO_printf(bio_out, "\n"); | |
0f113f3e | 570 | /* G_y value taken from the standard: */ |
2db85ac9 P |
571 | if (!TEST_true(BN_hex2bn(&z, "118" |
572 | "39296A789A3BC0045C8A5FB42C7D1BD9" | |
573 | "98F54449579B446817AFBD17273E662C" | |
574 | "97EE72995EF42640C550B9013FAD0761" | |
575 | "353C7086A272C24088BE94769FD16650")) | |
dc352c19 | 576 | || !TEST_BN_eq(y, z) |
2db85ac9 | 577 | || !TEST_true(BN_add(yplusone, y, BN_value_one())) |
1e2012b7 EK |
578 | /* |
579 | * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, | |
580 | * and therefore setting the coordinates should fail. | |
581 | */ | |
2db85ac9 P |
582 | || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x, |
583 | yplusone, ctx)) | |
584 | || !TEST_int_eq(EC_GROUP_get_degree(group), 521) | |
585 | || !group_order_tests(group) | |
586 | || !TEST_ptr(P_521 = EC_GROUP_new(EC_GROUP_method_of(group))) | |
587 | || !TEST_true(EC_GROUP_copy(P_521, group)) | |
0f113f3e MC |
588 | |
589 | /* more tests using the last curve */ | |
590 | ||
1e2012b7 | 591 | /* Restore the point that got mangled in the (x, y + 1) test. */ |
2db85ac9 P |
592 | || !TEST_true(EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx)) |
593 | || !TEST_true(EC_POINT_copy(Q, P)) | |
594 | || !TEST_false(EC_POINT_is_at_infinity(group, Q)) | |
595 | || !TEST_true(EC_POINT_dbl(group, P, P, ctx)) | |
596 | || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) | |
597 | || !TEST_true(EC_POINT_invert(group, Q, ctx)) /* P = -2Q */ | |
598 | || !TEST_true(EC_POINT_add(group, R, P, Q, ctx)) | |
599 | || !TEST_true(EC_POINT_add(group, R, R, Q, ctx)) | |
600 | || !TEST_true(EC_POINT_is_at_infinity(group, R)) /* R = P + 2Q */ | |
601 | || !TEST_false(EC_POINT_is_at_infinity(group, Q))) | |
602 | goto err; | |
603 | points[0] = Q; | |
604 | points[1] = Q; | |
605 | points[2] = Q; | |
606 | points[3] = Q; | |
607 | ||
608 | if (!TEST_true(EC_GROUP_get_order(group, z, ctx)) | |
609 | || !TEST_true(BN_add(y, z, BN_value_one())) | |
dc352c19 | 610 | || !TEST_BN_even(y) |
2db85ac9 P |
611 | || !TEST_true(BN_rshift1(y, y))) |
612 | goto err; | |
613 | scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */ | |
614 | scalars[1] = y; | |
615 | ||
616 | BIO_printf(bio_out, "combined multiplication ..."); | |
617 | ||
618 | /* z is still the group order */ | |
619 | if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) | |
620 | || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) | |
621 | || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)) | |
622 | || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx)) | |
623 | || !TEST_true(BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) | |
624 | || !TEST_true(BN_add(z, z, y))) | |
625 | goto err; | |
626 | BN_set_negative(z, 1); | |
627 | scalars[0] = y; | |
628 | scalars[1] = z; /* z = -(order + y) */ | |
629 | ||
630 | if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) | |
631 | || !TEST_true(EC_POINT_is_at_infinity(group, P)) | |
632 | || !TEST_true(BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) | |
633 | || !TEST_true(BN_add(z, x, y))) | |
634 | goto err; | |
635 | BN_set_negative(z, 1); | |
636 | scalars[0] = x; | |
637 | scalars[1] = y; | |
638 | scalars[2] = z; /* z = -(x+y) */ | |
639 | ||
640 | if (!TEST_ptr(scalar3 = BN_new())) | |
641 | goto err; | |
642 | BN_zero(scalar3); | |
643 | scalars[3] = scalar3; | |
644 | ||
645 | if (!TEST_true(EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx)) | |
646 | || !TEST_true(EC_POINT_is_at_infinity(group, P))) | |
647 | goto err; | |
648 | ||
649 | BIO_printf(bio_out, " ok\n\n"); | |
650 | ||
651 | ||
652 | r = 1; | |
653 | err: | |
654 | BN_CTX_free(ctx); | |
655 | BN_free(p); | |
656 | BN_free(a); | |
657 | BN_free(b); | |
658 | EC_GROUP_free(group); | |
659 | EC_GROUP_free(tmp); | |
660 | EC_POINT_free(P); | |
661 | EC_POINT_free(Q); | |
662 | EC_POINT_free(R); | |
663 | BN_free(x); | |
664 | BN_free(y); | |
665 | BN_free(z); | |
666 | BN_free(yplusone); | |
667 | BN_free(scalar3); | |
668 | ||
669 | EC_GROUP_free(P_160); | |
670 | EC_GROUP_free(P_192); | |
671 | EC_GROUP_free(P_224); | |
672 | EC_GROUP_free(P_256); | |
673 | EC_GROUP_free(P_384); | |
674 | EC_GROUP_free(P_521); | |
675 | return r; | |
676 | } | |
677 | ||
678 | # ifndef OPENSSL_NO_EC2M | |
0f113f3e | 679 | |
2db85ac9 P |
680 | static struct c2_curve_test { |
681 | const char *name; | |
682 | const char *p; | |
683 | const char *a; | |
684 | const char *b; | |
685 | const char *x; | |
686 | const char *y; | |
687 | int ybit; | |
688 | const char *order; | |
689 | const char *cof; | |
690 | int degree; | |
691 | } char2_curve_tests[] = { | |
692 | /* Curve K-163 (FIPS PUB 186-2, App. 6) */ | |
693 | { | |
694 | "NIST curve K-163", | |
695 | "0800000000000000000000000000000000000000C9", | |
696 | "1", | |
697 | "1", | |
698 | "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8", | |
699 | "0289070FB05D38FF58321F2E800536D538CCDAA3D9", | |
700 | 1, "04000000000000000000020108A2E0CC0D99F8A5EF", "2", 163 | |
701 | }, | |
702 | /* Curve B-163 (FIPS PUB 186-2, App. 6) */ | |
703 | { | |
704 | "NIST curve B-163", | |
705 | "0800000000000000000000000000000000000000C9", | |
706 | "1", | |
707 | "020A601907B8C953CA1481EB10512F78744A3205FD", | |
708 | "03F0EBA16286A2D57EA0991168D4994637E8343E36", | |
709 | "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1", | |
710 | 1, "040000000000000000000292FE77E70C12A4234C33", "2", 163 | |
711 | }, | |
712 | /* Curve K-233 (FIPS PUB 186-2, App. 6) */ | |
713 | { | |
714 | "NIST curve K-233", | |
715 | "020000000000000000000000000000000000000004000000000000000001", | |
716 | "0", | |
717 | "1", | |
718 | "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126", | |
719 | "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3", | |
720 | 0, | |
721 | "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF", | |
722 | "4", 233 | |
723 | }, | |
724 | /* Curve B-233 (FIPS PUB 186-2, App. 6) */ | |
725 | { | |
726 | "NIST curve B-233", | |
727 | "020000000000000000000000000000000000000004000000000000000001", | |
728 | "000000000000000000000000000000000000000000000000000000000001", | |
729 | "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD", | |
730 | "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B", | |
731 | "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052", | |
732 | 1, | |
733 | "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7", | |
734 | "2", 233 | |
735 | }, | |
736 | /* Curve K-283 (FIPS PUB 186-2, App. 6) */ | |
737 | { | |
738 | "NIST curve K-283", | |
739 | "08000000" | |
740 | "00000000000000000000000000000000000000000000000000000000000010A1", | |
741 | "0", | |
742 | "1", | |
743 | "0503213F" | |
744 | "78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836", | |
745 | "01CCDA38" | |
746 | "0F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259", | |
747 | 0, | |
748 | "01FFFFFF" | |
749 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61", | |
750 | "4", 283 | |
751 | }, | |
752 | /* Curve B-283 (FIPS PUB 186-2, App. 6) */ | |
753 | { | |
754 | "NIST curve B-283", | |
755 | "08000000" | |
756 | "00000000000000000000000000000000000000000000000000000000000010A1", | |
757 | "00000000" | |
758 | "0000000000000000000000000000000000000000000000000000000000000001", | |
759 | "027B680A" | |
760 | "C8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5", | |
761 | "05F93925" | |
762 | "8DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053", | |
763 | "03676854" | |
764 | "FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4", | |
765 | 1, | |
766 | "03FFFFFF" | |
767 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307", | |
768 | "2", 283 | |
769 | }, | |
770 | /* Curve K-409 (FIPS PUB 186-2, App. 6) */ | |
771 | { | |
772 | "NIST curve K-409", | |
773 | "0200000000000000000000000000000000000000" | |
774 | "0000000000000000000000000000000000000000008000000000000000000001", | |
775 | "0", | |
776 | "1", | |
777 | "0060F05F658F49C1AD3AB1890F7184210EFD0987" | |
778 | "E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746", | |
779 | "01E369050B7C4E42ACBA1DACBF04299C3460782F" | |
780 | "918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B", | |
781 | 1, | |
782 | "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | |
783 | "FFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF", | |
784 | "4", 409 | |
785 | }, | |
786 | /* Curve B-409 (FIPS PUB 186-2, App. 6) */ | |
787 | { | |
788 | "NIST curve B-409", | |
789 | "0200000000000000000000000000000000000000" | |
790 | "0000000000000000000000000000000000000000008000000000000000000001", | |
791 | "0000000000000000000000000000000000000000" | |
792 | "0000000000000000000000000000000000000000000000000000000000000001", | |
793 | "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422E" | |
794 | "F1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F", | |
795 | "015D4860D088DDB3496B0C6064756260441CDE4A" | |
796 | "F1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7", | |
797 | "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5" | |
798 | "A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706", | |
799 | 1, | |
800 | "0100000000000000000000000000000000000000" | |
801 | "00000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173", | |
802 | "2", 409 | |
803 | }, | |
804 | /* Curve K-571 (FIPS PUB 186-2, App. 6) */ | |
805 | { | |
806 | "NIST curve K-571", | |
807 | "800000000000000" | |
808 | "0000000000000000000000000000000000000000000000000000000000000000" | |
809 | "0000000000000000000000000000000000000000000000000000000000000425", | |
810 | "0", | |
811 | "1", | |
812 | "026EB7A859923FBC" | |
813 | "82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E6" | |
814 | "47DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972", | |
815 | "0349DC807F4FBF37" | |
816 | "4F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA7" | |
817 | "4FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3", | |
818 | 0, | |
819 | "0200000000000000" | |
820 | "00000000000000000000000000000000000000000000000000000000131850E1" | |
821 | "F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001", | |
822 | "4", 571 | |
823 | }, | |
824 | /* Curve B-571 (FIPS PUB 186-2, App. 6) */ | |
0f113f3e | 825 | { |
2db85ac9 P |
826 | "NIST curve B-571", |
827 | "800000000000000" | |
828 | "0000000000000000000000000000000000000000000000000000000000000000" | |
829 | "0000000000000000000000000000000000000000000000000000000000000425", | |
830 | "0000000000000000" | |
831 | "0000000000000000000000000000000000000000000000000000000000000000" | |
832 | "0000000000000000000000000000000000000000000000000000000000000001", | |
833 | "02F40E7E2221F295" | |
834 | "DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA5933" | |
835 | "2BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A", | |
836 | "0303001D34B85629" | |
837 | "6C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293" | |
838 | "CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19", | |
839 | "037BF27342DA639B" | |
840 | "6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A57" | |
841 | "6291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B", | |
842 | 1, | |
843 | "03FFFFFFFFFFFFFF" | |
844 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18" | |
845 | "FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47", | |
846 | "2", 571 | |
847 | } | |
848 | }; | |
849 | ||
850 | static int char2_curve_test(int n) | |
851 | { | |
852 | int r = 0; | |
853 | BN_CTX *ctx = NULL; | |
854 | BIGNUM *p = NULL, *a = NULL, *b = NULL; | |
855 | BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL; | |
856 | EC_GROUP *group = NULL, *variable = NULL; | |
857 | EC_POINT *P = NULL, *Q = NULL, *R = NULL; | |
858 | const EC_POINT *points[3]; | |
859 | const BIGNUM *scalars[3]; | |
860 | struct c2_curve_test *const test = char2_curve_tests + n; | |
861 | ||
862 | if (!TEST_ptr(ctx = BN_CTX_new()) | |
863 | || !TEST_ptr(p = BN_new()) | |
864 | || !TEST_ptr(a = BN_new()) | |
865 | || !TEST_ptr(b = BN_new()) | |
866 | || !TEST_ptr(x = BN_new()) | |
867 | || !TEST_ptr(y = BN_new()) | |
868 | || !TEST_ptr(z = BN_new()) | |
869 | || !TEST_ptr(yplusone = BN_new()) | |
870 | || !TEST_true(BN_hex2bn(&p, test->p)) | |
871 | || !TEST_true(BN_hex2bn(&a, test->a)) | |
872 | || !TEST_true(BN_hex2bn(&b, test->b)) | |
873 | || !TEST_true(group = EC_GROUP_new(EC_GF2m_simple_method())) | |
874 | || !TEST_true(EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) | |
875 | || !TEST_ptr(P = EC_POINT_new(group)) | |
876 | || !TEST_ptr(Q = EC_POINT_new(group)) | |
877 | || !TEST_ptr(R = EC_POINT_new(group)) | |
878 | || !TEST_true(BN_hex2bn(&x, test->x)) | |
879 | || !TEST_true(BN_hex2bn(&y, test->y)) | |
880 | || !TEST_true(BN_add(yplusone, y, BN_value_one()))) | |
881 | goto err; | |
882 | ||
883 | /* Change test based on whether binary point compression is enabled or not. */ | |
884 | # ifdef OPENSSL_EC_BIN_PT_COMP | |
885 | /* | |
886 | * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, | |
887 | * and therefore setting the coordinates should fail. | |
888 | */ | |
889 | if (!TEST_false(EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone, | |
890 | ctx)) | |
891 | || !TEST_true(EC_POINT_set_compressed_coordinates_GF2m(group, P, x, | |
892 | test->y_bit, | |
893 | ctx)) | |
894 | || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) | |
895 | || !TEST_true(BN_hex2bn(&z, test->order)) | |
896 | || !TEST_true(BN_hex2bn(&cof, test->cof)) | |
897 | || !TEST_true(EC_GROUP_set_generator(group, P, z, cof)) | |
898 | || !TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, | |
899 | ctx))) | |
900 | goto err; | |
901 | BIO_printf(bio_out, "\n%s -- Generator:\n x = 0x", test->name); | |
902 | BN_print(bio_out, x); | |
903 | BIO_printf(bio_out, "\n y = 0x"); | |
904 | BN_print(bio_out, y); | |
905 | BIO_printf(bio_out, "\n"); | |
906 | /* G_y value taken from the standard: */ | |
907 | if (!TEST_true(BN_hex2bn(&z, test->y)) | |
dc352c19 | 908 | || !TEST_BN_eq(y, z)) |
2db85ac9 P |
909 | goto err; |
910 | # else | |
911 | /* | |
912 | * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, | |
913 | * and therefore setting the coordinates should fail. | |
914 | */ | |
915 | if (!TEST_false(EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone, | |
916 | ctx)) | |
917 | || !TEST_true(EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) | |
918 | || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) | |
919 | || !TEST_true(BN_hex2bn(&z, test->order)) | |
920 | || !TEST_true(BN_hex2bn(&cof, test->cof)) | |
921 | || !TEST_true(EC_GROUP_set_generator(group, P, z, cof))) | |
922 | goto err; | |
923 | BIO_printf(bio_out, "\n%s -- Generator:\n x = 0x", test->name); \ | |
924 | BN_print(bio_out, x); \ | |
925 | BIO_printf(bio_out, "\n y = 0x"); \ | |
926 | BN_print(bio_out, y); \ | |
927 | BIO_printf(bio_out, "\n"); | |
928 | # endif | |
929 | ||
930 | if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree) | |
931 | || !group_order_tests(group) | |
932 | || !TEST_ptr(variable = EC_GROUP_new(EC_GROUP_method_of(group))) | |
933 | || !TEST_true(EC_GROUP_copy(variable, group))) | |
934 | goto err; | |
935 | ||
936 | /* more tests using the last curve */ | |
937 | if (n == OSSL_NELEM(char2_curve_tests) - 1) { | |
938 | if (!TEST_true(EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, | |
939 | ctx)) | |
940 | || !TEST_true(EC_POINT_copy(Q, P)) | |
941 | || !TEST_false(EC_POINT_is_at_infinity(group, Q)) | |
942 | || !TEST_true(EC_POINT_dbl(group, P, P, ctx)) | |
943 | || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0) | |
944 | || !TEST_true(EC_POINT_invert(group, Q, ctx)) /* P = -2Q */ | |
945 | || !TEST_true(EC_POINT_add(group, R, P, Q, ctx)) | |
946 | || !TEST_true(EC_POINT_add(group, R, R, Q, ctx)) | |
947 | || !TEST_true(EC_POINT_is_at_infinity(group, R)) /* R = P + 2Q */ | |
948 | || !TEST_false(EC_POINT_is_at_infinity(group, Q))) | |
949 | goto err; | |
0f113f3e | 950 | |
0f113f3e MC |
951 | points[0] = Q; |
952 | points[1] = Q; | |
953 | points[2] = Q; | |
2db85ac9 P |
954 | |
955 | if (!TEST_true(BN_add(y, z, BN_value_one())) | |
dc352c19 | 956 | || !TEST_BN_even(y) |
2db85ac9 P |
957 | || !TEST_true(BN_rshift1(y, y))) |
958 | goto err; | |
0f113f3e MC |
959 | scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */ |
960 | scalars[1] = y; | |
961 | ||
2db85ac9 | 962 | BIO_printf(bio_out, "combined multiplication ..."); |
0f113f3e MC |
963 | |
964 | /* z is still the group order */ | |
2db85ac9 P |
965 | if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) |
966 | || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) | |
967 | || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)) | |
968 | || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx))) | |
969 | goto err; | |
970 | ||
971 | if (!TEST_true(BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) | |
972 | || !TEST_true(BN_add(z, z, y))) | |
973 | goto err; | |
0f113f3e MC |
974 | BN_set_negative(z, 1); |
975 | scalars[0] = y; | |
976 | scalars[1] = z; /* z = -(order + y) */ | |
977 | ||
2db85ac9 P |
978 | if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) |
979 | || !TEST_true(EC_POINT_is_at_infinity(group, P))) | |
980 | goto err; | |
0f113f3e | 981 | |
2db85ac9 P |
982 | if (!TEST_true(BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) |
983 | || !TEST_true(BN_add(z, x, y))) | |
984 | goto err; | |
0f113f3e MC |
985 | BN_set_negative(z, 1); |
986 | scalars[0] = x; | |
987 | scalars[1] = y; | |
988 | scalars[2] = z; /* z = -(x+y) */ | |
989 | ||
2db85ac9 P |
990 | if (!TEST_true(EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) |
991 | || !TEST_true(EC_POINT_is_at_infinity(group, P))) | |
992 | goto err;; | |
0f113f3e MC |
993 | } |
994 | ||
2db85ac9 P |
995 | r = 1; |
996 | err: | |
23a1d5e9 | 997 | BN_CTX_free(ctx); |
0f113f3e MC |
998 | BN_free(p); |
999 | BN_free(a); | |
1000 | BN_free(b); | |
0f113f3e MC |
1001 | BN_free(x); |
1002 | BN_free(y); | |
1003 | BN_free(z); | |
1e2012b7 | 1004 | BN_free(yplusone); |
2db85ac9 P |
1005 | BN_free(cof); |
1006 | EC_POINT_free(P); | |
1007 | EC_POINT_free(Q); | |
1008 | EC_POINT_free(R); | |
1009 | EC_GROUP_free(group); | |
1010 | EC_GROUP_free(variable); | |
1011 | return r; | |
0f113f3e | 1012 | } |
7793f30e | 1013 | |
2db85ac9 | 1014 | static int char2_field_tests(void) |
0f113f3e MC |
1015 | { |
1016 | BN_CTX *ctx = NULL; | |
2db85ac9 P |
1017 | BIGNUM *p = NULL, *a = NULL, *b = NULL; |
1018 | EC_GROUP *group = NULL, *tmp = NULL; | |
1019 | EC_POINT *P = NULL, *Q = NULL, *R = NULL; | |
1020 | BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL; | |
0f113f3e MC |
1021 | unsigned char buf[100]; |
1022 | size_t i, len; | |
2db85ac9 | 1023 | int k, r = 0; |
0f113f3e | 1024 | |
2db85ac9 P |
1025 | if (!TEST_ptr(ctx = BN_CTX_new()) |
1026 | || !TEST_ptr(p = BN_new()) | |
1027 | || !TEST_ptr(a = BN_new()) | |
1028 | || !TEST_ptr(b = BN_new()) | |
1029 | || !TEST_true(BN_hex2bn(&p, "13")) | |
1030 | || !TEST_true(BN_hex2bn(&a, "3")) | |
1031 | || !TEST_true(BN_hex2bn(&b, "1"))) | |
1032 | goto err; | |
0f113f3e MC |
1033 | |
1034 | group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use | |
1035 | * EC_GROUP_new_curve_GF2m | |
1036 | * so that the library gets | |
1037 | * to choose the EC_METHOD */ | |
2db85ac9 P |
1038 | if (!TEST_ptr(group) |
1039 | || !TEST_true(EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) | |
1040 | || !TEST_ptr(tmp = EC_GROUP_new(EC_GROUP_method_of(group))) | |
1041 | || !TEST_true(EC_GROUP_copy(tmp, group))) | |
1042 | goto err; | |
1043 | EC_GROUP_free(group); | |
1044 | group = tmp; | |
1045 | tmp = NULL; | |
1046 | ||
1047 | if (!TEST_true(EC_GROUP_get_curve_GF2m(group, p, a, b, ctx))) | |
1048 | goto err; | |
1049 | ||
1050 | BIO_printf(bio_out, | |
1051 | "Curve defined by Weierstrass equation\n" | |
1052 | " y^2 + x*y = x^3 + a*x^2 + b (mod 0x"); | |
1053 | BN_print(bio_out, p); | |
1054 | BIO_printf(bio_out, ")\n a = 0x"); | |
1055 | BN_print(bio_out, a); | |
1056 | BIO_printf(bio_out, "\n b = 0x"); | |
1057 | BN_print(bio_out, b); | |
1058 | BIO_printf(bio_out, "\n(0x... means binary polynomial)\n"); | |
1059 | ||
1060 | if (!TEST_ptr(P = EC_POINT_new(group)) | |
1061 | || !TEST_ptr(Q = EC_POINT_new(group)) | |
1062 | || !TEST_ptr(R = EC_POINT_new(group)) | |
1063 | || !TEST_true(EC_POINT_set_to_infinity(group, P)) | |
1064 | || !TEST_true(EC_POINT_is_at_infinity(group, P))) | |
1065 | goto err; | |
0f113f3e MC |
1066 | |
1067 | buf[0] = 0; | |
2db85ac9 P |
1068 | if (!TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx)) |
1069 | || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)) | |
1070 | || !TEST_true(EC_POINT_is_at_infinity(group, P)) | |
1071 | || !TEST_ptr(x = BN_new()) | |
1072 | || !TEST_ptr(y = BN_new()) | |
1073 | || !TEST_ptr(z = BN_new()) | |
1074 | || !TEST_ptr(cof = BN_new()) | |
1075 | || !TEST_ptr(yplusone = BN_new()) | |
1076 | || !TEST_true(BN_hex2bn(&x, "6")) | |
7793f30e | 1077 | /* Change test based on whether binary point compression is enabled or not. */ |
0f113f3e | 1078 | # ifdef OPENSSL_EC_BIN_PT_COMP |
2db85ac9 P |
1079 | || !TEST_true(EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, |
1080 | ctx)) | |
0f113f3e | 1081 | # else |
2db85ac9 P |
1082 | || !TEST_true(BN_hex2bn(&y, "8")) |
1083 | || !TEST_true(EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx)) | |
0f113f3e | 1084 | # endif |
2db85ac9 P |
1085 | ) |
1086 | goto err; | |
1087 | if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) { | |
7793f30e | 1088 | /* Change test based on whether binary point compression is enabled or not. */ |
0f113f3e | 1089 | # ifdef OPENSSL_EC_BIN_PT_COMP |
2db85ac9 P |
1090 | if (!TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, |
1091 | ctx))) | |
1092 | goto err; | |
0f113f3e | 1093 | # endif |
2db85ac9 | 1094 | BIO_printf(bio_err, "Point is not on curve: x = 0x"); |
0f113f3e | 1095 | BN_print_fp(stderr, x); |
2db85ac9 | 1096 | BIO_printf(bio_err, ", y = 0x"); |
0f113f3e | 1097 | BN_print_fp(stderr, y); |
2db85ac9 P |
1098 | BIO_printf(bio_err, "\n"); |
1099 | goto err; | |
0f113f3e MC |
1100 | } |
1101 | ||
2db85ac9 | 1102 | BIO_printf(bio_out, "A cyclic subgroup:\n"); |
0f113f3e MC |
1103 | k = 100; |
1104 | do { | |
2db85ac9 P |
1105 | if (!TEST_int_ne(k--, 0)) |
1106 | goto err; | |
0f113f3e MC |
1107 | |
1108 | if (EC_POINT_is_at_infinity(group, P)) | |
2db85ac9 | 1109 | BIO_printf(bio_out, " point at infinity\n"); |
0f113f3e | 1110 | else { |
2db85ac9 P |
1111 | if (!TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, |
1112 | ctx))) | |
1113 | goto err; | |
1114 | ||
1115 | BIO_printf(bio_out, " x = 0x"); | |
1116 | BN_print(bio_out, x); | |
1117 | BIO_printf(bio_out, ", y = 0x"); | |
1118 | BN_print(bio_out, y); | |
1119 | BIO_printf(bio_out, "\n"); | |
0f113f3e MC |
1120 | } |
1121 | ||
2db85ac9 P |
1122 | if (!TEST_true(EC_POINT_copy(R, P)) |
1123 | || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))) | |
1124 | goto err; | |
0f113f3e MC |
1125 | } |
1126 | while (!EC_POINT_is_at_infinity(group, P)); | |
1127 | ||
2db85ac9 P |
1128 | if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx)) |
1129 | || !TEST_true(EC_POINT_is_at_infinity(group, P))) | |
1130 | goto err; | |
7793f30e BM |
1131 | |
1132 | /* Change test based on whether binary point compression is enabled or not. */ | |
0f113f3e | 1133 | # ifdef OPENSSL_EC_BIN_PT_COMP |
2db85ac9 P |
1134 | len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, |
1135 | buf, sizeof buf, ctx); | |
1136 | if (!TEST_size_t_ne(len, 0) | |
1137 | || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) | |
1138 | || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) | |
1139 | goto err; | |
1140 | BIO_printf(bio_out, "Generator as octet string, compressed form:\n "); | |
0f113f3e | 1141 | for (i = 0; i < len; i++) |
2db85ac9 | 1142 | BIO_printf(bio_out, "%02X", buf[i]); |
0f113f3e MC |
1143 | # endif |
1144 | ||
2db85ac9 P |
1145 | len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, |
1146 | buf, sizeof buf, ctx); | |
1147 | if (!TEST_size_t_ne(len, 0) | |
1148 | || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) | |
1149 | || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) | |
1150 | goto err; | |
1151 | BIO_printf(bio_out, "\nGenerator as octet string, uncompressed form:\n" | |
1152 | " "); | |
0f113f3e | 1153 | for (i = 0; i < len; i++) |
2db85ac9 | 1154 | BIO_printf(bio_out, "%02X", buf[i]); |
7793f30e | 1155 | |
0f113f3e MC |
1156 | /* Change test based on whether binary point compression is enabled or not. */ |
1157 | # ifdef OPENSSL_EC_BIN_PT_COMP | |
1158 | len = | |
1159 | EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, | |
1160 | ctx); | |
2db85ac9 P |
1161 | if (!TEST_size_t_ne(len, 0) |
1162 | || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx)) | |
1163 | || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx))) | |
1164 | goto err; | |
1165 | BIO_printf(bio_out, "\nGenerator as octet string, hybrid form:\n "); | |
0f113f3e | 1166 | for (i = 0; i < len; i++) |
2db85ac9 | 1167 | BIO_printf(bio_out, "%02X", buf[i]); |
0f113f3e | 1168 | # endif |
2db85ac9 | 1169 | BIO_printf(bio_out, "\n"); |
0f113f3e | 1170 | |
2db85ac9 P |
1171 | if (!TEST_true(EC_POINT_invert(group, P, ctx)) |
1172 | || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))) | |
1173 | goto err; | |
0f113f3e | 1174 | |
0f113f3e | 1175 | |
2db85ac9 P |
1176 | #if 0 |
1177 | #endif | |
1178 | BIO_printf(bio_out, "\n\n"); | |
0f113f3e | 1179 | |
2db85ac9 P |
1180 | r = 1; |
1181 | err: | |
23a1d5e9 | 1182 | BN_CTX_free(ctx); |
0f113f3e MC |
1183 | BN_free(p); |
1184 | BN_free(a); | |
1185 | BN_free(b); | |
1186 | EC_GROUP_free(group); | |
2db85ac9 | 1187 | EC_GROUP_free(tmp); |
0f113f3e MC |
1188 | EC_POINT_free(P); |
1189 | EC_POINT_free(Q); | |
1190 | EC_POINT_free(R); | |
1191 | BN_free(x); | |
1192 | BN_free(y); | |
1193 | BN_free(z); | |
1194 | BN_free(cof); | |
1e2012b7 | 1195 | BN_free(yplusone); |
2db85ac9 | 1196 | return r; |
0f113f3e MC |
1197 | } |
1198 | # endif | |
7793f30e | 1199 | |
2db85ac9 | 1200 | static int internal_curve_test(int n) |
0f113f3e | 1201 | { |
2db85ac9 P |
1202 | EC_GROUP *group = NULL; |
1203 | int nid = curves[n].nid; | |
0f113f3e | 1204 | |
2db85ac9 P |
1205 | if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) { |
1206 | TEST_info("EC_GROUP_new_curve_name() failed with curve %s\n", | |
1207 | OBJ_nid2sn(nid)); | |
1208 | return 0; | |
0f113f3e | 1209 | } |
2db85ac9 P |
1210 | if (!TEST_true(EC_GROUP_check(group, NULL))) { |
1211 | TEST_info("EC_GROUP_check() failed with curve %s\n", OBJ_nid2sn(nid)); | |
0f113f3e | 1212 | EC_GROUP_free(group); |
2db85ac9 | 1213 | return 0; |
0f113f3e | 1214 | } |
2db85ac9 P |
1215 | EC_GROUP_free(group); |
1216 | return 1; | |
1217 | } | |
920ed8c8 | 1218 | |
2db85ac9 P |
1219 | static int internal_curve_test_method(int n) |
1220 | { | |
1221 | int r, nid = curves[n].nid; | |
1222 | EC_GROUP *group; | |
920ed8c8 | 1223 | |
2db85ac9 P |
1224 | /* |
1225 | * Skip for X25519 because low level operations such as EC_POINT_mul() | |
1226 | * are not supported for this curve | |
1227 | */ | |
1228 | if (nid == NID_X25519) | |
1229 | return 1; | |
1230 | if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) { | |
1231 | TEST_info("Curve %s failed\n", OBJ_nid2sn(nid)); | |
1232 | return 0; | |
1233 | } | |
1234 | r = group_order_tests(group); | |
1235 | EC_GROUP_free(group); | |
1236 | return r; | |
0f113f3e MC |
1237 | } |
1238 | ||
1239 | # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 | |
1240 | /* | |
1241 | * nistp_test_params contains magic numbers for testing our optimized | |
1242 | * implementations of several NIST curves with characteristic > 3. | |
1243 | */ | |
1244 | struct nistp_test_params { | |
1245 | const EC_METHOD *(*meth) (); | |
1246 | int degree; | |
1247 | /* | |
1248 | * Qx, Qy and D are taken from | |
79caf5d3 | 1249 | * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf |
0f113f3e MC |
1250 | * Otherwise, values are standard curve parameters from FIPS 180-3 |
1251 | */ | |
1252 | const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d; | |
1253 | }; | |
1254 | ||
1255 | static const struct nistp_test_params nistp_tests_params[] = { | |
1256 | { | |
1257 | /* P-224 */ | |
1258 | EC_GFp_nistp224_method, | |
1259 | 224, | |
1260 | /* p */ | |
1261 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", | |
1262 | /* a */ | |
1263 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", | |
1264 | /* b */ | |
1265 | "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4", | |
1266 | /* Qx */ | |
1267 | "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E", | |
1268 | /* Qy */ | |
1269 | "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555", | |
1270 | /* Gx */ | |
1271 | "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21", | |
1272 | /* Gy */ | |
1273 | "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34", | |
1274 | /* order */ | |
1275 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D", | |
1276 | /* d */ | |
1277 | "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8", | |
1278 | }, | |
1279 | { | |
1280 | /* P-256 */ | |
1281 | EC_GFp_nistp256_method, | |
1282 | 256, | |
1283 | /* p */ | |
1284 | "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", | |
1285 | /* a */ | |
1286 | "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", | |
1287 | /* b */ | |
1288 | "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", | |
1289 | /* Qx */ | |
1290 | "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19", | |
1291 | /* Qy */ | |
1292 | "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09", | |
1293 | /* Gx */ | |
1294 | "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", | |
1295 | /* Gy */ | |
1296 | "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", | |
1297 | /* order */ | |
1298 | "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", | |
1299 | /* d */ | |
1300 | "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96", | |
1301 | }, | |
1302 | { | |
1303 | /* P-521 */ | |
1304 | EC_GFp_nistp521_method, | |
1305 | 521, | |
1306 | /* p */ | |
2db85ac9 P |
1307 | "1ff" |
1308 | "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" | |
1309 | "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | |
0f113f3e | 1310 | /* a */ |
2db85ac9 P |
1311 | "1ff" |
1312 | "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" | |
1313 | "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc", | |
0f113f3e | 1314 | /* b */ |
2db85ac9 P |
1315 | "051" |
1316 | "953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e1" | |
1317 | "56193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", | |
0f113f3e | 1318 | /* Qx */ |
2db85ac9 P |
1319 | "0098" |
1320 | "e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e" | |
1321 | "59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4", | |
0f113f3e | 1322 | /* Qy */ |
2db85ac9 P |
1323 | "0164" |
1324 | "350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8" | |
1325 | "554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e", | |
0f113f3e | 1326 | /* Gx */ |
2db85ac9 P |
1327 | "c6" |
1328 | "858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dba" | |
1329 | "a14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", | |
0f113f3e | 1330 | /* Gy */ |
2db85ac9 P |
1331 | "118" |
1332 | "39296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c" | |
1333 | "97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", | |
0f113f3e | 1334 | /* order */ |
2db85ac9 P |
1335 | "1ff" |
1336 | "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa" | |
1337 | "51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409", | |
0f113f3e | 1338 | /* d */ |
2db85ac9 P |
1339 | "0100" |
1340 | "085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eee" | |
1341 | "df09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722", | |
0f113f3e MC |
1342 | }, |
1343 | }; | |
3e00b4c9 | 1344 | |
2db85ac9 | 1345 | static int nistp_single_test(int idx) |
0f113f3e | 1346 | { |
2db85ac9 P |
1347 | const struct nistp_test_params *test = nistp_tests_params + idx; |
1348 | BN_CTX *ctx = NULL; | |
1349 | BIGNUM *p = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL; | |
1350 | BIGNUM *n = NULL, *m = NULL, *order = NULL, *yplusone = NULL; | |
1351 | EC_GROUP *NISTP = NULL; | |
1352 | EC_POINT *G = NULL, *P = NULL, *Q = NULL, *Q_CHECK = NULL; | |
1353 | int r = 0; | |
0f113f3e | 1354 | |
2db85ac9 | 1355 | BIO_printf(bio_out, "\nNIST curve P-%d (optimised implementation):\n", |
0f113f3e | 1356 | test->degree); |
2db85ac9 P |
1357 | if (!TEST_ptr(ctx = BN_CTX_new()) |
1358 | || !TEST_ptr(p = BN_new()) | |
1359 | || !TEST_ptr(a = BN_new()) | |
1360 | || !TEST_ptr(b = BN_new()) | |
1361 | || !TEST_ptr(x = BN_new()) | |
1362 | || !TEST_ptr(y = BN_new()) | |
1363 | || !TEST_ptr(m = BN_new()) | |
1364 | || !TEST_ptr(n = BN_new()) | |
1365 | || !TEST_ptr(order = BN_new()) | |
1366 | || !TEST_ptr(yplusone = BN_new()) | |
1367 | ||
1368 | || !TEST_ptr(NISTP = EC_GROUP_new(test->meth())) | |
1369 | || !TEST_true(BN_hex2bn(&p, test->p)) | |
1370 | || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) | |
1371 | || !TEST_true(BN_hex2bn(&a, test->a)) | |
1372 | || !TEST_true(BN_hex2bn(&b, test->b)) | |
1373 | || !TEST_true(EC_GROUP_set_curve_GFp(NISTP, p, a, b, ctx)) | |
1374 | || !TEST_ptr(G = EC_POINT_new(NISTP)) | |
1375 | || !TEST_ptr(P = EC_POINT_new(NISTP)) | |
1376 | || !TEST_ptr(Q = EC_POINT_new(NISTP)) | |
1377 | || !TEST_ptr(Q_CHECK = EC_POINT_new(NISTP)) | |
1378 | || !TEST_true(BN_hex2bn(&x, test->Qx)) | |
1379 | || !TEST_true(BN_hex2bn(&y, test->Qy)) | |
1380 | || !TEST_true(BN_add(yplusone, y, BN_value_one())) | |
1e2012b7 EK |
1381 | /* |
1382 | * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, | |
1383 | * and therefore setting the coordinates should fail. | |
1384 | */ | |
2db85ac9 P |
1385 | || !TEST_false(EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, |
1386 | yplusone, ctx)) | |
1387 | || !TEST_true(EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, y, | |
1388 | ctx)) | |
1389 | || !TEST_true(BN_hex2bn(&x, test->Gx)) | |
1390 | || !TEST_true(BN_hex2bn(&y, test->Gy)) | |
1391 | || !TEST_true(EC_POINT_set_affine_coordinates_GFp(NISTP, G, x, y, ctx)) | |
1392 | || !TEST_true(BN_hex2bn(&order, test->order)) | |
1393 | || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) | |
1394 | || !TEST_int_eq(EC_GROUP_get_degree(NISTP), test->degree)) | |
1395 | goto err; | |
1396 | ||
1397 | BIO_printf(bio_out, "NIST test vectors ... "); | |
1398 | if (!TEST_true(BN_hex2bn(&n, test->d))) | |
1399 | goto err; | |
0f113f3e MC |
1400 | /* fixed point multiplication */ |
1401 | EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx); | |
2db85ac9 P |
1402 | if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))) |
1403 | goto err; | |
0f113f3e MC |
1404 | /* random point multiplication */ |
1405 | EC_POINT_mul(NISTP, Q, NULL, G, n, ctx); | |
2db85ac9 P |
1406 | if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) |
1407 | ||
1408 | /* set generator to P = 2*G, where G is the standard generator */ | |
1409 | || !TEST_true(EC_POINT_dbl(NISTP, P, G, ctx)) | |
1410 | || !TEST_true(EC_GROUP_set_generator(NISTP, P, order, BN_value_one())) | |
1411 | /* set the scalar to m=n/2, where n is the NIST test scalar */ | |
1412 | || !TEST_true(BN_rshift(m, n, 1))) | |
1413 | goto err; | |
0f113f3e MC |
1414 | |
1415 | /* test the non-standard generator */ | |
1416 | /* fixed point multiplication */ | |
1417 | EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx); | |
2db85ac9 P |
1418 | if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))) |
1419 | goto err; | |
0f113f3e MC |
1420 | /* random point multiplication */ |
1421 | EC_POINT_mul(NISTP, Q, NULL, P, m, ctx); | |
2db85ac9 | 1422 | if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) |
0f113f3e | 1423 | |
8ce4e7e6 MC |
1424 | /* |
1425 | * We have not performed precomputation so have_precompute mult should be | |
1426 | * false | |
1427 | */ | |
2db85ac9 | 1428 | || !TEST_false(EC_GROUP_have_precompute_mult(NISTP)) |
8ce4e7e6 | 1429 | |
0f113f3e | 1430 | /* now repeat all tests with precomputation */ |
2db85ac9 P |
1431 | || !TEST_true(EC_GROUP_precompute_mult(NISTP, ctx)) |
1432 | || !TEST_true(EC_GROUP_have_precompute_mult(NISTP))) | |
1433 | goto err; | |
0f113f3e MC |
1434 | |
1435 | /* fixed point multiplication */ | |
1436 | EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx); | |
2db85ac9 P |
1437 | if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))) |
1438 | goto err; | |
0f113f3e MC |
1439 | /* random point multiplication */ |
1440 | EC_POINT_mul(NISTP, Q, NULL, P, m, ctx); | |
2db85ac9 | 1441 | if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) |
0f113f3e MC |
1442 | |
1443 | /* reset generator */ | |
2db85ac9 P |
1444 | || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))) |
1445 | goto err; | |
0f113f3e MC |
1446 | /* fixed point multiplication */ |
1447 | EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx); | |
2db85ac9 P |
1448 | if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))) |
1449 | goto err; | |
0f113f3e MC |
1450 | /* random point multiplication */ |
1451 | EC_POINT_mul(NISTP, Q, NULL, G, n, ctx); | |
2db85ac9 P |
1452 | if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))) |
1453 | goto err; | |
0f113f3e | 1454 | |
2db85ac9 P |
1455 | r = group_order_tests(NISTP); |
1456 | err: | |
0f113f3e MC |
1457 | EC_GROUP_free(NISTP); |
1458 | EC_POINT_free(G); | |
1459 | EC_POINT_free(P); | |
1460 | EC_POINT_free(Q); | |
1461 | EC_POINT_free(Q_CHECK); | |
1462 | BN_free(n); | |
1463 | BN_free(m); | |
1464 | BN_free(p); | |
1465 | BN_free(a); | |
1466 | BN_free(b); | |
1467 | BN_free(x); | |
1468 | BN_free(y); | |
1469 | BN_free(order); | |
1e2012b7 | 1470 | BN_free(yplusone); |
0f113f3e | 1471 | BN_CTX_free(ctx); |
2db85ac9 | 1472 | return r; |
0f113f3e MC |
1473 | } |
1474 | # endif | |
7793f30e | 1475 | |
2db85ac9 | 1476 | static int parameter_test(void) |
0110a470 | 1477 | { |
2db85ac9 P |
1478 | EC_GROUP *group = NULL, *group2 = NULL; |
1479 | ECPARAMETERS *ecparameters = NULL; | |
1480 | int r; | |
0110a470 | 1481 | |
2db85ac9 P |
1482 | r = TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp112r1)) |
1483 | && TEST_ptr(ecparameters = EC_GROUP_get_ecparameters(group, NULL)) | |
1484 | && TEST_ptr(group2 = EC_GROUP_new_from_ecparameters(ecparameters)) | |
1485 | && TEST_int_eq(EC_GROUP_cmp(group, group2, NULL), 0); | |
0110a470 KY |
1486 | |
1487 | EC_GROUP_free(group); | |
1488 | EC_GROUP_free(group2); | |
1489 | ECPARAMETERS_free(ecparameters); | |
2db85ac9 | 1490 | return r; |
0110a470 KY |
1491 | } |
1492 | ||
0f113f3e MC |
1493 | static const char rnd_seed[] = |
1494 | "string to make the random number generator think it has entropy"; | |
2db85ac9 | 1495 | #endif |
7793f30e | 1496 | |
2db85ac9 | 1497 | int test_main(int argc, char *argv[]) |
0f113f3e | 1498 | { |
2db85ac9 P |
1499 | int result = EXIT_SUCCESS; |
1500 | #ifndef OPENSSL_NO_EC | |
0f113f3e | 1501 | |
2db85ac9 P |
1502 | crv_len = EC_get_builtin_curves(NULL, 0); |
1503 | if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len)) | |
1504 | || !TEST_true(EC_get_builtin_curves(curves, crv_len))) | |
1505 | return EXIT_FAILURE; | |
0f113f3e MC |
1506 | |
1507 | RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */ | |
1508 | ||
2db85ac9 P |
1509 | ADD_TEST(parameter_test); |
1510 | ADD_TEST(prime_field_tests); | |
0f113f3e | 1511 | # ifndef OPENSSL_NO_EC2M |
2db85ac9 P |
1512 | ADD_TEST(char2_field_tests); |
1513 | ADD_ALL_TESTS(char2_curve_test, OSSL_NELEM(char2_curve_tests)); | |
0f113f3e MC |
1514 | # endif |
1515 | # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 | |
2db85ac9 | 1516 | ADD_ALL_TESTS(nistp_single_test, OSSL_NELEM(nistp_tests_params)); |
0f113f3e | 1517 | # endif |
2db85ac9 P |
1518 | ADD_ALL_TESTS(internal_curve_test, crv_len); |
1519 | ADD_ALL_TESTS(internal_curve_test_method, crv_len); | |
0f113f3e | 1520 | |
2db85ac9 P |
1521 | result = run_tests(argv[0]); |
1522 | OPENSSL_free(curves); | |
bbd86bf5 | 1523 | #endif |
2db85ac9 | 1524 | return result; |
0f113f3e | 1525 | } |