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

AES-CBC #380

Merged
merged 6 commits into from
May 8, 2024
Merged

AES-CBC #380

merged 6 commits into from
May 8, 2024

Conversation

amirhosv
Copy link
Contributor

Description of changes:

Adding support for AES-CBC with no padding and with PKCS7 padding.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@amirhosv amirhosv force-pushed the aes-cbc branch 5 times, most recently from 3b64d33 to 755c978 Compare April 30, 2024 19:17
@amirhosv amirhosv marked this pull request as ready for review April 30, 2024 20:50
@amirhosv amirhosv requested a review from a team as a code owner April 30, 2024 20:50
@geedo0
Copy link
Contributor

geedo0 commented May 1, 2024

I did a first pass, but still need to go through how the buffering works and how we handle the padding/non-padding cases.

I normally don't like to police commit messages and PR descriptions, but this PR could use some more context and explanation around what's going on. This helps with the review process and also helps future developers figure out what happened in the code. This is especially important for large diffs like this. Things that would be helpful to explain:

  • Everything that's going on with the buffers.
  • Why PKCS5 aliases PKCS7.
  • Why we're adding this algorithm or a reference to a ticket.
  • What testing you're doing and why.
  • The refactoring you did around AES GCM and this new CBC class.

The README.md file needs to be updated to include the new algorithm.

Curious on your thoughts why we pulled in the NIST test vectors for this algorithm. ACCP doesn't consistently pull-in NIST vectors and tests for all algorithms. This PR accounts for half the rsp.gz files that we've imported. It's unclear to me what decision criteria is used for when we do vector testing in ACCP in addition to AWS-LC's extensive vector testing.

src/com/amazon/corretto/crypto/provider/AesCbcSpi.java Outdated Show resolved Hide resolved
tst/com/amazon/corretto/crypto/provider/test/TestUtil.java Outdated Show resolved Hide resolved
@ExtendWith(TestResultLogger.class)
@Execution(ExecutionMode.CONCURRENT)
@ResourceLock(value = TestUtil.RESOURCE_GLOBAL, mode = ResourceAccessMode.READ)
public class AesCbcNistTest {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm unsure about whether or not we should be including NIST test vectors in ACCP. This is not something we do for all algorithms in ACCP. More testing is always great, but I think it's a bit redundant with AWS-LC's testing. Our focus should be more on tests that surface issues around interfacing with the native layer and less so around exhaustive validation of the cryptographic implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For other algorithms, I noticed that we have known answer tests (KATs), so I looked for AES/CBC KATs and the NIST website showed up. I'm just trying to treat AES/CBC the same way that other algorithms have been treated.

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand that we have them for other algorithms, but not for //all// algorithms. I'm trying to understand what our decision criteria is for including or not including KATs. It seems pretty random. Why are we exhaustively testing every single KAT style/variant for CBC but not for GCM? It just seems off to me for us to be KAT testing AES-CBC much more thoroughly than literally every other algorithm in ACCP.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess we never formally specified when to add KATs. My rule of thumb is to search online for KATs and if found, then add.

* Added comments to the code and moved some methods around to imporve
  readability.
* Added a test to show that BouncCastle recognizes PKCS7Padding but
  SunJCE does not.
@amirhosv
Copy link
Contributor Author

amirhosv commented May 3, 2024

I did a first pass, but still need to go through how the buffering works and how we handle the padding/non-padding cases.

I normally don't like to police commit messages and PR descriptions, but this PR could use some more context and explanation around what's going on. This helps with the review process and also helps future developers figure out what happened in the code. This is especially important for large diffs like this. Things that would be helpful to explain:

  • Everything that's going on with the buffers.
  • Why PKCS5 aliases PKCS7.
  • Why we're adding this algorithm or a reference to a ticket.
  • What testing you're doing and why.
  • The refactoring you did around AES GCM and this new CBC class.

The README.md file needs to be updated to include the new algorithm.

Curious on your thoughts why we pulled in the NIST test vectors for this algorithm. ACCP doesn't consistently pull-in NIST vectors and tests for all algorithms. This PR accounts for half the rsp.gz files that we've imported. It's unclear to me what decision criteria is used for when we do vector testing in ACCP in addition to AWS-LC's extensive vector testing.

I think I've answered all your comments.

@geedo0
Copy link
Contributor

geedo0 commented May 6, 2024

I think I've answered all your comments.

Yes, I see it all in the code. To clarify though, I was really hoping to see some of these design decisions documented in the PR itself. That helps me a lot when trying to reason out why certain code looks the way it does.

geedo0
geedo0 previously approved these changes May 6, 2024
Copy link
Contributor

@WillChilds-Klein WillChilds-Klein left a comment

Choose a reason for hiding this comment

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

still reviewing, about 1/2 way through.

csrc/aes_cbc.cpp Show resolved Hide resolved
csrc/aes_cbc.cpp Outdated Show resolved Hide resolved
@@ -603,5 +603,30 @@ class JByteArrayCritical {
jbyteArray jarray_;
};

Copy link
Contributor

Choose a reason for hiding this comment

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

suggest some documentation here. what is this class? how does it differ from other buffer types?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry, i'd meant doc comments for the new ShimBuffer, but it's not super important.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No worries. I added comments to ShimByteBuffer as well.

csrc/buffer.cpp Outdated
SimpleBuffer::SimpleBuffer(int size)
: buffer_(nullptr)
{
buffer_ = (uint8_t*)malloc(size);
Copy link
Contributor

Choose a reason for hiding this comment

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

are we sure we want to use libc malloc here? why not use the same allocator as in other primitives?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we only have a SecureBuffer that allocates on stack. The scratch buffer size used when output clobbers input depends on the input/output buffers at runtime, so don't know the size before hand.
I don't think there is any issue in using malloc/free. The only concern could be that we do not zero memory after deallocation. The buffer is used to hold cipherText/plainText. In that sense it's as secure as how Java treats buffers by the garbage collector.
I'm open to suggestion to see if zeroing memory is needed before freeing up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Two things:

  1. The code is updated so that before freeing the memory it is zeroed.
  2. I see that we have SecureAlloc that could potentially be used. The main issue with this class is that it throws std::bad_alloc. In our JNI code, we only catch java_ex, which means any other type of exception could potentially result in undefined behavior in JNI calls.

Copy link
Contributor

Choose a reason for hiding this comment

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

Replacing SecureAlloc with OPENSSL_cleanse makes sense. I wonder if we should extend that idea a little further, using OPENSSL_malloc and OPENSSL_free in SimpleBuffer. This would provide two benefits:

  1. Automatically call OPENSSL_cleanse on free
  2. Automatically use the same underlying allocator as AWS-LC in case they ever decide to switch to e.g. jemalloc or something. due to portability concerns, IMO this is unlikely.

not a blocker at all, just perhaps something to consider.

Copy link
Contributor Author

@amirhosv amirhosv May 8, 2024

Choose a reason for hiding this comment

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

That's a great suggestion. I changed it so that it will use OPENSS_{malloc,free}.

Copy link
Contributor

@WillChilds-Klein WillChilds-Klein left a comment

Choose a reason for hiding this comment

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

LGTM, just two small open comment threads to consider here or in future PRs.

@amirhosv amirhosv merged commit ee2fa55 into corretto:main May 8, 2024
10 checks passed
@amirhosv amirhosv deleted the aes-cbc branch May 8, 2024 18:46
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