]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ectest.c
error codes are longs, not ints
[thirdparty/openssl.git] / crypto / ec / ectest.c
CommitLineData
62763f68
BM
1/* crypto/ec/ectest.c */
2/* ====================================================================
3 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
adfe54b7
BM
56#include <stdio.h>
57#include <stdlib.h>
48fe4d62 58#include <time.h>
adfe54b7 59
bb62a8b0
BM
60
61#ifdef OPENSSL_NO_EC
62int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
63#else
64
65
62763f68 66#include <openssl/ec.h>
adfe54b7
BM
67#include <openssl/err.h>
68
adfe54b7 69#define ABORT do { \
48fe4d62 70 fflush(stdout); \
bb62a8b0 71 fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
adfe54b7
BM
72 ERR_print_errors_fp(stderr); \
73 exit(1); \
74} while (0)
75
48fe4d62
BM
76
77void timings(EC_GROUP *group, int simult, BN_CTX *ctx)
78 {
79 clock_t clck;
80 int i, j;
81 BIGNUM *s, *s0;
82 EC_POINT *P;
48fe4d62
BM
83
84 s = BN_new();
85 s0 = BN_new();
86 if (s == NULL || s0 == NULL) ABORT;
87
88 if (!EC_GROUP_get_curve_GFp(group, s, NULL, NULL, ctx)) ABORT;
89 fprintf(stdout, "Timings for %d bit prime, ", (int)BN_num_bits(s));
90 if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
e44fceda 91 fprintf(stdout, "%d bit scalars ", (int)BN_num_bits(s));
48fe4d62
BM
92 fflush(stdout);
93
94 P = EC_POINT_new(group);
95 if (P == NULL) ABORT;
96 EC_POINT_copy(P, EC_GROUP_get0_generator(group));
97
48fe4d62
BM
98 clck = clock();
99 for (i = 0; i < 10; i++)
100 {
101 if (!BN_pseudo_rand(s, BN_num_bits(s), 0, 0)) ABORT;
102 if (simult)
103 {
104 if (!BN_pseudo_rand(s0, BN_num_bits(s), 0, 0)) ABORT;
105 }
106 for (j = 0; j < 10; j++)
107 {
38374911 108 if (!EC_POINT_mul(group, P, s, simult ? P : NULL, simult ? s0 : NULL, ctx)) ABORT;
48fe4d62
BM
109 }
110 fprintf(stdout, ".");
111 fflush(stdout);
112 }
113 fprintf(stdout, "\n");
114
115 clck = clock() - clck;
116
117#ifdef CLOCKS_PER_SEC
118 /* "To determine the time in seconds, the value returned
119 * by the clock function should be divided by the value
120 * of the macro CLOCKS_PER_SEC."
121 * -- ISO/IEC 9899 */
122# define UNIT "s"
123#else
124 /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
125 * -- cc on NeXTstep/OpenStep */
126# define UNIT "units"
127# define CLOCKS_PER_SEC 1
128#endif
129
130 fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
131 simult ? "s*P+t*Q operations" : "point multiplications",
132 (double)clck/CLOCKS_PER_SEC);
133 fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
134
135 EC_POINT_free(P);
136 BN_free(s);
137 BN_free(s0);
138 }
139
140
adfe54b7
BM
141int main(int argc, char *argv[])
142 {
bb62a8b0
BM
143 BN_CTX *ctx = NULL;
144 BIGNUM *p, *a, *b;
adfe54b7 145 EC_GROUP *group;
48fe4d62 146 EC_GROUP *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
bb62a8b0
BM
147 EC_POINT *P, *Q, *R;
148 BIGNUM *x, *y, *z;
149 unsigned char buf[100];
150 size_t i, len;
151
10654d3a
BM
152 /* enable memory leak checking unless explicitly disabled */
153 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
154 {
155 CRYPTO_malloc_debug_init();
156 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
157 }
bb62a8b0 158 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
adfe54b7
BM
159 ERR_load_crypto_strings();
160
48fe4d62 161#if 1 /* optional */
bb62a8b0
BM
162 ctx = BN_CTX_new();
163 if (!ctx) ABORT;
164#endif
165
166 p = BN_new();
167 a = BN_new();
168 b = BN_new();
169 if (!p || !a || !b) ABORT;
170
42909e39
BM
171 if (!BN_hex2bn(&p, "17")) ABORT;
172 if (!BN_hex2bn(&a, "1")) ABORT;
173 if (!BN_hex2bn(&b, "1")) ABORT;
bb62a8b0 174
48fe4d62
BM
175 group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
176 * so that the library gets to choose the EC_METHOD */
adfe54b7 177 if (!group) ABORT;
48fe4d62 178
156e8557 179 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
48fe4d62
BM
180
181 {
182 EC_GROUP *tmp;
183 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
184 if (!tmp) ABORT;
185 if (!EC_GROUP_copy(tmp, group));
186 EC_GROUP_free(group);
187 group = tmp;
188 }
189
156e8557 190 if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
bb62a8b0
BM
191
192 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
193 BN_print_fp(stdout, p);
194 fprintf(stdout, ")\n a = 0x");
195 BN_print_fp(stdout, a);
196 fprintf(stdout, "\n b = 0x");
197 BN_print_fp(stdout, b);
198 fprintf(stdout, "\n");
199
200 P = EC_POINT_new(group);
201 Q = EC_POINT_new(group);
202 R = EC_POINT_new(group);
203 if (!P || !Q || !R) ABORT;
204
205 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
206 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
207
208 buf[0] = 0;
209 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
210
211 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
212 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
213
214 x = BN_new();
215 y = BN_new();
216 z = BN_new();
217 if (!x || !y || !z) ABORT;
218
42909e39 219 if (!BN_hex2bn(&x, "D")) ABORT;
bb62a8b0
BM
220 if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
221 if (!EC_POINT_is_on_curve(group, Q, ctx))
222 {
156e8557
BM
223 if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
224 fprintf(stderr, "Point is not on curve: x = 0x");
bb62a8b0 225 BN_print_fp(stderr, x);
156e8557
BM
226 fprintf(stderr, ", y = 0x");
227 BN_print_fp(stderr, y);
bb62a8b0
BM
228 fprintf(stderr, "\n");
229 ABORT;
230 }
231
232 fprintf(stdout, "A cyclic subgroup:\n");
233 do
234 {
235 if (EC_POINT_is_at_infinity(group, P))
236 fprintf(stdout, " point at infinity\n");
237 else
238 {
239 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
240
241 fprintf(stdout, " x = 0x");
242 BN_print_fp(stdout, x);
243 fprintf(stdout, ", y = 0x");
244 BN_print_fp(stdout, y);
245 fprintf(stdout, "\n");
246 }
247
248 if (!EC_POINT_copy(R, P)) ABORT;
249 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
250
251#if 0 /* optional */
48fe4d62
BM
252 {
253 EC_POINT *points[3];
254
255 points[0] = R;
256 points[1] = Q;
257 points[2] = P;
258 if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
259 }
adfe54b7 260#endif
48fe4d62 261
bb62a8b0
BM
262 }
263 while (!EC_POINT_is_at_infinity(group, P));
264
265 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
266 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
adfe54b7 267
bb62a8b0
BM
268 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
269 if (len == 0) ABORT;
270 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
271 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
272 fprintf(stdout, "Generator as octect string, compressed form:\n ");
273 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
274
275 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
276 if (len == 0) ABORT;
277 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
278 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
279 fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n ");
280 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
281
282 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
283 if (len == 0) ABORT;
284 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
285 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
286 fprintf(stdout, "\nGenerator as octect string, hybrid form:\n ");
287 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
288
289 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
290 fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
291 BN_print_fp(stdout, x);
292 fprintf(stdout, ", Y = 0x");
293 BN_print_fp(stdout, y);
294 fprintf(stdout, ", Z = 0x");
295 BN_print_fp(stdout, z);
296 fprintf(stdout, "\n");
297
298 if (!EC_POINT_invert(group, P, ctx)) ABORT;
299 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
300
48fe4d62
BM
301
302 /* Curve P-192 (FIPS PUB 186-2, App. 6) */
303
304 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
305 if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
306 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
307 if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
308 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
309
310 if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
311 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
312 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
313 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
314 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
315
316 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
317 fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x");
318 BN_print_fp(stdout, x);
319 fprintf(stdout, "\n y = 0x");
320 BN_print_fp(stdout, y);
321 fprintf(stdout, "\n");
322 /* G_y value taken from the standard: */
323 if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
324 if (0 != BN_cmp(y, z)) ABORT;
325
38374911 326 fprintf(stdout, "verify group order ...");
616df356 327 fflush(stdout);
48fe4d62 328 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
38374911
BM
329 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
330 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
331 fprintf(stdout, ".");
332 fflush(stdout);
194dd046 333 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
38374911 334 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
48fe4d62 335 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
38374911 336 fprintf(stdout, " ok\n");
48fe4d62
BM
337
338 if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
339 if (!EC_GROUP_copy(P_192, group)) ABORT;
340
341
342 /* Curve P-224 (FIPS PUB 186-2, App. 6) */
343
344 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
345 if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
346 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
347 if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
348 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
349
350 if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
351 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
352 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
353 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
354 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
355
356 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
357 fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x");
358 BN_print_fp(stdout, x);
359 fprintf(stdout, "\n y = 0x");
360 BN_print_fp(stdout, y);
361 fprintf(stdout, "\n");
362 /* G_y value taken from the standard: */
363 if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
364 if (0 != BN_cmp(y, z)) ABORT;
365
38374911 366 fprintf(stdout, "verify group order ...");
616df356 367 fflush(stdout);
48fe4d62 368 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
38374911 369 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
48fe4d62 370 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
38374911
BM
371 fprintf(stdout, ".");
372 fflush(stdout);
194dd046 373 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
38374911
BM
374 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
375 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
376 fprintf(stdout, " ok\n");
377
48fe4d62
BM
378 if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
379 if (!EC_GROUP_copy(P_224, group)) ABORT;
380
381
382 /* Curve P-256 (FIPS PUB 186-2, App. 6) */
383
384 if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
385 if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
386 if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
387 if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
388 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
389
390 if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
391 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
392 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
393 if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
394 "84F3B9CAC2FC632551")) ABORT;
395 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
396
397 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
398 fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x");
399 BN_print_fp(stdout, x);
400 fprintf(stdout, "\n y = 0x");
401 BN_print_fp(stdout, y);
402 fprintf(stdout, "\n");
403 /* G_y value taken from the standard: */
404 if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
405 if (0 != BN_cmp(y, z)) ABORT;
406
38374911 407 fprintf(stdout, "verify group order ...");
616df356 408 fflush(stdout);
48fe4d62 409 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
38374911 410 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
48fe4d62 411 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
38374911
BM
412 fprintf(stdout, ".");
413 fflush(stdout);
194dd046 414 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
38374911
BM
415 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
416 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
417 fprintf(stdout, " ok\n");
418
48fe4d62
BM
419 if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
420 if (!EC_GROUP_copy(P_256, group)) ABORT;
421
422
423 /* Curve P-384 (FIPS PUB 186-2, App. 6) */
424
425 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
426 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
427 if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
428 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
429 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
430 if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
431 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
432 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
433
434 if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
435 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
436 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
437 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
438 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
439 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
440 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
441
442 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
443 fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x");
444 BN_print_fp(stdout, x);
445 fprintf(stdout, "\n y = 0x");
446 BN_print_fp(stdout, y);
447 fprintf(stdout, "\n");
448 /* G_y value taken from the standard: */
449 if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
450 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
451 if (0 != BN_cmp(y, z)) ABORT;
452
38374911 453 fprintf(stdout, "verify group order ...");
616df356 454 fflush(stdout);
48fe4d62 455 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
38374911 456 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
48fe4d62 457 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
38374911
BM
458 fprintf(stdout, ".");
459 fflush(stdout);
194dd046 460 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
38374911
BM
461 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
462 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
463 fprintf(stdout, " ok\n");
464
48fe4d62
BM
465 if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
466 if (!EC_GROUP_copy(P_384, group)) ABORT;
467
468
469 /* Curve P-521 (FIPS PUB 186-2, App. 6) */
470
471 if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
472 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
473 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
474 if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
475 if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
476 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
477 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
478 if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
479 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
480 "DF883D2C34F1EF451FD46B503F00")) ABORT;
481 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
482
483 if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
484 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
485 "3C1856A429BF97E7E31C2E5BD66")) ABORT;
486 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
487 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
488 if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
489 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
490 "C9B8899C47AEBB6FB71E91386409")) ABORT;
491 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
492
493 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
494 fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x");
495 BN_print_fp(stdout, x);
496 fprintf(stdout, "\n y = 0x");
497 BN_print_fp(stdout, y);
498 fprintf(stdout, "\n");
499 /* G_y value taken from the standard: */
500 if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
501 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
502 "7086A272C24088BE94769FD16650")) ABORT;
503 if (0 != BN_cmp(y, z)) ABORT;
504
38374911 505 fprintf(stdout, "verify group order ...");
616df356 506 fflush(stdout);
48fe4d62 507 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
38374911
BM
508 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
509 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
510 fprintf(stdout, ".");
511 fflush(stdout);
194dd046 512 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
38374911 513 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
48fe4d62 514 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
38374911 515 fprintf(stdout, " ok\n");
48fe4d62
BM
516
517 if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
518 if (!EC_GROUP_copy(P_521, group)) ABORT;
519
520
521 /* more tests using the last curve */
522
523 if (!EC_POINT_copy(Q, P)) ABORT;
524 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
525 if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
526 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
527 if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
528
529 if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
530 if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
531 if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
532
533 {
38374911
BM
534 const EC_POINT *points[3];
535 const BIGNUM *scalars[3];
48fe4d62
BM
536
537 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
38374911
BM
538 points[0] = Q;
539 points[1] = Q;
540 points[2] = Q;
48fe4d62
BM
541
542 if (!BN_add(y, z, BN_value_one())) ABORT;
543 if (BN_is_odd(y)) ABORT;
544 if (!BN_rshift1(y, y)) ABORT;
48fe4d62
BM
545 scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
546 scalars[1] = y;
547
86a921af 548 fprintf(stdout, "simultaneous multiplication ...");
616df356 549 fflush(stdout);
48fe4d62
BM
550
551 /* z is still the group order */
552 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
553 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
554 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
555 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
556
86a921af
BM
557 fprintf(stdout, ".");
558 fflush(stdout);
559
560 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
561 if (!BN_copy(z, y)) ABORT;
562 z->neg = 1;
86a921af 563 scalars[0] = y;
38374911 564 scalars[1] = z; /* z = -y */
86a921af
BM
565
566 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
567 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
568
38374911
BM
569 fprintf(stdout, ".");
570 fflush(stdout);
571
572 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
573 if (!BN_add(z, x, y)) ABORT;
574 z->neg = 1;
575 scalars[0] = x;
576 scalars[1] = y;
577 scalars[2] = z; /* z = -(x+y) */
578
579 if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
580 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
581
86a921af 582 fprintf(stdout, " ok\n\n");
48fe4d62
BM
583 }
584
585
6017e604 586#if 0
48fe4d62
BM
587 timings(P_192, 0, ctx);
588 timings(P_192, 1, ctx);
e44fceda
BM
589 timings(P_224, 0, ctx);
590 timings(P_224, 1, ctx);
591 timings(P_256, 0, ctx);
592 timings(P_256, 1, ctx);
593 timings(P_384, 0, ctx);
594 timings(P_384, 1, ctx);
595 timings(P_521, 0, ctx);
596 timings(P_521, 1, ctx);
48fe4d62
BM
597#endif
598
bb62a8b0
BM
599
600 if (ctx)
601 BN_CTX_free(ctx);
602 BN_free(p); BN_free(a); BN_free(b);
603 EC_GROUP_free(group);
604 EC_POINT_free(P);
605 EC_POINT_free(Q);
606 EC_POINT_free(R);
607 BN_free(x); BN_free(y); BN_free(z);
608
48fe4d62
BM
609 if (P_192) EC_GROUP_free(P_192);
610 if (P_224) EC_GROUP_free(P_224);
611 if (P_256) EC_GROUP_free(P_256);
612 if (P_384) EC_GROUP_free(P_384);
613 if (P_521) EC_GROUP_free(P_521);
614
bb62a8b0
BM
615 ERR_free_strings();
616 ERR_remove_state(0);
617 CRYPTO_mem_leaks_fp(stderr);
618
adfe54b7
BM
619 return 0;
620 }
bb62a8b0 621#endif