security/nss/lib/freebl/ecl/ecl_curve.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/ecl/ecl_curve.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,89 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "ecl.h"
     1.9 +#include "ecl-curve.h"
    1.10 +#include "ecl-priv.h"
    1.11 +#include <stdlib.h>
    1.12 +#include <string.h>
    1.13 +
    1.14 +#define CHECK(func) if ((func) == NULL) { res = 0; goto CLEANUP; }
    1.15 +
    1.16 +/* Duplicates an ECCurveParams */
    1.17 +ECCurveParams *
    1.18 +ECCurveParams_dup(const ECCurveParams * params)
    1.19 +{
    1.20 +	int res = 1;
    1.21 +	ECCurveParams *ret = NULL;
    1.22 +
    1.23 +	CHECK(ret = (ECCurveParams *) calloc(1, sizeof(ECCurveParams)));
    1.24 +	if (params->text != NULL) {
    1.25 +		CHECK(ret->text = strdup(params->text));
    1.26 +	}
    1.27 +	ret->field = params->field;
    1.28 +	ret->size = params->size;
    1.29 +	if (params->irr != NULL) {
    1.30 +		CHECK(ret->irr = strdup(params->irr));
    1.31 +	}
    1.32 +	if (params->curvea != NULL) {
    1.33 +		CHECK(ret->curvea = strdup(params->curvea));
    1.34 +	}
    1.35 +	if (params->curveb != NULL) {
    1.36 +		CHECK(ret->curveb = strdup(params->curveb));
    1.37 +	}
    1.38 +	if (params->genx != NULL) {
    1.39 +		CHECK(ret->genx = strdup(params->genx));
    1.40 +	}
    1.41 +	if (params->geny != NULL) {
    1.42 +		CHECK(ret->geny = strdup(params->geny));
    1.43 +	}
    1.44 +	if (params->order != NULL) {
    1.45 +		CHECK(ret->order = strdup(params->order));
    1.46 +	}
    1.47 +	ret->cofactor = params->cofactor;
    1.48 +
    1.49 +  CLEANUP:
    1.50 +	if (res != 1) {
    1.51 +		EC_FreeCurveParams(ret);
    1.52 +		return NULL;
    1.53 +	}
    1.54 +	return ret;
    1.55 +}
    1.56 +
    1.57 +#undef CHECK
    1.58 +
    1.59 +/* Construct ECCurveParams from an ECCurveName */
    1.60 +ECCurveParams *
    1.61 +EC_GetNamedCurveParams(const ECCurveName name)
    1.62 +{
    1.63 +	if ((name <= ECCurve_noName) || (ECCurve_pastLastCurve <= name) ||
    1.64 +					(ecCurve_map[name] == NULL)) {
    1.65 +		return NULL;
    1.66 +	} else {
    1.67 +		return ECCurveParams_dup(ecCurve_map[name]);
    1.68 +	}
    1.69 +}
    1.70 +
    1.71 +/* Free the memory allocated (if any) to an ECCurveParams object. */
    1.72 +void
    1.73 +EC_FreeCurveParams(ECCurveParams * params)
    1.74 +{
    1.75 +	if (params == NULL)
    1.76 +		return;
    1.77 +	if (params->text != NULL)
    1.78 +		free(params->text);
    1.79 +	if (params->irr != NULL)
    1.80 +		free(params->irr);
    1.81 +	if (params->curvea != NULL)
    1.82 +		free(params->curvea);
    1.83 +	if (params->curveb != NULL)
    1.84 +		free(params->curveb);
    1.85 +	if (params->genx != NULL)
    1.86 +		free(params->genx);
    1.87 +	if (params->geny != NULL)
    1.88 +		free(params->geny);
    1.89 +	if (params->order != NULL)
    1.90 +		free(params->order);
    1.91 +	free(params);
    1.92 +}

mercurial