blob: 4bc0df7b119ef194f582e8cc094feb6a360e70e2 [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
Paul Crowley0323afd2016-03-15 17:04:39 -070017#ifndef ANDROID_VOLD_KEYMASTER_H
18#define ANDROID_VOLD_KEYMASTER_H
Paul Crowley1ef25582016-01-21 20:26:12 +000019
Janis Danisevskis015ec302017-01-31 11:31:08 +000020#ifdef __cplusplus
21
Paul Crowley0323afd2016-03-15 17:04:39 -070022#include <memory>
Paul Crowley1ef25582016-01-21 20:26:12 +000023#include <string>
Paul Crowley0323afd2016-03-15 17:04:39 -070024#include <utility>
Paul Crowley1ef25582016-01-21 20:26:12 +000025
Janis Danisevskis8e537b82016-10-26 14:27:10 +010026#include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
Steven Moreland25e8b4b2017-05-01 12:45:32 -070027#include <android-base/macros.h>
Janis Danisevskis8e537b82016-10-26 14:27:10 +010028#include <keystore/authorization_set.h>
Paul Crowley1ef25582016-01-21 20:26:12 +000029
30namespace android {
31namespace vold {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010032using ::android::hardware::keymaster::V3_0::IKeymasterDevice;
33using ::keystore::ErrorCode;
34using ::keystore::KeyPurpose;
35using ::keystore::AuthorizationSet;
Paul Crowley1ef25582016-01-21 20:26:12 +000036
Janis Danisevskis8e537b82016-10-26 14:27:10 +010037// C++ wrappers to the Keymaster hidl interface.
Paul Crowley1ef25582016-01-21 20:26:12 +000038// This is tailored to the needs of KeyStorage, but could be extended to be
39// a more general interface.
40
Janis Danisevskis8e537b82016-10-26 14:27:10 +010041// Wrapper for a Keymaster operation handle representing an
Paul Crowley1ef25582016-01-21 20:26:12 +000042// ongoing Keymaster operation. Aborts the operation
43// in the destructor if it is unfinished. Methods log failures
44// to LOG(ERROR).
45class KeymasterOperation {
Paul Crowleydf528a72016-03-09 09:31:37 -080046 public:
Paul Crowley0323afd2016-03-15 17:04:39 -070047 ~KeymasterOperation();
Paul Crowley1ef25582016-01-21 20:26:12 +000048 // Is this instance valid? This is false if creation fails, and becomes
49 // false on finish or if an update fails.
Janis Danisevskis8e537b82016-10-26 14:27:10 +010050 explicit operator bool() { return mError == ErrorCode::OK; }
Wei Wang4375f1b2017-02-24 17:43:01 -080051 ErrorCode errorCode() { return mError; }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000052 // Call "update" repeatedly until all of the input is consumed, and
Paul Crowley1ef25582016-01-21 20:26:12 +000053 // concatenate the output. Return true on success.
Paul Crowleydf528a72016-03-09 09:31:37 -080054 bool updateCompletely(const std::string& input, std::string* output);
Paul Crowleydff8c722016-05-16 08:14:56 -070055 // Finish and write the output to this string, unless pointer is null.
56 bool finish(std::string* output);
Paul Crowley1ef25582016-01-21 20:26:12 +000057 // Move constructor
58 KeymasterOperation(KeymasterOperation&& rhs) {
Paul Crowley0323afd2016-03-15 17:04:39 -070059 mDevice = std::move(rhs.mDevice);
Paul Crowleydff8c722016-05-16 08:14:56 -070060 mOpHandle = std::move(rhs.mOpHandle);
61 mError = std::move(rhs.mError);
Paul Crowley1ef25582016-01-21 20:26:12 +000062 }
Paul Crowleydff8c722016-05-16 08:14:56 -070063 // Construct an object in an error state for error returns
Janis Danisevskis8e537b82016-10-26 14:27:10 +010064 KeymasterOperation()
Janis Danisevskis015ec302017-01-31 11:31:08 +000065 : mDevice{nullptr}, mOpHandle{0},
Janis Danisevskis8e537b82016-10-26 14:27:10 +010066 mError {ErrorCode::UNKNOWN_ERROR} {}
Janis Danisevskis015ec302017-01-31 11:31:08 +000067 // Move Assignment
68 KeymasterOperation& operator= (KeymasterOperation&& rhs) {
69 mDevice = std::move(rhs.mDevice);
70 mOpHandle = std::move(rhs.mOpHandle);
71 mError = std::move(rhs.mError);
72 rhs.mError = ErrorCode::UNKNOWN_ERROR;
73 rhs.mOpHandle = 0;
74 return *this;
75 }
Paul Crowleydf528a72016-03-09 09:31:37 -080076
77 private:
Janis Danisevskis8e537b82016-10-26 14:27:10 +010078 KeymasterOperation(const sp<IKeymasterDevice>& d, uint64_t h)
79 : mDevice{d}, mOpHandle{h}, mError {ErrorCode::OK} {}
80 KeymasterOperation(ErrorCode error)
81 : mDevice{nullptr}, mOpHandle{0},
Paul Crowleydff8c722016-05-16 08:14:56 -070082 mError {error} {}
Janis Danisevskis8e537b82016-10-26 14:27:10 +010083 sp<IKeymasterDevice> mDevice;
84 uint64_t mOpHandle;
85 ErrorCode mError;
Paul Crowley1ef25582016-01-21 20:26:12 +000086 DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
87 friend class Keymaster;
88};
89
Paul Crowley0323afd2016-03-15 17:04:39 -070090// Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not
91// part of one.
Paul Crowley1ef25582016-01-21 20:26:12 +000092class Keymaster {
Paul Crowleydf528a72016-03-09 09:31:37 -080093 public:
Paul Crowley1ef25582016-01-21 20:26:12 +000094 Keymaster();
Paul Crowley1ef25582016-01-21 20:26:12 +000095 // false if we failed to open the keymaster device.
Janis Danisevskis8e537b82016-10-26 14:27:10 +010096 explicit operator bool() { return mDevice.get() != nullptr; }
Paul Crowley1ef25582016-01-21 20:26:12 +000097 // Generate a key in the keymaster from the given params.
Paul Crowleydf528a72016-03-09 09:31:37 -080098 bool generateKey(const AuthorizationSet& inParams, std::string* key);
Paul Crowley1ef25582016-01-21 20:26:12 +000099 // If the keymaster supports it, permanently delete a key.
Paul Crowleydf528a72016-03-09 09:31:37 -0800100 bool deleteKey(const std::string& key);
Paul Crowleydff8c722016-05-16 08:14:56 -0700101 // Replace stored key blob in response to KM_ERROR_KEY_REQUIRES_UPGRADE.
102 bool upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
103 std::string* newKey);
104 // Begin a new cryptographic operation, collecting output parameters if pointer is non-null
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100105 KeymasterOperation begin(KeyPurpose purpose, const std::string& key,
Paul Crowleydf528a72016-03-09 09:31:37 -0800106 const AuthorizationSet& inParams, AuthorizationSet* outParams);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000107 bool isSecure();
Paul Crowleydf528a72016-03-09 09:31:37 -0800108
109 private:
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100110 sp<hardware::keymaster::V3_0::IKeymasterDevice> mDevice;
Paul Crowley1ef25582016-01-21 20:26:12 +0000111 DISALLOW_COPY_AND_ASSIGN(Keymaster);
112};
113
Paul Crowley1ef25582016-01-21 20:26:12 +0000114} // namespace vold
115} // namespace android
116
Janis Danisevskis015ec302017-01-31 11:31:08 +0000117#endif // __cplusplus
118
119
120/*
121 * The following functions provide C bindings to keymaster services
122 * needed by cryptfs scrypt. The compatibility check checks whether
123 * the keymaster implementation is considered secure, i.e., TEE backed.
124 * The create_key function generates an RSA key for signing.
125 * The sign_object function signes an object with the given keymaster
126 * key.
127 */
128__BEGIN_DECLS
129
130int keymaster_compatibility_cryptfs_scrypt();
131int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size,
132 uint64_t rsa_exponent,
133 uint32_t ratelimit,
134 uint8_t* key_buffer,
135 uint32_t key_buffer_size,
136 uint32_t* key_out_size);
137
138int keymaster_sign_object_for_cryptfs_scrypt(const uint8_t* key_blob,
139 size_t key_blob_size,
140 uint32_t ratelimit,
141 const uint8_t* object,
142 const size_t object_size,
143 uint8_t** signature_buffer,
144 size_t* signature_buffer_size);
145
146__END_DECLS
147
Paul Crowley1ef25582016-01-21 20:26:12 +0000148#endif