1 /*
2  * Copyright 2014 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 #ifndef SYSTEM_KEYMASTER_ANDROID_KEYMASTER_H_
18 #define SYSTEM_KEYMASTER_ANDROID_KEYMASTER_H_
19 
20 #include <keymaster/android_keymaster_messages.h>
21 #include <keymaster/authorization_set.h>
22 
23 namespace keymaster {
24 
25 class Key;
26 class KeyFactory;
27 class KeymasterContext;
28 class OperationTable;
29 
30 /**
31  * This is the reference implementation of Keymaster.  In addition to acting as a reference for
32  * other Keymaster implementers to check their assumptions against, it is used by Keystore as the
33  * default implementation when no secure implementation is available, and may be installed and
34  * executed in secure hardware as a secure implementation.
35  *
36  * Note that this class doesn't actually implement the Keymaster HAL interface, instead it
37  * implements an alternative API which is similar to and based upon the HAL, but uses C++ "message"
38  * classes which support serialization.
39  *
40  * For non-secure, pure software implementation there is a HAL translation layer that converts the
41  * HAL's parameters to and from the message representations, which are then passed in to this
42  * API.
43  *
44  * For secure implementation there is another HAL translation layer that serializes the messages to
45  * the TEE. In the TEE implementation there's another component which deserializes the messages,
46  * extracts the relevant parameters and calls this API.
47  */
48 class AndroidKeymaster {
49   public:
50     AndroidKeymaster(KeymasterContext* context, size_t operation_table_size);
51     virtual ~AndroidKeymaster();
52     AndroidKeymaster(AndroidKeymaster&&);
53 
54     void GetVersion(const GetVersionRequest& request, GetVersionResponse* response);
55     void SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
56                              SupportedAlgorithmsResponse* response);
57     void SupportedBlockModes(const SupportedBlockModesRequest& request,
58                              SupportedBlockModesResponse* response);
59     void SupportedPaddingModes(const SupportedPaddingModesRequest& request,
60                                SupportedPaddingModesResponse* response);
61     void SupportedDigests(const SupportedDigestsRequest& request,
62                           SupportedDigestsResponse* response);
63     void SupportedImportFormats(const SupportedImportFormatsRequest& request,
64                                 SupportedImportFormatsResponse* response);
65     void SupportedExportFormats(const SupportedExportFormatsRequest& request,
66                                 SupportedExportFormatsResponse* response);
67 
68     GetHmacSharingParametersResponse GetHmacSharingParameters();
69     ComputeSharedHmacResponse ComputeSharedHmac(const ComputeSharedHmacRequest& request);
70     VerifyAuthorizationResponse VerifyAuthorization(const VerifyAuthorizationRequest& request);
71 
72     void AddRngEntropy(const AddEntropyRequest& request, AddEntropyResponse* response);
73     void Configure(const ConfigureRequest& request, ConfigureResponse* response);
74     void GenerateKey(const GenerateKeyRequest& request, GenerateKeyResponse* response);
75     void GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
76                                GetKeyCharacteristicsResponse* response);
77     void ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response);
78     void ImportWrappedKey(const ImportWrappedKeyRequest& request,
79                           ImportWrappedKeyResponse* response);
80     void ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response);
81     void AttestKey(const AttestKeyRequest& request, AttestKeyResponse* response);
82     void UpgradeKey(const UpgradeKeyRequest& request, UpgradeKeyResponse* response);
83     void DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response);
84     void DeleteAllKeys(const DeleteAllKeysRequest& request, DeleteAllKeysResponse* response);
85     void BeginOperation(const BeginOperationRequest& request, BeginOperationResponse* response);
86     void UpdateOperation(const UpdateOperationRequest& request, UpdateOperationResponse* response);
87     void FinishOperation(const FinishOperationRequest& request, FinishOperationResponse* response);
88     void AbortOperation(const AbortOperationRequest& request, AbortOperationResponse* response);
89 
90     EarlyBootEndedResponse EarlyBootEnded();
91     DeviceLockedResponse DeviceLocked(const DeviceLockedRequest& request);
92 
93     bool has_operation(keymaster_operation_handle_t op_handle) const;
94 
95   private:
96     keymaster_error_t LoadKey(const keymaster_key_blob_t& key_blob,
97                               const AuthorizationSet& additional_params,
98                               const KeyFactory** factory, UniquePtr<Key>* key);
99 
100     UniquePtr<KeymasterContext> context_;
101     UniquePtr<OperationTable> operation_table_;
102 };
103 
104 }  // namespace keymaster
105 
106 #endif  //  SYSTEM_KEYMASTER_ANDROID_KEYMASTER_H_
107