1 /******************************************************************************
2  **
3  ** The original Work has been changed by NXP.
4  **
5  ** Licensed under the Apache License, Version 2.0 (the "License");
6  ** you may not use this file except in compliance with the License.
7  ** You may obtain a copy of the License at
8  **
9  ** http://www.apache.org/licenses/LICENSE-2.0
10  **
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  **
17  ** Copyright 2021-2024 NXP
18  **
19  *********************************************************************************/
20 #define LOG_TAG "javacard.strongbox.keymint.operation-impl"
21 #include <android-base/logging.h>
22 
23 #include "JavacardSharedSecret.h"
24 #include <JavacardKeyMintUtils.h>
25 #include <memunreachable/memunreachable.h>
26 
27 /* 1 sec delay till OMAPI service initialized (~ 30 to 40 secs)
28  * 20 retry as per transport layer retry logic.
29  * Each retry logic takes 11~12 secs*/
30 /* OMAPI may take longer to load after a factory reset. */
31 #define MAX_SHARED_SECRET_RETRY_COUNT 120
32 
33 namespace aidl::android::hardware::security::sharedsecret {
34 using ::keymint::javacard::Instruction;
35 using ndk::ScopedAStatus;
36 using std::vector;
37 
38 static uint8_t getSharedSecretRetryCount = 0x00;
39 
getSharedSecretParameters(SharedSecretParameters * params)40 ScopedAStatus JavacardSharedSecret::getSharedSecretParameters(SharedSecretParameters* params) {
41     card_->initializeJavacard();
42     auto [item, err] = card_->sendRequest(Instruction::INS_GET_SHARED_SECRET_PARAM_CMD);
43 #ifdef NXP_EXTNS
44     if (err == KM_ERROR_SECURE_HW_COMMUNICATION_FAILED &&
45         (getSharedSecretRetryCount < MAX_SHARED_SECRET_RETRY_COUNT)) {
46         getSharedSecretRetryCount++;
47     } else if (err != KM_ERROR_OK) {
48         std::vector<uint8_t> refNonceSeed = {
49           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
52         params->seed.assign(refNonceSeed.begin(), refNonceSeed.end());
53         params->nonce.assign(refNonceSeed.begin(), refNonceSeed.end());
54         err = KM_ERROR_OK;
55         return ScopedAStatus::ok();
56     }
57 #endif
58     if (err != KM_ERROR_OK || !cbor_.getSharedSecretParameters(item, 1, *params)) {
59         LOG(ERROR) << "Error in sending in getSharedSecretParameters.";
60         return keymint::km_utils::kmError2ScopedAStatus(KM_ERROR_UNKNOWN_ERROR);
61     }
62     return ScopedAStatus::ok();
63 }
64 
65 ScopedAStatus
computeSharedSecret(const std::vector<SharedSecretParameters> & params,std::vector<uint8_t> * secret)66 JavacardSharedSecret::computeSharedSecret(const std::vector<SharedSecretParameters>& params,
67                                           std::vector<uint8_t>* secret) {
68 
69     card_->initializeJavacard();
70     cppbor::Array request;
71     cbor_.addSharedSecretParameters(request, params);
72     auto [item, err] = card_->sendRequest(Instruction::INS_COMPUTE_SHARED_SECRET_CMD, request);
73     if (err != KM_ERROR_OK) {
74         LOG(ERROR) << "Error in sending in computeSharedSecret.";
75         return keymint::km_utils::kmError2ScopedAStatus(err);
76     }
77     if (!cbor_.getBinaryArray(item, 1, *secret)) {
78         LOG(ERROR) << "Error in decoding the response in computeSharedSecret.";
79         return keymint::km_utils::kmError2ScopedAStatus(KM_ERROR_UNKNOWN_ERROR);
80     }
81     return ScopedAStatus::ok();
82 }
83 
dump(int,const char **,uint32_t)84 binder_status_t JavacardSharedSecret::dump(int /* fd */, const char** /* p */, uint32_t /* q */) {
85     LOG(INFO) << "\n KeyMint-JavacardSharedSecret HAL MemoryLeak Info = \n"
86               << ::android::GetUnreachableMemoryString(true, 10000).c_str();
87     return STATUS_OK;
88 }
89 
90 }  // namespace aidl::android::hardware::security::sharedsecret
91