-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpublic.c
485 lines (420 loc) · 11.4 KB
/
public.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* Copyright (C) lnwdl ([email protected])
* All rights reserved.
*
* Define && Implement some public macros && funcs.
*/
#include <stdio.h>
#include <string.h>
#include "public.h"
#include "evp_sm3.h"
void ShwHexBuf(const void *in, const size_t ilen)
{
const unsigned char *buf = (const unsigned char *)in;
size_t i, j;
if (!buf || !ilen) {
ERROR_MSG("parameters ERROR\n");
return;
}
printf("========== [%d] ==========\n", ilen);
for (i = 0, j = 0; i < ilen; ++i, ++j) {
if (!(j % 16)) {
if (j) {
printf("\n");
}
printf("%08x: ", j);
}
printf("%02x ", buf[i]);
}
printf("\n====================\n");
fflush(stdout);
}
/* just for test, do not use it in release version
* becase there is a static variable.
* */
char *hex2oct(const unsigned char *in, const size_t ilen)
{
size_t i;
static char buf[512];
for (i = 0; i < ilen && i < sizeof (buf); ++i) {
sprintf(buf + i * 2, "%02x", in[i]);
}
return buf;
}
/* In GM specification, all BIGNUM must be extend */
int BN_bn2bin_gm(BIGNUM *a, unsigned char *out, const int degree)
{
int bbytes, dbytes, padlen;
/* if a is 0, we also need to extend it */
//if (!a || !out || BN_is_zero(a)) {
if (!a || !out) {
return 0;
}
bbytes = (BN_num_bits(a) + 7) / 8 ;
dbytes = (degree + 7) / 8;
if (dbytes <= bbytes) {
return BN_bn2bin(a, out);
}
padlen = dbytes - bbytes;
memset(out, 0, padlen);
return BN_bn2bin(a, out + padlen) + padlen;
}
/* from GM/T 003.1-2012 4.2.5/4.2.6 */
int getUserExtInfo(EC_KEY *eckey,
const EC_POINT *pub, const EVP_MD *md,
const unsigned char *ID, const size_t IDlen,
unsigned char *out, size_t *outlen)
{
EVP_MD_CTX mdctx;
BN_CTX *bnctx = NULL;
const EC_GROUP *group;
const EC_POINT *G;
BIGNUM *p, *a, *b, *Gx, *Gy, *Px, *Py;
unsigned char ENTL[2];
unsigned char bin[512]; /* long enough to hold curve point coordinate */
unsigned int blen;
int degree, ret = 0;
if (!eckey || !pub || !md || !out || !outlen
|| *outlen < (size_t)EVP_MD_size(md)) {
goto error;
}
ENTL[1] = (IDlen * 8) & 0xff;
ENTL[0] = ((IDlen * 8) >> 8) & 0xff;
EVP_MD_CTX_init(&mdctx);
EVP_DigestInit(&mdctx, md);
EVP_DigestUpdate(&mdctx, ENTL, sizeof (ENTL));
EVP_DigestUpdate(&mdctx, ID, IDlen);
group = EC_KEY_get0_group(eckey);
if (!group) {
goto error;
}
G = EC_GROUP_get0_generator(group);
if (!G) {
goto error;
}
bnctx = BN_CTX_new();
if (!bnctx) {
goto error;
}
BN_CTX_start(bnctx);
p = BN_CTX_get(bnctx);
a = BN_CTX_get(bnctx);
b = BN_CTX_get(bnctx);
Gx = BN_CTX_get(bnctx);
Gy = BN_CTX_get(bnctx);
Px = BN_CTX_get(bnctx);
Py = BN_CTX_get(bnctx);
if (!p || !a || !b || !Gx || !Gy || !Px || !Py) {
goto error;
}
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group))
== NID_X9_62_prime_field) {
if (!EC_GROUP_get_curve_GFp(group, p, a, b, NULL)) {
goto error;
}
if (!EC_POINT_get_affine_coordinates_GFp(group, G, Gx, Gy, NULL)) {
goto error;
}
if (!EC_POINT_get_affine_coordinates_GFp(group, pub, Px, Py, NULL)) {
goto error;
}
} else {
#ifndef OPENSSL_NO_EC2M
if (!EC_GROUP_get_curve_GF2m(group, p, a, b, NULL)) {
goto error;
}
if (!EC_POINT_get_affine_coordinates_GF2m(group, G, Gx, Gy, NULL)) {
goto error;
}
if (!EC_POINT_get_affine_coordinates_GF2m(group, pub, Px, Py, NULL)) {
goto error;
}
#else
goto error;
#endif
}
degree = EC_GROUP_get_degree(group);
blen = BN_bn2bin_gm(a, bin, degree);
EVP_DigestUpdate(&mdctx, bin, blen);
blen = BN_bn2bin_gm(b, bin, degree);
EVP_DigestUpdate(&mdctx, bin, blen);
blen = BN_bn2bin_gm(Gx, bin, degree);
EVP_DigestUpdate(&mdctx, bin, blen);
blen = BN_bn2bin_gm(Gy, bin, degree);
EVP_DigestUpdate(&mdctx, bin, blen);
blen = BN_bn2bin_gm(Px, bin, degree);
EVP_DigestUpdate(&mdctx, bin, blen);
blen = BN_bn2bin_gm(Py, bin, degree);
EVP_DigestUpdate(&mdctx, bin, blen);
EVP_DigestFinal(&mdctx, out, outlen);
ret = 1;
error:
EVP_MD_CTX_cleanup(&mdctx);
if (bnctx) {
BN_CTX_end(bnctx);
BN_CTX_free(bnctx);
}
return ret;
}
void BNPrintf(const BIGNUM *bn)
{
char *p = NULL;
p = BN_bn2hex(bn);
fprintf(stdout, "0x%s\n", p);
OPENSSL_free(p);
}
/* come from GM/T 0009-2012 10 */
char *ID_default = "1234567812345678";
const char *ID_set = NULL;
char *getDefID(void)
{
if (ID_set) {
return (char *)ID_set;
}
return ID_default;
}
void setDefID(const char *id)
{
ID_set = id;
}
/* used in sm2 test */
EC_GROUP *sm2_create_group(const EC_METHOD *meth, const char *in_p,
const char *in_a, const char *in_b,
const char *in_gx, const char *in_gy,
const char *in_o, const char *in_cf)
{
EC_GROUP *group = NULL;
BIGNUM *p = NULL, *a = NULL, *b = NULL,
*Gx = NULL, *Gy = NULL, *order = NULL, *cofactor = NULL;
EC_POINT *generator = NULL;
int ret = 0, is_prime;
is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field);
group = EC_GROUP_new(meth);
if (!group) {
ERROR_MSG("EC_GROUP_new ERROR\n");
goto error;
}
p = BN_new();
a = BN_new();
b = BN_new();
if (!p || !a || !b) {
ERROR_MSG("BN_new ERROR\n");
goto error;
}
BN_hex2bn(&p, in_p);
BN_hex2bn(&a, in_a);
BN_hex2bn(&b, in_b);
if (is_prime) {
if (!EC_GROUP_set_curve_GFp(group, p, a, b, NULL)) {
ERROR_MSG("EC_GROUP_set_curve_GFp ERROR\n");
goto error;
}
} else {
# ifndef OPENSSL_NO_EC2M
if (!EC_GROUP_set_curve_GF2m(group, p, a, b, NULL)) {
ERROR_MSG("EC_GROUP_set_curve_GFp ERROR\n");
goto error;
}
# else
ERROR_MSG("OPENSSL_NO_EC2M ERROR\n");
goto error;
# endif
}
generator = EC_POINT_new(group);
if (!generator) {
ERROR_MSG("EC_POINT_new ERROR\n");
goto error;
}
Gx = BN_new();
Gy = BN_new();
order = BN_new();
cofactor = BN_new();
if (!Gx || !Gy || !order || !cofactor) {
ERROR_MSG("BN_new ERROR\n");
goto error;
}
BN_hex2bn(&Gx, in_gx);
BN_hex2bn(&Gy, in_gy);
BN_hex2bn(&order, in_o);
if (is_prime) {
if (!EC_POINT_set_affine_coordinates_GFp(group,
generator, Gx, Gy, NULL)) {
ERROR_MSG("EC_POINT_set_affine_coordinates_GFp ERROR\n");
goto error;
}
} else {
# ifndef OPENSSL_NO_EC2M
if (!EC_POINT_set_affine_coordinates_GF2m(group,
generator, Gx, Gy, NULL)) {
ERROR_MSG("EC_POINT_set_affine_coordinates_GFp ERROR\n");
goto error;
}
# else
ERROR_MSG("OPENSSL_NO_EC2M ERROR\n");
goto error;
# endif
}
if (EC_POINT_is_on_curve(group, generator, NULL) <= 0) {
ERROR_MSG("EC_POINT_is_on_curve ERROR\n");
goto error;
}
BN_hex2bn(&cofactor, in_cf);
if (!EC_GROUP_set_generator(group, generator, order, cofactor)) {
ERROR_MSG("EC_GROUP_set_generator ERROR\n");
goto error;
}
#ifdef SHOW_DEBUG_PUB
DEBUG_MSG("Chinese SM2 ec curve (%s) attribites are:\n",
is_prime ? "Fp" : "F2m");
DEBUG_MSG("degree is %d\n", EC_GROUP_get_degree(group));
PrintBN(p);
PrintBN(a);
PrintBN(b);
PrintBN(order);
PrintBN(cofactor);
DEBUG_MSG(" -- Generator:\n");
PrintBN(Gx);
PrintBN(Gy);
#endif
ret = 1;
error:
BN_free(p);
BN_free(a);
BN_free(b);
EC_POINT_free(generator);
BN_free(Gx);
BN_free(Gy);
BN_free(order);
BN_free(cofactor);
if (!ret) {
EC_GROUP_free(group);
group = NULL;
}
return group;
}
/* used in sm2 test */
EC_KEY *sm2_create_eckey(const EC_GROUP *group, const char *in_pri,
const char *in_pubx, const char *in_puby)
{
EC_KEY *eckey = NULL;
BIGNUM *pri = NULL;
BIGNUM *x = NULL, *y = NULL;
EC_POINT *pub = NULL;
int ret = -1, is_prime;
is_prime = (EC_METHOD_get_field_type(EC_GROUP_method_of(group))
== NID_X9_62_prime_field);
eckey = EC_KEY_new();
if (!eckey) {
ERROR_MSG("EC_KEY_new ERROR\n");
goto error;
}
if (!EC_KEY_set_group(eckey, group)) {
ERROR_MSG("EC_KEY_set_group ERROR\n");
goto error;
}
x = BN_new();
y = BN_new();
if (!x || !y) {
ERROR_MSG("BN_new ERROR\n");
goto error;
}
pri = BN_new();
BN_hex2bn(&pri, in_pri);
BN_hex2bn(&x, in_pubx);
BN_hex2bn(&y, in_puby);
pub = EC_POINT_new(group);
if (is_prime) {
if (!EC_POINT_set_affine_coordinates_GFp(group, pub, x, y, NULL)) {
ERROR_MSG("EC_POINT_set_affine_coordinates_GFp ERROR\n");
goto error;
}
} else {
# ifndef OPENSSL_NO_EC2M
if (!EC_POINT_set_affine_coordinates_GF2m(group, pub, x, y, NULL)) {
ERROR_MSG("EC_POINT_set_affine_coordinates_GFp ERROR\n");
goto error;
}
# else
ERROR_MSG("OPENSSL_NO_EC2M ERROR\n");
goto error;
# endif
}
if (!EC_KEY_set_private_key(eckey, pri)) {
ERROR_MSG("EC_KEY_set_private_key ERROR\n");
goto error;
}
if (!EC_KEY_set_public_key(eckey, pub)) {
ERROR_MSG("EC_KEY_set_public_key ERROR\n");
goto error;
}
if (!EC_KEY_check_key(eckey)) {
ERROR_MSG("EC_KEY_check_key ERROR\n");
goto error;
}
#ifdef SHOW_DEBUG_PUB
DEBUG_MSG("Private Key:\n");
PrintBN(pri);
DEBUG_MSG("Public Key:\n");
PrintBN(x);
PrintBN(y);
#endif
ret = 0;
error:
if (pri) BN_free(pri);
if (x) BN_free(x);
if (y) BN_free(y);
if (pub) EC_POINT_free(pub);
if (ret) {
if (eckey) EC_KEY_free(eckey);
eckey = NULL;
}
return eckey;
}
#define ECDH_KDF_MAX (1 << 30)
int myECDH_KDF_X9_62(unsigned char *out, size_t outlen,
const unsigned char *Z, size_t Zlen,
const unsigned char *sinfo, size_t sinfolen,
const EVP_MD *md)
{
EVP_MD_CTX mctx;
int rv = 0;
unsigned int i;
size_t mdlen;
unsigned char ctr[4];
if (sinfolen > ECDH_KDF_MAX || outlen > ECDH_KDF_MAX
|| Zlen > ECDH_KDF_MAX)
return 0;
mdlen = EVP_MD_size(md);
EVP_MD_CTX_init(&mctx);
for (i = 1;; i++) {
unsigned char mtmp[EVP_MAX_MD_SIZE];
EVP_DigestInit_ex(&mctx, md, NULL);
ctr[3] = i & 0xFF;
ctr[2] = (i >> 8) & 0xFF;
ctr[1] = (i >> 16) & 0xFF;
ctr[0] = (i >> 24) & 0xFF;
if (!EVP_DigestUpdate(&mctx, Z, Zlen))
goto err;
if (!EVP_DigestUpdate(&mctx, ctr, sizeof(ctr)))
goto err;
if (!EVP_DigestUpdate(&mctx, sinfo, sinfolen))
goto err;
if (outlen >= mdlen) {
if (!EVP_DigestFinal(&mctx, out, NULL))
goto err;
outlen -= mdlen;
if (outlen == 0)
break;
out += mdlen;
} else {
if (!EVP_DigestFinal(&mctx, mtmp, NULL))
goto err;
memcpy(out, mtmp, outlen);
OPENSSL_cleanse(mtmp, mdlen);
break;
}
}
rv = 1;
err:
EVP_MD_CTX_cleanup(&mctx);
return rv;
}