corePKCS11 v3.5.0
PKCS #11 Cryptoki Library
C_DigestFinal

Finishes a multiple-part digesting operation.

CK_DECLARE_FUNCTION( CK_RV, C_DigestFinal )( CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pDigest,
CK_ULONG_PTR pulDigestLen )
{
P11Session_t * pxSession = prvSessionPointerFromHandle( hSession );
CK_RV xResult = prvCheckValidSessionAndModule( pxSession );
int32_t lMbedTLSResult = 0;
if( pulDigestLen == NULL )
{
LogError( ( "Failed to finish digest operation. Digest Length pointer "
"was NULL." ) );
xResult = CKR_ARGUMENTS_BAD;
}
if( xResult == CKR_OK )
{
if( pxSession->xOperationDigestMechanism != CKM_SHA256 )
{
LogError( ( "Failed to finish digest operation. Digest operation "
"was not initialized." ) );
xResult = CKR_OPERATION_NOT_INITIALIZED;
}
}
if( xResult == CKR_OK )
{
if( pDigest == NULL )
{
/* Supply the required buffer size. */
*pulDigestLen = ( CK_ULONG ) pkcs11SHA256_DIGEST_LENGTH;
}
else
{
if( *pulDigestLen == ( CK_ULONG ) pkcs11SHA256_DIGEST_LENGTH )
{
#if MBEDTLS_VERSION_NUMBER < 0x03000000
lMbedTLSResult = mbedtls_sha256_finish_ret( &pxSession->xSHA256Context, pDigest );
#else
lMbedTLSResult = mbedtls_sha256_finish( &pxSession->xSHA256Context, pDigest );
#endif /* MBEDTLS_VERSION_NUMBER < 0x03000000 */
if( 0 != lMbedTLSResult )
{
LogError( ( "Failed to finish digest operation. "
"mbedtls_sha256_finish_ret failed: mbed TLS "
"error = %s : %s.",
mbedtlsHighLevelCodeOrDefault( lMbedTLSResult ),
mbedtlsLowLevelCodeOrDefault( lMbedTLSResult ) ) );
xResult = CKR_FUNCTION_FAILED;
}
}
else
{
LogError( ( "Failed to finish digest operation. Received a "
"buffer that was an unexpected size. Expected %lu and "
"received %lu.",
( unsigned long int ) pkcs11SHA256_DIGEST_LENGTH,
( unsigned long int ) *pulDigestLen ) );
xResult = CKR_BUFFER_TOO_SMALL;
}
}
}
if( ( xResult != CKR_OK ) && ( xResult != CKR_BUFFER_TOO_SMALL ) &&
( xResult != CKR_SESSION_HANDLE_INVALID ) &&
( xResult != CKR_OPERATION_NOT_INITIALIZED ) )
{
LogDebug( ( "Error occurred, tearing down digest operation." ) );
mbedtls_sha256_free( &pxSession->xSHA256Context );
}
return xResult;
}
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: core_pkcs11.h:77
#define pkcs11SHA256_DIGEST_LENGTH
Length of a SHA256 digest, in bytes.
Definition: core_pkcs11.h:97
#define LogError(message)
Macro that is called in the corePKCS11 library for logging "Error" level messages.
Definition: core_pkcs11_config_defaults.h:317
#define LogDebug(message)
Macro that is called in the corePKCS11 library for logging "Debug" level messages.
Definition: core_pkcs11_config_defaults.h:377
#define mbedtlsLowLevelCodeOrDefault(mbedTlsCode)
Utility for converting the level-level code in an mbedTLS error to string, if the code-contains a lev...
Definition: core_pkcs11_mbedtls.c:96
CK_RV C_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
Finishes a multiple-part digesting operation.
Definition: core_pkcs11_mbedtls.c:3816
static P11Session_t * prvSessionPointerFromHandle(CK_SESSION_HANDLE xSession)
Maps an opaque caller session handle into its internal state structure.
Definition: core_pkcs11_mbedtls.c:382
#define mbedtlsHighLevelCodeOrDefault(mbedTlsCode)
Utility for converting the high-level code in an mbedTLS error to string, if the code-contains a high...
Definition: core_pkcs11_mbedtls.c:88
static CK_RV prvCheckValidSessionAndModule(const P11Session_t *pxSession)
Helper to check if the current session is initialized and valid.
Definition: core_pkcs11_mbedtls.c:336
#define pkcs11NO_OPERATION
Indicates that no PKCS #11 operation is underway for given session.
Definition: core_pkcs11_mbedtls.c:112
Session structure.
Definition: core_pkcs11_mbedtls.c:299
mbedtls_sha256_context xSHA256Context
Context for in progress digest operation.
Definition: core_pkcs11_mbedtls.c:313
CK_MECHANISM_TYPE xOperationDigestMechanism
Indicates if a digest operation is in progress.
Definition: core_pkcs11_mbedtls.c:302
See also
C_DigestInit(), C_DigestUpdate()
Note
Digest parameters are shared by a session. Calling C_DigestInit(), C_DigestUpdate(), and C_DigestFinal() with the same session across different tasks may lead to unexpected results.
Parameters
[in]hSessionHandle of a valid PKCS #11 session.
[out]pDigestPointer to the location that receives the message digest. Memory must be allocated by the caller. Caller is responsible for allocating memory. Providing NULL for this input will cause pulDigestLen to be updated for length of buffer required.
[in,out]pulDigestLenPoints to the location that holds the length of the message digest. If pDigest is NULL, this value is updated to contain the length of the buffer needed to hold the digest. Else it is updated to contain the actual length of the digest placed in pDigest.
Returns
CKR_OK if successful.