• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "symmetric_key.h"
22 
23 namespace keymaster {
24 
25 const size_t kMinHmacLengthBits = 64;
26 const size_t kMinHmacKeyLengthBits = 64;
27 const size_t kMaxHmacKeyLengthBits = 2048;  // Some RFC test cases require >1024-bit keys
28 
29 class HmacKeyFactory : public SymmetricKeyFactory {
30   public:
HmacKeyFactory(const SoftwareKeyBlobMaker & blob_maker,const RandomSource & random_source)31     explicit HmacKeyFactory(const SoftwareKeyBlobMaker& blob_maker,
32                             const RandomSource& random_source)
33         : SymmetricKeyFactory(blob_maker, random_source) {}
34 
35     keymaster_error_t LoadKey(KeymasterKeyBlob&& key_material,
36                               const AuthorizationSet& additional_params,
37                               AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced,
38                               UniquePtr<Key>* key) const override;
39 
40     OperationFactory* GetOperationFactory(keymaster_purpose_t purpose) const override;
41 
42   private:
key_size_supported(size_t key_size_bits)43     bool key_size_supported(size_t key_size_bits) const override {
44         return key_size_bits > 0 && key_size_bits % 8 == 00 &&
45                key_size_bits >= kMinHmacKeyLengthBits && key_size_bits <= kMaxHmacKeyLengthBits;
46     }
47     keymaster_error_t validate_algorithm_specific_new_key_params(
48         const AuthorizationSet& key_description) const override;
49 };
50 
51 class HmacKey : public SymmetricKey {
52   public:
HmacKey(KeymasterKeyBlob && key_material,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,const KeyFactory * key_factory)53     HmacKey(KeymasterKeyBlob&& key_material, AuthorizationSet&& hw_enforced,
54             AuthorizationSet&& sw_enforced, const KeyFactory* key_factory)
55         : SymmetricKey(std::move(key_material), std::move(hw_enforced), std::move(sw_enforced),
56                        key_factory) {}
57 };
58 
59 }  // namespace keymaster
60