2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the elliptic curve math library for binary polynomial field curves.
17 * The Initial Developer of the Original Code is
18 * Sun Microsystems, Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
23 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
44 #include "ecl-curve.h"
52 #include <sys/resource.h>
54 /* Time k repetitions of operation op. */
55 #define M_TimeOperation(op, k) { \
56 double dStart, dNow, dUserTime; \
59 getrusage(RUSAGE_SELF, &ru); \
60 dStart = (double)ru.ru_utime.tv_sec+(double)ru.ru_utime.tv_usec*0.000001; \
61 for (i = 0; i < k; i++) { \
64 getrusage(RUSAGE_SELF, &ru); \
65 dNow = (double)ru.ru_utime.tv_sec+(double)ru.ru_utime.tv_usec*0.000001; \
66 dUserTime = dNow-dStart; \
67 if (dUserTime) printf(" %-45s k: %6i, t: %6.2f sec\n", #op, k, dUserTime); \
70 /* Test curve using generic field arithmetic. */
71 #define ECTEST_GENERIC_GF2M(name_c, name) \
72 printf("Testing %s using generic implementation...\n", name_c); \
73 params = EC_GetNamedCurveParams(name); \
74 if (params == NULL) { \
75 printf(" Error: could not construct params.\n"); \
79 ECGroup_free(group); \
80 group = ECGroup_fromHex(params); \
81 if (group == NULL) { \
82 printf(" Error: could not construct group.\n"); \
86 MP_CHECKOK( ectest_curve_GF2m(group, ectestPrint, ectestTime, 1) ); \
87 printf("... okay.\n");
89 /* Test curve using specific field arithmetic. */
90 #define ECTEST_NAMED_GF2M(name_c, name) \
91 printf("Testing %s using specific implementation...\n", name_c); \
92 ECGroup_free(group); \
93 group = ECGroup_fromName(name); \
94 if (group == NULL) { \
95 printf(" Warning: could not construct group.\n"); \
96 printf("... failed; continuing with remaining tests.\n"); \
98 MP_CHECKOK( ectest_curve_GF2m(group, ectestPrint, ectestTime, 0) ); \
99 printf("... okay.\n"); \
102 /* Performs basic tests of elliptic curve cryptography over binary
103 * polynomial fields. If tests fail, then it prints an error message,
104 * aborts, and returns an error code. Otherwise, returns 0. */
106 ectest_curve_GF2m(ECGroup
*group
, int ectestPrint
, int ectestTime
,
110 mp_int one
, order_1
, gx
, gy
, rx
, ry
, n
;
115 /* initialize values */
116 MP_CHECKOK(mp_init(&one
));
117 MP_CHECKOK(mp_init(&order_1
));
118 MP_CHECKOK(mp_init(&gx
));
119 MP_CHECKOK(mp_init(&gy
));
120 MP_CHECKOK(mp_init(&rx
));
121 MP_CHECKOK(mp_init(&ry
));
122 MP_CHECKOK(mp_init(&n
));
124 MP_CHECKOK(mp_set_int(&one
, 1));
125 MP_CHECKOK(mp_sub(&group
->order
, &one
, &order_1
));
127 /* encode base point */
128 if (group
->meth
->field_dec
) {
129 MP_CHECKOK(group
->meth
->field_dec(&group
->genx
, &gx
, group
->meth
));
130 MP_CHECKOK(group
->meth
->field_dec(&group
->geny
, &gy
, group
->meth
));
132 MP_CHECKOK(mp_copy(&group
->genx
, &gx
));
133 MP_CHECKOK(mp_copy(&group
->geny
, &gy
));
137 /* output base point */
138 printf(" base point P:\n");
139 MP_CHECKOK(mp_toradix(&gx
, s
, 16));
141 MP_CHECKOK(mp_toradix(&gy
, s
, 16));
143 if (group
->meth
->field_enc
) {
144 printf(" base point P (encoded):\n");
145 MP_CHECKOK(mp_toradix(&group
->genx
, s
, 16));
147 MP_CHECKOK(mp_toradix(&group
->geny
, s
, 16));
152 #ifdef ECL_ENABLE_GF2M_PT_MUL_AFF
153 /* multiply base point by order - 1 and check for negative of base
155 MP_CHECKOK(ec_GF2m_pt_mul_aff
156 (&order_1
, &group
->genx
, &group
->geny
, &rx
, &ry
, group
));
158 printf(" (order-1)*P (affine):\n");
159 MP_CHECKOK(mp_toradix(&rx
, s
, 16));
161 MP_CHECKOK(mp_toradix(&ry
, s
, 16));
164 MP_CHECKOK(group
->meth
->field_add(&ry
, &rx
, &ry
, group
->meth
));
165 if ((mp_cmp(&rx
, &group
->genx
) != 0)
166 || (mp_cmp(&ry
, &group
->geny
) != 0)) {
167 printf(" Error: invalid result (expected (- base point)).\n");
173 /* multiply base point by order - 1 and check for negative of base
175 MP_CHECKOK(ec_GF2m_pt_mul_mont
176 (&order_1
, &group
->genx
, &group
->geny
, &rx
, &ry
, group
));
178 printf(" (order-1)*P (montgomery):\n");
179 MP_CHECKOK(mp_toradix(&rx
, s
, 16));
181 MP_CHECKOK(mp_toradix(&ry
, s
, 16));
184 MP_CHECKOK(group
->meth
->field_add(&ry
, &rx
, &ry
, group
->meth
));
185 if ((mp_cmp(&rx
, &group
->genx
) != 0)
186 || (mp_cmp(&ry
, &group
->geny
) != 0)) {
187 printf(" Error: invalid result (expected (- base point)).\n");
192 #ifdef ECL_ENABLE_GF2M_PROJ
193 /* multiply base point by order - 1 and check for negative of base
195 MP_CHECKOK(ec_GF2m_pt_mul_proj
196 (&order_1
, &group
->genx
, &group
->geny
, &rx
, &ry
, group
));
198 printf(" (order-1)*P (projective):\n");
199 MP_CHECKOK(mp_toradix(&rx
, s
, 16));
201 MP_CHECKOK(mp_toradix(&ry
, s
, 16));
204 MP_CHECKOK(group
->meth
->field_add(&ry
, &rx
, &ry
, group
->meth
));
205 if ((mp_cmp(&rx
, &group
->genx
) != 0)
206 || (mp_cmp(&ry
, &group
->geny
) != 0)) {
207 printf(" Error: invalid result (expected (- base point)).\n");
213 /* multiply base point by order - 1 and check for negative of base
215 MP_CHECKOK(ECPoint_mul(group
, &order_1
, NULL
, NULL
, &rx
, &ry
));
217 printf(" (order-1)*P (ECPoint_mul):\n");
218 MP_CHECKOK(mp_toradix(&rx
, s
, 16));
220 MP_CHECKOK(mp_toradix(&ry
, s
, 16));
223 MP_CHECKOK(ec_GF2m_add(&ry
, &rx
, &ry
, group
->meth
));
224 if ((mp_cmp(&rx
, &gx
) != 0) || (mp_cmp(&ry
, &gy
) != 0)) {
225 printf(" Error: invalid result (expected (- base point)).\n");
230 /* multiply base point by order - 1 and check for negative of base
232 MP_CHECKOK(ECPoint_mul(group
, &order_1
, &gx
, &gy
, &rx
, &ry
));
234 printf(" (order-1)*P (ECPoint_mul):\n");
235 MP_CHECKOK(mp_toradix(&rx
, s
, 16));
237 MP_CHECKOK(mp_toradix(&ry
, s
, 16));
240 MP_CHECKOK(ec_GF2m_add(&ry
, &rx
, &ry
, group
->meth
));
241 if ((mp_cmp(&rx
, &gx
) != 0) || (mp_cmp(&ry
, &gy
) != 0)) {
242 printf(" Error: invalid result (expected (- base point)).\n");
247 #ifdef ECL_ENABLE_GF2M_PT_MUL_AFF
248 /* multiply base point by order and check for point at infinity */
249 MP_CHECKOK(ec_GF2m_pt_mul_aff
250 (&group
->order
, &group
->genx
, &group
->geny
, &rx
, &ry
,
253 printf(" (order)*P (affine):\n");
254 MP_CHECKOK(mp_toradix(&rx
, s
, 16));
256 MP_CHECKOK(mp_toradix(&ry
, s
, 16));
259 if (ec_GF2m_pt_is_inf_aff(&rx
, &ry
) != MP_YES
) {
260 printf(" Error: invalid result (expected point at infinity).\n");
266 /* multiply base point by order and check for point at infinity */
267 MP_CHECKOK(ec_GF2m_pt_mul_mont
268 (&group
->order
, &group
->genx
, &group
->geny
, &rx
, &ry
,
271 printf(" (order)*P (montgomery):\n");
272 MP_CHECKOK(mp_toradix(&rx
, s
, 16));
274 MP_CHECKOK(mp_toradix(&ry
, s
, 16));
277 if (ec_GF2m_pt_is_inf_aff(&rx
, &ry
) != MP_YES
) {
278 printf(" Error: invalid result (expected point at infinity).\n");
283 #ifdef ECL_ENABLE_GF2M_PROJ
284 /* multiply base point by order and check for point at infinity */
285 MP_CHECKOK(ec_GF2m_pt_mul_proj
286 (&group
->order
, &group
->genx
, &group
->geny
, &rx
, &ry
,
289 printf(" (order)*P (projective):\n");
290 MP_CHECKOK(mp_toradix(&rx
, s
, 16));
292 MP_CHECKOK(mp_toradix(&ry
, s
, 16));
295 if (ec_GF2m_pt_is_inf_aff(&rx
, &ry
) != MP_YES
) {
296 printf(" Error: invalid result (expected point at infinity).\n");
302 /* multiply base point by order and check for point at infinity */
303 MP_CHECKOK(ECPoint_mul(group
, &group
->order
, NULL
, NULL
, &rx
, &ry
));
305 printf(" (order)*P (ECPoint_mul):\n");
306 MP_CHECKOK(mp_toradix(&rx
, s
, 16));
308 MP_CHECKOK(mp_toradix(&ry
, s
, 16));
311 if (ec_GF2m_pt_is_inf_aff(&rx
, &ry
) != MP_YES
) {
312 printf(" Error: invalid result (expected point at infinity).\n");
317 /* multiply base point by order and check for point at infinity */
318 MP_CHECKOK(ECPoint_mul(group
, &group
->order
, &gx
, &gy
, &rx
, &ry
));
320 printf(" (order)*P (ECPoint_mul):\n");
321 MP_CHECKOK(mp_toradix(&rx
, s
, 16));
323 MP_CHECKOK(mp_toradix(&ry
, s
, 16));
326 if (ec_GF2m_pt_is_inf_aff(&rx
, &ry
) != MP_YES
) {
327 printf(" Error: invalid result (expected point at infinity).\n");
332 /* check that (order-1)P + (order-1)P + P == (order-1)P */
333 MP_CHECKOK(ECPoints_mul
334 (group
, &order_1
, &order_1
, &gx
, &gy
, &rx
, &ry
));
335 MP_CHECKOK(ECPoints_mul(group
, &one
, &one
, &rx
, &ry
, &rx
, &ry
));
338 (" (order-1)*P + (order-1)*P + P == (order-1)*P (ECPoints_mul):\n");
339 MP_CHECKOK(mp_toradix(&rx
, s
, 16));
341 MP_CHECKOK(mp_toradix(&ry
, s
, 16));
344 MP_CHECKOK(ec_GF2m_add(&ry
, &rx
, &ry
, group
->meth
));
345 if ((mp_cmp(&rx
, &gx
) != 0) || (mp_cmp(&ry
, &gy
) != 0)) {
346 printf(" Error: invalid result (expected (- base point)).\n");
351 /* test validate_point function */
352 if (ECPoint_validate(group
, &gx
, &gy
) != MP_YES
) {
353 printf(" Error: validate point on base point failed.\n");
357 MP_CHECKOK(mp_add_d(&gy
, 1, &ry
));
358 if (ECPoint_validate(group
, &gx
, &ry
) != MP_NO
) {
359 printf(" Error: validate point on invalid point passed.\n");
365 /* compute random scalar */
366 size
= mpl_significant_bits(&group
->meth
->irr
);
367 if (size
< MP_OKAY
) {
370 MP_CHECKOK(mpp_random_size(&n
, (size
+ ECL_BITS
- 1) / ECL_BITS
));
371 MP_CHECKOK(group
->meth
->field_mod(&n
, &n
, group
->meth
));
374 #ifdef ECL_ENABLE_GF2M_PT_MUL_AFF
375 M_TimeOperation(MP_CHECKOK
377 (&n
, &group
->genx
, &group
->geny
, &rx
, &ry
,
380 M_TimeOperation(MP_CHECKOK
381 (ECPoint_mul(group
, &n
, NULL
, NULL
, &rx
, &ry
)),
383 M_TimeOperation(MP_CHECKOK
385 (group
, &n
, &n
, &gx
, &gy
, &rx
, &ry
)), 100);
387 M_TimeOperation(MP_CHECKOK
388 (ECPoint_mul(group
, &n
, NULL
, NULL
, &rx
, &ry
)),
390 M_TimeOperation(MP_CHECKOK
391 (ECPoint_mul(group
, &n
, &gx
, &gy
, &rx
, &ry
)),
393 M_TimeOperation(MP_CHECKOK
395 (group
, &n
, &n
, &gx
, &gy
, &rx
, &ry
)), 100);
407 if (res
!= MP_OKAY
) {
408 printf(" Error: exiting with error value %i\n", res
);
413 /* Prints help information. */
417 printf("Usage: ecp_test [--print] [--time]\n");
419 (" --print Print out results of each point arithmetic test.\n");
421 (" --time Benchmark point operations and print results.\n");
424 /* Performs tests of elliptic curve cryptography over binary polynomial
425 * fields. If tests fail, then it prints an error message, aborts, and
426 * returns an error code. Otherwise, returns 0. */
428 main(int argv
, char **argc
)
434 ECGroup
*group
= NULL
;
435 ECCurveParams
*params
= NULL
;
438 /* read command-line arguments */
439 for (i
= 1; i
< argv
; i
++) {
440 if ((strcasecmp(argc
[i
], "time") == 0)
441 || (strcasecmp(argc
[i
], "-time") == 0)
442 || (strcasecmp(argc
[i
], "--time") == 0)) {
444 } else if ((strcasecmp(argc
[i
], "print") == 0)
445 || (strcasecmp(argc
[i
], "-print") == 0)
446 || (strcasecmp(argc
[i
], "--print") == 0)) {
454 /* generic arithmetic tests */
455 ECTEST_GENERIC_GF2M("SECT-131R1", ECCurve_SECG_CHAR2_131R1
);
457 /* specific arithmetic tests */
458 ECTEST_NAMED_GF2M("NIST-K163", ECCurve_NIST_K163
);
459 ECTEST_NAMED_GF2M("NIST-B163", ECCurve_NIST_B163
);
460 ECTEST_NAMED_GF2M("NIST-K233", ECCurve_NIST_K233
);
461 ECTEST_NAMED_GF2M("NIST-B233", ECCurve_NIST_B233
);
462 ECTEST_NAMED_GF2M("NIST-K283", ECCurve_NIST_K283
);
463 ECTEST_NAMED_GF2M("NIST-B283", ECCurve_NIST_B283
);
464 ECTEST_NAMED_GF2M("NIST-K409", ECCurve_NIST_K409
);
465 ECTEST_NAMED_GF2M("NIST-B409", ECCurve_NIST_B409
);
466 ECTEST_NAMED_GF2M("NIST-K571", ECCurve_NIST_K571
);
467 ECTEST_NAMED_GF2M("NIST-B571", ECCurve_NIST_B571
);
468 ECTEST_NAMED_GF2M("ANSI X9.62 C2PNB163V1", ECCurve_X9_62_CHAR2_PNB163V1
);
469 ECTEST_NAMED_GF2M("ANSI X9.62 C2PNB163V2", ECCurve_X9_62_CHAR2_PNB163V2
);
470 ECTEST_NAMED_GF2M("ANSI X9.62 C2PNB163V3", ECCurve_X9_62_CHAR2_PNB163V3
);
471 ECTEST_NAMED_GF2M("ANSI X9.62 C2PNB176V1", ECCurve_X9_62_CHAR2_PNB176V1
);
472 ECTEST_NAMED_GF2M("ANSI X9.62 C2TNB191V1", ECCurve_X9_62_CHAR2_TNB191V1
);
473 ECTEST_NAMED_GF2M("ANSI X9.62 C2TNB191V2", ECCurve_X9_62_CHAR2_TNB191V2
);
474 ECTEST_NAMED_GF2M("ANSI X9.62 C2TNB191V3", ECCurve_X9_62_CHAR2_TNB191V3
);
475 ECTEST_NAMED_GF2M("ANSI X9.62 C2PNB208W1", ECCurve_X9_62_CHAR2_PNB208W1
);
476 ECTEST_NAMED_GF2M("ANSI X9.62 C2TNB239V1", ECCurve_X9_62_CHAR2_TNB239V1
);
477 ECTEST_NAMED_GF2M("ANSI X9.62 C2TNB239V2", ECCurve_X9_62_CHAR2_TNB239V2
);
478 ECTEST_NAMED_GF2M("ANSI X9.62 C2TNB239V3", ECCurve_X9_62_CHAR2_TNB239V3
);
479 ECTEST_NAMED_GF2M("ANSI X9.62 C2PNB272W1", ECCurve_X9_62_CHAR2_PNB272W1
);
480 ECTEST_NAMED_GF2M("ANSI X9.62 C2PNB304W1", ECCurve_X9_62_CHAR2_PNB304W1
);
481 ECTEST_NAMED_GF2M("ANSI X9.62 C2TNB359V1", ECCurve_X9_62_CHAR2_TNB359V1
);
482 ECTEST_NAMED_GF2M("ANSI X9.62 C2PNB368W1", ECCurve_X9_62_CHAR2_PNB368W1
);
483 ECTEST_NAMED_GF2M("ANSI X9.62 C2TNB431R1", ECCurve_X9_62_CHAR2_TNB431R1
);
484 ECTEST_NAMED_GF2M("SECT-113R1", ECCurve_SECG_CHAR2_113R1
);
485 ECTEST_NAMED_GF2M("SECT-113R2", ECCurve_SECG_CHAR2_113R2
);
486 ECTEST_NAMED_GF2M("SECT-131R1", ECCurve_SECG_CHAR2_131R1
);
487 ECTEST_NAMED_GF2M("SECT-131R2", ECCurve_SECG_CHAR2_131R2
);
488 ECTEST_NAMED_GF2M("SECT-163K1", ECCurve_SECG_CHAR2_163K1
);
489 ECTEST_NAMED_GF2M("SECT-163R1", ECCurve_SECG_CHAR2_163R1
);
490 ECTEST_NAMED_GF2M("SECT-163R2", ECCurve_SECG_CHAR2_163R2
);
491 ECTEST_NAMED_GF2M("SECT-193R1", ECCurve_SECG_CHAR2_193R1
);
492 ECTEST_NAMED_GF2M("SECT-193R2", ECCurve_SECG_CHAR2_193R2
);
493 ECTEST_NAMED_GF2M("SECT-233K1", ECCurve_SECG_CHAR2_233K1
);
494 ECTEST_NAMED_GF2M("SECT-233R1", ECCurve_SECG_CHAR2_233R1
);
495 ECTEST_NAMED_GF2M("SECT-239K1", ECCurve_SECG_CHAR2_239K1
);
496 ECTEST_NAMED_GF2M("SECT-283K1", ECCurve_SECG_CHAR2_283K1
);
497 ECTEST_NAMED_GF2M("SECT-283R1", ECCurve_SECG_CHAR2_283R1
);
498 ECTEST_NAMED_GF2M("SECT-409K1", ECCurve_SECG_CHAR2_409K1
);
499 ECTEST_NAMED_GF2M("SECT-409R1", ECCurve_SECG_CHAR2_409R1
);
500 ECTEST_NAMED_GF2M("SECT-571K1", ECCurve_SECG_CHAR2_571K1
);
501 ECTEST_NAMED_GF2M("SECT-571R1", ECCurve_SECG_CHAR2_571R1
);
502 ECTEST_NAMED_GF2M("WTLS-1 (113)", ECCurve_WTLS_1
);
503 ECTEST_NAMED_GF2M("WTLS-3 (163)", ECCurve_WTLS_3
);
504 ECTEST_NAMED_GF2M("WTLS-4 (113)", ECCurve_WTLS_4
);
505 ECTEST_NAMED_GF2M("WTLS-5 (163)", ECCurve_WTLS_5
);
506 ECTEST_NAMED_GF2M("WTLS-10 (233)", ECCurve_WTLS_10
);
507 ECTEST_NAMED_GF2M("WTLS-11 (233)", ECCurve_WTLS_11
);
510 EC_FreeCurveParams(params
);
512 if (res
!= MP_OKAY
) {
513 printf("Error: exiting with error value %i\n", res
);