blob: dc6f1bc2d79de0612a41837511a5946cc3bab1b4 [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
Pavel Grafove2e2d302017-08-01 17:15:53 +010022#include "KeyBuffer.h"
23
Paul Crowley0323afd2016-03-15 17:04:39 -070024#include <memory>
Paul Crowley1ef25582016-01-21 20:26:12 +000025#include <string>
Paul Crowley0323afd2016-03-15 17:04:39 -070026#include <utility>
Paul Crowley1ef25582016-01-21 20:26:12 +000027
Janis Danisevskis8e537b82016-10-26 14:27:10 +010028#include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
Steven Moreland25e8b4b2017-05-01 12:45:32 -070029#include <android-base/macros.h>
Janis Danisevskis8e537b82016-10-26 14:27:10 +010030#include <keystore/authorization_set.h>
Paul Crowley1ef25582016-01-21 20:26:12 +000031
32namespace android {
33namespace vold {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010034using ::android::hardware::keymaster::V3_0::IKeymasterDevice;
35using ::keystore::ErrorCode;
36using ::keystore::KeyPurpose;
37using ::keystore::AuthorizationSet;
Paul Crowley1ef25582016-01-21 20:26:12 +000038
Janis Danisevskis8e537b82016-10-26 14:27:10 +010039// C++ wrappers to the Keymaster hidl interface.
Paul Crowley1ef25582016-01-21 20:26:12 +000040// This is tailored to the needs of KeyStorage, but could be extended to be
41// a more general interface.
42
Janis Danisevskis8e537b82016-10-26 14:27:10 +010043// Wrapper for a Keymaster operation handle representing an
Paul Crowley1ef25582016-01-21 20:26:12 +000044// ongoing Keymaster operation. Aborts the operation
45// in the destructor if it is unfinished. Methods log failures
46// to LOG(ERROR).
47class KeymasterOperation {
Paul Crowleydf528a72016-03-09 09:31:37 -080048 public:
Paul Crowley0323afd2016-03-15 17:04:39 -070049 ~KeymasterOperation();
Paul Crowley1ef25582016-01-21 20:26:12 +000050 // Is this instance valid? This is false if creation fails, and becomes
51 // false on finish or if an update fails.
Janis Danisevskis8e537b82016-10-26 14:27:10 +010052 explicit operator bool() { return mError == ErrorCode::OK; }
Wei Wang4375f1b2017-02-24 17:43:01 -080053 ErrorCode errorCode() { return mError; }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000054 // Call "update" repeatedly until all of the input is consumed, and
Paul Crowley1ef25582016-01-21 20:26:12 +000055 // concatenate the output. Return true on success.
Pavel Grafove2e2d302017-08-01 17:15:53 +010056 template <class TI, class TO>
57 bool updateCompletely(TI& input, TO* output) {
58 if (output) output->clear();
59 return updateCompletely(input.data(), input.size(), [&](const char* b, size_t n) {
60 if (output) std::copy(b, b+n, std::back_inserter(*output));
61 });
62 }
63
Paul Crowleydff8c722016-05-16 08:14:56 -070064 // Finish and write the output to this string, unless pointer is null.
65 bool finish(std::string* output);
Paul Crowley1ef25582016-01-21 20:26:12 +000066 // Move constructor
67 KeymasterOperation(KeymasterOperation&& rhs) {
Paul Crowley0323afd2016-03-15 17:04:39 -070068 mDevice = std::move(rhs.mDevice);
Paul Crowleydff8c722016-05-16 08:14:56 -070069 mOpHandle = std::move(rhs.mOpHandle);
70 mError = std::move(rhs.mError);
Paul Crowley1ef25582016-01-21 20:26:12 +000071 }
Paul Crowleydff8c722016-05-16 08:14:56 -070072 // Construct an object in an error state for error returns
Janis Danisevskis8e537b82016-10-26 14:27:10 +010073 KeymasterOperation()
Janis Danisevskis015ec302017-01-31 11:31:08 +000074 : mDevice{nullptr}, mOpHandle{0},
Janis Danisevskis8e537b82016-10-26 14:27:10 +010075 mError {ErrorCode::UNKNOWN_ERROR} {}
Janis Danisevskis015ec302017-01-31 11:31:08 +000076 // Move Assignment
77 KeymasterOperation& operator= (KeymasterOperation&& rhs) {
78 mDevice = std::move(rhs.mDevice);
79 mOpHandle = std::move(rhs.mOpHandle);
80 mError = std::move(rhs.mError);
81 rhs.mError = ErrorCode::UNKNOWN_ERROR;
82 rhs.mOpHandle = 0;
83 return *this;
84 }
Paul Crowleydf528a72016-03-09 09:31:37 -080085
86 private:
Janis Danisevskis8e537b82016-10-26 14:27:10 +010087 KeymasterOperation(const sp<IKeymasterDevice>& d, uint64_t h)
88 : mDevice{d}, mOpHandle{h}, mError {ErrorCode::OK} {}
89 KeymasterOperation(ErrorCode error)
90 : mDevice{nullptr}, mOpHandle{0},
Paul Crowleydff8c722016-05-16 08:14:56 -070091 mError {error} {}
Pavel Grafove2e2d302017-08-01 17:15:53 +010092
93 bool updateCompletely(const char* input, size_t inputLen,
94 const std::function<void(const char*, size_t)> consumer);
95
Janis Danisevskis8e537b82016-10-26 14:27:10 +010096 sp<IKeymasterDevice> mDevice;
97 uint64_t mOpHandle;
98 ErrorCode mError;
Paul Crowley1ef25582016-01-21 20:26:12 +000099 DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
100 friend class Keymaster;
101};
102
Paul Crowley0323afd2016-03-15 17:04:39 -0700103// Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not
104// part of one.
Paul Crowley1ef25582016-01-21 20:26:12 +0000105class Keymaster {
Paul Crowleydf528a72016-03-09 09:31:37 -0800106 public:
Paul Crowley1ef25582016-01-21 20:26:12 +0000107 Keymaster();
Paul Crowley1ef25582016-01-21 20:26:12 +0000108 // false if we failed to open the keymaster device.
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100109 explicit operator bool() { return mDevice.get() != nullptr; }
Paul Crowley1ef25582016-01-21 20:26:12 +0000110 // Generate a key in the keymaster from the given params.
Paul Crowleydf528a72016-03-09 09:31:37 -0800111 bool generateKey(const AuthorizationSet& inParams, std::string* key);
Paul Crowley1ef25582016-01-21 20:26:12 +0000112 // If the keymaster supports it, permanently delete a key.
Paul Crowleydf528a72016-03-09 09:31:37 -0800113 bool deleteKey(const std::string& key);
Paul Crowleydff8c722016-05-16 08:14:56 -0700114 // Replace stored key blob in response to KM_ERROR_KEY_REQUIRES_UPGRADE.
115 bool upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
116 std::string* newKey);
117 // Begin a new cryptographic operation, collecting output parameters if pointer is non-null
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100118 KeymasterOperation begin(KeyPurpose purpose, const std::string& key,
Paul Crowleydf528a72016-03-09 09:31:37 -0800119 const AuthorizationSet& inParams, AuthorizationSet* outParams);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000120 bool isSecure();
Paul Crowleydf528a72016-03-09 09:31:37 -0800121
122 private:
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100123 sp<hardware::keymaster::V3_0::IKeymasterDevice> mDevice;
Paul Crowley1ef25582016-01-21 20:26:12 +0000124 DISALLOW_COPY_AND_ASSIGN(Keymaster);
125};
126
Paul Crowley1ef25582016-01-21 20:26:12 +0000127} // namespace vold
128} // namespace android
129
Janis Danisevskis015ec302017-01-31 11:31:08 +0000130#endif // __cplusplus
131
132
133/*
134 * The following functions provide C bindings to keymaster services
135 * needed by cryptfs scrypt. The compatibility check checks whether
136 * the keymaster implementation is considered secure, i.e., TEE backed.
137 * The create_key function generates an RSA key for signing.
138 * The sign_object function signes an object with the given keymaster
139 * key.
140 */
141__BEGIN_DECLS
142
143int keymaster_compatibility_cryptfs_scrypt();
144int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size,
145 uint64_t rsa_exponent,
146 uint32_t ratelimit,
147 uint8_t* key_buffer,
148 uint32_t key_buffer_size,
149 uint32_t* key_out_size);
150
151int keymaster_sign_object_for_cryptfs_scrypt(const uint8_t* key_blob,
152 size_t key_blob_size,
153 uint32_t ratelimit,
154 const uint8_t* object,
155 const size_t object_size,
156 uint8_t** signature_buffer,
157 size_t* signature_buffer_size);
158
159__END_DECLS
160
Paul Crowley1ef25582016-01-21 20:26:12 +0000161#endif