blob: 003baa69986dfb7782054bca0a7d817c011727ee [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
36
37// Wrapper for a keymaster_operation_handle_t representing an
38// ongoing Keymaster operation. Aborts the operation
39// in the destructor if it is unfinished. Methods log failures
40// to LOG(ERROR).
41class KeymasterOperation {
42public:
Paul Crowley13ffd8e2016-01-27 14:30:22 +000043 ~KeymasterOperation() { if (mDevice) mDevice->abort(mDevice, mOpHandle); }
Paul Crowley1ef25582016-01-21 20:26:12 +000044 // Is this instance valid? This is false if creation fails, and becomes
45 // false on finish or if an update fails.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000046 explicit operator bool() {return mDevice != nullptr;}
47 // Call "update" repeatedly until all of the input is consumed, and
Paul Crowley1ef25582016-01-21 20:26:12 +000048 // concatenate the output. Return true on success.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000049 bool updateCompletely(const std::string &input, std::string &output);
50 // Finish; pass nullptr for the "output" param.
51 bool finish();
Paul Crowley1ef25582016-01-21 20:26:12 +000052 // Finish and write the output to this string.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000053 bool finishWithOutput(std::string &output);
Paul Crowley1ef25582016-01-21 20:26:12 +000054 // Move constructor
55 KeymasterOperation(KeymasterOperation&& rhs) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +000056 mOpHandle = rhs.mOpHandle;
57 mDevice = rhs.mDevice;
58 rhs.mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000059 }
Paul Crowley1ef25582016-01-21 20:26:12 +000060private:
61 KeymasterOperation(keymaster1_device_t *d, keymaster_operation_handle_t h):
Paul Crowley13ffd8e2016-01-27 14:30:22 +000062 mDevice {d}, mOpHandle {h} {}
63 keymaster1_device_t *mDevice;
64 keymaster_operation_handle_t mOpHandle;
Paul Crowley1ef25582016-01-21 20:26:12 +000065 DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
66 friend class Keymaster;
67};
68
69// Wrapper for a keymaster1_device_t representing an open connection
70// to the keymaster, which is closed in the destructor.
71class Keymaster {
72public:
73 Keymaster();
Paul Crowley13ffd8e2016-01-27 14:30:22 +000074 ~Keymaster() { if (mDevice) keymaster1_close(mDevice); }
Paul Crowley1ef25582016-01-21 20:26:12 +000075 // false if we failed to open the keymaster device.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000076 explicit operator bool() {return mDevice != nullptr;}
Paul Crowley1ef25582016-01-21 20:26:12 +000077 // Generate a key in the keymaster from the given params.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000078 bool generateKey(const AuthorizationSet &inParams, std::string &key);
Paul Crowley1ef25582016-01-21 20:26:12 +000079 // If the keymaster supports it, permanently delete a key.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000080 bool deleteKey(const std::string &key);
Paul Crowley1ef25582016-01-21 20:26:12 +000081 // Begin a new cryptographic operation, collecting output parameters.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000082 KeymasterOperation begin(
Paul Crowley1ef25582016-01-21 20:26:12 +000083 keymaster_purpose_t purpose,
84 const std::string &key,
Paul Crowley13ffd8e2016-01-27 14:30:22 +000085 const AuthorizationSet &inParams,
86 AuthorizationSet &outParams);
Paul Crowley1ef25582016-01-21 20:26:12 +000087 // Begin a new cryptographic operation; don't collect output parameters.
Paul Crowley13ffd8e2016-01-27 14:30:22 +000088 KeymasterOperation begin(
Paul Crowley1ef25582016-01-21 20:26:12 +000089 keymaster_purpose_t purpose,
90 const std::string &key,
Paul Crowley13ffd8e2016-01-27 14:30:22 +000091 const AuthorizationSet &inParams);
Paul Crowley1ef25582016-01-21 20:26:12 +000092private:
Paul Crowley13ffd8e2016-01-27 14:30:22 +000093 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 Crowley13ffd8e2016-01-27 14:30:22 +000098inline AuthorizationSetBuilder& addStringParam(AuthorizationSetBuilder &&params,
Paul Crowley1ef25582016-01-21 20:26:12 +000099 TypedTag<KM_BYTES, Tag> tag, const std::string& val) {
100 return params.Authorization(tag, val.data(), val.size());
101}
102
103} // namespace vold
104} // namespace android
105
106#endif