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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright (C) 2019-Present SKALE Labs
This file is part of sgxwallet.
sgxwallet is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
sgxwallet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with sgxwallet. If not, see <https://www.gnu.org/licenses/>.
@file domain_parameters.c
@author Stan Kladko
@date 2019
*/
#define SAFE_FREE(__X__) if (__X__) {free(__X__); __X__ = NULL;}
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
#define SAFE_CHAR_BUF(__X__, __Y__) ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);
#ifdef USER_SPACE
#include <gmp.h>
#else
#include <../tgmp-build/include/sgx_tgmp.h>
#endif
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "EnclaveCommon.h"
#include "Point.h"
#include "DomainParameters.h"
#define CHECK_ARG_ABORT(_EXPRESSION_) \
if (!(_EXPRESSION_)) { \
abort(); \
}
/*Initialize a curve*/
domain_parameters domain_parameters_init()
{
domain_parameters curve;
curve = (domain_parameters) calloc(sizeof(struct domain_parameters_s),1);
CHECK_ARG_ABORT(curve);
//Initialize all members
mpz_init(curve->p);
mpz_init(curve->a);
mpz_init(curve->b);
mpz_init(curve->n);
mpz_init(curve->h);
curve->G = point_init();
CHECK_ARG_ABORT(curve->G);
return curve;
}
/*Sets the name of a curve*/
void domain_parameters_set_name(domain_parameters curve, char* name)
{
CHECK_ARG_ABORT(name);
int len = strlen(name);
curve->name = (char*)calloc( sizeof(char) * (len+1), 1 );
curve->name[len] = '\0';
strncpy(curve->name, name, len+1);
}
/*Set domain parameters from decimal unsigned long ints*/
void domain_parameters_set_ui(domain_parameters curve,
char* name,
unsigned long int p,
unsigned long int a,
unsigned long int b,
unsigned long int Gx,
unsigned long int Gy,
unsigned long int n,
unsigned long int h)
{
CHECK_ARG_ABORT(name);
domain_parameters_set_name(curve, name);
mpz_set_ui(curve->p, p);
mpz_set_ui(curve->a, a);
mpz_set_ui(curve->b, b);
point_set_ui(curve->G, Gx, Gy);
mpz_set_ui(curve->n, n);
mpz_set_ui(curve->h, h);
}
/*Set domain parameters from hexadecimal string*/
void domain_parameters_set_hex(domain_parameters curve, char* name, char* p, char* a, char* b, char* Gx, char* Gy, char* n, char* h)
{
CHECK_ARG_ABORT(name);
CHECK_ARG_ABORT(p);
CHECK_ARG_ABORT(a);
CHECK_ARG_ABORT(b);
CHECK_ARG_ABORT(Gx);
CHECK_ARG_ABORT(Gy);
CHECK_ARG_ABORT(n);
CHECK_ARG_ABORT(h);
domain_parameters_set_name(curve, name);
mpz_set_str(curve->p, p, 16);
mpz_set_str(curve->a, a, 16);
mpz_set_str(curve->b, b, 16);
point_set_hex(curve->G, Gx, Gy);
mpz_set_str(curve->n, n, 16);
mpz_set_str(curve->h, h, 16);
}
/*Release memory*/
void domain_parameters_clear(domain_parameters curve)
{
if (!curve)
return;
mpz_clear(curve->p);
mpz_clear(curve->a);
mpz_clear(curve->b);
point_clear(curve->G);
mpz_clear(curve->n);
mpz_clear(curve->h);
SAFE_FREE(curve->name);
free(curve);
}