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 * 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 #ifndef _KERNEL 128 CLEANUP: 129 #endif 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