1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "inputer_data_impl.h"
17 #include <openssl/evp.h>
18 #include <openssl/kdf.h>
19 #include "pinauth_log_wrapper.h"
20
21 namespace OHOS {
22 namespace UserIAM {
23 namespace PinAuth {
InputerDataImpl(std::vector<uint8_t> salt,sptr<IRemoteInputerData> remoteInputerData)24 InputerDataImpl::InputerDataImpl(std::vector<uint8_t> salt, sptr<IRemoteInputerData> remoteInputerData) : salt_(salt),
25 remoteInputerData_(remoteInputerData)
26 {
27 }
28
~InputerDataImpl()29 InputerDataImpl::~InputerDataImpl()
30 {
31 }
32
OnSetData(int32_t authSubType,std::vector<uint8_t> data)33 void InputerDataImpl::OnSetData(int32_t authSubType, std::vector<uint8_t> data)
34 {
35 PINAUTH_HILOGI(MODULE_FRAMEWORKS, "InputerDataImpl::OnSetData start");
36 std::vector<uint8_t> scrypt;
37 PINAUTH_HILOGI(MODULE_FRAMEWORKS, "InputerDataImpl::OnSetData data size is : %{public}zu", data.size());
38 getScrypt(data, scrypt);
39 remoteInputerData_->OnSetData(authSubType, scrypt);
40 }
41
getScrypt(std::vector<uint8_t> data,std::vector<uint8_t> & scrypt)42 void InputerDataImpl::getScrypt(std::vector<uint8_t> data, std::vector<uint8_t> &scrypt)
43 {
44 PINAUTH_HILOGI(MODULE_FRAMEWORKS, "InputerDataImpl::OnSetData start");
45 EVP_PKEY_CTX *pctx;
46 unsigned char out[OUT_LENGTH];
47
48 size_t outlen = sizeof(out);
49 pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SCRYPT, NULL);
50 if (EVP_PKEY_derive_init(pctx) <= 0) {
51 PINAUTH_HILOGE(MODULE_FRAMEWORKS, "InputerDataImpl::getScrypt EVP_PKEY_derive_init error");
52 return;
53 }
54 if (EVP_PKEY_CTX_set1_pbe_pass(pctx, data.data(), data.size()) <= 0) {
55 PINAUTH_HILOGE(MODULE_FRAMEWORKS, "InputerDataImpl::getScrypt EVP_PKEY_CTX_set1_pbe_pass error");
56 EVP_PKEY_CTX_free(pctx);
57 return;
58 }
59 if (EVP_PKEY_CTX_set1_scrypt_salt(pctx, salt_.data(), salt_.size()) <= 0) {
60 PINAUTH_HILOGE(MODULE_FRAMEWORKS, "InputerDataImpl::getScrypt EVP_PKEY_CTX_set1_scrypt_salt error");
61 EVP_PKEY_CTX_free(pctx);
62 return;
63 }
64 if (EVP_PKEY_CTX_set_scrypt_N(pctx, SCRYPT_N) <= 0) {
65 PINAUTH_HILOGE(MODULE_FRAMEWORKS, "InputerDataImpl::getScrypt EVP_PKEY_CTX_set_scrypt_N error");
66 EVP_PKEY_CTX_free(pctx);
67 return;
68 }
69 if (EVP_PKEY_CTX_set_scrypt_r(pctx, SCRYPT_R) <= 0) {
70 PINAUTH_HILOGE(MODULE_FRAMEWORKS, "InputerDataImpl::getScrypt EVP_PKEY_CTX_set_scrypt_r error");
71 EVP_PKEY_CTX_free(pctx);
72 return;
73 }
74 if (EVP_PKEY_CTX_set_scrypt_p(pctx, SCRYPT_P) <= 0) {
75 PINAUTH_HILOGE(MODULE_FRAMEWORKS, "InputerDataImpl::getScrypt EVP_PKEY_CTX_set_scrypt_p error");
76 EVP_PKEY_CTX_free(pctx);
77 return;
78 }
79 if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
80 PINAUTH_HILOGE(MODULE_FRAMEWORKS, "InputerDataImpl::getScrypt EVP_PKEY_derive error");
81 EVP_PKEY_CTX_free(pctx);
82 return;
83 }
84
85 for (size_t i = 0; i < OUT_LENGTH; i++) {
86 scrypt.push_back(out[i]);
87 }
88
89 EVP_PKEY_CTX_free(pctx);
90 }
91 } // namespace PinAuth
92 } // namespace UserIAM
93 } // namespace OHOS
94