Repository navigation
Fix AES-GCM output when OPENSSL_ia32cap masks AES-NI but not VAES #32129
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
Open
robobun
wants to merge
3
commits into
main
Choose a base branch
from
farm/29623eb3/fix-gcm-vaes-dispatch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
patches/boringssl/gcm-vaes-requires-hwaes-key-schedule.patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| CRYPTO_gcm128_init_aes_key selects the stitched VAES AES-GCM implementations | ||
| (gcm_x86_vaes_avx2/avx512) based only on the VPCLMULQDQ/AVX ghash choice and | ||
| CRYPTO_is_VAES_capable(), without checking that the AES key schedule was | ||
| actually produced by aes_hw_set_encrypt_key. When OPENSSL_ia32cap masks | ||
| AES-NI (bit 57, e.g. `~0x200000000000000`) but leaves VAES set, | ||
| aes_ctr_set_key falls back to vpaes, whose key schedule is stored in a | ||
| transformed basis. The VAES bulk assembly then runs against that vpaes-format | ||
| schedule and silently produces wrong ciphertext (and decrypts garbage whose | ||
| tag still verifies, since GHASH and EK0 come from the consistent vpaes block | ||
| function). The aarch64 branches directly below already gate on |is_hwaes|; | ||
| this adds the same gate to the two x86-64 VAES branches. | ||
|
|
||
| Upstream BoringSSL fixed this the same way in | ||
| 997bafbf73886ec19ac3eb70b31db64fe42832c3 ("Gate VAES codepaths on is_hwaes | ||
| to fix a theoretical concern", | ||
| https://boringssl-review.googlesource.com/c/boringssl/+/101787). Delete | ||
| this patch when the pinned BoringSSL commit includes that change. The | ||
| patch application fails loudly at dep-fetch time when that happens. | ||
| See https://github.com/oven-sh/bun/issues/32126 | ||
|
|
||
| --- a/crypto/fipsmodule/aes/gcm.cc.inc | ||
| +++ b/crypto/fipsmodule/aes/gcm.cc.inc | ||
| @@ -276,11 +276,16 @@ void bssl::CRYPTO_gcm128_init_aes_key(GCM128_KEY *gcm_key, const uint8_t *key, | ||
|
|
||
| #if !defined(OPENSSL_NO_ASM) | ||
| #if defined(OPENSSL_X86_64) | ||
| + // The stitched AES-GCM implementations consume |gcm_key->aes| directly and | ||
| + // expect the standard round-key layout produced by |aes_hw_set_encrypt_key|. | ||
| + // Without |is_hwaes|, masking AES-NI but not VAES via OPENSSL_ia32cap made | ||
| + // |aes_ctr_set_key| fall back to vpaes's transformed key schedule while the | ||
| + // VAES bulk code still ran, silently producing wrong ciphertext. | ||
| if (gcm_key->ghash == gcm_ghash_vpclmulqdq_avx512 && | ||
| - CRYPTO_is_VAES_capable()) { | ||
| + CRYPTO_is_VAES_capable() && is_hwaes) { | ||
| gcm_key->impl = gcm_x86_vaes_avx512; | ||
| } else if (gcm_key->ghash == gcm_ghash_vpclmulqdq_avx2 && | ||
| - CRYPTO_is_VAES_capable()) { | ||
| + CRYPTO_is_VAES_capable() && is_hwaes) { | ||
| gcm_key->impl = gcm_x86_vaes_avx2; | ||
| } else if (gcm_key->ghash == gcm_ghash_avx && is_hwaes) { | ||
| gcm_key->impl = gcm_x86_aesni; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.