2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "Keymaster.h"
19 #include <android-base/logging.h>
20 #include <keystore/keymaster_tags.h>
21 #include <keystore/authorization_set.h>
22 #include <keystore/keystore_hidl_support.h>
24 using namespace ::keystore;
29 KeymasterOperation::~KeymasterOperation() {
30 if (mDevice.get()) mDevice->abort(mOpHandle);
33 bool KeymasterOperation::updateCompletely(const std::string& input, std::string* output) {
36 auto it = input.begin();
37 uint32_t inputConsumed;
40 auto hidlCB = [&] (ErrorCode ret, uint32_t _inputConsumed,
41 const hidl_vec<KeyParameter>& /*ignored*/, const hidl_vec<uint8_t>& _output) {
43 if (km_error != ErrorCode::OK) return;
44 inputConsumed = _inputConsumed;
46 output->append(reinterpret_cast<const char*>(&_output[0]), _output.size());
49 while (it != input.end()) {
50 size_t toRead = static_cast<size_t>(input.end() - it);
51 auto inputBlob = blob2hidlVec(reinterpret_cast<const uint8_t*>(&*it), toRead);
52 auto error = mDevice->update(mOpHandle, hidl_vec<KeyParameter>(), inputBlob, hidlCB);
54 LOG(ERROR) << "update failed: " << error.description();
58 if (km_error != ErrorCode::OK) {
59 LOG(ERROR) << "update failed, code " << int32_t(km_error);
63 if (inputConsumed > toRead) {
64 LOG(ERROR) << "update reported too much input consumed";
73 bool KeymasterOperation::finish(std::string* output) {
75 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& /*ignored*/,
76 const hidl_vec<uint8_t>& _output) {
78 if (km_error != ErrorCode::OK) return;
80 output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size());
82 auto error = mDevice->finish(mOpHandle, hidl_vec<KeyParameter>(), hidl_vec<uint8_t>(),
83 hidl_vec<uint8_t>(), hidlCb);
86 LOG(ERROR) << "finish failed: " << error.description();
89 if (km_error != ErrorCode::OK) {
90 LOG(ERROR) << "finish failed, code " << int32_t(km_error);
96 Keymaster::Keymaster() {
97 mDevice = ::android::hardware::keymaster::V3_0::IKeymasterDevice::getService();
100 bool Keymaster::generateKey(const AuthorizationSet& inParams, std::string* key) {
102 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& keyBlob,
103 const KeyCharacteristics& /*ignored*/) {
105 if (km_error != ErrorCode::OK) return;
107 key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size());
110 auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb);
112 LOG(ERROR) << "generate_key failed: " << error.description();
115 if (km_error != ErrorCode::OK) {
116 LOG(ERROR) << "generate_key failed, code " << int32_t(km_error);
122 bool Keymaster::deleteKey(const std::string& key) {
123 auto keyBlob = blob2hidlVec(key);
124 auto error = mDevice->deleteKey(keyBlob);
126 LOG(ERROR) << "delete_key failed: " << error.description();
129 if (ErrorCode(error) != ErrorCode::OK) {
130 LOG(ERROR) << "delete_key failed, code " << uint32_t(ErrorCode(error));
136 bool Keymaster::upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
137 std::string* newKey) {
138 auto oldKeyBlob = blob2hidlVec(oldKey);
140 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) {
142 if (km_error != ErrorCode::OK) return;
144 newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]),
145 upgradedKeyBlob.size());
147 auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb);
149 LOG(ERROR) << "upgrade_key failed: " << error.description();
152 if (km_error != ErrorCode::OK) {
153 LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error);
159 KeymasterOperation Keymaster::begin(KeyPurpose purpose, const std::string& key,
160 const AuthorizationSet& inParams,
161 AuthorizationSet* outParams) {
162 auto keyBlob = blob2hidlVec(key);
166 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& _outParams,
167 uint64_t operationHandle) {
169 if (km_error != ErrorCode::OK) return;
171 *outParams = _outParams;
172 mOpHandle = operationHandle;
175 auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), hidlCb);
177 LOG(ERROR) << "begin failed: " << error.description();
178 return KeymasterOperation(ErrorCode::UNKNOWN_ERROR);
180 if (km_error != ErrorCode::OK) {
181 LOG(ERROR) << "begin failed, code " << int32_t(km_error);
182 return KeymasterOperation(km_error);
184 return KeymasterOperation(mDevice, mOpHandle);
186 bool Keymaster::isSecure() {
187 bool _isSecure = false;
188 auto rc = mDevice->getHardwareFeatures(
189 [&] (bool isSecure, bool, bool, bool) { _isSecure = isSecure; });
190 return rc.isOk() && _isSecure;
194 } // namespace android
196 using namespace ::android::vold;
198 int keymaster_compatibility_cryptfs_scrypt() {
199 return Keymaster().isSecure();
202 int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size,
203 uint64_t rsa_exponent,
206 uint32_t key_buffer_size,
207 uint32_t* key_out_size)
212 LOG(ERROR) << "Failed to initiate keymaster session";
215 if (!key_buffer || !key_out_size) {
216 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
223 auto paramBuilder = AuthorizationSetBuilder()
224 .Authorization(TAG_ALGORITHM, Algorithm::RSA)
225 .Authorization(TAG_KEY_SIZE, rsa_key_size)
226 .Authorization(TAG_RSA_PUBLIC_EXPONENT, rsa_exponent)
227 .Authorization(TAG_PURPOSE, KeyPurpose::SIGN)
228 .Authorization(TAG_PADDING, PaddingMode::NONE)
229 .Authorization(TAG_DIGEST, Digest::NONE)
230 .Authorization(TAG_BLOB_USAGE_REQUIREMENTS,
231 KeyBlobUsageRequirements::STANDALONE)
232 .Authorization(TAG_NO_AUTH_REQUIRED)
233 .Authorization(TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit);
235 if (!dev.generateKey(paramBuilder, &key)) {
240 *key_out_size = key.size();
243 if (key_buffer_size < key.size()) {
247 std::copy(key.data(), key.data() + key.size(), key_buffer);
251 int keymaster_sign_object_for_cryptfs_scrypt(const uint8_t* key_blob,
252 size_t key_blob_size,
254 const uint8_t* object,
255 const size_t object_size,
256 uint8_t** signature_buffer,
257 size_t* signature_buffer_size)
261 LOG(ERROR) << "Failed to initiate keymaster session";
264 if (!key_blob || !object || !signature_buffer || !signature_buffer_size) {
265 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
269 AuthorizationSet outParams;
270 std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size);
271 std::string input(reinterpret_cast<const char*>(object), object_size);
273 KeymasterOperation op;
275 auto paramBuilder = AuthorizationSetBuilder()
276 .Authorization(TAG_PADDING, PaddingMode::NONE)
277 .Authorization(TAG_DIGEST, Digest::NONE);
280 op = dev.begin(KeyPurpose::SIGN, key, paramBuilder, &outParams);
281 if (op.errorCode() == ErrorCode::KEY_RATE_LIMIT_EXCEEDED) {
287 if (op.errorCode() != ErrorCode::OK) {
288 LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode());
292 if (!op.updateCompletely(input, &output)) {
293 LOG(ERROR) << "Error sending data to keymaster signature transaction: "
294 << uint32_t(op.errorCode());
298 if (!op.finish(&output)) {
299 LOG(ERROR) << "Error finalizing keymaster signature transaction: " << int32_t(op.errorCode());
303 *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size()));
304 if (*signature_buffer == nullptr) {
305 LOG(ERROR) << "Error allocation buffer for keymaster signature";
308 *signature_buffer_size = output.size();
309 std::copy(output.data(), output.data() + output.size(), *signature_buffer);