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 #ifndef ANDROID_VOLD_KEYMASTER_H
18 #define ANDROID_VOLD_KEYMASTER_H
20 #include "KeyBuffer.h"
26 #include <android-base/macros.h>
27 #include <keymasterV4_0/Keymaster.h>
28 #include <keymasterV4_0/authorization_set.h>
33 namespace km = ::android::hardware::keymaster::V4_0;
34 using KmDevice = km::support::Keymaster;
36 // C++ wrappers to the Keymaster hidl interface.
37 // This is tailored to the needs of KeyStorage, but could be extended to be
38 // a more general interface.
40 // Wrapper for a Keymaster operation handle representing an
41 // ongoing Keymaster operation. Aborts the operation
42 // in the destructor if it is unfinished. Methods log failures
44 class KeymasterOperation {
46 ~KeymasterOperation();
47 // Is this instance valid? This is false if creation fails, and becomes
48 // false on finish or if an update fails.
49 explicit operator bool() { return mError == km::ErrorCode::OK; }
50 km::ErrorCode errorCode() { return mError; }
51 // Call "update" repeatedly until all of the input is consumed, and
52 // concatenate the output. Return true on success.
53 template <class TI, class TO>
54 bool updateCompletely(TI& input, TO* output) {
55 if (output) output->clear();
56 return updateCompletely(input.data(), input.size(), [&](const char* b, size_t n) {
57 if (output) std::copy(b, b + n, std::back_inserter(*output));
61 // Finish and write the output to this string, unless pointer is null.
62 bool finish(std::string* output);
64 KeymasterOperation(KeymasterOperation&& rhs) { *this = std::move(rhs); }
65 // Construct an object in an error state for error returns
66 KeymasterOperation() : mDevice{nullptr}, mOpHandle{0}, mError{km::ErrorCode::UNKNOWN_ERROR} {}
68 KeymasterOperation& operator=(KeymasterOperation&& rhs) {
69 mDevice = rhs.mDevice;
70 rhs.mDevice = nullptr;
72 mOpHandle = rhs.mOpHandle;
76 rhs.mError = km::ErrorCode::UNKNOWN_ERROR;
82 KeymasterOperation(KmDevice* d, uint64_t h)
83 : mDevice{d}, mOpHandle{h}, mError{km::ErrorCode::OK} {}
84 KeymasterOperation(km::ErrorCode error) : mDevice{nullptr}, mOpHandle{0}, mError{error} {}
86 bool updateCompletely(const char* input, size_t inputLen,
87 const std::function<void(const char*, size_t)> consumer);
92 DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
93 friend class Keymaster;
96 // Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not
101 // false if we failed to open the keymaster device.
102 explicit operator bool() { return mDevice.get() != nullptr; }
103 // Generate a key in the keymaster from the given params.
104 bool generateKey(const km::AuthorizationSet& inParams, std::string* key);
105 // If the keymaster supports it, permanently delete a key.
106 bool deleteKey(const std::string& key);
107 // Replace stored key blob in response to KM_ERROR_KEY_REQUIRES_UPGRADE.
108 bool upgradeKey(const std::string& oldKey, const km::AuthorizationSet& inParams,
109 std::string* newKey);
110 // Begin a new cryptographic operation, collecting output parameters if pointer is non-null
111 KeymasterOperation begin(km::KeyPurpose purpose, const std::string& key,
112 const km::AuthorizationSet& inParams,
113 const km::HardwareAuthToken& authToken,
114 km::AuthorizationSet* outParams);
118 std::unique_ptr<KmDevice> mDevice;
119 DISALLOW_COPY_AND_ASSIGN(Keymaster);
120 static bool hmacKeyGenerated;
124 } // namespace android
126 // FIXME no longer needed now cryptfs is in C++.
129 * The following functions provide C bindings to keymaster services
130 * needed by cryptfs scrypt. The compatibility check checks whether
131 * the keymaster implementation is considered secure, i.e., TEE backed.
132 * The create_key function generates an RSA key for signing.
133 * The sign_object function signes an object with the given keymaster
137 /* Return values for keymaster_sign_object_for_cryptfs_scrypt */
139 enum class KeymasterSignResult {
145 int keymaster_compatibility_cryptfs_scrypt();
146 int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
147 uint32_t ratelimit, uint8_t* key_buffer,
148 uint32_t key_buffer_size, uint32_t* key_out_size);
150 int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
151 uint32_t ratelimit, const uint8_t* key_blob,
152 size_t key_blob_size, uint8_t* key_buffer,
153 uint32_t key_buffer_size, uint32_t* key_out_size);
155 KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt(
156 const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object,
157 const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size);