blob: 0fde8fa073b97fafcc1ef376f1133604ccd4f785 [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
Paul Crowley13ffd8e2016-01-27 14:30:22 +000024bool KeymasterOperation::updateCompletely(
Paul Crowley1ef25582016-01-21 20:26:12 +000025 const std::string &input,
26 std::string &output) {
27 output.clear();
28 auto it = input.begin();
29 while (it != input.end()) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +000030 size_t toRead = static_cast<size_t>(input.end() - it);
31 keymaster_blob_t inputBlob {reinterpret_cast<const uint8_t *>(&*it), toRead};
32 keymaster_blob_t outputBlob;
33 size_t inputConsumed;
34 auto error = mDevice->update(mDevice, mOpHandle,
35 nullptr, &inputBlob, &inputConsumed, nullptr, &outputBlob);
Paul Crowley1ef25582016-01-21 20:26:12 +000036 if (error != KM_ERROR_OK) {
37 LOG(ERROR) << "update failed, code " << error;
Paul Crowley13ffd8e2016-01-27 14:30:22 +000038 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000039 return false;
40 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000041 output.append(reinterpret_cast<const char *>(outputBlob.data), outputBlob.data_length);
42 free(const_cast<uint8_t *>(outputBlob.data));
43 if (inputConsumed > toRead) {
Paul Crowley1ef25582016-01-21 20:26:12 +000044 LOG(ERROR) << "update reported too much input consumed";
Paul Crowley13ffd8e2016-01-27 14:30:22 +000045 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000046 return false;
47 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000048 it += inputConsumed;
Paul Crowley1ef25582016-01-21 20:26:12 +000049 }
50 return true;
51}
52
Paul Crowley13ffd8e2016-01-27 14:30:22 +000053bool KeymasterOperation::finish() {
54 auto error = mDevice->finish(mDevice, mOpHandle,
Paul Crowley1ef25582016-01-21 20:26:12 +000055 nullptr, nullptr, nullptr, nullptr);
Paul Crowley13ffd8e2016-01-27 14:30:22 +000056 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000057 if (error != KM_ERROR_OK) {
58 LOG(ERROR) << "finish failed, code " << error;
59 return false;
60 }
61 return true;
62}
63
Paul Crowley13ffd8e2016-01-27 14:30:22 +000064bool KeymasterOperation::finishWithOutput(std::string &output) {
65 keymaster_blob_t outputBlob;
66 auto error = mDevice->finish(mDevice, mOpHandle,
67 nullptr, nullptr, nullptr, &outputBlob);
68 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000069 if (error != KM_ERROR_OK) {
70 LOG(ERROR) << "finish failed, code " << error;
71 return false;
72 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000073 output.assign(reinterpret_cast<const char *>(outputBlob.data), outputBlob.data_length);
74 free(const_cast<uint8_t *>(outputBlob.data));
Paul Crowley1ef25582016-01-21 20:26:12 +000075 return true;
76}
77
78Keymaster::Keymaster() {
Paul Crowley13ffd8e2016-01-27 14:30:22 +000079 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000080 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 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000091 ret = keymaster1_open(module, &mDevice);
Paul Crowley1ef25582016-01-21 20:26:12 +000092 if (ret != 0) {
93 LOG(ERROR) << "keymaster1_open returned " << ret;
Paul Crowley13ffd8e2016-01-27 14:30:22 +000094 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000095 return;
96 }
97}
98
Paul Crowley13ffd8e2016-01-27 14:30:22 +000099bool Keymaster::generateKey(
100 const keymaster::AuthorizationSet &inParams,
Paul Crowley1ef25582016-01-21 20:26:12 +0000101 std::string &key) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000102 keymaster_key_blob_t keyBlob;
103 auto error = mDevice->generate_key(mDevice, &inParams, &keyBlob, nullptr);
Paul Crowley1ef25582016-01-21 20:26:12 +0000104 if (error != KM_ERROR_OK) {
105 LOG(ERROR) << "generate_key failed, code " << error;
106 return false;
107 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000108 key.assign(reinterpret_cast<const char *>(keyBlob.key_material), keyBlob.key_material_size);
Paul Crowley1ef25582016-01-21 20:26:12 +0000109 return true;
110}
111
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000112bool Keymaster::deleteKey(const std::string &key) {
113 if (mDevice->delete_key == nullptr) return true;
114 keymaster_key_blob_t keyBlob { reinterpret_cast<const uint8_t *>(key.data()), key.size() };
115 auto error = mDevice->delete_key(mDevice, &keyBlob);
Paul Crowley1ef25582016-01-21 20:26:12 +0000116 if (error != KM_ERROR_OK) {
117 LOG(ERROR) << "delete_key failed, code " << error;
118 return false;
119 }
120 return true;
121}
122
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000123KeymasterOperation Keymaster::begin(
Paul Crowley1ef25582016-01-21 20:26:12 +0000124 keymaster_purpose_t purpose,
125 const std::string &key,
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000126 const keymaster::AuthorizationSet &inParams,
127 keymaster::AuthorizationSet &outParams) {
128 keymaster_key_blob_t keyBlob { reinterpret_cast<const uint8_t *>(key.data()), key.size() };
129 keymaster_operation_handle_t mOpHandle;
130 keymaster_key_param_set_t outParams_set;
131 auto error = mDevice->begin(mDevice, purpose,
132 &keyBlob, &inParams, &outParams_set, &mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000133 if (error != KM_ERROR_OK) {
134 LOG(ERROR) << "begin failed, code " << error;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000135 return KeymasterOperation(nullptr, mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000136 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000137 outParams.Clear();
138 outParams.push_back(outParams_set);
139 keymaster_free_param_set(&outParams_set);
140 return KeymasterOperation(mDevice, mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000141}
142
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000143KeymasterOperation Keymaster::begin(
Paul Crowley1ef25582016-01-21 20:26:12 +0000144 keymaster_purpose_t purpose,
145 const std::string &key,
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000146 const keymaster::AuthorizationSet &inParams) {
147 keymaster_key_blob_t keyBlob { reinterpret_cast<const uint8_t *>(key.data()), key.size() };
148 keymaster_operation_handle_t mOpHandle;
149 auto error = mDevice->begin(mDevice, purpose,
150 &keyBlob, &inParams, nullptr, &mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000151 if (error != KM_ERROR_OK) {
152 LOG(ERROR) << "begin failed, code " << error;
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000153 return KeymasterOperation(nullptr, mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000154 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000155 return KeymasterOperation(mDevice, mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000156}
157
158} // namespace vold
159} // namespace android