blob: cf692f3d0ae67762e003cdfdfd8075e8ab86dc94 [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
17#ifndef ANDROID_VOLD_KEYMASTER1_H
18#define ANDROID_VOLD_KEYMASTER1_H
19
20#include <string>
21
22#include <hardware/hardware.h>
23#include <hardware/keymaster1.h>
24
25#include <keymaster/authorization_set.h>
26
27namespace android {
28namespace vold {
29
30using namespace keymaster;
31
32// C++ wrappers to the keymaster1 C interface.
33// This is tailored to the needs of KeyStorage, but could be extended to be
34// a more general interface.
35
Paul Crowley1ef25582016-01-21 20:26:12 +000036// Wrapper for a keymaster_operation_handle_t representing an
37// ongoing Keymaster operation. Aborts the operation
38// in the destructor if it is unfinished. Methods log failures
39// to LOG(ERROR).
40class KeymasterOperation {
Paul Crowleydf528a72016-03-09 09:31:37 -080041 public:
42 ~KeymasterOperation() {
43 if (mDevice) mDevice->abort(mDevice, mOpHandle);
44 }
Paul Crowley1ef25582016-01-21 20:26:12 +000045 // Is this instance valid? This is false if creation fails, and becomes
46 // false on finish or if an update fails.
Paul Crowleydf528a72016-03-09 09:31:37 -080047 explicit operator bool() { return mDevice != nullptr; }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000048 // Call "update" repeatedly until all of the input is consumed, and
Paul Crowley1ef25582016-01-21 20:26:12 +000049 // concatenate the output. Return true on success.
Paul Crowleydf528a72016-03-09 09:31:37 -080050 bool updateCompletely(const std::string& input, std::string* output);
Paul Crowley13ffd8e2016-01-27 14:30:22 +000051 // Finish; pass nullptr for the "output" param.
52 bool finish();
Paul Crowley1ef25582016-01-21 20:26:12 +000053 // Finish and write the output to this string.
Paul Crowleydf528a72016-03-09 09:31:37 -080054 bool finishWithOutput(std::string* output);
Paul Crowley1ef25582016-01-21 20:26:12 +000055 // Move constructor
56 KeymasterOperation(KeymasterOperation&& rhs) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +000057 mOpHandle = rhs.mOpHandle;
58 mDevice = rhs.mDevice;
59 rhs.mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000060 }
Paul Crowleydf528a72016-03-09 09:31:37 -080061
62 private:
63 KeymasterOperation(keymaster1_device_t* d, keymaster_operation_handle_t h)
64 : mDevice{d}, mOpHandle{h} {}
65 keymaster1_device_t* 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
71// Wrapper for a keymaster1_device_t representing an open connection
72// to the keymaster, which is closed in the destructor.
73class Keymaster {
Paul Crowleydf528a72016-03-09 09:31:37 -080074 public:
Paul Crowley1ef25582016-01-21 20:26:12 +000075 Keymaster();
Paul Crowleydf528a72016-03-09 09:31:37 -080076 ~Keymaster() {
77 if (mDevice) keymaster1_close(mDevice);
78 }
Paul Crowley1ef25582016-01-21 20:26:12 +000079 // false if we failed to open the keymaster device.
Paul Crowleydf528a72016-03-09 09:31:37 -080080 explicit operator bool() { return mDevice != nullptr; }
Paul Crowley1ef25582016-01-21 20:26:12 +000081 // Generate a key in the keymaster from the given params.
Paul Crowleydf528a72016-03-09 09:31:37 -080082 bool generateKey(const AuthorizationSet& inParams, std::string* key);
Paul Crowley1ef25582016-01-21 20:26:12 +000083 // If the keymaster supports it, permanently delete a key.
Paul Crowleydf528a72016-03-09 09:31:37 -080084 bool deleteKey(const std::string& key);
Paul Crowley1ef25582016-01-21 20:26:12 +000085 // Begin a new cryptographic operation, collecting output parameters.
Paul Crowleydf528a72016-03-09 09:31:37 -080086 KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key,
87 const AuthorizationSet& inParams, AuthorizationSet* outParams);
Paul Crowley1ef25582016-01-21 20:26:12 +000088 // Begin a new cryptographic operation; don't collect output parameters.
Paul Crowleydf528a72016-03-09 09:31:37 -080089 KeymasterOperation begin(keymaster_purpose_t purpose, const std::string& key,
90 const AuthorizationSet& inParams);
91
92 private:
93 keymaster1_device_t* mDevice;
Paul Crowley1ef25582016-01-21 20:26:12 +000094 DISALLOW_COPY_AND_ASSIGN(Keymaster);
95};
96
97template <keymaster_tag_t Tag>
Paul Crowleydf528a72016-03-09 09:31:37 -080098inline AuthorizationSetBuilder& addStringParam(AuthorizationSetBuilder&& params,
99 TypedTag<KM_BYTES, Tag> tag,
100 const std::string& val) {
Paul Crowley1ef25582016-01-21 20:26:12 +0000101 return params.Authorization(tag, val.data(), val.size());
102}
103
Paul Crowley05720802016-02-08 15:55:41 +0000104template <keymaster_tag_t Tag>
Paul Crowleydf528a72016-03-09 09:31:37 -0800105inline void addStringParam(AuthorizationSetBuilder* params, TypedTag<KM_BYTES, Tag> tag,
106 const std::string& val) {
Paul Crowleya051eb72016-03-08 16:08:32 -0800107 params->Authorization(tag, val.data(), val.size());
Paul Crowley05720802016-02-08 15:55:41 +0000108}
109
Paul Crowley1ef25582016-01-21 20:26:12 +0000110} // namespace vold
111} // namespace android
112
113#endif