1 /*
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is the elliptic curve math library.
16 *
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.
21 *
22 * Contributor(s):
23 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
24 *
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.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 /*
39 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
40 * Use is subject to license terms.
41 *
42 * Sun elects to use this software under the MPL license.
43 */
44
45 #include "ecl.h"
46 #include "ecl-curve.h"
47 #include "ecl-priv.h"
48 #ifndef _KERNEL
49 #include <stdlib.h>
50 #include <string.h>
51 #endif
52
53 #define CHECK(func) if ((func) == NULL) { res = 0; goto CLEANUP; }
54
55 /* Duplicates an ECCurveParams */
56 ECCurveParams *
ECCurveParams_dup(const ECCurveParams * params,int kmflag)57 ECCurveParams_dup(const ECCurveParams * params, int kmflag)
58 {
59 int res = 1;
60 ECCurveParams *ret = NULL;
61
62 #ifdef _KERNEL
63 ret = (ECCurveParams *) kmem_zalloc(sizeof(ECCurveParams), kmflag);
64 #else
65 CHECK(ret = (ECCurveParams *) calloc(1, sizeof(ECCurveParams)));
66 #endif
67 if (params->text != NULL) {
68 #ifdef _KERNEL
69 ret->text = kmem_alloc(strlen(params->text) + 1, kmflag);
70 bcopy(params->text, ret->text, strlen(params->text) + 1);
71 #else
72 CHECK(ret->text = strdup(params->text));
73 #endif
74 }
75 ret->field = params->field;
76 ret->size = params->size;
77 if (params->irr != NULL) {
78 #ifdef _KERNEL
79 ret->irr = kmem_alloc(strlen(params->irr) + 1, kmflag);
80 bcopy(params->irr, ret->irr, strlen(params->irr) + 1);
81 #else
82 CHECK(ret->irr = strdup(params->irr));
83 #endif
84 }
85 if (params->curvea != NULL) {
86 #ifdef _KERNEL
87 ret->curvea = kmem_alloc(strlen(params->curvea) + 1, kmflag);
88 bcopy(params->curvea, ret->curvea, strlen(params->curvea) + 1);
89 #else
90 CHECK(ret->curvea = strdup(params->curvea));
91 #endif
92 }
93 if (params->curveb != NULL) {
94 #ifdef _KERNEL
95 ret->curveb = kmem_alloc(strlen(params->curveb) + 1, kmflag);
96 bcopy(params->curveb, ret->curveb, strlen(params->curveb) + 1);
97 #else
98 CHECK(ret->curveb = strdup(params->curveb));
99 #endif
100 }
101 if (params->genx != NULL) {
102 #ifdef _KERNEL
103 ret->genx = kmem_alloc(strlen(params->genx) + 1, kmflag);
104 bcopy(params->genx, ret->genx, strlen(params->genx) + 1);
105 #else
106 CHECK(ret->genx = strdup(params->genx));
107 #endif
108 }
109 if (params->geny != NULL) {
110 #ifdef _KERNEL
111 ret->geny = kmem_alloc(strlen(params->geny) + 1, kmflag);
112 bcopy(params->geny, ret->geny, strlen(params->geny) + 1);
113 #else
114 CHECK(ret->geny = strdup(params->geny));
115 #endif
116 }
117 if (params->order != NULL) {
118 #ifdef _KERNEL
119 ret->order = kmem_alloc(strlen(params->order) + 1, kmflag);
120 bcopy(params->order, ret->order, strlen(params->order) + 1);
121 #else
122 CHECK(ret->order = strdup(params->order));
123 #endif
124 }
125 ret->cofactor = params->cofactor;
126
127 CLEANUP:
128 if (res != 1) {
129 EC_FreeCurveParams(ret);
130 return NULL;
131 }
132 return ret;
133 }
134
135 #undef CHECK
136
137 /* Construct ECCurveParams from an ECCurveName */
138 ECCurveParams *
EC_GetNamedCurveParams(const ECCurveName name,int kmflag)139 EC_GetNamedCurveParams(const ECCurveName name, int kmflag)
140 {
141 if ((name <= ECCurve_noName) || (ECCurve_pastLastCurve <= name) ||
142 (ecCurve_map[name] == NULL)) {
143 return NULL;
144 } else {
145 return ECCurveParams_dup(ecCurve_map[name], kmflag);
146 }
147 }
148
149 /* Free the memory allocated (if any) to an ECCurveParams object. */
150 void
EC_FreeCurveParams(ECCurveParams * params)151 EC_FreeCurveParams(ECCurveParams * params)
152 {
153 if (params == NULL)
154 return;
155 if (params->text != NULL)
156 #ifdef _KERNEL
157 kmem_free(params->text, strlen(params->text) + 1);
158 #else
159 free(params->text);
160 #endif
161 if (params->irr != NULL)
162 #ifdef _KERNEL
163 kmem_free(params->irr, strlen(params->irr) + 1);
164 #else
165 free(params->irr);
166 #endif
167 if (params->curvea != NULL)
168 #ifdef _KERNEL
169 kmem_free(params->curvea, strlen(params->curvea) + 1);
170 #else
171 free(params->curvea);
172 #endif
173 if (params->curveb != NULL)
174 #ifdef _KERNEL
175 kmem_free(params->curveb, strlen(params->curveb) + 1);
176 #else
177 free(params->curveb);
178 #endif
179 if (params->genx != NULL)
180 #ifdef _KERNEL
181 kmem_free(params->genx, strlen(params->genx) + 1);
182 #else
183 free(params->genx);
184 #endif
185 if (params->geny != NULL)
186 #ifdef _KERNEL
187 kmem_free(params->geny, strlen(params->geny) + 1);
188 #else
189 free(params->geny);
190 #endif
191 if (params->order != NULL)
192 #ifdef _KERNEL
193 kmem_free(params->order, strlen(params->order) + 1);
194 #else
195 free(params->order);
196 #endif
197 #ifdef _KERNEL
198 kmem_free(params, sizeof(ECCurveParams));
199 #else
200 free(params);
201 #endif
202 }
203