in components/pkcs11_helpers/source/pkcs11_helpers.c [40:103]
BaseType_t xPkcs11GenerateRandomNumber( uint8_t * pusRandomNumBuffer,
size_t xBufferLength )
{
BaseType_t xStatus = pdPASS;
CK_RV xResult = CKR_OK;
CK_FUNCTION_LIST_PTR pxFunctionList = NULL;
CK_SESSION_HANDLE xSession = CK_INVALID_HANDLE;
if( ( pusRandomNumBuffer == NULL ) || ( xBufferLength == 0U ) )
{
LogError( ( "Cannot generate random number. Invalid parameters passed. "
"buffer=%p,bufferLen=%lu", pusRandomNumBuffer, xBufferLength ) );
xStatus = pdFAIL;
}
if( xStatus == pdPASS )
{
/* Get list of functions supported by the PKCS #11 port. */
xResult = C_GetFunctionList( &pxFunctionList );
if( ( xResult != CKR_OK ) || ( pxFunctionList == NULL ) )
{
LogError( ( "Failed to generate random number. "
"PKCS #11 API, C_GetFunctionList, failed." ) );
xStatus = pdFAIL;
}
}
if( xStatus == pdPASS )
{
/* Initialize PKCS #11 module and create a new session. */
xResult = xInitializePkcs11Session( &xSession );
if( ( xResult != CKR_OK ) || ( xSession == CK_INVALID_HANDLE ) )
{
LogError( ( "Failed to generate random number. "
"Failed to initialize PKCS #11 session." ) );
xStatus = pdFAIL;
}
}
if( xStatus == pdPASS )
{
if( pxFunctionList->C_GenerateRandom( xSession,
pusRandomNumBuffer,
xBufferLength ) != CKR_OK )
{
xStatus = pdFAIL;
LogError( ( "Failed to generate random number. "
"PKCS #11 API, C_GenerateRandom, failed to generate random number." ) );
}
}
if( xStatus == pdPASS )
{
if( pxFunctionList->C_CloseSession( xSession ) != CKR_OK )
{
xStatus = pdFAIL;
LogError( ( " Failed to close PKCS #11 session after generating random number." ) );
}
}
return xStatus;
}