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 #include <optional>
22 #include <vector>
23 
24 namespace android {
25 namespace hardware {
26 namespace keymaster {
27 namespace V4_0 {
28 
29 /**
30  * Define a lexicographical ordering on HmacSharingParameters.  The parameters to
31  * IKeymasterDevice::computeSharedHmac are required to be delivered in the order specified by this
32  * comparison operator.
33  */
34 bool operator<(const HmacSharingParameters& a, const HmacSharingParameters& b);
35 
36 namespace support {
37 
blob2hidlVec(const uint8_t * data,const size_t length)38 inline static hidl_vec<uint8_t> blob2hidlVec(const uint8_t* data, const size_t length) {
39     hidl_vec<uint8_t> result(data, data + length);
40     return result;
41 }
42 
blob2hidlVec(const std::string & value)43 inline static hidl_vec<uint8_t> blob2hidlVec(const std::string& value) {
44     hidl_vec<uint8_t> result(reinterpret_cast<const uint8_t*>(value.data()),
45                              reinterpret_cast<const uint8_t*>(value.data()) + value.size());
46     return result;
47 }
48 
blob2hidlVec(const std::vector<uint8_t> & blob)49 inline static hidl_vec<uint8_t> blob2hidlVec(const std::vector<uint8_t>& blob) {
50     hidl_vec<uint8_t> result(blob.data(), blob.data() + static_cast<size_t>(blob.size()));
51     return result;
52 }
53 
54 HardwareAuthToken hidlVec2AuthToken(const hidl_vec<uint8_t>& buffer);
55 hidl_vec<uint8_t> authToken2HidlVec(const HardwareAuthToken& token);
56 
57 // Serializes and deserializes a verification token. This format is private and
58 // not stable between releases and should not be persisted to disk.
59 //
60 // Currently doesn't support the |parametersVerified| field, will fail if set.
61 //
62 std::optional<VerificationToken> deserializeVerificationToken(
63         const std::vector<uint8_t>& serializedToken);
64 std::optional<std::vector<uint8_t>> serializeVerificationToken(const VerificationToken& token);
65 
66 uint32_t getOsVersion();
67 uint32_t getOsPatchlevel();
68 
69 }  // namespace support
70 }  // namespace V4_0
71 }  // namespace keymaster
72 }  // namespace hardware
73 }  // namespace android
74 
75 #endif  // HARDWARE_INTERFACES_KEYMASTER_40_SUPPORT_KEYMASTER_UTILS_H_
76