corretto / amazon-corretto-crypto-provider

The Amazon Corretto Crypto Provider is a collection of high-performance cryptographic implementations exposed via standard JCA/JCE interfaces.
Apache License 2.0
236 stars 56 forks source link

Memory allocation is too aggressive for AES/GCM encryption #231

Closed willyborankin closed 2 years ago

willyborankin commented 2 years ago

Hi all, I'm trying to use Amazon Corretto together with OpenSearch to restore an encrypted snapshot and the way how Amazon Corretto allocates memory is IMHO is too aggressive. I have such use case:

To compare (but this is not a good comparison) the same snapshot BoucyCastle restores without additional memory.

SalusaSecondus commented 2 years ago

Will this work with stock Java (not using BouncyCastle)? (I'm guessing that it will not.)

ACCP, like the crypto providers distributed by the OpenJDK, caches all AES-GCM plaintext and does not return any of it prior to completing the decryption. (In other words, it does not stream decryption.) This design decision was made to avoid "Release of Unverified Plaintext", a common security issue in many cryptographic systems.

Looking at the related issue, I see that you are talking about snapshots that are over 300GB. AES-GCM does not scale to encrypt data larger than about 64GB in a single call. Thus, you will need to fragment your files for successful decryption and cannot use AES-GCM directly. I strongly recommend you investigate the AWS Encryption SDK for Java which can encrypt/decrypt extremely large files in a streaming manner. This will:

  1. Let you protect files larger than ~64GB
  2. Let you decrypt in a streaming manner without needing to worry about the Release of Unverified Plaintext
  3. Let you decrypt in a streaming manner and thus avoid the memory overhead of caching the entire result
  4. Integrate with ACCP (for more efficient cryptography)
  5. Do all of the above while abstracting away many other cryptographic complexities and sharp edges.
willyborankin commented 2 years ago

Thank you for the answer now it is clear.