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