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

[SigV4]Payload optimization #51

Merged
merged 31 commits into from
Aug 23, 2021
Merged

[SigV4]Payload optimization #51

merged 31 commits into from
Aug 23, 2021

Conversation

gshvang
Copy link
Contributor

@gshvang gshvang commented Aug 17, 2021

This PR contains changes related to optimizing the sigV4 library for the already hashed payload.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@gshvang gshvang marked this pull request as ready for review August 18, 2021 00:08
source/include/sigv4.h Outdated Show resolved Hide resolved
{
/* Copy the hashed payload data supplied by the user in the headers data list. */
returnStatus = copyHeaderStringToCanonicalBuffer( pCanonicalContext->pHashPayloadLoc, pCanonicalContext->hashPayloadLen, pParams->pHttpParameters->flags, '\n', pCanonicalContext );
pCanonicalContext->pBufCur--;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the decrement for?

Copy link
Contributor Author

@gshvang gshvang Aug 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need new line at the end of hash payload in the canonical request but the above function adds new line also so decrementing it.

<td><center>4.9K</center></td>
<td><center>4.2K</center></td>
<td><center>5.1K</center></td>
<td><center>4.3K</center></td>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the user likely to have already hashed the payload and encoded it in lowercase hex? Is the extra memory usage worth the speedup gained from not having to hash the payload?

Copy link
Contributor Author

@gshvang gshvang Aug 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, In almost all cases the payload will be hashed and hex encoded by the application. So I think it is worth. Also if there is x-amz-content-sha256 header is there in the HTTP headers passed to the library, then the payload is definitely hashed. And almost all AWS requests include this header.

The x-amz-content-sha256 header is required for all AWS Signature Version 4 requests. It provides a hash of the request payload. If there is no payload, you must provide the hash of an empty string.

This is mentioned in this AWS doc: https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But is that header something the application is expected to have been added already, or would a user expect the library to add the header for them? If it's the latter case, then the user can save themself time by just telling the library to add it for them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The application will add that header before calling the Sigv4 API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it's not almost all cases then, it's every case

@gshvang gshvang requested a review from muneebahmed10 August 18, 2021 01:04
source/sigv4.c Outdated Show resolved Hide resolved
@@ -76,7 +76,7 @@
* <b>Default value:</b> `1024`
*/
#ifndef SIGV4_PROCESSING_BUFFER_LENGTH
#define SIGV4_PROCESSING_BUFFER_LENGTH 350
#define SIGV4_PROCESSING_BUFFER_LENGTH 428
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems very arbitrary. Why 428?

Copy link
Contributor Author

@gshvang gshvang Aug 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the buffer for the new header X_AMZ_CONTENT_SHA256 as now the canonical headers and signed headers will include the header too.

test/unit-test/sigv4_utest.c Outdated Show resolved Hide resolved
source/sigv4.c Outdated Show resolved Hide resolved
source/sigv4.c Outdated Show resolved Hide resolved
Co-authored-by: Oscar Michael Abrina <[email protected]>
assert( canonicalRequest != NULL );

const uint8_t * pHeaderData = ( const uint8_t * ) canonicalRequest->pHeadersLoc[ headerIndex ].key.pData;
const uint8_t * pHeaderLiteral = ( const uint8_t * ) pAmzSHA256Header;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need an intermediate pHeaderLiteral variable here? We can just use pAmzSHA256Header and cast directly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or better yet, just use the macro directly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we do. MISRA reasons

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done to avoid MISRA violation(doing the cast within the function call still triggers 21.16.)

Co-authored-by: Oscar Michael Abrina <[email protected]>
source/sigv4.c Outdated Show resolved Hide resolved
@yourslab
Copy link
Contributor

The CBMC proofs are still failing. I suggest running them locally.

@gshvang
Copy link
Contributor Author

gshvang commented Aug 20, 2021

The CBMC proofs are still failing. I suggest running them locally.

Yeah, I am investigating that and will raise a separate PR for it.

@gshvang gshvang merged commit b1f4eb7 into aws:main Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants