|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "ecl.h" |
|
6 #include "ecl-curve.h" |
|
7 #include "ecl-priv.h" |
|
8 #include <stdlib.h> |
|
9 #include <string.h> |
|
10 |
|
11 #define CHECK(func) if ((func) == NULL) { res = 0; goto CLEANUP; } |
|
12 |
|
13 /* Duplicates an ECCurveParams */ |
|
14 ECCurveParams * |
|
15 ECCurveParams_dup(const ECCurveParams * params) |
|
16 { |
|
17 int res = 1; |
|
18 ECCurveParams *ret = NULL; |
|
19 |
|
20 CHECK(ret = (ECCurveParams *) calloc(1, sizeof(ECCurveParams))); |
|
21 if (params->text != NULL) { |
|
22 CHECK(ret->text = strdup(params->text)); |
|
23 } |
|
24 ret->field = params->field; |
|
25 ret->size = params->size; |
|
26 if (params->irr != NULL) { |
|
27 CHECK(ret->irr = strdup(params->irr)); |
|
28 } |
|
29 if (params->curvea != NULL) { |
|
30 CHECK(ret->curvea = strdup(params->curvea)); |
|
31 } |
|
32 if (params->curveb != NULL) { |
|
33 CHECK(ret->curveb = strdup(params->curveb)); |
|
34 } |
|
35 if (params->genx != NULL) { |
|
36 CHECK(ret->genx = strdup(params->genx)); |
|
37 } |
|
38 if (params->geny != NULL) { |
|
39 CHECK(ret->geny = strdup(params->geny)); |
|
40 } |
|
41 if (params->order != NULL) { |
|
42 CHECK(ret->order = strdup(params->order)); |
|
43 } |
|
44 ret->cofactor = params->cofactor; |
|
45 |
|
46 CLEANUP: |
|
47 if (res != 1) { |
|
48 EC_FreeCurveParams(ret); |
|
49 return NULL; |
|
50 } |
|
51 return ret; |
|
52 } |
|
53 |
|
54 #undef CHECK |
|
55 |
|
56 /* Construct ECCurveParams from an ECCurveName */ |
|
57 ECCurveParams * |
|
58 EC_GetNamedCurveParams(const ECCurveName name) |
|
59 { |
|
60 if ((name <= ECCurve_noName) || (ECCurve_pastLastCurve <= name) || |
|
61 (ecCurve_map[name] == NULL)) { |
|
62 return NULL; |
|
63 } else { |
|
64 return ECCurveParams_dup(ecCurve_map[name]); |
|
65 } |
|
66 } |
|
67 |
|
68 /* Free the memory allocated (if any) to an ECCurveParams object. */ |
|
69 void |
|
70 EC_FreeCurveParams(ECCurveParams * params) |
|
71 { |
|
72 if (params == NULL) |
|
73 return; |
|
74 if (params->text != NULL) |
|
75 free(params->text); |
|
76 if (params->irr != NULL) |
|
77 free(params->irr); |
|
78 if (params->curvea != NULL) |
|
79 free(params->curvea); |
|
80 if (params->curveb != NULL) |
|
81 free(params->curveb); |
|
82 if (params->genx != NULL) |
|
83 free(params->genx); |
|
84 if (params->geny != NULL) |
|
85 free(params->geny); |
|
86 if (params->order != NULL) |
|
87 free(params->order); |
|
88 free(params); |
|
89 } |