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