-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathwd_ecc.c
2420 lines (1970 loc) · 51.7 KB
/
wd_ecc.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: Apache-2.0 */
/*
* Copyright 2020-2021 Huawei Technologies Co.,Ltd. All rights reserved.
* Copyright 2020-2021 Linaro ltd.
*/
#include <dirent.h>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <time.h>
#include <dlfcn.h>
#include "include/drv/wd_ecc_drv.h"
#include "include/wd_ecc_curve.h"
#include "wd_ecc.h"
#define ECC_MAX_HW_BITS 521
#define ECC_MAX_KEY_SIZE BITS_TO_BYTES(ECC_MAX_HW_BITS)
#define ECC_MAX_IN_NUM 4
#define ECC_MAX_OUT_NUM 4
#define CURVE_PARAM_NUM 6
#define ECC_POINT_NUM 2
#define MAX_CURVE_SIZE (ECC_MAX_KEY_SIZE * CURVE_PARAM_NUM)
#define MAX_HASH_LENS ECC_MAX_KEY_SIZE
#define SM2_KEY_SIZE 32
#define GET_NEGATIVE(val) (0 - (val))
#define ZA_PARAM_NUM 6
static __thread __u64 balance;
struct curve_param_desc {
__u32 type;
__u32 pri_offset;
__u32 pub_offset;
};
enum wd_ecc_curve_param_type {
ECC_CURVE_P,
ECC_CURVE_A,
ECC_CURVE_B,
ECC_CURVE_N,
ECC_CURVE_G
};
struct wd_ecc_sess {
__u32 key_size;
struct wd_ecc_key key;
struct wd_ecc_sess_setup setup;
void *sched_key;
};
struct wd_ecc_curve_list {
__u32 id;
const char *name;
__u32 key_bits;
__u8 data[MAX_CURVE_SIZE];
};
static struct wd_ecc_setting {
enum wd_status status;
struct wd_ctx_config_internal config;
struct wd_sched sched;
struct wd_async_msg_pool pool;
struct wd_alg_driver *driver;
void *dlhandle;
void *dlh_list;
} wd_ecc_setting;
struct wd_env_config wd_ecc_env_config;
static struct wd_init_attrs wd_ecc_init_attrs;
static const struct wd_ecc_curve_list curve_list[] = {
/* parameter 3 is key width */
{ WD_X25519, "x25519", 256, X25519_256_PARAM },
{ WD_X448, "x448", 448, X448_448_PARAM },
{ WD_SECP128R1, "secp128r1", 128, SECG_P128_R1_PARAM },
{ WD_SECP192K1, "secp192k1", 192, SECG_P192_K1_PARAM },
{ WD_SECP224R1, "secp224R1", 224, SECG_P224_R1_PARAM },
{ WD_SECP256K1, "secp256k1", 256, SECG_P256_K1_PARAM },
{ WD_BRAINPOOLP320R1, "bpP320r1", 320, BRAINPOOL_P320_R1_PARAM },
{ WD_BRAINPOOLP384R1, "bpP384r1", 384, BRAINPOOL_P384_R1_PARAM },
{ WD_SECP384R1, "secp384r1", 384, SECG_P384_R1_PARAM },
{ WD_SECP521R1, "secp521r1", 521, SECG_P521_R1_PARAM },
{ WD_SM2P256, "sm2", 256, SM2_P256_V1_PARAM }
};
static const struct curve_param_desc curve_pram_list[] = {
{ ECC_CURVE_P, offsetof(struct wd_ecc_prikey, p), offsetof(struct wd_ecc_pubkey, p) },
{ ECC_CURVE_A, offsetof(struct wd_ecc_prikey, a), offsetof(struct wd_ecc_pubkey, a) },
{ ECC_CURVE_B, offsetof(struct wd_ecc_prikey, b), offsetof(struct wd_ecc_pubkey, b) },
{ ECC_CURVE_N, offsetof(struct wd_ecc_prikey, n), offsetof(struct wd_ecc_pubkey, n) },
{ ECC_CURVE_G, offsetof(struct wd_ecc_prikey, g), offsetof(struct wd_ecc_pubkey, g) }
};
static void wd_ecc_close_driver(int init_type)
{
#ifndef WD_STATIC_DRV
if (init_type == WD_TYPE_V2) {
wd_dlclose_drv(wd_ecc_setting.dlh_list);
return;
}
if (!wd_ecc_setting.dlhandle)
return;
wd_release_drv(wd_ecc_setting.driver);
dlclose(wd_ecc_setting.dlhandle);
wd_ecc_setting.dlhandle = NULL;
#else
wd_release_drv(wd_ecc_setting.driver);
hisi_hpre_remove();
#endif
}
static int wd_ecc_open_driver(int init_type)
{
struct wd_alg_driver *driver = NULL;
const char *alg_name = "sm2";
#ifndef WD_STATIC_DRV
char lib_path[PATH_MAX];
int ret;
if (init_type == WD_TYPE_V2) {
/*
* Driver lib file path could set by env param.
* then open them by wd_dlopen_drv()
* default dir in the /root/lib/xxx.so and then dlopen
*/
wd_ecc_setting.dlh_list = wd_dlopen_drv(NULL);
if (!wd_ecc_setting.dlh_list) {
WD_ERR("failed to open driver lib files.\n");
return -WD_EINVAL;
}
return WD_SUCCESS;
}
ret = wd_get_lib_file_path("libhisi_hpre.so", lib_path, false);
if (ret)
return ret;
wd_ecc_setting.dlhandle = dlopen(lib_path, RTLD_NOW);
if (!wd_ecc_setting.dlhandle) {
WD_ERR("failed to open libhisi_hpre.so, %s!\n", dlerror());
return -WD_EINVAL;
}
#else
hisi_hpre_probe();
if (init_type == WD_TYPE_V2)
return WD_SUCCESS;
#endif
driver = wd_request_drv(alg_name, false);
if (!driver) {
wd_ecc_close_driver(WD_TYPE_V1);
WD_ERR("failed to get %s driver support\n", alg_name);
return -WD_EINVAL;
}
wd_ecc_setting.driver = driver;
return WD_SUCCESS;
}
static bool is_alg_support(const char *alg)
{
if (unlikely(strcmp(alg, "ecdh") && strcmp(alg, "ecdsa") &&
strcmp(alg, "x25519") && strcmp(alg, "x448") &&
strcmp(alg, "sm2")))
return false;
return true;
}
static void wd_ecc_clear_status(void)
{
wd_alg_clear_init(&wd_ecc_setting.status);
}
static int wd_ecc_common_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
int ret;
ret = wd_set_epoll_en("WD_ECC_EPOLL_EN",
&wd_ecc_setting.config.epoll_en);
if (ret < 0)
return ret;
ret = wd_init_ctx_config(&wd_ecc_setting.config, config);
if (ret < 0)
return ret;
ret = wd_init_sched(&wd_ecc_setting.sched, sched);
if (ret < 0)
goto out_clear_ctx_config;
ret = wd_init_async_request_pool(&wd_ecc_setting.pool,
config, WD_POOL_MAX_ENTRIES,
sizeof(struct wd_ecc_msg));
if (ret < 0)
goto out_clear_sched;
ret = wd_alg_init_driver(&wd_ecc_setting.config,
wd_ecc_setting.driver);
if (ret)
goto out_clear_pool;
return WD_SUCCESS;
out_clear_pool:
wd_uninit_async_request_pool(&wd_ecc_setting.pool);
out_clear_sched:
wd_clear_sched(&wd_ecc_setting.sched);
out_clear_ctx_config:
wd_clear_ctx_config(&wd_ecc_setting.config);
return ret;
}
static int wd_ecc_common_uninit(void)
{
enum wd_status status;
wd_alg_get_init(&wd_ecc_setting.status, &status);
if (status == WD_UNINIT)
return -WD_EINVAL;
/* uninit async request pool */
wd_uninit_async_request_pool(&wd_ecc_setting.pool);
/* unset config, sched, driver */
wd_clear_sched(&wd_ecc_setting.sched);
wd_alg_uninit_driver(&wd_ecc_setting.config,
wd_ecc_setting.driver);
return WD_SUCCESS;
}
int wd_ecc_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
int ret;
pthread_atfork(NULL, NULL, wd_ecc_clear_status);
ret = wd_alg_try_init(&wd_ecc_setting.status);
if (ret)
return ret;
ret = wd_init_param_check(config, sched);
if (ret)
goto out_clear_init;
ret = wd_ecc_open_driver(WD_TYPE_V1);
if (ret)
goto out_clear_init;
ret = wd_ecc_common_init(config, sched);
if (ret)
goto out_close_driver;
wd_alg_set_init(&wd_ecc_setting.status);
return WD_SUCCESS;
out_close_driver:
wd_ecc_close_driver(WD_TYPE_V1);
out_clear_init:
wd_alg_clear_init(&wd_ecc_setting.status);
return ret;
}
void wd_ecc_uninit(void)
{
int ret;
ret = wd_ecc_common_uninit();
if (ret)
return;
wd_ecc_close_driver(WD_TYPE_V1);
wd_alg_clear_init(&wd_ecc_setting.status);
}
int wd_ecc_init2_(char *alg, __u32 sched_type, int task_type, struct wd_ctx_params *ctx_params)
{
struct wd_ctx_nums ecc_ctx_num[WD_EC_OP_MAX] = {0};
struct wd_ctx_params ecc_ctx_params = {0};
int state, ret = -WD_EINVAL;
bool flag;
pthread_atfork(NULL, NULL, wd_ecc_clear_status);
state = wd_alg_try_init(&wd_ecc_setting.status);
if (state)
return state;
if (!alg || sched_type >= SCHED_POLICY_BUTT ||
task_type < 0 || task_type >= TASK_MAX_TYPE) {
WD_ERR("invalid: input param is wrong!\n");
goto out_clear_init;
}
flag = is_alg_support(alg);
if (!flag) {
WD_ERR("invalid: alg %s not support!\n", alg);
goto out_clear_init;
}
state = wd_ecc_open_driver(WD_TYPE_V2);
if (state)
goto out_clear_init;
while (ret) {
memset(&wd_ecc_setting.config, 0, sizeof(struct wd_ctx_config_internal));
/* Get alg driver and dev name */
wd_ecc_setting.driver = wd_alg_drv_bind(task_type, alg);
if (!wd_ecc_setting.driver) {
WD_ERR("failed to bind a valid driver!\n");
ret = -WD_EINVAL;
goto out_dlopen;
}
ecc_ctx_params.ctx_set_num = ecc_ctx_num;
ret = wd_ctx_param_init(&ecc_ctx_params, ctx_params,
wd_ecc_setting.driver, WD_ECC_TYPE, WD_EC_OP_MAX);
if (ret) {
if (ret == -WD_EAGAIN) {
wd_disable_drv(wd_ecc_setting.driver);
wd_alg_drv_unbind(wd_ecc_setting.driver);
continue;
}
goto out_driver;
}
wd_ecc_init_attrs.alg = alg;
wd_ecc_init_attrs.sched_type = sched_type;
wd_ecc_init_attrs.driver = wd_ecc_setting.driver;
wd_ecc_init_attrs.ctx_params = &ecc_ctx_params;
wd_ecc_init_attrs.alg_init = wd_ecc_common_init;
wd_ecc_init_attrs.alg_poll_ctx = wd_ecc_poll_ctx;
ret = wd_alg_attrs_init(&wd_ecc_init_attrs);
if (ret) {
if (ret == -WD_ENODEV) {
wd_disable_drv(wd_ecc_setting.driver);
wd_alg_drv_unbind(wd_ecc_setting.driver);
wd_ctx_param_uninit(&ecc_ctx_params);
continue;
}
WD_ERR("failed to init alg attrs!\n");
goto out_params_uninit;
}
}
wd_alg_set_init(&wd_ecc_setting.status);
wd_ctx_param_uninit(&ecc_ctx_params);
return WD_SUCCESS;
out_params_uninit:
wd_ctx_param_uninit(&ecc_ctx_params);
out_driver:
wd_alg_drv_unbind(wd_ecc_setting.driver);
out_dlopen:
wd_ecc_close_driver(WD_TYPE_V2);
out_clear_init:
wd_alg_clear_init(&wd_ecc_setting.status);
return ret;
}
void wd_ecc_uninit2(void)
{
int ret;
ret = wd_ecc_common_uninit();
if (ret)
return;
wd_alg_attrs_uninit(&wd_ecc_init_attrs);
wd_alg_drv_unbind(wd_ecc_setting.driver);
wd_ecc_close_driver(WD_TYPE_V2);
wd_ecc_setting.dlh_list = NULL;
wd_alg_clear_init(&wd_ecc_setting.status);
}
static int trans_to_binpad(char *dst, const char *src,
int b_size, int d_size, const char *p_name)
{
int i = d_size - 1;
int j;
if (unlikely(!dst || !src || !b_size || !d_size || b_size < d_size)) {
WD_ERR("invalid: %s trans to hpre bin params err!\n", p_name);
return -WD_EINVAL;
}
if (dst == src)
return WD_SUCCESS;
for (j = b_size - 1; j >= 0; j--, i--) {
if (i >= 0)
dst[j] = src[i];
else
dst[j] = 0;
}
return WD_SUCCESS;
}
static __u32 get_key_bsz(__u32 ksz)
{
__u32 size = 0;
/* key width */
if (ksz <= BITS_TO_BYTES(256))
size = BITS_TO_BYTES(256);
else if (ksz <= BITS_TO_BYTES(384))
size = BITS_TO_BYTES(384);
else if (ksz <= BITS_TO_BYTES(576))
size = BITS_TO_BYTES(576);
else
WD_ERR("invalid: get key size %u is error!\n", ksz);
return size;
}
static __u32 get_hash_bytes(__u8 type)
{
__u32 val = 0;
switch (type) {
case WD_HASH_MD4:
case WD_HASH_MD5:
val = BITS_TO_BYTES(128); /* output width */
break;
case WD_HASH_SHA1:
val = BITS_TO_BYTES(160);
break;
case WD_HASH_SHA224:
val = BITS_TO_BYTES(224);
break;
case WD_HASH_SHA256:
case WD_HASH_SM3:
val = BITS_TO_BYTES(256);
break;
case WD_HASH_SHA384:
val = BITS_TO_BYTES(384);
break;
case WD_HASH_SHA512:
val = BITS_TO_BYTES(512);
break;
default:
break;
}
return val;
}
static void init_dtb_param(void *dtb, char *str,
__u32 dsz, __u32 bsz, __u32 num)
{
struct wd_dtb *tmp = dtb;
char *start = str;
__u32 i = 0;
while (i++ < num) {
tmp->data = start;
tmp->dsize = dsz;
tmp->bsize = bsz;
tmp += 1;
start += bsz;
}
}
static void init_ecc_prikey(struct wd_ecc_prikey *prikey,
__u32 ksz, __u32 bsz)
{
init_dtb_param(prikey, prikey->data, ksz, bsz, ECC_PRIKEY_PARAM_NUM);
}
static void init_ecc_pubkey(struct wd_ecc_pubkey *pubkey,
__u32 ksz, __u32 bsz)
{
init_dtb_param(pubkey, pubkey->data, ksz, bsz, ECC_PUBKEY_PARAM_NUM);
}
static void release_ecc_prikey(struct wd_ecc_sess *sess)
{
struct wd_ecc_prikey *prikey = sess->key.prikey;
wd_memset_zero(prikey->data, prikey->size);
free(prikey->data);
free(prikey);
sess->key.prikey = NULL;
}
static void release_ecc_pubkey(struct wd_ecc_sess *sess)
{
struct wd_ecc_pubkey *pubkey = sess->key.pubkey;
free(pubkey->data);
free(pubkey);
sess->key.pubkey = NULL;
}
static struct wd_ecc_prikey *create_ecc_prikey(struct wd_ecc_sess *sess)
{
struct wd_ecc_prikey *prikey;
__u32 hsz, dsz;
void *data;
hsz = get_key_bsz(sess->key_size);
prikey = malloc(sizeof(struct wd_ecc_prikey));
if (!prikey) {
WD_ERR("failed to malloc prikey!\n");
return NULL;
}
dsz = ECC_PRIKEY_SZ(hsz);
data = malloc(dsz);
if (!data) {
WD_ERR("failed to malloc prikey data, sz = %u!\n", dsz);
free(prikey);
return NULL;
}
memset(data, 0, dsz);
prikey->size = dsz;
prikey->data = data;
init_ecc_prikey(prikey, sess->key_size, hsz);
return prikey;
}
static struct wd_ecc_pubkey *create_ecc_pubkey(struct wd_ecc_sess *sess)
{
struct wd_ecc_pubkey *pubkey;
__u32 hsz, dsz;
void *data;
hsz = get_key_bsz(sess->key_size);
pubkey = malloc(sizeof(struct wd_ecc_pubkey));
if (!pubkey) {
WD_ERR("failed to malloc pubkey!\n");
return NULL;
}
dsz = ECC_PUBKEY_SZ(hsz);
data = malloc(dsz);
if (!data) {
WD_ERR("failed to malloc pubkey data, sz = %u!\n", dsz);
free(pubkey);
return NULL;
}
memset(data, 0, dsz);
pubkey->size = dsz;
pubkey->data = data;
init_ecc_pubkey(pubkey, sess->key_size, hsz);
return pubkey;
}
static void release_ecc_in(struct wd_ecc_sess *sess,
struct wd_ecc_in *ecc_in)
{
wd_memset_zero(ecc_in->data, ecc_in->size);
free(ecc_in);
}
static struct wd_ecc_in *create_ecc_in(struct wd_ecc_sess *sess, __u32 num)
{
struct wd_ecc_in *in;
__u32 hsz, len;
if (!sess->key_size || sess->key_size > ECC_MAX_KEY_SIZE) {
WD_ERR("invalid: sess key size %u is error!\n", sess->key_size);
return NULL;
}
hsz = get_key_bsz(sess->key_size);
len = sizeof(struct wd_ecc_in) + hsz * num;
in = malloc(len);
if (!in) {
WD_ERR("failed to malloc ecc in, sz = %u!\n", len);
return NULL;
}
memset(in, 0, len);
in->size = hsz * num;
init_dtb_param(in, in->data, sess->key_size, hsz, num);
return in;
}
static struct wd_ecc_in *create_sm2_sign_in(struct wd_ecc_sess *sess,
__u64 m_len)
{
struct wd_dtb *dgst, *k, *plaintext;
__u32 ksz = sess->key_size;
struct wd_ecc_in *in;
__u64 len;
if (ksz != SM2_KEY_SIZE) {
WD_ERR("invalid: sess key size %u is error!\n", ksz);
return NULL;
}
len = sizeof(struct wd_ecc_in)
+ ECC_SIGN_IN_PARAM_NUM * ksz + m_len;
in = malloc(len);
if (!in) {
WD_ERR("failed to malloc sm2 sign in, sz = %llu!\n", len);
return NULL;
}
memset(in, 0, len);
in->size = len - sizeof(struct wd_ecc_in);
dgst = (void *)in;
dgst->data = in->data;
dgst->dsize = ksz;
dgst->bsize = ksz;
k = dgst + 1;
k->data = dgst->data + ksz;
k->dsize = ksz;
k->bsize = ksz;
plaintext = k + 1;
plaintext->data = k->data + ksz;
plaintext->dsize = m_len;
plaintext->bsize = m_len;
return in;
}
static struct wd_ecc_in *create_sm2_enc_in(struct wd_ecc_sess *sess,
__u64 m_len)
{
struct wd_dtb *k, *plaintext;
__u32 ksz = sess->key_size;
struct wd_ecc_in *in;
__u64 len;
if (ksz != SM2_KEY_SIZE) {
WD_ERR("invalid: sess key size %u is error!\n", sess->key_size);
return NULL;
}
len = sizeof(struct wd_ecc_in) + ksz + m_len;
in = malloc(len);
if (!in) {
WD_ERR("failed to malloc sm2 enc in, sz = %llu!\n", len);
return NULL;
}
memset(in, 0, len);
in->size = ksz + m_len;
k = (void *)in;
k->data = in->data;
k->dsize = ksz;
k->bsize = ksz;
plaintext = k + 1;
plaintext->data = k->data + ksz;
plaintext->dsize = m_len;
plaintext->bsize = m_len;
return in;
}
static void *create_sm2_ciphertext(struct wd_ecc_sess *sess, __u32 m_len,
__u64 *len, __u32 st_sz)
{
struct wd_hash_mt *hash = &sess->setup.hash;
__u32 ksz = sess->key_size;
struct wd_ecc_point *c1;
struct wd_dtb *c3, *c2;
__u32 h_byts;
void *start;
if (unlikely(ksz != SM2_KEY_SIZE)) {
WD_ERR("invalid: sess key size %u is error!\n", ksz);
return NULL;
}
h_byts = get_hash_bytes(hash->type);
if (!h_byts) {
WD_ERR("failed to get hash bytes, type = %u!\n", hash->type);
return NULL;
}
*len = (__u64)st_sz + ECC_POINT_PARAM_NUM * (__u64)sess->key_size +
(__u64)m_len + (__u64)h_byts;
start = malloc(*len);
if (unlikely(!start)) {
WD_ERR("failed to alloc start, sz = %llu!\n", *len);
return NULL;
}
c1 = (struct wd_ecc_point *)start;
c1->x.data = (char *)start + st_sz;
c1->x.dsize = ksz;
c1->x.bsize = ksz;
c1->y.data = c1->x.data + ksz;
c1->y.dsize = ksz;
c1->y.bsize = ksz;
c2 = (struct wd_dtb *)(c1 + 1);
c2->data = c1->y.data + ksz;
c2->dsize = m_len;
c2->bsize = m_len;
c3 = c2 + 1;
c3->data = c2->data + m_len;
c3->dsize = h_byts;
c3->bsize = h_byts;
return start;
}
static struct wd_ecc_in *create_ecc_sign_in(struct wd_ecc_sess *sess,
__u64 m_len, __u8 is_dgst)
{
if (is_dgst)
return create_ecc_in(sess, ECC_SIGN_IN_PARAM_NUM);
return create_sm2_sign_in(sess, m_len);
}
static struct wd_ecc_out *create_ecc_out(struct wd_ecc_sess *sess, __u32 num)
{
struct wd_ecc_out *out;
__u32 hsz, len;
if (!sess->key_size || sess->key_size > ECC_MAX_KEY_SIZE) {
WD_ERR("invalid: sess key size %u is error!\n", sess->key_size);
return NULL;
}
hsz = get_key_bsz(sess->key_size);
len = sizeof(struct wd_ecc_out) + hsz * num;
out = malloc(len);
if (!out) {
WD_ERR("failed to malloc out, sz = %u!\n", len);
return NULL;
}
memset(out, 0, len);
out->size = hsz * num;
init_dtb_param(out, out->data, sess->key_size, hsz, num);
return out;
}
static struct wd_ecc_curve *create_ecc_curve(struct wd_ecc_sess *sess)
{
struct wd_ecc_curve *cv;
__u32 ksize, len;
ksize = sess->key_size;
len = sizeof(*cv) + ksize * CURVE_PARAM_NUM;
cv = malloc(len);
if (!cv) {
WD_ERR("failed to malloc curve!\n");
return NULL;
}
memset(cv, 0, len);
init_dtb_param(cv, (void *)(cv + 1), ksize, ksize, CURVE_PARAM_NUM);
return cv;
}
static struct wd_ecc_point *create_ecc_pub(struct wd_ecc_sess *sess)
{
struct wd_ecc_point *pub;
__u32 ksize, len;
ksize = sess->key_size;
len = sizeof(*pub) + ksize * ECC_POINT_NUM;
pub = malloc(len);
if (!pub) {
WD_ERR("failed to malloc pub!\n");
return NULL;
}
memset(pub, 0, len);
init_dtb_param(pub, (void *)(pub + 1), ksize, ksize, ECC_POINT_NUM);
return pub;
}
static struct wd_dtb *create_ecc_d(struct wd_ecc_sess *sess)
{
struct wd_dtb *d;
__u32 ksize, len;
ksize = sess->key_size;
len = sizeof(*d) + ksize;
d = malloc(len);
if (!d) {
WD_ERR("failed to malloc d!\n");
return NULL;
}
memset(d, 0, len);
init_dtb_param(d, (void *)(d + 1), ksize, ksize, 1);
return d;
}
static void release_ecc_curve(struct wd_ecc_sess *sess)
{
free(sess->key.cv);
sess->key.cv = NULL;
}
static void release_ecc_pub(struct wd_ecc_sess *sess)
{
free(sess->key.pub);
sess->key.pub = NULL;
}
static void release_ecc_d(struct wd_ecc_sess *sess)
{
wd_memset_zero(sess->key.d + 1, sess->key_size);
free(sess->key.d);
sess->key.d = NULL;
}
static int set_param_single(struct wd_dtb *dst, const struct wd_dtb *src,
const char *p_name)
{
if (unlikely(!src || !src->data)) {
WD_ERR("invalid: %s src or data NULL!\n", p_name);
return -WD_EINVAL;
}
if (!src->dsize || src->dsize > dst->dsize) {
WD_ERR("invalid: %s src dsz %u or dst dsz %u is error!\n",
p_name, src->dsize, dst->dsize);
return -WD_EINVAL;
}
dst->dsize = src->dsize;
memset(dst->data, 0, dst->bsize);
memcpy(dst->data, src->data, src->dsize);
return WD_SUCCESS;
}
int wd_ecc_get_key_bits(handle_t sess)
{
if (!sess) {
WD_ERR("invalid: get ecc key bits, sess NULL!\n");
return -WD_EINVAL;
}
return ((struct wd_ecc_sess *)sess)->setup.key_bits;
}
static int set_curve_param_single(struct wd_ecc_key *key,
const struct wd_dtb *param,
__u32 type)
{
struct wd_ecc_prikey *pri = key->prikey;
struct wd_ecc_pubkey *pub = key->pubkey;
struct wd_dtb *tmp1, *tmp2;
int ret;
tmp1 = (struct wd_dtb *)((char *)pri +
curve_pram_list[type].pri_offset);
tmp2 = (struct wd_dtb *)((char *)pub +
curve_pram_list[type].pub_offset);
if (type == ECC_CURVE_G) {
ret = set_param_single(tmp1 + 1, param + 1, "set cv");
if (unlikely(ret))
return ret;
ret = set_param_single(tmp2 + 1, param + 1, "set cv");
if (unlikely(ret))
return ret;
}
ret = set_param_single(tmp1, param, "set cv");
if (unlikely(ret))
return ret;
return set_param_single(tmp2, param, "set cv");
}
static int set_curve_param(struct wd_ecc_key *key,
const struct wd_ecc_curve *param)
{
int ret;
ret = set_curve_param_single(key, ¶m->p, ECC_CURVE_P);
if (ret) {
WD_ERR("failed to set curve param: p error!\n");
return -WD_EINVAL;
}
ret = set_curve_param_single(key, ¶m->a, ECC_CURVE_A);
if (ret) {
WD_ERR("failed to set curve param: a error!\n");
return -WD_EINVAL;
}
ret = set_curve_param_single(key, ¶m->b, ECC_CURVE_B);
if (ret) {
WD_ERR("failed to set curve param: b error!\n");
return -WD_EINVAL;
}
ret = set_curve_param_single(key, ¶m->n, ECC_CURVE_N);
if (ret) {
WD_ERR("failed to set curve param: n error!\n");
return -WD_EINVAL;
}
ret = set_curve_param_single(key, (void *)¶m->g, ECC_CURVE_G);
if (ret) {
WD_ERR("failed to set curve param: g error!\n");
return -WD_EINVAL;
}
return WD_SUCCESS;
}
static const struct wd_ecc_curve_list *find_curve_list(__u32 id)
{
int len = ARRAY_SIZE(curve_list);
int i = 0;
while (i < len) {
if (curve_list[i].id == id)
return &curve_list[i];
i++;
}
return NULL;
}
static int fill_param_by_id(struct wd_ecc_curve *c,
__u16 key_bits, __u32 id)
{
struct wd_ecc_curve_list *item;
__u32 key_size;
item = (struct wd_ecc_curve_list *)find_curve_list(id);
if (!item) {
WD_ERR("failed to find curve id %u!\n", id);
return -WD_EINVAL;
}
if (item->key_bits != key_bits) {
WD_ERR("invalid: curve %u and key bits %u not match!\n", id, key_bits);
return -WD_EINVAL;
}
key_size = BITS_TO_BYTES(item->key_bits);
memcpy(c->p.data, item->data, CURVE_PARAM_NUM * key_size);
return WD_SUCCESS;
}
static void setup_curve_cfg(struct wd_ecc_sess_setup *setup)
{
if (!strcmp(setup->alg, "x25519")) {
setup->key_bits = 256; /* key width */
setup->cv.type = WD_CV_CFG_ID;
setup->cv.cfg.id = WD_X25519;
} else if (!strcmp(setup->alg, "x448")) {
setup->key_bits = 448;
setup->cv.type = WD_CV_CFG_ID;
setup->cv.cfg.id = WD_X448;
} else if ((!strcmp(setup->alg, "sm2"))) {
setup->key_bits = 256;
setup->cv.type = WD_CV_CFG_ID;
setup->cv.cfg.id = WD_SM2P256;
}
}
static int set_key_cv(struct wd_ecc_curve *dst,
struct wd_ecc_curve *src)
{
int ret;
if (unlikely(!src)) {
WD_ERR("invalid: set key cv, praram NULL!\n");
return -WD_EINVAL;
}
ret = set_param_single(&dst->p, &src->p, "cv p");
if (ret)
return ret;
ret = set_param_single(&dst->a, &src->a, "cv a");