Repository navigation
Calculate output size ourselves for AES/GCM and ChaCha20. - #438
Merged
Merged
Conversation
Since both AES/GCM and ChaCha20 don't have any variance in the length of their AEAD tags, we can just calculate the output length directly rather than making a JNI call. This gives better bounds for the necessary buffer size; in particular, we previously weren't factoring in the removal of the tag during decryption, so we always demanded an unnecessarily-long buffer.
nmittler
approved these changes
Mar 12, 2018
This was referenced Nov 24, 2026
Closed
intergalacticspacehighway
pushed a commit
to expo/expo
that referenced
this pull request
Nov 26, 2026
## Why Fixes #54150. On a Samsung SM-P583 running Android 8.1, the released versions of `expo-crypto@58.0.7` and `58.0.8` both returned 74 bytes from `aesDecryptAsync()` for a 58-byte JSON document. The 16 extra zero bytes made the document invalid when the app restored its session. [Downstream report](huamurui/zhihu-minus-minus#162) The app restores sessions normally on the reporter's Android 16 device. Conscrypt changed its GCM decryption buffer estimate between the Android 9 and 10 releases. The older provider counted the tag in its estimate. [Conscrypt #438](google/conscrypt#438) · [Android 9 source](https://android.googlesource.com/platform/external/conscrypt/+/android-9.0.0_r1/common/src/main/java/org/conscrypt/OpenSSLCipher.java#1255) · [Android 10 source](https://android.googlesource.com/platform/external/conscrypt/+/android-10.0.0_r1/common/src/main/java/org/conscrypt/OpenSSLCipher.java#1404) Android allows `Cipher.getOutputSize()` to return an upper bound. Expo returned the entire backing array to JavaScript, including bytes that `doFinal()` never wrote. [Android Cipher reference](https://developer.android.com/reference/javax/crypto/Cipher#getOutputSize(int)) ## How Return the Android plaintext as a `ByteArray` sized to the byte count from `doFinal()`. Return the allocated array directly if the provider fills it. Copy the written range if the provider reserves extra space. This handles providers that overestimate the output size and preserves zero bytes that belong to the plaintext. OpenAI Codex helped prepare the patch, regression tests and test report. ## Test Plan - Ran `CipherExtensionsTest` with JUnit 4.13.2 on JDK 17. The test cipher delegates AES-GCM to the JDK and reports 16 additional bytes of output capacity. On the original implementation, the assertions read the backing array as the module did. Both assertions failed. A 0-byte input returned 16 bytes and a 4-byte input returned 20 bytes. Removing the output trim from the `ByteArray` implementation produced the same failures with the final test source. Both tests passed byte-for-byte after the fix, including the case with two trailing zeros. The AES module compiled in both runs. - Built a standalone Expo app using the published Android library, and then rebuilt it with the patched Kotlin source. Ran the same three round trips on the SM-P583 for 58.0.7 and 58.0.8. Both original builds returned lengths of 74, 16 and 21 for inputs of 58, 0 and 5 bytes. Both patched builds returned exactly the input bytes, including two legitimate trailing zeros. Expo Doctor passed all 20 checks for the current SDK 58 reproduction. [Standalone reproduction](https://github.com/huamurui/zhihu-minus-minus/tree/codex/repro-expo-crypto-android8) - Ran the same Java `Cipher.doFinal(ByteBuffer, ByteBuffer)` probe on official Android 9 (API 28), 10 (API 29), and 16 (API 36.1) emulator images using their default AndroidOpenSSL provider. In each case the actual bytes written matched the input lengths of 58, 0 and 5. Android 9 reserved 74, 16 and 21 bytes. Android 10 and 16 reserved exactly 58, 0 and 5 bytes. - A native probe on the SM-P583 (API 27, AndroidOpenSSL) returned 74, 16 and 21 bytes with the original implementation for synthetic inputs of 58, 0 and 5 bytes. With the same length fix it returned the original bytes for all three, including the five-byte input ending in two zeros. Modified ciphertext failed authentication in all three cases. - Compiled the upstream Kotlin function before and after the change and ran it on JDK 17. A test provider delegated AES-GCM to SunJCE and reported an extra 16 bytes of output capacity. Before the fix, empty input and `[1, 2, 0, 0]` decrypted to 16 and 20 bytes. Afterward, both matched their input byte for byte. The ordinary SunJCE path also passed. Both providers rejected modified ciphertext. - Added a case in the `CryptoAES` test suite for those two plaintexts. Ran its assertions in a WebCrypto harness with exact and oversized results. The assertions caught the extra bytes. To repeat the native check, run `CryptoAES` in `apps/bare-expo` on Android API 27. - Ran `et check-packages expo-crypto`. All checks passed. ## Screenshots No UI change. ## Checklist - [x] Added a patch changeset for `expo-crypto`.
expo-bot
pushed a commit
to expo/expo
that referenced
this pull request
Nov 26, 2026
## Why Fixes #54150. On a Samsung SM-P583 running Android 8.1, the released versions of `expo-crypto@58.0.7` and `58.0.8` both returned 74 bytes from `aesDecryptAsync()` for a 58-byte JSON document. The 16 extra zero bytes made the document invalid when the app restored its session. [Downstream report](huamurui/zhihu-minus-minus#162) The app restores sessions normally on the reporter's Android 16 device. Conscrypt changed its GCM decryption buffer estimate between the Android 9 and 10 releases. The older provider counted the tag in its estimate. [Conscrypt #438](google/conscrypt#438) · [Android 9 source](https://android.googlesource.com/platform/external/conscrypt/+/android-9.0.0_r1/common/src/main/java/org/conscrypt/OpenSSLCipher.java#1255) · [Android 10 source](https://android.googlesource.com/platform/external/conscrypt/+/android-10.0.0_r1/common/src/main/java/org/conscrypt/OpenSSLCipher.java#1404) Android allows `Cipher.getOutputSize()` to return an upper bound. Expo returned the entire backing array to JavaScript, including bytes that `doFinal()` never wrote. [Android Cipher reference](https://developer.android.com/reference/javax/crypto/Cipher#getOutputSize(int)) ## How Return the Android plaintext as a `ByteArray` sized to the byte count from `doFinal()`. Return the allocated array directly if the provider fills it. Copy the written range if the provider reserves extra space. This handles providers that overestimate the output size and preserves zero bytes that belong to the plaintext. OpenAI Codex helped prepare the patch, regression tests and test report. ## Test Plan - Ran `CipherExtensionsTest` with JUnit 4.13.2 on JDK 17. The test cipher delegates AES-GCM to the JDK and reports 16 additional bytes of output capacity. On the original implementation, the assertions read the backing array as the module did. Both assertions failed. A 0-byte input returned 16 bytes and a 4-byte input returned 20 bytes. Removing the output trim from the `ByteArray` implementation produced the same failures with the final test source. Both tests passed byte-for-byte after the fix, including the case with two trailing zeros. The AES module compiled in both runs. - Built a standalone Expo app using the published Android library, and then rebuilt it with the patched Kotlin source. Ran the same three round trips on the SM-P583 for 58.0.7 and 58.0.8. Both original builds returned lengths of 74, 16 and 21 for inputs of 58, 0 and 5 bytes. Both patched builds returned exactly the input bytes, including two legitimate trailing zeros. Expo Doctor passed all 20 checks for the current SDK 58 reproduction. [Standalone reproduction](https://github.com/huamurui/zhihu-minus-minus/tree/codex/repro-expo-crypto-android8) - Ran the same Java `Cipher.doFinal(ByteBuffer, ByteBuffer)` probe on official Android 9 (API 28), 10 (API 29), and 16 (API 36.1) emulator images using their default AndroidOpenSSL provider. In each case the actual bytes written matched the input lengths of 58, 0 and 5. Android 9 reserved 74, 16 and 21 bytes. Android 10 and 16 reserved exactly 58, 0 and 5 bytes. - A native probe on the SM-P583 (API 27, AndroidOpenSSL) returned 74, 16 and 21 bytes with the original implementation for synthetic inputs of 58, 0 and 5 bytes. With the same length fix it returned the original bytes for all three, including the five-byte input ending in two zeros. Modified ciphertext failed authentication in all three cases. - Compiled the upstream Kotlin function before and after the change and ran it on JDK 17. A test provider delegated AES-GCM to SunJCE and reported an extra 16 bytes of output capacity. Before the fix, empty input and `[1, 2, 0, 0]` decrypted to 16 and 20 bytes. Afterward, both matched their input byte for byte. The ordinary SunJCE path also passed. Both providers rejected modified ciphertext. - Added a case in the `CryptoAES` test suite for those two plaintexts. Ran its assertions in a WebCrypto harness with exact and oversized results. The assertions caught the extra bytes. To repeat the native check, run `CryptoAES` in `apps/bare-expo` on Android API 27. - Ran `et check-packages expo-crypto`. All checks passed. ## Screenshots No UI change. ## Checklist - [x] Added a patch changeset for `expo-crypto`. (cherry picked from commit 5d4d271)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Since both AES/GCM and ChaCha20 don't have any variance in the length
of their AEAD tags, we can just calculate the output length directly
rather than making a JNI call. This gives better bounds for the
necessary buffer size; in particular, we previously weren't factoring
in the removal of the tag during decryption, so we always demanded an
unnecessarily-long buffer.