|
1 /* ********************************************************************* |
|
2 * |
|
3 * Sun elects to have this file available under and governed by the |
|
4 * Mozilla Public License Version 1.1 ("MPL") (see |
|
5 * http://www.mozilla.org/MPL/ for full license text). For the avoidance |
|
6 * of doubt and subject to the following, Sun also elects to allow |
|
7 * licensees to use this file under the MPL, the GNU General Public |
|
8 * License version 2 only or the Lesser General Public License version |
|
9 * 2.1 only. Any references to the "GNU General Public License version 2 |
|
10 * or later" or "GPL" in the following shall be construed to mean the |
|
11 * GNU General Public License version 2 only. Any references to the "GNU |
|
12 * Lesser General Public License version 2.1 or later" or "LGPL" in the |
|
13 * following shall be construed to mean the GNU Lesser General Public |
|
14 * License version 2.1 only. However, the following notice accompanied |
|
15 * the original version of this file: |
|
16 * |
|
17 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
|
18 * |
|
19 * The contents of this file are subject to the Mozilla Public License Version |
|
20 * 1.1 (the "License"); you may not use this file except in compliance with |
|
21 * the License. You may obtain a copy of the License at |
|
22 * http://www.mozilla.org/MPL/ |
|
23 * |
|
24 * Software distributed under the License is distributed on an "AS IS" basis, |
|
25 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
|
26 * for the specific language governing rights and limitations under the |
|
27 * License. |
|
28 * |
|
29 * The Original Code is the elliptic curve math library. |
|
30 * |
|
31 * The Initial Developer of the Original Code is |
|
32 * Sun Microsystems, Inc. |
|
33 * Portions created by the Initial Developer are Copyright (C) 2003 |
|
34 * the Initial Developer. All Rights Reserved. |
|
35 * |
|
36 * Contributor(s): |
|
37 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories |
|
38 * |
|
39 * Alternatively, the contents of this file may be used under the terms of |
|
40 * either the GNU General Public License Version 2 or later (the "GPL"), or |
|
41 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
|
42 * in which case the provisions of the GPL or the LGPL are applicable instead |
|
43 * of those above. If you wish to allow use of your version of this file only |
|
44 * under the terms of either the GPL or the LGPL, and not to allow others to |
|
45 * use your version of this file under the terms of the MPL, indicate your |
|
46 * decision by deleting the provisions above and replace them with the notice |
|
47 * and other provisions required by the GPL or the LGPL. If you do not delete |
|
48 * the provisions above, a recipient may use your version of this file under |
|
49 * the terms of any one of the MPL, the GPL or the LGPL. |
|
50 * |
|
51 *********************************************************************** */ |
|
52 /* |
|
53 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
|
54 * Use is subject to license terms. |
|
55 */ |
|
56 |
|
57 #pragma ident "%Z%%M% %I% %E% SMI" |
|
58 |
|
59 #include "ecl.h" |
|
60 #include "ecl-curve.h" |
|
61 #include "ecl-priv.h" |
|
62 #ifndef _KERNEL |
|
63 #include <stdlib.h> |
|
64 #include <string.h> |
|
65 #endif |
|
66 |
|
67 #define CHECK(func) if ((func) == NULL) { res = 0; goto CLEANUP; } |
|
68 |
|
69 /* Duplicates an ECCurveParams */ |
|
70 ECCurveParams * |
|
71 ECCurveParams_dup(const ECCurveParams * params, int kmflag) |
|
72 { |
|
73 int res = 1; |
|
74 ECCurveParams *ret = NULL; |
|
75 |
|
76 #ifdef _KERNEL |
|
77 ret = (ECCurveParams *) kmem_zalloc(sizeof(ECCurveParams), kmflag); |
|
78 #else |
|
79 CHECK(ret = (ECCurveParams *) calloc(1, sizeof(ECCurveParams))); |
|
80 #endif |
|
81 if (params->text != NULL) { |
|
82 #ifdef _KERNEL |
|
83 ret->text = kmem_alloc(strlen(params->text) + 1, kmflag); |
|
84 bcopy(params->text, ret->text, strlen(params->text) + 1); |
|
85 #else |
|
86 CHECK(ret->text = strdup(params->text)); |
|
87 #endif |
|
88 } |
|
89 ret->field = params->field; |
|
90 ret->size = params->size; |
|
91 if (params->irr != NULL) { |
|
92 #ifdef _KERNEL |
|
93 ret->irr = kmem_alloc(strlen(params->irr) + 1, kmflag); |
|
94 bcopy(params->irr, ret->irr, strlen(params->irr) + 1); |
|
95 #else |
|
96 CHECK(ret->irr = strdup(params->irr)); |
|
97 #endif |
|
98 } |
|
99 if (params->curvea != NULL) { |
|
100 #ifdef _KERNEL |
|
101 ret->curvea = kmem_alloc(strlen(params->curvea) + 1, kmflag); |
|
102 bcopy(params->curvea, ret->curvea, strlen(params->curvea) + 1); |
|
103 #else |
|
104 CHECK(ret->curvea = strdup(params->curvea)); |
|
105 #endif |
|
106 } |
|
107 if (params->curveb != NULL) { |
|
108 #ifdef _KERNEL |
|
109 ret->curveb = kmem_alloc(strlen(params->curveb) + 1, kmflag); |
|
110 bcopy(params->curveb, ret->curveb, strlen(params->curveb) + 1); |
|
111 #else |
|
112 CHECK(ret->curveb = strdup(params->curveb)); |
|
113 #endif |
|
114 } |
|
115 if (params->genx != NULL) { |
|
116 #ifdef _KERNEL |
|
117 ret->genx = kmem_alloc(strlen(params->genx) + 1, kmflag); |
|
118 bcopy(params->genx, ret->genx, strlen(params->genx) + 1); |
|
119 #else |
|
120 CHECK(ret->genx = strdup(params->genx)); |
|
121 #endif |
|
122 } |
|
123 if (params->geny != NULL) { |
|
124 #ifdef _KERNEL |
|
125 ret->geny = kmem_alloc(strlen(params->geny) + 1, kmflag); |
|
126 bcopy(params->geny, ret->geny, strlen(params->geny) + 1); |
|
127 #else |
|
128 CHECK(ret->geny = strdup(params->geny)); |
|
129 #endif |
|
130 } |
|
131 if (params->order != NULL) { |
|
132 #ifdef _KERNEL |
|
133 ret->order = kmem_alloc(strlen(params->order) + 1, kmflag); |
|
134 bcopy(params->order, ret->order, strlen(params->order) + 1); |
|
135 #else |
|
136 CHECK(ret->order = strdup(params->order)); |
|
137 #endif |
|
138 } |
|
139 ret->cofactor = params->cofactor; |
|
140 |
|
141 CLEANUP: |
|
142 if (res != 1) { |
|
143 EC_FreeCurveParams(ret); |
|
144 return NULL; |
|
145 } |
|
146 return ret; |
|
147 } |
|
148 |
|
149 #undef CHECK |
|
150 |
|
151 /* Construct ECCurveParams from an ECCurveName */ |
|
152 ECCurveParams * |
|
153 EC_GetNamedCurveParams(const ECCurveName name, int kmflag) |
|
154 { |
|
155 if ((name <= ECCurve_noName) || (ECCurve_pastLastCurve <= name) || |
|
156 (ecCurve_map[name] == NULL)) { |
|
157 return NULL; |
|
158 } else { |
|
159 return ECCurveParams_dup(ecCurve_map[name], kmflag); |
|
160 } |
|
161 } |
|
162 |
|
163 /* Free the memory allocated (if any) to an ECCurveParams object. */ |
|
164 void |
|
165 EC_FreeCurveParams(ECCurveParams * params) |
|
166 { |
|
167 if (params == NULL) |
|
168 return; |
|
169 if (params->text != NULL) |
|
170 #ifdef _KERNEL |
|
171 kmem_free(params->text, strlen(params->text) + 1); |
|
172 #else |
|
173 free(params->text); |
|
174 #endif |
|
175 if (params->irr != NULL) |
|
176 #ifdef _KERNEL |
|
177 kmem_free(params->irr, strlen(params->irr) + 1); |
|
178 #else |
|
179 free(params->irr); |
|
180 #endif |
|
181 if (params->curvea != NULL) |
|
182 #ifdef _KERNEL |
|
183 kmem_free(params->curvea, strlen(params->curvea) + 1); |
|
184 #else |
|
185 free(params->curvea); |
|
186 #endif |
|
187 if (params->curveb != NULL) |
|
188 #ifdef _KERNEL |
|
189 kmem_free(params->curveb, strlen(params->curveb) + 1); |
|
190 #else |
|
191 free(params->curveb); |
|
192 #endif |
|
193 if (params->genx != NULL) |
|
194 #ifdef _KERNEL |
|
195 kmem_free(params->genx, strlen(params->genx) + 1); |
|
196 #else |
|
197 free(params->genx); |
|
198 #endif |
|
199 if (params->geny != NULL) |
|
200 #ifdef _KERNEL |
|
201 kmem_free(params->geny, strlen(params->geny) + 1); |
|
202 #else |
|
203 free(params->geny); |
|
204 #endif |
|
205 if (params->order != NULL) |
|
206 #ifdef _KERNEL |
|
207 kmem_free(params->order, strlen(params->order) + 1); |
|
208 #else |
|
209 free(params->order); |
|
210 #endif |
|
211 #ifdef _KERNEL |
|
212 kmem_free(params, sizeof(ECCurveParams)); |
|
213 #else |
|
214 free(params); |
|
215 #endif |
|
216 } |