Newer
Older
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "ecc_api.h"
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/ec.h>
#include <openssl/pem.h>
#include <openssl/sha.h>
#include <openssl/ecdsa.h>
#include <string.h>
static const int _NIDS[] = {
NID_secp256k1,
NID_secp256k1
};
static EVP_PKEY_CTX *pctx[sizeof(_NIDS) / sizeof(_NIDS[0])] = { NULL };
static EVP_PKEY_CTX *kctx[sizeof(_NIDS) / sizeof(_NIDS[0])] = { NULL };
static EVP_PKEY *params[sizeof(_NIDS) / sizeof(_NIDS[0])] = { NULL };
int ecc_api_init()
{
int i;
int rc = -1;
for (i = 0; i < sizeof(_NIDS) / sizeof(_NIDS[0]); i++){
/* Create the context for generating the parameters */
pctx[i] = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
if (pctx[i]){
if (EVP_PKEY_paramgen_init(pctx[i])){
/* Set the paramgen parameters according to the type */
if (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx[i], _NIDS[i])){
/* Generate parameters */
if (EVP_PKEY_paramgen(pctx[i], ¶ms[i])) {
/* Create context for the key generation */
kctx[i] = EVP_PKEY_CTX_new(params[i], NULL);
if (kctx[i]){
if (EVP_PKEY_keygen_init(kctx[i])){
rc = 0;
continue;
}
}
}
}
}
}
ERR_print_errors_fp(stderr);
if (kctx[i])EVP_PKEY_CTX_free(kctx[i]); kctx[i] = NULL;
if (params[i])EVP_PKEY_free(params[i]); params[i] = NULL;
if (pctx[i])EVP_PKEY_CTX_free(pctx[i]); pctx[i] = NULL;
}
return rc;
}
int ecc_api_done()
{
int i;
for (i = 0; i < sizeof(_NIDS) / sizeof(_NIDS[0]); i++){
if (kctx[i])EVP_PKEY_CTX_free(kctx[i]); kctx[i] = NULL;
if (params[i])EVP_PKEY_free(params[i]); params[i] = NULL;
if (pctx[i])EVP_PKEY_CTX_free(pctx[i]); pctx[i] = NULL;
}
return 0;
}
void * ecc_api_key_gen(ecc_pk_algorithm pk_alg, ecc_sym_algorithm sym_alg)
{
EVP_PKEY * key = NULL;
if (!EVP_PKEY_keygen(kctx[pk_alg], &key)){
ERR_print_errors_fp(stderr);
}
return (void*)key;
}
void * ecc_api_key_init(ecc_pk_algorithm pk_alg, ecc_sym_algorithm sym_alg, const char* pkey)
{
EVP_PKEY * key = NULL;
BIGNUM * bn = BN_new();
if (BN_bin2bn(pkey, 32, bn)){
EC_KEY * eckey = EC_KEY_new_by_curve_name(_NIDS[pk_alg]);
if (eckey){
if (EC_KEY_set_private_key(eckey, bn)){
EC_POINT * point;
const EC_GROUP * group;
group = EC_KEY_get0_group(eckey);
point = EC_POINT_new(group);
if (EC_POINT_mul(group, point, bn, NULL, NULL, NULL)){
EC_KEY_set_public_key(eckey, point);
key = EVP_PKEY_new();
EVP_PKEY_set1_EC_KEY(key, eckey);
eckey = NULL;
}
EC_POINT_free(point);
}
if (eckey){
EC_KEY_free(eckey);
}
}
}
BN_free(bn);
return (void*)key;
}
void ecc_api_key_free(void* key)
{
EVP_PKEY_free((EVP_PKEY*)key);
}
int ecc_api_key_private(void* key, char* buf)
{
int len = -1;
const EC_KEY * eckey;
eckey = EVP_PKEY_get1_EC_KEY((EVP_PKEY*)key);
if(eckey){
const BIGNUM * ecbn;
ecbn = EC_KEY_get0_private_key(eckey);
if(ecbn){
len = BN_num_bytes(ecbn);
if(buf){
BN_bn2bin(ecbn, (unsigned char*)buf);
}
}
}
return len;
}
int ecc_api_key_public(void* key, char * px, char * py)
{
const EC_KEY * eckey;
const EC_GROUP * ecgroup;
const EC_POINT * ecpoint;
BIGNUM x, y;
int bcount = -1;
eckey = EVP_PKEY_get1_EC_KEY(key);
ecgroup = EC_KEY_get0_group(eckey);
ecpoint = EC_KEY_get0_public_key(eckey);
//fill public key data
BN_init(&x); BN_init(&y);
if(EC_POINT_get_affine_coordinates_GFp(ecgroup, ecpoint, &x, &y, NULL)){
bcount = BN_num_bytes(&x);
if(px){
BN_bn2bin(&x, (unsigned char*)px);
}
bcount = BN_num_bytes(&y);
if(py){
BN_bn2bin(&x, (unsigned char*)py);
}
}
BN_clear_free(&x); BN_clear_free(&y);
return bcount;
}
static int _pass_cb(char *buf, int size, int rwflag, void *u)
{
fprintf(stderr, "Ask for a pass phrase");
return 0;
}
int ecc_api_key_private_save(void* key, const char* path, ecc_format format)
{
int rc = -1;
FILE * f = fopen(path, "wb");
if(f){
if (format == ecc_pem){
rc = PEM_write_PKCS8PrivateKey(f, key, NULL, NULL, 0, _pass_cb, NULL) ? 0 : -1;
if (rc < 0){
ERR_print_errors_fp(stderr);
}
}
else{
const EC_KEY * eckey;
eckey = EVP_PKEY_get1_EC_KEY((EVP_PKEY*)key);
if (eckey){
const BIGNUM * ecbn;
ecbn = EC_KEY_get0_private_key(eckey);
if (ecbn){
char * buf = NULL;
int len = BN_num_bytes(ecbn);
if (format == ecc_bin){
buf = (char *)OPENSSL_malloc(len);
BN_bn2bin(ecbn, buf);
rc = 0;
}
else if (format == ecc_hex){
buf = BN_bn2hex(ecbn);
len = strlen(buf);
rc = 0;
}
if (buf){
rc = (len == fwrite(buf, 1, len, f)) ? 0 : -1;
OPENSSL_free(buf);
}
}
}
}
fclose(f);
if (rc < 0){
ERR_print_errors_fp(stderr);
remove(path);
rc = -1;
}
}
else{
perror(path);
}
return rc;
}
void * ecc_api_key_private_load(const char* path)
{
EVP_PKEY * key = NULL;
FILE * f = fopen(path, "rb");
if (f){
key = PEM_read_PrivateKey(f, NULL, NULL, NULL);
if (key == NULL){
BIGNUM * bn = NULL;
fseek(f, 0, SEEK_END);
int len = ftell(f);
fseek(f, 0, SEEK_SET);
char * buf = OPENSSL_malloc(len + 1);
if (len == fread(buf, 1, len, f)){
buf[len] = 0;
// try hex first
if (len != BN_hex2bn(&bn, buf)){
if (bn){
BN_free(bn); bn = NULL;
}
bn = BN_bin2bn(buf, len, NULL);
}
}
OPENSSL_free(buf);
if (bn){
EC_KEY * eckey = EC_KEY_new_by_curve_name(_NIDS[0]);
if (eckey){
if (EC_KEY_set_private_key(eckey, bn)){
EC_POINT * point;
const EC_GROUP * group;
group = EC_KEY_get0_group(eckey);
point = EC_POINT_new(group);
if (EC_POINT_mul(group, point, bn, NULL, NULL, NULL)){
EC_KEY_set_public_key(eckey, point);
key = EVP_PKEY_new();
EVP_PKEY_assign_EC_KEY(key, eckey);
eckey = NULL;
}
EC_POINT_free(point);
}
if (eckey)EC_KEY_free(eckey);
}
BN_free(bn);
}
}
fclose(f);
}
return key;
}
int ecc_api_key_public_save(void* key , const char* path, ecc_format format)
{
int rc = -1;
FILE * f = fopen(path, "wb");
if (f){
const EC_KEY * eckey;
eckey = EVP_PKEY_get1_EC_KEY((EVP_PKEY*)key);
if (eckey){
if (format == ecc_pem){
rc = PEM_write_EC_PUBKEY(f, eckey) ? 0 : -1;
}
else{
size_t len;
char * buf = NULL;
const EC_POINT * point = EC_KEY_get0_public_key(eckey);
const EC_GROUP * group = EC_KEY_get0_group(eckey);
if (format == ecc_hex){
buf = EC_POINT_point2hex(group, point, POINT_CONVERSION_UNCOMPRESSED, NULL);
len = strlen(buf);
}
else if (format == ecc_bin){
len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
if (len > 0){
buf = OPENSSL_malloc(len + 1);
if (len != EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED, buf, len, NULL)){
OPENSSL_free(buf); buf = NULL;
}
}
}
if (buf){
if (len == fwrite(buf, 1, len, f)){
rc = 0;
}
OPENSSL_free(buf); buf = NULL;
}
}
}
fclose(f);
if (rc < 0){
ERR_print_errors_fp(stderr);
remove(path);
}
}
else{
perror(path);
}
return rc;
}
void * ecc_api_key_public_load(const char* path, ecc_pk_algorithm pk_alg)
{
EVP_PKEY * key = NULL;
FILE * f = fopen(path, "rb");
if (f){
key = PEM_read_PUBKEY(f, &key, NULL, NULL);
if (key == NULL){
fseek(f, 0, SEEK_END);
int len = ftell(f);
fseek(f, 0, SEEK_SET);
char * buf = OPENSSL_malloc(len + 1);
if (len == fread(buf, 1, len, f)){
buf[len] = 0;
EC_KEY * eckey = EC_KEY_new_by_curve_name(_NIDS[pk_alg]);
if (eckey){
// try load hex
EC_POINT * point = NULL;
const EC_GROUP * group = EC_KEY_get0_group(eckey);
// try hex first
point = EC_POINT_hex2point(group, buf, NULL, NULL);
if (point == NULL){
// try oct
point = EC_POINT_oct2point(group, NULL, buf, len, NULL);
}
if (point){
EC_KEY_set_public_key(eckey, point);
EC_POINT_free(point);
key = EVP_PKEY_new();
EVP_PKEY_assign_EC_KEY(key, eckey);
eckey = NULL;
}
else{
EC_KEY_free(eckey);
}
}
}
OPENSSL_free(buf);
}
fclose(f);
}
return key;
}
int sha256_calculate(char* hash, const char * ptr, int len)
{
SHA256_CTX ctx;
SHA256_Init(&ctx);
SHA256_Update(&ctx, ptr, len);
SHA256_Final((unsigned char*)hash, &ctx);
return 0;
}
int ecc_sign(void * key, const char * data, int length, char ** psig, int maxsize)
{
EC_KEY * eckey;
unsigned char *sig = (unsigned char *)*psig;
if (65 <= maxsize){
eckey = EC_KEY_dup(EVP_PKEY_get1_EC_KEY(key));
if(eckey){
unsigned char hash[32];
ECDSA_SIG * ecdsa;
SHA256_CTX ctx;
BIGNUM *kinv = NULL;
BIGNUM *rp = NULL;
SHA256_Init(&ctx);
SHA256_Update(&ctx, data, length);
SHA256_Final(hash, &ctx);
if (ECDSA_sign_setup(eckey, NULL, &kinv, &rp)){
ecdsa = ECDSA_do_sign_ex(hash, 32, kinv, rp, eckey);
EC_KEY_free(eckey);
BN_clear_free(kinv);
BN_clear_free(rp);
if (ecdsa){
int bcount;
*(sig++) = 0; // x_coordinate_only
bcount = BN_num_bytes(ecdsa->r);
BN_bn2bin(ecdsa->r, sig);
sig += bcount;
bcount = BN_num_bytes(ecdsa->s);
BN_bn2bin(ecdsa->s, sig);
sig += bcount;
ECDSA_SIG_free(ecdsa);
*psig = (char*)sig;
return 0;
}
}
}
}
return -1;
}