2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is the elliptic curve math library.
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.
23 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
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.
37 * ***** END LICENSE BLOCK ***** */
39 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
40 * Use is subject to license terms.
42 * Sun elects to use this software under the MPL license.
45 #pragma ident "%Z%%M% %I% %E% SMI"
48 #include "ecl-curve.h"
55 #define CHECK(func) if ((func) == NULL) { res = 0; goto CLEANUP; }
57 /* Duplicates an ECCurveParams */
59 ECCurveParams_dup(const ECCurveParams
* params
, int kmflag
)
62 ECCurveParams
*ret
= NULL
;
65 ret
= (ECCurveParams
*) kmem_zalloc(sizeof(ECCurveParams
), kmflag
);
67 CHECK(ret
= (ECCurveParams
*) calloc(1, sizeof(ECCurveParams
)));
69 if (params
->text
!= NULL
) {
71 ret
->text
= kmem_alloc(strlen(params
->text
) + 1, kmflag
);
72 bcopy(params
->text
, ret
->text
, strlen(params
->text
) + 1);
74 CHECK(ret
->text
= strdup(params
->text
));
77 ret
->field
= params
->field
;
78 ret
->size
= params
->size
;
79 if (params
->irr
!= NULL
) {
81 ret
->irr
= kmem_alloc(strlen(params
->irr
) + 1, kmflag
);
82 bcopy(params
->irr
, ret
->irr
, strlen(params
->irr
) + 1);
84 CHECK(ret
->irr
= strdup(params
->irr
));
87 if (params
->curvea
!= NULL
) {
89 ret
->curvea
= kmem_alloc(strlen(params
->curvea
) + 1, kmflag
);
90 bcopy(params
->curvea
, ret
->curvea
, strlen(params
->curvea
) + 1);
92 CHECK(ret
->curvea
= strdup(params
->curvea
));
95 if (params
->curveb
!= NULL
) {
97 ret
->curveb
= kmem_alloc(strlen(params
->curveb
) + 1, kmflag
);
98 bcopy(params
->curveb
, ret
->curveb
, strlen(params
->curveb
) + 1);
100 CHECK(ret
->curveb
= strdup(params
->curveb
));
103 if (params
->genx
!= NULL
) {
105 ret
->genx
= kmem_alloc(strlen(params
->genx
) + 1, kmflag
);
106 bcopy(params
->genx
, ret
->genx
, strlen(params
->genx
) + 1);
108 CHECK(ret
->genx
= strdup(params
->genx
));
111 if (params
->geny
!= NULL
) {
113 ret
->geny
= kmem_alloc(strlen(params
->geny
) + 1, kmflag
);
114 bcopy(params
->geny
, ret
->geny
, strlen(params
->geny
) + 1);
116 CHECK(ret
->geny
= strdup(params
->geny
));
119 if (params
->order
!= NULL
) {
121 ret
->order
= kmem_alloc(strlen(params
->order
) + 1, kmflag
);
122 bcopy(params
->order
, ret
->order
, strlen(params
->order
) + 1);
124 CHECK(ret
->order
= strdup(params
->order
));
127 ret
->cofactor
= params
->cofactor
;
131 EC_FreeCurveParams(ret
);
139 /* Construct ECCurveParams from an ECCurveName */
141 EC_GetNamedCurveParams(const ECCurveName name
, int kmflag
)
143 if ((name
<= ECCurve_noName
) || (ECCurve_pastLastCurve
<= name
) ||
144 (ecCurve_map
[name
] == NULL
)) {
147 return ECCurveParams_dup(ecCurve_map
[name
], kmflag
);
151 /* Free the memory allocated (if any) to an ECCurveParams object. */
153 EC_FreeCurveParams(ECCurveParams
* params
)
157 if (params
->text
!= NULL
)
159 kmem_free(params
->text
, strlen(params
->text
) + 1);
163 if (params
->irr
!= NULL
)
165 kmem_free(params
->irr
, strlen(params
->irr
) + 1);
169 if (params
->curvea
!= NULL
)
171 kmem_free(params
->curvea
, strlen(params
->curvea
) + 1);
173 free(params
->curvea
);
175 if (params
->curveb
!= NULL
)
177 kmem_free(params
->curveb
, strlen(params
->curveb
) + 1);
179 free(params
->curveb
);
181 if (params
->genx
!= NULL
)
183 kmem_free(params
->genx
, strlen(params
->genx
) + 1);
187 if (params
->geny
!= NULL
)
189 kmem_free(params
->geny
, strlen(params
->geny
) + 1);
193 if (params
->order
!= NULL
)
195 kmem_free(params
->order
, strlen(params
->order
) + 1);
200 kmem_free(params
, sizeof(ECCurveParams
));