← All articlesMobile applications

RSA-OAEP on Android: Aligning the Main Digest and MGF1

Using the same RSA-OAEP name on both sides of an exchange does not fully describe the parameters of a particular operation. Android developers need to agree separately on the main digest and the MGF1 digest, then verify the decrypted result. We will walk through an entirely fictional integration using our own test keys and a synthetic message: first, the receiver must recover the original bytes; then, it must reject the message when an MGF1 mismatch is deliberately introduced.

Why the Transformation Name Is Not Enough

RSA-OAEP has two separate digest parameters: the main digest and the MGF1 digest. The name RSA/ECB/OAEPwithSHA-256andMGF1Padding specifies SHA-256 as the main digest but leaves the MGF1 digest unspecified. Using this name in both the sender's and receiver's settings therefore does not confirm that both values agree.

For this implicit selection, the Android documentation identifies a difference: Android Keystore uses SHA-1 for MGF1, while other Android cryptographic providers use the same digest for the main digest and MGF1. When investigating an incompatibility, check this specific pair rather than relying on the presence of SHA-256 in the transformation name.

Define One Operation with Explicit Parameters

The Android documentation suggests using RSA/ECB/OAEPPadding and passing OAEPParameterSpec to init() to explicitly select both digests. Its examples show two configurations: a main digest of SHA-256 with SHA-1 for MGF1, and SHA-256 for both digests. For our fictional exchange, we will choose the second option; this is a condition of our example, not a universal recommendation for every integration.

On Android, the parameters for this operation are expressed as OAEPParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT). Pass this to init() for RSA/ECB/OAEPPadding. On the receiving side, explicitly configure the corresponding parameters using the facilities provided by that implementation. OWASP recommends performing cryptographic operations through standard Android and iOS APIs.

  • Transformation: RSA/ECB/OAEPPadding; main digest: SHA-256.
  • Mask generation algorithm: MGF1; MGF1 digest: SHA-256.
  • PSource: PSource.PSpecified.DEFAULT; the same agreed value on both sides.

Prepare a Fictional Exchange and Define the Trust Boundary

Imagine a fictional app called “Practice Order” and a test receiver owned by its developer. The test uses only the developer's own test key pair and the short synthetic message “order:test-001”. Asymmetric encryption uses two separate keys: a public key and a private key. In our scenario, the sender encrypts with the public test key, and the receiver decrypts with the corresponding private key.

The trust boundary in this exercise lies between receiving the ciphertext and recognizing the message as successfully decrypted. The receiver must not treat delivery itself as proof of the result. Save the message's original byte sequence in advance and identify where the test can observe the decrypted result and its final acceptance status. Record both implementations under test and their configurations.

Positive Control: Compare the Original Bytes

The following plan is an original proposed acceptance test, not an official Android or OWASP procedure. Encrypt the synthetic message with the selected parameters and send the ciphertext to the test receiver. On the receiving side, use the corresponding private key, SHA-256 as the main digest, MGF1 with SHA-256, and the agreed PSource.

The acceptance criterion is byte-for-byte equality between the decrypted result and the saved original message. Successfully creating a Cipher, completing init(), or receiving a successful HTTP response does not, by itself, meet this criterion. If the exchange passes through a web handler, also check the observable processing result: this specific synthetic message must be marked as successfully decrypted. Save the ciphertext for the next test.

Negative Control: Change Only MGF1

This is also an original proposed test using your own data. Take the ciphertext from the successful positive control. On the receiver, change only the MGF1 digest from SHA-256 to SHA-1. Keep the main SHA-256 digest, keys, ciphertext, and all other parameters unchanged. This setup lets you test one specific, deliberate mismatch.

The acceptance criterion is that the message is not accepted as successfully decrypted. No particular error type is prescribed: the test does not require identical exceptions or responses from different implementations. Check the message's final status and confirm that no result is recognized as a successful decryption; an HTTP status alone is insufficient. If the system still marks the message as accepted, the acceptance criterion has not been met, and the result needs investigation.

What to Fix First

Start by documenting the parameters on both sides: list the main digest and the MGF1 digest as separate values. Then replace the implicit assumption with an explicit OAEPParameterSpec passed to init() on Android and a corresponding configuration on the receiver. After the change, repeat both controls: matching parameters must recover the original bytes, while a deliberate MGF1 mismatch must not result in the message being recognized as successfully decrypted.

If the positive control fails, first compare the parameters actually configured and verify that the test keys correspond. If the negative control reports success, check which parameters the receiver used and why the handler assigned a successful status. Do not consider the fix complete merely because an error disappeared or a request received a successful response.

Limits of the Results and Reviewing the Web Component

These checks confirm compatibility only for the implementations and configurations tested. They provide no statistical guarantee of security and do not promise identical behavior across all implementations. This article does not cover cryptographic provider selection, key migration, Keystore recovery, digital signatures, or the IV lifecycle.

The practical outcome is a description of one RSA-OAEP operation with two explicitly agreed digests and observable results from the positive and negative controls. If the integration includes a website or web product, you can request a free CheckMyApp audit for an additional review of its web component.

Sources and documentation

  1. Cryptography
  2. Mobile App Cryptography
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free