1*5697Smcpowers /*
2*5697Smcpowers * ***** BEGIN LICENSE BLOCK *****
3*5697Smcpowers * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4*5697Smcpowers *
5*5697Smcpowers * The contents of this file are subject to the Mozilla Public License Version
6*5697Smcpowers * 1.1 (the "License"); you may not use this file except in compliance with
7*5697Smcpowers * the License. You may obtain a copy of the License at
8*5697Smcpowers * http://www.mozilla.org/MPL/
9*5697Smcpowers *
10*5697Smcpowers * Software distributed under the License is distributed on an "AS IS" basis,
11*5697Smcpowers * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12*5697Smcpowers * for the specific language governing rights and limitations under the
13*5697Smcpowers * License.
14*5697Smcpowers *
15*5697Smcpowers * The Original Code is the elliptic curve math library for prime field curves.
16*5697Smcpowers *
17*5697Smcpowers * The Initial Developer of the Original Code is
18*5697Smcpowers * Sun Microsystems, Inc.
19*5697Smcpowers * Portions created by the Initial Developer are Copyright (C) 2003
20*5697Smcpowers * the Initial Developer. All Rights Reserved.
21*5697Smcpowers *
22*5697Smcpowers * Contributor(s):
23*5697Smcpowers * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
24*5697Smcpowers *
25*5697Smcpowers * Alternatively, the contents of this file may be used under the terms of
26*5697Smcpowers * either the GNU General Public License Version 2 or later (the "GPL"), or
27*5697Smcpowers * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28*5697Smcpowers * in which case the provisions of the GPL or the LGPL are applicable instead
29*5697Smcpowers * of those above. If you wish to allow use of your version of this file only
30*5697Smcpowers * under the terms of either the GPL or the LGPL, and not to allow others to
31*5697Smcpowers * use your version of this file under the terms of the MPL, indicate your
32*5697Smcpowers * decision by deleting the provisions above and replace them with the notice
33*5697Smcpowers * and other provisions required by the GPL or the LGPL. If you do not delete
34*5697Smcpowers * the provisions above, a recipient may use your version of this file under
35*5697Smcpowers * the terms of any one of the MPL, the GPL or the LGPL.
36*5697Smcpowers *
37*5697Smcpowers * ***** END LICENSE BLOCK ***** */
38*5697Smcpowers /*
39*5697Smcpowers * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
40*5697Smcpowers * Use is subject to license terms.
41*5697Smcpowers *
42*5697Smcpowers * Sun elects to use this software under the MPL license.
43*5697Smcpowers */
44*5697Smcpowers
45*5697Smcpowers #pragma ident "%Z%%M% %I% %E% SMI"
46*5697Smcpowers
47*5697Smcpowers #ifdef _KERNEL
48*5697Smcpowers #include <sys/types.h>
49*5697Smcpowers #include <sys/systm.h>
50*5697Smcpowers #include <sys/param.h>
51*5697Smcpowers #include <sys/modctl.h>
52*5697Smcpowers #include <sys/ddi.h>
53*5697Smcpowers #include <sys/crypto/spi.h>
54*5697Smcpowers #include <sys/sysmacros.h>
55*5697Smcpowers #include <sys/strsun.h>
56*5697Smcpowers #include <sys/md5.h>
57*5697Smcpowers #include <sys/sha1.h>
58*5697Smcpowers #include <sys/sha2.h>
59*5697Smcpowers #include <sys/random.h>
60*5697Smcpowers #include <sys/conf.h>
61*5697Smcpowers #include <sys/devops.h>
62*5697Smcpowers #include <sys/sunddi.h>
63*5697Smcpowers #include <sys/varargs.h>
64*5697Smcpowers #include <sys/kmem.h>
65*5697Smcpowers #include <sys/kstat.h>
66*5697Smcpowers #include <sys/crypto/common.h>
67*5697Smcpowers #else
68*5697Smcpowers #include <stdio.h>
69*5697Smcpowers #include <string.h>
70*5697Smcpowers #include <strings.h>
71*5697Smcpowers #include <assert.h>
72*5697Smcpowers #include <time.h>
73*5697Smcpowers #include <sys/time.h>
74*5697Smcpowers #include <sys/resource.h>
75*5697Smcpowers #endif /* _KERNEL */
76*5697Smcpowers
77*5697Smcpowers #include "mpi.h"
78*5697Smcpowers #include "mplogic.h"
79*5697Smcpowers #include "mpprime.h"
80*5697Smcpowers #include "ecl.h"
81*5697Smcpowers #include "ecl-curve.h"
82*5697Smcpowers #include "ecp.h"
83*5697Smcpowers #include "ecc_impl.h"
84*5697Smcpowers #include "ec.h"
85*5697Smcpowers
86*5697Smcpowers #ifndef KM_SLEEP
87*5697Smcpowers #define KM_SLEEP 0
88*5697Smcpowers #endif
89*5697Smcpowers
90*5697Smcpowers #ifndef _KERNEL
91*5697Smcpowers /* Time k repetitions of operation op. */
92*5697Smcpowers #define M_TimeOperation(op, k) { \
93*5697Smcpowers double dStart, dNow, dUserTime; \
94*5697Smcpowers struct rusage ru; \
95*5697Smcpowers int i; \
96*5697Smcpowers getrusage(RUSAGE_SELF, &ru); \
97*5697Smcpowers dStart = (double)ru.ru_utime.tv_sec+(double)ru.ru_utime.tv_usec*0.000001; \
98*5697Smcpowers for (i = 0; i < k; i++) { \
99*5697Smcpowers { op; } \
100*5697Smcpowers }; \
101*5697Smcpowers getrusage(RUSAGE_SELF, &ru); \
102*5697Smcpowers dNow = (double)ru.ru_utime.tv_sec+(double)ru.ru_utime.tv_usec*0.000001; \
103*5697Smcpowers dUserTime = dNow-dStart; \
104*5697Smcpowers if (dUserTime) printf(" %-45s k: %6i, t: %6.2f sec\n", #op, k, dUserTime); \
105*5697Smcpowers }
106*5697Smcpowers #else
107*5697Smcpowers #define M_TimeOperation(op, k)
108*5697Smcpowers #endif
109*5697Smcpowers
110*5697Smcpowers /* Test curve using generic field arithmetic. */
111*5697Smcpowers #define ECTEST_GENERIC_GFP(name_c, name) \
112*5697Smcpowers printf("Testing %s using generic implementation...\n", name_c); \
113*5697Smcpowers params = EC_GetNamedCurveParams(name, KM_SLEEP); \
114*5697Smcpowers if (params == NULL) { \
115*5697Smcpowers printf(" Error: could not construct params.\n"); \
116*5697Smcpowers res = MP_NO; \
117*5697Smcpowers goto CLEANUP; \
118*5697Smcpowers } \
119*5697Smcpowers ECGroup_free(group); \
120*5697Smcpowers group = ECGroup_fromHex(params, KM_SLEEP); \
121*5697Smcpowers if (group == NULL) { \
122*5697Smcpowers printf(" Error: could not construct group.\n"); \
123*5697Smcpowers res = MP_NO; \
124*5697Smcpowers goto CLEANUP; \
125*5697Smcpowers } \
126*5697Smcpowers MP_CHECKOK( ectest_curve_GFp(group, ectestPrint, ectestTime, 1, KM_SLEEP) ); \
127*5697Smcpowers printf("... okay.\n");
128*5697Smcpowers
129*5697Smcpowers /* Test curve using specific field arithmetic. */
130*5697Smcpowers #define ECTEST_NAMED_GFP(name_c, name) \
131*5697Smcpowers printf("Testing %s using specific implementation...\n", name_c); \
132*5697Smcpowers ECGroup_free(group); \
133*5697Smcpowers group = ECGroup_fromName(name, KM_SLEEP); \
134*5697Smcpowers if (group == NULL) { \
135*5697Smcpowers printf(" Warning: could not construct group.\n"); \
136*5697Smcpowers printf("... failed; continuing with remaining tests.\n"); \
137*5697Smcpowers } else { \
138*5697Smcpowers MP_CHECKOK( ectest_curve_GFp(group, ectestPrint, ectestTime, 0, KM_SLEEP) ); \
139*5697Smcpowers printf("... okay.\n"); \
140*5697Smcpowers }
141*5697Smcpowers
142*5697Smcpowers /* Performs basic tests of elliptic curve cryptography over prime fields.
143*5697Smcpowers * If tests fail, then it prints an error message, aborts, and returns an
144*5697Smcpowers * error code. Otherwise, returns 0. */
145*5697Smcpowers int
ectest_curve_GFp(ECGroup * group,int ectestPrint,int ectestTime,int generic,int kmflag)146*5697Smcpowers ectest_curve_GFp(ECGroup *group, int ectestPrint, int ectestTime,
147*5697Smcpowers int generic, int kmflag)
148*5697Smcpowers {
149*5697Smcpowers
150*5697Smcpowers mp_int one, order_1, gx, gy, rx, ry, n;
151*5697Smcpowers int size;
152*5697Smcpowers mp_err res;
153*5697Smcpowers char s[1000];
154*5697Smcpowers
155*5697Smcpowers /* initialize values */
156*5697Smcpowers MP_CHECKOK(mp_init(&one, kmflag));
157*5697Smcpowers MP_CHECKOK(mp_init(&order_1, kmflag));
158*5697Smcpowers MP_CHECKOK(mp_init(&gx, kmflag));
159*5697Smcpowers MP_CHECKOK(mp_init(&gy, kmflag));
160*5697Smcpowers MP_CHECKOK(mp_init(&rx, kmflag));
161*5697Smcpowers MP_CHECKOK(mp_init(&ry, kmflag));
162*5697Smcpowers MP_CHECKOK(mp_init(&n, kmflag));
163*5697Smcpowers
164*5697Smcpowers MP_CHECKOK(mp_set_int(&one, 1));
165*5697Smcpowers MP_CHECKOK(mp_sub(&group->order, &one, &order_1));
166*5697Smcpowers
167*5697Smcpowers /* encode base point */
168*5697Smcpowers if (group->meth->field_dec) {
169*5697Smcpowers MP_CHECKOK(group->meth->field_dec(&group->genx, &gx, group->meth));
170*5697Smcpowers MP_CHECKOK(group->meth->field_dec(&group->geny, &gy, group->meth));
171*5697Smcpowers } else {
172*5697Smcpowers MP_CHECKOK(mp_copy(&group->genx, &gx));
173*5697Smcpowers MP_CHECKOK(mp_copy(&group->geny, &gy));
174*5697Smcpowers }
175*5697Smcpowers if (ectestPrint) {
176*5697Smcpowers /* output base point */
177*5697Smcpowers printf(" base point P:\n");
178*5697Smcpowers MP_CHECKOK(mp_toradix(&gx, s, 16));
179*5697Smcpowers printf(" %s\n", s);
180*5697Smcpowers MP_CHECKOK(mp_toradix(&gy, s, 16));
181*5697Smcpowers printf(" %s\n", s);
182*5697Smcpowers if (group->meth->field_enc) {
183*5697Smcpowers printf(" base point P (encoded):\n");
184*5697Smcpowers MP_CHECKOK(mp_toradix(&group->genx, s, 16));
185*5697Smcpowers printf(" %s\n", s);
186*5697Smcpowers MP_CHECKOK(mp_toradix(&group->geny, s, 16));
187*5697Smcpowers printf(" %s\n", s);
188*5697Smcpowers }
189*5697Smcpowers }
190*5697Smcpowers
191*5697Smcpowers #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
192*5697Smcpowers /* multiply base point by order - 1 and check for negative of base
193*5697Smcpowers * point */
194*5697Smcpowers MP_CHECKOK(ec_GFp_pt_mul_aff
195*5697Smcpowers (&order_1, &group->genx, &group->geny, &rx, &ry, group));
196*5697Smcpowers if (ectestPrint) {
197*5697Smcpowers printf(" (order-1)*P (affine):\n");
198*5697Smcpowers MP_CHECKOK(mp_toradix(&rx, s, 16));
199*5697Smcpowers printf(" %s\n", s);
200*5697Smcpowers MP_CHECKOK(mp_toradix(&ry, s, 16));
201*5697Smcpowers printf(" %s\n", s);
202*5697Smcpowers }
203*5697Smcpowers MP_CHECKOK(group->meth->field_neg(&ry, &ry, group->meth));
204*5697Smcpowers if ((mp_cmp(&rx, &group->genx) != 0)
205*5697Smcpowers || (mp_cmp(&ry, &group->geny) != 0)) {
206*5697Smcpowers printf(" Error: invalid result (expected (- base point)).\n");
207*5697Smcpowers res = MP_NO;
208*5697Smcpowers goto CLEANUP;
209*5697Smcpowers }
210*5697Smcpowers #endif
211*5697Smcpowers
212*5697Smcpowers #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
213*5697Smcpowers /* multiply base point by order - 1 and check for negative of base
214*5697Smcpowers * point */
215*5697Smcpowers MP_CHECKOK(ec_GFp_pt_mul_jac
216*5697Smcpowers (&order_1, &group->genx, &group->geny, &rx, &ry, group));
217*5697Smcpowers if (ectestPrint) {
218*5697Smcpowers printf(" (order-1)*P (jacobian):\n");
219*5697Smcpowers MP_CHECKOK(mp_toradix(&rx, s, 16));
220*5697Smcpowers printf(" %s\n", s);
221*5697Smcpowers MP_CHECKOK(mp_toradix(&ry, s, 16));
222*5697Smcpowers printf(" %s\n", s);
223*5697Smcpowers }
224*5697Smcpowers MP_CHECKOK(group->meth->field_neg(&ry, &ry, group->meth));
225*5697Smcpowers if ((mp_cmp(&rx, &group->genx) != 0)
226*5697Smcpowers || (mp_cmp(&ry, &group->geny) != 0)) {
227*5697Smcpowers printf(" Error: invalid result (expected (- base point)).\n");
228*5697Smcpowers res = MP_NO;
229*5697Smcpowers goto CLEANUP;
230*5697Smcpowers }
231*5697Smcpowers #endif
232*5697Smcpowers
233*5697Smcpowers /* multiply base point by order - 1 and check for negative of base
234*5697Smcpowers * point */
235*5697Smcpowers MP_CHECKOK(ECPoint_mul(group, &order_1, NULL, NULL, &rx, &ry));
236*5697Smcpowers if (ectestPrint) {
237*5697Smcpowers printf(" (order-1)*P (ECPoint_mul):\n");
238*5697Smcpowers MP_CHECKOK(mp_toradix(&rx, s, 16));
239*5697Smcpowers printf(" %s\n", s);
240*5697Smcpowers MP_CHECKOK(mp_toradix(&ry, s, 16));
241*5697Smcpowers printf(" %s\n", s);
242*5697Smcpowers }
243*5697Smcpowers MP_CHECKOK(mp_submod(&group->meth->irr, &ry, &group->meth->irr, &ry));
244*5697Smcpowers if ((mp_cmp(&rx, &gx) != 0) || (mp_cmp(&ry, &gy) != 0)) {
245*5697Smcpowers printf(" Error: invalid result (expected (- base point)).\n");
246*5697Smcpowers res = MP_NO;
247*5697Smcpowers goto CLEANUP;
248*5697Smcpowers }
249*5697Smcpowers
250*5697Smcpowers /* multiply base point by order - 1 and check for negative of base
251*5697Smcpowers * point */
252*5697Smcpowers MP_CHECKOK(ECPoint_mul(group, &order_1, &gx, &gy, &rx, &ry));
253*5697Smcpowers if (ectestPrint) {
254*5697Smcpowers printf(" (order-1)*P (ECPoint_mul):\n");
255*5697Smcpowers MP_CHECKOK(mp_toradix(&rx, s, 16));
256*5697Smcpowers printf(" %s\n", s);
257*5697Smcpowers MP_CHECKOK(mp_toradix(&ry, s, 16));
258*5697Smcpowers printf(" %s\n", s);
259*5697Smcpowers }
260*5697Smcpowers MP_CHECKOK(mp_submod(&group->meth->irr, &ry, &group->meth->irr, &ry));
261*5697Smcpowers if ((mp_cmp(&rx, &gx) != 0) || (mp_cmp(&ry, &gy) != 0)) {
262*5697Smcpowers printf(" Error: invalid result (expected (- base point)).\n");
263*5697Smcpowers res = MP_NO;
264*5697Smcpowers goto CLEANUP;
265*5697Smcpowers }
266*5697Smcpowers
267*5697Smcpowers #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
268*5697Smcpowers /* multiply base point by order and check for point at infinity */
269*5697Smcpowers MP_CHECKOK(ec_GFp_pt_mul_aff
270*5697Smcpowers (&group->order, &group->genx, &group->geny, &rx, &ry,
271*5697Smcpowers group));
272*5697Smcpowers if (ectestPrint) {
273*5697Smcpowers printf(" (order)*P (affine):\n");
274*5697Smcpowers MP_CHECKOK(mp_toradix(&rx, s, 16));
275*5697Smcpowers printf(" %s\n", s);
276*5697Smcpowers MP_CHECKOK(mp_toradix(&ry, s, 16));
277*5697Smcpowers printf(" %s\n", s);
278*5697Smcpowers }
279*5697Smcpowers if (ec_GFp_pt_is_inf_aff(&rx, &ry) != MP_YES) {
280*5697Smcpowers printf(" Error: invalid result (expected point at infinity).\n");
281*5697Smcpowers res = MP_NO;
282*5697Smcpowers goto CLEANUP;
283*5697Smcpowers }
284*5697Smcpowers #endif
285*5697Smcpowers
286*5697Smcpowers #ifdef ECL_ENABLE_GFP_PT_MUL_JAC
287*5697Smcpowers /* multiply base point by order and check for point at infinity */
288*5697Smcpowers MP_CHECKOK(ec_GFp_pt_mul_jac
289*5697Smcpowers (&group->order, &group->genx, &group->geny, &rx, &ry,
290*5697Smcpowers group));
291*5697Smcpowers if (ectestPrint) {
292*5697Smcpowers printf(" (order)*P (jacobian):\n");
293*5697Smcpowers MP_CHECKOK(mp_toradix(&rx, s, 16));
294*5697Smcpowers printf(" %s\n", s);
295*5697Smcpowers MP_CHECKOK(mp_toradix(&ry, s, 16));
296*5697Smcpowers printf(" %s\n", s);
297*5697Smcpowers }
298*5697Smcpowers if (ec_GFp_pt_is_inf_aff(&rx, &ry) != MP_YES) {
299*5697Smcpowers printf(" Error: invalid result (expected point at infinity).\n");
300*5697Smcpowers res = MP_NO;
301*5697Smcpowers goto CLEANUP;
302*5697Smcpowers }
303*5697Smcpowers #endif
304*5697Smcpowers
305*5697Smcpowers /* multiply base point by order and check for point at infinity */
306*5697Smcpowers MP_CHECKOK(ECPoint_mul(group, &group->order, NULL, NULL, &rx, &ry));
307*5697Smcpowers if (ectestPrint) {
308*5697Smcpowers printf(" (order)*P (ECPoint_mul):\n");
309*5697Smcpowers MP_CHECKOK(mp_toradix(&rx, s, 16));
310*5697Smcpowers printf(" %s\n", s);
311*5697Smcpowers MP_CHECKOK(mp_toradix(&ry, s, 16));
312*5697Smcpowers printf(" %s\n", s);
313*5697Smcpowers }
314*5697Smcpowers if (ec_GFp_pt_is_inf_aff(&rx, &ry) != MP_YES) {
315*5697Smcpowers printf(" Error: invalid result (expected point at infinity).\n");
316*5697Smcpowers res = MP_NO;
317*5697Smcpowers goto CLEANUP;
318*5697Smcpowers }
319*5697Smcpowers
320*5697Smcpowers /* multiply base point by order and check for point at infinity */
321*5697Smcpowers MP_CHECKOK(ECPoint_mul(group, &group->order, &gx, &gy, &rx, &ry));
322*5697Smcpowers if (ectestPrint) {
323*5697Smcpowers printf(" (order)*P (ECPoint_mul):\n");
324*5697Smcpowers MP_CHECKOK(mp_toradix(&rx, s, 16));
325*5697Smcpowers printf(" %s\n", s);
326*5697Smcpowers MP_CHECKOK(mp_toradix(&ry, s, 16));
327*5697Smcpowers printf(" %s\n", s);
328*5697Smcpowers }
329*5697Smcpowers if (ec_GFp_pt_is_inf_aff(&rx, &ry) != MP_YES) {
330*5697Smcpowers printf(" Error: invalid result (expected point at infinity).\n");
331*5697Smcpowers res = MP_NO;
332*5697Smcpowers goto CLEANUP;
333*5697Smcpowers }
334*5697Smcpowers
335*5697Smcpowers /* check that (order-1)P + (order-1)P + P == (order-1)P */
336*5697Smcpowers MP_CHECKOK(ECPoints_mul
337*5697Smcpowers (group, &order_1, &order_1, &gx, &gy, &rx, &ry));
338*5697Smcpowers MP_CHECKOK(ECPoints_mul(group, &one, &one, &rx, &ry, &rx, &ry));
339*5697Smcpowers if (ectestPrint) {
340*5697Smcpowers printf
341*5697Smcpowers (" (order-1)*P + (order-1)*P + P == (order-1)*P (ECPoints_mul):\n");
342*5697Smcpowers MP_CHECKOK(mp_toradix(&rx, s, 16));
343*5697Smcpowers printf(" %s\n", s);
344*5697Smcpowers MP_CHECKOK(mp_toradix(&ry, s, 16));
345*5697Smcpowers printf(" %s\n", s);
346*5697Smcpowers }
347*5697Smcpowers MP_CHECKOK(mp_submod(&group->meth->irr, &ry, &group->meth->irr, &ry));
348*5697Smcpowers if ((mp_cmp(&rx, &gx) != 0) || (mp_cmp(&ry, &gy) != 0)) {
349*5697Smcpowers printf(" Error: invalid result (expected (- base point)).\n");
350*5697Smcpowers res = MP_NO;
351*5697Smcpowers goto CLEANUP;
352*5697Smcpowers }
353*5697Smcpowers
354*5697Smcpowers /* test validate_point function */
355*5697Smcpowers if (ECPoint_validate(group, &gx, &gy) != MP_YES) {
356*5697Smcpowers printf(" Error: validate point on base point failed.\n");
357*5697Smcpowers res = MP_NO;
358*5697Smcpowers goto CLEANUP;
359*5697Smcpowers }
360*5697Smcpowers MP_CHECKOK(mp_add_d(&gy, 1, &ry));
361*5697Smcpowers if (ECPoint_validate(group, &gx, &ry) != MP_NO) {
362*5697Smcpowers printf(" Error: validate point on invalid point passed.\n");
363*5697Smcpowers res = MP_NO;
364*5697Smcpowers goto CLEANUP;
365*5697Smcpowers }
366*5697Smcpowers
367*5697Smcpowers if (ectestTime) {
368*5697Smcpowers /* compute random scalar */
369*5697Smcpowers size = mpl_significant_bits(&group->meth->irr);
370*5697Smcpowers if (size < MP_OKAY) {
371*5697Smcpowers goto CLEANUP;
372*5697Smcpowers }
373*5697Smcpowers MP_CHECKOK(mpp_random_size(&n, (size + ECL_BITS - 1) / ECL_BITS));
374*5697Smcpowers MP_CHECKOK(group->meth->field_mod(&n, &n, group->meth));
375*5697Smcpowers /* timed test */
376*5697Smcpowers if (generic) {
377*5697Smcpowers #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
378*5697Smcpowers M_TimeOperation(MP_CHECKOK
379*5697Smcpowers (ec_GFp_pt_mul_aff
380*5697Smcpowers (&n, &group->genx, &group->geny, &rx, &ry,
381*5697Smcpowers group)), 100);
382*5697Smcpowers #endif
383*5697Smcpowers M_TimeOperation(MP_CHECKOK
384*5697Smcpowers (ECPoint_mul(group, &n, NULL, NULL, &rx, &ry)),
385*5697Smcpowers 100);
386*5697Smcpowers M_TimeOperation(MP_CHECKOK
387*5697Smcpowers (ECPoints_mul
388*5697Smcpowers (group, &n, &n, &gx, &gy, &rx, &ry)), 100);
389*5697Smcpowers } else {
390*5697Smcpowers M_TimeOperation(MP_CHECKOK
391*5697Smcpowers (ECPoint_mul(group, &n, NULL, NULL, &rx, &ry)),
392*5697Smcpowers 100);
393*5697Smcpowers M_TimeOperation(MP_CHECKOK
394*5697Smcpowers (ECPoint_mul(group, &n, &gx, &gy, &rx, &ry)),
395*5697Smcpowers 100);
396*5697Smcpowers M_TimeOperation(MP_CHECKOK
397*5697Smcpowers (ECPoints_mul
398*5697Smcpowers (group, &n, &n, &gx, &gy, &rx, &ry)), 100);
399*5697Smcpowers }
400*5697Smcpowers }
401*5697Smcpowers
402*5697Smcpowers CLEANUP:
403*5697Smcpowers mp_clear(&one);
404*5697Smcpowers mp_clear(&order_1);
405*5697Smcpowers mp_clear(&gx);
406*5697Smcpowers mp_clear(&gy);
407*5697Smcpowers mp_clear(&rx);
408*5697Smcpowers mp_clear(&ry);
409*5697Smcpowers mp_clear(&n);
410*5697Smcpowers if (res != MP_OKAY) {
411*5697Smcpowers #ifdef _KERNEL
412*5697Smcpowers printf(" Error: exiting with error value 0x%x\n", res);
413*5697Smcpowers #else
414*5697Smcpowers printf(" Error: exiting with error value %i\n", res);
415*5697Smcpowers #endif
416*5697Smcpowers }
417*5697Smcpowers return res;
418*5697Smcpowers }
419*5697Smcpowers
420*5697Smcpowers /* Performs tests of elliptic curve cryptography over prime fields If
421*5697Smcpowers * tests fail, then it prints an error message, aborts, and returns an
422*5697Smcpowers * error code. Otherwise, returns 0. */
423*5697Smcpowers int
ecp_test()424*5697Smcpowers ecp_test()
425*5697Smcpowers {
426*5697Smcpowers
427*5697Smcpowers int ectestTime = 0;
428*5697Smcpowers int ectestPrint = 0;
429*5697Smcpowers int i;
430*5697Smcpowers ECGroup *group = NULL;
431*5697Smcpowers ECCurveParams *params = NULL;
432*5697Smcpowers mp_err res;
433*5697Smcpowers
434*5697Smcpowers /* generic arithmetic tests */
435*5697Smcpowers ECTEST_GENERIC_GFP("SECP-160R1", ECCurve_SECG_PRIME_160R1);
436*5697Smcpowers
437*5697Smcpowers /* specific arithmetic tests */
438*5697Smcpowers ECTEST_NAMED_GFP("NIST-P192", ECCurve_NIST_P192);
439*5697Smcpowers ECTEST_NAMED_GFP("NIST-P224", ECCurve_NIST_P224);
440*5697Smcpowers ECTEST_NAMED_GFP("NIST-P256", ECCurve_NIST_P256);
441*5697Smcpowers ECTEST_NAMED_GFP("NIST-P384", ECCurve_NIST_P384);
442*5697Smcpowers ECTEST_NAMED_GFP("NIST-P521", ECCurve_NIST_P521);
443*5697Smcpowers ECTEST_NAMED_GFP("ANSI X9.62 PRIME192v1", ECCurve_X9_62_PRIME_192V1);
444*5697Smcpowers ECTEST_NAMED_GFP("ANSI X9.62 PRIME192v2", ECCurve_X9_62_PRIME_192V2);
445*5697Smcpowers ECTEST_NAMED_GFP("ANSI X9.62 PRIME192v3", ECCurve_X9_62_PRIME_192V3);
446*5697Smcpowers ECTEST_NAMED_GFP("ANSI X9.62 PRIME239v1", ECCurve_X9_62_PRIME_239V1);
447*5697Smcpowers ECTEST_NAMED_GFP("ANSI X9.62 PRIME239v2", ECCurve_X9_62_PRIME_239V2);
448*5697Smcpowers ECTEST_NAMED_GFP("ANSI X9.62 PRIME239v3", ECCurve_X9_62_PRIME_239V3);
449*5697Smcpowers ECTEST_NAMED_GFP("ANSI X9.62 PRIME256v1", ECCurve_X9_62_PRIME_256V1);
450*5697Smcpowers ECTEST_NAMED_GFP("SECP-112R1", ECCurve_SECG_PRIME_112R1);
451*5697Smcpowers ECTEST_NAMED_GFP("SECP-112R2", ECCurve_SECG_PRIME_112R2);
452*5697Smcpowers ECTEST_NAMED_GFP("SECP-128R1", ECCurve_SECG_PRIME_128R1);
453*5697Smcpowers ECTEST_NAMED_GFP("SECP-128R2", ECCurve_SECG_PRIME_128R2);
454*5697Smcpowers ECTEST_NAMED_GFP("SECP-160K1", ECCurve_SECG_PRIME_160K1);
455*5697Smcpowers ECTEST_NAMED_GFP("SECP-160R1", ECCurve_SECG_PRIME_160R1);
456*5697Smcpowers ECTEST_NAMED_GFP("SECP-160R2", ECCurve_SECG_PRIME_160R2);
457*5697Smcpowers ECTEST_NAMED_GFP("SECP-192K1", ECCurve_SECG_PRIME_192K1);
458*5697Smcpowers ECTEST_NAMED_GFP("SECP-192R1", ECCurve_SECG_PRIME_192R1);
459*5697Smcpowers ECTEST_NAMED_GFP("SECP-224K1", ECCurve_SECG_PRIME_224K1);
460*5697Smcpowers ECTEST_NAMED_GFP("SECP-224R1", ECCurve_SECG_PRIME_224R1);
461*5697Smcpowers ECTEST_NAMED_GFP("SECP-256K1", ECCurve_SECG_PRIME_256K1);
462*5697Smcpowers ECTEST_NAMED_GFP("SECP-256R1", ECCurve_SECG_PRIME_256R1);
463*5697Smcpowers ECTEST_NAMED_GFP("SECP-384R1", ECCurve_SECG_PRIME_384R1);
464*5697Smcpowers ECTEST_NAMED_GFP("SECP-521R1", ECCurve_SECG_PRIME_521R1);
465*5697Smcpowers ECTEST_NAMED_GFP("WTLS-6 (112)", ECCurve_WTLS_6);
466*5697Smcpowers ECTEST_NAMED_GFP("WTLS-7 (160)", ECCurve_WTLS_7);
467*5697Smcpowers ECTEST_NAMED_GFP("WTLS-8 (112)", ECCurve_WTLS_8);
468*5697Smcpowers ECTEST_NAMED_GFP("WTLS-9 (160)", ECCurve_WTLS_9);
469*5697Smcpowers ECTEST_NAMED_GFP("WTLS-12 (224)", ECCurve_WTLS_12);
470*5697Smcpowers
471*5697Smcpowers CLEANUP:
472*5697Smcpowers EC_FreeCurveParams(params);
473*5697Smcpowers ECGroup_free(group);
474*5697Smcpowers if (res != MP_OKAY) {
475*5697Smcpowers #ifdef _KERNEL
476*5697Smcpowers printf("Error: exiting with error value 0x%x\n", res);
477*5697Smcpowers #else
478*5697Smcpowers printf("Error: exiting with error value %i\n", res);
479*5697Smcpowers #endif
480*5697Smcpowers }
481*5697Smcpowers return res;
482*5697Smcpowers }
483