1 /*
2  * Copyright (C) 2019 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 android.car.encryptionrunner;
18 
19 import android.annotation.NonNull;
20 
21 import java.security.NoSuchAlgorithmException;
22 import java.security.SignatureException;
23 
24 /**
25  * Represents a serializable encryption key.
26  */
27 public interface Key {
28     /**
29      * Returns a serialized encryption key.
30      */
31     @NonNull
asBytes()32     byte[] asBytes();
33 
34     /**
35      * Encrypts data using this key.
36      *
37      * @param data the data to be encrypted
38      * @return the encrypted data.
39      */
40     @NonNull
encryptData(@onNull byte[] data)41     byte[] encryptData(@NonNull byte[] data);
42 
43     /**
44      * Decrypts data using this key.
45      *
46      * @param encryptedData The encrypted data.
47      * @return decrypted data.
48      * @throws SignatureException if encrypted data is not properly signed.
49      */
50     @NonNull
decryptData(@onNull byte[] encryptedData)51     byte[] decryptData(@NonNull byte[] encryptedData) throws SignatureException;
52 
53     /**
54      * Returns a cryptographic digest of the key.
55      *
56      * @throws NoSuchAlgorithmException when a unique session can not be created.
57      */
58     @NonNull
getUniqueSession()59     byte[] getUniqueSession() throws NoSuchAlgorithmException;
60 }
61