blob: 786cdb559248cd3b493c2de620c8f784026f6128 [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>
Shawn Willden35f0f222020-01-16 13:21:42 -070020#include <keymasterV4_1/authorization_set.h>
21#include <keymasterV4_1/keymaster_utils.h>
Paul Crowley1ef25582016-01-21 20:26:12 +000022
23namespace android {
24namespace vold {
25
Shawn Willden35351812018-01-22 09:08:32 -070026using ::android::hardware::hidl_string;
27using ::android::hardware::hidl_vec;
Shawn Willdenc1903ad2018-03-30 17:06:14 -060028using ::android::hardware::keymaster::V4_0::SecurityLevel;
Shawn Willden35351812018-01-22 09:08:32 -070029
Paul Crowley0323afd2016-03-15 17:04:39 -070030KeymasterOperation::~KeymasterOperation() {
Shawn Willden35351812018-01-22 09:08:32 -070031 if (mDevice) mDevice->abort(mOpHandle);
Paul Crowley0323afd2016-03-15 17:04:39 -070032}
33
Pavel Grafove2e2d302017-08-01 17:15:53 +010034bool KeymasterOperation::updateCompletely(const char* input, size_t inputLen,
Shawn Willden785365b2018-01-20 09:37:36 -070035 const std::function<void(const char*, size_t)> consumer) {
Pavel Grafove2e2d302017-08-01 17:15:53 +010036 uint32_t inputConsumed = 0;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010037
Shawn Willden35351812018-01-22 09:08:32 -070038 km::ErrorCode km_error;
39 auto hidlCB = [&](km::ErrorCode ret, uint32_t inputConsumedDelta,
40 const hidl_vec<km::KeyParameter>& /*ignored*/,
41 const hidl_vec<uint8_t>& _output) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010042 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -070043 if (km_error != km::ErrorCode::OK) return;
Pavel Grafove2e2d302017-08-01 17:15:53 +010044 inputConsumed += inputConsumedDelta;
45 consumer(reinterpret_cast<const char*>(&_output[0]), _output.size());
Janis Danisevskis8e537b82016-10-26 14:27:10 +010046 };
47
Pavel Grafove2e2d302017-08-01 17:15:53 +010048 while (inputConsumed != inputLen) {
49 size_t toRead = static_cast<size_t>(inputLen - inputConsumed);
Shawn Willden35351812018-01-22 09:08:32 -070050 auto inputBlob = km::support::blob2hidlVec(
51 reinterpret_cast<const uint8_t*>(&input[inputConsumed]), toRead);
Shawn Willden35351812018-01-22 09:08:32 -070052 auto error = mDevice->update(mOpHandle, hidl_vec<km::KeyParameter>(), inputBlob,
53 km::HardwareAuthToken(), km::VerificationToken(), hidlCB);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010054 if (!error.isOk()) {
55 LOG(ERROR) << "update failed: " << error.description();
Paul Crowley13ffd8e2016-01-27 14:30:22 +000056 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000057 return false;
58 }
Shawn Willden35351812018-01-22 09:08:32 -070059 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000060 LOG(ERROR) << "update failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010061 mDevice = nullptr;
62 return false;
63 }
Pavel Grafove2e2d302017-08-01 17:15:53 +010064 if (inputConsumed > inputLen) {
Paul Crowley1ef25582016-01-21 20:26:12 +000065 LOG(ERROR) << "update reported too much input consumed";
Paul Crowley13ffd8e2016-01-27 14:30:22 +000066 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000067 return false;
68 }
Paul Crowley1ef25582016-01-21 20:26:12 +000069 }
70 return true;
71}
72
Paul Crowleydff8c722016-05-16 08:14:56 -070073bool KeymasterOperation::finish(std::string* output) {
Shawn Willden35351812018-01-22 09:08:32 -070074 km::ErrorCode km_error;
75 auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<km::KeyParameter>& /*ignored*/,
Shawn Willden785365b2018-01-20 09:37:36 -070076 const hidl_vec<uint8_t>& _output) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010077 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -070078 if (km_error != km::ErrorCode::OK) return;
Shawn Willden785365b2018-01-20 09:37:36 -070079 if (output) output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size());
Janis Danisevskis8e537b82016-10-26 14:27:10 +010080 };
Shawn Willden35351812018-01-22 09:08:32 -070081 auto error = mDevice->finish(mOpHandle, hidl_vec<km::KeyParameter>(), hidl_vec<uint8_t>(),
82 hidl_vec<uint8_t>(), km::HardwareAuthToken(),
83 km::VerificationToken(), hidlCb);
Paul Crowley13ffd8e2016-01-27 14:30:22 +000084 mDevice = nullptr;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010085 if (!error.isOk()) {
86 LOG(ERROR) << "finish failed: " << error.description();
Paul Crowley1ef25582016-01-21 20:26:12 +000087 return false;
88 }
Shawn Willden35351812018-01-22 09:08:32 -070089 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000090 LOG(ERROR) << "finish failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010091 return false;
Paul Crowleydff8c722016-05-16 08:14:56 -070092 }
Paul Crowley1ef25582016-01-21 20:26:12 +000093 return true;
94}
95
Shawn Willden28075362018-05-09 08:12:10 -060096/* static */ bool Keymaster::hmacKeyGenerated = false;
97
Paul Crowley1ef25582016-01-21 20:26:12 +000098Keymaster::Keymaster() {
Shawn Willden35351812018-01-22 09:08:32 -070099 auto devices = KmDevice::enumerateAvailableDevices();
Shawn Willden28075362018-05-09 08:12:10 -0600100 if (!hmacKeyGenerated) {
101 KmDevice::performHmacKeyAgreement(devices);
102 hmacKeyGenerated = true;
103 }
Shawn Willdenc1903ad2018-03-30 17:06:14 -0600104 for (auto& dev : devices) {
Shawn Willdene763ed22018-05-17 15:24:56 -0600105 // Do not use StrongBox for device encryption / credential encryption. If a security chip
106 // is present it will have Weaver, which already strengthens CE. We get no additional
107 // benefit from using StrongBox here, so skip it.
Shawn Willdenc1903ad2018-03-30 17:06:14 -0600108 if (dev->halVersion().securityLevel != SecurityLevel::STRONGBOX) {
109 mDevice = std::move(dev);
110 break;
111 }
112 }
113 if (!mDevice) return;
Shawn Willden35351812018-01-22 09:08:32 -0700114 auto& version = mDevice->halVersion();
115 LOG(INFO) << "Using " << version.keymasterName << " from " << version.authorName
116 << " for encryption. Security level: " << toString(version.securityLevel)
117 << ", HAL: " << mDevice->descriptor() << "/" << mDevice->instanceName();
Paul Crowley1ef25582016-01-21 20:26:12 +0000118}
119
Shawn Willden35351812018-01-22 09:08:32 -0700120bool Keymaster::generateKey(const km::AuthorizationSet& inParams, std::string* key) {
121 km::ErrorCode km_error;
122 auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& keyBlob,
123 const km::KeyCharacteristics& /*ignored*/) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100124 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -0700125 if (km_error != km::ErrorCode::OK) return;
Shawn Willden785365b2018-01-20 09:37:36 -0700126 if (key) key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size());
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100127 };
128
129 auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb);
130 if (!error.isOk()) {
131 LOG(ERROR) << "generate_key failed: " << error.description();
Paul Crowley1ef25582016-01-21 20:26:12 +0000132 return false;
133 }
Shawn Willden35351812018-01-22 09:08:32 -0700134 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100135 LOG(ERROR) << "generate_key failed, code " << int32_t(km_error);
136 return false;
137 }
Paul Crowley1ef25582016-01-21 20:26:12 +0000138 return true;
139}
140
Barani Muthukumaran3dfb0942020-02-03 13:06:45 -0800141bool Keymaster::exportKey(const KeyBuffer& kmKey, std::string* key) {
142 auto kmKeyBlob = km::support::blob2hidlVec(std::string(kmKey.data(), kmKey.size()));
143 km::ErrorCode km_error;
144 auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& exportedKeyBlob) {
145 km_error = ret;
146 if (km_error != km::ErrorCode::OK) return;
147 if (key)
148 key->assign(reinterpret_cast<const char*>(&exportedKeyBlob[0]), exportedKeyBlob.size());
149 };
150 auto error = mDevice->exportKey(km::KeyFormat::RAW, kmKeyBlob, {}, {}, hidlCb);
151 if (!error.isOk()) {
152 LOG(ERROR) << "export_key failed: " << error.description();
153 return false;
154 }
155 if (km_error != km::ErrorCode::OK) {
156 LOG(ERROR) << "export_key failed, code " << int32_t(km_error);
157 return false;
158 }
159 return true;
160}
161
Paul Crowleydf528a72016-03-09 09:31:37 -0800162bool Keymaster::deleteKey(const std::string& key) {
Shawn Willden35351812018-01-22 09:08:32 -0700163 auto keyBlob = km::support::blob2hidlVec(key);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100164 auto error = mDevice->deleteKey(keyBlob);
165 if (!error.isOk()) {
166 LOG(ERROR) << "delete_key failed: " << error.description();
167 return false;
168 }
Shawn Willden35351812018-01-22 09:08:32 -0700169 if (error != km::ErrorCode::OK) {
170 LOG(ERROR) << "delete_key failed, code " << int32_t(km::ErrorCode(error));
Paul Crowley1ef25582016-01-21 20:26:12 +0000171 return false;
172 }
173 return true;
174}
175
Shawn Willden35351812018-01-22 09:08:32 -0700176bool Keymaster::upgradeKey(const std::string& oldKey, const km::AuthorizationSet& inParams,
Paul Crowleydff8c722016-05-16 08:14:56 -0700177 std::string* newKey) {
Shawn Willden35351812018-01-22 09:08:32 -0700178 auto oldKeyBlob = km::support::blob2hidlVec(oldKey);
179 km::ErrorCode km_error;
180 auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100181 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -0700182 if (km_error != km::ErrorCode::OK) return;
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100183 if (newKey)
184 newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]),
Shawn Willden785365b2018-01-20 09:37:36 -0700185 upgradedKeyBlob.size());
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100186 };
187 auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb);
188 if (!error.isOk()) {
189 LOG(ERROR) << "upgrade_key failed: " << error.description();
Paul Crowleydff8c722016-05-16 08:14:56 -0700190 return false;
191 }
Shawn Willden35351812018-01-22 09:08:32 -0700192 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100193 LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error);
194 return false;
195 }
Paul Crowleydff8c722016-05-16 08:14:56 -0700196 return true;
197}
198
Shawn Willden35351812018-01-22 09:08:32 -0700199KeymasterOperation Keymaster::begin(km::KeyPurpose purpose, const std::string& key,
200 const km::AuthorizationSet& inParams,
201 const km::HardwareAuthToken& authToken,
202 km::AuthorizationSet* outParams) {
203 auto keyBlob = km::support::blob2hidlVec(key);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100204 uint64_t mOpHandle;
Shawn Willden35351812018-01-22 09:08:32 -0700205 km::ErrorCode km_error;
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100206
Shawn Willden35351812018-01-22 09:08:32 -0700207 auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<km::KeyParameter>& _outParams,
Shawn Willden785365b2018-01-20 09:37:36 -0700208 uint64_t operationHandle) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100209 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -0700210 if (km_error != km::ErrorCode::OK) return;
Shawn Willden785365b2018-01-20 09:37:36 -0700211 if (outParams) *outParams = _outParams;
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100212 mOpHandle = operationHandle;
213 };
214
Shawn Willden35351812018-01-22 09:08:32 -0700215 auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), authToken, hidlCb);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100216 if (!error.isOk()) {
217 LOG(ERROR) << "begin failed: " << error.description();
Shawn Willden35351812018-01-22 09:08:32 -0700218 return KeymasterOperation(km::ErrorCode::UNKNOWN_ERROR);
Paul Crowley1ef25582016-01-21 20:26:12 +0000219 }
Shawn Willden35351812018-01-22 09:08:32 -0700220 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000221 LOG(ERROR) << "begin failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100222 return KeymasterOperation(km_error);
Paul Crowley1ef25582016-01-21 20:26:12 +0000223 }
Shawn Willden35351812018-01-22 09:08:32 -0700224 return KeymasterOperation(mDevice.get(), mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000225}
Shawn Willden35351812018-01-22 09:08:32 -0700226
Janis Danisevskis015ec302017-01-31 11:31:08 +0000227bool Keymaster::isSecure() {
Shawn Willden35351812018-01-22 09:08:32 -0700228 return mDevice->halVersion().securityLevel != km::SecurityLevel::SOFTWARE;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000229}
Paul Crowley1ef25582016-01-21 20:26:12 +0000230
Shawn Willden2b1ff5a2020-01-16 14:08:36 -0700231void Keymaster::earlyBootEnded() {
Shawn Willden28eddbd2020-04-01 10:02:16 -0600232 auto devices = KmDevice::enumerateAvailableDevices();
233 for (auto& dev : devices) {
234 auto error = dev->earlyBootEnded();
235 if (!error.isOk()) {
236 LOG(ERROR) << "earlyBootEnded call failed: " << error.description() << " for "
237 << dev->halVersion().keymasterName;
238 }
239 km::V4_1_ErrorCode km_error = error;
240 if (km_error != km::V4_1_ErrorCode::OK && km_error != km::V4_1_ErrorCode::UNIMPLEMENTED) {
241 LOG(ERROR) << "Error reporting early boot ending to keymaster: "
242 << static_cast<int32_t>(km_error) << " for "
243 << dev->halVersion().keymasterName;
244 }
Shawn Willden2b1ff5a2020-01-16 14:08:36 -0700245 }
246}
247
Paul Crowley1ef25582016-01-21 20:26:12 +0000248} // namespace vold
249} // namespace android
Janis Danisevskis015ec302017-01-31 11:31:08 +0000250
251using namespace ::android::vold;
252
253int keymaster_compatibility_cryptfs_scrypt() {
Janis Danisevskise7152c32017-03-08 11:02:30 -0800254 Keymaster dev;
255 if (!dev) {
256 LOG(ERROR) << "Failed to initiate keymaster session";
257 return -1;
258 }
259 return dev.isSecure();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000260}
261
Paul Crowley73473332017-11-21 15:43:51 -0800262static bool write_string_to_buf(const std::string& towrite, uint8_t* buffer, uint32_t buffer_size,
263 uint32_t* out_size) {
264 if (!buffer || !out_size) {
265 LOG(ERROR) << "Missing target pointers";
266 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000267 }
Paul Crowley73473332017-11-21 15:43:51 -0800268 *out_size = towrite.size();
269 if (buffer_size < towrite.size()) {
270 LOG(ERROR) << "Buffer too small " << buffer_size << " < " << towrite.size();
271 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000272 }
Paul Crowley73473332017-11-21 15:43:51 -0800273 memset(buffer, '\0', buffer_size);
274 std::copy(towrite.begin(), towrite.end(), buffer);
275 return true;
276}
277
Shawn Willden35351812018-01-22 09:08:32 -0700278static km::AuthorizationSet keyParams(uint32_t rsa_key_size, uint64_t rsa_exponent,
279 uint32_t ratelimit) {
280 return km::AuthorizationSetBuilder()
281 .RsaSigningKey(rsa_key_size, rsa_exponent)
282 .NoDigestOrPadding()
283 .Authorization(km::TAG_BLOB_USAGE_REQUIREMENTS, km::KeyBlobUsageRequirements::STANDALONE)
284 .Authorization(km::TAG_NO_AUTH_REQUIRED)
285 .Authorization(km::TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit);
Paul Crowley73473332017-11-21 15:43:51 -0800286}
287
288int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
289 uint32_t ratelimit, uint8_t* key_buffer,
290 uint32_t key_buffer_size, uint32_t* key_out_size) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000291 if (key_out_size) {
292 *key_out_size = 0;
293 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000294 Keymaster dev;
295 if (!dev) {
296 LOG(ERROR) << "Failed to initiate keymaster session";
297 return -1;
298 }
Paul Crowley73473332017-11-21 15:43:51 -0800299 std::string key;
300 if (!dev.generateKey(keyParams(rsa_key_size, rsa_exponent, ratelimit), &key)) return -1;
301 if (!write_string_to_buf(key, key_buffer, key_buffer_size, key_out_size)) return -1;
302 return 0;
303}
304
305int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
306 uint32_t ratelimit, const uint8_t* key_blob,
307 size_t key_blob_size, uint8_t* key_buffer,
308 uint32_t key_buffer_size, uint32_t* key_out_size) {
309 if (key_out_size) {
310 *key_out_size = 0;
311 }
312 Keymaster dev;
313 if (!dev) {
314 LOG(ERROR) << "Failed to initiate keymaster session";
315 return -1;
316 }
317 std::string old_key(reinterpret_cast<const char*>(key_blob), key_blob_size);
318 std::string new_key;
319 if (!dev.upgradeKey(old_key, keyParams(rsa_key_size, rsa_exponent, ratelimit), &new_key))
320 return -1;
321 if (!write_string_to_buf(new_key, key_buffer, key_buffer_size, key_out_size)) return -1;
322 return 0;
323}
324
325KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt(
326 const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object,
327 const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size) {
328 Keymaster dev;
329 if (!dev) {
330 LOG(ERROR) << "Failed to initiate keymaster session";
331 return KeymasterSignResult::error;
332 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000333 if (!key_blob || !object || !signature_buffer || !signature_buffer_size) {
334 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
Paul Crowley73473332017-11-21 15:43:51 -0800335 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000336 }
337
Shawn Willden35351812018-01-22 09:08:32 -0700338 km::AuthorizationSet outParams;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000339 std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size);
340 std::string input(reinterpret_cast<const char*>(object), object_size);
341 std::string output;
342 KeymasterOperation op;
343
Shawn Willden35351812018-01-22 09:08:32 -0700344 auto paramBuilder = km::AuthorizationSetBuilder().NoDigestOrPadding();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000345 while (true) {
Shawn Willden35351812018-01-22 09:08:32 -0700346 op = dev.begin(km::KeyPurpose::SIGN, key, paramBuilder, km::HardwareAuthToken(), &outParams);
347 if (op.errorCode() == km::ErrorCode::KEY_RATE_LIMIT_EXCEEDED) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000348 sleep(ratelimit);
349 continue;
Shawn Willden785365b2018-01-20 09:37:36 -0700350 } else
351 break;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000352 }
353
Shawn Willden35351812018-01-22 09:08:32 -0700354 if (op.errorCode() == km::ErrorCode::KEY_REQUIRES_UPGRADE) {
Paul Crowley73473332017-11-21 15:43:51 -0800355 LOG(ERROR) << "Keymaster key requires upgrade";
356 return KeymasterSignResult::upgrade;
357 }
358
Shawn Willden35351812018-01-22 09:08:32 -0700359 if (op.errorCode() != km::ErrorCode::OK) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800360 LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800361 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000362 }
363
364 if (!op.updateCompletely(input, &output)) {
365 LOG(ERROR) << "Error sending data to keymaster signature transaction: "
Wei Wang4375f1b2017-02-24 17:43:01 -0800366 << uint32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800367 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000368 }
369
370 if (!op.finish(&output)) {
Shawn Willden785365b2018-01-20 09:37:36 -0700371 LOG(ERROR) << "Error finalizing keymaster signature transaction: "
372 << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800373 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000374 }
375
376 *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size()));
377 if (*signature_buffer == nullptr) {
378 LOG(ERROR) << "Error allocation buffer for keymaster signature";
Paul Crowley73473332017-11-21 15:43:51 -0800379 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000380 }
381 *signature_buffer_size = output.size();
382 std::copy(output.data(), output.data() + output.size(), *signature_buffer);
Paul Crowley73473332017-11-21 15:43:51 -0800383 return KeymasterSignResult::ok;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000384}