blob: c8006660546c7762643d1e57017880a6129f3f36 [file] [log] [blame]
Chirag Pathak7a079942021-01-25 20:16:30 +00001/*
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 "AndroidSharedSecret.h"
21#include "KeyMintUtils.h"
22#include <aidl/android/hardware/security/keymint/ErrorCode.h>
23#include <keymaster/android_keymaster.h>
24
25namespace aidl::android::hardware::security::sharedsecret {
26
Shawn Willden9a3792e2021-04-08 09:38:14 -060027using keymaster::ComputeSharedHmacRequest;
28using keymint::km_utils::kmBlob2vector;
29using keymint::km_utils::kmError2ScopedAStatus;
Chirag Pathak7a079942021-01-25 20:16:30 +000030
Shawn Willden9a3792e2021-04-08 09:38:14 -060031AndroidSharedSecret::AndroidSharedSecret(
32 const std::shared_ptr<keymint::AndroidKeyMintDevice>& keymint)
Chirag Pathak7a079942021-01-25 20:16:30 +000033 : impl_(keymint->getKeymasterImpl()) {}
34
35AndroidSharedSecret::~AndroidSharedSecret() {}
36
37ScopedAStatus AndroidSharedSecret::getSharedSecretParameters(SharedSecretParameters* params) {
38 auto response = impl_->GetHmacSharingParameters();
39 params->seed = kmBlob2vector(response.params.seed);
40 params->nonce = {std::begin(response.params.nonce), std::end(response.params.nonce)};
41 return kmError2ScopedAStatus(response.error);
42}
43
44ScopedAStatus AndroidSharedSecret::computeSharedSecret(const vector<SharedSecretParameters>& params,
45 vector<uint8_t>* sharingCheck) {
46 ComputeSharedHmacRequest request(impl_->message_version());
David Drysdaleb80ef352022-01-24 08:07:34 +000047 request.params_array.params_array =
48 new (std::nothrow) keymaster::HmacSharingParameters[params.size()];
David Drysdale01d2e132022-01-25 09:46:45 +000049 if (request.params_array.params_array == nullptr) {
50 return kmError2ScopedAStatus(KM_ERROR_MEMORY_ALLOCATION_FAILED);
51 }
Chirag Pathak7a079942021-01-25 20:16:30 +000052 request.params_array.num_params = params.size();
53 for (size_t i = 0; i < params.size(); ++i) {
54 request.params_array.params_array[i].seed = {params[i].seed.data(), params[i].seed.size()};
55 if (sizeof(request.params_array.params_array[i].nonce) != params[i].nonce.size()) {
56 return kmError2ScopedAStatus(KM_ERROR_INVALID_ARGUMENT);
57 }
58 memcpy(request.params_array.params_array[i].nonce, params[i].nonce.data(),
59 params[i].nonce.size());
60 }
61 auto response = impl_->ComputeSharedHmac(request);
62 if (response.error == KM_ERROR_OK) *sharingCheck = kmBlob2vector(response.sharing_check);
63 return kmError2ScopedAStatus(response.error);
64}
65
66} // namespace aidl::android::hardware::security::sharedsecret