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
23namespace keymaster {
24
25class Key;
26class KeyFactory;
27class KeymasterContext;
28class 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 */
48class AndroidKeymaster {
49  public:
50    AndroidKeymaster(KeymasterContext* context, size_t operation_table_size);
51    virtual ~AndroidKeymaster();
52
53    void SupportedAlgorithms(const SupportedAlgorithmsRequest& request,
54                             SupportedAlgorithmsResponse* response);
55    void SupportedBlockModes(const SupportedBlockModesRequest& request,
56                             SupportedBlockModesResponse* response);
57    void SupportedPaddingModes(const SupportedPaddingModesRequest& request,
58                               SupportedPaddingModesResponse* response);
59    void SupportedDigests(const SupportedDigestsRequest& request,
60                          SupportedDigestsResponse* response);
61    void SupportedImportFormats(const SupportedImportFormatsRequest& request,
62                                SupportedImportFormatsResponse* response);
63    void SupportedExportFormats(const SupportedExportFormatsRequest& request,
64                                SupportedExportFormatsResponse* response);
65
66    void AddRngEntropy(const AddEntropyRequest& request, AddEntropyResponse* response);
67    void GenerateKey(const GenerateKeyRequest& request, GenerateKeyResponse* response);
68    void GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request,
69                               GetKeyCharacteristicsResponse* response);
70    void ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response);
71    void ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response);
72    void DeleteKey(const DeleteKeyRequest& request, DeleteKeyResponse* response);
73    void DeleteAllKeys(const DeleteAllKeysRequest& request, DeleteAllKeysResponse* response);
74    void BeginOperation(const BeginOperationRequest& request, BeginOperationResponse* response);
75    void UpdateOperation(const UpdateOperationRequest& request, UpdateOperationResponse* response);
76    void FinishOperation(const FinishOperationRequest& request, FinishOperationResponse* response);
77    void AbortOperation(const AbortOperationRequest& request, AbortOperationResponse* response);
78    void GetVersion(const GetVersionRequest& request, GetVersionResponse* response);
79
80  private:
81    keymaster_error_t LoadKey(const keymaster_key_blob_t& key_blob,
82                              const AuthorizationSet& additional_params,
83                              AuthorizationSet* hw_enforced, AuthorizationSet* sw_enforced,
84                              const KeyFactory** factory, UniquePtr<Key>* key);
85
86    UniquePtr<KeymasterContext> context_;
87    UniquePtr<OperationTable> operation_table_;
88};
89
90}  // namespace keymaster
91
92#endif  //  SYSTEM_KEYMASTER_ANDROID_KEYMASTER_H_
93