Signs single-part data.
CK_BYTE_PTR pData,
CK_ULONG ulDataLen,
CK_BYTE_PTR pSignature,
CK_ULONG_PTR pulSignatureLen )
{
CK_ULONG xSignatureLength = 0;
size_t xExpectedInputLength = 0;
CK_BYTE_PTR pxSignatureBuffer = pSignature;
CK_BBOOL xSignatureGenerated = ( CK_BBOOL ) CK_FALSE;
int32_t lMbedTLSResult;
if( ( NULL == pulSignatureLen ) || ( NULL == pData ) )
{
xResult = CKR_ARGUMENTS_BAD;
}
if( CKR_OK == xResult )
{
{
}
{
pxSignatureBuffer = ecSignature;
}
else
{
xResult = CKR_OPERATION_NOT_INITIALIZED;
}
}
if( xResult == CKR_OK )
{
if( NULL != pSignature )
{
if( *pulSignatureLen < xSignatureLength )
{
xResult = CKR_BUFFER_TOO_SMALL;
}
if( CKR_OK == xResult )
{
if( xExpectedInputLength != ulDataLen )
{
xResult = CKR_DATA_LEN_RANGE;
}
}
if( CKR_OK == xResult )
{
if( pdTRUE == xSemaphoreTake( pxSessionObj->
xSignMutex, portMAX_DELAY ) )
{
lMbedTLSResult = mbedtls_pk_sign( &pxSessionObj->
xSignKey,
MBEDTLS_MD_NONE,
pData,
ulDataLen,
pxSignatureBuffer,
&xExpectedInputLength,
mbedtls_ctr_drbg_random,
if( lMbedTLSResult != 0 )
{
xResult = CKR_FUNCTION_FAILED;
}
( void ) xSemaphoreGive( pxSessionObj->
xSignMutex );
xSignatureGenerated = ( CK_BBOOL ) CK_TRUE;
}
else
{
xResult = CKR_CANT_LOCK;
}
}
}
}
if( xResult == CKR_OK )
{
{
if( lMbedTLSResult != 0 )
{
xResult = CKR_FUNCTION_FAILED;
}
}
}
if( ( xResult == CKR_OK ) || ( xResult == CKR_BUFFER_TOO_SMALL ) )
{
*pulSignatureLen = xSignatureLength;
}
if( ( xResult != CKR_BUFFER_TOO_SMALL ) && ( xResult != CKR_SESSION_HANDLE_INVALID ) )
{
}
return xResult;
}
- See also
- C_SignInit() initiates signatures signature creation.
- 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] | hSession | Handle of a valid PKCS #11 session. |
[in] | pData | Data to be signed. Note: Some applications may require this data to be hashed before passing to C_Sign(). |
[in] | ulDataLen | Length of pucData, in bytes. |
[out] | pSignature | Buffer where signature will be placed. Caller is responsible for allocating memory. Providing NULL for this input will cause pulSignatureLen to be updated for length of buffer required. |
[in,out] | pulSignatureLen | Length of pucSignature buffer. If pucSignature is non-NULL, pulSignatureLen is updated to contain the actual signature length. If pucSignature is NULL, pulSignatureLen is updated to the buffer length required for signature data. |
- Returns
- CKR_OK if successful. Else, see PKCS #11 specification for more information.
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: iot_pkcs11.h:66
#define pkcs11RSA_SIGNATURE_INPUT_LENGTH
Length of RSA signature data before padding.
Definition: iot_pkcs11.h:119
mbedtls_pk_context xSignKey
Signing key. Set during C_SignInit.
Definition: iot_pkcs11_mbedtls.c:228
#define pkcs11SHA256_DIGEST_LENGTH
Length of a SHA256 digest, in bytes.
Definition: iot_pkcs11.h:83
BaseType_t PKI_mbedTLSSignatureToPkcs11Signature(uint8_t *pxSignaturePKCS, const uint8_t *pxMbedSignature)
Converts an ECDSA P-256 signature from the format provided by mbedTLS to the format expected by PKCS ...
CK_MECHANISM_TYPE xOperationSignMechanism
Mechanism of the sign operation in progress. Set during C_SignInit.
Definition: iot_pkcs11_mbedtls.c:226
#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
#define PKCS11_PRINT(X)
Macro for logging in PKCS #11.
Definition: iot_pkcs11_mbedtls.c:103
#define pkcs11RSA_2048_SIGNATURE_LENGTH
Length of PKCS #11 signature for RSA 2048 key, in bytes.
Definition: iot_pkcs11.h:111
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
CK_RV C_Sign(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen)
Signs single-part data.
Definition: iot_pkcs11_mbedtls.c:3535
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
mbedtls_ctr_drbg_context xMbedDrbgCtx
CTR-DRBG context for PKCS #11 module - used to generate pseudo-random numbers.
Definition: iot_pkcs11_mbedtls.c:203
#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
Session structure.
Definition: iot_pkcs11_mbedtls.c:217
SemaphoreHandle_t xSignMutex
Protects the signing key from being modified while in use.
Definition: iot_pkcs11_mbedtls.c:227
#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
#define pkcs11NO_OPERATION
Indicates that no PKCS #11 operation is underway for given session.
Definition: iot_pkcs11_mbedtls.c:117
static P11Struct_t xP11Context
The global PKCS #11 module object. Entropy/randomness and object lists are shared across PKCS #11 ses...
Definition: iot_pkcs11_mbedtls.c:238