⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sadvapi.cpp

📁 windows的加密api源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    BOOL            fCritSec = FALSE;
    BOOL            fRet = FALSE;

    __try {
        pVTable = pVKey->pVTable;

        EnterProviderCritSec(pVTable);
        fCritSec = TRUE;

        fRet = CPGetKeyParam(
                        pVTable->hProv,
                        pVKey->hKey,
                        dwParam,
                        pbData,
                        pdwDataLen,
                        dwFlags
                        );
    } __except( EXCEPTION_EXECUTE_HANDLER ) {
        fRet = FALSE;
        ASSERT( fRet );
    }

    if( fCritSec ) {
        LeaveProviderCritSec(pVTable);
    }

    return fRet;
}


/*
 -  CryptGenRandom
 -
 *  Purpose:
 *                Used to fill a buffer with random bytes
 *
 *
 *  Parameters:
 *               IN  hProv      -  Handle to the user identifcation
 *               IN  dwLen      -  Number of bytes of random data requested
 *               OUT pbBuffer   -  Pointer to the buffer where the random
 *                                 bytes are to be placed
 *
 *  Returns:
 */
BOOL
WINAPI SCryptGenRandom(IN HCRYPTPROV hProv,
                      IN DWORD dwLen,
                      OUT BYTE *pbBuffer)

{
    PVTableStruc    pVTable = (PVTableStruc) hProv;
    BOOL            fCritSec = FALSE;
    BOOL            fRet = FALSE;

    __try {
        EnterProviderCritSec(pVTable);
        fCritSec = TRUE;

        fRet = CPGenRandom(pVTable->hProv, dwLen, pbBuffer);
    } __except( EXCEPTION_EXECUTE_HANDLER ) {
        fRet = FALSE;
        ASSERT( fRet );
    }

    if( fCritSec ) {
        LeaveProviderCritSec(pVTable);
    }

    return fRet;
}

/*
 -  CryptGetUserKey
 -
 *  Purpose:
 *                Gets a handle to a permanent user key
 *
 *
 *  Parameters:
 *               IN  hProv      -  Handle to the user identifcation
 *               IN  dwKeySpec  -  Specification of the key to retrieve
 *               OUT phUserKey  -  Pointer to key handle of retrieved key
 *
 *  Returns:
 */
BOOL
WINAPI SCryptGetUserKey(IN HCRYPTPROV hProv,
                       IN DWORD dwKeySpec,
                       OUT HCRYPTKEY *phUserKey)
{
    PVTableStruc    pVTable = (PVTableStruc) hProv;
    PVKeyStruc      pVKey;
    BOOL            fCritSec = FALSE;
    BOOL            fRet = FALSE;


    __try {
        *phUserKey = 0;
        pVKey = BuildVKey(pVTable);

        if( pVKey == NULL ) {
            return FALSE;
        }

        EnterProviderCritSec(pVTable);
        fCritSec = TRUE;

        fRet = CPGetUserKey(pVTable->hProv, dwKeySpec, &pVKey->hKey);
    } __except( EXCEPTION_EXECUTE_HANDLER ) {
        fRet = FALSE;
        ASSERT( fRet );
    }

    if( fCritSec ) {
        LeaveProviderCritSec(pVTable);
    }

    if( fRet ) {
        *phUserKey = (HCRYPTKEY) pVKey;
        return TRUE;
    }

    if (pVKey)
        LocalFree(pVKey);

    __try {
        *phUserKey = 0;
    } __except(EXCEPTION_EXECUTE_HANDLER) {
        ; // gulp
    }

    return FALSE;
}



/*
 -  CryptExportKey
 -
 *  Purpose:
 *                Export cryptographic keys out of a CSP in a secure manner
 *
 *
 *  Parameters:
 *               IN  hKey       - Handle to the key to export
 *               IN  hPubKey    - Handle to the exchange public key value of
 *                                the destination user
 *               IN  dwBlobType - Type of key blob to be exported
 *               IN  dwFlags -    Flags values
 *               OUT pbData -     Key blob data
 *               OUT pdwDataLen - Length of key blob in bytes
 *
 *  Returns:
 */
BOOL
WINAPI SCryptExportKey(IN HCRYPTKEY hKey,
                      IN HCRYPTKEY hPubKey,
                      IN DWORD dwBlobType,
                      IN DWORD dwFlags,
                      OUT BYTE *pbData,
                      OUT DWORD *pdwDataLen)
{
    PVKeyStruc      pVKey = (PVKeyStruc) hKey;
    PVTableStruc    pVTable;
    PVKeyStruc      pVPublicKey = (PVKeyStruc) hPubKey;
    BOOL            fCritSec = FALSE;
    BOOL            fRet = FALSE;


    __try {
        // Note: the SCP requires that the hPubKey has the same hProv as
        // the hKey. This is a problem for the MITV implementation where the
        // signature and exchange keys always have a different provider.

        pVTable = pVKey->pVTable;

        if (pVPublicKey && pVPublicKey->pVTable != pVTable)
        {
            *pdwDataLen = 0;
            SetLastError(ERROR_INVALID_PARAMETER);
            return FALSE;
        }

        EnterProviderCritSec(pVTable);
        fCritSec = TRUE;

        fRet = CPExportKey(
                        pVTable->hProv,
                        pVKey->hKey,
                        (pVPublicKey == NULL ? 0 : pVPublicKey->hKey),
                        dwBlobType,
                        dwFlags,
                        pbData,
                        pdwDataLen
                        );
    } __except( EXCEPTION_EXECUTE_HANDLER ) {
        fRet = FALSE;
        ASSERT( fRet );
    }

    if( fCritSec ) {
        LeaveProviderCritSec(pVTable);
    }

    return fRet;
}


/*
 -  CryptImportKey
 -
 *  Purpose:
 *                Import cryptographic keys
 *
 *
 *  Parameters:
 *               IN  hProv     -  Handle to the CSP user
 *               IN  pbData    -  Key blob data
 *               IN  dwDataLen -  Length of the key blob data
 *               IN  hPubKey   -  Handle to the exchange public key value of
 *                                the destination user
 *               IN  dwFlags   -  Flags values
 *               OUT phKey     -  Pointer to the handle to the key which was
 *                                Imported
 *
 *  Returns:
 */
BOOL
WINAPI SCryptImportKey(IN HCRYPTPROV hProv,
                      IN CONST BYTE *pbData,
                      IN DWORD dwDataLen,
                      IN HCRYPTKEY hPubKey,
                      IN DWORD dwFlags,
                      OUT HCRYPTKEY *phKey)
{
    PVTableStruc    pVTable = (PVTableStruc) hProv;
    PVKeyStruc      pVKey;
    PVKeyStruc      pVPublicKey = (PVKeyStruc) hPubKey;
    BOOL            fCritSec = FALSE;
    BOOL            fRet = FALSE;

    __try {

        *phKey = 0;

        if (pVPublicKey && pVPublicKey->pVTable != pVTable)
        {
            SetLastError(ERROR_INVALID_PARAMETER);
            return FALSE;
        }

        pVKey = BuildVKey(pVTable);

        if( pVKey == NULL )
            return FALSE;

        EnterProviderCritSec(pVTable);
        fCritSec = TRUE;

        fRet = CPImportKey(
                        pVTable->hProv,
                        pbData,
                        dwDataLen,
                        (pVPublicKey == NULL ? 0 : pVPublicKey->hKey),
                        dwFlags,
                        &pVKey->hKey
                        );
    } __except( EXCEPTION_EXECUTE_HANDLER ) {
        fRet = FALSE;
        ASSERT( fRet );
    }

    if( fCritSec ) {
        LeaveProviderCritSec(pVTable);
    }

    if( fRet ) {
        *phKey = (HCRYPTKEY) pVKey;
        return TRUE;
    }

    if (pVKey)
        LocalFree(pVKey);

    __try {
        *phKey = 0;
    } __except( EXCEPTION_EXECUTE_HANDLER ) {
        ; // gulp
    }

    return FALSE;
}


/*
 -  CryptEncrypt
 -
 *  Purpose:
 *                Encrypt data
 *
 *
 *  Parameters:
 *               IN  hKey          -  Handle to the key
 *               IN  hHash         -  Optional handle to a hash
 *               IN  Final         -  Boolean indicating if this is the final
 *                                    block of plaintext
 *               IN  dwFlags       -  Flags values
 *               IN OUT pbData     -  Data to be encrypted
 *               IN OUT pdwDataLen -  Pointer to the length of the data to be
 *                                    encrypted
 *               IN dwBufLen       -  Size of Data buffer
 *
 *  Returns:
 */
BOOL
WINAPI SCryptEncrypt(IN HCRYPTKEY hKey,
                    IN HCRYPTHASH hHash,
                    IN BOOL Final,
                    IN DWORD dwFlags,
                    IN OUT BYTE *pbData,
                    IN OUT DWORD *pdwDataLen,
                    IN DWORD dwBufLen)
{
    PVKeyStruc      pVKey = (PVKeyStruc) hKey;
    PVTableStruc    pVTable;
    PVHashStruc     pVHash = (PVHashStruc) hHash;
    BOOL            fCritSec = FALSE;
    BOOL            fRet = FALSE;


    __try {

        pVTable = pVKey->pVTable;

        if (pVHash && pVHash->pVTable != pVTable)
        {
            *pdwDataLen = 0;
            SetLastError(ERROR_INVALID_PARAMETER);
            return FALSE;
        }

        EnterProviderCritSec(pVTable);
        fCritSec = TRUE;

        fRet = CPEncrypt(
                        pVTable->hProv,
                        pVKey->hKey,
                        (pVHash == NULL ? 0 : pVHash->hHash),
                        Final,
                        dwFlags,
                        pbData,
                        pdwDataLen,
                        dwBufLen
                        );
    } __except( EXCEPTION_EXECUTE_HANDLER ) {
        fRet = FALSE;
        ASSERT( fRet );
    }

    if( fCritSec ) {
        LeaveProviderCritSec(pVTable);
    }

    return fRet;
}


/*
 -  CryptDecrypt
 -
 *  Purpose:
 *                Decrypt data
 *
 *
 *  Parameters:
 *               IN  hKey          -  Handle to the key
 *               IN  hHash         -  Optional handle to a hash
 *               IN  Final         -  Boolean indicating if this is the final
 *                                    block of ciphertext
 *               IN  dwFlags       -  Flags values
 *               IN OUT pbData     -  Data to be decrypted
 *               IN OUT pdwDataLen -  Pointer to the length of the data to be
 *                                    decrypted
 *
 *  Returns:
 */
BOOL
WINAPI SCryptDecrypt(IN HCRYPTKEY hKey,
                    IN HCRYPTHASH hHash,
                    IN BOOL Final,
                    IN DWORD dwFlags,
                    IN OUT BYTE *pbData,
                    IN OUT DWORD *pdwDataLen)

{
    PVKeyStruc      pVKey = (PVKeyStruc) hKey;
    PVTableStruc    pVTable;
    PVHashStruc     pVHash = (PVHashStruc) hHash;
    BOOL            fCritSec = FALSE;
    BOOL            fRet = FALSE;

    __try {
        pVTable = pVKey->pVTable;

        if (pVHash && pVHash->pVTable != pVTable)
        {
            *pdwDataLen = 0;
            SetLastError(ERROR_INVALID_PARAMETER);
            return FALSE;
        }

        EnterProviderCritSec(pVTable);
        fCritSec = TRUE;

        fRet = CPDecrypt(
                        pVTable->hProv,
                        pVKey->hKey,
                        (pVHash == NULL ? 0 : pVHash->hHash),
                        Final,
                        dwFlags,
                        pbData,
                        pdwDataLen
                        );
    } __except( EXCEPTION_EXECUTE_HANDLER ) {
        fRet = FALSE;
        ASSERT( fRet );
    }

    if( fCritSec ) {
        LeaveProviderCritSec(pVTable);
    }

    return fRet;
}


/*
 -  CryptCreateHash
 -
 *  Purpose:
 *                initate the hashing of a stream of data
 *
 *
 *  Parameters:
 *               IN  hProv   -  Handle to the user identifcation
 *               IN  Algid   -  Algorithm identifier of the hash algorithm
 *                              to be used
 *               IN  hKey    -  Optional key for MAC algorithms
 *               IN  dwFlags -  Flags values
 *               OUT pHash   -  Handle to hash object
 *
 *  Returns:
 */
BOOL
WINAPI SCryptCreateHash(IN HCRYPTPROV hProv,
                       IN ALG_ID Algid,
                       IN HCRYPTKEY hKey,
                       IN DWORD dwFlags,
                       OUT HCRYPTHASH *phHash)
{
    PVTableStruc    pVTable = (PVTableStruc) hProv;
    PVKeyStruc      pVKey = (PVKeyStruc) hKey;
    PVHashStruc     pVHash;
    BOOL            fCritSec = FALSE;
    BOOL            fRet = FALSE;

    __try {
        *phHash = 0;

        if (pVKey && pVKey->pVTable != pVTable)
        {
            SetLastError(ERROR_INVALID_PARAMETER);
            return FALSE;
        }

        pVHash = BuildVHash(pVTable);
        if( pVHash == NULL )
            return FALSE;

        EnterProviderCritSec(pVTable);
        fCritSec = TRUE;

        fRet = CPCreateHash(
                        pVTable->hProv,
                        Algid,
                        (pVKey == NULL ? 0 : pVKey->hKey),
                        dwFlags,
                        &pVHash->hHash
                        );
    } __except( EXCEPTION_EXECUTE_HANDLER ) {
        fRet = FALSE;
        ASSERT( fRet );
    }

    if( fCritSec ) {
        LeaveProviderCritSec(pVTable);
    }

    if( fRet ) {
        *phHash = (HCRYPTHASH) pVHash;
        return TRUE;
    }

    if (pVHash)
        LocalFree(pVHash);

    __try {
        *phHash = 0;
    } __except(EXCEPTION_EXECUTE_HANDLER) {
        ; // gulp
    }

    return FALSE;
}

/*
 -      CryptDuplicateHash
 -
 *      Purpose:
 *                Duplicate a cryptographic hash
 *
 *
 *      Parameters:
 *               IN      hHash          -  Handle to the hash to be duplicated
 *               IN      pdwReserved    -  Reserved for later use
 *               IN      dwFlags        -  Flags values
 *               OUT     phHash         -  Handle to the new duplicate hash
 *
 *      Returns:
 */
BOOL
WINAPI SCryptDuplicateHash(
                         IN HCRYPTHASH hHash,
                         IN DWORD *pdwReserved,
                         IN DWORD dwFlags,
                         OUT HCRYPTHASH * phHash
                         )
{
    PVHashStruc     pVHash = (PVHashStruc) hHash;
    PVTableStruc    pVTable;
    PVHashStruc     pVNewHash;
    BOOL            fCritSec = FALSE;
    BOOL            fRet = FALSE;


    __try {
        *phHash = 0;
        pVTable = pVHash->pVTable;

        pVNewHash = BuildVHash(pVTable);
        if( pVNewHash == NULL ) {
            return FALSE;
        }

        EnterProviderCritSec(pVTable);
        fCritSec = TRUE;

        fRet = CPDuplicateHash(
                        pVTable->hProv,
                        pVHash->hHash,
                        pdwReserved,
                        dwFlags,
                        &pVNewHash->hHash
                        );
    } __except (EXCEPTION_EXECUTE_HANDLER) {
        fRet = FALSE;
        ASSERT( fRet );
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -