diff --git a/crypto/saf/saf_cert.c b/crypto/saf/saf_cert.c index 99d6fafb..8f8dda91 100644 --- a/crypto/saf/saf_cert.c +++ b/crypto/saf/saf_cert.c @@ -53,6 +53,7 @@ #include #include #include +#include "saf_lcl.h" /* 7.2.2 */ diff --git a/crypto/saf/saf_ec.c b/crypto/saf/saf_ec.c index eeed3140..7850da48 100644 --- a/crypto/saf/saf_ec.c +++ b/crypto/saf/saf_ec.c @@ -50,6 +50,7 @@ #include #include #include +#include "saf_lcl.h" /* 7.3.23 */ @@ -87,8 +88,6 @@ int SAF_GenEccKeyPair( return SAR_KeyUsageErr; } - - /* set return value */ ret = SAR_Ok; @@ -110,7 +109,7 @@ int SAF_GetEccPublicKey( int rv; /* check arguments */ - if (!hAppHandle || !pucContainerNamae || !pucPUblicKey || + if (!hAppHandle || !pucContainerName || !pucPublicKey || !puiPublicKeyLen) { SAFerr(SAF_F_SAF_GETECCPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER); @@ -161,8 +160,8 @@ int SAF_EccSign( unsigned int uiISKIndex; /* check arguments */ - if (!hAppHandle || !pucContainerNamae || !pucPUblicKey || - !pucSignData || !pucSignDataLen) { + if (!hAppHandle || !pucContainerName || !pucInData || + !pucSignData || !puiSignDataLen) { SAFerr(SAF_F_SAF_ECCSIGN, ERR_R_PASSED_NULL_PARAMETER); return SAR_IndataErr; @@ -208,7 +207,7 @@ int SAF_EccVerifySign( SAFerr(SAF_F_SAF_ECCVERIFYSIGN, ERR_R_PASSED_NULL_PARAMETER); return SAR_IndataErr; } - if (uiPublicKeyLen != sizeof(ECCrefPublic)) { + if (uiPublicKeyLen != sizeof(ECCrefPublicKey)) { SAFerr(SAF_F_SAF_ECCVERIFYSIGN, SAF_R_INVALID_INPUT_LENGTH); return SAR_IndataLenErr; } @@ -241,13 +240,15 @@ int SAF_EccPublicKeyEnc( unsigned char *pucOutData, unsigned int *puiOutDataLen) { + int ret = -1; + /* check arguments */ if (!pucPublicKey || !pucInData || !pucOutData || !puiOutDataLen) { SAFerr(SAF_F_SAF_ECCPUBLICKEYENC, ERR_R_PASSED_NULL_PARAMETER); return SAR_IndataErr; } - if (uiPublicKeyLen != sizeof(ECCrefPublic)) { + if (uiPublicKeyLen != sizeof(ECCrefPublicKey)) { SAFerr(SAF_F_SAF_ECCPUBLICKEYENC, SAF_R_INVALID_INPUT_LENGTH); return SAR_IndataLenErr; @@ -293,7 +294,7 @@ int SAF_EccPublicKeyEncByCert( ERR_R_PASSED_NULL_PARAMETER); return SAR_IndataErr; } - if (uiCertificateLen <= 0 || uiCertificate > INT_MAX) { + if (uiCertificateLen <= 0 || uiCertificateLen > INT_MAX) { SAFerr(SAF_F_SAF_ECCPUBLICKEYENCBYCERT, SAF_R_INVALID_INPUT_LENGTH); return SAR_IndataLenErr; @@ -340,7 +341,7 @@ int SAF_EccVerifySignByCert( ERR_R_PASSED_NULL_PARAMETER); return SAR_IndataErr; } - if (uiCertificateLen <= 0 || uiCertificate > INT_MAX) { + if (uiCertificateLen <= 0 || uiCertificateLen > INT_MAX) { SAFerr(SAF_F_SAF_ECCVERIFYSIGNBYCERT, SAF_R_INVALID_INPUT_LENGTH); return SAR_IndataLenErr; @@ -372,7 +373,8 @@ end: /* 7.3.33 */ int SAF_GenerateAgreementDataWithECC( void *hSymmKeyObj, - unsigned int uiISKIndex, + unsigned char *pucContainerName, + unsigned int uiContainerNameLen, unsigned int uiKeyBits, unsigned char *pucSponsorID, unsigned int uiSponsorIDLength, @@ -409,7 +411,8 @@ int SAF_GenerateKeyWithECC( /* 7.3.35 */ int SAF_GenerateAgreementDataAdnKeyWithECC( void *hSymmKeyObj, - unsigned int uiISKIndex, + unsigned char *pucContainerName, + unsigned int uiContainerNameLen, unsigned int uiKeyBits, unsigned char *pucResponseID, unsigned int uiResponseIDLength, @@ -430,7 +433,8 @@ int SAF_GenerateAgreementDataAdnKeyWithECC( if ((ret = SAF_GenerateAgreementDataWithECC( hSymmKeyObj, - uiISKIndex, + pucContainerName, + uiContainerNameLen, uiKeyBits, pucSponsorID, uiSponsorIDLength, diff --git a/crypto/saf/saf_enc.c b/crypto/saf/saf_enc.c index bd9906a5..3e4e0887 100644 --- a/crypto/saf/saf_enc.c +++ b/crypto/saf/saf_enc.c @@ -51,6 +51,7 @@ #include #include #include +#include "saf_lcl.h" /* 7.3.39 */ int SAF_SymmEncryptUpdate( @@ -61,7 +62,7 @@ int SAF_SymmEncryptUpdate( unsigned int *puiOutDataLen) { int ret = SAR_UnknownErr; - SAF_KEY_HANDLE *hkey = (SAF_KEY_HANDLE *)hKeyHandle; + SAF_KEY *hkey = (SAF_KEY *)hKeyHandle; unsigned char *out = pucOutData; int inlen, outlen; @@ -142,7 +143,7 @@ int SAF_SymmDecryptUpdate( unsigned int *puiOutDataLen) { int ret = SAR_UnknownErr; - SAF_KEY_HANDLE *hkey = (SAF_KEY_HANDLE *)hKeyHandle; + SAF_KEY *hkey = (SAF_KEY *)hKeyHandle; unsigned char *in = pucInData; int inlen, outlen; @@ -204,8 +205,6 @@ end: /* 7.3.43 */ int SAF_SymmDecryptFinal( void *hKeyHandle, - const unsigned char *pucInData, - unsigned int uiInDataLen, unsigned char *pucOutData, unsigned int *puiOutDataLen) { diff --git a/crypto/saf/saf_errstr.c b/crypto/saf/saf_errstr.c index e86642b4..d72d69de 100644 --- a/crypto/saf/saf_errstr.c +++ b/crypto/saf/saf_errstr.c @@ -92,7 +92,7 @@ static ERR_STRING_DATA saf_errstr[] = { { SAR_NotLogin, "Not login" }, }; -char *SAF_GetErrorString(int err) +const char *SAF_GetErrorString(int err) { int i; for (i = 0; i < OSSL_NELEM(saf_errstr); i++) { diff --git a/crypto/saf/saf_hash.c b/crypto/saf/saf_hash.c index 029ee0f6..3c0bf188 100644 --- a/crypto/saf/saf_hash.c +++ b/crypto/saf/saf_hash.c @@ -52,6 +52,7 @@ #include #include #include +#include "saf_lcl.h" /* 7.3.12 */ int SAF_CreateHashObj(void **phHashObj, @@ -61,11 +62,11 @@ int SAF_CreateHashObj(void **phHashObj, unsigned char *pucID, unsigned int ulIDLen) { - int ret = SAR_UnkownErr; + int ret = SAR_UnknownErr; const EVP_MD *md; EVP_MD_CTX *ctx = NULL; - if (!(md = EVP_get_digestbysgd(uiAlgorithmType))) { + if (!(md = EVP_get_digestbysgd(uiAlgoType))) { return SAR_AlgoTypeErr; } @@ -80,7 +81,7 @@ int SAF_CreateHashObj(void **phHashObj, *phHashObj = ctx; end: - if (ret != SAR_OK) { + if (ret != SAR_Ok) { EVP_MD_CTX_free(ctx); *phHashObj = NULL; } @@ -92,7 +93,7 @@ int SAF_DestroyHashObj( void *phHashObj) { EVP_MD_CTX_free((EVP_MD_CTX *)phHashObj); - return SAR_OK; + return SAR_Ok; } /* 7.3.14 */ @@ -101,10 +102,10 @@ int SAF_HashUpdate( const unsigned char *pucInData, unsigned int uiInDataLen) { - if (!EVP_DigestUpdate((EVP_MD_CTX *)phHashObj, pucInData, (size_t)uiInDataLne)) { + if (!EVP_DigestUpdate((EVP_MD_CTX *)phHashObj, pucInData, (size_t)uiInDataLen)) { return SAR_HashErr; } - return SAR_OK; + return SAR_Ok; } /* 7.3.15 */ @@ -115,7 +116,7 @@ int SAF_HashFinal(void *phHashObj, if (!EVP_DigestFinal((EVP_MD_CTX *)phHashObj, pucOutData, uiOutDataLen)) { return SAR_HashErr; } - return SAR_OK; + return SAR_Ok; } /* 7.3.11 */ @@ -142,6 +143,6 @@ int SAF_Hash( return SAR_HashErr; } - return SAR_OK; + return SAR_Ok; } diff --git a/crypto/saf/saf_lcl.h b/crypto/saf/saf_lcl.h index 1d0f7f8e..06fedd78 100644 --- a/crypto/saf/saf_lcl.h +++ b/crypto/saf/saf_lcl.h @@ -51,9 +51,9 @@ #include #include #include +#include - -typedef struct { +typedef struct saf_app_st { const char *config_path; ENGINE *engine; } SAF_APP; @@ -75,6 +75,8 @@ typedef struct { typedef struct { SAF_SYMMKEYOBJ obj; unsigned char key[64]; + int keylen; + const EVP_CIPHER *cipher; EVP_CIPHER_CTX *cipher_ctx; CMAC_CTX *cmac_ctx; } SAF_KEY; diff --git a/crypto/saf/saf_lib.c b/crypto/saf/saf_lib.c index 0ca1dd18..a87e65a7 100644 --- a/crypto/saf/saf_lib.c +++ b/crypto/saf/saf_lib.c @@ -112,16 +112,7 @@ int saf_get_ec_public_key_from_cert( static int readfile(const char *file, unsigned char **pout, size_t *len) { - FILE *fp = fopen(file, "rb"); - fseek(fp, 0, SEEK_END); - long fsize = ftell(fp); - fseek(fp, 0, SEEK_SET); - char *out = malloc(fsize); - fread(out, fsize, 1, f); - fclose(f); - *pout = out; - *len = fsize; - return SAR_OK; + return SAR_Ok; } static int cert_get_pubkey( diff --git a/crypto/saf/saf_mac.c b/crypto/saf/saf_mac.c index bd506e18..225c8c89 100644 --- a/crypto/saf/saf_mac.c +++ b/crypto/saf/saf_mac.c @@ -51,6 +51,7 @@ #include #include #include +#include "saf_lcl.h" /* 7.3.45 */ int SAF_MacUpdate( @@ -117,7 +118,7 @@ int SAF_MacFinal( } siz = EVP_CIPHER_block_size(hkey->cipher); - if (!CBCMAC_Final(hkey->cbcmac_ctx, pucOutData, &siz)) { + if (!CBCMAC_Final(hkey->cmac_ctx, pucOutData, &siz)) { SAFerr(SAF_F_SAF_MACFINAL, SAF_R_MAC_FAILURE); return SAR_UnknownErr; } diff --git a/crypto/saf/saf_rand.c b/crypto/saf/saf_rand.c index 92d1ac55..010b8de5 100644 --- a/crypto/saf/saf_rand.c +++ b/crypto/saf/saf_rand.c @@ -70,7 +70,7 @@ int SAF_GenRandom( return SAR_IndataErr; } - if (!RAND_bytes(pucRand, len)) { + if (!RAND_bytes(pucRand, uiRandLen)) { SAFerr(SAF_F_SAF_GENRANDOM, SAF_R_GEN_RANDOM_FAILURE); return SAR_GenRandErr; } diff --git a/crypto/sdf/sdf_err.c b/crypto/sdf/sdf_err.c new file mode 100644 index 00000000..df2d8140 --- /dev/null +++ b/crypto/sdf/sdf_err.c @@ -0,0 +1,118 @@ +/* + * Generated by util/mkerr.pl DO NOT EDIT + * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the OpenSSL license (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include +#include +#include + +/* BEGIN ERROR CODES */ +#ifndef OPENSSL_NO_ERR + +# define ERR_FUNC(func) ERR_PACK(ERR_LIB_SDF,func,0) +# define ERR_REASON(reason) ERR_PACK(ERR_LIB_SDF,0,reason) + +static ERR_STRING_DATA SDF_str_functs[] = { + {ERR_FUNC(SDF_F_SDF_CALCULATEMAC), "SDF_CalculateMAC"}, + {ERR_FUNC(SDF_F_SDF_CLOSEDEVICE), "SDF_CloseDevice"}, + {ERR_FUNC(SDF_F_SDF_CLOSESESSION), "SDF_CloseSession"}, + {ERR_FUNC(SDF_F_SDF_CREATEFILE), "SDF_CreateFile"}, + {ERR_FUNC(SDF_F_SDF_DECRYPT), "SDF_Decrypt"}, + {ERR_FUNC(SDF_F_SDF_DELETEFILE), "SDF_DeleteFile"}, + {ERR_FUNC(SDF_F_SDF_DESTROYKEY), "SDF_DestroyKey"}, + {ERR_FUNC(SDF_F_SDF_ENCRYPT), "SDF_Encrypt"}, + {ERR_FUNC(SDF_F_SDF_EXCHANGEDIGITENVELOPEBASEONECC), + "SDF_ExchangeDigitEnvelopeBaseOnECC"}, + {ERR_FUNC(SDF_F_SDF_EXCHANGEDIGITENVELOPEBASEONRSA), + "SDF_ExchangeDigitEnvelopeBaseOnRSA"}, + {ERR_FUNC(SDF_F_SDF_EXPORTENCPUBLICKEY_ECC), + "SDF_ExportEncPublicKey_ECC"}, + {ERR_FUNC(SDF_F_SDF_EXPORTENCPUBLICKEY_RSA), + "SDF_ExportEncPublicKey_RSA"}, + {ERR_FUNC(SDF_F_SDF_EXPORTSIGNPUBLICKEY_ECC), + "SDF_ExportSignPublicKey_ECC"}, + {ERR_FUNC(SDF_F_SDF_EXPORTSIGNPUBLICKEY_RSA), + "SDF_ExportSignPublicKey_RSA"}, + {ERR_FUNC(SDF_F_SDF_EXTERNALENCRYPT_ECC), "SDF_ExternalEncrypt_ECC"}, + {ERR_FUNC(SDF_F_SDF_EXTERNALPRIVATEKEYOPERATION_RSA), + "SDF_EXTERNALPRIVATEKEYOPERATION_RSA"}, + {ERR_FUNC(SDF_F_SDF_EXTERNALPUBLICKEYOPERATION_RSA), + "SDF_ExternalPublicKeyOperation_RSA"}, + {ERR_FUNC(SDF_F_SDF_EXTERNALVERIFY_ECC), "SDF_ExternalVerify_ECC"}, + {ERR_FUNC(SDF_F_SDF_GENERATEAGREEMENTDATAANDKEYWITHECC), + "SDF_GenerateAgreementDataAndKeyWithECC"}, + {ERR_FUNC(SDF_F_SDF_GENERATEAGREEMENTDATAWITHECC), + "SDF_GenerateAgreementDataWithECC"}, + {ERR_FUNC(SDF_F_SDF_GENERATEKEYPAIR_ECC), "SDF_GenerateKeyPair_ECC"}, + {ERR_FUNC(SDF_F_SDF_GENERATEKEYPAIR_RSA), "SDF_GenerateKeyPair_RSA"}, + {ERR_FUNC(SDF_F_SDF_GENERATEKEYWITHECC), "SDF_GenerateKeyWithECC"}, + {ERR_FUNC(SDF_F_SDF_GENERATEKEYWITHEPK_ECC), + "SDF_GenerateKeyWithEPK_ECC"}, + {ERR_FUNC(SDF_F_SDF_GENERATEKEYWITHEPK_RSA), + "SDF_GenerateKeyWithEPK_RSA"}, + {ERR_FUNC(SDF_F_SDF_GENERATEKEYWITHIPK_ECC), + "SDF_GenerateKeyWithIPK_ECC"}, + {ERR_FUNC(SDF_F_SDF_GENERATEKEYWITHIPK_RSA), + "SDF_GenerateKeyWithIPK_RSA"}, + {ERR_FUNC(SDF_F_SDF_GENERATEKEYWITHKEK), "SDF_GenerateKeyWithKEK"}, + {ERR_FUNC(SDF_F_SDF_GENERATERANDOM), "SDF_GenerateRandom"}, + {ERR_FUNC(SDF_F_SDF_GETDEVICEINFO), "SDF_GetDeviceInfo"}, + {ERR_FUNC(SDF_F_SDF_GETPRIVATEKEYACCESSRIGHT), + "SDF_GetPrivateKeyAccessRight"}, + {ERR_FUNC(SDF_F_SDF_HASHFINAL), "SDF_HashFinal"}, + {ERR_FUNC(SDF_F_SDF_HASHINIT), "SDF_HashInit"}, + {ERR_FUNC(SDF_F_SDF_HASHUPDATE), "SDF_HashUpdate"}, + {ERR_FUNC(SDF_F_SDF_IMPORTKEY), "SDF_ImportKey"}, + {ERR_FUNC(SDF_F_SDF_IMPORTKEYWITHISK_ECC), "SDF_ImportKeyWithISK_ECC"}, + {ERR_FUNC(SDF_F_SDF_IMPORTKEYWITHISK_RSA), "SDF_ImportKeyWithISK_RSA"}, + {ERR_FUNC(SDF_F_SDF_IMPORTKEYWITHKEK), "SDF_ImportKeyWithKEK"}, + {ERR_FUNC(SDF_F_SDF_INTERNALPRIVATEKEYOPERATION_RSA), + "SDF_InternalPrivateKeyOperation_RSA"}, + {ERR_FUNC(SDF_F_SDF_INTERNALPUBLICKEYOPERATION_RSA), + "SDF_InternalPublicKeyOperation_RSA"}, + {ERR_FUNC(SDF_F_SDF_INTERNALSIGN_ECC), "SDF_InternalSign_ECC"}, + {ERR_FUNC(SDF_F_SDF_INTERNALVERIFY_ECC), "SDF_InternalVerify_ECC"}, + {ERR_FUNC(SDF_F_SDF_METHOD_LOAD_LIBRARY), "SDF_METHOD_LOAD_LIBRARY"}, + {ERR_FUNC(SDF_F_SDF_OPENDEVICE), "SDF_OpenDevice"}, + {ERR_FUNC(SDF_F_SDF_OPENSESSION), "SDF_OpenSession"}, + {ERR_FUNC(SDF_F_SDF_READFILE), "SDF_ReadFile"}, + {ERR_FUNC(SDF_F_SDF_RELEASEPRIVATEKEYACCESSRIGHT), + "SDF_ReleasePrivateKeyAccessRight"}, + {ERR_FUNC(SDF_F_SDF_WRITEFILE), "SDF_WriteFile"}, + {0, NULL} +}; + +static ERR_STRING_DATA SDF_str_reasons[] = { + {ERR_REASON(SDF_R_INVALID_KEY_LENGTH), "invalid key length"}, + {ERR_REASON(SDF_R_INVALID_SDF_LIBRARY), "invalid sdf library"}, + {ERR_REASON(SDF_R_INVALID_SESSION_HANDLE), "invalid session handle"}, + {ERR_REASON(SDF_R_LOAD_LIBRARY_FAILURE), "load library failure"}, + {ERR_REASON(SDF_R_METHOD_OPERATION_FAILURE), "method operation failure"}, + {ERR_REASON(SDF_R_NOT_INITIALIZED), "not initialized"}, + {ERR_REASON(SDF_R_NOT_SUPPORTED), "not supported"}, + {ERR_REASON(SDF_R_OPERATION_FAILED), "operation failed"}, + {ERR_REASON(SDF_R_SDF_METHOD_RETURN_FAILURE), + "sdf method return failure"}, + {ERR_REASON(SDF_R_SDF_OPERATION_FAILED), "sdf operation failed"}, + {0, NULL} +}; + +#endif + +int ERR_load_SDF_strings(void) +{ +#ifndef OPENSSL_NO_ERR + + if (ERR_func_error_string(SDF_str_functs[0].error) == NULL) { + ERR_load_strings(0, SDF_str_functs); + ERR_load_strings(0, SDF_str_reasons); + } +#endif + return 1; +} diff --git a/crypto/sdf/sdf_lib.c b/crypto/sdf/sdf_lib.c new file mode 100644 index 00000000..99d5d22d --- /dev/null +++ b/crypto/sdf/sdf_lib.c @@ -0,0 +1,1332 @@ +/* ==================================================================== + * Copyright (c) 2016 - 2017 The GmSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the GmSSL Project. + * (http://gmssl.org/)" + * + * 4. The name "GmSSL Project" must not be used to endorse or promote + * products derived from this software without prior written + * permission. For written permission, please contact + * guanzhi1980@gmail.com. + * + * 5. Products derived from this software may not be called "GmSSL" + * nor may "GmSSL" appear in their names without prior written + * permission of the GmSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the GmSSL Project + * (http://gmssl.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + */ + +#include +#include +#include "internal/sdf_meth.h" + +static SDF_METHOD *sdf_method = NULL; + +int SDF_OpenDevice( + void **phDeviceHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->OpenDevice) { + SDFerr(SDF_F_SDF_OPENDEVICE, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->OpenDevice( + phDeviceHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_OPENDEVICE, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + +int SDF_CloseDevice( + void *hDeviceHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->CloseDevice) { + SDFerr(SDF_F_SDF_CLOSEDEVICE, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->CloseDevice( + hDeviceHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_CLOSEDEVICE, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_OpenSession( + void *hDeviceHandle, + void **phSessionHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->OpenSession) { + SDFerr(SDF_F_SDF_OPENSESSION, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->OpenSession( + hDeviceHandle, + phSessionHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_OPENSESSION, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_CloseSession( + void *hSessionHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->CloseSession) { + SDFerr(SDF_F_SDF_CLOSESESSION, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->CloseSession( + hSessionHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_CLOSESESSION, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_GetDeviceInfo( + void *hSessionHandle, + DEVICEINFO *pstDeviceInfo) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GetDeviceInfo) { + SDFerr(SDF_F_SDF_GETDEVICEINFO, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GetDeviceInfo( + hSessionHandle, + pstDeviceInfo)) != SDR_OK) { + SDFerr(SDF_F_SDF_GETDEVICEINFO, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_GenerateRandom( + void *hSessionHandle, + unsigned int uiLength, + unsigned char *pucRandom) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GenerateRandom) { + SDFerr(SDF_F_SDF_GENERATERANDOM, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GenerateRandom( + hSessionHandle, + uiLength, + pucRandom)) != SDR_OK) { + SDFerr(SDF_F_SDF_GENERATERANDOM, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_GetPrivateKeyAccessRight( + void *hSessionHandle, + unsigned int uiKeyIndex, + unsigned char *pucPassword, + unsigned int uiPwdLength) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GetPrivateKeyAccessRight) { + SDFerr(SDF_F_SDF_GETPRIVATEKEYACCESSRIGHT, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GetPrivateKeyAccessRight( + hSessionHandle, + uiKeyIndex, + pucPassword, + uiPwdLength)) != SDR_OK) { + SDFerr(SDF_F_SDF_GETPRIVATEKEYACCESSRIGHT, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ReleasePrivateKeyAccessRight( + void *hSessionHandle, + unsigned int uiKeyIndex) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ReleasePrivateKeyAccessRight) { + SDFerr(SDF_F_SDF_RELEASEPRIVATEKEYACCESSRIGHT, + SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ReleasePrivateKeyAccessRight( + hSessionHandle, + uiKeyIndex)) != SDR_OK) { + SDFerr(SDF_F_SDF_RELEASEPRIVATEKEYACCESSRIGHT, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ExportSignPublicKey_RSA( + void *hSessionHandle, + unsigned int uiKeyIndex, + RSArefPublicKey *pucPublicKey) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ExportSignPublicKey_RSA) { + SDFerr(SDF_F_SDF_EXPORTSIGNPUBLICKEY_RSA, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ExportSignPublicKey_RSA( + hSessionHandle, + uiKeyIndex, + pucPublicKey)) != SDR_OK) { + SDFerr(SDF_F_SDF_EXPORTSIGNPUBLICKEY_RSA, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ExportEncPublicKey_RSA( + void *hSessionHandle, + unsigned int uiKeyIndex, + RSArefPublicKey *pucPublicKey) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ExportEncPublicKey_RSA) { + SDFerr(SDF_F_SDF_EXPORTENCPUBLICKEY_RSA, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ExportEncPublicKey_RSA( + hSessionHandle, + uiKeyIndex, + pucPublicKey)) != SDR_OK) { + SDFerr(SDF_F_SDF_EXPORTENCPUBLICKEY_RSA, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_GenerateKeyPair_RSA( + void *hSessionHandle, + unsigned int uiKeyBits, + RSArefPublicKey *pucPublicKey, + RSArefPrivateKey *pucPrivateKey) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GenerateKeyPair_RSA) { + SDFerr(SDF_F_SDF_GENERATEKEYPAIR_RSA, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GenerateKeyPair_RSA( + hSessionHandle, + uiKeyBits, + pucPublicKey, + pucPrivateKey)) != SDR_OK) { + SDFerr(SDF_F_SDF_GENERATEKEYPAIR_RSA, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_GenerateKeyWithIPK_RSA( + void *hSessionHandle, + unsigned int uiIPKIndex, + unsigned int uiKeyBits, + unsigned char *pucKey, + unsigned int *puiKeyLength, + void **phKeyHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GenerateKeyWithIPK_RSA) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHIPK_RSA, + SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GenerateKeyWithIPK_RSA( + hSessionHandle, + uiIPKIndex, + uiKeyBits, + pucKey, + puiKeyLength, + phKeyHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHIPK_RSA, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_GenerateKeyWithEPK_RSA( + void *hSessionHandle, + unsigned int uiKeyBits, + RSArefPublicKey *pucPublicKey, + unsigned char *pucKey, + unsigned int *puiKeyLength, + void **phKeyHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GenerateKeyWithEPK_RSA) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHEPK_RSA, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GenerateKeyWithEPK_RSA( + hSessionHandle, + uiKeyBits, + pucPublicKey, + pucKey, + puiKeyLength, + phKeyHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHEPK_RSA, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ImportKeyWithISK_RSA( + void *hSessionHandle, + unsigned int uiISKIndex, + unsigned char *pucKey, + unsigned int uiKeyLength, + void **phKeyHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ImportKeyWithISK_RSA) { + SDFerr(SDF_F_SDF_IMPORTKEYWITHISK_RSA, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ImportKeyWithISK_RSA( + hSessionHandle, + uiISKIndex, + pucKey, + uiKeyLength, + phKeyHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_IMPORTKEYWITHISK_RSA, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ExchangeDigitEnvelopeBaseOnRSA( + void *hSessionHandle, + unsigned int uiKeyIndex, + RSArefPublicKey *pucPublicKey, + unsigned char *pucDEInput, + unsigned int uiDELength, + unsigned char *pucDEOutput, + unsigned int *puiDELength) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ExchangeDigitEnvelopeBaseOnRSA) { + SDFerr(SDF_F_SDF_EXCHANGEDIGITENVELOPEBASEONRSA, + SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ExchangeDigitEnvelopeBaseOnRSA( + hSessionHandle, + uiKeyIndex, + pucPublicKey, + pucDEInput, + uiDELength, + pucDEOutput, + puiDELength)) != SDR_OK) { + SDFerr(SDF_F_SDF_EXCHANGEDIGITENVELOPEBASEONRSA, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ExportSignPublicKey_ECC( + void *hSessionHandle, + unsigned int uiKeyIndex, + ECCrefPublicKey *pucPublicKey) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ExportSignPublicKey_ECC) { + SDFerr(SDF_F_SDF_EXPORTSIGNPUBLICKEY_ECC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ExportSignPublicKey_ECC( + hSessionHandle, + uiKeyIndex, + pucPublicKey)) != SDR_OK) { + SDFerr(SDF_F_SDF_EXPORTSIGNPUBLICKEY_ECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ExportEncPublicKey_ECC( + void *hSessionHandle, + unsigned int uiKeyIndex, + ECCrefPublicKey *pucPublicKey) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ExportEncPublicKey_ECC) { + SDFerr(SDF_F_SDF_EXPORTENCPUBLICKEY_ECC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ExportEncPublicKey_ECC( + hSessionHandle, + uiKeyIndex, + pucPublicKey)) != SDR_OK) { + SDFerr(SDF_F_SDF_EXPORTENCPUBLICKEY_ECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_GenerateKeyPair_ECC( + void *hSessionHandle, + unsigned int uiAlgID, + unsigned int uiKeyBits, + ECCrefPublicKey *pucPublicKey, + ECCrefPrivateKey *pucPrivateKey) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GenerateKeyPair_ECC) { + SDFerr(SDF_F_SDF_GENERATEKEYPAIR_ECC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GenerateKeyPair_ECC( + hSessionHandle, + uiAlgID, + uiKeyBits, + pucPublicKey, + pucPrivateKey)) != SDR_OK) { + SDFerr(SDF_F_SDF_GENERATEKEYPAIR_ECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_GenerateKeyWithIPK_ECC( + void *hSessionHandle, + unsigned int uiIPKIndex, + unsigned int uiKeyBits, + ECCCipher *pucKey, + void **phKeyHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GenerateKeyWithIPK_ECC) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHIPK_ECC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GenerateKeyWithIPK_ECC( + hSessionHandle, + uiIPKIndex, + uiKeyBits, + pucKey, + phKeyHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHIPK_ECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_GenerateKeyWithEPK_ECC( + void *hSessionHandle, + unsigned int uiKeyBits, + unsigned int uiAlgID, + ECCrefPublicKey *pucPublicKey, + ECCCipher *pucKey, + void **phKeyHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GenerateKeyWithEPK_ECC) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHEPK_ECC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GenerateKeyWithEPK_ECC( + hSessionHandle, + uiKeyBits, + uiAlgID, + pucPublicKey, + pucKey, + phKeyHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHEPK_ECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ImportKeyWithISK_ECC( + void *hSessionHandle, + unsigned int uiISKIndex, + ECCCipher *pucKey, + void **phKeyHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ImportKeyWithISK_ECC) { + SDFerr(SDF_F_SDF_IMPORTKEYWITHISK_ECC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ImportKeyWithISK_ECC( + hSessionHandle, + uiISKIndex, + pucKey, + phKeyHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_IMPORTKEYWITHISK_ECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_GenerateAgreementDataWithECC( + void *hSessionHandle, + unsigned int uiISKIndex, + unsigned int uiKeyBits, + unsigned char *pucSponsorID, + unsigned int uiSponsorIDLength, + ECCrefPublicKey *pucSponsorPublicKey, + ECCrefPublicKey *pucSponsorTmpPublicKey, + void **phAgreementHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GenerateAgreementDataWithECC) { + SDFerr(SDF_F_SDF_GENERATEAGREEMENTDATAWITHECC, + SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GenerateAgreementDataWithECC( + hSessionHandle, + uiISKIndex, + uiKeyBits, + pucSponsorID, + uiSponsorIDLength, + pucSponsorPublicKey, + pucSponsorTmpPublicKey, + phAgreementHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_GENERATEAGREEMENTDATAWITHECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_GenerateKeyWithECC( + void *hSessionHandle, + unsigned char *pucResponseID, + unsigned int uiResponseIDLength, + ECCrefPublicKey *pucResponsePublicKey, + ECCrefPublicKey *pucResponseTmpPublicKey, + void *hAgreementHandle, + void **phKeyHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GenerateKeyWithECC) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHECC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GenerateKeyWithECC( + hSessionHandle, + pucResponseID, + uiResponseIDLength, + pucResponsePublicKey, + pucResponseTmpPublicKey, + hAgreementHandle, + phKeyHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int 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) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GenerateAgreementDataAndKeyWithECC) { + SDFerr(SDF_F_SDF_GENERATEAGREEMENTDATAANDKEYWITHECC, + SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GenerateAgreementDataAndKeyWithECC( + hSessionHandle, + uiISKIndex, + uiKeyBits, + pucResponseID, + uiResponseIDLength, + pucSponsorID, + uiSponsorIDLength, + pucSponsorPublicKey, + pucSponsorTmpPublicKey, + pucResponsePublicKey, + pucResponseTmpPublicKey, + phKeyHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_GENERATEAGREEMENTDATAANDKEYWITHECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ExchangeDigitEnvelopeBaseOnECC( + void *hSessionHandle, + unsigned int uiKeyIndex, + unsigned int uiAlgID, + ECCrefPublicKey *pucPublicKey, + ECCCipher *pucEncDataIn, + ECCCipher *pucEncDataOut) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ExchangeDigitEnvelopeBaseOnECC) { + SDFerr(SDF_F_SDF_EXCHANGEDIGITENVELOPEBASEONECC, + SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ExchangeDigitEnvelopeBaseOnECC( + hSessionHandle, + uiKeyIndex, + uiAlgID, + pucPublicKey, + pucEncDataIn, + pucEncDataOut)) != SDR_OK) { + SDFerr(SDF_F_SDF_EXCHANGEDIGITENVELOPEBASEONECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + +int SDF_GenerateKeyWithKEK( + void *hSessionHandle, + unsigned int uiKeyBits, + unsigned int uiAlgID, + unsigned int uiKEKIndex, + unsigned char *pucKey, + unsigned int *puiKeyLength, + void **phKeyHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->GenerateKeyWithKEK) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHKEK, + SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->GenerateKeyWithKEK( + hSessionHandle, + uiKeyBits, + uiAlgID, + uiKEKIndex, + pucKey, + puiKeyLength, + phKeyHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_GENERATEKEYWITHKEK, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ImportKeyWithKEK( + void *hSessionHandle, + unsigned int uiAlgID, + unsigned int uiKEKIndex, + unsigned char *pucKey, + unsigned int uiKeyLength, + void **phKeyHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ImportKeyWithKEK) { + SDFerr(SDF_F_SDF_IMPORTKEYWITHKEK, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ImportKeyWithKEK( + hSessionHandle, + uiAlgID, + uiKEKIndex, + pucKey, + uiKeyLength, + phKeyHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_IMPORTKEYWITHKEK, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_DestroyKey( + void *hSessionHandle, + void *hKeyHandle) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->DestroyKey) { + SDFerr(SDF_F_SDF_DESTROYKEY, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->DestroyKey( + hSessionHandle, + hKeyHandle)) != SDR_OK) { + SDFerr(SDF_F_SDF_DESTROYKEY, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ExternalPublicKeyOperation_RSA( + void *hSessionHandle, + RSArefPublicKey *pucPublicKey, + unsigned char *pucDataInput, + unsigned int uiInputLength, + unsigned char *pucDataOutput, + unsigned int *puiOutputLength) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ExternalPublicKeyOperation_RSA) { + SDFerr(SDF_F_SDF_EXTERNALPUBLICKEYOPERATION_RSA, + SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ExternalPublicKeyOperation_RSA( + hSessionHandle, + pucPublicKey, + pucDataInput, + uiInputLength, + pucDataOutput, + puiOutputLength)) != SDR_OK) { + SDFerr(SDF_F_SDF_EXTERNALPUBLICKEYOPERATION_RSA, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_InternalPublicKeyOperation_RSA( + void *hSessionHandle, + unsigned int uiKeyIndex, + unsigned char *pucDataInput, + unsigned int uiInputLength, + unsigned char *pucDataOutput, + unsigned int *puiOutputLength) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->InternalPublicKeyOperation_RSA) { + SDFerr(SDF_F_SDF_INTERNALPUBLICKEYOPERATION_RSA, + SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->InternalPublicKeyOperation_RSA( + hSessionHandle, + uiKeyIndex, + pucDataInput, + uiInputLength, + pucDataOutput, + puiOutputLength)) != SDR_OK) { + SDFerr(SDF_F_SDF_INTERNALPUBLICKEYOPERATION_RSA, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_InternalPrivateKeyOperation_RSA( + void *hSessionHandle, + unsigned int uiKeyIndex, + unsigned char *pucDataInput, + unsigned int uiInputLength, + unsigned char *pucDataOutput, + unsigned int *puiOutputLength) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->InternalPrivateKeyOperation_RSA) { + SDFerr(SDF_F_SDF_INTERNALPRIVATEKEYOPERATION_RSA, + SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->InternalPrivateKeyOperation_RSA( + hSessionHandle, + uiKeyIndex, + pucDataInput, + uiInputLength, + pucDataOutput, + puiOutputLength)) != SDR_OK) { + SDFerr(SDF_F_SDF_INTERNALPRIVATEKEYOPERATION_RSA, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ExternalVerify_ECC( + void *hSessionHandle, + unsigned int uiAlgID, + ECCrefPublicKey *pucPublicKey, + unsigned char *pucDataInput, + unsigned int uiInputLength, + ECCSignature *pucSignature) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ExternalVerify_ECC) { + SDFerr(SDF_F_SDF_EXTERNALVERIFY_ECC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ExternalVerify_ECC( + hSessionHandle, + uiAlgID, + pucPublicKey, + pucDataInput, + uiInputLength, + pucSignature)) != SDR_OK) { + SDFerr(SDF_F_SDF_EXTERNALVERIFY_ECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_InternalSign_ECC( + void *hSessionHandle, + unsigned int uiISKIndex, + unsigned char *pucData, + unsigned int uiDataLength, + ECCSignature *pucSignature) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->InternalSign_ECC) { + SDFerr(SDF_F_SDF_INTERNALSIGN_ECC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->InternalSign_ECC( + hSessionHandle, + uiISKIndex, + pucData, + uiDataLength, + pucSignature)) != SDR_OK) { + SDFerr(SDF_F_SDF_INTERNALSIGN_ECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_InternalVerify_ECC( + void *hSessionHandle, + unsigned int uiIPKIndex, + unsigned char *pucData, + unsigned int uiDataLength, + ECCSignature *pucSignature) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->InternalVerify_ECC) { + SDFerr(SDF_F_SDF_INTERNALVERIFY_ECC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->InternalVerify_ECC( + hSessionHandle, + uiIPKIndex, + pucData, + uiDataLength, + pucSignature)) != SDR_OK) { + SDFerr(SDF_F_SDF_INTERNALVERIFY_ECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_ExternalEncrypt_ECC( + void *hSessionHandle, + unsigned int uiAlgID, + ECCrefPublicKey *pucPublicKey, + unsigned char *pucData, + unsigned int uiDataLength, + ECCCipher *pucEncData) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ExternalEncrypt_ECC) { + SDFerr(SDF_F_SDF_EXTERNALENCRYPT_ECC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ExternalEncrypt_ECC( + hSessionHandle, + uiAlgID, + pucPublicKey, + pucData, + uiDataLength, + pucEncData)) != SDR_OK) { + SDFerr(SDF_F_SDF_EXTERNALENCRYPT_ECC, + SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_Encrypt( + void *hSessionHandle, + void *hKeyHandle, + unsigned int uiAlgID, + unsigned char *pucIV, + unsigned char *pucData, + unsigned int uiDataLength, + unsigned char *pucEncData, + unsigned int *puiEncDataLength) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->Encrypt) { + SDFerr(SDF_F_SDF_ENCRYPT, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->Encrypt( + hSessionHandle, + hKeyHandle, + uiAlgID, + pucIV, + pucData, + uiDataLength, + pucEncData, + puiEncDataLength)) != SDR_OK) { + SDFerr(SDF_F_SDF_ENCRYPT, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_Decrypt( + void *hSessionHandle, + void *hKeyHandle, + unsigned int uiAlgID, + unsigned char *pucIV, + unsigned char *pucEncData, + unsigned int uiEncDataLength, + unsigned char *pucData, + unsigned int *puiDataLength) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->Decrypt) { + SDFerr(SDF_F_SDF_DECRYPT, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->Decrypt( + hSessionHandle, + hKeyHandle, + uiAlgID, + pucIV, + pucEncData, + uiEncDataLength, + pucData, + puiDataLength)) != SDR_OK) { + SDFerr(SDF_F_SDF_DECRYPT, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_CalculateMAC( + void *hSessionHandle, + void *hKeyHandle, + unsigned int uiAlgID, + unsigned char *pucIV, + unsigned char *pucData, + unsigned int uiDataLength, + unsigned char *pucMAC, + unsigned int *puiMACLength) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->CalculateMAC) { + SDFerr(SDF_F_SDF_CALCULATEMAC, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->CalculateMAC( + hSessionHandle, + hKeyHandle, + uiAlgID, + pucIV, + pucData, + uiDataLength, + pucMAC, + puiMACLength)) != SDR_OK) { + SDFerr(SDF_F_SDF_CALCULATEMAC, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_HashInit( + void *hSessionHandle, + unsigned int uiAlgID, + ECCrefPublicKey *pucPublicKey, + unsigned char *pucID, + unsigned int uiIDLength) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->HashInit) { + SDFerr(SDF_F_SDF_HASHINIT, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->HashInit( + hSessionHandle, + uiAlgID, + pucPublicKey, + pucID, + uiIDLength)) != SDR_OK) { + SDFerr(SDF_F_SDF_HASHINIT, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_HashUpdate( + void *hSessionHandle, + unsigned char *pucData, + unsigned int uiDataLength) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->HashUpdate) { + SDFerr(SDF_F_SDF_HASHUPDATE, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->HashUpdate( + hSessionHandle, + pucData, + uiDataLength)) != SDR_OK) { + SDFerr(SDF_F_SDF_HASHUPDATE, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_HashFinal( + void *hSessionHandle, + unsigned char *pucHash, + unsigned int *puiHashLength) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->HashFinal) { + SDFerr(SDF_F_SDF_HASHFINAL, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->HashFinal( + hSessionHandle, + pucHash, + puiHashLength)) != SDR_OK) { + SDFerr(SDF_F_SDF_HASHFINAL, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + + +int SDF_CreateFile( + void *hSessionHandle, + unsigned char *pucFileName, + unsigned int uiNameLen, + unsigned int uiFileSize) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->CreateFile) { + SDFerr(SDF_F_SDF_CREATEFILE, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->CreateFile( + hSessionHandle, + pucFileName, + uiNameLen, + uiFileSize)) != SDR_OK) { + SDFerr(SDF_F_SDF_CREATEFILE, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + +int SDF_ReadFile( + void *hSessionHandle, + unsigned char *pucFileName, + unsigned int uiNameLen, + unsigned int uiOffset, + unsigned int *puiReadLength, + unsigned char *pucBuffer) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->ReadFile) { + SDFerr(SDF_F_SDF_READFILE, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->ReadFile( + hSessionHandle, + pucFileName, + uiNameLen, + uiOffset, + puiReadLength, + pucBuffer)) != SDR_OK) { + SDFerr(SDF_F_SDF_READFILE, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + +int SDF_WriteFile( + void *hSessionHandle, + unsigned char *pucFileName, + unsigned int uiNameLen, + unsigned int uiOffset, + unsigned int uiWriteLength, + unsigned char *pucBuffer) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->WriteFile) { + SDFerr(SDF_F_SDF_WRITEFILE, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->WriteFile( + hSessionHandle, + pucFileName, + uiNameLen, + uiOffset, + uiWriteLength, + pucBuffer)) != SDR_OK) { + SDFerr(SDF_F_SDF_WRITEFILE, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} + +int SDF_DeleteFile( + void *hSessionHandle, + unsigned char *pucFileName, + unsigned int uiNameLen) +{ + int ret = SDR_UNKNOWERR; + + if (!sdf_method || !sdf_method->DeleteFile) { + SDFerr(SDF_F_SDF_DELETEFILE, SDF_R_NOT_INITIALIZED); + return SDR_NOTSUPPORT; + } + + if ((ret = sdf_method->DeleteFile( + hSessionHandle, + pucFileName, + uiNameLen)) != SDR_OK) { + SDFerr(SDF_F_SDF_DELETEFILE, SDF_R_METHOD_OPERATION_FAILURE); + return ret; + } + + return SDR_OK; +} +