blob: 92ea66e08bd94fffcde0f2527130b03e2ce943c0 [file] [log] [blame]
Shawn Willden19fca882015-01-22 16:35:30 -07001/*
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
Shawn Willden95e13822014-12-15 16:12:16 -070017#include "aes_key.h"
18
Shawn Willden907c3012014-12-08 15:51:55 -070019#include <assert.h>
20
Shawn Willden0f906ec2015-06-20 09:16:30 -060021#include <new>
22
Shawn Willden19fca882015-01-22 16:35:30 -070023#include <openssl/err.h>
24#include <openssl/rand.h>
25
Shawn Willden907c3012014-12-08 15:51:55 -070026#include "aes_operation.h"
Shawn Willden19fca882015-01-22 16:35:30 -070027
28namespace keymaster {
29
Shawn Willden06298102015-05-25 23:12:48 -060030AesEncryptionOperationFactory encrypt_factory;
31AesDecryptionOperationFactory decrypt_factory;
32
33OperationFactory* AesKeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
34 switch (purpose) {
35 case KM_PURPOSE_ENCRYPT:
36 return &encrypt_factory;
37 case KM_PURPOSE_DECRYPT:
38 return &decrypt_factory;
39 default:
40 return nullptr;
41 }
42}
43
Shawn Willden0cb69422015-05-26 08:31:37 -060044keymaster_error_t AesKeyFactory::LoadKey(const KeymasterKeyBlob& key_material,
45 const AuthorizationSet& hw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -060046 const AuthorizationSet& sw_enforced,
47 UniquePtr<Key>* key) const {
Shawn Willden0cb69422015-05-26 08:31:37 -060048 if (!key)
49 return KM_ERROR_OUTPUT_PARAMETER_NULL;
Shawn Willdena278f612014-12-23 11:22:21 -070050
Shawn Willden33ab0382015-07-08 08:47:25 -060051 uint32_t min_mac_length = 0;
52 if (hw_enforced.Contains(TAG_BLOCK_MODE, KM_MODE_GCM) ||
53 sw_enforced.Contains(TAG_BLOCK_MODE, KM_MODE_GCM)) {
54
55 if (!hw_enforced.GetTagValue(TAG_MIN_MAC_LENGTH, &min_mac_length) &&
56 !sw_enforced.GetTagValue(TAG_MIN_MAC_LENGTH, &min_mac_length)) {
57
58 LOG_E("AES-GCM key must have KM_TAG_MIN_MAC_LENGTH", 0);
59 return KM_ERROR_INVALID_KEY_BLOB;
60 }
61 }
62
Shawn Willden0cb69422015-05-26 08:31:37 -060063 keymaster_error_t error = KM_ERROR_OK;
Shawn Willden0f906ec2015-06-20 09:16:30 -060064 key->reset(new (std::nothrow) AesKey(key_material, hw_enforced, sw_enforced, &error));
Shawn Willden0cb69422015-05-26 08:31:37 -060065 if (!key->get())
66 error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
67 return error;
Shawn Willdenf9239632015-05-12 06:57:37 -060068}
Shawn Willdena278f612014-12-23 11:22:21 -070069
Shawn Willden33ab0382015-07-08 08:47:25 -060070keymaster_error_t AesKeyFactory::validate_algorithm_specific_new_key_params(
71 const AuthorizationSet& key_description) const {
72 if (key_description.Contains(TAG_BLOCK_MODE, KM_MODE_GCM)) {
73 uint32_t min_tag_length;
74 if (!key_description.GetTagValue(TAG_MIN_MAC_LENGTH, &min_tag_length))
75 return KM_ERROR_MISSING_MIN_MAC_LENGTH;
76
77 if (min_tag_length % 8 != 0)
78 return KM_ERROR_UNSUPPORTED_MIN_MAC_LENGTH;
79
80 if (min_tag_length < kMinGcmTagLength || min_tag_length > kMaxGcmTagLength)
81 return KM_ERROR_UNSUPPORTED_MIN_MAC_LENGTH;
82 } else {
83 // Not GCM
84 if (key_description.find(TAG_MIN_MAC_LENGTH) != -1) {
85 LOG_W("KM_TAG_MIN_MAC_LENGTH found for non AES-GCM key", 0);
86 return KM_ERROR_INVALID_TAG;
87 }
88 }
89
90 return KM_ERROR_OK;
91}
92
Shawn Willden19fca882015-01-22 16:35:30 -070093} // namespace keymaster