corePKCS11  v3.2.0
PKCS #11 Cryptoki Library
C_SignInit

Initializes a signature operation.

CK_DECLARE_FUNCTION( CK_RV, C_SignInit )( CK_SESSION_HANDLE hSession,
CK_MECHANISM_PTR pMechanism,
CK_OBJECT_HANDLE hKey )
{
/* See explanation in prvCheckValidSessionAndModule for this exception. */
/* coverity[misra_c_2012_rule_10_5_violation] */
CK_BBOOL xIsPrivate = ( CK_BBOOL ) CK_TRUE;
CK_OBJECT_HANDLE xPalHandle;
CK_BYTE_PTR pxLabel = NULL;
CK_ULONG xLabelLength = 0;
CK_BYTE_PTR pucKeyData = NULL;
CK_ULONG ulKeyDataLength = 0;
P11Session_t * pxSession = prvSessionPointerFromHandle( hSession );
CK_RV xResult = prvCheckValidSessionAndModule( pxSession );
if( NULL == pMechanism )
{
LogError( ( "Failed to initialize sign operation. NULL pointer to "
"signing mechanism provided." ) );
xResult = CKR_ARGUMENTS_BAD;
}
/* See explanation in prvCheckValidSessionAndModule for this exception. */
/* coverity[misra_c_2012_rule_10_5_violation] */
if( ( xResult == CKR_OK ) && ( prvOperationActive( pxSession ) == ( CK_BBOOL ) CK_TRUE ) )
{
LogError( ( "Failed to initialize sign operation. Operation already active." ) );
xResult = CKR_OPERATION_ACTIVE;
}
/* Retrieve key value from storage. */
if( xResult == CKR_OK )
{
&xPalHandle,
&pxLabel,
&xLabelLength );
if( xPalHandle != CK_INVALID_HANDLE )
{
xResult = PKCS11_PAL_GetObjectValue( xPalHandle, &pucKeyData, &ulKeyDataLength, &xIsPrivate );
if( xResult != CKR_OK )
{
LogError( ( "Failed to initialize sign operation. Unable to "
"retrieve value of private key for signing 0x%0lX.", ( unsigned long int ) xResult ) );
xResult = CKR_KEY_HANDLE_INVALID;
}
}
else
{
LogDebug( ( "Could not find PKCS #11 PAL Handle." ) );
xResult = CKR_KEY_HANDLE_INVALID;
}
}
/* Check that a private key was retrieved. */
if( xResult == CKR_OK )
{
/* See explanation in prvCheckValidSessionAndModule for this exception. */
/* coverity[misra_c_2012_rule_10_5_violation] */
if( xIsPrivate != ( CK_BBOOL ) CK_TRUE )
{
LogError( ( "Failed to initialize sign operation. Sign operation "
"attempted with public key." ) );
xResult = CKR_KEY_TYPE_INCONSISTENT;
}
}
/* Convert the private key from storage format to mbedTLS usable format. */
if( xResult == CKR_OK )
{
if( 0 == mbedtls_mutex_lock( &pxSession->xSignMutex ) )
{
switch( pMechanism->mechanism )
{
case CKM_RSA_PKCS:
case CKM_ECDSA:
if( ( pxSession->xSignKeyHandle == CK_INVALID_HANDLE ) || ( pxSession->xSignKeyHandle != hKey ) )
{
xResult = prvSignInitEC_RSAKeys( pxSession, pMechanism, hKey, pucKeyData, ulKeyDataLength );
}
else
{
/* The correct credentials are already initialized. */
}
break;
case CKM_SHA256_HMAC:
if( ( pxSession->xHMACKeyHandle == CK_INVALID_HANDLE ) || ( pxSession->xHMACKeyHandle != hKey ) )
{
xResult = prvSignInitSHA256HMAC( pxSession, hKey, pucKeyData, ulKeyDataLength );
}
else
{
/* The correct credentials are already initialized. */
}
break;
case CKM_AES_CMAC:
if( ( pxSession->xCMACKeyHandle == CK_INVALID_HANDLE ) || ( pxSession->xCMACKeyHandle != hKey ) )
{
xResult = prvSignInitAESCMAC( pxSession, hKey, pucKeyData, ulKeyDataLength );
}
else
{
/* The correct credentials are already initialized. */
}
break;
default:
LogError( ( "Failed to initialize sign operation. Received "
"an unknown or invalid mechanism." ) );
xResult = CKR_MECHANISM_INVALID;
break;
}
( void ) mbedtls_mutex_unlock( &pxSession->xSignMutex );
}
else
{
LogError( ( "Failed sign operation. Could not take sign mutex." ) );
xResult = CKR_CANT_LOCK;
}
}
if( xPalHandle != CK_INVALID_HANDLE )
{
PKCS11_PAL_GetObjectValueCleanup( pucKeyData, ulKeyDataLength );
}
if( xResult == CKR_OK )
{
LogDebug( ( "Sign mechanism set to 0x%0lX.", ( unsigned long int ) pMechanism->mechanism ) );
pxSession->xOperationSignMechanism = pMechanism->mechanism;
}
return xResult;
}
See also
C_Sign() completes signatures initiated by C_SignInit().
Note
C_Sign() parameters are shared by a session. Calling C_SignInit() & C_Sign() with the same session across different tasks may lead to unexpected results.
Parameters
[in]hSessionHandle of a valid PKCS #11 session.
[in]pMechanismMechanism used to sign. This port supports the following mechanisms:
  • CKM_RSA_PKCS for RSA signatures
  • CKM_ECDSA for elliptic curve signatures Note that neither of these mechanisms perform hash operations.
[in]hKeyThe handle of the private key to be used for signature. Key must be compatible with the mechanism chosen by pMechanism.
Returns
CKR_OK if successful.
P11Session_t::xOperationSignMechanism
CK_MECHANISM_TYPE xOperationSignMechanism
Mechanism of the sign operation in progress. Set during C_SignInit.
Definition: core_pkcs11_mbedtls.c:296
prvFindObjectInListByHandle
static void prvFindObjectInListByHandle(CK_OBJECT_HANDLE xAppHandle, CK_OBJECT_HANDLE_PTR pxPalHandle, CK_BYTE_PTR *ppcLabel, CK_ULONG_PTR pxLabelLength)
Looks up a PKCS #11 object's label and PAL handle given an application handle.
Definition: core_pkcs11_mbedtls.c:1070
PKCS11_PAL_GetObjectValueCleanup
void PKCS11_PAL_GetObjectValueCleanup(CK_BYTE_PTR pucData, CK_ULONG ulDataSize)
Cleanup after PKCS11_GetObjectValue().
prvSignInitEC_RSAKeys
static CK_RV prvSignInitEC_RSAKeys(P11Session_t *pxSession, CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey, CK_BYTE_PTR pucKeyData, CK_ULONG ulKeyDataLength)
Helper function for initializing a sign operation for an EC or RSA key.
Definition: core_pkcs11_mbedtls.c:4004
PKCS11_PAL_GetObjectValue
CK_RV PKCS11_PAL_GetObjectValue(CK_OBJECT_HANDLE xHandle, CK_BYTE_PTR *ppucData, CK_ULONG_PTR pulDataSize, CK_BBOOL *pIsPrivate)
Gets the value of an object in storage, by handle.
LogDebug
#define LogDebug(message)
Macro that is called in the corePKCS11 library for logging "Debug" level messages.
Definition: core_pkcs11_config_defaults.h:375
prvSignInitAESCMAC
static CK_RV prvSignInitAESCMAC(P11Session_t *pxSession, CK_OBJECT_HANDLE hKey, CK_BYTE_PTR pucKeyData, CK_ULONG ulKeyDataLength)
Helper function for initializing a sign operation for AES-CMAC.
Definition: core_pkcs11_mbedtls.c:3970
prvOperationActive
static CK_BBOOL prvOperationActive(const P11Session_t *pxSession)
Determines if an operation is in progress.
Definition: core_pkcs11_mbedtls.c:385
P11Session_t::xSignMutex
mbedtls_threading_mutex_t xSignMutex
Protects the signing key from being modified while in use.
Definition: core_pkcs11_mbedtls.c:297
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::xSignKeyHandle
CK_OBJECT_HANDLE xSignKeyHandle
Object handle to the signing key.
Definition: core_pkcs11_mbedtls.c:298
P11Session_t::xHMACKeyHandle
CK_OBJECT_HANDLE xHMACKeyHandle
Object handle to the HMAC key.
Definition: core_pkcs11_mbedtls.c:301
P11Session_t
Session structure.
Definition: core_pkcs11_mbedtls.c:286
LogError
#define LogError(message)
Macro that is called in the corePKCS11 library for logging "Error" level messages.
Definition: core_pkcs11_config_defaults.h:315
C_SignInit
CK_RV C_SignInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey)
Initializes a signature operation.
Definition: core_pkcs11_mbedtls.c:4083
prvSignInitSHA256HMAC
static CK_RV prvSignInitSHA256HMAC(P11Session_t *pxSession, CK_OBJECT_HANDLE hKey, CK_BYTE_PTR pucKeyData, CK_ULONG ulKeyDataLength)
Helper function for initializing a sign operation for SHA256-HMAC.
Definition: core_pkcs11_mbedtls.c:3868
P11Session_t::xCMACKeyHandle
CK_OBJECT_HANDLE xCMACKeyHandle
Object handle to the CMAC key.
Definition: core_pkcs11_mbedtls.c:303