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.
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 #include "ecl.h"
48*5697Smcpowers #include "ecl-curve.h"
49*5697Smcpowers #include "ecl-priv.h"
50*5697Smcpowers #ifndef _KERNEL
51*5697Smcpowers #include <stdlib.h>
52*5697Smcpowers #include <string.h>
53*5697Smcpowers #endif
54*5697Smcpowers
55*5697Smcpowers #define CHECK(func) if ((func) == NULL) { res = 0; goto CLEANUP; }
56*5697Smcpowers
57*5697Smcpowers /* Duplicates an ECCurveParams */
58*5697Smcpowers ECCurveParams *
ECCurveParams_dup(const ECCurveParams * params,int kmflag)59*5697Smcpowers ECCurveParams_dup(const ECCurveParams * params, int kmflag)
60*5697Smcpowers {
61*5697Smcpowers int res = 1;
62*5697Smcpowers ECCurveParams *ret = NULL;
63*5697Smcpowers
64*5697Smcpowers #ifdef _KERNEL
65*5697Smcpowers ret = (ECCurveParams *) kmem_zalloc(sizeof(ECCurveParams), kmflag);
66*5697Smcpowers #else
67*5697Smcpowers CHECK(ret = (ECCurveParams *) calloc(1, sizeof(ECCurveParams)));
68*5697Smcpowers #endif
69*5697Smcpowers if (params->text != NULL) {
70*5697Smcpowers #ifdef _KERNEL
71*5697Smcpowers ret->text = kmem_alloc(strlen(params->text) + 1, kmflag);
72*5697Smcpowers bcopy(params->text, ret->text, strlen(params->text) + 1);
73*5697Smcpowers #else
74*5697Smcpowers CHECK(ret->text = strdup(params->text));
75*5697Smcpowers #endif
76*5697Smcpowers }
77*5697Smcpowers ret->field = params->field;
78*5697Smcpowers ret->size = params->size;
79*5697Smcpowers if (params->irr != NULL) {
80*5697Smcpowers #ifdef _KERNEL
81*5697Smcpowers ret->irr = kmem_alloc(strlen(params->irr) + 1, kmflag);
82*5697Smcpowers bcopy(params->irr, ret->irr, strlen(params->irr) + 1);
83*5697Smcpowers #else
84*5697Smcpowers CHECK(ret->irr = strdup(params->irr));
85*5697Smcpowers #endif
86*5697Smcpowers }
87*5697Smcpowers if (params->curvea != NULL) {
88*5697Smcpowers #ifdef _KERNEL
89*5697Smcpowers ret->curvea = kmem_alloc(strlen(params->curvea) + 1, kmflag);
90*5697Smcpowers bcopy(params->curvea, ret->curvea, strlen(params->curvea) + 1);
91*5697Smcpowers #else
92*5697Smcpowers CHECK(ret->curvea = strdup(params->curvea));
93*5697Smcpowers #endif
94*5697Smcpowers }
95*5697Smcpowers if (params->curveb != NULL) {
96*5697Smcpowers #ifdef _KERNEL
97*5697Smcpowers ret->curveb = kmem_alloc(strlen(params->curveb) + 1, kmflag);
98*5697Smcpowers bcopy(params->curveb, ret->curveb, strlen(params->curveb) + 1);
99*5697Smcpowers #else
100*5697Smcpowers CHECK(ret->curveb = strdup(params->curveb));
101*5697Smcpowers #endif
102*5697Smcpowers }
103*5697Smcpowers if (params->genx != NULL) {
104*5697Smcpowers #ifdef _KERNEL
105*5697Smcpowers ret->genx = kmem_alloc(strlen(params->genx) + 1, kmflag);
106*5697Smcpowers bcopy(params->genx, ret->genx, strlen(params->genx) + 1);
107*5697Smcpowers #else
108*5697Smcpowers CHECK(ret->genx = strdup(params->genx));
109*5697Smcpowers #endif
110*5697Smcpowers }
111*5697Smcpowers if (params->geny != NULL) {
112*5697Smcpowers #ifdef _KERNEL
113*5697Smcpowers ret->geny = kmem_alloc(strlen(params->geny) + 1, kmflag);
114*5697Smcpowers bcopy(params->geny, ret->geny, strlen(params->geny) + 1);
115*5697Smcpowers #else
116*5697Smcpowers CHECK(ret->geny = strdup(params->geny));
117*5697Smcpowers #endif
118*5697Smcpowers }
119*5697Smcpowers if (params->order != NULL) {
120*5697Smcpowers #ifdef _KERNEL
121*5697Smcpowers ret->order = kmem_alloc(strlen(params->order) + 1, kmflag);
122*5697Smcpowers bcopy(params->order, ret->order, strlen(params->order) + 1);
123*5697Smcpowers #else
124*5697Smcpowers CHECK(ret->order = strdup(params->order));
125*5697Smcpowers #endif
126*5697Smcpowers }
127*5697Smcpowers ret->cofactor = params->cofactor;
128*5697Smcpowers
129*5697Smcpowers CLEANUP:
130*5697Smcpowers if (res != 1) {
131*5697Smcpowers EC_FreeCurveParams(ret);
132*5697Smcpowers return NULL;
133*5697Smcpowers }
134*5697Smcpowers return ret;
135*5697Smcpowers }
136*5697Smcpowers
137*5697Smcpowers #undef CHECK
138*5697Smcpowers
139*5697Smcpowers /* Construct ECCurveParams from an ECCurveName */
140*5697Smcpowers ECCurveParams *
EC_GetNamedCurveParams(const ECCurveName name,int kmflag)141*5697Smcpowers EC_GetNamedCurveParams(const ECCurveName name, int kmflag)
142*5697Smcpowers {
143*5697Smcpowers if ((name <= ECCurve_noName) || (ECCurve_pastLastCurve <= name) ||
144*5697Smcpowers (ecCurve_map[name] == NULL)) {
145*5697Smcpowers return NULL;
146*5697Smcpowers } else {
147*5697Smcpowers return ECCurveParams_dup(ecCurve_map[name], kmflag);
148*5697Smcpowers }
149*5697Smcpowers }
150*5697Smcpowers
151*5697Smcpowers /* Free the memory allocated (if any) to an ECCurveParams object. */
152*5697Smcpowers void
EC_FreeCurveParams(ECCurveParams * params)153*5697Smcpowers EC_FreeCurveParams(ECCurveParams * params)
154*5697Smcpowers {
155*5697Smcpowers if (params == NULL)
156*5697Smcpowers return;
157*5697Smcpowers if (params->text != NULL)
158*5697Smcpowers #ifdef _KERNEL
159*5697Smcpowers kmem_free(params->text, strlen(params->text) + 1);
160*5697Smcpowers #else
161*5697Smcpowers free(params->text);
162*5697Smcpowers #endif
163*5697Smcpowers if (params->irr != NULL)
164*5697Smcpowers #ifdef _KERNEL
165*5697Smcpowers kmem_free(params->irr, strlen(params->irr) + 1);
166*5697Smcpowers #else
167*5697Smcpowers free(params->irr);
168*5697Smcpowers #endif
169*5697Smcpowers if (params->curvea != NULL)
170*5697Smcpowers #ifdef _KERNEL
171*5697Smcpowers kmem_free(params->curvea, strlen(params->curvea) + 1);
172*5697Smcpowers #else
173*5697Smcpowers free(params->curvea);
174*5697Smcpowers #endif
175*5697Smcpowers if (params->curveb != NULL)
176*5697Smcpowers #ifdef _KERNEL
177*5697Smcpowers kmem_free(params->curveb, strlen(params->curveb) + 1);
178*5697Smcpowers #else
179*5697Smcpowers free(params->curveb);
180*5697Smcpowers #endif
181*5697Smcpowers if (params->genx != NULL)
182*5697Smcpowers #ifdef _KERNEL
183*5697Smcpowers kmem_free(params->genx, strlen(params->genx) + 1);
184*5697Smcpowers #else
185*5697Smcpowers free(params->genx);
186*5697Smcpowers #endif
187*5697Smcpowers if (params->geny != NULL)
188*5697Smcpowers #ifdef _KERNEL
189*5697Smcpowers kmem_free(params->geny, strlen(params->geny) + 1);
190*5697Smcpowers #else
191*5697Smcpowers free(params->geny);
192*5697Smcpowers #endif
193*5697Smcpowers if (params->order != NULL)
194*5697Smcpowers #ifdef _KERNEL
195*5697Smcpowers kmem_free(params->order, strlen(params->order) + 1);
196*5697Smcpowers #else
197*5697Smcpowers free(params->order);
198*5697Smcpowers #endif
199*5697Smcpowers #ifdef _KERNEL
200*5697Smcpowers kmem_free(params, sizeof(ECCurveParams));
201*5697Smcpowers #else
202*5697Smcpowers free(params);
203*5697Smcpowers #endif
204*5697Smcpowers }
205