| 1 | /* |
| 2 | * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
| 3 | * Use is subject to license terms. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public License |
| 16 | * along with this library; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
| 22 | */ |
| 23 | |
| 24 | /* ********************************************************************* |
| 25 | * |
| 26 | * The Original Code is the elliptic curve math library. |
| 27 | * |
| 28 | * The Initial Developer of the Original Code is |
| 29 | * Sun Microsystems, Inc. |
| 30 | * Portions created by the Initial Developer are Copyright (C) 2003 |
| 31 | * the Initial Developer. All Rights Reserved. |
| 32 | * |
| 33 | * Contributor(s): |
| 34 | * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories |
| 35 | * |
| 36 | *********************************************************************** */ |
| 37 | |
| 38 | #include "ecl.h" |
| 39 | #include "ecl-curve.h" |
| 40 | #include "ecl-priv.h" |
| 41 | #ifndef _KERNEL |
| 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
| 44 | #endif |
| 45 | |
| 46 | #define CHECK(func) if ((func) == NULL) { res = 0; goto CLEANUP; } |
| 47 | |
| 48 | /* Duplicates an ECCurveParams */ |
| 49 | ECCurveParams * |
| 50 | ECCurveParams_dup(const ECCurveParams * params, int kmflag) |
| 51 | { |
| 52 | int res = 1; |
| 53 | ECCurveParams *ret = NULL; |
| 54 | |
| 55 | #ifdef _KERNEL |
| 56 | ret = (ECCurveParams *) kmem_zalloc(sizeof(ECCurveParams), kmflag); |
| 57 | #else |
| 58 | CHECK(ret = (ECCurveParams *) calloc(1, sizeof(ECCurveParams))); |
| 59 | #endif |
| 60 | if (params->text != NULL) { |
| 61 | #ifdef _KERNEL |
| 62 | ret->text = kmem_alloc(strlen(params->text) + 1, kmflag); |
| 63 | bcopy(params->text, ret->text, strlen(params->text) + 1); |
| 64 | #else |
| 65 | CHECK(ret->text = strdup(params->text)); |
| 66 | #endif |
| 67 | } |
| 68 | ret->field = params->field; |
| 69 | ret->size = params->size; |
| 70 | if (params->irr != NULL) { |
| 71 | #ifdef _KERNEL |
| 72 | ret->irr = kmem_alloc(strlen(params->irr) + 1, kmflag); |
| 73 | bcopy(params->irr, ret->irr, strlen(params->irr) + 1); |
| 74 | #else |
| 75 | CHECK(ret->irr = strdup(params->irr)); |
| 76 | #endif |
| 77 | } |
| 78 | if (params->curvea != NULL) { |
| 79 | #ifdef _KERNEL |
| 80 | ret->curvea = kmem_alloc(strlen(params->curvea) + 1, kmflag); |
| 81 | bcopy(params->curvea, ret->curvea, strlen(params->curvea) + 1); |
| 82 | #else |
| 83 | CHECK(ret->curvea = strdup(params->curvea)); |
| 84 | #endif |
| 85 | } |
| 86 | if (params->curveb != NULL) { |
| 87 | #ifdef _KERNEL |
| 88 | ret->curveb = kmem_alloc(strlen(params->curveb) + 1, kmflag); |
| 89 | bcopy(params->curveb, ret->curveb, strlen(params->curveb) + 1); |
| 90 | #else |
| 91 | CHECK(ret->curveb = strdup(params->curveb)); |
| 92 | #endif |
| 93 | } |
| 94 | if (params->genx != NULL) { |
| 95 | #ifdef _KERNEL |
| 96 | ret->genx = kmem_alloc(strlen(params->genx) + 1, kmflag); |
| 97 | bcopy(params->genx, ret->genx, strlen(params->genx) + 1); |
| 98 | #else |
| 99 | CHECK(ret->genx = strdup(params->genx)); |
| 100 | #endif |
| 101 | } |
| 102 | if (params->geny != NULL) { |
| 103 | #ifdef _KERNEL |
| 104 | ret->geny = kmem_alloc(strlen(params->geny) + 1, kmflag); |
| 105 | bcopy(params->geny, ret->geny, strlen(params->geny) + 1); |
| 106 | #else |
| 107 | CHECK(ret->geny = strdup(params->geny)); |
| 108 | #endif |
| 109 | } |
| 110 | if (params->order != NULL) { |
| 111 | #ifdef _KERNEL |
| 112 | ret->order = kmem_alloc(strlen(params->order) + 1, kmflag); |
| 113 | bcopy(params->order, ret->order, strlen(params->order) + 1); |
| 114 | #else |
| 115 | CHECK(ret->order = strdup(params->order)); |
| 116 | #endif |
| 117 | } |
| 118 | ret->cofactor = params->cofactor; |
| 119 | |
| 120 | CLEANUP: |
| 121 | if (res != 1) { |
| 122 | EC_FreeCurveParams(ret); |
| 123 | return NULL; |
| 124 | } |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | #undef CHECK |
| 129 | |
| 130 | /* Construct ECCurveParams from an ECCurveName */ |
| 131 | ECCurveParams * |
| 132 | EC_GetNamedCurveParams(const ECCurveName name, int kmflag) |
| 133 | { |
| 134 | if ((name <= ECCurve_noName) || (ECCurve_pastLastCurve <= name) || |
| 135 | (ecCurve_map[name] == NULL)) { |
| 136 | return NULL; |
| 137 | } else { |
| 138 | return ECCurveParams_dup(ecCurve_map[name], kmflag); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /* Free the memory allocated (if any) to an ECCurveParams object. */ |
| 143 | void |
| 144 | EC_FreeCurveParams(ECCurveParams * params) |
| 145 | { |
| 146 | if (params == NULL) |
| 147 | return; |
| 148 | if (params->text != NULL) |
| 149 | #ifdef _KERNEL |
| 150 | kmem_free(params->text, strlen(params->text) + 1); |
| 151 | #else |
| 152 | free(params->text); |
| 153 | #endif |
| 154 | if (params->irr != NULL) |
| 155 | #ifdef _KERNEL |
| 156 | kmem_free(params->irr, strlen(params->irr) + 1); |
| 157 | #else |
| 158 | free(params->irr); |
| 159 | #endif |
| 160 | if (params->curvea != NULL) |
| 161 | #ifdef _KERNEL |
| 162 | kmem_free(params->curvea, strlen(params->curvea) + 1); |
| 163 | #else |
| 164 | free(params->curvea); |
| 165 | #endif |
| 166 | if (params->curveb != NULL) |
| 167 | #ifdef _KERNEL |
| 168 | kmem_free(params->curveb, strlen(params->curveb) + 1); |
| 169 | #else |
| 170 | free(params->curveb); |
| 171 | #endif |
| 172 | if (params->genx != NULL) |
| 173 | #ifdef _KERNEL |
| 174 | kmem_free(params->genx, strlen(params->genx) + 1); |
| 175 | #else |
| 176 | free(params->genx); |
| 177 | #endif |
| 178 | if (params->geny != NULL) |
| 179 | #ifdef _KERNEL |
| 180 | kmem_free(params->geny, strlen(params->geny) + 1); |
| 181 | #else |
| 182 | free(params->geny); |
| 183 | #endif |
| 184 | if (params->order != NULL) |
| 185 | #ifdef _KERNEL |
| 186 | kmem_free(params->order, strlen(params->order) + 1); |
| 187 | #else |
| 188 | free(params->order); |
| 189 | #endif |
| 190 | #ifdef _KERNEL |
| 191 | kmem_free(params, sizeof(ECCurveParams)); |
| 192 | #else |
| 193 | free(params); |
| 194 | #endif |
| 195 | } |
| 196 | |