summaryrefslogtreecommitdiffstats
path: root/security/nss/lib/freebl/ecl/ecl_curve.c
blob: cf090cfc34a340c970a5fb2152dcd89361854adf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ecl.h"
#include "ecl-curve.h"
#include "ecl-priv.h"
#include <stdlib.h>
#include <string.h>

#define CHECK(func)       \
    if ((func) == NULL) { \
        res = 0;          \
        goto CLEANUP;     \
    }

/* Duplicates an ECCurveParams */
ECCurveParams *
ECCurveParams_dup(const ECCurveParams *params)
{
    int res = 1;
    ECCurveParams *ret = NULL;

    CHECK(ret = (ECCurveParams *)calloc(1, sizeof(ECCurveParams)));
    if (params->text != NULL) {
        CHECK(ret->text = strdup(params->text));
    }
    ret->field = params->field;
    ret->size = params->size;
    if (params->irr != NULL) {
        CHECK(ret->irr = strdup(params->irr));
    }
    if (params->curvea != NULL) {
        CHECK(ret->curvea = strdup(params->curvea));
    }
    if (params->curveb != NULL) {
        CHECK(ret->curveb = strdup(params->curveb));
    }
    if (params->genx != NULL) {
        CHECK(ret->genx = strdup(params->genx));
    }
    if (params->geny != NULL) {
        CHECK(ret->geny = strdup(params->geny));
    }
    if (params->order != NULL) {
        CHECK(ret->order = strdup(params->order));
    }
    ret->cofactor = params->cofactor;

CLEANUP:
    if (res != 1) {
        EC_FreeCurveParams(ret);
        return NULL;
    }
    return ret;
}

#undef CHECK

/* Construct ECCurveParams from an ECCurveName */
ECCurveParams *
EC_GetNamedCurveParams(const ECCurveName name)
{
    if ((name <= ECCurve_noName) || (ECCurve_pastLastCurve <= name) ||
        (ecCurve_map[name] == NULL)) {
        return NULL;
    } else {
        return ECCurveParams_dup(ecCurve_map[name]);
    }
}

/* Free the memory allocated (if any) to an ECCurveParams object. */
void
EC_FreeCurveParams(ECCurveParams *params)
{
    if (params == NULL)
        return;
    if (params->text != NULL)
        free(params->text);
    if (params->irr != NULL)
        free(params->irr);
    if (params->curvea != NULL)
        free(params->curvea);
    if (params->curveb != NULL)
        free(params->curveb);
    if (params->genx != NULL)
        free(params->genx);
    if (params->geny != NULL)
        free(params->geny);
    if (params->order != NULL)
        free(params->order);
    free(params);
}