Skip to content

[Android] AesGcm.Encrypt/Decrypt with an empty payload crashes with SIGABRT in AndroidCryptoNative_CipherUpdate #135333

Description

@ilwren

Description

On Android, AesGcm.Encrypt with an empty plaintext (or Decrypt with an empty ciphertext) kills the process with SIGABRT. The abort happens inside the native crypto PAL, so there's no managed exception and try/catch doesn't help.

Encrypting an empty payload is a normal GCM operation — you get a tag over the AAD — and it works fine on Windows and Linux. Only Android crashes.

We hit this with an encrypted-configuration library: every first launch on Android died, because creating a new store encrypts an empty initial payload, and all there was to show for it was a tombstone.

Reproduction Steps

The crash below was observed with a net10.0-android app on an API 29 x86_64 emulator. The snippet is the minimal form of the failing call — it's what "encrypt the initial (empty) payload" reduces to in our library:

using System.Security.Cryptography;

var key = RandomNumberGenerator.GetBytes(32);
var nonce = RandomNumberGenerator.GetBytes(12);
var tag = new byte[16];

using var aes = new AesGcm(key, 16);
aes.Encrypt(nonce, ReadOnlySpan<byte>.Empty, Span<byte>.Empty, tag);

The decrypt direction crashes the same way:

aes.Decrypt(nonce, ReadOnlySpan<byte>.Empty, tag, Span<byte>.Empty);

Expected behavior

Same as on Windows and Linux: a tag computed over the AAD, no plaintext output, no crash.

Actual behavior

The process aborts. From adb logcat:

signal 6 (SIGABRT), code -1 (SI_QUEUE)
Abort message: '/__w/1/s/src/runtime/src/native/libs/System.Security.Cryptography.Native.Android/pal_cipher.c:258
(AndroidCryptoNative_CipherUpdate): Parameter 'in' must be a valid pointer'

backtrace:
  #02 .../lib/x86_64/libSystem.Security.Cryptography.Native.Android.so (+0x1028a)
  #03 .../lib/x86_64/libSystem.Security.Cryptography.Native.Android.so (AndroidCryptoNative_CipherUpdate+157)
  #04 .../lib/x86_64/libmonosgen-2.0.so
  ...

The crash was on the .NET 10 runtime, and pal_cipher.c:258 is exactly where release/10.0 has the abort_if_invalid_pointer_argument(in) check in AndroidCryptoNative_CipherUpdate. The same check sits at the same line on 7.0 through 9.0, so this isn't something new in 10.

Regression?

I don't think this ever worked. I traced the source back to release/6.0 (when the Android shims still lived in System.Security.Cryptography.Algorithms) and the same pattern is there; it's still there on current main. As far as I can tell every version with Android AEAD support is affected.

Known Workarounds

Don't hand an empty payload to AesGcm on Android. In our library we skip the call when the payload is empty and compute/verify the tag ourselves. Padding the payload so it's never empty would also work.

Configuration

  • .NET 10 — the app was on net10.0 when it crashed; 6.0 through 10.0 and main checked in source, the pattern is identical everywhere
  • Android 10 (API 29) x86_64 emulator
  • I don't think it's specific to the OS version or arch — the abort is an unconditional pointer check in the PAL
  • Windows and Linux are fine, which is probably why this never showed up in CI: the OpenSSL PAL accepts NULL with length 0

Other information

I think I know where this comes from.

The Android shim guards the AAD but not the payload. From AesGcm.Android.cs on release/8.0:

if (associatedData.Length != 0)                       // AAD: guarded
{
    Interop.Crypto.CipherUpdateAAD(_ctxHandle, associatedData);
}
...
if (!Interop.Crypto.EvpCipherUpdate(_ctxHandle, ciphertextAndTag, out int n, plaintext))   // payload: not guarded

A zero-length span marshals as NULL, and the PAL aborts on it:

// pal_cipher.c, AndroidCryptoNative_CipherUpdate
if (!outl && !in)
    return SUCCESS;

abort_if_invalid_pointer_argument(outl);
abort_if_invalid_pointer_argument(in);   /* in == NULL && outl != NULL -> abort */

This looks like the same class of bug as #61783 (the HmacUpdate one from 2021). pal_hmac.c still carries the comment from that fix — "Callers are expected to skip update calls with no data" — but the AEAD shims don't follow that contract for the payload.

AesCcm.Android.cs and ChaCha20Poly1305.Android.cs make the same unguarded EvpCipherUpdate calls. I only runtime-tested AesGcm, so I'm flagging those two as suspected rather than confirmed.

The managed-side fix would mirror the AAD guard:

if (!plaintext.IsEmpty)
{
    if (!Interop.Crypto.EvpCipherUpdate(_ctxHandle, ciphertextAndTag, out int ciphertextBytesWritten, plaintext))
    {
        throw new CryptographicException();
    }
}

plus the matching guard on the first EvpCipherUpdate in DecryptCore (the second one passes the tag, which is never empty).

The PAL-side alternative would be treating inl == 0 as a no-op update, the way EVP_EncryptUpdate/EVP_DecryptUpdate do — though I assume the abort is there deliberately to catch programming errors, so the managed guard is probably the better fix.

Activity

  1. added
    untriaged
    on Oct 7, 2026
  2. dotnet-policy-service commented on Oct 7, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
    See info in area-owners.md if you want to be subscribed.

  3. self-assigned this
    on Oct 7, 2026
  4. dotnet-policy-service commented on Oct 7, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'arch-android': @vitek-karas, @simonrozsival, @steveisok, @akoeplinger
    See info in area-owners.md if you want to be subscribed.

  5. vcsjones commented on Oct 7, 2026

    @vcsjones
    Member

    Don't hand an empty payload to AesGcm on Android. In our library we skip the call when the payload is empty and compute/verify the tag ourselves. Padding the payload so it's never empty would also work.

    The problem isn't that the payloads are empty, the problem is that the payload is empty and the internal pointer of the empty span is nullptr. This is not supposed to be something a developer cares about, but another potential work around is using stackalloc byte[0] as the empty input.

    For example, this crashes:

    using System.Security.Cryptography;
    
    ReadOnlySpan<byte> key = new byte[16];
    ReadOnlySpan<byte> nonce = new byte[12];
    ReadOnlySpan<byte> plaintext = ReadOnlySpan<byte>.Empty;
    ReadOnlySpan<byte> aad = ReadOnlySpan<byte>.Empty;
    Span<byte> tag = new byte[16];
    Span<byte> ciphertext = ReadOnlySpan<byte>.Empty;
    
    AesGcm aesGcm = new AesGcm(key, tag.Length);
    aesGcm.Encrypt(nonce, plaintext, ciphertext, tag, aad);
    Console.WriteLine(Convert.ToHexString(tag));

    This will not:

    using System.Security.Cryptography;
    
    ReadOnlySpan<byte> key = new byte[16];
    ReadOnlySpan<byte> nonce = new byte[12];
    ReadOnlySpan<byte> plaintext = stackalloc byte[0];
    ReadOnlySpan<byte> aad = stackalloc byte[0];
    Span<byte> tag = new byte[16];
    Span<byte> ciphertext = stackalloc byte[0];
    
    AesGcm aesGcm = new AesGcm(key, tag.Length);
    aesGcm.Encrypt(nonce, plaintext, ciphertext, tag, aad);
    Console.WriteLine(Convert.ToHexString(tag));
  6. vcsjones commented on Oct 7, 2026

    @vcsjones
    Member

    I did an audit of other empty failures. This will also abort, even though it's a little odd.

    aes.TryEncryptEcb(
        ReadOnlySpan<byte>.Empty,
        Span<byte>.Empty,
        PaddingMode.None,
        out _);
  7. added theissue type on Oct 7, 2026
  8. added this to the 12.0.0 milestone on Oct 7, 2026
  9. removed
    untriaged
    on Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions