Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove superfluous memmove #59

Merged
merged 4 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lexicon.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const
constness
copydoc
copyheaderstringtocanonicalbuffer
credentialscope
crypto
cryptointerface
css
Expand Down Expand Up @@ -61,6 +62,7 @@ gr
hashblocklen
hashcontext
hashdigestlen
hashedcanonicalrequest
hashfinal
hashinit
hashpayloadlen
Expand Down Expand Up @@ -206,6 +208,7 @@ quicksort
rande
readloc
regionlen
requestdatetime
rfc
sdk
sec
Expand Down
63 changes: 34 additions & 29 deletions source/sigv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -2416,6 +2416,7 @@ static SigV4Status_t completeHashAndHexEncode( const char * pInput,

if( returnStatus == SigV4Success )
{
assert( hexEncodedHash.dataLen == pCryptoInterface->hashDigestLen * 2U );
*pOutputLen = hexEncodedHash.dataLen;
}

Expand Down Expand Up @@ -2713,50 +2714,54 @@ static SigV4Status_t writeStringToSign( const SigV4Parameters_t * pParams,
CanonicalContext_t * pCanonicalContext )
{
SigV4Status_t returnStatus = SigV4Success;
size_t encodedLen = pCanonicalContext->bufRemaining;
char * pBufStart = ( char * ) pCanonicalContext->pBufProcessing;
ptrdiff_t bufferLen = pCanonicalContext->pBufCur - pBufStart;
/* An overestimate but sufficient memory is checked before proceeding. */
size_t encodedLen = SIGV4_PROCESSING_BUFFER_LENGTH;

/* The string to sign is composed of (+ means string concatenation):
* Algorithm + \n +
* RequestDateTime + \n +
* CredentialScope + \n +
* HashedCanonicalRequest
*
* The processing buffer is verified beforehand that it has enough
* space to hold this string. */
size_t sizeNeededBeforeHash = algorithmLen + 1U + \
yourslab marked this conversation as resolved.
Show resolved Hide resolved
SIGV4_ISO_STRING_LEN + 1U;

assert( pParams != NULL );
assert( ( pAlgorithm != NULL ) && ( algorithmLen > 0 ) );
assert( pCanonicalContext != NULL );

returnStatus = completeHashAndHexEncode( pBufStart,
( size_t ) bufferLen,
pCanonicalContext->pBufCur + 1,
&encodedLen,
pParams->pCryptoInterface );
sizeNeededBeforeHash += sizeNeededForCredentialScope( pParams ) + 1U;

if( returnStatus == SigV4Success )
/* Check if there is enough space for the string to sign. */
if( ( sizeNeededBeforeHash + ( pParams->pCryptoInterface->hashDigestLen * 2U ) ) >
SIGV4_PROCESSING_BUFFER_LENGTH )
{
size_t sizeNeededBeforeHash = algorithmLen + 1U + \
SIGV4_ISO_STRING_LEN + 1U + \
sizeNeededForCredentialScope( pParams ) + 1U;

/* Check if there is enough space for the string to sign. */
if( ( sizeNeededBeforeHash + ( pParams->pCryptoInterface->hashDigestLen * 2U ) ) >
SIGV4_PROCESSING_BUFFER_LENGTH )
{
returnStatus = SigV4InsufficientMemory;
LOG_INSUFFICIENT_MEMORY_ERROR( "for string to sign",
sizeNeededBeforeHash + ( pParams->pCryptoInterface->hashDigestLen * 2U ) - SIGV4_PROCESSING_BUFFER_LENGTH );
}
else
{
/* Copy the hash of the canonical request beforehand to its precalculated location
* in the string to sign. */
( void ) memmove( pBufStart + sizeNeededBeforeHash,
pCanonicalContext->pBufCur + 1,
encodedLen );
pCanonicalContext->pBufCur = pBufStart + sizeNeededBeforeHash + encodedLen;
pCanonicalContext->bufRemaining = SIGV4_PROCESSING_BUFFER_LENGTH - encodedLen - sizeNeededBeforeHash;
}
returnStatus = SigV4InsufficientMemory;
LOG_INSUFFICIENT_MEMORY_ERROR( "for string to sign",
sizeNeededBeforeHash + ( pParams->pCryptoInterface->hashDigestLen * 2U ) - SIGV4_PROCESSING_BUFFER_LENGTH );
}
else
{
/* Hash the canonical request to its precalculated location in the string to sign. */
returnStatus = completeHashAndHexEncode( pBufStart,
( size_t ) bufferLen,
pBufStart + sizeNeededBeforeHash,
&encodedLen,
pParams->pCryptoInterface );
}

if( returnStatus == SigV4Success )
{
size_t bytesWritten = 0U;
SigV4String_t credentialScope;

pCanonicalContext->pBufCur = pBufStart + sizeNeededBeforeHash + encodedLen;
pCanonicalContext->bufRemaining = SIGV4_PROCESSING_BUFFER_LENGTH - encodedLen - sizeNeededBeforeHash;

bytesWritten = writeStringToSignPrefix( pBufStart,
pAlgorithm,
algorithmLen,
Expand Down