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 #pragma once
18 
19 #include <utility>
20 
21 #include <openssl/aes.h>
22 
23 #include "symmetric_key.h"
24 
25 namespace keymaster {
26 
27 const size_t kMinGcmTagLength = 12 * 8;
28 const size_t kMaxGcmTagLength = 16 * 8;
29 
30 class AesKeyFactory : public SymmetricKeyFactory {
31   public:
AesKeyFactory(const SoftwareKeyBlobMaker & blob_maker,const RandomSource & random_source)32     explicit AesKeyFactory(const SoftwareKeyBlobMaker& blob_maker,
33                            const RandomSource& random_source)
34         : SymmetricKeyFactory(blob_maker, random_source) {}
35 
registry_key()36     keymaster_algorithm_t registry_key() const { return KM_ALGORITHM_AES; }
37 
38     keymaster_error_t LoadKey(KeymasterKeyBlob&& key_material,
39                               const AuthorizationSet& additional_params,
40                               AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced,
41                               UniquePtr<Key>* key) const override;
42 
43     OperationFactory* GetOperationFactory(keymaster_purpose_t purpose) const override;
44 
45   private:
key_size_supported(size_t key_size_bits)46     bool key_size_supported(size_t key_size_bits) const override {
47         return key_size_bits == 128 || key_size_bits == 192 || key_size_bits == 256;
48     }
49     keymaster_error_t validate_algorithm_specific_new_key_params(
50         const AuthorizationSet& key_description) const override;
51 };
52 
53 class AesKey : public SymmetricKey {
54   public:
AesKey(KeymasterKeyBlob && key_material,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,const KeyFactory * key_factory)55     AesKey(KeymasterKeyBlob&& key_material, AuthorizationSet&& hw_enforced,
56            AuthorizationSet&& sw_enforced, const KeyFactory* key_factory)
57         : SymmetricKey(std::move(key_material), std::move(hw_enforced), std::move(sw_enforced),
58                        key_factory) {}
59 };
60 
61 }  // namespace keymaster
62