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