1 /*
2  * Copyright (C) 2018 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 HARDWARE_INTERFACES_KEYMASTER_40_SUPPORT_KEYMASTER_UTILS_H_
18 #define HARDWARE_INTERFACES_KEYMASTER_40_SUPPORT_KEYMASTER_UTILS_H_
19 
20 #include <android/hardware/keymaster/4.0/types.h>
21 
22 namespace android {
23 namespace hardware {
24 namespace keymaster {
25 namespace V4_0 {
26 
27 /**
28  * Define a lexicographical ordering on HmacSharingParameters.  The parameters to
29  * IKeymasterDevice::computeSharedHmac are required to be delivered in the order specified by this
30  * comparison operator.
31  */
32 bool operator<(const HmacSharingParameters& a, const HmacSharingParameters& b);
33 
34 namespace support {
35 
36 inline static hidl_vec<uint8_t> blob2hidlVec(const uint8_t* data, const size_t length,
37                                              bool inPlace = true) {
38     hidl_vec<uint8_t> result;
39     result.setToExternal(const_cast<unsigned char*>(data), length, !inPlace);
40     return result;
41 }
42 
43 inline static hidl_vec<uint8_t> blob2hidlVec(const std::string& value, bool inPlace = true) {
44     hidl_vec<uint8_t> result;
45     result.setToExternal(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(value.data())),
46                          static_cast<size_t>(value.size()), !inPlace);
47     return result;
48 }
49 
50 inline static hidl_vec<uint8_t> blob2hidlVec(const std::vector<uint8_t>& blob,
51                                              bool inPlace = true) {
52     hidl_vec<uint8_t> result;
53     result.setToExternal(const_cast<uint8_t*>(blob.data()), static_cast<size_t>(blob.size()),
54                          !inPlace);
55     return result;
56 }
57 
58 HardwareAuthToken hidlVec2AuthToken(const hidl_vec<uint8_t>& buffer);
59 hidl_vec<uint8_t> authToken2HidlVec(const HardwareAuthToken& token);
60 
61 }  // namespace support
62 }  // namespace V4_0
63 }  // namespace keymaster
64 }  // namespace hardware
65 }  // namespace android
66 
67 #endif  // HARDWARE_INTERFACES_KEYMASTER_40_SUPPORT_KEYMASTER_UTILS_H_
68