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

feat: add AEAD seal_in_place_scatter method #206

Merged
merged 5 commits into from
Aug 28, 2023
Merged

Conversation

camshaft
Copy link
Collaborator

@camshaft camshaft commented Jul 28, 2023

Description of changes:

The current AEAD encrypt API takes a in_out: &mut [u8], which forces callers to copy all of the cleartext into a single slice. This shouldn't be needed since encrypting a payload is copying the data since it has to XOR it with the cipher stream.

Luckily, AWS-LC already provides a EVP API to account for this: EVP_AEAD_CTX_seal_scatter. This allows the caller to pass a extra_in, which is encrypted into the tag_out buffer.

In transport protocols like TLS and QUIC, where the bulk of the data being encrypted is separate from the framing header, using this API can yield large wins. In the case of s2n-quic, we see a reduction in 8% of CPU by avoiding the copy.

Call-outs:

API ergonomics

This new API isn't the most straightforward interface to use. You have to make sure to split the slices up in the correct order and account for the tag size. If we want to limit its use, we can add a feature flag stating that it's somewhat advanced.

API naming

I'm not sure if this is the best name, but it's what I came up with. Let me know if there's something better.

Testing:

I've updated the AEAD tests to check the new API matches the existing ones. I'm using part of the key bytes to pick an arbitrary split point in the input for the extra_in parameter. This will make the tests deterministic for a given key but still be somewhat "random" in how the in_out is split from the extra_in.

s2n-quic without this change

Throughput is ~20Gbps. You can see the memcpy taking about 8% of CPU cycles in the flamegraph:

flamegraph

s2n-quic with this change

Throughput is ~21Gbps. You can see in the flamegraph that the memcpy is gone:

flamegraph

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.

@camshaft camshaft marked this pull request as ready for review July 28, 2023 21:04
@camshaft camshaft requested a review from a team as a code owner July 28, 2023 21:04
@justsmth justsmth requested review from skmcgrail and justsmth August 1, 2023 15:24
if 1 != EVP_AEAD_CTX_seal_scatter(
aead_ctx,
in_out.as_mut_ptr(),
extra_out_and_tag.as_mut_ptr().cast(),
Copy link
Contributor

Choose a reason for hiding this comment

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

NP: The cast() here is not needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes sense

aws-lc-rs/src/aead.rs Outdated Show resolved Hide resolved
justsmth
justsmth previously approved these changes Aug 4, 2023
///
#[inline]
#[allow(clippy::needless_pass_by_value)]
pub fn seal_in_place_scatter<A>(
Copy link
Member

Choose a reason for hiding this comment

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

Had an offline conversation with Justin, so will summarize it here so that we can get this closed out. Let's remove SealingKey::seal_in_place_scatter, and only have support for this feature on LessSafeKey. With that small modification we are open to accepting this feature.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Works for me!

justsmth
justsmth previously approved these changes Aug 24, 2023
@justsmth justsmth merged commit 865a634 into aws:main Aug 28, 2023
@camshaft camshaft deleted the seal-scatter branch August 28, 2023 19:00
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.

3 participants