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 #include <trusty_keymaster/TrustySharedSecret.h>
18 
19 #include <aidl/android/hardware/security/keymint/ErrorCode.h>
20 #include <keymaster/android_keymaster.h>
21 #include "KeyMintUtils.h"
22 
23 namespace aidl::android::hardware::security::sharedsecret::trusty {
24 
25 using keymint::km_utils::kmBlob2vector;
26 using keymint::km_utils::kmError2ScopedAStatus;
27 
getSharedSecretParameters(SharedSecretParameters * params)28 ::ndk::ScopedAStatus TrustySharedSecret::getSharedSecretParameters(SharedSecretParameters* params) {
29     auto response = impl_->GetHmacSharingParameters();
30     params->seed = kmBlob2vector(response.params.seed);
31     params->nonce = {std::begin(response.params.nonce), std::end(response.params.nonce)};
32     return kmError2ScopedAStatus(response.error);
33 }
34 
computeSharedSecret(const std::vector<SharedSecretParameters> & params,std::vector<uint8_t> * sharingCheck)35 ::ndk::ScopedAStatus TrustySharedSecret::computeSharedSecret(
36         const std::vector<SharedSecretParameters>& params, std::vector<uint8_t>* sharingCheck) {
37     keymaster::ComputeSharedHmacRequest request(impl_->message_version());
38     request.params_array.params_array = new keymaster::HmacSharingParameters[params.size()];
39     request.params_array.num_params = params.size();
40     for (size_t i = 0; i < params.size(); ++i) {
41         request.params_array.params_array[i].seed = {params[i].seed.data(), params[i].seed.size()};
42         if (sizeof(request.params_array.params_array[i].nonce) != params[i].nonce.size()) {
43             return kmError2ScopedAStatus(KM_ERROR_INVALID_ARGUMENT);
44         }
45         memcpy(request.params_array.params_array[i].nonce, params[i].nonce.data(),
46                params[i].nonce.size());
47     }
48 
49     auto response = impl_->ComputeSharedHmac(request);
50     if (response.error == KM_ERROR_OK) *sharingCheck = kmBlob2vector(response.sharing_check);
51     return kmError2ScopedAStatus(response.error);
52 }
53 
54 }  // namespace aidl::android::hardware::security::sharedsecret::trusty
55