1 /*
2  * Copyright 2015 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_KEY_FACTORY_H_
18 #define SYSTEM_KEYMASTER_KEY_FACTORY_H_
19 
20 #include <hardware/keymaster_defs.h>
21 #include <keymaster/authorization_set.h>
22 
23 namespace keymaster {
24 
25 class Key;
26 class KeymasterContext;
27 class OperationFactory;
28 template<typename BlobType> struct TKeymasterBlob;
29 typedef TKeymasterBlob<keymaster_key_blob_t> KeymasterKeyBlob;
30 
31 /**
32  * KeyFactory is a abstraction that encapsulats the knowledge of how to build and parse a specifiec
33  * subclass of Key.
34  */
35 class KeyFactory {
36   public:
~KeyFactory()37     virtual ~KeyFactory() {}
38 
39     // Factory methods.
40     virtual keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
41                                           KeymasterKeyBlob* key_blob, AuthorizationSet* hw_enforced,
42                                           AuthorizationSet* sw_enforced) const = 0;
43 
44     virtual keymaster_error_t ImportKey(const AuthorizationSet& key_description,
45                                         keymaster_key_format_t input_key_material_format,
46                                         const KeymasterKeyBlob& input_key_material,
47                                         KeymasterKeyBlob* output_key_blob,
48                                         AuthorizationSet* hw_enforced,
49                                         AuthorizationSet* sw_enforced) const = 0;
50 
51     virtual keymaster_error_t LoadKey(KeymasterKeyBlob&& key_material,
52                                       const AuthorizationSet& additional_params,
53                                       AuthorizationSet&& hw_enforced,
54                                       AuthorizationSet&& sw_enforced,
55                                       UniquePtr<Key>* key) const = 0;
56 
57     virtual OperationFactory* GetOperationFactory(keymaster_purpose_t purpose) const = 0;
58 
59     // Informational methods.
60     virtual const keymaster_key_format_t* SupportedImportFormats(size_t* format_count) const = 0;
61     virtual const keymaster_key_format_t* SupportedExportFormats(size_t* format_count) const = 0;
62 };
63 
64 }  // namespace keymaster
65 
66 #endif  // SYSTEM_KEYMASTER_KEY_FACTORY_H_
67