-
Notifications
You must be signed in to change notification settings - Fork 6
/
ec_lib.c
141 lines (102 loc) · 2.62 KB
/
ec_lib.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
/*
* ec_lib.c
*
* Created on: Oct 20, 2015
* Author: tslld
*/
#include "ecdsa.h"
#include "ec.h"
#include "ec_point.h"
/** Get/Set the order of the group of points being used on the elliptic curve
* \param ec pointer to an ec_group structure
* \param order BIG number
* \return
*/
void ec_group_get_order(ec_group ec, mpz_t order) {
mpz_set(order, ec->order);
}
void ec_group_set_order(ec_group ec, mpz_t order) {
mpz_set(ec->order, order);
}
/** Get/Set the co-factor the elliptic curve
* \param ec pointer to an ec_group structure
* \param cofactor BIG number
* \return
*/
void ec_group_get_cofactor(ec_group ec, mpz_t cofactor){
mpz_set(cofactor, ec->cofactor);
}
void ec_group_set_cofactor(ec_group ec, mpz_t cofactor){
mpz_set(ec->cofactor, cofactor);
}
/** Get/Set the characteristic of field
* \param ec pointer to an ec_group structure
* \param field BIG number
* \return
*/
void ec_group_get_field(ec_group ec, mpz_t field){
mpz_set(field, ec->field);
}
void ec_group_set_field(ec_group ec, mpz_t field){
mpz_set(ec->field, field);
}
/** Get/Set the parameter A the elliptic curve
* \param ec pointer to an ec_group structure
* \param A BIG number
* \return
*/
void ec_group_get_a(ec_group ec, mpz_t a){
mpz_set(a, ec->A);
}
void ec_group_set_a(ec_group ec, mpz_t a){
mpz_set(ec->A, a);
}
/** Get/Set the parameter B of the elliptic curve
* \param ec pointer to an ec_group structure
* \param B BIG number
* \return
*/
void ec_group_get_b(ec_group ec, mpz_t b){
mpz_set(b, ec->B);
}
void ec_group_set_b(ec_group ec, mpz_t b){
mpz_set(ec->B, b);
}
/** Set the name of a curve
* \param ec pointer to an ec_group structure
* \param name string name
* \return
* */
int ec_group_set_name(ec_group ec, const char* name) {
int ok = 0;
if(name == NULL)
return (ok);
int length = strlen(name);
/* Allocate memory of curve name is NULL */
if (ec->curve_name == NULL) {
ec->curve_name = (char*)malloc(sizeof(char) * (length + 1));
ec->curve_name[length] = '\0';
}
if(ec->curve_name == NULL)
return (ok);
strcpy(ec->curve_name, name);
ok = 1;
return (ok);
}
/* Get the name of the elliptic curve being used */
char* ec_group_get_name(ec_group ec) {
if ((ec == NULL) || (ec->curve_name == NULL)) {
fprintf(stdout, "Cannot get the curve name. Parsing NULL parameters !\n");
return NULL;
}
char* name;
int length = strlen(ec->curve_name);
name = (char*)malloc(sizeof(char) * (length + 1));
if(name == NULL) {
fprintf(stdout, "Couldn't allocate a memory ! \n");
return NULL;
}
name[length] = '\0';
strcpy(name, ec->curve_name);
return name;
}