1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.verifier.biometrics; 18 19 import android.hardware.biometrics.BiometricPrompt; 20 import android.security.keystore.KeyGenParameterSpec; 21 import android.security.keystore.KeyProperties; 22 23 import javax.crypto.KeyGenerator; 24 import javax.crypto.Mac; 25 26 public abstract class AbstractUserAuthenticationMacTest extends AbstractUserAuthenticationTest { 27 private Mac mMac; 28 29 @Override createUserAuthenticationKey(String keyName, int timeout, int authType, boolean useStrongBox)30 void createUserAuthenticationKey(String keyName, int timeout, int authType, 31 boolean useStrongBox) throws Exception { 32 KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder( 33 keyName, KeyProperties.PURPOSE_SIGN); 34 builder.setUserAuthenticationRequired(true) 35 .setIsStrongBoxBacked(useStrongBox) 36 .setUserAuthenticationParameters(timeout, authType); 37 38 KeyGenerator keyGenerator = KeyGenerator.getInstance( 39 KeyProperties.KEY_ALGORITHM_HMAC_SHA256, "AndroidKeyStore"); 40 keyGenerator.init(builder.build()); 41 keyGenerator.generateKey(); 42 } 43 44 @Override initializeKeystoreOperation(String keyName)45 void initializeKeystoreOperation(String keyName) throws Exception { 46 mMac = Utils.initMac(keyName); 47 } 48 49 @Override getCryptoObject()50 BiometricPrompt.CryptoObject getCryptoObject() { 51 return new BiometricPrompt.CryptoObject(mMac); 52 } 53 54 @Override doKeystoreOperation(byte[] payload)55 void doKeystoreOperation(byte[] payload) throws Exception { 56 try { 57 mMac.doFinal(payload); 58 } finally { 59 mMac = null; 60 } 61 } 62 } 63