blob: abee9b23667fc8555436c5f5cb9b335a553cff09 [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
Paul Crowleydf528a72016-03-09 09:31:37 -0800141bool Keymaster::deleteKey(const std::string& key) {
Shawn Willden35351812018-01-22 09:08:32 -0700142 auto keyBlob = km::support::blob2hidlVec(key);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100143 auto error = mDevice->deleteKey(keyBlob);
144 if (!error.isOk()) {
145 LOG(ERROR) << "delete_key failed: " << error.description();
146 return false;
147 }
Shawn Willden35351812018-01-22 09:08:32 -0700148 if (error != km::ErrorCode::OK) {
149 LOG(ERROR) << "delete_key failed, code " << int32_t(km::ErrorCode(error));
Paul Crowley1ef25582016-01-21 20:26:12 +0000150 return false;
151 }
152 return true;
153}
154
Shawn Willden35351812018-01-22 09:08:32 -0700155bool Keymaster::upgradeKey(const std::string& oldKey, const km::AuthorizationSet& inParams,
Paul Crowleydff8c722016-05-16 08:14:56 -0700156 std::string* newKey) {
Shawn Willden35351812018-01-22 09:08:32 -0700157 auto oldKeyBlob = km::support::blob2hidlVec(oldKey);
158 km::ErrorCode km_error;
159 auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100160 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -0700161 if (km_error != km::ErrorCode::OK) return;
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100162 if (newKey)
163 newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]),
Shawn Willden785365b2018-01-20 09:37:36 -0700164 upgradedKeyBlob.size());
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100165 };
166 auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb);
167 if (!error.isOk()) {
168 LOG(ERROR) << "upgrade_key failed: " << error.description();
Paul Crowleydff8c722016-05-16 08:14:56 -0700169 return false;
170 }
Shawn Willden35351812018-01-22 09:08:32 -0700171 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100172 LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error);
173 return false;
174 }
Paul Crowleydff8c722016-05-16 08:14:56 -0700175 return true;
176}
177
Shawn Willden35351812018-01-22 09:08:32 -0700178KeymasterOperation Keymaster::begin(km::KeyPurpose purpose, const std::string& key,
179 const km::AuthorizationSet& inParams,
180 const km::HardwareAuthToken& authToken,
181 km::AuthorizationSet* outParams) {
182 auto keyBlob = km::support::blob2hidlVec(key);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100183 uint64_t mOpHandle;
Shawn Willden35351812018-01-22 09:08:32 -0700184 km::ErrorCode km_error;
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100185
Shawn Willden35351812018-01-22 09:08:32 -0700186 auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<km::KeyParameter>& _outParams,
Shawn Willden785365b2018-01-20 09:37:36 -0700187 uint64_t operationHandle) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100188 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -0700189 if (km_error != km::ErrorCode::OK) return;
Shawn Willden785365b2018-01-20 09:37:36 -0700190 if (outParams) *outParams = _outParams;
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100191 mOpHandle = operationHandle;
192 };
193
Shawn Willden35351812018-01-22 09:08:32 -0700194 auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), authToken, hidlCb);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100195 if (!error.isOk()) {
196 LOG(ERROR) << "begin failed: " << error.description();
Shawn Willden35351812018-01-22 09:08:32 -0700197 return KeymasterOperation(km::ErrorCode::UNKNOWN_ERROR);
Paul Crowley1ef25582016-01-21 20:26:12 +0000198 }
Shawn Willden35351812018-01-22 09:08:32 -0700199 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000200 LOG(ERROR) << "begin failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100201 return KeymasterOperation(km_error);
Paul Crowley1ef25582016-01-21 20:26:12 +0000202 }
Shawn Willden35351812018-01-22 09:08:32 -0700203 return KeymasterOperation(mDevice.get(), mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000204}
Shawn Willden35351812018-01-22 09:08:32 -0700205
Janis Danisevskis015ec302017-01-31 11:31:08 +0000206bool Keymaster::isSecure() {
Shawn Willden35351812018-01-22 09:08:32 -0700207 return mDevice->halVersion().securityLevel != km::SecurityLevel::SOFTWARE;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000208}
Paul Crowley1ef25582016-01-21 20:26:12 +0000209
Shawn Willden2b1ff5a2020-01-16 14:08:36 -0700210void Keymaster::earlyBootEnded() {
211 auto error = mDevice->earlyBootEnded();
212 if (!error.isOk()) {
213 LOG(ERROR) << "earlyBootEnded failed: " << error.description();
214 }
215 km::V4_1_ErrorCode km_error = error;
216 if (km_error != km::V4_1_ErrorCode::OK && km_error != km::V4_1_ErrorCode::UNIMPLEMENTED) {
217 LOG(ERROR) << "Error reporting early boot ending to keymaster: " << int32_t(km_error);
218 }
219}
220
Paul Crowley1ef25582016-01-21 20:26:12 +0000221} // namespace vold
222} // namespace android
Janis Danisevskis015ec302017-01-31 11:31:08 +0000223
224using namespace ::android::vold;
225
226int keymaster_compatibility_cryptfs_scrypt() {
Janis Danisevskise7152c32017-03-08 11:02:30 -0800227 Keymaster dev;
228 if (!dev) {
229 LOG(ERROR) << "Failed to initiate keymaster session";
230 return -1;
231 }
232 return dev.isSecure();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000233}
234
Paul Crowley73473332017-11-21 15:43:51 -0800235static bool write_string_to_buf(const std::string& towrite, uint8_t* buffer, uint32_t buffer_size,
236 uint32_t* out_size) {
237 if (!buffer || !out_size) {
238 LOG(ERROR) << "Missing target pointers";
239 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000240 }
Paul Crowley73473332017-11-21 15:43:51 -0800241 *out_size = towrite.size();
242 if (buffer_size < towrite.size()) {
243 LOG(ERROR) << "Buffer too small " << buffer_size << " < " << towrite.size();
244 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000245 }
Paul Crowley73473332017-11-21 15:43:51 -0800246 memset(buffer, '\0', buffer_size);
247 std::copy(towrite.begin(), towrite.end(), buffer);
248 return true;
249}
250
Shawn Willden35351812018-01-22 09:08:32 -0700251static km::AuthorizationSet keyParams(uint32_t rsa_key_size, uint64_t rsa_exponent,
252 uint32_t ratelimit) {
253 return km::AuthorizationSetBuilder()
254 .RsaSigningKey(rsa_key_size, rsa_exponent)
255 .NoDigestOrPadding()
256 .Authorization(km::TAG_BLOB_USAGE_REQUIREMENTS, km::KeyBlobUsageRequirements::STANDALONE)
257 .Authorization(km::TAG_NO_AUTH_REQUIRED)
258 .Authorization(km::TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit);
Paul Crowley73473332017-11-21 15:43:51 -0800259}
260
261int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
262 uint32_t ratelimit, uint8_t* key_buffer,
263 uint32_t key_buffer_size, uint32_t* key_out_size) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000264 if (key_out_size) {
265 *key_out_size = 0;
266 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000267 Keymaster dev;
268 if (!dev) {
269 LOG(ERROR) << "Failed to initiate keymaster session";
270 return -1;
271 }
Paul Crowley73473332017-11-21 15:43:51 -0800272 std::string key;
273 if (!dev.generateKey(keyParams(rsa_key_size, rsa_exponent, ratelimit), &key)) return -1;
274 if (!write_string_to_buf(key, key_buffer, key_buffer_size, key_out_size)) return -1;
275 return 0;
276}
277
278int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
279 uint32_t ratelimit, const uint8_t* key_blob,
280 size_t key_blob_size, uint8_t* key_buffer,
281 uint32_t key_buffer_size, uint32_t* key_out_size) {
282 if (key_out_size) {
283 *key_out_size = 0;
284 }
285 Keymaster dev;
286 if (!dev) {
287 LOG(ERROR) << "Failed to initiate keymaster session";
288 return -1;
289 }
290 std::string old_key(reinterpret_cast<const char*>(key_blob), key_blob_size);
291 std::string new_key;
292 if (!dev.upgradeKey(old_key, keyParams(rsa_key_size, rsa_exponent, ratelimit), &new_key))
293 return -1;
294 if (!write_string_to_buf(new_key, key_buffer, key_buffer_size, key_out_size)) return -1;
295 return 0;
296}
297
298KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt(
299 const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object,
300 const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size) {
301 Keymaster dev;
302 if (!dev) {
303 LOG(ERROR) << "Failed to initiate keymaster session";
304 return KeymasterSignResult::error;
305 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000306 if (!key_blob || !object || !signature_buffer || !signature_buffer_size) {
307 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
Paul Crowley73473332017-11-21 15:43:51 -0800308 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000309 }
310
Shawn Willden35351812018-01-22 09:08:32 -0700311 km::AuthorizationSet outParams;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000312 std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size);
313 std::string input(reinterpret_cast<const char*>(object), object_size);
314 std::string output;
315 KeymasterOperation op;
316
Shawn Willden35351812018-01-22 09:08:32 -0700317 auto paramBuilder = km::AuthorizationSetBuilder().NoDigestOrPadding();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000318 while (true) {
Shawn Willden35351812018-01-22 09:08:32 -0700319 op = dev.begin(km::KeyPurpose::SIGN, key, paramBuilder, km::HardwareAuthToken(), &outParams);
320 if (op.errorCode() == km::ErrorCode::KEY_RATE_LIMIT_EXCEEDED) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000321 sleep(ratelimit);
322 continue;
Shawn Willden785365b2018-01-20 09:37:36 -0700323 } else
324 break;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000325 }
326
Shawn Willden35351812018-01-22 09:08:32 -0700327 if (op.errorCode() == km::ErrorCode::KEY_REQUIRES_UPGRADE) {
Paul Crowley73473332017-11-21 15:43:51 -0800328 LOG(ERROR) << "Keymaster key requires upgrade";
329 return KeymasterSignResult::upgrade;
330 }
331
Shawn Willden35351812018-01-22 09:08:32 -0700332 if (op.errorCode() != km::ErrorCode::OK) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800333 LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800334 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000335 }
336
337 if (!op.updateCompletely(input, &output)) {
338 LOG(ERROR) << "Error sending data to keymaster signature transaction: "
Wei Wang4375f1b2017-02-24 17:43:01 -0800339 << uint32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800340 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000341 }
342
343 if (!op.finish(&output)) {
Shawn Willden785365b2018-01-20 09:37:36 -0700344 LOG(ERROR) << "Error finalizing keymaster signature transaction: "
345 << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800346 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000347 }
348
349 *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size()));
350 if (*signature_buffer == nullptr) {
351 LOG(ERROR) << "Error allocation buffer for keymaster signature";
Paul Crowley73473332017-11-21 15:43:51 -0800352 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000353 }
354 *signature_buffer_size = output.size();
355 std::copy(output.data(), output.data() + output.size(), *signature_buffer);
Paul Crowley73473332017-11-21 15:43:51 -0800356 return KeymasterSignResult::ok;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000357}