FreeRTOS: PKCS11
PKCS11 Cryptoki Library
Return to main page ↑
C_Verify

Verifies a signature on single-part data.

CK_DECLARE_FUNCTION( CK_RV, C_Verify )( CK_SESSION_HANDLE hSession,
CK_BYTE_PTR pData,
CK_ULONG ulDataLen,
CK_BYTE_PTR pSignature,
CK_ULONG ulSignatureLen )
{
P11Session_t * pxSessionObj;
int32_t lMbedTLSResult;
pxSessionObj = prvSessionPointerFromHandle( hSession );
CK_RV xResult = prvCheckValidSessionAndModule( pxSessionObj );
/* Check parameters. */
if( ( NULL == pData ) ||
( NULL == pSignature ) )
{
xResult = CKR_ARGUMENTS_BAD;
}
/* Check that the signature and data are the expected length.
* These PKCS #11 mechanism expect data to be pre-hashed/formatted. */
if( xResult == CKR_OK )
{
if( pxSessionObj->xOperationVerifyMechanism == CKM_RSA_X_509 )
{
{
xResult = CKR_DATA_LEN_RANGE;
}
if( ulSignatureLen != pkcs11RSA_2048_SIGNATURE_LENGTH )
{
xResult = CKR_SIGNATURE_LEN_RANGE;
}
}
else if( pxSessionObj->xOperationVerifyMechanism == CKM_ECDSA )
{
if( ulDataLen != pkcs11SHA256_DIGEST_LENGTH )
{
xResult = CKR_DATA_LEN_RANGE;
}
if( ulSignatureLen != pkcs11ECDSA_P256_SIGNATURE_LENGTH )
{
xResult = CKR_SIGNATURE_LEN_RANGE;
}
}
else
{
xResult = CKR_OPERATION_NOT_INITIALIZED;
}
}
/* Verification step. */
if( xResult == CKR_OK )
{
/* Perform an RSA verification. */
if( pxSessionObj->xOperationVerifyMechanism == CKM_RSA_X_509 )
{
if( pdTRUE == xSemaphoreTake( pxSessionObj->xVerifyMutex, portMAX_DELAY ) )
{
/* Verify the signature. If a public key is present, use it. */
if( NULL != pxSessionObj->xVerifyKey.pk_ctx )
{
if( 0 != mbedtls_pk_verify( &pxSessionObj->xVerifyKey,
MBEDTLS_MD_SHA256,
pData,
ulDataLen,
pSignature,
ulSignatureLen ) )
{
xResult = CKR_SIGNATURE_INVALID;
}
}
( void ) xSemaphoreGive( pxSessionObj->xVerifyMutex );
}
else
{
xResult = CKR_CANT_LOCK;
}
}
/* Perform an ECDSA verification. */
else if( pxSessionObj->xOperationVerifyMechanism == CKM_ECDSA )
{
/* TODO: Refactor w/ test code
* An ECDSA signature is comprised of 2 components - R & S. C_Sign returns them one after another. */
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 )
{
lMbedTLSResult = mbedtls_mpi_read_binary( &xS, &pSignature[ 32 ], 32 );
}
if( lMbedTLSResult != 0 )
{
xResult = CKR_SIGNATURE_INVALID;
PKCS11_PRINT( ( "Failed to parse EC signature: %s : ",
mbedtlsHighLevelCodeOrDefault( lMbedTLSResult ) ) );
PKCS11_PRINT( ( "%s \r\n",
mbedtlsLowLevelCodeOrDefault( lMbedTLSResult ) ) );
}
if( xResult == CKR_OK )
{
if( pdTRUE == xSemaphoreTake( pxSessionObj->xVerifyMutex, portMAX_DELAY ) )
{
/* Verify the signature. If a public key is present, use it. */
if( NULL != pxSessionObj->xVerifyKey.pk_ctx )
{
pxEcdsaContext = pxSessionObj->xVerifyKey.pk_ctx;
lMbedTLSResult = mbedtls_ecdsa_verify( &pxEcdsaContext->grp, pData, ulDataLen, &pxEcdsaContext->Q, &xR, &xS );
}
( void ) xSemaphoreGive( pxSessionObj->xVerifyMutex );
if( lMbedTLSResult != 0 )
{
xResult = CKR_SIGNATURE_INVALID;
PKCS11_PRINT( ( "Failed to parse EC signature: %s : ",
mbedtlsHighLevelCodeOrDefault( lMbedTLSResult ) ) );
PKCS11_PRINT( ( "%s \r\n",
mbedtlsLowLevelCodeOrDefault( lMbedTLSResult ) ) );
}
}
}
mbedtls_mpi_free( &xR );
mbedtls_mpi_free( &xS );
}
else
{
xResult = CKR_TEMPLATE_INCONSISTENT;
}
}
if( xResult != CKR_SESSION_HANDLE_INVALID )
{
}
/* Return the signature verification result. */
return xResult;
}
Note
C_VerifyInit() must have been called previously.
C_Verify() parameters are shared by a session. Calling C_VerifyInit() & C_Verify() with the same session across different tasks may lead to unexpected results.
Parameters
[in]hSessionHandle of a valid PKCS #11 session.
[in]pDataData who's signature is to be verified. Note: In this implementation, this is generally expected to be the hash of the data.
[in]ulDataLenLength of pucData.
[in]pSignatureThe signature to be verified.
[in]ulSignatureLenLength of pucSignature in bytes.
Returns
CKR_OK if successful. Else, see PKCS #11 specification for more information.
P11Session_t::xOperationVerifyMechanism
CK_MECHANISM_TYPE xOperationVerifyMechanism
The mechanism of verify operation in progress. Set during C_VerifyInit.
Definition: iot_pkcs11_mbedtls.c:223
CK_DECLARE_FUNCTION
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: iot_pkcs11.h:66
P11Session_t::xVerifyMutex
SemaphoreHandle_t xVerifyMutex
Protects the verification key from being modified while in use.
Definition: iot_pkcs11_mbedtls.c:224
pkcs11SHA256_DIGEST_LENGTH
#define pkcs11SHA256_DIGEST_LENGTH
Length of a SHA256 digest, in bytes.
Definition: iot_pkcs11.h:83
mbedtlsLowLevelCodeOrDefault
#define mbedtlsLowLevelCodeOrDefault(mbedTlsCode)
Utility for converting the level-level code in an mbedTLS error to string, if the code-contains a lev...
Definition: iot_pkcs11_mbedtls.c:94
PKCS11_PRINT
#define PKCS11_PRINT(X)
Macro for logging in PKCS #11.
Definition: iot_pkcs11_mbedtls.c:103
pkcs11RSA_2048_SIGNATURE_LENGTH
#define pkcs11RSA_2048_SIGNATURE_LENGTH
Length of PKCS #11 signature for RSA 2048 key, in bytes.
Definition: iot_pkcs11.h:111
prvSessionPointerFromHandle
static P11Session_t * prvSessionPointerFromHandle(CK_SESSION_HANDLE xSession)
Maps an opaque caller session handle into its internal state structure.
Definition: iot_pkcs11_mbedtls.c:287
prvCheckValidSessionAndModule
static CK_RV prvCheckValidSessionAndModule(const P11Session_t *pxSession)
Helper to check if the current session is initialized and valid.
Definition: iot_pkcs11_mbedtls.c:248
P11Session_t::xVerifyKey
mbedtls_pk_context xVerifyKey
Verification key. Set during C_VerifyInit.
Definition: iot_pkcs11_mbedtls.c:225
C_Verify
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: iot_pkcs11_mbedtls.c:3844
mbedtlsHighLevelCodeOrDefault
#define mbedtlsHighLevelCodeOrDefault(mbedTlsCode)
Utility for converting the high-level code in an mbedTLS error to string, if the code-contains a high...
Definition: iot_pkcs11_mbedtls.c:86
P11Session_t
Session structure.
Definition: iot_pkcs11_mbedtls.c:217
pkcs11ECDSA_P256_SIGNATURE_LENGTH
#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: iot_pkcs11.h:90
pkcs11NO_OPERATION
#define pkcs11NO_OPERATION
Indicates that no PKCS #11 operation is underway for given session.
Definition: iot_pkcs11_mbedtls.c:117