1 /* 2 * Copyright (C) 2022 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.Cipher; 24 import javax.crypto.KeyGenerator; 25 26 /** 27 * An abstract base class to add Aead Cipher tests. 28 */ 29 public abstract class AbstractUserAuthenticationAeadCipherTest 30 extends AbstractUserAuthenticationTest { 31 private Cipher mCipher; 32 33 @Override createUserAuthenticationKey(String keyName, int timeout, int authType, boolean useStrongBox)34 void createUserAuthenticationKey(String keyName, int timeout, int authType, 35 boolean useStrongBox) throws Exception { 36 KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder( 37 keyName, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT); 38 builder.setBlockModes(KeyProperties.BLOCK_MODE_GCM) 39 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) 40 .setUserAuthenticationRequired(true) 41 .setUserAuthenticationParameters(timeout, authType) 42 .setIsStrongBoxBacked(useStrongBox); 43 44 KeyGenerator keyGenerator = KeyGenerator.getInstance( 45 KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"); 46 keyGenerator.init(builder.build()); 47 keyGenerator.generateKey(); 48 } 49 50 @Override initializeKeystoreOperation(String keyName)51 void initializeKeystoreOperation(String keyName) throws Exception { 52 mCipher = Utils.initAeadCipher(keyName); 53 } 54 55 @Override getCryptoObject()56 BiometricPrompt.CryptoObject getCryptoObject() { 57 return new BiometricPrompt.CryptoObject(mCipher); 58 } 59 60 @Override doKeystoreOperation(byte[] payload)61 void doKeystoreOperation(byte[] payload) throws Exception { 62 try { 63 byte[] aad = "Test aad data".getBytes(); 64 mCipher.updateAAD(aad); 65 Utils.doEncrypt(mCipher, payload); 66 } finally { 67 mCipher = null; 68 } 69 } 70 } 71