1 /*
2  * Copyright 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 #define LOG_TAG "android.hardware.security.sharedsecret-impl"
18 #include <log/log.h>
19 
20 #include "guest/hals/keymint/remote/remote_shared_secret.h"
21 
22 #include <aidl/android/hardware/security/keymint/ErrorCode.h>
23 #include <keymaster/android_keymaster.h>
24 #include "KeyMintUtils.h"
25 
26 namespace aidl::android::hardware::security::sharedsecret {
27 
28 using namespace ::keymaster;
29 using namespace ::aidl::android::hardware::security::keymint::km_utils;
30 
RemoteSharedSecret(::keymaster::RemoteKeymaster & keymint)31 RemoteSharedSecret::RemoteSharedSecret(::keymaster::RemoteKeymaster& keymint)
32     : impl_(keymint) {}
33 
~RemoteSharedSecret()34 RemoteSharedSecret::~RemoteSharedSecret() {}
35 
getSharedSecretParameters(SharedSecretParameters * params)36 ScopedAStatus RemoteSharedSecret::getSharedSecretParameters(
37     SharedSecretParameters* params) {
38   auto response = impl_.GetHmacSharingParameters();
39   params->seed = kmBlob2vector(response.params.seed);
40   params->nonce = {std::begin(response.params.nonce),
41                    std::end(response.params.nonce)};
42   return kmError2ScopedAStatus(response.error);
43 }
44 
computeSharedSecret(const vector<SharedSecretParameters> & params,vector<uint8_t> * sharingCheck)45 ScopedAStatus RemoteSharedSecret::computeSharedSecret(
46     const vector<SharedSecretParameters>& params,
47     vector<uint8_t>* sharingCheck) {
48   ComputeSharedHmacRequest request(impl_.message_version());
49   request.params_array.params_array =
50       new keymaster::HmacSharingParameters[params.size()];
51   request.params_array.num_params = params.size();
52   for (size_t i = 0; i < params.size(); ++i) {
53     request.params_array.params_array[i].seed = {params[i].seed.data(),
54                                                  params[i].seed.size()};
55     if (sizeof(request.params_array.params_array[i].nonce) !=
56         params[i].nonce.size()) {
57       return kmError2ScopedAStatus(KM_ERROR_INVALID_ARGUMENT);
58     }
59     memcpy(request.params_array.params_array[i].nonce, params[i].nonce.data(),
60            params[i].nonce.size());
61   }
62   auto response = impl_.ComputeSharedHmac(request);
63   if (response.error == KM_ERROR_OK) {
64     *sharingCheck = kmBlob2vector(response.sharing_check);
65   }
66   return kmError2ScopedAStatus(response.error);
67 }
68 
69 }  // namespace aidl::android::hardware::security::sharedsecret
70