1 /*
2  * Copyright (C) 2020 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  *
18  *  The original Work has been changed by NXP.
19  *
20  *  Licensed under the Apache License, Version 2.0 (the "License");
21  *  you may not use this file except in compliance with the License.
22  *  You may obtain a copy of the License at
23  *
24  *  http://www.apache.org/licenses/LICENSE-2.0
25  *
26  *  Unless required by applicable law or agreed to in writing, software
27  *  distributed under the License is distributed on an "AS IS" BASIS,
28  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  *  See the License for the specific language governing permissions and
30  *  limitations under the License.
31  *
32  *  Copyright 2023 NXP
33  *
34  ******************************************************************************/
35 
36 #pragma once
37 #include <aidl/android/hardware/security/keymint/KeyParameter.h>
38 #include <aidl/android/hardware/security/keymint/Tag.h>
39 #include <aidl/android/hardware/security/keymint/HardwareAuthToken.h>
40 #include <aidl/android/hardware/security/secureclock/ISecureClock.h>
41 #include <keymaster/android_keymaster_messages.h>
42 #include <keymaster/android_keymaster_utils.h>
43 #include <vector>
44 
45 namespace aidl::android::hardware::security::keymint::km_utils {
46 using keymaster::KeymasterBlob;
47 using ::ndk::ScopedAStatus;
48 using secureclock::TimeStampToken;
49 using std::vector;
50 using LegacyHardwareAuthToken = ::keymaster::HardwareAuthToken;
51 
legacy_enum_conversion(const Tag value)52 inline keymaster_tag_t legacy_enum_conversion(const Tag value) {
53     return static_cast<keymaster_tag_t>(value);
54 }
55 
legacy_enum_conversion(const keymaster_tag_t value)56 inline Tag legacy_enum_conversion(const keymaster_tag_t value) {
57     return static_cast<Tag>(value);
58 }
59 
typeFromTag(const keymaster_tag_t tag)60 inline keymaster_tag_type_t typeFromTag(const keymaster_tag_t tag) {
61     return keymaster_tag_get_type(tag);
62 }
63 
Vec2KmBlob(const vector<uint8_t> & input,KeymasterBlob * blob)64 inline void Vec2KmBlob(const vector<uint8_t>& input, KeymasterBlob* blob) {
65     blob->Reset(input.size());
66     memcpy(blob->writable_data(), input.data(), input.size());
67 }
68 
kmBlob2vector(const keymaster_key_blob_t & blob)69 inline vector<uint8_t> kmBlob2vector(const keymaster_key_blob_t& blob) {
70     vector<uint8_t> result(blob.key_material, blob.key_material + blob.key_material_size);
71     return result;
72 }
73 
kmBlob2vector(const keymaster_blob_t & blob)74 inline vector<uint8_t> kmBlob2vector(const keymaster_blob_t& blob) {
75     vector<uint8_t> result(blob.data, blob.data + blob.data_length);
76     return result;
77 }
78 
79 keymaster_error_t legacyHardwareAuthToken(const HardwareAuthToken& aidlToken,
80                                           LegacyHardwareAuthToken* legacyToken);
81 
82 keymaster_error_t encodeTimestampToken(const TimeStampToken& timestampToken,
83                                        vector<uint8_t>* encodedToken);
84 
kmError2ScopedAStatus(const keymaster_error_t value)85 inline ScopedAStatus kmError2ScopedAStatus(const keymaster_error_t value) {
86     return (value == KM_ERROR_OK
87                 ? ScopedAStatus::ok()
88                 : ScopedAStatus(AStatus_fromServiceSpecificError(static_cast<int32_t>(value))));
89 }
90 
91 KeyParameter kmParam2Aidl(const keymaster_key_param_t& param);
92 vector<KeyParameter> kmParamSet2Aidl(const keymaster_key_param_set_t& set);
93 keymaster_key_param_set_t aidlKeyParams2Km(const vector<KeyParameter>& keyParams);
94 
95 class KmParamSet : public keymaster_key_param_set_t {
96   public:
KmParamSet(const vector<KeyParameter> & keyParams)97     explicit KmParamSet(const vector<KeyParameter>& keyParams)
98         : keymaster_key_param_set_t(aidlKeyParams2Km(keyParams)) {}
99 
KmParamSet(KmParamSet && other)100     KmParamSet(KmParamSet&& other) : keymaster_key_param_set_t{other.params, other.length} {
101         other.length = 0;
102         other.params = nullptr;
103     }
104 
105     KmParamSet(const KmParamSet&) = delete;
~KmParamSet()106     ~KmParamSet() { keymaster_free_param_set(this); }
107 };
108 
109 }  // namespace aidl::android::hardware::security::keymint
110