blob: 4888fe3a67bd76c9e97f5d33772b9a0eb6f8be18 [file] [log] [blame]
Paul Crowley1ef25582016-01-21 20:26:12 +00001/*
2 * Copyright (C) 2016 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.h"
18
19#include <android-base/logging.h>
20
21namespace android {
22namespace vold {
23
24bool KeymasterOperation::UpdateCompletely(
25 const std::string &input,
26 std::string &output) {
27 output.clear();
28 auto it = input.begin();
29 while (it != input.end()) {
30 size_t to_read = static_cast<size_t>(input.end() - it);
31 keymaster_blob_t input_blob {reinterpret_cast<const uint8_t *>(&*it), to_read};
32 keymaster_blob_t output_blob;
33 size_t input_consumed;
34 auto error = device->update(device, op_handle,
35 nullptr, &input_blob, &input_consumed, nullptr, &output_blob);
36 if (error != KM_ERROR_OK) {
37 LOG(ERROR) << "update failed, code " << error;
38 device = nullptr;
39 return false;
40 }
41 output.append(reinterpret_cast<const char *>(output_blob.data), output_blob.data_length);
42 free(const_cast<uint8_t *>(output_blob.data));
43 if (input_consumed > to_read) {
44 LOG(ERROR) << "update reported too much input consumed";
45 device = nullptr;
46 return false;
47 }
48 it += input_consumed;
49 }
50 return true;
51}
52
53bool KeymasterOperation::Finish() {
54 auto error = device->finish(device, op_handle,
55 nullptr, nullptr, nullptr, nullptr);
56 device = nullptr;
57 if (error != KM_ERROR_OK) {
58 LOG(ERROR) << "finish failed, code " << error;
59 return false;
60 }
61 return true;
62}
63
64bool KeymasterOperation::FinishWithOutput(std::string &output) {
65 keymaster_blob_t output_blob;
66 auto error = device->finish(device, op_handle,
67 nullptr, nullptr, nullptr, &output_blob);
68 device = nullptr;
69 if (error != KM_ERROR_OK) {
70 LOG(ERROR) << "finish failed, code " << error;
71 return false;
72 }
73 output.assign(reinterpret_cast<const char *>(output_blob.data), output_blob.data_length);
74 free(const_cast<uint8_t *>(output_blob.data));
75 return true;
76}
77
78Keymaster::Keymaster() {
79 device = nullptr;
80 const hw_module_t *module;
81 int ret = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &module);
82 if (ret != 0) {
83 LOG(ERROR) << "hw_get_module_by_class returned " << ret;
84 return;
85 }
86 // TODO: This will need to be updated to support keymaster2.
87 if (module->module_api_version != KEYMASTER_MODULE_API_VERSION_1_0) {
88 LOG(ERROR) << "module_api_version is " << module->module_api_version;
89 return;
90 }
91 ret = keymaster1_open(module, &device);
92 if (ret != 0) {
93 LOG(ERROR) << "keymaster1_open returned " << ret;
94 device = nullptr;
95 return;
96 }
97}
98
99bool Keymaster::GenerateKey(
100 const keymaster::AuthorizationSet &in_params,
101 std::string &key) {
102 keymaster_key_blob_t key_blob;
103 auto error = device->generate_key(device, &in_params, &key_blob, nullptr);
104 if (error != KM_ERROR_OK) {
105 LOG(ERROR) << "generate_key failed, code " << error;
106 return false;
107 }
108 key.assign(reinterpret_cast<const char *>(key_blob.key_material), key_blob.key_material_size);
109 return true;
110}
111
112bool Keymaster::DeleteKey(const std::string &key) {
113 if (device->delete_key == nullptr) return true;
114 keymaster_key_blob_t key_blob { reinterpret_cast<const uint8_t *>(key.data()), key.size() };
115 auto error = device->delete_key(device, &key_blob);
116 if (error != KM_ERROR_OK) {
117 LOG(ERROR) << "delete_key failed, code " << error;
118 return false;
119 }
120 return true;
121}
122
123KeymasterOperation Keymaster::Begin(
124 keymaster_purpose_t purpose,
125 const std::string &key,
126 const keymaster::AuthorizationSet &in_params,
127 keymaster::AuthorizationSet &out_params) {
128 keymaster_key_blob_t key_blob { reinterpret_cast<const uint8_t *>(key.data()), key.size() };
129 keymaster_operation_handle_t op_handle;
130 keymaster_key_param_set_t out_params_set;
131 auto error = device->begin(device, purpose,
132 &key_blob, &in_params, &out_params_set, &op_handle);
133 if (error != KM_ERROR_OK) {
134 LOG(ERROR) << "begin failed, code " << error;
135 return KeymasterOperation(nullptr, op_handle);
136 }
137 out_params.Clear();
138 out_params.push_back(out_params_set);
139 keymaster_free_param_set(&out_params_set);
140 return KeymasterOperation(device, op_handle);
141}
142
143KeymasterOperation Keymaster::Begin(
144 keymaster_purpose_t purpose,
145 const std::string &key,
146 const keymaster::AuthorizationSet &in_params) {
147 keymaster_key_blob_t key_blob { reinterpret_cast<const uint8_t *>(key.data()), key.size() };
148 keymaster_operation_handle_t op_handle;
149 auto error = device->begin(device, purpose,
150 &key_blob, &in_params, nullptr, &op_handle);
151 if (error != KM_ERROR_OK) {
152 LOG(ERROR) << "begin failed, code " << error;
153 return KeymasterOperation(nullptr, op_handle);
154 }
155 return KeymasterOperation(device, op_handle);
156}
157
158} // namespace vold
159} // namespace android