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 #include <keymaster/random_source.h>
22 #include <keymaster/soft_key_factory.h>
23 
24 #include <keymaster/key.h>
25 
26 namespace keymaster {
27 
28 class SymmetricKey;
29 
30 class SymmetricKeyFactory : public KeyFactory, public SoftKeyFactoryMixin {
31   public:
SymmetricKeyFactory(const SoftwareKeyBlobMaker * blob_maker,const RandomSource * random_source)32     explicit SymmetricKeyFactory(const SoftwareKeyBlobMaker* blob_maker,
33                                  const RandomSource* random_source) :
34             SoftKeyFactoryMixin(blob_maker),
35             random_source_(*random_source) {}
36 
37     keymaster_error_t GenerateKey(const AuthorizationSet& key_description,
38                                   KeymasterKeyBlob* key_blob, AuthorizationSet* hw_enforced,
39                                   AuthorizationSet* sw_enforced) const override;
40     keymaster_error_t ImportKey(const AuthorizationSet& key_description,
41                                 keymaster_key_format_t input_key_material_format,
42                                 const KeymasterKeyBlob& input_key_material,
43                                 KeymasterKeyBlob* output_key_blob, AuthorizationSet* hw_enforced,
44                                 AuthorizationSet* sw_enforced) const override;
45 
46     virtual const keymaster_key_format_t* SupportedImportFormats(size_t* count) const override;
SupportedExportFormats(size_t * count)47     virtual const keymaster_key_format_t* SupportedExportFormats(size_t* count) const override {
48         return NoFormats(count);
49     };
50 
51   private:
52     virtual bool key_size_supported(size_t key_size_bits) const = 0;
53 
54     // These methods translate between key size in bits and bytes.  Normally it's just 8 bits to the
55     // byte, but DES is different.
key_size_bytes(size_t key_size_bits)56     virtual size_t key_size_bytes(size_t key_size_bits) const { return key_size_bits / 8; }
key_size_bits(size_t key_size_bytes)57     virtual size_t key_size_bits(size_t key_size_bytes) const { return key_size_bytes * 8; }
58 
59     virtual keymaster_error_t
60     validate_algorithm_specific_new_key_params(const AuthorizationSet& key_description) const = 0;
61 
NoFormats(size_t * format_count)62     const keymaster_key_format_t* NoFormats(size_t* format_count) const {
63         *format_count = 0;
64         return nullptr;
65     }
66     const RandomSource& random_source_;
67 };
68 
69 class SymmetricKey : public Key {
70   public:
71     ~SymmetricKey();
72 
formatted_key_material(keymaster_key_format_t,UniquePtr<uint8_t[]> *,size_t *)73     virtual keymaster_error_t formatted_key_material(keymaster_key_format_t, UniquePtr<uint8_t[]>*,
74                                                      size_t*) const {
75         return KM_ERROR_UNSUPPORTED_KEY_FORMAT;
76     }
77 
78   protected:
79     SymmetricKey(KeymasterKeyBlob&& key_material, AuthorizationSet&& hw_enforced,
80                  AuthorizationSet&& sw_enforced,
81                  const KeyFactory* key_factory);
82 };
83 
84 }  // namespace keymaster
85 
86 #endif  // SYSTEM_KEYMASTER_AES_KEY_H_
87