Skip to content

Calculate output size ourselves for AES/GCM and ChaCha20. - #438

Merged
flooey merged 1 commit into
google:masterfrom
flooey:getoutputsize
Mar 13, 2018
Merged

flooey merged 1 commit into
google:masterfrom
flooey:getoutputsize

Conversation

@flooey

@flooey flooey commented Mar 12, 2018

Copy link
Copy Markdown
Contributor

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.

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.
@flooey
flooey requested a review from nmittler March 12, 2018 16:04
@flooey
flooey merged commit 1402f8a into google:master Mar 13, 2018
@flooey
flooey deleted the getoutputsize branch March 13, 2018 10:35
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)
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.

2 participants