Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 20 | #include <keymasterV4_0/authorization_set.h> |
| 21 | #include <keymasterV4_0/keymaster_utils.h> |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | namespace vold { |
| 25 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 26 | using ::android::hardware::hidl_string; |
| 27 | using ::android::hardware::hidl_vec; |
| 28 | |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 29 | KeymasterOperation::~KeymasterOperation() { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 30 | if (mDevice) mDevice->abort(mOpHandle); |
Paul Crowley | 0323afd | 2016-03-15 17:04:39 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 33 | bool KeymasterOperation::updateCompletely(const char* input, size_t inputLen, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 34 | const std::function<void(const char*, size_t)> consumer) { |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 35 | uint32_t inputConsumed = 0; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 36 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 37 | km::ErrorCode km_error; |
| 38 | auto hidlCB = [&](km::ErrorCode ret, uint32_t inputConsumedDelta, |
| 39 | const hidl_vec<km::KeyParameter>& /*ignored*/, |
| 40 | const hidl_vec<uint8_t>& _output) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 41 | km_error = ret; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 42 | if (km_error != km::ErrorCode::OK) return; |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 43 | inputConsumed += inputConsumedDelta; |
| 44 | consumer(reinterpret_cast<const char*>(&_output[0]), _output.size()); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 47 | while (inputConsumed != inputLen) { |
| 48 | size_t toRead = static_cast<size_t>(inputLen - inputConsumed); |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 49 | auto inputBlob = km::support::blob2hidlVec( |
| 50 | reinterpret_cast<const uint8_t*>(&input[inputConsumed]), toRead); |
| 51 | // TODO(swillden): Need to handle getting a VerificationToken from the TEE if mDevice is |
| 52 | // StrongBox, so we can provide it here. The VerificationToken will need to be |
| 53 | // requested/retrieved during Keymaster::begin(). |
| 54 | auto error = mDevice->update(mOpHandle, hidl_vec<km::KeyParameter>(), inputBlob, |
| 55 | km::HardwareAuthToken(), km::VerificationToken(), hidlCB); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 56 | if (!error.isOk()) { |
| 57 | LOG(ERROR) << "update failed: " << error.description(); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 58 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 59 | return false; |
| 60 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 61 | if (km_error != km::ErrorCode::OK) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 62 | LOG(ERROR) << "update failed, code " << int32_t(km_error); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 63 | mDevice = nullptr; |
| 64 | return false; |
| 65 | } |
Pavel Grafov | e2e2d30 | 2017-08-01 17:15:53 +0100 | [diff] [blame] | 66 | if (inputConsumed > inputLen) { |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 67 | LOG(ERROR) << "update reported too much input consumed"; |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 68 | mDevice = nullptr; |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 69 | return false; |
| 70 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 75 | bool KeymasterOperation::finish(std::string* output) { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 76 | km::ErrorCode km_error; |
| 77 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<km::KeyParameter>& /*ignored*/, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 78 | const hidl_vec<uint8_t>& _output) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 79 | km_error = ret; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 80 | if (km_error != km::ErrorCode::OK) return; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 81 | if (output) output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size()); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 82 | }; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 83 | auto error = mDevice->finish(mOpHandle, hidl_vec<km::KeyParameter>(), hidl_vec<uint8_t>(), |
| 84 | hidl_vec<uint8_t>(), km::HardwareAuthToken(), |
| 85 | km::VerificationToken(), hidlCb); |
Paul Crowley | 13ffd8e | 2016-01-27 14:30:22 +0000 | [diff] [blame] | 86 | mDevice = nullptr; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 87 | if (!error.isOk()) { |
| 88 | LOG(ERROR) << "finish failed: " << error.description(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 89 | return false; |
| 90 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 91 | if (km_error != km::ErrorCode::OK) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 92 | LOG(ERROR) << "finish failed, code " << int32_t(km_error); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 93 | return false; |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 94 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | |
| 98 | Keymaster::Keymaster() { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 99 | auto devices = KmDevice::enumerateAvailableDevices(); |
| 100 | if (devices.empty()) return; |
| 101 | mDevice = std::move(devices[0]); |
| 102 | auto& version = mDevice->halVersion(); |
| 103 | LOG(INFO) << "Using " << version.keymasterName << " from " << version.authorName |
| 104 | << " for encryption. Security level: " << toString(version.securityLevel) |
| 105 | << ", HAL: " << mDevice->descriptor() << "/" << mDevice->instanceName(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 108 | bool Keymaster::generateKey(const km::AuthorizationSet& inParams, std::string* key) { |
| 109 | km::ErrorCode km_error; |
| 110 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& keyBlob, |
| 111 | const km::KeyCharacteristics& /*ignored*/) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 112 | km_error = ret; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 113 | if (km_error != km::ErrorCode::OK) return; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 114 | if (key) key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size()); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb); |
| 118 | if (!error.isOk()) { |
| 119 | LOG(ERROR) << "generate_key failed: " << error.description(); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 120 | return false; |
| 121 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 122 | if (km_error != km::ErrorCode::OK) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 123 | LOG(ERROR) << "generate_key failed, code " << int32_t(km_error); |
| 124 | return false; |
| 125 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
Paul Crowley | df528a7 | 2016-03-09 09:31:37 -0800 | [diff] [blame] | 129 | bool Keymaster::deleteKey(const std::string& key) { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 130 | auto keyBlob = km::support::blob2hidlVec(key); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 131 | auto error = mDevice->deleteKey(keyBlob); |
| 132 | if (!error.isOk()) { |
| 133 | LOG(ERROR) << "delete_key failed: " << error.description(); |
| 134 | return false; |
| 135 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 136 | if (error != km::ErrorCode::OK) { |
| 137 | LOG(ERROR) << "delete_key failed, code " << int32_t(km::ErrorCode(error)); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 143 | bool Keymaster::upgradeKey(const std::string& oldKey, const km::AuthorizationSet& inParams, |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 144 | std::string* newKey) { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 145 | auto oldKeyBlob = km::support::blob2hidlVec(oldKey); |
| 146 | km::ErrorCode km_error; |
| 147 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 148 | km_error = ret; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 149 | if (km_error != km::ErrorCode::OK) return; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 150 | if (newKey) |
| 151 | newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]), |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 152 | upgradedKeyBlob.size()); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 153 | }; |
| 154 | auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb); |
| 155 | if (!error.isOk()) { |
| 156 | LOG(ERROR) << "upgrade_key failed: " << error.description(); |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 157 | return false; |
| 158 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 159 | if (km_error != km::ErrorCode::OK) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 160 | LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error); |
| 161 | return false; |
| 162 | } |
Paul Crowley | dff8c72 | 2016-05-16 08:14:56 -0700 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 166 | KeymasterOperation Keymaster::begin(km::KeyPurpose purpose, const std::string& key, |
| 167 | const km::AuthorizationSet& inParams, |
| 168 | const km::HardwareAuthToken& authToken, |
| 169 | km::AuthorizationSet* outParams) { |
| 170 | auto keyBlob = km::support::blob2hidlVec(key); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 171 | uint64_t mOpHandle; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 172 | km::ErrorCode km_error; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 173 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 174 | auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<km::KeyParameter>& _outParams, |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 175 | uint64_t operationHandle) { |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 176 | km_error = ret; |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 177 | if (km_error != km::ErrorCode::OK) return; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 178 | if (outParams) *outParams = _outParams; |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 179 | mOpHandle = operationHandle; |
| 180 | }; |
| 181 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 182 | auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), authToken, hidlCb); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 183 | if (!error.isOk()) { |
| 184 | LOG(ERROR) << "begin failed: " << error.description(); |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 185 | return KeymasterOperation(km::ErrorCode::UNKNOWN_ERROR); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 186 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 187 | if (km_error != km::ErrorCode::OK) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 188 | LOG(ERROR) << "begin failed, code " << int32_t(km_error); |
Janis Danisevskis | 8e537b8 | 2016-10-26 14:27:10 +0100 | [diff] [blame] | 189 | return KeymasterOperation(km_error); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 190 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 191 | return KeymasterOperation(mDevice.get(), mOpHandle); |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 192 | } |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 193 | |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 194 | bool Keymaster::isSecure() { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 195 | return mDevice->halVersion().securityLevel != km::SecurityLevel::SOFTWARE; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 196 | } |
Paul Crowley | 1ef2558 | 2016-01-21 20:26:12 +0000 | [diff] [blame] | 197 | |
| 198 | } // namespace vold |
| 199 | } // namespace android |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 200 | |
| 201 | using namespace ::android::vold; |
| 202 | |
| 203 | int keymaster_compatibility_cryptfs_scrypt() { |
Janis Danisevskis | e7152c3 | 2017-03-08 11:02:30 -0800 | [diff] [blame] | 204 | Keymaster dev; |
| 205 | if (!dev) { |
| 206 | LOG(ERROR) << "Failed to initiate keymaster session"; |
| 207 | return -1; |
| 208 | } |
| 209 | return dev.isSecure(); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 212 | static bool write_string_to_buf(const std::string& towrite, uint8_t* buffer, uint32_t buffer_size, |
| 213 | uint32_t* out_size) { |
| 214 | if (!buffer || !out_size) { |
| 215 | LOG(ERROR) << "Missing target pointers"; |
| 216 | return false; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 217 | } |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 218 | *out_size = towrite.size(); |
| 219 | if (buffer_size < towrite.size()) { |
| 220 | LOG(ERROR) << "Buffer too small " << buffer_size << " < " << towrite.size(); |
| 221 | return false; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 222 | } |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 223 | memset(buffer, '\0', buffer_size); |
| 224 | std::copy(towrite.begin(), towrite.end(), buffer); |
| 225 | return true; |
| 226 | } |
| 227 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 228 | static km::AuthorizationSet keyParams(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 229 | uint32_t ratelimit) { |
| 230 | return km::AuthorizationSetBuilder() |
| 231 | .RsaSigningKey(rsa_key_size, rsa_exponent) |
| 232 | .NoDigestOrPadding() |
| 233 | .Authorization(km::TAG_BLOB_USAGE_REQUIREMENTS, km::KeyBlobUsageRequirements::STANDALONE) |
| 234 | .Authorization(km::TAG_NO_AUTH_REQUIRED) |
| 235 | .Authorization(km::TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit); |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 239 | uint32_t ratelimit, uint8_t* key_buffer, |
| 240 | uint32_t key_buffer_size, uint32_t* key_out_size) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 241 | if (key_out_size) { |
| 242 | *key_out_size = 0; |
| 243 | } |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 244 | Keymaster dev; |
| 245 | if (!dev) { |
| 246 | LOG(ERROR) << "Failed to initiate keymaster session"; |
| 247 | return -1; |
| 248 | } |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 249 | std::string key; |
| 250 | if (!dev.generateKey(keyParams(rsa_key_size, rsa_exponent, ratelimit), &key)) return -1; |
| 251 | if (!write_string_to_buf(key, key_buffer, key_buffer_size, key_out_size)) return -1; |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent, |
| 256 | uint32_t ratelimit, const uint8_t* key_blob, |
| 257 | size_t key_blob_size, uint8_t* key_buffer, |
| 258 | uint32_t key_buffer_size, uint32_t* key_out_size) { |
| 259 | if (key_out_size) { |
| 260 | *key_out_size = 0; |
| 261 | } |
| 262 | Keymaster dev; |
| 263 | if (!dev) { |
| 264 | LOG(ERROR) << "Failed to initiate keymaster session"; |
| 265 | return -1; |
| 266 | } |
| 267 | std::string old_key(reinterpret_cast<const char*>(key_blob), key_blob_size); |
| 268 | std::string new_key; |
| 269 | if (!dev.upgradeKey(old_key, keyParams(rsa_key_size, rsa_exponent, ratelimit), &new_key)) |
| 270 | return -1; |
| 271 | if (!write_string_to_buf(new_key, key_buffer, key_buffer_size, key_out_size)) return -1; |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt( |
| 276 | const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object, |
| 277 | const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size) { |
| 278 | Keymaster dev; |
| 279 | if (!dev) { |
| 280 | LOG(ERROR) << "Failed to initiate keymaster session"; |
| 281 | return KeymasterSignResult::error; |
| 282 | } |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 283 | if (!key_blob || !object || !signature_buffer || !signature_buffer_size) { |
| 284 | LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument"; |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 285 | return KeymasterSignResult::error; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 288 | km::AuthorizationSet outParams; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 289 | std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size); |
| 290 | std::string input(reinterpret_cast<const char*>(object), object_size); |
| 291 | std::string output; |
| 292 | KeymasterOperation op; |
| 293 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 294 | auto paramBuilder = km::AuthorizationSetBuilder().NoDigestOrPadding(); |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 295 | while (true) { |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 296 | op = dev.begin(km::KeyPurpose::SIGN, key, paramBuilder, km::HardwareAuthToken(), &outParams); |
| 297 | if (op.errorCode() == km::ErrorCode::KEY_RATE_LIMIT_EXCEEDED) { |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 298 | sleep(ratelimit); |
| 299 | continue; |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 300 | } else |
| 301 | break; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 304 | if (op.errorCode() == km::ErrorCode::KEY_REQUIRES_UPGRADE) { |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 305 | LOG(ERROR) << "Keymaster key requires upgrade"; |
| 306 | return KeymasterSignResult::upgrade; |
| 307 | } |
| 308 | |
Shawn Willden | 3535181 | 2018-01-22 09:08:32 -0700 | [diff] [blame^] | 309 | if (op.errorCode() != km::ErrorCode::OK) { |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 310 | LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode()); |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 311 | return KeymasterSignResult::error; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (!op.updateCompletely(input, &output)) { |
| 315 | LOG(ERROR) << "Error sending data to keymaster signature transaction: " |
Wei Wang | 4375f1b | 2017-02-24 17:43:01 -0800 | [diff] [blame] | 316 | << uint32_t(op.errorCode()); |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 317 | return KeymasterSignResult::error; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | if (!op.finish(&output)) { |
Shawn Willden | 785365b | 2018-01-20 09:37:36 -0700 | [diff] [blame] | 321 | LOG(ERROR) << "Error finalizing keymaster signature transaction: " |
| 322 | << int32_t(op.errorCode()); |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 323 | return KeymasterSignResult::error; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size())); |
| 327 | if (*signature_buffer == nullptr) { |
| 328 | LOG(ERROR) << "Error allocation buffer for keymaster signature"; |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 329 | return KeymasterSignResult::error; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 330 | } |
| 331 | *signature_buffer_size = output.size(); |
| 332 | std::copy(output.data(), output.data() + output.size(), *signature_buffer); |
Paul Crowley | 7347333 | 2017-11-21 15:43:51 -0800 | [diff] [blame] | 333 | return KeymasterSignResult::ok; |
Janis Danisevskis | 015ec30 | 2017-01-31 11:31:08 +0000 | [diff] [blame] | 334 | } |