corePKCS11  V3.0.0
PKCS #11 Cryptoki Library
C_GenerateRandom
CK_DECLARE_FUNCTION( CK_RV, C_GenerateRandom )( CK_SESSION_HANDLE hSession,
CK_BYTE_PTR RandomData,
CK_ULONG ulRandomLen )
{
CK_RV xResult = CKR_OK;
int32_t lMbedTLSResult = 0;
const P11Session_t * pxSession = prvSessionPointerFromHandle( hSession );
xResult = prvCheckValidSessionAndModule( pxSession );
if( ( NULL == RandomData ) ||
( ulRandomLen == 0UL ) )
{
LogError( ( "Failed to generate random bytes. The buffer to store "
"random numbers in was NULL or the length of the buffer was 0." ) );
xResult = CKR_ARGUMENTS_BAD;
}
if( xResult == CKR_OK )
{
lMbedTLSResult = mbedtls_ctr_drbg_random( &xP11Context.xMbedDrbgCtx, RandomData, ulRandomLen );
if( lMbedTLSResult != 0 )
{
LogError( ( "Failed to generate random bytes. mbed TLS DRBG failed "
"to generate a random number: mbed TLS error = %s : %s.",
mbedtlsHighLevelCodeOrDefault( lMbedTLSResult ),
mbedtlsLowLevelCodeOrDefault( lMbedTLSResult ) ) );
xResult = CKR_FUNCTION_FAILED;
}
else
{
LogDebug( ( "Successfully generated %lu random bytes.", ( unsigned long int ) ulRandomLen ) );
}
}
return xResult;
}
CK_DECLARE_FUNCTION
#define CK_DECLARE_FUNCTION(returnType, name)
Macro for defining a PKCS #11 functions.
Definition: core_pkcs11.h:72