← All articlesMobile applications

Verifying digital signatures in Android: how to avoid accepting a modified message

Digital signature verification is useful to an application only when its result affects whether data is accepted. Calling verify() and seeing a Boolean in the debugger is not enough: you need to check that a negative result prevents the message from becoming an accepted order or another business record. We will examine one operation using an entirely fictional example and our own test keys. The outcome will be observable acceptance criteria for an Android developer and a product owner.

What we are checking and where the trust boundary lies

Our fictional application receives a synthetic message, ‘Order TEST-ONLY: 10’, and a separate signature. We will define acceptance as writing this order to a test store and triggering the designated test action. Until verification, the message remains input data; permission to write it must depend on the verification result. This is a proposed application implementation requirement, rather than an action that Signature performs itself.

Public key A is predefined as trusted in the test configuration. Verification requires a PublicKey containing the signer's public key. Obtaining a trusted key from external sources is outside our scope: a key received with the message must not automatically replace the predefined key A in this scenario.

How message bytes, the signature and the verify result relate

In the Android documentation example, the verification object is created using Signature.getInstance("SHA256withECDSA"). This is followed by initVerify(key), update(message) and verify(signature). The message bytes are passed to update, and the verify result is stored as Boolean valid. For our acceptance criteria, we need to trace this entire path through to the application's decision to write the order.

Creating a signature requires a PrivateKey containing the signing key. The documented example uses initSign(key), followed by update(message), after which sign() returns the signature bytes. In our test, we will capture the UTF-8 bytes of the original string once and use those exact bytes for signing and positive verification. This keeps the comparison of results independent of reconstructing the string.

A positive control using your own data

The following are original editorial tests, rather than an official Android procedure. Prepare your own test key pair A and sign the captured message bytes with private key A. Save the original bytes and the resulting signature as separate test values. For verification, create a new SHA256withECDSA operation, call initVerify with public key A, pass the original bytes through update and call verify with the saved signature.

The positive control has two observable acceptance criteria: valid is true, and the application accepts the exact message that was verified. The expected TEST-ONLY record with a value of 10 must appear in the test store, followed by the designated action. If the record is missing or contains different data, the full scenario has not yet passed, even if the result is true.

Two negative tests: a modified message and a different key

In the first negative test, change one byte of the original message, for example by replacing the digit 1 with 2 in the value 10. Keep the existing signature and trusted public key A. Run a separate verification operation with the modified bytes. Do not sign them again: that would test a different message and signature combination. The proposed acceptance criteria are false and no acceptance of the modified order.

In the second test, create your own independent key pair B. In a separate test configuration, pass public key B to initVerify while retaining the original message and signature A. The expected result of this editorial test is false, with no order written and no subsequent action. The key substitution here is deliberate and confined to the test; it does not change the trust rule in the operational configuration.

Check application rejection, beyond the cryptographic call

Before each scenario, put the test store into a known state. After verification, compare its contents and whether the action was triggered against the expected result. In both negative scenarios, the record must be absent and the action must not run. A successful HTTP response, a closed screen or a log message alone does not establish whether the application accepted the data.

Define a separate requirement for handling a verification exception: it must also prevent acceptance of the message. When fixing the implementation, first block the write path for false results and exceptions, then ensure that the data reaching the write operation is the exact data that was verified. After that, establish the positive control and both negative scenarios as acceptance criteria. This sequence ties the fix to observable product behavior.

Limits of the result and the next step

The Android Keystore documentation also demonstrates verification using verify(byte[]): the example initializes verification with the entry's certificate, passes the data and returns a Boolean result. For this task, that does not require a separate discussion of key storage: the application's decision after signature verification remains central.

Save the results of the three scenarios: original bytes with key A, modified bytes with key A and original bytes with key B. Limit your conclusion to these cases for this one operation. They do not prove the application's overall security; key distribution, certificate infrastructure and protection against replay remain outside the scope of this article. If the product has a website or web component, you can start with a free website or web-product audit from CheckMyApp. These acceptance criteria for the Android operation remain a separate task for the development team.

Sources and documentation

  1. Android Keystore system
  2. 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