Verifies a signature on single-part data.
CK_BYTE_PTR pData,
CK_ULONG ulDataLen,
CK_BYTE_PTR pSignature,
CK_ULONG ulSignatureLen )
{
int32_t lMbedTLSResult;
CK_RV xResult = CKR_OK;
#if defined( MBEDTLS_SHA512_C )
#else
#endif
CK_BYTE pxCMACBuffer[ MBEDTLS_AES_BLOCK_SIZE ] = { 0 };
if( ( NULL == pData ) ||
( NULL == pSignature ) )
{
LogError( (
"Failed verify operation. Received a NULL pointer." ) );
xResult = CKR_ARGUMENTS_BAD;
}
if( xResult == CKR_OK )
{
{
{
LogError( (
"Failed verify operation. Data Length was too "
"short for pkcs11RSA_2048_SIGNATURE_LENGTH." ) );
xResult = CKR_DATA_LEN_RANGE;
}
{
LogError( (
"Failed verify operation. Signature Length was too "
"short for pkcs11RSA_2048_SIGNATURE_LENGTH." ) );
xResult = CKR_SIGNATURE_LEN_RANGE;
}
}
{
{
LogError( (
"Failed verify operation. Data Length was too "
"short for pkcs11SHA256_DIGEST_LENGTH." ) );
xResult = CKR_DATA_LEN_RANGE;
}
{
LogError( (
"Failed verify operation. Data Length was too "
"short for pkcs11ECDSA_P256_SIGNATURE_LENGTH." ) );
xResult = CKR_SIGNATURE_LEN_RANGE;
}
}
{
{
LogError( (
"Failed verify operation. Data Length was too "
"short for pkcs11SHA256_DIGEST_LENGTH." ) );
xResult = CKR_SIGNATURE_LEN_RANGE;
}
}
{
{
LogError( (
"Failed verify operation. Data Length was too "
"short for PKCS11_AES_CMAC_MIN_SIZE." ) );
xResult = CKR_SIGNATURE_LEN_RANGE;
}
}
else
{
LogError( (
"Failed verify operation. A C_Verify operation must be "
"initialized by a preceding call to C_VerifyInit. "
"This must happen before every call to C_Verify." ) );
xResult = CKR_OPERATION_NOT_INITIALIZED;
}
}
if( xResult == CKR_OK )
{
if( 0 == mbedtls_mutex_lock( &pxSessionObj->
xVerifyMutex ) )
{
{
{
lMbedTLSResult = mbedtls_pk_verify( &pxSessionObj->
xVerifyKey,
MBEDTLS_MD_SHA256,
pData,
ulDataLen,
pSignature,
ulSignatureLen );
if( 0 != lMbedTLSResult )
{
LogError( (
"Failed verify operation. mbedtls_pk_verify "
"failed: mbed TLS error = %s : %s.",
xResult = CKR_SIGNATURE_INVALID;
}
}
else
{
LogError( (
"Failed verify operation. Verify Key was not "
"present in session context." ) );
xResult = CKR_SIGNATURE_INVALID;
}
}
{
mbedtls_ecdsa_context * pxEcdsaContext;
mbedtls_mpi xR;
mbedtls_mpi xS;
mbedtls_mpi_init( &xR );
mbedtls_mpi_init( &xS );
lMbedTLSResult = mbedtls_mpi_read_binary( &xR, &pSignature[ 0 ], 32 );
if( lMbedTLSResult != 0 )
{
xResult = CKR_SIGNATURE_INVALID;
LogError( (
"Failed verify operation. Failed to parse R in EC "
"signature: mbed TLS error = %s : %s.",
}
else
{
lMbedTLSResult = mbedtls_mpi_read_binary( &xS, &pSignature[ 32 ], 32 );
if( lMbedTLSResult != 0 )
{
xResult = CKR_SIGNATURE_INVALID;
LogError( (
"Failed verify operation. Failed to parse S in "
"EC signature: mbed TLS error = %s : %s.",
}
}
if( xResult == CKR_OK )
{
{
lMbedTLSResult = mbedtls_ecdsa_verify( &pxEcdsaContext->grp, pData, ulDataLen, &pxEcdsaContext->Q, &xR, &xS );
}
if( lMbedTLSResult != 0 )
{
xResult = CKR_SIGNATURE_INVALID;
"mbedtls_ecdsa_verify failed: mbed TLS error = %s : %s.",
}
}
mbedtls_mpi_free( &xR );
mbedtls_mpi_free( &xS );
}
{
lMbedTLSResult = mbedtls_md_hmac_update( &pxSessionObj->
xHMACSecretContext, pData, ulDataLen );
if( lMbedTLSResult != 0 )
{
xResult = CKR_SIGNATURE_INVALID;
"mbedtls_md_hmac_update failed: mbed TLS error = %s : %s.",
}
else
{
lMbedTLSResult = mbedtls_md_hmac_finish( &pxSessionObj->
xHMACSecretContext, pxHMACBuffer );
if( lMbedTLSResult != 0 )
{
xResult = CKR_SIGNATURE_INVALID;
"mbedtls_md_hmac_finish failed: mbed TLS error = %s : %s.",
}
else
{
{
xResult = CKR_SIGNATURE_INVALID;
LogError( (
"Failed verify operation. Signature was invalid." ) );
}
}
}
}
{
lMbedTLSResult = mbedtls_cipher_cmac_update( &pxSessionObj->
xCMACSecretContext, pData, ulDataLen );
if( lMbedTLSResult != 0 )
{
xResult = CKR_SIGNATURE_INVALID;
"mbedtls_md_hmac_update failed: mbed TLS error = %s : %s.",
}
else
{
lMbedTLSResult = mbedtls_cipher_cmac_finish( &pxSessionObj->
xCMACSecretContext, pxCMACBuffer );
if( lMbedTLSResult != 0 )
{
xResult = CKR_SIGNATURE_INVALID;
"mbedtls_md_hmac_finish failed: mbed TLS error = %s : %s.",
}
else
{
if( 0 != memcmp( pxCMACBuffer, pSignature, MBEDTLS_AES_BLOCK_SIZE ) )
{
xResult = CKR_SIGNATURE_INVALID;
LogError( (
"Failed verify operation. Signature was invalid." ) );
}
}
}
}
else
{
LogError( (
"Failed verify operation. Received an unexpected mechanism." ) );
}
( void ) mbedtls_mutex_unlock( &pxSessionObj->
xVerifyMutex );
}
else
{
LogError( (
"Failed to initialize verify operation. Could not "
"take xVerifyMutex." ) );
xResult = CKR_CANT_LOCK;
}
}
if( xResult != CKR_SESSION_HANDLE_INVALID )
{
LogDebug( (
"Reset Verify mechanism to pkcs11NO_OPERATION." ) );
}
return xResult;
}
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: core_pkcs11.h:77
#define pkcs11RSA_2048_SIGNATURE_LENGTH
Length of PKCS #11 signature for RSA 2048 key, in bytes.
Definition: core_pkcs11.h:136
#define pkcs11SHA256_DIGEST_LENGTH
Length of a SHA256 digest, in bytes.
Definition: core_pkcs11.h:97
#define pkcs11ECDSA_P256_SIGNATURE_LENGTH
Length of a curve P-256 ECDSA signature, in bytes. PKCS #11 EC signatures are represented as a 32-bit...
Definition: core_pkcs11.h:111
#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
static void prvHMACCleanUp(P11Session_t *pxSession)
Helper function for cleaning up a HMAC operation.
Definition: core_pkcs11_mbedtls.c:3901
static void prvCMACCleanUp(P11Session_t *pxSession)
Helper function for cleaning up an CMAC operation.
Definition: core_pkcs11_mbedtls.c:4003
CK_RV C_Verify(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG ulSignatureLen)
Verifies a signature on single-part data.
Definition: core_pkcs11_mbedtls.c:4939
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
static void prvVerifyInitEC_RSACleanUp(P11Session_t *pxSession)
Helper function for cleaning up a verify operation for an EC or RSA key.
Definition: core_pkcs11_mbedtls.c:4660
#define PKCS11_AES_CMAC_MIN_SIZE
Private define for minimum AES-CMAC key size, in bytes.
Definition: core_pkcs11_mbedtls.c:236
#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_threading_mutex_t xVerifyMutex
Protects the verification key from being modified while in use.
Definition: core_pkcs11_mbedtls.c:306
mbedtls_md_context_t xHMACSecretContext
Context for in progress HMAC operation. Set during C_SignInit or C_VerifyInit.
Definition: core_pkcs11_mbedtls.c:315
CK_MECHANISM_TYPE xOperationVerifyMechanism
The mechanism of verify operation in progress. Set during C_VerifyInit.
Definition: core_pkcs11_mbedtls.c:305
mbedtls_pk_context xVerifyKey
Verification key. Set during C_VerifyInit.
Definition: core_pkcs11_mbedtls.c:308
mbedtls_cipher_context_t xCMACSecretContext
Context for in progress CMAC operation. Set during C_SignInit or C_VerifyInit.
Definition: core_pkcs11_mbedtls.c:317