-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
1081 lines (1022 loc) · 46.2 KB
/
base.go
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
/*
Copyright (C) BABEC. All rights reserved.
Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package base
/*
#cgo windows CFLAGS: -DPACKED_STRUCTURES
#cgo linux LDFLAGS: -ldl
#cgo darwin LDFLAGS: -ldl
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sdf.h> //GM-T0018 sdf header
typedef unsigned char SGD_UCHAR;
typedef unsigned char* SGD_UCHAR_PRT;
#ifdef _WIN32
#include<windows.h>
struct LibHandle {
HMODULE handle;
};
struct LibHandle *NewLib(const char *iLibrary)
{
struct LibHandle *h = calloc(1,sizeof(struct LibHandle));
h->handle = LoadLibrary(iLibrary);
if (h->handle == NULL) {
free(h);
return NULL;
}
return h;
}
void DestroyLib(struct LibHandle *h)
{
if(!h){
return ;
}
if (h->handle == NULL) {
return;
}
free(h);
}
#else
#include <dlfcn.h>
struct LibHandle {
void *handle;
};
struct LibHandle *NewLib(const char *iLibrary)
{
struct LibHandle *h = calloc(1,sizeof(struct LibHandle));
h->handle = dlopen(iLibrary,1);
if(h->handle == NULL){
free(h);
return NULL;
}
return h;
}
void DestroyLib(struct LibHandle *h)
{
if (!h) {
return;
}
if (h->handle == NULL) {
return;
}
if (dlclose(h->handle) < 0) {
return;
}
free(h);
}
#endif
//1. 打开设备
SGD_RV SDFOpenDevice(struct LibHandle * h,SGD_HANDLE *phDeviceHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE*);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_OpenDevice");
return (*fptr)(phDeviceHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_OpenDevice");
return (*fptr)(phDeviceHandle);
#endif
}
//2. 关闭设备
SGD_RV SDFCloseDevice(struct LibHandle * h,SGD_HANDLE hDeviceHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_CloseDevice");
return (*fptr)(hDeviceHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_CloseDevice");
return (*fptr)(hDeviceHandle);
#endif
}
//3. 创建会话
SGD_RV SDFOpenSession(struct LibHandle * h,SGD_HANDLE hDeviceHandle, SGD_HANDLE *phSessionHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_HANDLE *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_OpenSession");
return (*fptr)(hDeviceHandle,phSessionHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_OpenSession");
return (*fptr)(hDeviceHandle,phSessionHandle);
#endif
}
//4. 关闭会话
SGD_RV SDFCloseSession(struct LibHandle * h,SGD_HANDLE hSessionHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_CloseSession");
return (*fptr)(hSessionHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_CloseSession");
return (*fptr)(hSessionHandle);
#endif
}
//5. 获取设备信息
SGD_RV SDFGetDeviceInfo(struct LibHandle * h,SGD_HANDLE hSessionHandle, DEVICEINFO *pstDeviceInfo)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,DEVICEINFO *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GetDeviceInfo");
return (*fptr)(hSessionHandle,pstDeviceInfo);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GetDeviceInfo");
return (*fptr)(hSessionHandle,pstDeviceInfo);
#endif
}
//6. 产生随机数
SGD_RV SDFGenerateRandom(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiLength, SGD_UCHAR_PRT *pucRandom)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,SGD_UCHAR*);
*pucRandom = calloc(uiLength, sizeof(SGD_UCHAR));
if (*pucRandom == NULL) {
return SGD_FALSE;
}
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GenerateRandom");
return (*fptr)(hSessionHandle,uiLength,*pucRandom);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GenerateRandom");
return (*fptr)(hSessionHandle,uiLength,*pucRandom);
#endif
}
//7. 获取私钥使用权限
SGD_RV SDFGetPrivateKeyAccessRight(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyIndex,SGD_UCHAR_PRT pucPassword, SGD_UINT32 uiPwdLength)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,SGD_UCHAR*,SGD_UINT32);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GetPrivateKeyAccessRight");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPassword,uiPwdLength);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GetPrivateKeyAccessRight");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPassword,uiPwdLength);
#endif
}
//8. 释放私钥使用权限
SGD_RV SDFReleasePrivateKeyAccessRight(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyIndex)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ReleasePrivateKeyAccessRight");
return (*fptr)(hSessionHandle,uiKeyIndex);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ReleasePrivateKeyAccessRight");
return (*fptr)(hSessionHandle,uiKeyIndex);
#endif
}
//9. 导出RSA签名公钥
SGD_RV SDFExportSignPublicKey_RSA(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyIndex,RSArefPublicKey *pucPublicKey)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,RSArefPublicKey*);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExportSignPublicKey_RSA");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPublicKey);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExportSignPublicKey_RSA");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPublicKey);
#endif
}
//10. 导出RSA加密公钥
SGD_RV SDFExportEncPublicKey_RSA(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyIndex,RSArefPublicKey *pucPublicKey)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,RSArefPublicKey*);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExportEncPublicKey_RSA");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPublicKey);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExportEncPublicKey_RSA");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPublicKey);
#endif
}
//11. 产生RSA非对称密钥对并输出
SGD_RV SDFGenerateKeyPair_RSA(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyBits,RSArefPublicKey *pucPublicKey,RSArefPrivateKey *pucPrivateKey)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,RSArefPublicKey*,RSArefPrivateKey*);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GenerateKeyPair_RSA");
return (*fptr)(hSessionHandle,uiKeyBits,pucPublicKey,pucPrivateKey);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GenerateKeyPair_RSA");
return (*fptr)(hSessionHandle,uiKeyBits,pucPublicKey,pucPrivateKey);
#endif
}
//12. 生成会话密钥并用内部RSA公钥加密输出
SGD_RV SDFGenerateKeyWithIPK_RSA(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiIPKIndex,SGD_UINT32 uiKeyBits,SGD_UCHAR_PRT *pucKey,SGD_UINT32 *puiKeyLength,SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,SGD_UINT32,SGD_UCHAR*,SGD_UINT32*,SGD_HANDLE*);
*pucKey = calloc(*puiKeyLength, sizeof(SGD_UCHAR));
if (*pucKey == NULL) {
return SGD_FALSE;
}
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GenerateKeyWithIPK_RSA");
return (*fptr)(hSessionHandle,uiIPKIndex,uiKeyBits,*pucKey,puiKeyLength,phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GenerateKeyWithIPK_RSA");
return (*fptr)(hSessionHandle,uiIPKIndex,uiKeyBits,*pucKey,puiKeyLength,phKeyHandle);
#endif
}
//13. 生成会话密钥并用外部RSA公钥加密输出
SGD_RV SDFGenerateKeyWithEPK_RSA(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyBits,RSArefPublicKey *pucPublicKey,SGD_UCHAR_PRT *pucKey,SGD_UINT32 *puiKeyLength,SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,RSArefPublicKey*,SGD_UCHAR*,SGD_UINT32*,SGD_HANDLE*);
*pucKey = calloc(*puiKeyLength, sizeof(SGD_UCHAR));
if (*pucKey == NULL) {
return SGD_FALSE;
}
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GenerateKeyWithEPK_RSA");
return (*fptr)(hSessionHandle,uiKeyBits,pucPublicKey,*pucKey,puiKeyLength,phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle,"SDF_GenerateKeyWithEPK_RSA");
return (*fptr)(hSessionHandle,uiKeyBits,pucPublicKey,*pucKey,puiKeyLength,phKeyHandle);
#endif
}
//14. 导入会话密钥并用内部RSA私钥解密
SGD_RV SDFImportKeyWithISK_RSA(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiISKIndex,SGD_UCHAR_PRT pucKey,SGD_UINT32 uiKeyLength,SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,SGD_UCHAR*,SGD_UINT32,SGD_HANDLE*);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ImportKeyWithISK_RSA");
return (*fptr)(hSessionHandle,uiISKIndex,pucKey,uiKeyLength,phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ImportKeyWithISK_RSA");
return (*fptr)(hSessionHandle,uiISKIndex,pucKey,uiKeyLength,phKeyHandle);
#endif
}
//15. 基于RSA算法的数字信封转换
SGD_RV SDFExchangeDigitEnvelopeBaseOnRSA(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyIndex,RSArefPublicKey *pucPublicKey,SGD_UCHAR_PRT pucDEInput,SGD_UINT32 uiDELength,SGD_UCHAR_PRT *pucDEOutput,SGD_UINT32 *puiDELength)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,RSArefPublicKey*,SGD_UCHAR*,SGD_UINT32,SGD_UCHAR*,SGD_UINT32*);
*pucDEOutput = calloc(*puiDELength, sizeof(SGD_UCHAR));
if (*pucDEOutput == NULL) {
return SGD_FALSE;
}
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExchangeDigitEnvelopeBaseOnRSA");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPublicKey,pucDEInput,uiDELength,*pucDEOutput,puiDELength);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExchangeDigitEnvelopeBaseOnRSA");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPublicKey,pucDEInput,uiDELength,*pucDEOutput,puiDELength);
#endif
}
//16. 导出ECC签名公钥
SGD_RV SDFExportSignPublicKey_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyIndex,ECCrefPublicKey *pucPublicKey)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,ECCrefPublicKey*);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExportSignPublicKey_ECC");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPublicKey);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExportSignPublicKey_ECC");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPublicKey);
#endif
}
//17. 导出ECC加密公钥
SGD_RV SDFExportEncPublicKey_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyIndex,ECCrefPublicKey *pucPublicKey)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,ECCrefPublicKey*);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExportEncPublicKey_ECC");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPublicKey);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExportEncPublicKey_ECC");
return (*fptr)(hSessionHandle,uiKeyIndex,pucPublicKey);
#endif
}
//18. 产生ECC非对称密钥对并输出
SGD_RV SDFGenerateKeyPair_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiAlgID,SGD_UINT32 uiKeyBits,ECCrefPublicKey *pucPublicKey,ECCrefPrivateKey *pucPrivateKey)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,SGD_UINT32,ECCrefPublicKey*,ECCrefPrivateKey*);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GenerateKeyPair_ECC");
return (*fptr)(hSessionHandle,uiAlgID,uiKeyBits,pucPublicKey,pucPrivateKey);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GenerateKeyPair_ECC");
return (*fptr)(hSessionHandle,uiAlgID,uiKeyBits,pucPublicKey,pucPrivateKey);
#endif
}
//19. 生成会话密钥并用内部ECC公钥加密输出
SGD_RV SDFGenerateKeyWithIPK_ECC (struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiIPKIndex,SGD_UINT32 uiKeyBits,ECCCipher *pucKey,SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,SGD_UINT32,ECCCipher*,SGD_HANDLE*);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GenerateKeyWithIPK_ECC");
return (*fptr)(hSessionHandle,uiIPKIndex,uiKeyBits,pucKey,phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GenerateKeyWithIPK_ECC");
return (*fptr)(hSessionHandle,uiIPKIndex,uiKeyBits,pucKey,phKeyHandle);
#endif
}
//20. 生成会话密钥并用外部ECC公钥加密输出
SGD_RV SDFGenerateKeyWithEPK_ECC (struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyBits,SGD_UINT32 uiAlgID,ECCrefPublicKey *pucPublicKey,ECCCipher *pucKey,SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,SGD_UINT32,ECCrefPublicKey*,ECCCipher*,SGD_HANDLE*);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GenerateKeyWithEPK_ECC");
return (*fptr)(hSessionHandle,uiKeyBits,uiAlgID,pucPublicKey,pucKey,phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GenerateKeyWithEPK_ECC");
return (*fptr)(hSessionHandle,uiKeyBits,uiAlgID,pucPublicKey,pucKey,phKeyHandle);
#endif
}
//21. 导入会话密钥并用内部ECC私钥解密
SGD_RV SDFImportKeyWithISK_ECC (struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_UINT32 uiISKIndex,ECCCipher *pucKey,SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE,SGD_UINT32,ECCCipher*,SGD_HANDLE*);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ImportKeyWithISK_ECC");
return (*fptr)(hSessionHandle,uiISKIndex,pucKey,phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ImportKeyWithISK_ECC");
return (*fptr)(hSessionHandle,uiISKIndex,pucKey,phKeyHandle);
#endif
}
//22. 生成密钥协商参数并输出
SGD_RV SDFGenerateAgreementDataWithECC (struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiISKIndex,SGD_UINT32 uiKeyBits,SGD_UCHAR_PRT pucSponsorID,SGD_UINT32 uiSponsorIDLength,ECCrefPublicKey *pucSponsorPublicKey,ECCrefPublicKey *pucSponsorTmpPublicKey,SGD_HANDLE *phAgreementHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE , SGD_UINT32 ,SGD_UINT32 ,SGD_UCHAR *,SGD_UINT32 ,ECCrefPublicKey *,ECCrefPublicKey *,SGD_HANDLE *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GenerateAgreementDataWithECC");
return (*fptr)(hSessionHandle, uiISKIndex, uiKeyBits, pucSponsorID, uiSponsorIDLength, pucSponsorPublicKey, pucSponsorTmpPublicKey, phAgreementHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GenerateAgreementDataWithECC");
return (*fptr)(hSessionHandle, uiISKIndex, uiKeyBits, pucSponsorID, uiSponsorIDLength, pucSponsorPublicKey, pucSponsorTmpPublicKey, phAgreementHandle);
#endif
}
//23. 计算会话密钥
SGD_RV SDFGenerateKeyWithECC (struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UCHAR_PRT pucResponseID,SGD_UINT32 uiResponseIDLength,ECCrefPublicKey *pucResponsePublicKey,ECCrefPublicKey *pucResponseTmpPublicKey,SGD_HANDLE hAgreementHandle,SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE, SGD_UCHAR *,SGD_UINT32 ,ECCrefPublicKey *,ECCrefPublicKey *,SGD_HANDLE ,SGD_HANDLE *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GenerateKeyWithECC");
return (*fptr)(hSessionHandle,pucResponseID,uiResponseIDLength,pucResponsePublicKey,pucResponseTmpPublicKey,hAgreementHandle,phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GenerateKeyWithECC");
return (*fptr)(hSessionHandle,pucResponseID,uiResponseIDLength,pucResponsePublicKey,pucResponseTmpPublicKey,hAgreementHandle,phKeyHandle);
#endif
}
//24. 产生协商数据并计算会话密钥
SGD_RV SDFGenerateAgreementDataAndKeyWithECC (struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiISKIndex,SGD_UINT32 uiKeyBits,SGD_UCHAR_PRT pucResponseID,SGD_UINT32 uiResponseIDLength,SGD_UCHAR_PRT pucSponsorID,SGD_UINT32 uiSponsorIDLength,ECCrefPublicKey *pucSponsorPublicKey,ECCrefPublicKey *pucSponsorTmpPublicKey,ECCrefPublicKey *pucResponsePublicKey, ECCrefPublicKey *pucResponseTmpPublicKey,SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE , SGD_UINT32 ,SGD_UINT32 ,SGD_UCHAR *,SGD_UINT32 ,SGD_UCHAR *,SGD_UINT32 ,ECCrefPublicKey *,ECCrefPublicKey *,ECCrefPublicKey *, ECCrefPublicKey *,SGD_HANDLE *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GenerateAgreementDataAndKeyWithECC");
return (*fptr)(hSessionHandle, uiISKIndex, uiKeyBits, pucResponseID, uiResponseIDLength, pucSponsorID, uiSponsorIDLength, pucSponsorPublicKey, pucSponsorTmpPublicKey, pucResponsePublicKey, pucResponseTmpPublicKey, phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GenerateAgreementDataAndKeyWithECC");
return (*fptr)(hSessionHandle, uiISKIndex, uiKeyBits, pucResponseID, uiResponseIDLength, pucSponsorID, uiSponsorIDLength, pucSponsorPublicKey, pucSponsorTmpPublicKey, pucResponsePublicKey, pucResponseTmpPublicKey, phKeyHandle);
#endif
}
//25. 基于 ECC算法的数字信封转换
SGD_RV SDFExchangeDigitEnvelopeBaseOnECC(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyIndex,SGD_UINT32 uiAlgID,ECCrefPublicKey *pucPublicKey,ECCCipher *pucEncDataIn,ECCCipher *pucEncDataOut)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE , SGD_UINT32 ,SGD_UINT32 ,ECCrefPublicKey *,ECCCipher *,ECCCipher *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExchangeDigitEnvelopeBaseOnECC");
return (*fptr)(hSessionHandle, uiKeyIndex, uiAlgID, pucPublicKey, pucEncDataIn, pucEncDataOut);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExchangeDigitEnvelopeBaseOnECC");
return (*fptr)(hSessionHandle, uiKeyIndex, uiAlgID, pucPublicKey, pucEncDataIn, pucEncDataOut);
#endif
}
//26. 生成会话密钥并用密钥加密密钥加密输出
SGD_RV SDFGenerateKeyWithKEK(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyBits,SGD_UINT32 uiAlgID,SGD_UINT32 uiKEKIndex, SGD_UCHAR_PRT *pucKey, SGD_UINT32 *puiKeyLength, SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE , SGD_UINT32 ,SGD_UINT32 ,SGD_UINT32 , SGD_UCHAR *, SGD_UINT32 *, SGD_HANDLE *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GenerateKeyWithKEK");
return (*fptr)(hSessionHandle, uiKeyBits, uiAlgID, uiKEKIndex, *pucKey, puiKeyLength, phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GenerateKeyWithKEK");
return (*fptr)(hSessionHandle, uiKeyBits, uiAlgID, uiKEKIndex, *pucKey, puiKeyLength, phKeyHandle);
#endif
}
//27. 导入会话密钥并用密钥加密密钥解密
SGD_RV SDFImportKeyWithKEK(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiAlgID,SGD_UINT32 uiKEKIndex, SGD_UCHAR_PRT pucKey, SGD_UINT32 uiKeyLength, SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE , SGD_UINT32 ,SGD_UINT32 , SGD_UCHAR *, SGD_UINT32 , SGD_HANDLE *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ImportKeyWithKEK");
return (*fptr)(hSessionHandle, uiAlgID, uiKEKIndex, pucKey, uiKeyLength, phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ImportKeyWithKEK");
return (*fptr)(hSessionHandle, uiAlgID, uiKEKIndex, pucKey, uiKeyLength, phKeyHandle);
#endif
}
//28. 导入明文会话密钥
SGD_RV SDFImportKey(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UCHAR_PRT pucKey, SGD_UINT32 uiKeyLength,SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE , SGD_UCHAR *, SGD_UINT32 ,SGD_HANDLE *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ImportKey");
return (*fptr)(hSessionHandle, pucKey, uiKeyLength, phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ImportKey");
return (*fptr)(hSessionHandle, pucKey, uiKeyLength, phKeyHandle);
#endif
}
//29. 销毁会话密钥
SGD_RV SDFDestroyKey(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_HANDLE hKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE , SGD_HANDLE);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_DestroyKey");
return (*fptr)(hSessionHandle, hKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_DestroyKey");
return (*fptr)(hSessionHandle, hKeyHandle);
#endif
}
//34. 外部密钥ECC签名
SGD_RV SDFExternalSign_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_UINT32 uiAlgID,ECCrefPrivateKey *pucPrivateKey,SGD_UCHAR_PRT pucData,SGD_UINT32 uiDataLength,ECCSignature *pucSignature)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_UINT32 ,ECCrefPrivateKey *,SGD_UCHAR *,SGD_UINT32 ,ECCSignature *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExternalSign_ECC");
return (*fptr)(hSessionHandle, uiAlgID, pucPrivateKey, pucData, uiDataLength, pucSignature);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExternalSign_ECC");
return (*fptr)(hSessionHandle, uiAlgID, pucPrivateKey, pucData, uiDataLength, pucSignature);
#endif
}
//35. 外部密钥ECC验证
SGD_RV SDFExternalVerify_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_UINT32 uiAlgID,ECCrefPublicKey *pucPublicKey,SGD_UCHAR_PRT pucDataInput,SGD_UINT32 uiInputLength,ECCSignature *pucSignature)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_UINT32 ,ECCrefPublicKey *,SGD_UCHAR *,SGD_UINT32 ,ECCSignature *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExternalVerify_ECC");
return (*fptr)(hSessionHandle, uiAlgID, pucPublicKey, pucDataInput, uiInputLength, pucSignature);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExternalVerify_ECC");
return (*fptr)(hSessionHandle, uiAlgID, pucPublicKey, pucDataInput, uiInputLength, pucSignature);
#endif
}
//36. 内部密钥ECC签名
SGD_RV SDFInternalSign_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_UINT32 uiISKIndex,SGD_UCHAR_PRT pucData,SGD_UINT32 uiDataLength,ECCSignature *pucSignature)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_UINT32 uiISKIndex,SGD_UCHAR *pucData,SGD_UINT32 uiDataLength,ECCSignature *pucSignature);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_InternalSign_ECC");
return (*fptr)(hSessionHandle, uiISKIndex, pucData, uiDataLength, pucSignature);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_InternalSign_ECC");
return (*fptr)(hSessionHandle, uiISKIndex, pucData, uiDataLength, pucSignature);
#endif
}
//37. 内部密钥ECC验证
SGD_RV SDFInternalVerify_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_UINT32 uiISKIndex,SGD_UCHAR_PRT pucData,SGD_UINT32 uiDataLength,ECCSignature *pucSignature)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_UINT32 ,SGD_UCHAR *,SGD_UINT32 ,ECCSignature *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_InternalVerify_ECC");
return (*fptr)(hSessionHandle, uiISKIndex, pucData, uiDataLength, pucSignature);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_InternalVerify_ECC");
return (*fptr)(hSessionHandle, uiISKIndex, pucData, uiDataLength, pucSignature);
#endif
}
//38. 外部密钥ECC加密
SGD_RV SDFExternalEncrypt_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_UINT32 uiAlgID,ECCrefPublicKey *pucPublicKey,SGD_UCHAR_PRT pucData,SGD_UINT32 uiDataLength,ECCCipher *pucEncData)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_UINT32 ,ECCrefPublicKey *,SGD_UCHAR *,SGD_UINT32 ,ECCCipher *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExternalEncrypt_ECC");
return (*fptr)(hSessionHandle, uiAlgID, pucPublicKey, pucData, uiDataLength, pucEncData);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExternalEncrypt_ECC");
return (*fptr)(hSessionHandle, uiAlgID, pucPublicKey, pucData, uiDataLength, pucEncData);
#endif
}
//39. 外部密钥ECC解密
SGD_RV SDFExternalDecrypt_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_UINT32 uiAlgID,ECCrefPrivateKey *pucPrivateKey,ECCCipher *pucEncData,SGD_UCHAR_PRT *pucData,SGD_UINT32 *puiDataLength)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_UINT32 ,ECCrefPrivateKey *,ECCCipher *,SGD_UCHAR *,SGD_UINT32 *);
*pucData = calloc(*puiDataLength, sizeof(SGD_UCHAR));
if (*pucData == NULL) {
return SGD_FALSE;
}
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExternalDecrypt_ECC");
return (*fptr)(hSessionHandle, uiAlgID, pucPrivateKey, pucEncData, *pucData, puiDataLength);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExternalDecrypt_ECC");
return (*fptr)(hSessionHandle, uiAlgID, pucPrivateKey, pucEncData, *pucData, puiDataLength);
#endif
}
//40. 对称加密
SGD_RV SDFEncrypt(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_HANDLE hKeyHandle,SGD_UINT32 uiAlgID,SGD_UCHAR_PRT pucIV,SGD_UCHAR_PRT pucData,SGD_UINT32 uiDataLength,SGD_UCHAR_PRT *pucEncData,SGD_UINT32 *puiEncDataLength)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_HANDLE ,SGD_UINT32 ,SGD_UCHAR *,SGD_UCHAR *,SGD_UINT32 ,SGD_UCHAR *,SGD_UINT32 *);
*pucEncData = calloc(*puiEncDataLength, sizeof(SGD_UCHAR));
if (*pucEncData == NULL) {
return SGD_FALSE;
}
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_Encrypt");
return (*fptr)(hSessionHandle, hKeyHandle, uiAlgID, pucIV, pucData, uiDataLength, *pucEncData, puiEncDataLength);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_Encrypt");
return (*fptr)(hSessionHandle, hKeyHandle, uiAlgID, pucIV, pucData, uiDataLength, *pucEncData, puiEncDataLength);
#endif
}
//41. 对称解密
SGD_RV SDFDecrypt (struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_HANDLE hKeyHandle,SGD_UINT32 uiAlgID,SGD_UCHAR_PRT pucIV,SGD_UCHAR_PRT pucEncData,SGD_UINT32 uiEncDataLength,SGD_UCHAR_PRT *pucData,SGD_UINT32 *puiDataLength)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_HANDLE ,SGD_UINT32 ,SGD_UCHAR *,SGD_UCHAR *,SGD_UINT32 ,SGD_UCHAR *,SGD_UINT32 *);
*pucData = calloc(*puiDataLength, sizeof(SGD_UCHAR));
if (*pucData == NULL) {
return SGD_FALSE;
}
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_Decrypt");
return (*fptr)(hSessionHandle, hKeyHandle, uiAlgID, pucIV, pucEncData, uiEncDataLength, *pucData, puiDataLength);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_Decrypt");
return (*fptr)(hSessionHandle, hKeyHandle, uiAlgID, pucIV, pucEncData, uiEncDataLength, *pucData, puiDataLength);
#endif
}
//42. 计算MAC
SGD_RV SDFCalculateMAC(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_HANDLE hKeyHandle,SGD_UINT32 uiAlgID,SGD_UCHAR_PRT pucIV,SGD_UCHAR_PRT pucData,SGD_UINT32 uiDataLength,SGD_UCHAR_PRT *pucMAC,SGD_UINT32 *puiMACLength)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_HANDLE ,SGD_UINT32 ,SGD_UCHAR *,SGD_UCHAR *,SGD_UINT32 ,SGD_UCHAR *,SGD_UINT32 *);
*pucMAC = calloc(*puiMACLength, sizeof(SGD_UCHAR));
if (*pucMAC == NULL) {
return SGD_FALSE;
}
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_CalculateMAC");
return (*fptr)(hSessionHandle, hKeyHandle, uiAlgID, pucIV, pucData, uiDataLength, *pucMAC, puiMACLength);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_CalculateMAC");
return (*fptr)(hSessionHandle, hKeyHandle, uiAlgID, pucIV, pucData, uiDataLength, *pucMAC, puiMACLength);
#endif
}
//43. 杂凑运算初始化
SGD_RV SDFHashInit(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_UINT32 uiAlgID,ECCrefPublicKey *pucPublicKey,SGD_UCHAR_PRT pucID,SGD_UINT32 uiIDLength)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_UINT32 ,ECCrefPublicKey *,SGD_UCHAR *,SGD_UINT32 );
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_HashInit");
return (*fptr)(hSessionHandle, uiAlgID, pucPublicKey, pucID, uiIDLength);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_HashInit");
return (*fptr)(hSessionHandle, uiAlgID, pucPublicKey, pucID, uiIDLength);
#endif
}
//44. 多包杂凑运算
SGD_RV SDFHashUpdate(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_UCHAR_PRT pucData,SGD_UINT32 uiDataLength)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_UCHAR *,SGD_UINT32 );
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_HashUpdate");
return (*fptr)(hSessionHandle, pucData, uiDataLength);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_HashUpdate");
return (*fptr)(hSessionHandle, pucData, uiDataLength);
#endif
}
//45. 杂凑运算结束
SGD_RV SDFHashFinal(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_UCHAR_PRT *pucHash,SGD_UINT32 *puiHashLength)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_UCHAR *,SGD_UINT32 *);
*pucHash = calloc(*puiHashLength, sizeof(SGD_UCHAR));
if (*pucHash == NULL) {
return SGD_FALSE;
}
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_HashFinal");
return (*fptr)(hSessionHandle, *pucHash, puiHashLength);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_HashFinal");
return (*fptr)(hSessionHandle, *pucHash, puiHashLength);
#endif
}
//50. 获取对称句柄
SGD_RV SDFGetSymmKeyHandle(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiKeyIndex, SGD_HANDLE *phKeyHandle)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE , SGD_UINT32 , SGD_HANDLE *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_GetSymmKeyHandle");
return (*fptr)(hSessionHandle, uiKeyIndex, phKeyHandle);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_GetSymmKeyHandle");
return (*fptr)(hSessionHandle, uiKeyIndex, phKeyHandle);
#endif
}
//51. ECC方式的加密
SGD_RV SDFInternalEncrypt_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_UINT32 uiISKIndex, SGD_UINT32 uiAlgID, SGD_UCHAR_PRT pucData, SGD_UINT32 uiDataLength, ECCCipher *pucEncData)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE , SGD_UINT32 , SGD_UINT32 , SGD_UCHAR *, SGD_UINT32 , ECCCipher *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_InternalEncrypt_ECC");
return (*fptr)(hSessionHandle, uiISKIndex, uiAlgID, pucData, uiDataLength, pucEncData);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_InternalEncrypt_ECC");
return (*fptr)(hSessionHandle, uiISKIndex, uiAlgID, pucData, uiDataLength, pucEncData);
#endif
}
//52. ECC方式的解密
SGD_RV SDFInternalDecrypt_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle,SGD_UINT32 uiISKIndex,SGD_UINT32 uiAlgID,ECCCipher *pucEncData,SGD_UCHAR_PRT *pucData,SGD_UINT32 *puiDataLength)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE ,SGD_UINT32 ,SGD_UINT32 ,ECCCipher *,SGD_UCHAR *,SGD_UINT32 *);
*pucData = calloc(*puiDataLength, sizeof(SGD_UCHAR));
if (*pucData == NULL) {
return SGD_FALSE;
}
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_InternalDecrypt_ECC");
return (*fptr)(hSessionHandle, uiISKIndex, uiAlgID, pucEncData, *pucData, puiDataLength);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_InternalDecrypt_ECC");
return (*fptr)(hSessionHandle, uiISKIndex, uiAlgID, pucEncData, *pucData, puiDataLength);
#endif
}
//54. EPK方式导出ECC密钥
SGD_RV SDFExportKeyWithEPK_ECC(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_HANDLE hKeyHandle, SGD_UINT32 uiAlgID, ECCrefPublicKey *pucPublicKey, ECCCipher *pucKey)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE , SGD_HANDLE , SGD_UINT32 , ECCrefPublicKey *, ECCCipher *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExportKeyWithEPK_ECC");
return (*fptr)(hSessionHandle, hKeyHandle, uiAlgID, pucPublicKey, pucKey);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExportKeyWithEPK_ECC");
return (*fptr)(hSessionHandle, hKeyHandle, uiAlgID, pucPublicKey, pucKey);
#endif
}
//55. EPK方式导出密钥
SGD_RV SDFExportKeyWithKEK(struct LibHandle * h,SGD_HANDLE hSessionHandle, SGD_HANDLE hKeyHandle, SGD_UINT32 uiAlgID, SGD_UINT32 uiKEKIndex, SGD_UCHAR_PRT *pucKey, SGD_UINT32 *puiKeyLength)
{
typedef SGD_RV (*FPTR)(SGD_HANDLE , SGD_HANDLE , SGD_UINT32 , SGD_UINT32 , SGD_UCHAR *, SGD_UINT32 *);
#ifdef _WIN32
FPTR fptr = (FPTR)GetProcAddress(h->handle, "SDF_ExportKeyWithKEK");
return (*fptr)(hSessionHandle, hKeyHandle, uiAlgID, uiKEKIndex, *pucKey, puiKeyLength);
#else
FPTR fptr = (FPTR)dlsym(h->handle, "SDF_ExportKeyWithKEK");
return (*fptr)(hSessionHandle, hKeyHandle, uiAlgID, uiKEKIndex, *pucKey, puiKeyLength);
#endif
}
*/
import "C"
import (
"fmt"
"strings"
"unsafe"
)
func ConvertToDeviceInfoGo(deviceInfo1 C.DEVICEINFO) (deviceInfo DeviceInfo) {
deviceInfo = DeviceInfo{
IssuerName: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&deviceInfo1.IssuerName[0]), 40)), " "),
DeviceName: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&deviceInfo1.DeviceName[0]), 16)), " "),
DeviceSerial: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&deviceInfo1.DeviceSerial[0]), 16)), " "),
DeviceVersion: uint(deviceInfo1.DeviceVersion),
StandardVersion: uint(deviceInfo1.StandardVersion),
SymAlgAbility: uint(deviceInfo1.SymAlgAbility),
HashAlgAbility: uint(deviceInfo1.HashAlgAbility),
BufferSize: uint(deviceInfo1.BufferSize),
}
temp1 := C.GoBytes(unsafe.Pointer(&deviceInfo1.AsymAlgAbility[0]), 2)
temp2 := C.GoBytes(unsafe.Pointer(&deviceInfo1.AsymAlgAbility[1]), 2)
deviceInfo.AsymAlgAbility[0] = uint(temp1[0])
deviceInfo.AsymAlgAbility[1] = uint(temp2[0])
return deviceInfo
}
func ConvertToECCrefPublicKeyC(publicKey ECCrefPublicKey) (pucPublicKey C.ECCrefPublicKey) {
pucPublicKey.bits = C.SGD_UINT32(publicKey.Bits)
for i := 0; i < len(publicKey.X); i++ {
pucPublicKey.x[i] = C.SGD_UCHAR(publicKey.X[i])
}
for i := 0; i < len(publicKey.Y); i++ {
pucPublicKey.y[i] = C.SGD_UCHAR(publicKey.Y[i])
}
return pucPublicKey
}
func ConvertToECCrefPublicKeyGo(pucPublicKey C.ECCrefPublicKey) (publicKey ECCrefPublicKey) {
publicKey = ECCrefPublicKey{
Bits: uint(pucPublicKey.bits),
X: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&pucPublicKey.x[0]), 64)), " "),
Y: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&pucPublicKey.y[0]), 64)), " "),
}
return publicKey
}
func ConvertToECCrefPrivateKeyC(privateKey ECCrefPrivateKey) (pucPrivateKey C.ECCrefPrivateKey) {
pucPrivateKey.bits = C.SGD_UINT32(privateKey.Bits)
for i := 0; i < len(privateKey.K); i++ {
pucPrivateKey.K[i] = C.SGD_UCHAR(privateKey.K[i])
}
return pucPrivateKey
}
func ConvertToECCrefPrivateKeyGo(pucPrivateKey C.ECCrefPrivateKey) (privateKey ECCrefPrivateKey) {
privateKey = ECCrefPrivateKey{
Bits: uint(pucPrivateKey.bits),
K: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&pucPrivateKey.K[0]), 64)), " "),
}
return privateKey
}
func ConvertToECCCipherC(encData ECCCipher) (pucEncData C.ECCCipher) {
for i := 0; i < len(encData.X); i++ {
pucEncData.x[i] = C.SGD_UCHAR(encData.X[i])
}
for i := 0; i < len(encData.Y); i++ {
pucEncData.y[i] = C.SGD_UCHAR(encData.Y[i])
}
for i := 0; i < len(encData.M); i++ {
pucEncData.M[i] = C.SGD_UCHAR(encData.M[i])
}
pucEncData.L = C.SGD_UINT32(encData.L)
for i := 0; i < len(encData.C); i++ {
pucEncData.C[i] = C.SGD_UCHAR(encData.C[i])
}
return pucEncData
}
func ConvertToECCCipherGo(pucKey C.ECCCipher) (key ECCCipher) {
key = ECCCipher{
X: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&pucKey.x[0]), 64)), " "),
Y: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&pucKey.y[0]), 64)), " "),
M: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&pucKey.M[0]), 32)), " "),
L: uint(pucKey.L),
C: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&pucKey.C[0]), C.int(pucKey.L))), " "),
}
return key
}
func ConvertToECCSignatureC(signature ECCSignature) (pSignature C.ECCSignature) {
for i := 0; i < len(signature.R); i++ {
pSignature.r[i] = C.SGD_UCHAR(signature.R[i])
}
for i := 0; i < len(signature.S); i++ {
pSignature.s[i] = C.SGD_UCHAR(signature.S[i])
}
return pSignature
}
func ConvertToECCSignatureGo(pucSignature C.ECCSignature) (signature ECCSignature) {
signature = ECCSignature{
R: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&pucSignature.r[0]), 64)), " "),
S: strings.TrimRight(string(C.GoBytes(unsafe.Pointer(&pucSignature.s[0]), 64)), " "),
}
return signature
}
func New(libPath string) *Ctx {
c := new(Ctx)
mod := C.CString(libPath)
defer C.free(unsafe.Pointer(mod))
c.libHandle = C.NewLib(mod)
if c.libHandle == nil {
return nil
}
return c
}
func ToError(e C.SGD_RV) error {
if e == C.SDR_OK {
return nil
}
return fmt.Errorf("sdf: 0x%X", uint(e))
}
func (c *Ctx) Destroy() {
if c == nil || c.libHandle == nil {
return
}
C.DestroyLib(c.libHandle)
c.libHandle = nil
}
type Ctx struct {
libHandle *C.struct_LibHandle
}
type SessionHandle C.SGD_HANDLE
var stubData = []byte{0}
func CMessage(data []byte) (dataPtr C.SGD_UCHAR_PRT) {
l := len(data)
if l == 0 {
data = stubData
}
dataPtr = C.SGD_UCHAR_PRT(unsafe.Pointer(&data[0]))
return dataPtr
}
// SDFOpenDevice 1.打开设备
func (c *Ctx) SDFOpenDevice() (deviceHandle SessionHandle, err error) {
var err1 C.SGD_RV
var dH C.SGD_HANDLE
err1 = C.SDFOpenDevice(c.libHandle, &dH)
err = ToError(err1)
deviceHandle = SessionHandle(dH)
return deviceHandle, err
}
// SDFCloseDevice 2.关闭设备
func (c *Ctx) SDFCloseDevice(deviceHandle SessionHandle) (err error) {
var err1 = C.SDFCloseDevice(c.libHandle, C.SGD_HANDLE(deviceHandle))
return ToError(err1)
}
// SDFOpenSession 3.创建会话
func (c *Ctx) SDFOpenSession(deviceHandle SessionHandle) (SessionHandle, error) {
var s C.SGD_HANDLE
var err1 = C.SDFOpenSession(c.libHandle, C.SGD_HANDLE(deviceHandle), &s)
return SessionHandle(s), ToError(err1)
}
// SDFCloseSession 4.关闭会话
func (c *Ctx) SDFCloseSession(sessionHandle SessionHandle) (err error) {
var err1 = C.SDFCloseSession(c.libHandle, C.SGD_HANDLE(sessionHandle))
return ToError(err1)
}
// SDFGetDeviceInfo 5.获取设备信息
func (c *Ctx) SDFGetDeviceInfo(sessionHandle SessionHandle) (deviceInfo DeviceInfo, err error) {
var deviceInfo1 C.DEVICEINFO
var err1 = C.SDFGetDeviceInfo(c.libHandle, C.SGD_HANDLE(sessionHandle), &deviceInfo1)
deviceInfo = ConvertToDeviceInfoGo(deviceInfo1)
err = ToError(err1)
return deviceInfo, err
}
// SDFGenerateRandom 6.产生随机数
func (c *Ctx) SDFGenerateRandom(sessionHandle SessionHandle, length uint) (randomData []byte, err error) {
var random C.SGD_UCHAR_PRT
var err1 = C.SDFGenerateRandom(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_UINT32(length), &random)
err = ToError(err1)
randomData = C.GoBytes(unsafe.Pointer(random), C.int(length))
C.free(unsafe.Pointer(random))
return randomData, err
}
// SDFGetPrivateKeyAccessRight 7.获取私钥使用权限
func (c *Ctx) SDFGetPrivateKeyAccessRight(sessionHandle SessionHandle, keyIndex uint, password []byte, pwdLength uint) (err error) {
var err1 = C.SDFGetPrivateKeyAccessRight(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_UINT32(keyIndex), CMessage(password), C.SGD_UINT32(pwdLength))
err = ToError(err1)
return err
}
// SDFReleasePrivateKeyAccessRight 8.释放私钥使用权限
func (c *Ctx) SDFReleasePrivateKeyAccessRight(sessionHandle SessionHandle, keyIndex uint) (err error) {
var err1 = C.SDFReleasePrivateKeyAccessRight(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_UINT32(keyIndex))
err = ToError(err1)
return err
}
// SDFExportSignPublicKey_ECC 16.导出 ECC签名公钥
func (c *Ctx) SDFExportSignPublicKey_ECC(sessionHandle SessionHandle, uiKeyIndex uint) (publicKey ECCrefPublicKey, err error) {
var pucPublicKey C.ECCrefPublicKey
var err1 = C.SDFExportSignPublicKey_ECC(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_UINT32(uiKeyIndex), &pucPublicKey)
publicKey = ConvertToECCrefPublicKeyGo(pucPublicKey)
err = ToError(err1)
return publicKey, err
}
// SDFExportEncPublicKey_ECC 17.导出 ECC加密公钥
func (c *Ctx) SDFExportEncPublicKey_ECC(sessionHandle SessionHandle, uiKeyIndex uint) (publicKey ECCrefPublicKey, err error) {
var err1 C.SGD_RV
var pucPublicKey C.ECCrefPublicKey
err1 = C.SDFExportEncPublicKey_ECC(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_UINT32(uiKeyIndex), &pucPublicKey)
publicKey = ConvertToECCrefPublicKeyGo(pucPublicKey)
err = ToError(err1)
return publicKey, err
}
// SDFGenerateKeyPair_ECC 18.产生 ECC非对称密钥对并输出
func (c *Ctx) SDFGenerateKeyPair_ECC(sessionHandle SessionHandle, uiAlgID uint, uiKeyBits uint) (publicKey ECCrefPublicKey, privateKey ECCrefPrivateKey, err error) {
var err1 C.SGD_RV
var pucPublicKey C.ECCrefPublicKey
var pucPrivateKey C.ECCrefPrivateKey
err1 = C.SDFGenerateKeyPair_ECC(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_UINT32(uiAlgID), C.SGD_UINT32(uiKeyBits), &pucPublicKey, &pucPrivateKey)
publicKey = ConvertToECCrefPublicKeyGo(pucPublicKey)
privateKey = ConvertToECCrefPrivateKeyGo(pucPrivateKey)
err = ToError(err1)
return publicKey, privateKey, err
}
// SDFImportKey 28.导入明文会话密钥
func (c *Ctx) SDFImportKey(sessionHandle SessionHandle, pucKey []byte, uiKeyLength uint) (keyHandle SessionHandle, err error) {
var err1 C.SGD_RV
var phKeyHandle C.SGD_HANDLE
err1 = C.SDFImportKey(c.libHandle, C.SGD_HANDLE(sessionHandle), CMessage(pucKey), C.SGD_UINT32(uiKeyLength), &phKeyHandle)
keyHandle = SessionHandle(phKeyHandle)
err = ToError(err1)
return keyHandle, err
}
// SDFDestroyKey 29.销毁会话密钥
func (c *Ctx) SDFDestroyKey(sessionHandle SessionHandle, hKeyHandle SessionHandle) (err error) {
var err1 = C.SDFDestroyKey(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_HANDLE(hKeyHandle))
err = ToError(err1)
return err
}
// SDFExternalSign_ECC 34. 外部密钥ECC签名
func (c *Ctx) SDFExternalSign_ECC(sessionHandle SessionHandle, uiAlgID uint, privateKey ECCrefPrivateKey, pucData []byte, uiDataLength uint) (signature ECCSignature, err error) {
pucPrivateKey := ConvertToECCrefPrivateKeyC(privateKey)
var pucSignature C.ECCSignature
var err1 = C.SDFExternalSign_ECC(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_UINT32(uiAlgID), &pucPrivateKey, CMessage(pucData), C.SGD_UINT32(uiDataLength), &pucSignature)
signature = ConvertToECCSignatureGo(pucSignature)
err = ToError(err1)
return signature, err
}
// SDFExternalVerify_ECC 35.外部密钥 ECC验证
func (c *Ctx) SDFExternalVerify_ECC(sessionHandle SessionHandle, uiAlgID uint, publicKey ECCrefPublicKey, inputData []byte, uiInputLength uint, signature ECCSignature) (err error) {
var err1 C.SGD_RV
pucPublicKey := ConvertToECCrefPublicKeyC(publicKey)
pucSignature := ConvertToECCSignatureC(signature)
err1 = C.SDFExternalVerify_ECC(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_UINT32(uiAlgID), &pucPublicKey, CMessage(inputData), C.SGD_UINT32(uiInputLength), &pucSignature)
err = ToError(err1)
return err
}
// SDFInternalSign_ECC 36.内部密钥 ECC签名
func (c *Ctx) SDFInternalSign_ECC(sessionHandle SessionHandle, uiISKIndex uint, pucData []byte, uiDataLength uint) (signature ECCSignature, err error) {
var err1 C.SGD_RV
var pucSignature C.ECCSignature
err1 = C.SDFInternalSign_ECC(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_UINT32(uiISKIndex), CMessage(pucData), C.SGD_UINT32(uiDataLength), &pucSignature)
signature = ConvertToECCSignatureGo(pucSignature)
err = ToError(err1)
return signature, err
}
// SDFInternalVerify_ECC 37.内部密钥 ECC验证
func (c *Ctx) SDFInternalVerify_ECC(sessionHandle SessionHandle, uiISKIndex uint, pucData []byte, uiDataLength uint, signature ECCSignature) (err error) {
var err1 C.SGD_RV
var pucSignature C.ECCSignature
for i := 0; i < len(signature.R); i++ {
pucSignature.r[i] = C.SGD_UCHAR(signature.R[i])
}
for i := 0; i < len(signature.S); i++ {
pucSignature.s[i] = C.SGD_UCHAR(signature.S[i])
}
err1 = C.SDFInternalVerify_ECC(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_UINT32(uiISKIndex), CMessage(pucData), C.SGD_UINT32(uiDataLength), &pucSignature)
err = ToError(err1)
return err
}
// SDFExternalEncrypt_ECC 38.外部密钥 ECC加密
func (c *Ctx) SDFExternalEncrypt_ECC(sessionHandle SessionHandle, uiAlgID uint, publicKey ECCrefPublicKey, data []byte, dataLength uint) (encData ECCCipher, err error) {
var err1 C.SGD_RV
pucPublicKey := ConvertToECCrefPublicKeyC(publicKey)
var pucEncData C.ECCCipher
err1 = C.SDFExternalEncrypt_ECC(c.libHandle, C.SGD_HANDLE(sessionHandle), C.SGD_UINT32(uiAlgID), &pucPublicKey, CMessage(data), C.SGD_UINT32(dataLength), &pucEncData)
encData = ConvertToECCCipherGo(pucEncData)
err = ToError(err1)
return encData, err
}
// SDFExternalDecrypt_ECC 39.外部密钥 ECC解密