blob: 412110c07cd45dc873e78be5fcaece35daa4fda0 [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
Paul Crowley0323afd2016-03-15 17:04:39 -070020#include <memory>
Paul Crowley1ef25582016-01-21 20:26:12 +000021#include <string>
Paul Crowley0323afd2016-03-15 17:04:39 -070022#include <utility>
Paul Crowley1ef25582016-01-21 20:26:12 +000023
24#include <keymaster/authorization_set.h>
25
26namespace android {
27namespace vold {
28
29using namespace keymaster;
30
Paul Crowley0323afd2016-03-15 17:04:39 -070031// C++ wrappers to the Keymaster C interface.
Paul Crowley1ef25582016-01-21 20:26:12 +000032// This is tailored to the needs of KeyStorage, but could be extended to be
33// a more general interface.
34
Paul Crowley0323afd2016-03-15 17:04:39 -070035// Class that wraps a keymaster1_device_t or keymaster2_device_t and provides methods
36// they have in common. Also closes the device on destruction.
37class IKeymasterDevice;
38
Paul Crowley1ef25582016-01-21 20:26:12 +000039// Wrapper for a keymaster_operation_handle_t representing an
40// ongoing Keymaster operation. Aborts the operation
41// in the destructor if it is unfinished. Methods log failures
42// to LOG(ERROR).
43class KeymasterOperation {
Paul Crowleydf528a72016-03-09 09:31:37 -080044 public:
Paul Crowley0323afd2016-03-15 17:04:39 -070045 ~KeymasterOperation();
Paul Crowley1ef25582016-01-21 20:26:12 +000046 // Is this instance valid? This is false if creation fails, and becomes
47 // false on finish or if an update fails.
Paul Crowleydf528a72016-03-09 09:31:37 -080048 explicit operator bool() { return mDevice != nullptr; }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000049 // Call "update" repeatedly until all of the input is consumed, and
Paul Crowley1ef25582016-01-21 20:26:12 +000050 // concatenate the output. Return true on success.
Paul Crowleydf528a72016-03-09 09:31:37 -080051 bool updateCompletely(const std::string& input, std::string* output);
Paul Crowley13ffd8e2016-01-27 14:30:22 +000052 // Finish; pass nullptr for the "output" param.
53 bool finish();
Paul Crowley1ef25582016-01-21 20:26:12 +000054 // Finish and write the output to this string.
Paul Crowleydf528a72016-03-09 09:31:37 -080055 bool finishWithOutput(std::string* output);
Paul Crowley1ef25582016-01-21 20:26:12 +000056 // Move constructor
57 KeymasterOperation(KeymasterOperation&& rhs) {
Paul Crowley0323afd2016-03-15 17:04:39 -070058 mOpHandle = std::move(rhs.mOpHandle);
59 mDevice = std::move(rhs.mDevice);
Paul Crowley1ef25582016-01-21 20:26:12 +000060 }
Paul Crowleydf528a72016-03-09 09:31:37 -080061
62 private:
Paul Crowley0323afd2016-03-15 17:04:39 -070063 KeymasterOperation(std::shared_ptr<IKeymasterDevice> d, keymaster_operation_handle_t h)
Paul Crowleydf528a72016-03-09 09:31:37 -080064 : mDevice{d}, mOpHandle{h} {}
Paul Crowley0323afd2016-03-15 17:04:39 -070065 std::shared_ptr<IKeymasterDevice> mDevice;
Paul Crowley13ffd8e2016-01-27 14:30:22 +000066 keymaster_operation_handle_t mOpHandle;
Paul Crowley1ef25582016-01-21 20:26:12 +000067 DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
68 friend class Keymaster;
69};
70
Paul Crowley0323afd2016-03-15 17:04:39 -070071// Wrapper for a Keymaster device for methods that start a KeymasterOperation or are not
72// part of one.
Paul Crowley1ef25582016-01-21 20:26:12 +000073class Keymaster {
Paul Crowleydf528a72016-03-09 09:31:37 -080074 public:
Paul Crowley1ef25582016-01-21 20:26:12 +000075 Keymaster();
Paul Crowley1ef25582016-01-21 20:26:12 +000076 // false if we failed to open the keymaster device.
Paul Crowleydf528a72016-03-09 09:31:37 -080077 explicit operator bool() { return mDevice != nullptr; }
Paul Crowley1ef25582016-01-21 20:26:12 +000078 // Generate a key in the keymaster from the given params.
Paul Crowleydf528a72016-03-09 09:31:37 -080079 bool generateKey(const AuthorizationSet& inParams, std::string* key);
Paul Crowley1ef25582016-01-21 20:26:12 +000080 // If the keymaster supports it, permanently delete a key.
Paul Crowleydf528a72016-03-09 09:31:37 -080081 bool deleteKey(const std::string& key);
Paul Crowley1ef25582016-01-21 20:26:12 +000082 // Begin a new cryptographic operation, collecting output parameters.
Paul Crowleydf528a72016-03-09 09:31:37 -080083 KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key,
84 const AuthorizationSet& inParams, AuthorizationSet* outParams);
Paul Crowley1ef25582016-01-21 20:26:12 +000085 // Begin a new cryptographic operation; don't collect output parameters.
Paul Crowleydf528a72016-03-09 09:31:37 -080086 KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key,
87 const AuthorizationSet& inParams);
88
89 private:
Paul Crowley0323afd2016-03-15 17:04:39 -070090 std::shared_ptr<IKeymasterDevice> mDevice;
Paul Crowley1ef25582016-01-21 20:26:12 +000091 DISALLOW_COPY_AND_ASSIGN(Keymaster);
92};
93
94template <keymaster_tag_t Tag>
Paul Crowleydf528a72016-03-09 09:31:37 -080095inline AuthorizationSetBuilder& addStringParam(AuthorizationSetBuilder&& params,
96 TypedTag<KM_BYTES, Tag> tag,
97 const std::string& val) {
Paul Crowley1ef25582016-01-21 20:26:12 +000098 return params.Authorization(tag, val.data(), val.size());
99}
100
Paul Crowley05720802016-02-08 15:55:41 +0000101template <keymaster_tag_t Tag>
Paul Crowleydf528a72016-03-09 09:31:37 -0800102inline void addStringParam(AuthorizationSetBuilder* params, TypedTag<KM_BYTES, Tag> tag,
103 const std::string& val) {
Paul Crowleya051eb72016-03-08 16:08:32 -0800104 params->Authorization(tag, val.data(), val.size());
Paul Crowley05720802016-02-08 15:55:41 +0000105}
106
Paul Crowley1ef25582016-01-21 20:26:12 +0000107} // namespace vold
108} // namespace android
109
110#endif