• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 java.security.KeyPairGenerator;
24 import java.security.KeyStore;
25 
26 import javax.crypto.Cipher;
27 import javax.crypto.KeyGenerator;
28 
29 public abstract class AbstractUserAuthenticationCipherTest extends AbstractUserAuthenticationTest {
30     private Cipher mCipher;
31 
32     @Override
createUserAuthenticationKey(String keyName, int timeout, int authType, boolean useStrongBox)33     void createUserAuthenticationKey(String keyName, int timeout, int authType,
34             boolean useStrongBox) throws Exception {
35         KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(
36                 keyName, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT);
37         builder.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
38                 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
39                 .setUserAuthenticationRequired(true)
40                 .setUserAuthenticationParameters(timeout, authType)
41                 .setIsStrongBoxBacked(useStrongBox);
42 
43         KeyGenerator keyGenerator = KeyGenerator.getInstance(
44                 KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
45         keyGenerator.init(builder.build());
46         keyGenerator.generateKey();
47     }
48 
49     @Override
initializeKeystoreOperation(String keyName)50     void initializeKeystoreOperation(String keyName) throws Exception {
51         mCipher = Utils.initCipher(keyName);
52     }
53 
54     @Override
getCryptoObject()55     BiometricPrompt.CryptoObject getCryptoObject() {
56         return new BiometricPrompt.CryptoObject(mCipher);
57     }
58 
59     @Override
doKeystoreOperation(byte[] payload)60     void doKeystoreOperation(byte[] payload) throws Exception {
61         try {
62             Utils.doEncrypt(mCipher, payload);
63         } finally {
64             mCipher = null;
65         }
66     }
67 }
68