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_SYMMETRIC_KEY_H_
18 #define SYSTEM_KEYMASTER_SYMMETRIC_KEY_H_
19 
20 #include <keymaster/key_factory.h>
21 
22 #include "key.h"
23 
24 namespace keymaster {
25 
26 class SymmetricKey;
27 
28 class SymmetricKeyFactory : public KeyFactory {
29   public:
SymmetricKeyFactory(const KeymasterContext * context)30     SymmetricKeyFactory(const KeymasterContext* context) : KeyFactory(context) {}
31 
32     keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
33                                   KeymasterKeyBlob* key_blob, AuthorizationSet* hw_enforced,
34                                   AuthorizationSet* sw_enforced) const override;
35     keymaster_error_t ImportKey(const AuthorizationSet& key_description,
36                                 keymaster_key_format_t input_key_material_format,
37                                 const KeymasterKeyBlob& input_key_material,
38                                 KeymasterKeyBlob* output_key_blob, AuthorizationSet* hw_enforced,
39                                 AuthorizationSet* sw_enforced) const override;
40 
41     virtual const keymaster_key_format_t* SupportedImportFormats(size_t* format_count) const;
SupportedExportFormats(size_t * format_count)42     virtual const keymaster_key_format_t* SupportedExportFormats(size_t* format_count) const {
43         return NoFormats(format_count);
44     };
45 
46   private:
47     virtual bool key_size_supported(size_t key_size_bits) const = 0;
48     virtual keymaster_error_t
49     validate_algorithm_specific_new_key_params(const AuthorizationSet& key_description) const = 0;
50 
NoFormats(size_t * format_count)51     const keymaster_key_format_t* NoFormats(size_t* format_count) const {
52         *format_count = 0;
53         return NULL;
54     }
55 };
56 
57 class SymmetricKey : public Key {
58   public:
59     ~SymmetricKey();
60 
61     virtual keymaster_error_t key_material(UniquePtr<uint8_t[]>* key_material, size_t* size) const;
formatted_key_material(keymaster_key_format_t,UniquePtr<uint8_t[]> *,size_t *)62     virtual keymaster_error_t formatted_key_material(keymaster_key_format_t, UniquePtr<uint8_t[]>*,
63                                                      size_t*) const {
64         return KM_ERROR_UNSUPPORTED_KEY_FORMAT;
65     }
66 
key_data()67     const uint8_t* key_data() const { return key_data_.get(); }
key_data_size()68     size_t key_data_size() const { return key_data_size_; }
69 
70   protected:
71     SymmetricKey(const KeymasterKeyBlob& key_material, const AuthorizationSet& hw_enforced,
72                  const AuthorizationSet& sw_enforced, keymaster_error_t* error);
73 
74   private:
75     size_t key_data_size_;
76     UniquePtr<uint8_t[]> key_data_;
77 };
78 
79 }  // namespace keymaster
80 
81 #endif  // SYSTEM_KEYMASTER_AES_KEY_H_
82