andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "ecl.h" andre@0: #include "ecl-curve.h" andre@0: #include "ecl-priv.h" andre@0: #include andre@0: #include andre@0: andre@0: #define CHECK(func) if ((func) == NULL) { res = 0; goto CLEANUP; } andre@0: andre@0: /* Duplicates an ECCurveParams */ andre@0: ECCurveParams * andre@0: ECCurveParams_dup(const ECCurveParams * params) andre@0: { andre@0: int res = 1; andre@0: ECCurveParams *ret = NULL; andre@0: andre@0: CHECK(ret = (ECCurveParams *) calloc(1, sizeof(ECCurveParams))); andre@0: if (params->text != NULL) { andre@0: CHECK(ret->text = strdup(params->text)); andre@0: } andre@0: ret->field = params->field; andre@0: ret->size = params->size; andre@0: if (params->irr != NULL) { andre@0: CHECK(ret->irr = strdup(params->irr)); andre@0: } andre@0: if (params->curvea != NULL) { andre@0: CHECK(ret->curvea = strdup(params->curvea)); andre@0: } andre@0: if (params->curveb != NULL) { andre@0: CHECK(ret->curveb = strdup(params->curveb)); andre@0: } andre@0: if (params->genx != NULL) { andre@0: CHECK(ret->genx = strdup(params->genx)); andre@0: } andre@0: if (params->geny != NULL) { andre@0: CHECK(ret->geny = strdup(params->geny)); andre@0: } andre@0: if (params->order != NULL) { andre@0: CHECK(ret->order = strdup(params->order)); andre@0: } andre@0: ret->cofactor = params->cofactor; andre@0: andre@0: CLEANUP: andre@0: if (res != 1) { andre@0: EC_FreeCurveParams(ret); andre@0: return NULL; andre@0: } andre@0: return ret; andre@0: } andre@0: andre@0: #undef CHECK andre@0: andre@0: /* Construct ECCurveParams from an ECCurveName */ andre@0: ECCurveParams * andre@0: EC_GetNamedCurveParams(const ECCurveName name) andre@0: { andre@0: if ((name <= ECCurve_noName) || (ECCurve_pastLastCurve <= name) || andre@0: (ecCurve_map[name] == NULL)) { andre@0: return NULL; andre@0: } else { andre@0: return ECCurveParams_dup(ecCurve_map[name]); andre@0: } andre@0: } andre@0: andre@0: /* Free the memory allocated (if any) to an ECCurveParams object. */ andre@0: void andre@0: EC_FreeCurveParams(ECCurveParams * params) andre@0: { andre@0: if (params == NULL) andre@0: return; andre@0: if (params->text != NULL) andre@0: free(params->text); andre@0: if (params->irr != NULL) andre@0: free(params->irr); andre@0: if (params->curvea != NULL) andre@0: free(params->curvea); andre@0: if (params->curveb != NULL) andre@0: free(params->curveb); andre@0: if (params->genx != NULL) andre@0: free(params->genx); andre@0: if (params->geny != NULL) andre@0: free(params->geny); andre@0: if (params->order != NULL) andre@0: free(params->order); andre@0: free(params); andre@0: }