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