in source/azure_iot.c [117:188]
AzureIoTResult_t AzureIoT_Base64HMACCalculate( AzureIoTGetHMACFunc_t xAzureIoTHMACFunction,
const uint8_t * pucKey,
uint32_t ulKeySize,
const uint8_t * pucMessage,
uint32_t ulMessageSize,
uint8_t * pucBuffer,
uint32_t ulBufferLength,
uint8_t * pucOutput,
uint32_t ulOutputSize,
uint32_t * pulOutputLength )
{
az_result xCoreResult;
uint8_t * pucHashBuf;
uint8_t * pucDecodedKeyBuf = pucBuffer;
uint32_t ulHashBufSize = azureiotBASE64_HASH_BUFFER_SIZE;
int32_t lDecodedKeyLength;
int32_t lEncodedLength;
az_span xEncodedKeySpan;
az_span xOutputDecodedKeySpan;
az_span xHashSpan;
az_span xOutputEncodedHashSpan;
if( ( xAzureIoTHMACFunction == NULL ) ||
( pucKey == NULL ) || ( ulKeySize == 0 ) ||
( pucMessage == NULL ) || ( ulMessageSize == 0 ) ||
( pucBuffer == NULL ) || ( ulBufferLength == 0 ) ||
( pucOutput == NULL ) || ( pulOutputLength == NULL ) )
{
AZLogError( ( "AzureIoT_Base64HMACCalculate failed: Invalid argument" ) );
return eAzureIoTErrorInvalidArgument;
}
xEncodedKeySpan = az_span_create( ( uint8_t * ) pucKey, ( int32_t ) ulKeySize );
xOutputDecodedKeySpan = az_span_create( ( uint8_t * ) pucDecodedKeyBuf, ( int32_t ) ulBufferLength );
if( az_result_failed( xCoreResult = az_base64_decode( xOutputDecodedKeySpan, xEncodedKeySpan, &lDecodedKeyLength ) ) )
{
AZLogError( ( "az_base64_decode failed: core error=0x%08x", ( uint16_t ) xCoreResult ) );
return eAzureIoTErrorFailed;
}
/* Decoded key is less than total decoded buffer size */
ulBufferLength -= ( uint32_t ) lDecodedKeyLength;
if( ulHashBufSize > ulBufferLength )
{
return eAzureIoTErrorOutOfMemory;
}
pucHashBuf = pucDecodedKeyBuf + lDecodedKeyLength;
memset( pucHashBuf, 0, ulHashBufSize );
if( xAzureIoTHMACFunction( pucDecodedKeyBuf, ( uint32_t ) lDecodedKeyLength,
pucMessage, ( uint32_t ) ulMessageSize,
pucHashBuf, ulHashBufSize, &ulHashBufSize ) )
{
return eAzureIoTErrorFailed;
}
xHashSpan = az_span_create( pucHashBuf, ( int32_t ) ulHashBufSize );
xOutputEncodedHashSpan = az_span_create( pucOutput, ( int32_t ) ulOutputSize );
if( az_result_failed( xCoreResult = az_base64_encode( xOutputEncodedHashSpan, xHashSpan, &lEncodedLength ) ) )
{
AZLogError( ( "az_base64_decode failed: core error=0x%08x", ( uint16_t ) xCoreResult ) );
return eAzureIoTErrorFailed;
}
*pulOutputLength = ( uint32_t ) lEncodedLength;
return eAzureIoTSuccess;
}