summaryrefslogtreecommitdiff
path: root/ng/AndroidSharedSecret.cpp
blob: 0ae3d2d80f730c93791a4c783b374e878ba626c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * Copyright 2020, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "android.hardware.security.sharedsecret-impl"
#include <log/log.h>

#include "AndroidSharedSecret.h"
#include "KeyMintUtils.h"
#include <aidl/android/hardware/security/keymint/ErrorCode.h>
#include <keymaster/android_keymaster.h>

namespace aidl::android::hardware::security::sharedsecret {

using keymaster::ComputeSharedHmacRequest;
using keymint::km_utils::kmBlob2vector;
using keymint::km_utils::kmError2ScopedAStatus;

AndroidSharedSecret::AndroidSharedSecret(
    const std::shared_ptr<keymint::AndroidKeyMintDevice>& keymint)
    : impl_(keymint->getKeymasterImpl()) {}

AndroidSharedSecret::~AndroidSharedSecret() {}

ScopedAStatus AndroidSharedSecret::getSharedSecretParameters(SharedSecretParameters* params) {
    auto response = impl_->GetHmacSharingParameters();
    params->seed = kmBlob2vector(response.params.seed);
    params->nonce = {std::begin(response.params.nonce), std::end(response.params.nonce)};
    return kmError2ScopedAStatus(response.error);
}

ScopedAStatus AndroidSharedSecret::computeSharedSecret(const vector<SharedSecretParameters>& params,
                                                       vector<uint8_t>* sharingCheck) {
    ComputeSharedHmacRequest request(impl_->message_version());
    request.params_array.params_array = new keymaster::HmacSharingParameters[params.size()];
    request.params_array.num_params = params.size();
    for (size_t i = 0; i < params.size(); ++i) {
        request.params_array.params_array[i].seed = {params[i].seed.data(), params[i].seed.size()};
        if (sizeof(request.params_array.params_array[i].nonce) != params[i].nonce.size()) {
            return kmError2ScopedAStatus(KM_ERROR_INVALID_ARGUMENT);
        }
        memcpy(request.params_array.params_array[i].nonce, params[i].nonce.data(),
               params[i].nonce.size());
    }
    auto response = impl_->ComputeSharedHmac(request);
    if (response.error == KM_ERROR_OK) *sharingCheck = kmBlob2vector(response.sharing_check);
    return kmError2ScopedAStatus(response.error);
}

}  // namespace aidl::android::hardware::security::sharedsecret