-
Notifications
You must be signed in to change notification settings - Fork 6
/
ec_inits.c
316 lines (274 loc) · 10.1 KB
/
ec_inits.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* ec.c
*
* Created on: Sep 9, 2015
* Author: tslld
*/
#include "ecdsa.h"
#include "ec.h"
#include "ec_point.h"
#include "utils.h"
struct curve_params {
char* name;
const char *p, *a, *b, *Gx, *Gy, *order, *cofactor;
};
static const struct curve_params curves_params[] = {
{
/* Certicom recommendations P-224 */
"secp224k1",
/* p */
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D",
/* a */
"00000000000000000000000000000000000000000000000000000000",
/* b */
"00000000000000000000000000000000000000000000000000000005",
/* Gx */
"A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C",
/* Gy */
"7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5",
/* order */
"010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7",
/* cofactor */
"01",
},
{
/* NIST P-224 */
"secp224r1",
/* p */
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
/* a */
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
/* b */
"B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
/* Gx */
"B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
/* Gy */
"BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
/* order */
"FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
/* cofactor */
"01",
},
{
/* Certicom 256 bits curve */
"secp256k1",
/* p */
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F",
/* a */
"0000000000000000000000000000000000000000000000000000000000000000",
/* b */
"0000000000000000000000000000000000000000000000000000000000000007",
/* Gx */
"79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
/* Gy */
"483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8",
/* order */
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",
/* cofactor */
"01",
},
{
/* NIST P-256 */
"secp256r1",
/* p */
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF",
/* a */
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC",
/* b */
"5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B",
/* Gx */
"6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296",
/* Gy */
"4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5",
/* order */
"FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551",
/* cofactor */
"01",
},
};
/** Allocates and initialize a ec_group structure
* \return pointer to a ec_group structure or NULL if an error occurred
*/
ec_group ec_group_init() {
ec_group ec;
ec = malloc(sizeof(struct ec_group_st));
assert(ec != NULL);
mpz_init(ec->A);
mpz_init(ec->B);
mpz_init(ec->field);
mpz_init(ec->order);
mpz_init(ec->cofactor);
ec->generator = ec_point_init();
return ec;
}
ec_group ec_group_init_by_curve_name(const char* name) {
ec_group ret = ec_group_init();
int ok = 0;
unsigned int i;
int no_curves = sizeof(curves_params) / sizeof(struct curve_params);
for (i = 0; i < no_curves; i++) {
if (strcmp(curves_params[i].name, name) == 0) {
ec_group_set_str_hex(ret, name, curves_params[i].p, curves_params[i].a, curves_params[i].b, curves_params[i].Gx,
curves_params[i].Gy, curves_params[i].order, curves_params[i].cofactor);
ok = 1;
break;
}
}
if (ok == 0) {
return NULL;
}
return ret;
}
/** Set up parameters for a ec_group from Big number mpz_t
* \param ec pointer to the ec_group structure
* \param name pointer to a constant string
* \param field mpz_t big number, the characteristic of the finite field
* \param a, b mpz_t big number, the curve parameters of the Weierstrass form
* \param Gx, Gy coordinates of the generator
* \param order the order of subgroup of points used on the curve
* \param cofactor the cofactor of the curve
*
* \return pointer to a ec_group structure or NULL if an error occurred
*
*/
void ec_group_set_mpz(ec_group ret, const char* name, mpz_t field, mpz_t a, mpz_t b, mpz_t Gx, mpz_t Gy, mpz_t order, mpz_t cofactor) {
int length = strlen(name);
ret->curve_name = (char*)malloc(sizeof(char) * (length + 1));
assert(ret->curve_name != NULL);
ret->curve_name[length] = '\0';
strcpy(ret->curve_name, name);
mpz_init_set(ret->field, field);
mpz_init_set(ret->A, a);
mpz_init_set(ret->B, b);
mpz_init_set(ret->order, order);
mpz_init_set(ret->cofactor, cofactor);
ret->generator = ec_point_init_set_mpz(Gx, Gy);
gmp_printf("\n We work on curve E: y^2 = x^3 + %Zd x + %Zd over finite field F_p: %Zd \n", a, b, field);
printf("The size of the finite field F_p = %d bits \n", bitlength(field));
gmp_printf("The order of the curve E = %Zd \n", order);
}
/** Initialize and assign an elliptic curve from big integers
* \param ec pointer to the ec_group structure
* \param name pointer to a constant string
* \param field mpz_t big number, the characteristic of the finite field
* \param a, b mpz_t big number, the curve parameters of the Weierstrass form
* \param Gx, Gy coordinates of the generator
* \param order the order of subgroup of points used on the curve
* \param cofactor the cofactor of the curve
*
* \return pointer to a ec_group structure or NULL if an error occurred
*/
ec_group ec_group_init_set_mpz(const char* name, mpz_t field, mpz_t a, mpz_t b, mpz_t Gx, mpz_t Gy, mpz_t order, mpz_t cofactor) {
ec_group ret;
ret = malloc(sizeof(struct ec_group_st));
assert(ret != NULL);
int length = strlen(name);
ret->curve_name = (char*)malloc(sizeof(char) * (length + 1));
assert(ret->curve_name != NULL);
ret->curve_name[length] = '\0';
strcpy(ret->curve_name, name);
mpz_init_set(ret->field, field);
mpz_init_set(ret->A, a);
mpz_init_set(ret->B, b);
mpz_init_set(ret->order, order);
mpz_init_set(ret->cofactor, cofactor);
ret->generator = ec_point_init_set_mpz(Gx, Gy);
gmp_printf("\n We work on curve E: y^2 = x^3 + %Zd x + %Zd over finite field F_p: %Zd \n", a, b, field);
printf("The size of the finite field F_p = %d bits \n", bitlength(field));
gmp_printf("The order of the curve E = %Zd \n", order);
return ret;
}
/*
* Initialize and set parameters of a curve from strings
*
* \param ec pointer to the ec_group structure
* \param name pointer to a constant string
* \param field mpz_t big number, the characteristic of the finite field
* \param a, b mpz_t big number, the curve parameters of the Weierstrass form
* \param Gx, Gy coordinates of the generator
* \param order the order of subgroup of points used on the curve
* \param cofactor the cofactor of the curve
*
* \return pointer to a ec_group structure or NULL if an error occurred
*/
ec_group ec_group_init_set_str(const char* name, const char* field, const char* a, const char* b,
const char* Gx, const char* Gy, const char* order, const char* cofactor,
int base) {
ec_group ret;
ret = malloc(sizeof(struct ec_group_st));
assert(ret != NULL);
int len = strlen(name);
ret->curve_name = (char*)malloc(sizeof(char) * (len + 1));
assert(ret->curve_name != NULL);
ret->curve_name[len] = '\0';
strcpy(ret->curve_name, name);
mpz_init_set_str(ret->field, field, base);
mpz_init_set_str(ret->A, a, base);
mpz_init_set_str(ret->B, b, base);
ret->generator = ec_point_init_set_str(Gx, Gy, base);
mpz_init_set_str(ret->order, order, base);
mpz_init_set_str(ret->cofactor, cofactor, base);
return ret;
}
/*
* Assign parameters of a curve from strings
*
* \param ec pointer to the ec_group structure
* \param name pointer to a constant string
* \param field mpz_t big number, the characteristic of the finite field
* \param a, b mpz_t big number, the curve parameters of the Weierstrass form
* \param Gx, Gy coordinates of the generator
* \param order the order of subgroup of points used on the curve
* \param cofactor the cofactor of the curve
*
* \return pointer to a ec_group structure or NULL if an error occurred
*/
void ec_group_set_str(ec_group ret, const char* name, const char* field, const char* a, const char* b,
const char* Gx, const char* Gy, const char* order, const char* cofactor,
int base) {
int len = strlen(name);
ret->curve_name = (char*)malloc(sizeof(char) * (len + 1));
assert(ret->curve_name != NULL);
ret->curve_name[len] = '\0';
strcpy(ret->curve_name, name);
mpz_init_set_str(ret->field, field, base);
mpz_init_set_str(ret->A, a, base);
mpz_init_set_str(ret->B, b, base);
ret->generator = ec_point_init_set_str(Gx, Gy, base);
mpz_init_set_str(ret->order, order, base);
mpz_init_set_str(ret->cofactor, cofactor, base);
}
/*
* Initialize and assign parameters of a curve from strings in hex
*
* \param ec pointer to the ec_group structure
* \param name pointer to a constant string
* \param field mpz_t big number, the characteristic of the finite field
* \param a, b mpz_t big number, the curve parameters of the Weierstrass form
* \param Gx, Gy coordinates of the generator
* \param order the order of subgroup of points used on the curve
* \param cofactor the cofactor of the curve
*
* \return pointer to a ec_group structure or NULL if an error occurred
*/
ec_group ec_group_init_set_str_hex(const char* name, const char* field, const char* a, const char* b,
const char* Gx, const char* Gy, const char* order, const char* cofactor) {
return ec_group_init_set_str(name, field, a, b, Gx, Gy, order, cofactor, 16);
}
/*
* Assign parameters of a curve from strings in hex
*
* \param ec pointer to the ec_group structure
* \param name pointer to a constant string
* \param field mpz_t big number, the characteristic of the finite field
* \param a, b mpz_t big number, the curve parameters of the Weierstrass form
* \param Gx, Gy coordinates of the generator
* \param order the order of subgroup of points used on the curve
* \param cofactor the cofactor of the curve
*
* \return pointer to a ec_group structure or NULL if an error occurred
*/
void ec_group_set_str_hex(ec_group ret, const char* name, const char* field, const char* a, const char* b,
const char* Gx, const char* Gy, const char* order, const char* cofactor) {
ec_group_set_str(ret, name, field, a, b, Gx, Gy, order, cofactor, 16);
}