title: Android Fingerprint API and Private/Public keys
body: <p>When I enrol first and only fingerprint and generate <code>KeyPair</code> the <code>PrivateKey</code> gets invalidated when I use it for the second time. This happens only once. Am I the only one having this issue? Is there something wrong with my code?</p>
  
  <p>I cannot use any other key as I'm using <code>PrivateKey</code> to sign data.</p>
  
  <p>Steps:</p>
  
  <ol>
  <li>Wipe all fingerprints</li>
  <li>Enrol one fingerprint</li>
  <li>Generate <code>KeyPair</code> and use <code>FingerprintManager :: authenticate</code></li>
  <li>During next use of <code>FingerprintManager :: authenticate PrivateKey</code> gets permanently invalidated. This happens only for the first time</li>
  </ol>
  
  <p>Below the code where I generate the <code>KeyPair</code></p>
  
  <pre><code>KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
  keystore.load(null);
  KeyPairGenerator generator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
  generator.initialize(new KeyGenParameterSpec.Builder("key_name", KeyProperties.PURPOSE_SIGN)
      .setDigests(digest) // I have defined digest before
      .setSignaturePaddings(paddings) // I have defined paddings before
      .setUserAuthenticationRequired(true)
      .build());
  generator.generateKeyPair();
  </code></pre>
  
  <p>And here is the code where I invoke fingerprint authentication for data signing:</p>
  
  <pre><code>KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
  keyStore.load(null);
  Signature signature = Signature.getInstance("signing_algorithm");
  PrivateKey privateKey = (PrivateKey) keyStore.getKey("key_name", null);
  signature.initSign(privateKey); // Here I get KeyPermanentlyInvalidatedException
  CryptoObject crypto = new CryptoObject(signature);
  FingerprintManager fingerprintManager = context.getSystemService(FingerprintManager.class);
  CancellationSignal cancellationSignal = new CancellationSignal();
  AuthenticationCallback authenticationCallback = new AuthenticationCallback() {
      ...
  };
  fingerprintManager.authenticate(crypto, cancelationSignal, 0, authenticationCallback, null);
  </code></pre>