blob: a3853f9511a72bdeddcd2229a76d8e9a86b6be8b [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 Willdenae8f06f2020-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
210} // namespace vold
211} // namespace android
Janis Danisevskis015ec302017-01-31 11:31:08 +0000212
213using namespace ::android::vold;
214
215int keymaster_compatibility_cryptfs_scrypt() {
Janis Danisevskise7152c32017-03-08 11:02:30 -0800216 Keymaster dev;
217 if (!dev) {
218 LOG(ERROR) << "Failed to initiate keymaster session";
219 return -1;
220 }
221 return dev.isSecure();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000222}
223
Paul Crowley73473332017-11-21 15:43:51 -0800224static bool write_string_to_buf(const std::string& towrite, uint8_t* buffer, uint32_t buffer_size,
225 uint32_t* out_size) {
226 if (!buffer || !out_size) {
227 LOG(ERROR) << "Missing target pointers";
228 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000229 }
Paul Crowley73473332017-11-21 15:43:51 -0800230 *out_size = towrite.size();
231 if (buffer_size < towrite.size()) {
232 LOG(ERROR) << "Buffer too small " << buffer_size << " < " << towrite.size();
233 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000234 }
Paul Crowley73473332017-11-21 15:43:51 -0800235 memset(buffer, '\0', buffer_size);
236 std::copy(towrite.begin(), towrite.end(), buffer);
237 return true;
238}
239
Shawn Willden35351812018-01-22 09:08:32 -0700240static km::AuthorizationSet keyParams(uint32_t rsa_key_size, uint64_t rsa_exponent,
241 uint32_t ratelimit) {
242 return km::AuthorizationSetBuilder()
243 .RsaSigningKey(rsa_key_size, rsa_exponent)
244 .NoDigestOrPadding()
245 .Authorization(km::TAG_BLOB_USAGE_REQUIREMENTS, km::KeyBlobUsageRequirements::STANDALONE)
246 .Authorization(km::TAG_NO_AUTH_REQUIRED)
247 .Authorization(km::TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit);
Paul Crowley73473332017-11-21 15:43:51 -0800248}
249
250int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
251 uint32_t ratelimit, uint8_t* key_buffer,
252 uint32_t key_buffer_size, uint32_t* key_out_size) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000253 if (key_out_size) {
254 *key_out_size = 0;
255 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000256 Keymaster dev;
257 if (!dev) {
258 LOG(ERROR) << "Failed to initiate keymaster session";
259 return -1;
260 }
Paul Crowley73473332017-11-21 15:43:51 -0800261 std::string key;
262 if (!dev.generateKey(keyParams(rsa_key_size, rsa_exponent, ratelimit), &key)) return -1;
263 if (!write_string_to_buf(key, key_buffer, key_buffer_size, key_out_size)) return -1;
264 return 0;
265}
266
267int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
268 uint32_t ratelimit, const uint8_t* key_blob,
269 size_t key_blob_size, uint8_t* key_buffer,
270 uint32_t key_buffer_size, uint32_t* key_out_size) {
271 if (key_out_size) {
272 *key_out_size = 0;
273 }
274 Keymaster dev;
275 if (!dev) {
276 LOG(ERROR) << "Failed to initiate keymaster session";
277 return -1;
278 }
279 std::string old_key(reinterpret_cast<const char*>(key_blob), key_blob_size);
280 std::string new_key;
281 if (!dev.upgradeKey(old_key, keyParams(rsa_key_size, rsa_exponent, ratelimit), &new_key))
282 return -1;
283 if (!write_string_to_buf(new_key, key_buffer, key_buffer_size, key_out_size)) return -1;
284 return 0;
285}
286
287KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt(
288 const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object,
289 const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size) {
290 Keymaster dev;
291 if (!dev) {
292 LOG(ERROR) << "Failed to initiate keymaster session";
293 return KeymasterSignResult::error;
294 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000295 if (!key_blob || !object || !signature_buffer || !signature_buffer_size) {
296 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
Paul Crowley73473332017-11-21 15:43:51 -0800297 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000298 }
299
Shawn Willden35351812018-01-22 09:08:32 -0700300 km::AuthorizationSet outParams;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000301 std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size);
302 std::string input(reinterpret_cast<const char*>(object), object_size);
303 std::string output;
304 KeymasterOperation op;
305
Shawn Willden35351812018-01-22 09:08:32 -0700306 auto paramBuilder = km::AuthorizationSetBuilder().NoDigestOrPadding();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000307 while (true) {
Shawn Willden35351812018-01-22 09:08:32 -0700308 op = dev.begin(km::KeyPurpose::SIGN, key, paramBuilder, km::HardwareAuthToken(), &outParams);
309 if (op.errorCode() == km::ErrorCode::KEY_RATE_LIMIT_EXCEEDED) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000310 sleep(ratelimit);
311 continue;
Shawn Willden785365b2018-01-20 09:37:36 -0700312 } else
313 break;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000314 }
315
Shawn Willden35351812018-01-22 09:08:32 -0700316 if (op.errorCode() == km::ErrorCode::KEY_REQUIRES_UPGRADE) {
Paul Crowley73473332017-11-21 15:43:51 -0800317 LOG(ERROR) << "Keymaster key requires upgrade";
318 return KeymasterSignResult::upgrade;
319 }
320
Shawn Willden35351812018-01-22 09:08:32 -0700321 if (op.errorCode() != km::ErrorCode::OK) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800322 LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800323 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000324 }
325
326 if (!op.updateCompletely(input, &output)) {
327 LOG(ERROR) << "Error sending data to keymaster signature transaction: "
Wei Wang4375f1b2017-02-24 17:43:01 -0800328 << uint32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800329 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000330 }
331
332 if (!op.finish(&output)) {
Shawn Willden785365b2018-01-20 09:37:36 -0700333 LOG(ERROR) << "Error finalizing keymaster signature transaction: "
334 << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800335 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000336 }
337
338 *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size()));
339 if (*signature_buffer == nullptr) {
340 LOG(ERROR) << "Error allocation buffer for keymaster signature";
Paul Crowley73473332017-11-21 15:43:51 -0800341 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000342 }
343 *signature_buffer_size = output.size();
344 std::copy(output.data(), output.data() + output.size(), *signature_buffer);
Paul Crowley73473332017-11-21 15:43:51 -0800345 return KeymasterSignResult::ok;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000346}