corePKCS11  v3.2.0
PKCS #11 Cryptoki Library
C_DigestInit

Initializes a message-digesting operation.

CK_DECLARE_FUNCTION( CK_RV, C_DigestInit )( CK_SESSION_HANDLE hSession,
CK_MECHANISM_PTR pMechanism )
{
P11Session_t * pxSession = prvSessionPointerFromHandle( hSession );
CK_RV xResult = prvCheckValidSessionAndModule( pxSession );
int32_t lMbedTLSResult = 0;
if( pMechanism == NULL )
{
LogError( ( "Failed to initialize digest operation. Mechanism pointer "
"was NULL." ) );
xResult = CKR_ARGUMENTS_BAD;
}
if( xResult == CKR_OK )
{
/* See explanation in prvCheckValidSessionAndModule for this exception. */
/* coverity[misra_c_2012_rule_10_5_violation] */
if( prvOperationActive( pxSession ) == ( CK_BBOOL ) CK_TRUE )
{
LogError( ( "Failed to initialize digest operation. An operation "
"was already active." ) );
xResult = CKR_OPERATION_ACTIVE;
}
}
if( xResult == CKR_OK )
{
if( pMechanism->mechanism != CKM_SHA256 )
{
LogError( ( "Failed to initialize digest operation. Currently only "
"the CKM_SHA256 mechanism is supported." ) );
xResult = CKR_MECHANISM_INVALID;
}
}
/*
* Initialize the requested hash type
*/
if( xResult == CKR_OK )
{
mbedtls_sha256_init( &pxSession->xSHA256Context );
lMbedTLSResult = mbedtls_sha256_starts_ret( &pxSession->xSHA256Context, 0 );
if( 0 != lMbedTLSResult )
{
LogError( ( "Failed to initialize digest operation. "
"mbedtls_sha256_starts_ret failed with: mbed TLS error = %s : %s.",
mbedtlsHighLevelCodeOrDefault( lMbedTLSResult ),
mbedtlsLowLevelCodeOrDefault( lMbedTLSResult ) ) );
xResult = CKR_FUNCTION_FAILED;
}
else
{
pxSession->xOperationDigestMechanism = pMechanism->mechanism;
}
}
return xResult;
}
See also
C_DigestUpdate(), C_DigestFinal()
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.
[in]pMechanismDigesting mechanism. This port only supports the mechanism CKM_SHA256.
Returns
CKR_OK if successful.
mbedtlsLowLevelCodeOrDefault
#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:90
P11Session_t::xOperationDigestMechanism
CK_MECHANISM_TYPE xOperationDigestMechanism
Indicates if a digest operation is in progress.
Definition: core_pkcs11_mbedtls.c:289
P11Session_t::xSHA256Context
mbedtls_sha256_context xSHA256Context
Context for in progress digest operation.
Definition: core_pkcs11_mbedtls.c:300
prvOperationActive
static CK_BBOOL prvOperationActive(const P11Session_t *pxSession)
Determines if an operation is in progress.
Definition: core_pkcs11_mbedtls.c:385
prvCheckValidSessionAndModule
static CK_RV prvCheckValidSessionAndModule(const P11Session_t *pxSession)
Helper to check if the current session is initialized and valid.
Definition: core_pkcs11_mbedtls.c:323
CK_DECLARE_FUNCTION
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: core_pkcs11.h:75
prvSessionPointerFromHandle
static P11Session_t * prvSessionPointerFromHandle(CK_SESSION_HANDLE xSession)
Maps an opaque caller session handle into its internal state structure.
Definition: core_pkcs11_mbedtls.c:365
P11Session_t
Session structure.
Definition: core_pkcs11_mbedtls.c:286
C_DigestInit
CK_RV C_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism)
Initializes a message-digesting operation.
Definition: core_pkcs11_mbedtls.c:3545
LogError
#define LogError(message)
Macro that is called in the corePKCS11 library for logging "Error" level messages.
Definition: core_pkcs11_config_defaults.h:315
mbedtlsHighLevelCodeOrDefault
#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:82