0

I have the following methods for generating keys and signing the password message.

public void generateKeys() {
try {
  keyStore = KeyStore.getInstance(KEYSTORE_NAME);
  keyStore.load(null);

  if (!keyStore.containsAlias(KEY_NAME)) {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, KEYSTORE_NAME);
    keyPairGenerator.initialize(
        new KeyGenParameterSpec.Builder(KEY_NAME,
                                        KeyProperties.PURPOSE_SIGN)
            .setDigests(KeyProperties.DIGEST_SHA256)
            .setAlgorithmParameterSpec(new ECGenParameterSpec("secp256r1"))
            // Require the user to authenticate with a fingerprint to authorize
            // every use of the private key
            .setUserAuthenticationRequired(true)
            .build());
    keyPairGenerator.generateKeyPair();
  }

  loadKeys();
} catch (NoSuchAlgorithmException e) {
  e.printStackTrace();
} catch (NoSuchProviderException e) {
  e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
  e.printStackTrace();
} catch (CertificateException e) {
  e.printStackTrace();
} catch (KeyStoreException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

}

public BiometricPrompt.CryptoObject getCryptoObject() {
cryptoObject = new BiometricPrompt.CryptoObject(signature);
return cryptoObject;
}

private void loadKeys() {
try {
  keyStore = KeyStore.getInstance(KEYSTORE_NAME);
  keyStore.load(null);
  if (keyStore.containsAlias(KEY_NAME)) {
    publicKey = keyStore.getCertificate(KEY_NAME).getPublicKey();
    privateKey = (PrivateKey) keyStore.getKey(KEY_NAME, null);
    signature = Signature.getInstance(Constants.SIGNATURE);
    signature.initSign(privateKey);
  }
} catch (IOException e) {
  e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
  e.printStackTrace();
} catch (CertificateException e) {
  e.printStackTrace();
} catch (KeyStoreException e) {
  e.printStackTrace();
} catch (UnrecoverableKeyException e) {
  e.printStackTrace();
} catch (InvalidKeyException e) {
  e.printStackTrace();
}
}

public String sign(String inputStr) {
try {
  Signature signature = cryptoObject.getSignature();
  signature.update(inputStr.getBytes());
  byte[] signedBytes = signature.sign();
  String result = HexManager.bytesToHex(signedBytes);
  Log.d("TAG", result);
  return result;
} catch (SignatureException e) {
  e.printStackTrace();
}
return null;

}

After that, I saved the signed password in Shared Preferences. Later, I want to verify the saved password with a new password that is verified with the fingerprint.

Here is my method to verify:

public boolean verify(String inputStr, String savedStr) {
try {
  Signature signature = cryptoObject.getSignature();
  signature.initVerify(publicKey);
  signature.update(inputStr.getBytes());
  boolean isVerified = signature.verify(savedStr.getBytes());
  return isVerified;
} catch (InvalidKeyException e) {
  e.printStackTrace();
} catch (SignatureException e) {
  e.printStackTrace();
}
return false;
}

But it always return false.

Does anybody knows why?

enter image description here

Zookey
  • 2,637
  • 13
  • 46
  • 80
  • Shouldn't you be undoing the `bytesToHex` step at some point during the signature verification process? – Michael May 21 '19 at 10:06

1 Answers1

1

It looks like in your sign() method you're returning the Hex of the byte[] obtained from the signature.sign() method. If this is what you're saving as savedStr. Then the verification method should be changed to convert the Hex encoded string to a byte[]. You could use HexManager.hexToBytes() (or equivalent) if it exists to convert the savedStr to a byte[] savedStrBytes.

public boolean verify(String inputStr, String savedStr) {
    try {
        byte[] savedStrBytes = HexManager.hexToByes(savedStr);
        Signature signature = cryptoObject.getSignature();
        signature.initVerify(publicKey);
        signature.update(inputStr.getBytes());
        boolean isVerified = signature.verify(savedStrBytes);
        return isVerified;
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (SignatureException e) {
        e.printStackTrace();
    }
    return false;
}
Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36