summaryrefslogtreecommitdiff
path: root/src/regress/lib/libcrypto/ec/ectest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libcrypto/ec/ectest.c')
-rw-r--r--src/regress/lib/libcrypto/ec/ectest.c1328
1 files changed, 1328 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/ec/ectest.c b/src/regress/lib/libcrypto/ec/ectest.c
new file mode 100644
index 0000000000..e3028881e0
--- /dev/null
+++ b/src/regress/lib/libcrypto/ec/ectest.c
@@ -0,0 +1,1328 @@
1/* crypto/ec/ectest.c */
2/*
3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */
5/* ====================================================================
6 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 *
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63 *
64 * The Contribution is licensed pursuant to the OpenSSL open source
65 * license provided above.
66 *
67 * The elliptic curve binary polynomial software is originally written by
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69 *
70 */
71
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#include <time.h>
76
77#include <openssl/ec.h>
78#ifndef OPENSSL_NO_ENGINE
79#include <openssl/engine.h>
80#endif
81#include <openssl/err.h>
82#include <openssl/obj_mac.h>
83#include <openssl/objects.h>
84#include <openssl/rand.h>
85#include <openssl/bn.h>
86#include <openssl/opensslconf.h>
87
88#define ABORT do { \
89 fflush(stdout); \
90 fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
91 ERR_print_errors_fp(stderr); \
92 exit(1); \
93} while (0)
94
95#define TIMING_BASE_PT 0
96#define TIMING_RAND_PT 1
97#define TIMING_SIMUL 2
98
99/* test multiplication with group order, long and negative scalars */
100static void group_order_tests(EC_GROUP *group)
101 {
102 BIGNUM *n1, *n2, *order;
103 EC_POINT *P = EC_POINT_new(group);
104 EC_POINT *Q = EC_POINT_new(group);
105 BN_CTX *ctx = BN_CTX_new();
106
107 n1 = BN_new(); n2 = BN_new(); order = BN_new();
108 fprintf(stdout, "verify group order ...");
109 fflush(stdout);
110 if (!EC_GROUP_get_order(group, order, ctx)) ABORT;
111 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) ABORT;
112 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
113 fprintf(stdout, ".");
114 fflush(stdout);
115 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
116 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) ABORT;
117 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
118 fprintf(stdout, " ok\n");
119 fprintf(stdout, "long/negative scalar tests ... ");
120 if (!BN_one(n1)) ABORT;
121 /* n1 = 1 - order */
122 if (!BN_sub(n1, n1, order)) ABORT;
123 if(!EC_POINT_mul(group, Q, NULL, P, n1, ctx)) ABORT;
124 if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
125 /* n2 = 1 + order */
126 if (!BN_add(n2, order, BN_value_one())) ABORT;
127 if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
128 if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
129 /* n2 = (1 - order) * (1 + order) */
130 if (!BN_mul(n2, n1, n2, ctx)) ABORT;
131 if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
132 if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
133 fprintf(stdout, "ok\n");
134 EC_POINT_free(P);
135 EC_POINT_free(Q);
136 BN_free(n1);
137 BN_free(n2);
138 BN_free(order);
139 BN_CTX_free(ctx);
140 }
141
142static void prime_field_tests(void)
143 {
144 BN_CTX *ctx = NULL;
145 BIGNUM *p, *a, *b;
146 EC_GROUP *group;
147 EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
148 EC_POINT *P, *Q, *R;
149 BIGNUM *x, *y, *z;
150 unsigned char buf[100];
151 size_t i, len;
152 int k;
153
154#if 1 /* optional */
155 ctx = BN_CTX_new();
156 if (!ctx) ABORT;
157#endif
158
159 p = BN_new();
160 a = BN_new();
161 b = BN_new();
162 if (!p || !a || !b) ABORT;
163
164 if (!BN_hex2bn(&p, "17")) ABORT;
165 if (!BN_hex2bn(&a, "1")) ABORT;
166 if (!BN_hex2bn(&b, "1")) ABORT;
167
168 group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
169 * so that the library gets to choose the EC_METHOD */
170 if (!group) ABORT;
171
172 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
173
174 {
175 EC_GROUP *tmp;
176 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
177 if (!tmp) ABORT;
178 if (!EC_GROUP_copy(tmp, group)) ABORT;
179 EC_GROUP_free(group);
180 group = tmp;
181 }
182
183 if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
184
185 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
186 BN_print_fp(stdout, p);
187 fprintf(stdout, ")\n a = 0x");
188 BN_print_fp(stdout, a);
189 fprintf(stdout, "\n b = 0x");
190 BN_print_fp(stdout, b);
191 fprintf(stdout, "\n");
192
193 P = EC_POINT_new(group);
194 Q = EC_POINT_new(group);
195 R = EC_POINT_new(group);
196 if (!P || !Q || !R) ABORT;
197
198 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
199 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
200
201 buf[0] = 0;
202 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
203
204 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
205 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
206
207 x = BN_new();
208 y = BN_new();
209 z = BN_new();
210 if (!x || !y || !z) ABORT;
211
212 if (!BN_hex2bn(&x, "D")) ABORT;
213 if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
214 if (!EC_POINT_is_on_curve(group, Q, ctx))
215 {
216 if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
217 fprintf(stderr, "Point is not on curve: x = 0x");
218 BN_print_fp(stderr, x);
219 fprintf(stderr, ", y = 0x");
220 BN_print_fp(stderr, y);
221 fprintf(stderr, "\n");
222 ABORT;
223 }
224
225 fprintf(stdout, "A cyclic subgroup:\n");
226 k = 100;
227 do
228 {
229 if (k-- == 0) ABORT;
230
231 if (EC_POINT_is_at_infinity(group, P))
232 fprintf(stdout, " point at infinity\n");
233 else
234 {
235 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
236
237 fprintf(stdout, " x = 0x");
238 BN_print_fp(stdout, x);
239 fprintf(stdout, ", y = 0x");
240 BN_print_fp(stdout, y);
241 fprintf(stdout, "\n");
242 }
243
244 if (!EC_POINT_copy(R, P)) ABORT;
245 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
246
247#if 0 /* optional */
248 {
249 EC_POINT *points[3];
250
251 points[0] = R;
252 points[1] = Q;
253 points[2] = P;
254 if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
255 }
256#endif
257
258 }
259 while (!EC_POINT_is_at_infinity(group, P));
260
261 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
262 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
263
264 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
265 if (len == 0) ABORT;
266 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
267 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
268 fprintf(stdout, "Generator as octet string, compressed form:\n ");
269 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
270
271 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
272 if (len == 0) ABORT;
273 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
274 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
275 fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
276 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
277
278 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
279 if (len == 0) ABORT;
280 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
281 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
282 fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
283 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
284
285 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
286 fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
287 BN_print_fp(stdout, x);
288 fprintf(stdout, ", Y = 0x");
289 BN_print_fp(stdout, y);
290 fprintf(stdout, ", Z = 0x");
291 BN_print_fp(stdout, z);
292 fprintf(stdout, "\n");
293
294 if (!EC_POINT_invert(group, P, ctx)) ABORT;
295 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
296
297
298 /* Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2, 2000)
299 * -- not a NIST curve, but commonly used */
300
301 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF")) ABORT;
302 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
303 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC")) ABORT;
304 if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45")) ABORT;
305 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
306
307 if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82")) ABORT;
308 if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
309 if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
310 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
311 if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257")) ABORT;
312 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
313
314 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
315 fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x");
316 BN_print_fp(stdout, x);
317 fprintf(stdout, "\n y = 0x");
318 BN_print_fp(stdout, y);
319 fprintf(stdout, "\n");
320 /* G_y value taken from the standard: */
321 if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
322 if (0 != BN_cmp(y, z)) ABORT;
323
324 fprintf(stdout, "verify degree ...");
325 if (EC_GROUP_get_degree(group) != 160) ABORT;
326 fprintf(stdout, " ok\n");
327
328 group_order_tests(group);
329
330 if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
331 if (!EC_GROUP_copy(P_160, group)) ABORT;
332
333
334 /* Curve P-192 (FIPS PUB 186-2, App. 6) */
335
336 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
337 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
338 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
339 if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
340 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
341
342 if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
343 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
344 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
345 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
346 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
347
348 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
349 fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x");
350 BN_print_fp(stdout, x);
351 fprintf(stdout, "\n y = 0x");
352 BN_print_fp(stdout, y);
353 fprintf(stdout, "\n");
354 /* G_y value taken from the standard: */
355 if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
356 if (0 != BN_cmp(y, z)) ABORT;
357
358 fprintf(stdout, "verify degree ...");
359 if (EC_GROUP_get_degree(group) != 192) ABORT;
360 fprintf(stdout, " ok\n");
361
362 group_order_tests(group);
363
364 if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
365 if (!EC_GROUP_copy(P_192, group)) ABORT;
366
367
368 /* Curve P-224 (FIPS PUB 186-2, App. 6) */
369
370 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
371 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
372 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
373 if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
374 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
375
376 if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
377 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
378 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
379 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
380 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
381
382 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
383 fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x");
384 BN_print_fp(stdout, x);
385 fprintf(stdout, "\n y = 0x");
386 BN_print_fp(stdout, y);
387 fprintf(stdout, "\n");
388 /* G_y value taken from the standard: */
389 if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
390 if (0 != BN_cmp(y, z)) ABORT;
391
392 fprintf(stdout, "verify degree ...");
393 if (EC_GROUP_get_degree(group) != 224) ABORT;
394 fprintf(stdout, " ok\n");
395
396 group_order_tests(group);
397
398 if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
399 if (!EC_GROUP_copy(P_224, group)) ABORT;
400
401
402 /* Curve P-256 (FIPS PUB 186-2, App. 6) */
403
404 if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
405 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
406 if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
407 if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
408 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
409
410 if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
411 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
412 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
413 if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
414 "84F3B9CAC2FC632551")) ABORT;
415 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
416
417 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
418 fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x");
419 BN_print_fp(stdout, x);
420 fprintf(stdout, "\n y = 0x");
421 BN_print_fp(stdout, y);
422 fprintf(stdout, "\n");
423 /* G_y value taken from the standard: */
424 if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
425 if (0 != BN_cmp(y, z)) ABORT;
426
427 fprintf(stdout, "verify degree ...");
428 if (EC_GROUP_get_degree(group) != 256) ABORT;
429 fprintf(stdout, " ok\n");
430
431 group_order_tests(group);
432
433 if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
434 if (!EC_GROUP_copy(P_256, group)) ABORT;
435
436
437 /* Curve P-384 (FIPS PUB 186-2, App. 6) */
438
439 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
440 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
441 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
442 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
443 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
444 if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
445 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
446 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
447
448 if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
449 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
450 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
451 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
452 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
453 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
454 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
455
456 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
457 fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x");
458 BN_print_fp(stdout, x);
459 fprintf(stdout, "\n y = 0x");
460 BN_print_fp(stdout, y);
461 fprintf(stdout, "\n");
462 /* G_y value taken from the standard: */
463 if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
464 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
465 if (0 != BN_cmp(y, z)) ABORT;
466
467 fprintf(stdout, "verify degree ...");
468 if (EC_GROUP_get_degree(group) != 384) ABORT;
469 fprintf(stdout, " ok\n");
470
471 group_order_tests(group);
472
473 if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
474 if (!EC_GROUP_copy(P_384, group)) ABORT;
475
476
477 /* Curve P-521 (FIPS PUB 186-2, App. 6) */
478
479 if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
480 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
481 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
482 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
483 if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
484 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
485 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
486 if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
487 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
488 "DF883D2C34F1EF451FD46B503F00")) ABORT;
489 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
490
491 if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
492 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
493 "3C1856A429BF97E7E31C2E5BD66")) ABORT;
494 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
495 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
496 if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
497 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
498 "C9B8899C47AEBB6FB71E91386409")) ABORT;
499 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
500
501 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
502 fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x");
503 BN_print_fp(stdout, x);
504 fprintf(stdout, "\n y = 0x");
505 BN_print_fp(stdout, y);
506 fprintf(stdout, "\n");
507 /* G_y value taken from the standard: */
508 if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
509 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
510 "7086A272C24088BE94769FD16650")) ABORT;
511 if (0 != BN_cmp(y, z)) ABORT;
512
513 fprintf(stdout, "verify degree ...");
514 if (EC_GROUP_get_degree(group) != 521) ABORT;
515 fprintf(stdout, " ok\n");
516
517 group_order_tests(group);
518
519 if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
520 if (!EC_GROUP_copy(P_521, group)) ABORT;
521
522
523 /* more tests using the last curve */
524
525 if (!EC_POINT_copy(Q, P)) ABORT;
526 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
527 if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
528 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
529 if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
530
531 if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
532 if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
533 if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
534
535 {
536 const EC_POINT *points[4];
537 const BIGNUM *scalars[4];
538 BIGNUM scalar3;
539
540 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
541 points[0] = Q;
542 points[1] = Q;
543 points[2] = Q;
544 points[3] = Q;
545
546 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
547 if (!BN_add(y, z, BN_value_one())) ABORT;
548 if (BN_is_odd(y)) ABORT;
549 if (!BN_rshift1(y, y)) ABORT;
550 scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
551 scalars[1] = y;
552
553 fprintf(stdout, "combined multiplication ...");
554 fflush(stdout);
555
556 /* z is still the group order */
557 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
558 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
559 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
560 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
561
562 fprintf(stdout, ".");
563 fflush(stdout);
564
565 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
566 if (!BN_add(z, z, y)) ABORT;
567 BN_set_negative(z, 1);
568 scalars[0] = y;
569 scalars[1] = z; /* z = -(order + y) */
570
571 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
572 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
573
574 fprintf(stdout, ".");
575 fflush(stdout);
576
577 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
578 if (!BN_add(z, x, y)) ABORT;
579 BN_set_negative(z, 1);
580 scalars[0] = x;
581 scalars[1] = y;
582 scalars[2] = z; /* z = -(x+y) */
583
584 BN_init(&scalar3);
585 BN_zero(&scalar3);
586 scalars[3] = &scalar3;
587
588 if (!EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx)) ABORT;
589 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
590
591 fprintf(stdout, " ok\n\n");
592
593 BN_free(&scalar3);
594 }
595
596
597 if (ctx)
598 BN_CTX_free(ctx);
599 BN_free(p); BN_free(a); BN_free(b);
600 EC_GROUP_free(group);
601 EC_POINT_free(P);
602 EC_POINT_free(Q);
603 EC_POINT_free(R);
604 BN_free(x); BN_free(y); BN_free(z);
605
606 if (P_160) EC_GROUP_free(P_160);
607 if (P_192) EC_GROUP_free(P_192);
608 if (P_224) EC_GROUP_free(P_224);
609 if (P_256) EC_GROUP_free(P_256);
610 if (P_384) EC_GROUP_free(P_384);
611 if (P_521) EC_GROUP_free(P_521);
612
613 }
614
615/* Change test based on whether binary point compression is enabled or not. */
616#ifdef OPENSSL_EC_BIN_PT_COMP
617#define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
618 if (!BN_hex2bn(&x, _x)) ABORT; \
619 if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
620 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
621 if (!BN_hex2bn(&z, _order)) ABORT; \
622 if (!BN_hex2bn(&cof, _cof)) ABORT; \
623 if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
624 if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
625 fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
626 BN_print_fp(stdout, x); \
627 fprintf(stdout, "\n y = 0x"); \
628 BN_print_fp(stdout, y); \
629 fprintf(stdout, "\n"); \
630 /* G_y value taken from the standard: */ \
631 if (!BN_hex2bn(&z, _y)) ABORT; \
632 if (0 != BN_cmp(y, z)) ABORT;
633#else
634#define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
635 if (!BN_hex2bn(&x, _x)) ABORT; \
636 if (!BN_hex2bn(&y, _y)) ABORT; \
637 if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
638 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
639 if (!BN_hex2bn(&z, _order)) ABORT; \
640 if (!BN_hex2bn(&cof, _cof)) ABORT; \
641 if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
642 fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
643 BN_print_fp(stdout, x); \
644 fprintf(stdout, "\n y = 0x"); \
645 BN_print_fp(stdout, y); \
646 fprintf(stdout, "\n");
647#endif
648
649#define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
650 if (!BN_hex2bn(&p, _p)) ABORT; \
651 if (!BN_hex2bn(&a, _a)) ABORT; \
652 if (!BN_hex2bn(&b, _b)) ABORT; \
653 if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
654 CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
655 fprintf(stdout, "verify degree ..."); \
656 if (EC_GROUP_get_degree(group) != _degree) ABORT; \
657 fprintf(stdout, " ok\n"); \
658 group_order_tests(group); \
659 if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
660 if (!EC_GROUP_copy(_variable, group)) ABORT; \
661
662#ifndef OPENSSL_NO_EC2M
663
664static void char2_field_tests(void)
665 {
666 BN_CTX *ctx = NULL;
667 BIGNUM *p, *a, *b;
668 EC_GROUP *group;
669 EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 = NULL, *C2_K571 = NULL;
670 EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 = NULL, *C2_B571 = NULL;
671 EC_POINT *P, *Q, *R;
672 BIGNUM *x, *y, *z, *cof;
673 unsigned char buf[100];
674 size_t i, len;
675 int k;
676
677#if 1 /* optional */
678 ctx = BN_CTX_new();
679 if (!ctx) ABORT;
680#endif
681
682 p = BN_new();
683 a = BN_new();
684 b = BN_new();
685 if (!p || !a || !b) ABORT;
686
687 if (!BN_hex2bn(&p, "13")) ABORT;
688 if (!BN_hex2bn(&a, "3")) ABORT;
689 if (!BN_hex2bn(&b, "1")) ABORT;
690
691 group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use EC_GROUP_new_curve_GF2m
692 * so that the library gets to choose the EC_METHOD */
693 if (!group) ABORT;
694 if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT;
695
696 {
697 EC_GROUP *tmp;
698 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
699 if (!tmp) ABORT;
700 if (!EC_GROUP_copy(tmp, group)) ABORT;
701 EC_GROUP_free(group);
702 group = tmp;
703 }
704
705 if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)) ABORT;
706
707 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
708 BN_print_fp(stdout, p);
709 fprintf(stdout, ")\n a = 0x");
710 BN_print_fp(stdout, a);
711 fprintf(stdout, "\n b = 0x");
712 BN_print_fp(stdout, b);
713 fprintf(stdout, "\n(0x... means binary polynomial)\n");
714
715 P = EC_POINT_new(group);
716 Q = EC_POINT_new(group);
717 R = EC_POINT_new(group);
718 if (!P || !Q || !R) ABORT;
719
720 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
721 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
722
723 buf[0] = 0;
724 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
725
726 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
727 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
728
729 x = BN_new();
730 y = BN_new();
731 z = BN_new();
732 cof = BN_new();
733 if (!x || !y || !z || !cof) ABORT;
734
735 if (!BN_hex2bn(&x, "6")) ABORT;
736/* Change test based on whether binary point compression is enabled or not. */
737#ifdef OPENSSL_EC_BIN_PT_COMP
738 if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx)) ABORT;
739#else
740 if (!BN_hex2bn(&y, "8")) ABORT;
741 if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
742#endif
743 if (!EC_POINT_is_on_curve(group, Q, ctx))
744 {
745/* Change test based on whether binary point compression is enabled or not. */
746#ifdef OPENSSL_EC_BIN_PT_COMP
747 if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
748#endif
749 fprintf(stderr, "Point is not on curve: x = 0x");
750 BN_print_fp(stderr, x);
751 fprintf(stderr, ", y = 0x");
752 BN_print_fp(stderr, y);
753 fprintf(stderr, "\n");
754 ABORT;
755 }
756
757 fprintf(stdout, "A cyclic subgroup:\n");
758 k = 100;
759 do
760 {
761 if (k-- == 0) ABORT;
762
763 if (EC_POINT_is_at_infinity(group, P))
764 fprintf(stdout, " point at infinity\n");
765 else
766 {
767 if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT;
768
769 fprintf(stdout, " x = 0x");
770 BN_print_fp(stdout, x);
771 fprintf(stdout, ", y = 0x");
772 BN_print_fp(stdout, y);
773 fprintf(stdout, "\n");
774 }
775
776 if (!EC_POINT_copy(R, P)) ABORT;
777 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
778 }
779 while (!EC_POINT_is_at_infinity(group, P));
780
781 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
782 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
783
784/* Change test based on whether binary point compression is enabled or not. */
785#ifdef OPENSSL_EC_BIN_PT_COMP
786 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
787 if (len == 0) ABORT;
788 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
789 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
790 fprintf(stdout, "Generator as octet string, compressed form:\n ");
791 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
792#endif
793
794 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
795 if (len == 0) ABORT;
796 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
797 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
798 fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
799 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
800
801/* Change test based on whether binary point compression is enabled or not. */
802#ifdef OPENSSL_EC_BIN_PT_COMP
803 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
804 if (len == 0) ABORT;
805 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
806 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
807 fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
808 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
809#endif
810
811 fprintf(stdout, "\n");
812
813 if (!EC_POINT_invert(group, P, ctx)) ABORT;
814 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
815
816
817 /* Curve K-163 (FIPS PUB 186-2, App. 6) */
818 CHAR2_CURVE_TEST
819 (
820 "NIST curve K-163",
821 "0800000000000000000000000000000000000000C9",
822 "1",
823 "1",
824 "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
825 "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
826 1,
827 "04000000000000000000020108A2E0CC0D99F8A5EF",
828 "2",
829 163,
830 C2_K163
831 );
832
833 /* Curve B-163 (FIPS PUB 186-2, App. 6) */
834 CHAR2_CURVE_TEST
835 (
836 "NIST curve B-163",
837 "0800000000000000000000000000000000000000C9",
838 "1",
839 "020A601907B8C953CA1481EB10512F78744A3205FD",
840 "03F0EBA16286A2D57EA0991168D4994637E8343E36",
841 "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
842 1,
843 "040000000000000000000292FE77E70C12A4234C33",
844 "2",
845 163,
846 C2_B163
847 );
848
849 /* Curve K-233 (FIPS PUB 186-2, App. 6) */
850 CHAR2_CURVE_TEST
851 (
852 "NIST curve K-233",
853 "020000000000000000000000000000000000000004000000000000000001",
854 "0",
855 "1",
856 "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
857 "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
858 0,
859 "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
860 "4",
861 233,
862 C2_K233
863 );
864
865 /* Curve B-233 (FIPS PUB 186-2, App. 6) */
866 CHAR2_CURVE_TEST
867 (
868 "NIST curve B-233",
869 "020000000000000000000000000000000000000004000000000000000001",
870 "000000000000000000000000000000000000000000000000000000000001",
871 "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
872 "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
873 "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
874 1,
875 "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
876 "2",
877 233,
878 C2_B233
879 );
880
881 /* Curve K-283 (FIPS PUB 186-2, App. 6) */
882 CHAR2_CURVE_TEST
883 (
884 "NIST curve K-283",
885 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
886 "0",
887 "1",
888 "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
889 "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
890 0,
891 "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
892 "4",
893 283,
894 C2_K283
895 );
896
897 /* Curve B-283 (FIPS PUB 186-2, App. 6) */
898 CHAR2_CURVE_TEST
899 (
900 "NIST curve B-283",
901 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
902 "000000000000000000000000000000000000000000000000000000000000000000000001",
903 "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
904 "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
905 "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
906 1,
907 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
908 "2",
909 283,
910 C2_B283
911 );
912
913 /* Curve K-409 (FIPS PUB 186-2, App. 6) */
914 CHAR2_CURVE_TEST
915 (
916 "NIST curve K-409",
917 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
918 "0",
919 "1",
920 "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
921 "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
922 1,
923 "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
924 "4",
925 409,
926 C2_K409
927 );
928
929 /* Curve B-409 (FIPS PUB 186-2, App. 6) */
930 CHAR2_CURVE_TEST
931 (
932 "NIST curve B-409",
933 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
934 "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
935 "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
936 "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
937 "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
938 1,
939 "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
940 "2",
941 409,
942 C2_B409
943 );
944
945 /* Curve K-571 (FIPS PUB 186-2, App. 6) */
946 CHAR2_CURVE_TEST
947 (
948 "NIST curve K-571",
949 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
950 "0",
951 "1",
952 "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
953 "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
954 0,
955 "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
956 "4",
957 571,
958 C2_K571
959 );
960
961 /* Curve B-571 (FIPS PUB 186-2, App. 6) */
962 CHAR2_CURVE_TEST
963 (
964 "NIST curve B-571",
965 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
966 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
967 "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
968 "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
969 "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
970 1,
971 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
972 "2",
973 571,
974 C2_B571
975 );
976
977 /* more tests using the last curve */
978
979 if (!EC_POINT_copy(Q, P)) ABORT;
980 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
981 if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
982 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
983 if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
984
985 if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
986 if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
987 if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
988
989 {
990 const EC_POINT *points[3];
991 const BIGNUM *scalars[3];
992
993 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
994 points[0] = Q;
995 points[1] = Q;
996 points[2] = Q;
997
998 if (!BN_add(y, z, BN_value_one())) ABORT;
999 if (BN_is_odd(y)) ABORT;
1000 if (!BN_rshift1(y, y)) ABORT;
1001 scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
1002 scalars[1] = y;
1003
1004 fprintf(stdout, "combined multiplication ...");
1005 fflush(stdout);
1006
1007 /* z is still the group order */
1008 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1009 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
1010 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
1011 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
1012
1013 fprintf(stdout, ".");
1014 fflush(stdout);
1015
1016 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
1017 if (!BN_add(z, z, y)) ABORT;
1018 BN_set_negative(z, 1);
1019 scalars[0] = y;
1020 scalars[1] = z; /* z = -(order + y) */
1021
1022 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1023 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1024
1025 fprintf(stdout, ".");
1026 fflush(stdout);
1027
1028 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
1029 if (!BN_add(z, x, y)) ABORT;
1030 BN_set_negative(z, 1);
1031 scalars[0] = x;
1032 scalars[1] = y;
1033 scalars[2] = z; /* z = -(x+y) */
1034
1035 if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
1036 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1037
1038 fprintf(stdout, " ok\n\n");
1039 }
1040
1041
1042 if (ctx)
1043 BN_CTX_free(ctx);
1044 BN_free(p); BN_free(a); BN_free(b);
1045 EC_GROUP_free(group);
1046 EC_POINT_free(P);
1047 EC_POINT_free(Q);
1048 EC_POINT_free(R);
1049 BN_free(x); BN_free(y); BN_free(z); BN_free(cof);
1050
1051 if (C2_K163) EC_GROUP_free(C2_K163);
1052 if (C2_B163) EC_GROUP_free(C2_B163);
1053 if (C2_K233) EC_GROUP_free(C2_K233);
1054 if (C2_B233) EC_GROUP_free(C2_B233);
1055 if (C2_K283) EC_GROUP_free(C2_K283);
1056 if (C2_B283) EC_GROUP_free(C2_B283);
1057 if (C2_K409) EC_GROUP_free(C2_K409);
1058 if (C2_B409) EC_GROUP_free(C2_B409);
1059 if (C2_K571) EC_GROUP_free(C2_K571);
1060 if (C2_B571) EC_GROUP_free(C2_B571);
1061
1062 }
1063#endif
1064
1065static void internal_curve_test(void)
1066 {
1067 EC_builtin_curve *curves = NULL;
1068 size_t crv_len = 0, n = 0;
1069 int ok = 1;
1070
1071 crv_len = EC_get_builtin_curves(NULL, 0);
1072
1073 curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
1074
1075 if (curves == NULL)
1076 return;
1077
1078 if (!EC_get_builtin_curves(curves, crv_len))
1079 {
1080 OPENSSL_free(curves);
1081 return;
1082 }
1083
1084 fprintf(stdout, "testing internal curves: ");
1085
1086 for (n = 0; n < crv_len; n++)
1087 {
1088 EC_GROUP *group = NULL;
1089 int nid = curves[n].nid;
1090 if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL)
1091 {
1092 ok = 0;
1093 fprintf(stdout, "\nEC_GROUP_new_curve_name() failed with"
1094 " curve %s\n", OBJ_nid2sn(nid));
1095 /* try next curve */
1096 continue;
1097 }
1098 if (!EC_GROUP_check(group, NULL))
1099 {
1100 ok = 0;
1101 fprintf(stdout, "\nEC_GROUP_check() failed with"
1102 " curve %s\n", OBJ_nid2sn(nid));
1103 EC_GROUP_free(group);
1104 /* try the next curve */
1105 continue;
1106 }
1107 fprintf(stdout, ".");
1108 fflush(stdout);
1109 EC_GROUP_free(group);
1110 }
1111 if (ok)
1112 fprintf(stdout, " ok\n\n");
1113 else
1114 {
1115 fprintf(stdout, " failed\n\n");
1116 ABORT;
1117 }
1118 OPENSSL_free(curves);
1119 return;
1120 }
1121
1122#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1123/* nistp_test_params contains magic numbers for testing our optimized
1124 * implementations of several NIST curves with characteristic > 3. */
1125struct nistp_test_params
1126 {
1127 const EC_METHOD* (*meth) ();
1128 int degree;
1129 /* Qx, Qy and D are taken from
1130 * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
1131 * Otherwise, values are standard curve parameters from FIPS 180-3 */
1132 const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
1133 };
1134
1135static const struct nistp_test_params nistp_tests_params[] =
1136 {
1137 {
1138 /* P-224 */
1139 EC_GFp_nistp224_method,
1140 224,
1141 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", /* p */
1142 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", /* a */
1143 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4", /* b */
1144 "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E", /* Qx */
1145 "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555", /* Qy */
1146 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21", /* Gx */
1147 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34", /* Gy */
1148 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D", /* order */
1149 "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8", /* d */
1150 },
1151 {
1152 /* P-256 */
1153 EC_GFp_nistp256_method,
1154 256,
1155 "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", /* p */
1156 "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", /* a */
1157 "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", /* b */
1158 "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19", /* Qx */
1159 "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09", /* Qy */
1160 "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", /* Gx */
1161 "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", /* Gy */
1162 "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", /* order */
1163 "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96", /* d */
1164 },
1165 {
1166 /* P-521 */
1167 EC_GFp_nistp521_method,
1168 521,
1169 "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", /* p */
1170 "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc", /* a */
1171 "051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", /* b */
1172 "0098e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4", /* Qx */
1173 "0164350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e", /* Qy */
1174 "c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", /* Gx */
1175 "11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", /* Gy */
1176 "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409", /* order */
1177 "0100085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eeedf09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722", /* d */
1178 },
1179 };
1180
1181void nistp_single_test(const struct nistp_test_params *test)
1182 {
1183 BN_CTX *ctx;
1184 BIGNUM *p, *a, *b, *x, *y, *n, *m, *order;
1185 EC_GROUP *NISTP;
1186 EC_POINT *G, *P, *Q, *Q_CHECK;
1187
1188 fprintf(stdout, "\nNIST curve P-%d (optimised implementation):\n", test->degree);
1189 ctx = BN_CTX_new();
1190 p = BN_new();
1191 a = BN_new();
1192 b = BN_new();
1193 x = BN_new(); y = BN_new();
1194 m = BN_new(); n = BN_new(); order = BN_new();
1195
1196 NISTP = EC_GROUP_new(test->meth());
1197 if(!NISTP) ABORT;
1198 if (!BN_hex2bn(&p, test->p)) ABORT;
1199 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
1200 if (!BN_hex2bn(&a, test->a)) ABORT;
1201 if (!BN_hex2bn(&b, test->b)) ABORT;
1202 if (!EC_GROUP_set_curve_GFp(NISTP, p, a, b, ctx)) ABORT;
1203 G = EC_POINT_new(NISTP);
1204 P = EC_POINT_new(NISTP);
1205 Q = EC_POINT_new(NISTP);
1206 Q_CHECK = EC_POINT_new(NISTP);
1207 if(!BN_hex2bn(&x, test->Qx)) ABORT;
1208 if(!BN_hex2bn(&y, test->Qy)) ABORT;
1209 if(!EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, y, ctx)) ABORT;
1210 if (!BN_hex2bn(&x, test->Gx)) ABORT;
1211 if (!BN_hex2bn(&y, test->Gy)) ABORT;
1212 if (!EC_POINT_set_affine_coordinates_GFp(NISTP, G, x, y, ctx)) ABORT;
1213 if (!BN_hex2bn(&order, test->order)) ABORT;
1214 if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) ABORT;
1215
1216 fprintf(stdout, "verify degree ... ");
1217 if (EC_GROUP_get_degree(NISTP) != test->degree) ABORT;
1218 fprintf(stdout, "ok\n");
1219
1220 fprintf(stdout, "NIST test vectors ... ");
1221 if (!BN_hex2bn(&n, test->d)) ABORT;
1222 /* fixed point multiplication */
1223 EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
1224 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1225 /* random point multiplication */
1226 EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
1227 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1228
1229 /* set generator to P = 2*G, where G is the standard generator */
1230 if (!EC_POINT_dbl(NISTP, P, G, ctx)) ABORT;
1231 if (!EC_GROUP_set_generator(NISTP, P, order, BN_value_one())) ABORT;
1232 /* set the scalar to m=n/2, where n is the NIST test scalar */
1233 if (!BN_rshift(m, n, 1)) ABORT;
1234
1235 /* test the non-standard generator */
1236 /* fixed point multiplication */
1237 EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
1238 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1239 /* random point multiplication */
1240 EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
1241 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1242
1243 /* now repeat all tests with precomputation */
1244 if (!EC_GROUP_precompute_mult(NISTP, ctx)) ABORT;
1245
1246 /* fixed point multiplication */
1247 EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
1248 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1249 /* random point multiplication */
1250 EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
1251 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1252
1253 /* reset generator */
1254 if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) ABORT;
1255 /* fixed point multiplication */
1256 EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
1257 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1258 /* random point multiplication */
1259 EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
1260 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1261
1262 fprintf(stdout, "ok\n");
1263 group_order_tests(NISTP);
1264 EC_GROUP_free(NISTP);
1265 EC_POINT_free(G);
1266 EC_POINT_free(P);
1267 EC_POINT_free(Q);
1268 EC_POINT_free(Q_CHECK);
1269 BN_free(n);
1270 BN_free(m);
1271 BN_free(p);
1272 BN_free(a);
1273 BN_free(b);
1274 BN_free(x);
1275 BN_free(y);
1276 BN_free(order);
1277 BN_CTX_free(ctx);
1278 }
1279
1280void nistp_tests()
1281 {
1282 unsigned i;
1283
1284 for (i = 0; i < sizeof(nistp_tests_params) / sizeof(struct nistp_test_params); i++)
1285 {
1286 nistp_single_test(&nistp_tests_params[i]);
1287 }
1288 }
1289#endif
1290
1291int main(int argc, char *argv[])
1292 {
1293
1294 /* enable memory leak checking unless explicitly disabled */
1295 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
1296 {
1297 CRYPTO_malloc_debug_init();
1298 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
1299 }
1300 else
1301 {
1302 /* OPENSSL_DEBUG_MEMORY=off */
1303 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
1304 }
1305 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1306 ERR_load_crypto_strings();
1307
1308 prime_field_tests();
1309 puts("");
1310#ifndef OPENSSL_NO_EC2M
1311 char2_field_tests();
1312#endif
1313#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1314 nistp_tests();
1315#endif
1316 /* test the internal curves */
1317 internal_curve_test();
1318
1319#ifndef OPENSSL_NO_ENGINE
1320 ENGINE_cleanup();
1321#endif
1322 CRYPTO_cleanup_all_ex_data();
1323 ERR_free_strings();
1324 ERR_remove_thread_state(NULL);
1325 CRYPTO_mem_leaks_fp(stderr);
1326
1327 return 0;
1328 }