• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include <cstddef>
19 #include <vector>
20 
21 #include <openssl/sha.h>
22 
23 #include "iam_logger.h"
24 #include "iam_ptr.h"
25 #include "scrypt.h"
26 
27 #define LOG_LABEL OHOS::UserIam::Common::LABEL_PIN_AUTH_SDK
28 
29 namespace OHOS {
30 namespace UserIam {
31 namespace PinAuth {
32 namespace {
33 constexpr uint32_t MIN_PIN_LENGTH = 6;
34 }
35 
InputerDataImpl(const std::vector<uint8_t> & algoParameter,const sptr<InputerSetData> & inputerSetData,uint32_t algoVersion,bool isEnroll)36 InputerDataImpl::InputerDataImpl(const std::vector<uint8_t> &algoParameter, const sptr<InputerSetData> &inputerSetData,
37     uint32_t algoVersion, bool isEnroll) : algoParameter_(algoParameter),
38     inputerSetData_(inputerSetData), algoVersion_(algoVersion), isEnroll_(isEnroll)
39 {
40 }
41 
OnSetData(int32_t authSubType,std::vector<uint8_t> data)42 void InputerDataImpl::OnSetData(int32_t authSubType, std::vector<uint8_t> data)
43 {
44     IAM_LOGI("start and data size:%{public}zu algo version:%{public}u", data.size(), algoVersion_);
45     std::vector<uint8_t> setData;
46     if (isEnroll_) {
47         if (data.size() < MIN_PIN_LENGTH) {
48             IAM_LOGE("enroll pin data size is less than min pin data length");
49             return OnSetDataInner(authSubType, setData);
50         }
51     } else {
52         if (data.size() == 0) {
53             IAM_LOGE("auth pin data size is 0");
54             return OnSetDataInner(authSubType, setData);
55         }
56     }
57 
58     auto scryptPointer = Common::MakeUnique<Scrypt>(algoParameter_);
59     if (scryptPointer == nullptr) {
60         IAM_LOGE("scryptPointer is nullptr");
61         return OnSetDataInner(authSubType, setData);
62     }
63     setData = scryptPointer->GetScrypt(data, algoVersion_);
64     if (setData.empty()) {
65         IAM_LOGE("get scrypt fail");
66         return OnSetDataInner(authSubType, setData);
67     }
68     if ((algoVersion_ > ALGO_VERSION_V1) && isEnroll_ && (!GetSha256(data, setData))) {
69         IAM_LOGE("get sha256 fail");
70         setData.clear();
71     }
72     return OnSetDataInner(authSubType, setData);
73 }
74 
GetSha256(std::vector<uint8_t> & data,std::vector<uint8_t> & out)75 bool InputerDataImpl::GetSha256(std::vector<uint8_t> &data, std::vector<uint8_t> &out)
76 {
77     uint8_t sha256Result[SHA256_DIGEST_LENGTH] = {};
78     if (SHA256(data.data(), data.size(), sha256Result) != sha256Result) {
79         IAM_LOGE("get sha256 fail");
80         return false;
81     }
82     out.insert(out.end(), sha256Result, sha256Result + SHA256_DIGEST_LENGTH);
83     return true;
84 }
85 
OnSetDataInner(int32_t authSubType,std::vector<uint8_t> & setData)86 void InputerDataImpl::OnSetDataInner(int32_t authSubType, std::vector<uint8_t> &setData)
87 {
88     if (inputerSetData_ == nullptr) {
89         IAM_LOGE("inputerSetData is nullptr");
90         return;
91     }
92     inputerSetData_->OnSetData(authSubType, setData);
93 }
94 } // namespace PinAuth
95 } // namespace UserIam
96 } // namespace OHOS
97