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/aes_key.h>
18
19 #include <assert.h>
20
21 #include <openssl/err.h>
22 #include <openssl/rand.h>
23
24 #include "aes_operation.h"
25
26 namespace keymaster {
27
28 static AesOperationFactory encrypt_factory(KM_PURPOSE_ENCRYPT);
29 static AesOperationFactory decrypt_factory(KM_PURPOSE_DECRYPT);
30
GetOperationFactory(keymaster_purpose_t purpose) const31 OperationFactory* AesKeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
32 switch (purpose) {
33 case KM_PURPOSE_ENCRYPT:
34 return &encrypt_factory;
35 case KM_PURPOSE_DECRYPT:
36 return &decrypt_factory;
37 default:
38 return nullptr;
39 }
40 }
41
LoadKey(KeymasterKeyBlob && key_material,const AuthorizationSet &,AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,UniquePtr<Key> * key) const42 keymaster_error_t AesKeyFactory::LoadKey(KeymasterKeyBlob&& key_material,
43 const AuthorizationSet& /* additional_params */,
44 AuthorizationSet&& hw_enforced,
45 AuthorizationSet&& sw_enforced,
46 UniquePtr<Key>* key) const {
47 if (!key) return KM_ERROR_OUTPUT_PARAMETER_NULL;
48
49 uint32_t min_mac_length = 0;
50 if (hw_enforced.Contains(TAG_BLOCK_MODE, KM_MODE_GCM) ||
51 sw_enforced.Contains(TAG_BLOCK_MODE, KM_MODE_GCM)) {
52
53 if (!hw_enforced.GetTagValue(TAG_MIN_MAC_LENGTH, &min_mac_length) &&
54 !sw_enforced.GetTagValue(TAG_MIN_MAC_LENGTH, &min_mac_length)) {
55
56 LOG_E("AES-GCM key must have KM_TAG_MIN_MAC_LENGTH", 0);
57 return KM_ERROR_INVALID_KEY_BLOB;
58 }
59 }
60
61 keymaster_error_t error = KM_ERROR_OK;
62 key->reset(new (std::nothrow)
63 AesKey(move(key_material), move(hw_enforced), move(sw_enforced), this));
64 if (!key->get()) error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
65 return error;
66 }
67
validate_algorithm_specific_new_key_params(const AuthorizationSet & key_description) const68 keymaster_error_t AesKeyFactory::validate_algorithm_specific_new_key_params(
69 const AuthorizationSet& key_description) const {
70 if (key_description.Contains(TAG_BLOCK_MODE, KM_MODE_GCM)) {
71 uint32_t min_tag_length;
72 if (!key_description.GetTagValue(TAG_MIN_MAC_LENGTH, &min_tag_length))
73 return KM_ERROR_MISSING_MIN_MAC_LENGTH;
74
75 if (min_tag_length % 8 != 0) return KM_ERROR_UNSUPPORTED_MIN_MAC_LENGTH;
76
77 if (min_tag_length < kMinGcmTagLength || min_tag_length > kMaxGcmTagLength)
78 return KM_ERROR_UNSUPPORTED_MIN_MAC_LENGTH;
79 } else {
80 // Not GCM
81 if (key_description.find(TAG_MIN_MAC_LENGTH) != -1) {
82 LOG_W("KM_TAG_MIN_MAC_LENGTH found for non AES-GCM key", 0);
83 return KM_ERROR_INVALID_TAG;
84 }
85 }
86
87 return KM_ERROR_OK;
88 }
89
90 } // namespace keymaster
91