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 #include <keymaster/km_openssl/hmac_key.h>
18
19 #include <openssl/err.h>
20 #include <openssl/rand.h>
21
22 #include "hmac_operation.h"
23
24 namespace keymaster {
25
26 static HmacSignOperationFactory sign_factory;
27 static HmacVerifyOperationFactory verify_factory;
28
GetOperationFactory(keymaster_purpose_t purpose) const29 OperationFactory* HmacKeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
30 switch (purpose) {
31 case KM_PURPOSE_SIGN:
32 return &sign_factory;
33 case KM_PURPOSE_VERIFY:
34 return &verify_factory;
35 default:
36 return nullptr;
37 }
38 }
39
LoadKey(KeymasterKeyBlob && key_material,const AuthorizationSet &,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,UniquePtr<Key> * key) const40 keymaster_error_t HmacKeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
41 const AuthorizationSet& /* additional_params */,
42 AuthorizationSet&& hw_enforced,
43 AuthorizationSet&& sw_enforced,
44 UniquePtr<Key>* key) const {
45 if (!key) return KM_ERROR_OUTPUT_PARAMETER_NULL;
46
47 uint32_t min_mac_length;
48 if (!hw_enforced.GetTagValue(TAG_MIN_MAC_LENGTH, &min_mac_length) &&
49 !sw_enforced.GetTagValue(TAG_MIN_MAC_LENGTH, &min_mac_length)) {
50 LOG_E("HMAC key must have KM_TAG_MIN_MAC_LENGTH", 0);
51 return KM_ERROR_INVALID_KEY_BLOB;
52 }
53
54 key->reset(new (std::nothrow)
55 HmacKey(move(key_material), move(hw_enforced), move(sw_enforced), this));
56 if (!key->get()) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
57 return KM_ERROR_OK;
58 }
59
validate_algorithm_specific_new_key_params(const AuthorizationSet & key_description) const60 keymaster_error_t HmacKeyFactory::validate_algorithm_specific_new_key_params(
61 const AuthorizationSet& key_description) const {
62 uint32_t min_mac_length_bits;
63 if (!key_description.GetTagValue(TAG_MIN_MAC_LENGTH, &min_mac_length_bits))
64 return KM_ERROR_MISSING_MIN_MAC_LENGTH;
65
66 keymaster_digest_t digest;
67 if (!key_description.GetTagValue(TAG_DIGEST, &digest)) {
68 LOG_E("%d digests specified for HMAC key", key_description.GetTagCount(TAG_DIGEST));
69 return KM_ERROR_UNSUPPORTED_DIGEST;
70 }
71
72 size_t hash_size_bits = 0;
73 switch (digest) {
74 case KM_DIGEST_NONE:
75 return KM_ERROR_UNSUPPORTED_DIGEST;
76 case KM_DIGEST_MD5:
77 hash_size_bits = 128;
78 break;
79 case KM_DIGEST_SHA1:
80 hash_size_bits = 160;
81 break;
82 case KM_DIGEST_SHA_2_224:
83 hash_size_bits = 224;
84 break;
85 case KM_DIGEST_SHA_2_256:
86 hash_size_bits = 256;
87 break;
88 case KM_DIGEST_SHA_2_384:
89 hash_size_bits = 384;
90 break;
91 case KM_DIGEST_SHA_2_512:
92 hash_size_bits = 512;
93 break;
94 };
95
96 if (hash_size_bits == 0) {
97 // digest was not matched
98 return KM_ERROR_UNSUPPORTED_DIGEST;
99 }
100
101 if (min_mac_length_bits % 8 != 0 || min_mac_length_bits > hash_size_bits)
102 return KM_ERROR_UNSUPPORTED_MIN_MAC_LENGTH;
103
104 if (min_mac_length_bits < kMinHmacLengthBits) return KM_ERROR_UNSUPPORTED_MIN_MAC_LENGTH;
105
106 return KM_ERROR_OK;
107 }
108
109 } // namespace keymaster
110