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 #pragma once
18 #include <aidl/android/hardware/security/keymint/KeyParameter.h>
19 #include <aidl/android/hardware/security/keymint/Tag.h>
20 #include <aidl/android/hardware/security/keymint/HardwareAuthToken.h>
21 #include <aidl/android/hardware/security/secureclock/ISecureClock.h>
22 #include <keymaster/android_keymaster_messages.h>
23 #include <keymaster/android_keymaster_utils.h>
24 #include <vector>
25 
26 namespace aidl::android::hardware::security::keymint::km_utils {
27 using namespace ::keymaster;
28 using secureclock::TimeStampToken;
29 using ::ndk::ScopedAStatus;
30 using std::vector;
31 using LegacyHardwareAuthToken = ::keymaster::HardwareAuthToken;
32 
legacy_enum_conversion(const Tag value)33 inline keymaster_tag_t legacy_enum_conversion(const Tag value) {
34     return static_cast<keymaster_tag_t>(value);
35 }
36 
legacy_enum_conversion(const keymaster_tag_t value)37 inline Tag legacy_enum_conversion(const keymaster_tag_t value) {
38     return static_cast<Tag>(value);
39 }
40 
typeFromTag(const keymaster_tag_t tag)41 inline keymaster_tag_type_t typeFromTag(const keymaster_tag_t tag) {
42     return keymaster_tag_get_type(tag);
43 }
44 
Vec2KmBlob(const vector<uint8_t> & input,KeymasterBlob * blob)45 inline void Vec2KmBlob(const vector<uint8_t>& input, KeymasterBlob* blob) {
46     blob->Reset(input.size());
47     memcpy(blob->writable_data(), input.data(), input.size());
48 }
49 
kmBlob2vector(const keymaster_key_blob_t & blob)50 inline vector<uint8_t> kmBlob2vector(const keymaster_key_blob_t& blob) {
51     vector<uint8_t> result(blob.key_material, blob.key_material + blob.key_material_size);
52     return result;
53 }
54 
kmBlob2vector(const keymaster_blob_t & blob)55 inline vector<uint8_t> kmBlob2vector(const keymaster_blob_t& blob) {
56     vector<uint8_t> result(blob.data, blob.data + blob.data_length);
57     return result;
58 }
59 
60 keymaster_error_t legacyHardwareAuthToken(const HardwareAuthToken& aidlToken,
61                                           LegacyHardwareAuthToken* legacyToken);
62 
63 keymaster_error_t encodeTimestampToken(const TimeStampToken& timestampToken,
64                                        vector<uint8_t>* encodedToken);
65 
kmError2ScopedAStatus(const keymaster_error_t value)66 inline ScopedAStatus kmError2ScopedAStatus(const keymaster_error_t value) {
67     return (value == KM_ERROR_OK
68                 ? ScopedAStatus::ok()
69                 : ScopedAStatus(AStatus_fromServiceSpecificError(static_cast<int32_t>(value))));
70 }
71 
72 KeyParameter kmParam2Aidl(const keymaster_key_param_t& param);
73 vector<KeyParameter> kmParamSet2Aidl(const keymaster_key_param_set_t& set);
74 keymaster_key_param_set_t aidlKeyParams2Km(const vector<KeyParameter>& keyParams);
75 
76 class KmParamSet : public keymaster_key_param_set_t {
77   public:
KmParamSet(const vector<KeyParameter> & keyParams)78     explicit KmParamSet(const vector<KeyParameter>& keyParams)
79         : keymaster_key_param_set_t(aidlKeyParams2Km(keyParams)) {}
80 
KmParamSet(KmParamSet && other)81     KmParamSet(KmParamSet&& other) : keymaster_key_param_set_t{other.params, other.length} {
82         other.length = 0;
83         other.params = nullptr;
84     }
85 
86     KmParamSet(const KmParamSet&) = delete;
~KmParamSet()87     ~KmParamSet() { keymaster_free_param_set(this); }
88 };
89 
90 }  // namespace aidl::android::hardware::security::keymint
91