/* * Copyright (c) 2022 Huawei Device Co., Ltd. * 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. */ #include "inputer_data_impl.h" #include #include #include #include "iam_logger.h" #include "iam_ptr.h" #include "scrypt.h" #define LOG_LABEL OHOS::UserIam::Common::LABEL_PIN_AUTH_SDK namespace OHOS { namespace UserIam { namespace PinAuth { namespace { constexpr uint32_t MIN_PIN_LENGTH = 6; } InputerDataImpl::InputerDataImpl(const std::vector &algoParameter, const sptr &inputerSetData, uint32_t algoVersion, bool isEnroll) : algoParameter_(algoParameter), inputerSetData_(inputerSetData), algoVersion_(algoVersion), isEnroll_(isEnroll) { } void InputerDataImpl::OnSetData(int32_t authSubType, std::vector data) { IAM_LOGI("start and data size:%{public}zu algo version:%{public}u", data.size(), algoVersion_); std::vector setData; if (isEnroll_) { if (data.size() < MIN_PIN_LENGTH) { IAM_LOGE("enroll pin data size is less than min pin data length"); return OnSetDataInner(authSubType, setData); } } else { if (data.size() == 0) { IAM_LOGE("auth pin data size is 0"); return OnSetDataInner(authSubType, setData); } } auto scryptPointer = Common::MakeUnique(algoParameter_); if (scryptPointer == nullptr) { IAM_LOGE("scryptPointer is nullptr"); return OnSetDataInner(authSubType, setData); } setData = scryptPointer->GetScrypt(data, algoVersion_); if (setData.empty()) { IAM_LOGE("get scrypt fail"); return OnSetDataInner(authSubType, setData); } if ((algoVersion_ > ALGO_VERSION_V1) && isEnroll_ && (!GetSha256(data, setData))) { IAM_LOGE("get sha256 fail"); setData.clear(); } return OnSetDataInner(authSubType, setData); } bool InputerDataImpl::GetSha256(std::vector &data, std::vector &out) { uint8_t sha256Result[SHA256_DIGEST_LENGTH] = {}; if (SHA256(data.data(), data.size(), sha256Result) != sha256Result) { IAM_LOGE("get sha256 fail"); return false; } out.insert(out.end(), sha256Result, sha256Result + SHA256_DIGEST_LENGTH); return true; } void InputerDataImpl::OnSetDataInner(int32_t authSubType, std::vector &setData) { if (inputerSetData_ == nullptr) { IAM_LOGE("inputerSetData is nullptr"); return; } inputerSetData_->OnSetData(authSubType, setData); } } // namespace PinAuth } // namespace UserIam } // namespace OHOS