1 /*
2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License); you may
5 * not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 */
9
10
11 #include <string.h>
12 #include "sdf_ext.h"
13 #include "sdf_int.h"
14
15 SDF_METHOD *sdf_method = NULL;
16 SDF_VENDOR *sdf_vendor = NULL;
17 extern SDF_VENDOR sdf_sansec;
18
19
20 #define SDFerr(reason) fprintf(stderr,"sdfutil: %s %d: %s %s\n", __FILE__, __LINE__, __FUNCTION__, reason)
21
22
23 #define SDF_R_LOAD_LIBRARY_FAILURE "SDF_R_LOAD_LIBRARY_FAILURE"
24 #define SDF_R_NOT_INITIALIZED "SDF_R_NOT_INITIALIZED"
25 #define SDF_R_NOT_SUPPORTED_ECC_ALGOR "SDF_R_NOT_SUPPORTED_ECC_ALGOR"
26 #define SDF_R_NOT_SUPPORTED_CIPHER_ALGOR "SDF_R_NOT_SUPPORTED_CIPHER_ALGOR"
27 #define SDF_R_BUFFER_TOO_SMALL "SDF_R_BUFFER_TOO_SMALL"
28 #define SDF_R_NOT_SUPPORTED_PKEY_ALGOR "SDF_R_NOT_SUPPORTED_PKEY_ALGOR"
29 #define SDF_R_NOT_SUPPORTED_DIGEST_ALGOR "SDF_R_NOT_SUPPORTED_DIGEST_ALGOR"
30 #define ERR_R_SDF_LIB "ERR_R_SDF_LIB"
31
32
33
SDF_LoadLibrary(char * so_path,char * vendor)34 int SDF_LoadLibrary(char *so_path, char *vendor)
35 {
36 if (sdf_method) {
37 SDF_METHOD_free(sdf_method);
38 sdf_method = NULL;
39 }
40
41 if (!(sdf_method = SDF_METHOD_load_library(so_path))) {
42 SDFerr(SDF_R_LOAD_LIBRARY_FAILURE);
43 return SDR_BASE;
44 }
45
46 if (vendor) {
47 if (strcmp(vendor, sdf_sansec.name) == 0) {
48 sdf_vendor = &sdf_sansec;
49 }
50 }
51
52 return SDR_OK;
53 }
54
SDF_UnloadLibrary(void)55 int SDF_UnloadLibrary(void)
56 {
57 SDF_METHOD_free(sdf_method);
58 sdf_method = NULL;
59 sdf_vendor = NULL;
60 return SDR_OK;
61 }
62
SDF_OpenDevice(void ** phDeviceHandle)63 int SDF_OpenDevice(
64 void **phDeviceHandle)
65 {
66 int ret = SDR_UNKNOWERR;
67
68 if (!sdf_method || !sdf_method->OpenDevice) {
69 SDFerr(SDF_R_NOT_INITIALIZED);
70 return SDR_NOTSUPPORT;
71 }
72
73 if ((ret = sdf_method->OpenDevice(
74 phDeviceHandle)) != SDR_OK) {
75 SDFerr(SDF_GetErrorReason(ret));
76 return ret;
77 }
78
79 return SDR_OK;
80 }
81
SDF_CloseDevice(void * hDeviceHandle)82 int SDF_CloseDevice(
83 void *hDeviceHandle)
84 {
85 int ret = SDR_UNKNOWERR;
86
87 if (!sdf_method || !sdf_method->CloseDevice) {
88 SDFerr(SDF_R_NOT_INITIALIZED);
89 return SDR_NOTSUPPORT;
90 }
91
92 if ((ret = sdf_method->CloseDevice(
93 hDeviceHandle)) != SDR_OK) {
94 SDFerr(SDF_GetErrorReason(ret));
95 return ret;
96 }
97
98 return SDR_OK;
99 }
100
SDF_OpenSession(void * hDeviceHandle,void ** phSessionHandle)101 int SDF_OpenSession(
102 void *hDeviceHandle,
103 void **phSessionHandle)
104 {
105 int ret = SDR_UNKNOWERR;
106
107 if (!sdf_method || !sdf_method->OpenSession) {
108 SDFerr(SDF_R_NOT_INITIALIZED);
109 return SDR_NOTSUPPORT;
110 }
111
112 if ((ret = sdf_method->OpenSession(
113 hDeviceHandle,
114 phSessionHandle)) != SDR_OK) {
115 SDFerr(SDF_GetErrorReason(ret));
116 return ret;
117 }
118
119 return SDR_OK;
120 }
121
SDF_CloseSession(void * hSessionHandle)122 int SDF_CloseSession(
123 void *hSessionHandle)
124 {
125 int ret = SDR_UNKNOWERR;
126
127 if (!sdf_method || !sdf_method->CloseSession) {
128 SDFerr(SDF_R_NOT_INITIALIZED);
129 return SDR_NOTSUPPORT;
130 }
131
132 if ((ret = sdf_method->CloseSession(
133 hSessionHandle)) != SDR_OK) {
134 SDFerr(SDF_GetErrorReason(ret));
135 return ret;
136 }
137
138 return SDR_OK;
139 }
140
SDF_GetDeviceInfo(void * hSessionHandle,DEVICEINFO * pstDeviceInfo)141 int SDF_GetDeviceInfo(
142 void *hSessionHandle,
143 DEVICEINFO *pstDeviceInfo)
144 {
145 int ret = SDR_UNKNOWERR;
146
147 if (!sdf_method || !sdf_method->GetDeviceInfo) {
148 SDFerr(SDF_R_NOT_INITIALIZED);
149 return SDR_NOTSUPPORT;
150 }
151
152 if ((ret = sdf_method->GetDeviceInfo(
153 hSessionHandle,
154 pstDeviceInfo)) != SDR_OK) {
155 SDFerr(SDF_GetErrorReason(ret));
156 return ret;
157 }
158
159 return SDR_OK;
160 }
161
SDF_GenerateRandom(void * hSessionHandle,unsigned int uiLength,unsigned char * pucRandom)162 int SDF_GenerateRandom(
163 void *hSessionHandle,
164 unsigned int uiLength,
165 unsigned char *pucRandom)
166 {
167 int ret = SDR_UNKNOWERR;
168
169 if (!sdf_method || !sdf_method->GenerateRandom) {
170 SDFerr(SDF_R_NOT_INITIALIZED);
171 return SDR_NOTSUPPORT;
172 }
173
174 if ((ret = sdf_method->GenerateRandom(
175 hSessionHandle,
176 uiLength,
177 pucRandom)) != SDR_OK) {
178 SDFerr(SDF_GetErrorReason(ret));
179 return ret;
180 }
181
182 return SDR_OK;
183 }
184
SDF_GetPrivateKeyAccessRight(void * hSessionHandle,unsigned int uiKeyIndex,unsigned char * pucPassword,unsigned int uiPwdLength)185 int SDF_GetPrivateKeyAccessRight(
186 void *hSessionHandle,
187 unsigned int uiKeyIndex,
188 unsigned char *pucPassword,
189 unsigned int uiPwdLength)
190 {
191 int ret = SDR_UNKNOWERR;
192
193 if (!sdf_method || !sdf_method->GetPrivateKeyAccessRight) {
194 SDFerr(SDF_R_NOT_INITIALIZED);
195 return SDR_NOTSUPPORT;
196 }
197
198 if ((ret = sdf_method->GetPrivateKeyAccessRight(
199 hSessionHandle,
200 uiKeyIndex,
201 pucPassword,
202 uiPwdLength)) != SDR_OK) {
203 SDFerr(SDF_GetErrorReason(ret));
204 return ret;
205 }
206
207 return SDR_OK;
208 }
209
SDF_ReleasePrivateKeyAccessRight(void * hSessionHandle,unsigned int uiKeyIndex)210 int SDF_ReleasePrivateKeyAccessRight(
211 void *hSessionHandle,
212 unsigned int uiKeyIndex)
213 {
214 int ret = SDR_UNKNOWERR;
215
216 if (!sdf_method || !sdf_method->ReleasePrivateKeyAccessRight) {
217 SDFerr(SDF_R_NOT_INITIALIZED);
218 return SDR_NOTSUPPORT;
219 }
220
221 if ((ret = sdf_method->ReleasePrivateKeyAccessRight(
222 hSessionHandle,
223 uiKeyIndex)) != SDR_OK) {
224 SDFerr(SDF_GetErrorReason(ret));
225 return ret;
226 }
227
228 return SDR_OK;
229 }
230
SDF_ExportSignPublicKey_RSA(void * hSessionHandle,unsigned int uiKeyIndex,RSArefPublicKey * pucPublicKey)231 int SDF_ExportSignPublicKey_RSA(
232 void *hSessionHandle,
233 unsigned int uiKeyIndex,
234 RSArefPublicKey *pucPublicKey)
235 {
236 int ret = SDR_UNKNOWERR;
237
238 if (!sdf_method || !sdf_method->ExportSignPublicKey_RSA) {
239 SDFerr(SDF_R_NOT_INITIALIZED);
240 return SDR_NOTSUPPORT;
241 }
242
243 if ((ret = sdf_method->ExportSignPublicKey_RSA(
244 hSessionHandle,
245 uiKeyIndex,
246 pucPublicKey)) != SDR_OK) {
247 SDFerr(SDF_GetErrorReason(ret));
248 return ret;
249 }
250
251 return SDR_OK;
252 }
253
SDF_ExportEncPublicKey_RSA(void * hSessionHandle,unsigned int uiKeyIndex,RSArefPublicKey * pucPublicKey)254 int SDF_ExportEncPublicKey_RSA(
255 void *hSessionHandle,
256 unsigned int uiKeyIndex,
257 RSArefPublicKey *pucPublicKey)
258 {
259 int ret = SDR_UNKNOWERR;
260
261 if (!sdf_method || !sdf_method->ExportEncPublicKey_RSA) {
262 SDFerr(SDF_R_NOT_INITIALIZED);
263 return SDR_NOTSUPPORT;
264 }
265
266 if ((ret = sdf_method->ExportEncPublicKey_RSA(
267 hSessionHandle,
268 uiKeyIndex,
269 pucPublicKey)) != SDR_OK) {
270 SDFerr(SDF_GetErrorReason(ret));
271 return ret;
272 }
273
274 return SDR_OK;
275 }
276
SDF_GenerateKeyPair_RSA(void * hSessionHandle,unsigned int uiKeyBits,RSArefPublicKey * pucPublicKey,RSArefPrivateKey * pucPrivateKey)277 int SDF_GenerateKeyPair_RSA(
278 void *hSessionHandle,
279 unsigned int uiKeyBits,
280 RSArefPublicKey *pucPublicKey,
281 RSArefPrivateKey *pucPrivateKey)
282 {
283 int ret = SDR_UNKNOWERR;
284
285 if (!sdf_method || !sdf_method->GenerateKeyPair_RSA) {
286 SDFerr(SDF_R_NOT_INITIALIZED);
287 return SDR_NOTSUPPORT;
288 }
289
290 if ((ret = sdf_method->GenerateKeyPair_RSA(
291 hSessionHandle,
292 uiKeyBits,
293 pucPublicKey,
294 pucPrivateKey)) != SDR_OK) {
295 SDFerr(SDF_GetErrorReason(ret));
296 return ret;
297 }
298
299 return SDR_OK;
300 }
301
SDF_GenerateKeyWithIPK_RSA(void * hSessionHandle,unsigned int uiIPKIndex,unsigned int uiKeyBits,unsigned char * pucKey,unsigned int * puiKeyLength,void ** phKeyHandle)302 int SDF_GenerateKeyWithIPK_RSA(
303 void *hSessionHandle,
304 unsigned int uiIPKIndex,
305 unsigned int uiKeyBits,
306 unsigned char *pucKey,
307 unsigned int *puiKeyLength,
308 void **phKeyHandle)
309 {
310 int ret = SDR_UNKNOWERR;
311
312 if (!sdf_method || !sdf_method->GenerateKeyWithIPK_RSA) {
313 SDFerr(SDF_R_NOT_INITIALIZED);
314 return SDR_NOTSUPPORT;
315 }
316
317 if ((ret = sdf_method->GenerateKeyWithIPK_RSA(
318 hSessionHandle,
319 uiIPKIndex,
320 uiKeyBits,
321 pucKey,
322 puiKeyLength,
323 phKeyHandle)) != SDR_OK) {
324 SDFerr(SDF_GetErrorReason(ret));
325 return ret;
326 }
327
328 return SDR_OK;
329 }
330
SDF_GenerateKeyWithEPK_RSA(void * hSessionHandle,unsigned int uiKeyBits,RSArefPublicKey * pucPublicKey,unsigned char * pucKey,unsigned int * puiKeyLength,void ** phKeyHandle)331 int SDF_GenerateKeyWithEPK_RSA(
332 void *hSessionHandle,
333 unsigned int uiKeyBits,
334 RSArefPublicKey *pucPublicKey,
335 unsigned char *pucKey,
336 unsigned int *puiKeyLength,
337 void **phKeyHandle)
338 {
339 int ret = SDR_UNKNOWERR;
340
341 if (!sdf_method || !sdf_method->GenerateKeyWithEPK_RSA) {
342 SDFerr(SDF_R_NOT_INITIALIZED);
343 return SDR_NOTSUPPORT;
344 }
345
346 if ((ret = sdf_method->GenerateKeyWithEPK_RSA(
347 hSessionHandle,
348 uiKeyBits,
349 pucPublicKey,
350 pucKey,
351 puiKeyLength,
352 phKeyHandle)) != SDR_OK) {
353 SDFerr(SDF_GetErrorReason(ret));
354 return ret;
355 }
356
357 return SDR_OK;
358 }
359
SDF_ImportKeyWithISK_RSA(void * hSessionHandle,unsigned int uiISKIndex,unsigned char * pucKey,unsigned int uiKeyLength,void ** phKeyHandle)360 int SDF_ImportKeyWithISK_RSA(
361 void *hSessionHandle,
362 unsigned int uiISKIndex,
363 unsigned char *pucKey,
364 unsigned int uiKeyLength,
365 void **phKeyHandle)
366 {
367 int ret = SDR_UNKNOWERR;
368
369 if (!sdf_method || !sdf_method->ImportKeyWithISK_RSA) {
370 SDFerr(SDF_R_NOT_INITIALIZED);
371 return SDR_NOTSUPPORT;
372 }
373
374 if ((ret = sdf_method->ImportKeyWithISK_RSA(
375 hSessionHandle,
376 uiISKIndex,
377 pucKey,
378 uiKeyLength,
379 phKeyHandle)) != SDR_OK) {
380 SDFerr(SDF_GetErrorReason(ret));
381 return ret;
382 }
383
384 return SDR_OK;
385 }
386
SDF_ExchangeDigitEnvelopeBaseOnRSA(void * hSessionHandle,unsigned int uiKeyIndex,RSArefPublicKey * pucPublicKey,unsigned char * pucDEInput,unsigned int uiDELength,unsigned char * pucDEOutput,unsigned int * puiDELength)387 int SDF_ExchangeDigitEnvelopeBaseOnRSA(
388 void *hSessionHandle,
389 unsigned int uiKeyIndex,
390 RSArefPublicKey *pucPublicKey,
391 unsigned char *pucDEInput,
392 unsigned int uiDELength,
393 unsigned char *pucDEOutput,
394 unsigned int *puiDELength)
395 {
396 int ret = SDR_UNKNOWERR;
397
398 if (!sdf_method || !sdf_method->ExchangeDigitEnvelopeBaseOnRSA) {
399 SDFerr(SDF_R_NOT_INITIALIZED);
400 return SDR_NOTSUPPORT;
401 }
402
403 if ((ret = sdf_method->ExchangeDigitEnvelopeBaseOnRSA(
404 hSessionHandle,
405 uiKeyIndex,
406 pucPublicKey,
407 pucDEInput,
408 uiDELength,
409 pucDEOutput,
410 puiDELength)) != SDR_OK) {
411 SDFerr(SDF_GetErrorReason(ret));
412 return ret;
413 }
414
415 return SDR_OK;
416 }
417
SDF_ExportSignPublicKey_ECC(void * hSessionHandle,unsigned int uiKeyIndex,ECCrefPublicKey * pucPublicKey)418 int SDF_ExportSignPublicKey_ECC(
419 void *hSessionHandle,
420 unsigned int uiKeyIndex,
421 ECCrefPublicKey *pucPublicKey)
422 {
423 int ret = SDR_UNKNOWERR;
424
425 if (!sdf_method || !sdf_method->ExportSignPublicKey_ECC) {
426 SDFerr(SDF_R_NOT_INITIALIZED);
427 return SDR_NOTSUPPORT;
428 }
429
430 if ((ret = sdf_method->ExportSignPublicKey_ECC(
431 hSessionHandle,
432 uiKeyIndex,
433 pucPublicKey)) != SDR_OK) {
434 SDFerr(SDF_GetErrorReason(ret));
435 return ret;
436 }
437
438 return SDR_OK;
439 }
440
SDF_ExportEncPublicKey_ECC(void * hSessionHandle,unsigned int uiKeyIndex,ECCrefPublicKey * pucPublicKey)441 int SDF_ExportEncPublicKey_ECC(
442 void *hSessionHandle,
443 unsigned int uiKeyIndex,
444 ECCrefPublicKey *pucPublicKey)
445 {
446 int ret = SDR_UNKNOWERR;
447
448 if (!sdf_method || !sdf_method->ExportEncPublicKey_ECC) {
449 SDFerr(SDF_R_NOT_INITIALIZED);
450 return SDR_NOTSUPPORT;
451 }
452
453 if ((ret = sdf_method->ExportEncPublicKey_ECC(
454 hSessionHandle,
455 uiKeyIndex,
456 pucPublicKey)) != SDR_OK) {
457 SDFerr(SDF_GetErrorReason(ret));
458 return ret;
459 }
460
461 return SDR_OK;
462 }
463
SDF_GenerateKeyPair_ECC(void * hSessionHandle,unsigned int uiAlgID,unsigned int uiKeyBits,ECCrefPublicKey * pucPublicKey,ECCrefPrivateKey * pucPrivateKey)464 int SDF_GenerateKeyPair_ECC(
465 void *hSessionHandle,
466 unsigned int uiAlgID,
467 unsigned int uiKeyBits,
468 ECCrefPublicKey *pucPublicKey,
469 ECCrefPrivateKey *pucPrivateKey)
470 {
471 int ret = SDR_UNKNOWERR;
472
473 if (!sdf_method || !sdf_method->GenerateKeyPair_ECC) {
474 SDFerr(SDF_R_NOT_INITIALIZED);
475 return SDR_NOTSUPPORT;
476 }
477
478 if (sdf_vendor) {
479 if (!(uiAlgID = sdf_vendor->pkey_std2vendor(uiAlgID))) {
480 SDFerr(SDF_R_NOT_SUPPORTED_ECC_ALGOR);
481 return SDR_ALGNOTSUPPORT;
482 }
483 }
484
485 if ((ret = sdf_method->GenerateKeyPair_ECC(
486 hSessionHandle,
487 uiAlgID,
488 uiKeyBits,
489 pucPublicKey,
490 pucPrivateKey)) != SDR_OK) {
491 SDFerr(SDF_GetErrorReason(ret));
492 return ret;
493 }
494
495 return SDR_OK;
496 }
497
SDF_GenerateKeyWithIPK_ECC(void * hSessionHandle,unsigned int uiIPKIndex,unsigned int uiKeyBits,ECCCipher * pucKey,void ** phKeyHandle)498 int SDF_GenerateKeyWithIPK_ECC(
499 void *hSessionHandle,
500 unsigned int uiIPKIndex,
501 unsigned int uiKeyBits,
502 ECCCipher *pucKey,
503 void **phKeyHandle)
504 {
505 int ret = SDR_UNKNOWERR;
506
507 if (!sdf_method || !sdf_method->GenerateKeyWithIPK_ECC) {
508 SDFerr(SDF_R_NOT_INITIALIZED);
509 return SDR_NOTSUPPORT;
510 }
511
512 if ((ret = sdf_method->GenerateKeyWithIPK_ECC(
513 hSessionHandle,
514 uiIPKIndex,
515 uiKeyBits,
516 pucKey,
517 phKeyHandle)) != SDR_OK) {
518 SDFerr(SDF_GetErrorReason(ret));
519 return ret;
520 }
521
522 return SDR_OK;
523 }
524
SDF_GenerateKeyWithEPK_ECC(void * hSessionHandle,unsigned int uiKeyBits,unsigned int uiAlgID,ECCrefPublicKey * pucPublicKey,ECCCipher * pucKey,void ** phKeyHandle)525 int SDF_GenerateKeyWithEPK_ECC(
526 void *hSessionHandle,
527 unsigned int uiKeyBits,
528 unsigned int uiAlgID,
529 ECCrefPublicKey *pucPublicKey,
530 ECCCipher *pucKey,
531 void **phKeyHandle)
532 {
533 int ret = SDR_UNKNOWERR;
534
535 if (!sdf_method || !sdf_method->GenerateKeyWithEPK_ECC) {
536 SDFerr(SDF_R_NOT_INITIALIZED);
537 return SDR_NOTSUPPORT;
538 }
539
540 if (sdf_vendor) {
541 if (!(uiAlgID = sdf_vendor->cipher_std2vendor(uiAlgID))) {
542 SDFerr(SDF_R_NOT_SUPPORTED_CIPHER_ALGOR);
543 return SDR_ALGNOTSUPPORT;
544 }
545 }
546
547 if ((ret = sdf_method->GenerateKeyWithEPK_ECC(
548 hSessionHandle,
549 uiKeyBits,
550 uiAlgID,
551 pucPublicKey,
552 pucKey,
553 phKeyHandle)) != SDR_OK) {
554 SDFerr(SDF_GetErrorReason(ret));
555 return ret;
556 }
557
558 return SDR_OK;
559 }
560
SDF_ImportKeyWithISK_ECC(void * hSessionHandle,unsigned int uiISKIndex,ECCCipher * pucKey,void ** phKeyHandle)561 int SDF_ImportKeyWithISK_ECC(
562 void *hSessionHandle,
563 unsigned int uiISKIndex,
564 ECCCipher *pucKey,
565 void **phKeyHandle)
566 {
567 int ret = SDR_UNKNOWERR;
568
569 if (!sdf_method || !sdf_method->ImportKeyWithISK_ECC) {
570 SDFerr(SDF_R_NOT_INITIALIZED);
571 return SDR_NOTSUPPORT;
572 }
573
574 if ((ret = sdf_method->ImportKeyWithISK_ECC(
575 hSessionHandle,
576 uiISKIndex,
577 pucKey,
578 phKeyHandle)) != SDR_OK) {
579 SDFerr(SDF_GetErrorReason(ret));
580 return ret;
581 }
582
583 return SDR_OK;
584 }
585
SDF_GenerateAgreementDataWithECC(void * hSessionHandle,unsigned int uiISKIndex,unsigned int uiKeyBits,unsigned char * pucSponsorID,unsigned int uiSponsorIDLength,ECCrefPublicKey * pucSponsorPublicKey,ECCrefPublicKey * pucSponsorTmpPublicKey,void ** phAgreementHandle)586 int SDF_GenerateAgreementDataWithECC(
587 void *hSessionHandle,
588 unsigned int uiISKIndex,
589 unsigned int uiKeyBits,
590 unsigned char *pucSponsorID,
591 unsigned int uiSponsorIDLength,
592 ECCrefPublicKey *pucSponsorPublicKey,
593 ECCrefPublicKey *pucSponsorTmpPublicKey,
594 void **phAgreementHandle)
595 {
596 int ret = SDR_UNKNOWERR;
597
598 if (!sdf_method || !sdf_method->GenerateAgreementDataWithECC) {
599 SDFerr(SDF_R_NOT_INITIALIZED);
600 return SDR_NOTSUPPORT;
601 }
602
603 if ((ret = sdf_method->GenerateAgreementDataWithECC(
604 hSessionHandle,
605 uiISKIndex,
606 uiKeyBits,
607 pucSponsorID,
608 uiSponsorIDLength,
609 pucSponsorPublicKey,
610 pucSponsorTmpPublicKey,
611 phAgreementHandle)) != SDR_OK) {
612 SDFerr(SDF_GetErrorReason(ret));
613 return ret;
614 }
615
616 return SDR_OK;
617 }
618
SDF_GenerateKeyWithECC(void * hSessionHandle,unsigned char * pucResponseID,unsigned int uiResponseIDLength,ECCrefPublicKey * pucResponsePublicKey,ECCrefPublicKey * pucResponseTmpPublicKey,void * hAgreementHandle,void ** phKeyHandle)619 int SDF_GenerateKeyWithECC(
620 void *hSessionHandle,
621 unsigned char *pucResponseID,
622 unsigned int uiResponseIDLength,
623 ECCrefPublicKey *pucResponsePublicKey,
624 ECCrefPublicKey *pucResponseTmpPublicKey,
625 void *hAgreementHandle,
626 void **phKeyHandle)
627 {
628 int ret = SDR_UNKNOWERR;
629
630 if (!sdf_method || !sdf_method->GenerateKeyWithECC) {
631 SDFerr(SDF_R_NOT_INITIALIZED);
632 return SDR_NOTSUPPORT;
633 }
634
635 if ((ret = sdf_method->GenerateKeyWithECC(
636 hSessionHandle,
637 pucResponseID,
638 uiResponseIDLength,
639 pucResponsePublicKey,
640 pucResponseTmpPublicKey,
641 hAgreementHandle,
642 phKeyHandle)) != SDR_OK) {
643 SDFerr(SDF_GetErrorReason(ret));
644 return ret;
645 }
646
647 return SDR_OK;
648 }
649
SDF_GenerateAgreementDataAndKeyWithECC(void * hSessionHandle,unsigned int uiISKIndex,unsigned int uiKeyBits,unsigned char * pucResponseID,unsigned int uiResponseIDLength,unsigned char * pucSponsorID,unsigned int uiSponsorIDLength,ECCrefPublicKey * pucSponsorPublicKey,ECCrefPublicKey * pucSponsorTmpPublicKey,ECCrefPublicKey * pucResponsePublicKey,ECCrefPublicKey * pucResponseTmpPublicKey,void ** phKeyHandle)650 int SDF_GenerateAgreementDataAndKeyWithECC(
651 void *hSessionHandle,
652 unsigned int uiISKIndex,
653 unsigned int uiKeyBits,
654 unsigned char *pucResponseID,
655 unsigned int uiResponseIDLength,
656 unsigned char *pucSponsorID,
657 unsigned int uiSponsorIDLength,
658 ECCrefPublicKey *pucSponsorPublicKey,
659 ECCrefPublicKey *pucSponsorTmpPublicKey,
660 ECCrefPublicKey *pucResponsePublicKey,
661 ECCrefPublicKey *pucResponseTmpPublicKey,
662 void **phKeyHandle)
663 {
664 int ret = SDR_UNKNOWERR;
665
666 if (!sdf_method || !sdf_method->GenerateAgreementDataAndKeyWithECC) {
667 SDFerr(SDF_R_NOT_INITIALIZED);
668 return SDR_NOTSUPPORT;
669 }
670
671 if ((ret = sdf_method->GenerateAgreementDataAndKeyWithECC(
672 hSessionHandle,
673 uiISKIndex,
674 uiKeyBits,
675 pucResponseID,
676 uiResponseIDLength,
677 pucSponsorID,
678 uiSponsorIDLength,
679 pucSponsorPublicKey,
680 pucSponsorTmpPublicKey,
681 pucResponsePublicKey,
682 pucResponseTmpPublicKey,
683 phKeyHandle)) != SDR_OK) {
684 SDFerr(SDF_GetErrorReason(ret));
685 return ret;
686 }
687
688 return SDR_OK;
689 }
690
SDF_ExchangeDigitEnvelopeBaseOnECC(void * hSessionHandle,unsigned int uiKeyIndex,unsigned int uiAlgID,ECCrefPublicKey * pucPublicKey,ECCCipher * pucEncDataIn,ECCCipher * pucEncDataOut)691 int SDF_ExchangeDigitEnvelopeBaseOnECC(
692 void *hSessionHandle,
693 unsigned int uiKeyIndex,
694 unsigned int uiAlgID,
695 ECCrefPublicKey *pucPublicKey,
696 ECCCipher *pucEncDataIn,
697 ECCCipher *pucEncDataOut)
698 {
699 int ret = SDR_UNKNOWERR;
700
701 if (!sdf_method || !sdf_method->ExchangeDigitEnvelopeBaseOnECC) {
702 SDFerr(SDF_R_NOT_INITIALIZED);
703 return SDR_NOTSUPPORT;
704 }
705
706 if (sdf_vendor) {
707 if (!(uiAlgID = sdf_vendor->cipher_std2vendor(uiAlgID))) {
708 SDFerr(SDF_R_NOT_SUPPORTED_CIPHER_ALGOR);
709 return SDR_ALGNOTSUPPORT;
710 }
711 }
712
713 if ((ret = sdf_method->ExchangeDigitEnvelopeBaseOnECC(
714 hSessionHandle,
715 uiKeyIndex,
716 uiAlgID,
717 pucPublicKey,
718 pucEncDataIn,
719 pucEncDataOut)) != SDR_OK) {
720 SDFerr(SDF_GetErrorReason(ret));
721 return ret;
722 }
723
724 return SDR_OK;
725 }
726
SDF_GenerateKeyWithKEK(void * hSessionHandle,unsigned int uiKeyBits,unsigned int uiAlgID,unsigned int uiKEKIndex,unsigned char * pucKey,unsigned int * puiKeyLength,void ** phKeyHandle)727 int SDF_GenerateKeyWithKEK(
728 void *hSessionHandle,
729 unsigned int uiKeyBits,
730 unsigned int uiAlgID,
731 unsigned int uiKEKIndex,
732 unsigned char *pucKey,
733 unsigned int *puiKeyLength,
734 void **phKeyHandle)
735 {
736 int ret = SDR_UNKNOWERR;
737
738 if (!sdf_method || !sdf_method->GenerateKeyWithKEK) {
739 SDFerr(SDF_R_NOT_INITIALIZED);
740 return SDR_NOTSUPPORT;
741 }
742
743 if (sdf_vendor) {
744 if (!(uiAlgID = sdf_vendor->cipher_std2vendor(uiAlgID))) {
745 SDFerr(SDF_R_NOT_SUPPORTED_CIPHER_ALGOR);
746 return SDR_ALGNOTSUPPORT;
747 }
748 }
749
750 if ((ret = sdf_method->GenerateKeyWithKEK(
751 hSessionHandle,
752 uiKeyBits,
753 uiAlgID,
754 uiKEKIndex,
755 pucKey,
756 puiKeyLength,
757 phKeyHandle)) != SDR_OK) {
758 SDFerr(SDF_GetErrorReason(ret));
759 return ret;
760 }
761
762 return SDR_OK;
763 }
764
SDF_ImportKeyWithKEK(void * hSessionHandle,unsigned int uiAlgID,unsigned int uiKEKIndex,unsigned char * pucKey,unsigned int uiKeyLength,void ** phKeyHandle)765 int SDF_ImportKeyWithKEK(
766 void *hSessionHandle,
767 unsigned int uiAlgID,
768 unsigned int uiKEKIndex,
769 unsigned char *pucKey,
770 unsigned int uiKeyLength,
771 void **phKeyHandle)
772 {
773 int ret = SDR_UNKNOWERR;
774
775 if (!sdf_method || !sdf_method->ImportKeyWithKEK) {
776 SDFerr(SDF_R_NOT_INITIALIZED);
777 return SDR_NOTSUPPORT;
778 }
779
780 if (sdf_vendor) {
781 if (!(uiAlgID = sdf_vendor->cipher_std2vendor(uiAlgID))) {
782 SDFerr(SDF_R_NOT_SUPPORTED_CIPHER_ALGOR);
783 return SDR_ALGNOTSUPPORT;
784 }
785 }
786
787 if ((ret = sdf_method->ImportKeyWithKEK(
788 hSessionHandle,
789 uiAlgID,
790 uiKEKIndex,
791 pucKey,
792 uiKeyLength,
793 phKeyHandle)) != SDR_OK) {
794 SDFerr(SDF_GetErrorReason(ret));
795 return ret;
796 }
797
798 return SDR_OK;
799 }
800
SDF_DestroyKey(void * hSessionHandle,void * hKeyHandle)801 int SDF_DestroyKey(
802 void *hSessionHandle,
803 void *hKeyHandle)
804 {
805 int ret = SDR_UNKNOWERR;
806
807 if (!sdf_method || !sdf_method->DestroyKey) {
808 SDFerr(SDF_R_NOT_INITIALIZED);
809 return SDR_NOTSUPPORT;
810 }
811
812 if ((ret = sdf_method->DestroyKey(
813 hSessionHandle,
814 hKeyHandle)) != SDR_OK) {
815 SDFerr(SDF_GetErrorReason(ret));
816 return ret;
817 }
818
819 return SDR_OK;
820 }
821
SDF_ExternalPublicKeyOperation_RSA(void * hSessionHandle,RSArefPublicKey * pucPublicKey,unsigned char * pucDataInput,unsigned int uiInputLength,unsigned char * pucDataOutput,unsigned int * puiOutputLength)822 int SDF_ExternalPublicKeyOperation_RSA(
823 void *hSessionHandle,
824 RSArefPublicKey *pucPublicKey,
825 unsigned char *pucDataInput,
826 unsigned int uiInputLength,
827 unsigned char *pucDataOutput,
828 unsigned int *puiOutputLength)
829 {
830 int ret = SDR_UNKNOWERR;
831
832 if (!sdf_method || !sdf_method->ExternalPublicKeyOperation_RSA) {
833 SDFerr(SDF_R_NOT_INITIALIZED);
834 return SDR_NOTSUPPORT;
835 }
836
837 if ((ret = sdf_method->ExternalPublicKeyOperation_RSA(
838 hSessionHandle,
839 pucPublicKey,
840 pucDataInput,
841 uiInputLength,
842 pucDataOutput,
843 puiOutputLength)) != SDR_OK) {
844 SDFerr(SDF_GetErrorReason(ret));
845 return ret;
846 }
847
848 return SDR_OK;
849 }
850
SDF_InternalPublicKeyOperation_RSA(void * hSessionHandle,unsigned int uiKeyIndex,unsigned char * pucDataInput,unsigned int uiInputLength,unsigned char * pucDataOutput,unsigned int * puiOutputLength)851 int SDF_InternalPublicKeyOperation_RSA(
852 void *hSessionHandle,
853 unsigned int uiKeyIndex,
854 unsigned char *pucDataInput,
855 unsigned int uiInputLength,
856 unsigned char *pucDataOutput,
857 unsigned int *puiOutputLength)
858 {
859 int ret = SDR_UNKNOWERR;
860
861 if (!sdf_method || !sdf_method->InternalPublicKeyOperation_RSA) {
862 SDFerr(SDF_R_NOT_INITIALIZED);
863 return SDR_NOTSUPPORT;
864 }
865
866 if ((ret = sdf_method->InternalPublicKeyOperation_RSA(
867 hSessionHandle,
868 uiKeyIndex,
869 pucDataInput,
870 uiInputLength,
871 pucDataOutput,
872 puiOutputLength)) != SDR_OK) {
873 SDFerr(SDF_GetErrorReason(ret));
874 return ret;
875 }
876
877 return SDR_OK;
878 }
879
SDF_InternalPrivateKeyOperation_RSA(void * hSessionHandle,unsigned int uiKeyIndex,unsigned char * pucDataInput,unsigned int uiInputLength,unsigned char * pucDataOutput,unsigned int * puiOutputLength)880 int SDF_InternalPrivateKeyOperation_RSA(
881 void *hSessionHandle,
882 unsigned int uiKeyIndex,
883 unsigned char *pucDataInput,
884 unsigned int uiInputLength,
885 unsigned char *pucDataOutput,
886 unsigned int *puiOutputLength)
887 {
888 int ret = SDR_UNKNOWERR;
889
890 if (!sdf_method || !sdf_method->InternalPrivateKeyOperation_RSA) {
891 SDFerr(SDF_R_NOT_INITIALIZED);
892 return SDR_NOTSUPPORT;
893 }
894
895 if ((ret = sdf_method->InternalPrivateKeyOperation_RSA(
896 hSessionHandle,
897 uiKeyIndex,
898 pucDataInput,
899 uiInputLength,
900 pucDataOutput,
901 puiOutputLength)) != SDR_OK) {
902 SDFerr(SDF_GetErrorReason(ret));
903 return ret;
904 }
905
906 return SDR_OK;
907 }
908
SDF_ExternalVerify_ECC(void * hSessionHandle,unsigned int uiAlgID,ECCrefPublicKey * pucPublicKey,unsigned char * pucDataInput,unsigned int uiInputLength,ECCSignature * pucSignature)909 int SDF_ExternalVerify_ECC(
910 void *hSessionHandle,
911 unsigned int uiAlgID,
912 ECCrefPublicKey *pucPublicKey,
913 unsigned char *pucDataInput,
914 unsigned int uiInputLength,
915 ECCSignature *pucSignature)
916 {
917 int ret = SDR_UNKNOWERR;
918
919 if (!sdf_method || !sdf_method->ExternalVerify_ECC) {
920 SDFerr(SDF_R_NOT_INITIALIZED);
921 return SDR_NOTSUPPORT;
922 }
923
924 if (sdf_vendor) {
925 if (!(uiAlgID = sdf_vendor->pkey_std2vendor(uiAlgID))) {
926 SDFerr(SDF_R_NOT_SUPPORTED_PKEY_ALGOR);
927 return SDR_ALGNOTSUPPORT;
928 }
929 }
930
931 if ((ret = sdf_method->ExternalVerify_ECC(
932 hSessionHandle,
933 uiAlgID,
934 pucPublicKey,
935 pucDataInput,
936 uiInputLength,
937 pucSignature)) != SDR_OK) {
938 SDFerr(SDF_GetErrorReason(ret));
939 return ret;
940 }
941
942 return SDR_OK;
943 }
944
SDF_InternalSign_ECC(void * hSessionHandle,unsigned int uiISKIndex,unsigned char * pucData,unsigned int uiDataLength,ECCSignature * pucSignature)945 int SDF_InternalSign_ECC(
946 void *hSessionHandle,
947 unsigned int uiISKIndex,
948 unsigned char *pucData,
949 unsigned int uiDataLength,
950 ECCSignature *pucSignature)
951 {
952 int ret = SDR_UNKNOWERR;
953
954 if (!sdf_method || !sdf_method->InternalSign_ECC) {
955 SDFerr(SDF_R_NOT_INITIALIZED);
956 return SDR_NOTSUPPORT;
957 }
958
959 if ((ret = sdf_method->InternalSign_ECC(
960 hSessionHandle,
961 uiISKIndex,
962 pucData,
963 uiDataLength,
964 pucSignature)) != SDR_OK) {
965 SDFerr(SDF_GetErrorReason(ret));
966 return ret;
967 }
968
969 return SDR_OK;
970 }
971
SDF_InternalVerify_ECC(void * hSessionHandle,unsigned int uiIPKIndex,unsigned char * pucData,unsigned int uiDataLength,ECCSignature * pucSignature)972 int SDF_InternalVerify_ECC(
973 void *hSessionHandle,
974 unsigned int uiIPKIndex,
975 unsigned char *pucData,
976 unsigned int uiDataLength,
977 ECCSignature *pucSignature)
978 {
979 int ret = SDR_UNKNOWERR;
980
981 if (!sdf_method || !sdf_method->InternalVerify_ECC) {
982 SDFerr(SDF_R_NOT_INITIALIZED);
983 return SDR_NOTSUPPORT;
984 }
985
986 if ((ret = sdf_method->InternalVerify_ECC(
987 hSessionHandle,
988 uiIPKIndex,
989 pucData,
990 uiDataLength,
991 pucSignature)) != SDR_OK) {
992 SDFerr(SDF_GetErrorReason(ret));
993 return ret;
994 }
995
996 return SDR_OK;
997 }
998
SDF_ExternalEncrypt_ECC(void * hSessionHandle,unsigned int uiAlgID,ECCrefPublicKey * pucPublicKey,unsigned char * pucData,unsigned int uiDataLength,ECCCipher * pucEncData)999 int SDF_ExternalEncrypt_ECC(
1000 void *hSessionHandle,
1001 unsigned int uiAlgID,
1002 ECCrefPublicKey *pucPublicKey,
1003 unsigned char *pucData,
1004 unsigned int uiDataLength,
1005 ECCCipher *pucEncData)
1006 {
1007 int ret = SDR_UNKNOWERR;
1008
1009 if (!sdf_method || !sdf_method->ExternalEncrypt_ECC) {
1010 SDFerr(SDF_R_NOT_INITIALIZED);
1011 return SDR_NOTSUPPORT;
1012 }
1013
1014 if (sdf_vendor) {
1015 if (!(uiAlgID = sdf_vendor->pkey_std2vendor(uiAlgID))) {
1016 SDFerr(SDF_R_NOT_SUPPORTED_PKEY_ALGOR);
1017 return SDR_ALGNOTSUPPORT;
1018 }
1019 }
1020
1021 if ((ret = sdf_method->ExternalEncrypt_ECC(
1022 hSessionHandle,
1023 uiAlgID,
1024 pucPublicKey,
1025 pucData,
1026 uiDataLength,
1027 pucEncData)) != SDR_OK) {
1028 SDFerr(SDF_GetErrorReason(ret));
1029 return ret;
1030 }
1031
1032 return SDR_OK;
1033 }
1034
SDF_InternalEncrypt_ECC(void * hSessionHandle,unsigned int uiIPKIndex,unsigned int uiAlgID,unsigned char * pucData,unsigned int uiDataLength,ECCCipher * pucEncData)1035 int SDF_InternalEncrypt_ECC(
1036 void *hSessionHandle,
1037 unsigned int uiIPKIndex,
1038 unsigned int uiAlgID,
1039 unsigned char *pucData,
1040 unsigned int uiDataLength,
1041 ECCCipher *pucEncData)
1042 {
1043 int ret = SDR_UNKNOWERR;
1044 ECCCipher *buf = pucEncData;
1045
1046 if (!sdf_method || !sdf_method->InternalEncrypt_ECC) {
1047 SDFerr(SDF_R_NOT_INITIALIZED);
1048 return SDR_NOTSUPPORT;
1049 }
1050
1051 if (pucEncData->L < uiDataLength) {
1052 SDFerr(SDF_R_BUFFER_TOO_SMALL);
1053 return SDR_NOBUFFER;
1054 }
1055
1056 if (sdf_vendor && sdf_vendor->decode_ecccipher) {
1057 if (SDF_NewECCCipher(&buf, uiDataLength) != SDR_OK) {
1058 SDFerr(ERR_R_SDF_LIB);
1059 return SDR_UNKNOWERR;
1060 }
1061 }
1062
1063 if (sdf_vendor && sdf_vendor->pkey_std2vendor) {
1064 if (!(uiAlgID = sdf_vendor->pkey_std2vendor(uiAlgID))) {
1065 SDFerr(SDF_R_NOT_SUPPORTED_PKEY_ALGOR);
1066 ret = SDR_ALGNOTSUPPORT;
1067 goto end;
1068 }
1069 }
1070
1071 if ((ret = sdf_method->InternalEncrypt_ECC(
1072 hSessionHandle,
1073 uiIPKIndex,
1074 uiAlgID,
1075 pucData,
1076 uiDataLength,
1077 buf)) != SDR_OK) {
1078 SDFerr(SDF_GetErrorReason(ret));
1079 goto end;
1080 }
1081
1082 if (sdf_vendor && sdf_vendor->decode_ecccipher) {
1083 if (!sdf_vendor->decode_ecccipher(pucEncData, buf)) {
1084 SDFerr(ERR_R_SDF_LIB);
1085 ret = SDR_UNKNOWERR;
1086 goto end;
1087 }
1088 }
1089
1090 /*
1091 {
1092 int i;
1093 unsigned char *p = (unsigned char *)pucEncData;
1094 for (i = 0; i < sizeof(ECCCipher) -1 + uiDataLength; i++) {
1095 printf("%02x", p[i]);
1096 }
1097 printf("\n");
1098 }
1099 */
1100
1101 ret = SDR_OK;
1102
1103 end:
1104 if (sdf_vendor && sdf_vendor->decode_ecccipher && buf) {
1105 SDF_FreeECCCipher(buf);
1106 }
1107 return ret;
1108 }
1109
SDF_InternalDecrypt_ECC(void * hSessionHandle,unsigned int uiISKIndex,unsigned int uiAlgID,ECCCipher * pucEncData,unsigned char * pucData,unsigned int * uiDataLength)1110 int SDF_InternalDecrypt_ECC(
1111 void *hSessionHandle,
1112 unsigned int uiISKIndex,
1113 unsigned int uiAlgID,
1114 ECCCipher *pucEncData,
1115 unsigned char *pucData,
1116 unsigned int *uiDataLength)
1117 {
1118 int ret = SDR_UNKNOWERR;
1119 ECCCipher *buf = pucEncData;
1120
1121 if (!sdf_method || !sdf_method->InternalDecrypt_ECC) {
1122 SDFerr(SDF_R_NOT_INITIALIZED);
1123 return SDR_NOTSUPPORT;
1124 }
1125
1126 if (sdf_vendor && sdf_vendor->pkey_std2vendor) {
1127 if (!(uiAlgID = sdf_vendor->pkey_std2vendor(uiAlgID))) {
1128 SDFerr(SDF_R_NOT_SUPPORTED_PKEY_ALGOR);
1129 return SDR_ALGNOTSUPPORT;
1130 }
1131 }
1132
1133 if (sdf_vendor && sdf_vendor->encode_ecccipher) {
1134 if (SDF_NewECCCipher(&buf, pucEncData->L) != SDR_OK) {
1135 SDFerr(ERR_R_SDF_LIB);
1136 return SDR_UNKNOWERR;
1137 }
1138
1139 if (!sdf_vendor->encode_ecccipher(pucEncData, buf)) {
1140 SDFerr(ERR_R_SDF_LIB);
1141 ret = SDR_UNKNOWERR;
1142 goto end;
1143 }
1144 }
1145
1146 if ((ret = sdf_method->InternalDecrypt_ECC(
1147 hSessionHandle,
1148 uiISKIndex,
1149 uiAlgID,
1150 buf,
1151 pucData,
1152 uiDataLength)) != SDR_OK) {
1153 SDFerr(SDF_GetErrorReason(ret));
1154 goto end;
1155 }
1156
1157 end:
1158 if (sdf_vendor && sdf_vendor->encode_ecccipher && buf) {
1159 SDF_FreeECCCipher(buf);
1160 }
1161 return ret;
1162 }
1163
SDF_Encrypt(void * hSessionHandle,void * hKeyHandle,unsigned int uiAlgID,unsigned char * pucIV,unsigned char * pucData,unsigned int uiDataLength,unsigned char * pucEncData,unsigned int * puiEncDataLength)1164 int SDF_Encrypt(
1165 void *hSessionHandle,
1166 void *hKeyHandle,
1167 unsigned int uiAlgID,
1168 unsigned char *pucIV,
1169 unsigned char *pucData,
1170 unsigned int uiDataLength,
1171 unsigned char *pucEncData,
1172 unsigned int *puiEncDataLength)
1173 {
1174 int ret = SDR_UNKNOWERR;
1175
1176 if (!sdf_method || !sdf_method->Encrypt) {
1177 SDFerr(SDF_R_NOT_INITIALIZED);
1178 return SDR_NOTSUPPORT;
1179 }
1180
1181 if (sdf_vendor) {
1182 if (!(uiAlgID = sdf_vendor->cipher_std2vendor(uiAlgID))) {
1183 SDFerr(SDF_R_NOT_SUPPORTED_CIPHER_ALGOR);
1184 return SDR_ALGNOTSUPPORT;
1185 }
1186 }
1187
1188 if ((ret = sdf_method->Encrypt(
1189 hSessionHandle,
1190 hKeyHandle,
1191 uiAlgID,
1192 pucIV,
1193 pucData,
1194 uiDataLength,
1195 pucEncData,
1196 puiEncDataLength)) != SDR_OK) {
1197 SDFerr(SDF_GetErrorReason(ret));
1198 return ret;
1199 }
1200
1201 return SDR_OK;
1202 }
1203
SDF_Decrypt(void * hSessionHandle,void * hKeyHandle,unsigned int uiAlgID,unsigned char * pucIV,unsigned char * pucEncData,unsigned int uiEncDataLength,unsigned char * pucData,unsigned int * puiDataLength)1204 int SDF_Decrypt(
1205 void *hSessionHandle,
1206 void *hKeyHandle,
1207 unsigned int uiAlgID,
1208 unsigned char *pucIV,
1209 unsigned char *pucEncData,
1210 unsigned int uiEncDataLength,
1211 unsigned char *pucData,
1212 unsigned int *puiDataLength)
1213 {
1214 int ret = SDR_UNKNOWERR;
1215
1216 if (!sdf_method || !sdf_method->Decrypt) {
1217 SDFerr(SDF_R_NOT_INITIALIZED);
1218 return SDR_NOTSUPPORT;
1219 }
1220
1221 if (sdf_vendor) {
1222 if (!(uiAlgID = sdf_vendor->cipher_std2vendor(uiAlgID))) {
1223 SDFerr(SDF_R_NOT_SUPPORTED_CIPHER_ALGOR);
1224 return SDR_ALGNOTSUPPORT;
1225 }
1226 }
1227
1228 if ((ret = sdf_method->Decrypt(
1229 hSessionHandle,
1230 hKeyHandle,
1231 uiAlgID,
1232 pucIV,
1233 pucEncData,
1234 uiEncDataLength,
1235 pucData,
1236 puiDataLength)) != SDR_OK) {
1237 SDFerr(SDF_GetErrorReason(ret));
1238 return ret;
1239 }
1240
1241 return SDR_OK;
1242 }
1243
SDF_CalculateMAC(void * hSessionHandle,void * hKeyHandle,unsigned int uiAlgID,unsigned char * pucIV,unsigned char * pucData,unsigned int uiDataLength,unsigned char * pucMAC,unsigned int * puiMACLength)1244 int SDF_CalculateMAC(
1245 void *hSessionHandle,
1246 void *hKeyHandle,
1247 unsigned int uiAlgID,
1248 unsigned char *pucIV,
1249 unsigned char *pucData,
1250 unsigned int uiDataLength,
1251 unsigned char *pucMAC,
1252 unsigned int *puiMACLength)
1253 {
1254 int ret = SDR_UNKNOWERR;
1255
1256 if (!sdf_method || !sdf_method->CalculateMAC) {
1257 SDFerr(SDF_R_NOT_INITIALIZED);
1258 return SDR_NOTSUPPORT;
1259 }
1260
1261 if (sdf_vendor) {
1262 if (!(uiAlgID = sdf_vendor->cipher_std2vendor(uiAlgID))) {
1263 SDFerr(SDF_R_NOT_SUPPORTED_CIPHER_ALGOR);
1264 return SDR_ALGNOTSUPPORT;
1265 }
1266 }
1267
1268 if ((ret = sdf_method->CalculateMAC(
1269 hSessionHandle,
1270 hKeyHandle,
1271 uiAlgID,
1272 pucIV,
1273 pucData,
1274 uiDataLength,
1275 pucMAC,
1276 puiMACLength)) != SDR_OK) {
1277 SDFerr(SDF_GetErrorReason(ret));
1278 return ret;
1279 }
1280
1281 return SDR_OK;
1282 }
1283
SDF_HashInit(void * hSessionHandle,unsigned int uiAlgID,ECCrefPublicKey * pucPublicKey,unsigned char * pucID,unsigned int uiIDLength)1284 int SDF_HashInit(
1285 void *hSessionHandle,
1286 unsigned int uiAlgID,
1287 ECCrefPublicKey *pucPublicKey,
1288 unsigned char *pucID,
1289 unsigned int uiIDLength)
1290 {
1291 int ret = SDR_UNKNOWERR;
1292
1293 if (!sdf_method || !sdf_method->HashInit) {
1294 SDFerr(SDF_R_NOT_INITIALIZED);
1295 return SDR_NOTSUPPORT;
1296 }
1297
1298 if (sdf_vendor) {
1299 if (!(uiAlgID = sdf_vendor->digest_std2vendor(uiAlgID))) {
1300 SDFerr(SDF_R_NOT_SUPPORTED_DIGEST_ALGOR);
1301 return SDR_ALGNOTSUPPORT;
1302 }
1303 }
1304
1305 if ((ret = sdf_method->HashInit(
1306 hSessionHandle,
1307 uiAlgID,
1308 pucPublicKey,
1309 pucID,
1310 uiIDLength)) != SDR_OK) {
1311 SDFerr(SDF_GetErrorReason(ret));
1312 return ret;
1313 }
1314
1315 return SDR_OK;
1316 }
1317
SDF_HashUpdate(void * hSessionHandle,unsigned char * pucData,unsigned int uiDataLength)1318 int SDF_HashUpdate(
1319 void *hSessionHandle,
1320 unsigned char *pucData,
1321 unsigned int uiDataLength)
1322 {
1323 int ret = SDR_UNKNOWERR;
1324
1325 if (!sdf_method || !sdf_method->HashUpdate) {
1326 SDFerr(SDF_R_NOT_INITIALIZED);
1327 return SDR_NOTSUPPORT;
1328 }
1329
1330 if ((ret = sdf_method->HashUpdate(
1331 hSessionHandle,
1332 pucData,
1333 uiDataLength)) != SDR_OK) {
1334 SDFerr(SDF_GetErrorReason(ret));
1335 return ret;
1336 }
1337
1338 return SDR_OK;
1339 }
1340
SDF_HashFinal(void * hSessionHandle,unsigned char * pucHash,unsigned int * puiHashLength)1341 int SDF_HashFinal(
1342 void *hSessionHandle,
1343 unsigned char *pucHash,
1344 unsigned int *puiHashLength)
1345 {
1346 int ret = SDR_UNKNOWERR;
1347
1348 if (!sdf_method || !sdf_method->HashFinal) {
1349 SDFerr(SDF_R_NOT_INITIALIZED);
1350 return SDR_NOTSUPPORT;
1351 }
1352
1353 if ((ret = sdf_method->HashFinal(
1354 hSessionHandle,
1355 pucHash,
1356 puiHashLength)) != SDR_OK) {
1357 SDFerr(SDF_GetErrorReason(ret));
1358 return ret;
1359 }
1360
1361 return SDR_OK;
1362 }
1363
SDF_CreateFile(void * hSessionHandle,unsigned char * pucFileName,unsigned int uiNameLen,unsigned int uiFileSize)1364 int SDF_CreateFile(
1365 void *hSessionHandle,
1366 unsigned char *pucFileName,
1367 unsigned int uiNameLen,
1368 unsigned int uiFileSize)
1369 {
1370 int ret = SDR_UNKNOWERR;
1371
1372 if (!sdf_method || !sdf_method->CreateObject) {
1373 SDFerr(SDF_R_NOT_INITIALIZED);
1374 return SDR_NOTSUPPORT;
1375 }
1376
1377 if ((ret = sdf_method->CreateObject(
1378 hSessionHandle,
1379 pucFileName,
1380 uiNameLen,
1381 uiFileSize)) != SDR_OK) {
1382 SDFerr(SDF_GetErrorReason(ret));
1383 return ret;
1384 }
1385
1386 return SDR_OK;
1387 }
1388
SDF_ReadFile(void * hSessionHandle,unsigned char * pucFileName,unsigned int uiNameLen,unsigned int uiOffset,unsigned int * puiReadLength,unsigned char * pucBuffer)1389 int SDF_ReadFile(
1390 void *hSessionHandle,
1391 unsigned char *pucFileName,
1392 unsigned int uiNameLen,
1393 unsigned int uiOffset,
1394 unsigned int *puiReadLength,
1395 unsigned char *pucBuffer)
1396 {
1397 int ret = SDR_UNKNOWERR;
1398
1399 if (!sdf_method || !sdf_method->ReadObject) {
1400 SDFerr(SDF_R_NOT_INITIALIZED);
1401 return SDR_NOTSUPPORT;
1402 }
1403
1404 if ((ret = sdf_method->ReadObject(
1405 hSessionHandle,
1406 pucFileName,
1407 uiNameLen,
1408 uiOffset,
1409 puiReadLength,
1410 pucBuffer)) != SDR_OK) {
1411 SDFerr(SDF_GetErrorReason(ret));
1412 return ret;
1413 }
1414
1415 return SDR_OK;
1416 }
1417
SDF_WriteFile(void * hSessionHandle,unsigned char * pucFileName,unsigned int uiNameLen,unsigned int uiOffset,unsigned int uiWriteLength,unsigned char * pucBuffer)1418 int SDF_WriteFile(
1419 void *hSessionHandle,
1420 unsigned char *pucFileName,
1421 unsigned int uiNameLen,
1422 unsigned int uiOffset,
1423 unsigned int uiWriteLength,
1424 unsigned char *pucBuffer)
1425 {
1426 int ret = SDR_UNKNOWERR;
1427
1428 if (!sdf_method || !sdf_method->WriteObject) {
1429 SDFerr(SDF_R_NOT_INITIALIZED);
1430 return SDR_NOTSUPPORT;
1431 }
1432
1433 if ((ret = sdf_method->WriteObject(
1434 hSessionHandle,
1435 pucFileName,
1436 uiNameLen,
1437 uiOffset,
1438 uiWriteLength,
1439 pucBuffer)) != SDR_OK) {
1440 SDFerr(SDF_GetErrorReason(ret));
1441 return ret;
1442 }
1443
1444 return SDR_OK;
1445 }
1446
SDF_DeleteFile(void * hSessionHandle,unsigned char * pucFileName,unsigned int uiNameLen)1447 int SDF_DeleteFile(
1448 void *hSessionHandle,
1449 unsigned char *pucFileName,
1450 unsigned int uiNameLen)
1451 {
1452 int ret = SDR_UNKNOWERR;
1453
1454 if (!sdf_method || !sdf_method->DeleteObject) {
1455 SDFerr(SDF_R_NOT_INITIALIZED);
1456 return SDR_NOTSUPPORT;
1457 }
1458
1459 if ((ret = sdf_method->DeleteObject(
1460 hSessionHandle,
1461 pucFileName,
1462 uiNameLen)) != SDR_OK) {
1463 SDFerr(SDF_GetErrorReason(ret));
1464 return ret;
1465 }
1466
1467 return SDR_OK;
1468 }
1469