← All articlesMobile applications

How to restrict Android Keystore key operations and digests and verify rejection

When working with Android Keystore, choosing a signature algorithm is only part of the task. You also need to define what a particular key is allowed to do. Checks in application code and restrictions on the key itself are separate control boundaries. We will explore how to define a minimal contract and test it in a fully fictional application: permitted signing with SHA-256 must produce a signature, while an attempt with SHA-512 using the same key must be rejected. All tests use your own test keys and synthetic messages.

Define one key’s authorizations first

Android Keystore lets you specify a key’s permitted uses when creating or importing it. These authorizations cannot be changed afterward. Define the contract in advance: which operation the product needs and which cryptographic parameters it must use. A general requirement that ‘the key is used for signing’ is insufficient for our example: the digest must also be explicitly restricted.

Cryptographic authorizations cover algorithms, operations, padding schemes, modes, and digests. Our task is narrower: allow a private EC key to generate signatures using only SHA-256. We will also explicitly require rejection of an attempt to sign a message with SHA-512. This testable contract is more useful than a vague requirement to ‘keep the key secure.’

Where the trust boundary lies

Android Keystore enforces restrictions every time a key is used, outside the application process. This is the central mechanism to test: the attempt must reach key usage with a prohibited parameter. If the application rejects the choice of SHA-512 before accessing the key, that result confirms only that the application’s check works.

The negative test therefore needs no preliminary digest restriction in application code. We will deliberately pass the same test key into a signing scenario with a different digest. The observed result must demonstrate rejection when attempting to use the key beyond its specified authorizations.

Fictional example: signing a test receipt

Imagine a fully fictional application called ‘Receipt’ that signs the synthetic message ‘receipt=test-001; amount=0’. For testing, we create our own new EC key pair using KeyPairGenerator with the AndroidKeyStore provider. In KeyGenParameterSpec.Builder, we specify only PURPOSE_SIGN and use setDigests to allow only DIGEST_SHA256.

This is a proposed editorial adaptation of the documented example, which allows both SHA-256 and SHA-512. The second digest is deliberately excluded for our task. Android recommends the SHA-2 family with ECDSA for digital signatures and gives SHA256withECDSA as an example. However, the algorithm recommendation itself does not replace restrictions on a particular key: those must be specified separately.

Positive control: obtain a permitted signature

The following checks are an original proposed acceptance test plan, not an official Android procedure. First, retrieve the generated private key from Keystore. Then use Signature.getInstance("SHA256withECDSA"), call initSign with that key, pass the synthetic message through update, and call sign.

The acceptance criterion is successful generation of a signature. Record which test key and message were used so that the next steps refer to the same contract. If the permitted scenario fails, investigate that failure first. Rejection with another digest does not yet confirm enforcement of the restriction when the positive control is failing.

Attempt the prohibited operation, then repeat the permitted one

Now try to generate a signature through SHA512withECDSA using the same private key and synthetic message. Do not add a preliminary SHA-512 block in the application. The acceptance criterion is that the operation using the unauthorized digest is rejected and no signature is obtained. Record the actual failure stage and diagnostic error without requiring a specific exception class.

If an implementation of the algorithm cannot be obtained before the key is passed in, the result does not count as evidence of the key’s authorizations: that attempt did not test the key restriction. Likewise, successful completion of the test handler proves nothing on its own—check the outcome of the signing operation.

After the negative attempt, sign again through SHA256withECDSA using the same key. This must produce a signature. The ‘success — rejection — success’ sequence connects the observed rejection to the selected digest while confirming that the permitted scenario still works.

What to fix first

The first priority is to compare the stated contract with the test key’s creation parameters. If only SHA-256 is required, allowing SHA-512 in setDigests contradicts that contract. Since an existing key’s authorizations cannot be changed, test the corrected configuration using a new test key pair of your own.

The second priority is the quality of the rejection evidence. Remove application-level digest blocking from the negative test, distinguish failure to obtain an algorithm implementation from rejection during key usage, and retain diagnostics. If a prohibited signature is obtained, the acceptance criterion has not been met. If the positive control fails, defer any conclusion about the restriction until that problem is resolved.

Limits of the result and the next step

Together, the three checks confirm the selected digest contract only in the tested configuration. Although PURPOSE_SIGN was specified during creation, comparing two signing variants does not prove rejection of all other operations. It also does not test the authenticity of incoming messages, hardware storage of keys, user authentication, data recovery, or migration of existing keys, and it provides no statistical guarantee of security.

For the product owner, the result is a clear acceptance criterion: the permitted signature is obtained, the prohibited one is rejected, and the permitted operation succeeds again. If the product has a website or web component, you can start with a free audit of that part through CheckMyApp. The Android Keystore checks described here remain a separate development task.

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