Skip to content

KMS: add ability to decrypt data with all rotated keys #12482

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

Merged
merged 2 commits into from
Apr 9, 2025

Conversation

sannya-singal
Copy link
Contributor

@sannya-singal sannya-singal commented Apr 4, 2025

Motivation

Add ability to decrypt data that was encrypted before the rotation event by preserving the history of the key material on RotateKeyOnDemand.

Closes: #10723

Changes

Store all the previous crypto keys with a maximum of 10 times per KMS key (checkout the AWS Documentation) to ensure the decryption of the data that was encrypted before the rotation of the key.

Validate the implementation using AWS validated snapshot tests.

@sannya-singal sannya-singal self-assigned this Apr 4, 2025
@sannya-singal sannya-singal added this to the 4.4 milestone Apr 4, 2025
@sannya-singal sannya-singal added semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases aws:kms AWS Key Management Service labels Apr 4, 2025
Copy link

github-actions bot commented Apr 4, 2025

LocalStack Community integration with Pro

  2 files    2 suites   4m 12s ⏱️
671 tests 664 ✅ 7 💤 0 ❌
673 runs  664 ✅ 9 💤 0 ❌

Results for commit cd6f742.

♻️ This comment has been updated with latest results.

@sannya-singal sannya-singal requested review from silv-io and k-a-il April 4, 2025 10:37
@sannya-singal sannya-singal marked this pull request as ready for review April 4, 2025 10:37
@k-a-il
Copy link
Contributor

k-a-il commented Apr 7, 2025

Nice work! It's great to see the GitHub issues being handled :) Could you please explain in the PR description or comments how you determine which key material, of possible 10 values, was used to encrypt the value?

Comment on lines +327 to +333
keys_to_try = [self.crypto_key.key_material] + self.previous_keys

for key in keys_to_try:
try:
return decrypt(key, ciphertext.ciphertext, ciphertext.iv, ciphertext.tag, aad)
except (InvalidTag, InvalidSignature):
continue
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here the data can be decrypted only with the key which encrypted it.
During decryption, we first attempt with the current key material. If decryption fails, we try previous key versions until we find the one that correctly matches the ciphertext.

Copy link
Contributor

@k-a-il k-a-il Apr 8, 2025

Choose a reason for hiding this comment

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

During decryption, we first attempt with the current key material. If decryption fails, we try previous key versions until we find the one that correctly matches the ciphertext.

Ok, but why does decryption actually fail here? When doesInvalidSignature get thrown, and how does it know the key material’s wrong—what value in the code is it checking?

Copy link
Contributor

Choose a reason for hiding this comment

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

What is the difference between InvalidTag and InvalidSignature being thrown in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When data is encrypted using a specific key, it can only be decrypted using that exact key. Attempting to decrypt the data with a different key will result in an error—commonly InvalidSignature or InvalidTag, depending on the algorithm being used.

To handle key rotation gracefully, especially when accessing older data, the decryption process must attempt to use not just the current key but also all previous key materials. This ensures backward compatibility and allows successful decryption of data encrypted before the most recent key rotation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sannya-singal sannya-singal requested a review from k-a-il April 8, 2025 06:23
@sannya-singal sannya-singal merged commit 807b69d into master Apr 9, 2025
33 checks passed
@sannya-singal sannya-singal deleted the rotation-kms branch April 9, 2025 10:43
k-wall added a commit to k-wall/kroxylicious that referenced this pull request May 9, 2025
why: now that localstack/localstack#12482 and localstack/localstack#10723 are done
and we have upgrade to the latest localstack, we can remove the workaround code.
k-wall added a commit to k-wall/kroxylicious that referenced this pull request May 9, 2025
why: now that localstack/localstack#12482 and localstack/localstack#10723 are done
and we have upgrade to the latest localstack, we can remove the workaround code.

Signed-off-by: Keith Wall <kwall@apache.org>
k-wall added a commit to k-wall/kroxylicious that referenced this pull request May 9, 2025
why: now that localstack/localstack#12482 and localstack/localstack#10723 are done
and we have upgrade to the latest localstack, we can remove the workaround code.

Signed-off-by: Keith Wall <kwall@apache.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws:kms AWS Key Management Service semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

enhancement request: RotateKeyOnDemand
2 participants