blob: 0bda8cd629b04508d5e14e8ac7e242d3de063dc2 [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
Pavel Grafove2e2d302017-08-01 17:15:53 +010020#include "KeyBuffer.h"
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
Steven Moreland25e8b4b2017-05-01 12:45:32 -070026#include <android-base/macros.h>
Shawn Willden35351812018-01-22 09:08:32 -070027#include <keymasterV4_0/Keymaster.h>
28#include <keymasterV4_0/authorization_set.h>
Paul Crowley1ef25582016-01-21 20:26:12 +000029
30namespace android {
31namespace vold {
Shawn Willden35351812018-01-22 09:08:32 -070032
33namespace km = ::android::hardware::keymaster::V4_0;
34using KmDevice = km::support::Keymaster;
Paul Crowley1ef25582016-01-21 20:26:12 +000035
Janis Danisevskis8e537b82016-10-26 14:27:10 +010036// C++ wrappers to the Keymaster hidl interface.
Paul Crowley1ef25582016-01-21 20:26:12 +000037// This is tailored to the needs of KeyStorage, but could be extended to be
38// a more general interface.
39
Janis Danisevskis8e537b82016-10-26 14:27:10 +010040// Wrapper for a Keymaster operation handle representing an
Paul Crowley1ef25582016-01-21 20:26:12 +000041// ongoing Keymaster operation. Aborts the operation
42// in the destructor if it is unfinished. Methods log failures
43// to LOG(ERROR).
44class KeymasterOperation {
Paul Crowleydf528a72016-03-09 09:31:37 -080045 public:
Paul Crowley0323afd2016-03-15 17:04:39 -070046 ~KeymasterOperation();
Paul Crowley1ef25582016-01-21 20:26:12 +000047 // Is this instance valid? This is false if creation fails, and becomes
48 // false on finish or if an update fails.
Shawn Willden35351812018-01-22 09:08:32 -070049 explicit operator bool() { return mError == km::ErrorCode::OK; }
50 km::ErrorCode errorCode() { return mError; }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000051 // Call "update" repeatedly until all of the input is consumed, and
Paul Crowley1ef25582016-01-21 20:26:12 +000052 // concatenate the output. Return true on success.
Pavel Grafove2e2d302017-08-01 17:15:53 +010053 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) {
Shawn Willden785365b2018-01-20 09:37:36 -070057 if (output) std::copy(b, b + n, std::back_inserter(*output));
Pavel Grafove2e2d302017-08-01 17:15:53 +010058 });
59 }
60
Paul Crowleydff8c722016-05-16 08:14:56 -070061 // Finish and write the output to this string, unless pointer is null.
62 bool finish(std::string* output);
Paul Crowley1ef25582016-01-21 20:26:12 +000063 // Move constructor
Shawn Willden35351812018-01-22 09:08:32 -070064 KeymasterOperation(KeymasterOperation&& rhs) { *this = std::move(rhs); }
Paul Crowleydff8c722016-05-16 08:14:56 -070065 // Construct an object in an error state for error returns
Shawn Willden35351812018-01-22 09:08:32 -070066 KeymasterOperation() : mDevice{nullptr}, mOpHandle{0}, mError{km::ErrorCode::UNKNOWN_ERROR} {}
Janis Danisevskis015ec302017-01-31 11:31:08 +000067 // Move Assignment
Shawn Willden785365b2018-01-20 09:37:36 -070068 KeymasterOperation& operator=(KeymasterOperation&& rhs) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000069 mDevice = std::move(rhs.mDevice);
70 mOpHandle = std::move(rhs.mOpHandle);
71 mError = std::move(rhs.mError);
Shawn Willden35351812018-01-22 09:08:32 -070072 rhs.mError = km::ErrorCode::UNKNOWN_ERROR;
Janis Danisevskis015ec302017-01-31 11:31:08 +000073 rhs.mOpHandle = 0;
74 return *this;
75 }
Paul Crowleydf528a72016-03-09 09:31:37 -080076
77 private:
Shawn Willden35351812018-01-22 09:08:32 -070078 KeymasterOperation(KmDevice* d, uint64_t h)
79 : mDevice{d}, mOpHandle{h}, mError{km::ErrorCode::OK} {}
80 KeymasterOperation(km::ErrorCode error) : mDevice{nullptr}, mOpHandle{0}, mError{error} {}
Pavel Grafove2e2d302017-08-01 17:15:53 +010081
82 bool updateCompletely(const char* input, size_t inputLen,
83 const std::function<void(const char*, size_t)> consumer);
84
Shawn Willden35351812018-01-22 09:08:32 -070085 KmDevice* mDevice;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010086 uint64_t mOpHandle;
Shawn Willden35351812018-01-22 09:08:32 -070087 km::ErrorCode mError;
Paul Crowley1ef25582016-01-21 20:26:12 +000088 DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
89 friend class Keymaster;
90};
91
Paul Crowley0323afd2016-03-15 17:04:39 -070092// Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not
93// part of one.
Paul Crowley1ef25582016-01-21 20:26:12 +000094class Keymaster {
Paul Crowleydf528a72016-03-09 09:31:37 -080095 public:
Paul Crowley1ef25582016-01-21 20:26:12 +000096 Keymaster();
Paul Crowley1ef25582016-01-21 20:26:12 +000097 // false if we failed to open the keymaster device.
Janis Danisevskis8e537b82016-10-26 14:27:10 +010098 explicit operator bool() { return mDevice.get() != nullptr; }
Paul Crowley1ef25582016-01-21 20:26:12 +000099 // Generate a key in the keymaster from the given params.
Shawn Willden35351812018-01-22 09:08:32 -0700100 bool generateKey(const km::AuthorizationSet& inParams, std::string* key);
Paul Crowley1ef25582016-01-21 20:26:12 +0000101 // If the keymaster supports it, permanently delete a key.
Paul Crowleydf528a72016-03-09 09:31:37 -0800102 bool deleteKey(const std::string& key);
Paul Crowleydff8c722016-05-16 08:14:56 -0700103 // Replace stored key blob in response to KM_ERROR_KEY_REQUIRES_UPGRADE.
Shawn Willden35351812018-01-22 09:08:32 -0700104 bool upgradeKey(const std::string& oldKey, const km::AuthorizationSet& inParams,
Paul Crowleydff8c722016-05-16 08:14:56 -0700105 std::string* newKey);
106 // Begin a new cryptographic operation, collecting output parameters if pointer is non-null
Shawn Willden35351812018-01-22 09:08:32 -0700107 KeymasterOperation begin(km::KeyPurpose purpose, const std::string& key,
108 const km::AuthorizationSet& inParams,
109 const km::HardwareAuthToken& authToken,
110 km::AuthorizationSet* outParams);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000111 bool isSecure();
Paul Crowleydf528a72016-03-09 09:31:37 -0800112
113 private:
Shawn Willden35351812018-01-22 09:08:32 -0700114 std::unique_ptr<KmDevice> mDevice;
Paul Crowley1ef25582016-01-21 20:26:12 +0000115 DISALLOW_COPY_AND_ASSIGN(Keymaster);
116};
117
Paul Crowley1ef25582016-01-21 20:26:12 +0000118} // namespace vold
119} // namespace android
120
Paul Crowley3b71fc52017-10-09 10:55:21 -0700121// FIXME no longer needed now cryptfs is in C++.
Janis Danisevskis015ec302017-01-31 11:31:08 +0000122
123/*
124 * The following functions provide C bindings to keymaster services
125 * needed by cryptfs scrypt. The compatibility check checks whether
126 * the keymaster implementation is considered secure, i.e., TEE backed.
127 * The create_key function generates an RSA key for signing.
128 * The sign_object function signes an object with the given keymaster
129 * key.
130 */
Janis Danisevskis015ec302017-01-31 11:31:08 +0000131
Paul Crowley73473332017-11-21 15:43:51 -0800132/* Return values for keymaster_sign_object_for_cryptfs_scrypt */
133
134enum class KeymasterSignResult {
135 ok = 0,
136 error = -1,
137 upgrade = -2,
138};
139
Janis Danisevskis015ec302017-01-31 11:31:08 +0000140int keymaster_compatibility_cryptfs_scrypt();
Shawn Willden785365b2018-01-20 09:37:36 -0700141int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
142 uint32_t ratelimit, uint8_t* key_buffer,
143 uint32_t key_buffer_size, uint32_t* key_out_size);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000144
Paul Crowley73473332017-11-21 15:43:51 -0800145int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
146 uint32_t ratelimit, const uint8_t* key_blob,
147 size_t key_blob_size, uint8_t* key_buffer,
148 uint32_t key_buffer_size, uint32_t* key_out_size);
149
150KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt(
151 const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object,
152 const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size);
Janis Danisevskis015ec302017-01-31 11:31:08 +0000153
Paul Crowley1ef25582016-01-21 20:26:12 +0000154#endif