blob: a4deddff7c549065bc21e2890ae5a41a5c6d21fc [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 {
41public:
Paul Crowley13ffd8e2016-01-27 14:30:22 +000042 ~KeymasterOperation() { if (mDevice) mDevice->abort(mDevice, mOpHandle); }
Paul Crowley1ef25582016-01-21 20:26:12 +000043 // Is this instance valid? This is false if creation fails, and becomes
44 // false on finish or if an update fails.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000045 explicit operator bool() {return mDevice != nullptr;}
46 // Call "update" repeatedly until all of the input is consumed, and
Paul Crowley1ef25582016-01-21 20:26:12 +000047 // concatenate the output. Return true on success.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000048 bool updateCompletely(const std::string &input, std::string &output);
49 // Finish; pass nullptr for the "output" param.
50 bool finish();
Paul Crowley1ef25582016-01-21 20:26:12 +000051 // Finish and write the output to this string.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000052 bool finishWithOutput(std::string &output);
Paul Crowley1ef25582016-01-21 20:26:12 +000053 // Move constructor
54 KeymasterOperation(KeymasterOperation&& rhs) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +000055 mOpHandle = rhs.mOpHandle;
56 mDevice = rhs.mDevice;
57 rhs.mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000058 }
Paul Crowley1ef25582016-01-21 20:26:12 +000059private:
60 KeymasterOperation(keymaster1_device_t *d, keymaster_operation_handle_t h):
Paul Crowley13ffd8e2016-01-27 14:30:22 +000061 mDevice {d}, mOpHandle {h} {}
62 keymaster1_device_t *mDevice;
63 keymaster_operation_handle_t mOpHandle;
Paul Crowley1ef25582016-01-21 20:26:12 +000064 DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
65 friend class Keymaster;
66};
67
68// Wrapper for a keymaster1_device_t representing an open connection
69// to the keymaster, which is closed in the destructor.
70class Keymaster {
71public:
72 Keymaster();
Paul Crowley13ffd8e2016-01-27 14:30:22 +000073 ~Keymaster() { if (mDevice) keymaster1_close(mDevice); }
Paul Crowley1ef25582016-01-21 20:26:12 +000074 // false if we failed to open the keymaster device.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000075 explicit operator bool() {return mDevice != nullptr;}
Paul Crowley1ef25582016-01-21 20:26:12 +000076 // Generate a key in the keymaster from the given params.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000077 bool generateKey(const AuthorizationSet &inParams, std::string &key);
Paul Crowley1ef25582016-01-21 20:26:12 +000078 // If the keymaster supports it, permanently delete a key.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000079 bool deleteKey(const std::string &key);
Paul Crowley1ef25582016-01-21 20:26:12 +000080 // Begin a new cryptographic operation, collecting output parameters.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000081 KeymasterOperation begin(
Paul Crowley1ef25582016-01-21 20:26:12 +000082 keymaster_purpose_t purpose,
83 const std::string &key,
Paul Crowley13ffd8e2016-01-27 14:30:22 +000084 const AuthorizationSet &inParams,
85 AuthorizationSet &outParams);
Paul Crowley1ef25582016-01-21 20:26:12 +000086 // Begin a new cryptographic operation; don't collect output parameters.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000087 KeymasterOperation begin(
Paul Crowley1ef25582016-01-21 20:26:12 +000088 keymaster_purpose_t purpose,
89 const std::string &key,
Paul Crowley13ffd8e2016-01-27 14:30:22 +000090 const AuthorizationSet &inParams);
Paul Crowley1ef25582016-01-21 20:26:12 +000091private:
Paul Crowley13ffd8e2016-01-27 14:30:22 +000092 keymaster1_device_t *mDevice;
Paul Crowley1ef25582016-01-21 20:26:12 +000093 DISALLOW_COPY_AND_ASSIGN(Keymaster);
94};
95
96template <keymaster_tag_t Tag>
Paul Crowley13ffd8e2016-01-27 14:30:22 +000097inline AuthorizationSetBuilder& addStringParam(AuthorizationSetBuilder &&params,
Paul Crowley1ef25582016-01-21 20:26:12 +000098 TypedTag<KM_BYTES, Tag> tag, const std::string& val) {
99 return params.Authorization(tag, val.data(), val.size());
100}
101
Paul Crowley05720802016-02-08 15:55:41 +0000102template <keymaster_tag_t Tag>
103inline void addStringParam(AuthorizationSetBuilder &params,
104 TypedTag<KM_BYTES, Tag> tag, const std::string& val) {
105 params.Authorization(tag, val.data(), val.size());
106}
107
Paul Crowley1ef25582016-01-21 20:26:12 +0000108} // namespace vold
109} // namespace android
110
111#endif