1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#ifndef PIN_AUTH_H
17094332d3Sopenharmony_ci#define PIN_AUTH_H
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_ci#include <cstdint>
20094332d3Sopenharmony_ci#include <mutex>
21094332d3Sopenharmony_ci#include <vector>
22094332d3Sopenharmony_ci#include "nocopyable.h"
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_cinamespace OHOS {
25094332d3Sopenharmony_cinamespace UserIam {
26094332d3Sopenharmony_cinamespace PinAuth {
27094332d3Sopenharmony_cistruct PinCredentialInfo {
28094332d3Sopenharmony_ci    uint64_t subType;
29094332d3Sopenharmony_ci    uint32_t remainTimes;
30094332d3Sopenharmony_ci    uint32_t freezingTime;
31094332d3Sopenharmony_ci    int32_t nextFailLockoutDuration;
32094332d3Sopenharmony_ci};
33094332d3Sopenharmony_ci
34094332d3Sopenharmony_cistruct PinAlgoParam {
35094332d3Sopenharmony_ci    uint32_t algoVersion;
36094332d3Sopenharmony_ci    uint64_t subType;
37094332d3Sopenharmony_ci    std::vector<uint8_t> algoParameter;
38094332d3Sopenharmony_ci    std::vector<uint8_t> challenge;
39094332d3Sopenharmony_ci};
40094332d3Sopenharmony_ci
41094332d3Sopenharmony_ciclass PinAuth {
42094332d3Sopenharmony_cipublic:
43094332d3Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(PinAuth);
44094332d3Sopenharmony_ci    PinAuth() = default;
45094332d3Sopenharmony_ci    ~PinAuth() = default;
46094332d3Sopenharmony_ci    int32_t Init();
47094332d3Sopenharmony_ci    int32_t Close();
48094332d3Sopenharmony_ci
49094332d3Sopenharmony_ci    int32_t GetExecutorInfo(int32_t executorRole, std::vector<uint8_t> &pubKey, uint32_t &esl,
50094332d3Sopenharmony_ci        uint32_t &maxTemplateAcl);
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_ci    // for all in one executor
53094332d3Sopenharmony_ci    int32_t SetAllInOneFwkParam(
54094332d3Sopenharmony_ci        const std::vector<uint64_t> &templateIdList, const std::vector<uint8_t> &frameworkPublicKey);
55094332d3Sopenharmony_ci    int32_t EnrollPin(uint64_t scheduleId, uint64_t subType, std::vector<uint8_t> &salt,
56094332d3Sopenharmony_ci        const std::vector<uint8_t> &pinData, std::vector<uint8_t> &result);
57094332d3Sopenharmony_ci    int32_t AuthPin(uint64_t scheduleId, uint64_t templateId, const std::vector<uint8_t> &pinData,
58094332d3Sopenharmony_ci        std::vector<uint8_t> &result);
59094332d3Sopenharmony_ci    void WriteAntiBrute(uint64_t templateId);
60094332d3Sopenharmony_ci    int32_t QueryPinInfo(uint64_t templateId, PinCredentialInfo &pinCredentialInfoRet);
61094332d3Sopenharmony_ci    int32_t DeleteTemplate(uint64_t templateId);
62094332d3Sopenharmony_ci    int32_t GenerateAlgoParameter(std::vector<uint8_t> &algoParameter, uint32_t &algoVersion);
63094332d3Sopenharmony_ci    int32_t AllInOneAuth(
64094332d3Sopenharmony_ci        uint64_t scheduleId, uint64_t templateId, const std::vector<uint8_t> &extraInfo, PinAlgoParam &pinAlgoParam);
65094332d3Sopenharmony_ci
66094332d3Sopenharmony_ci    // for collector executor
67094332d3Sopenharmony_ci    int32_t SetCollectorFwkParam(const std::vector<uint8_t> &frameworkPublicKey);
68094332d3Sopenharmony_ci    int32_t Collect(uint64_t scheduleId, const std::vector<uint8_t> &extraInfo, std::vector<uint8_t> &msg);
69094332d3Sopenharmony_ci    int32_t CancelCollect();
70094332d3Sopenharmony_ci    int32_t SendMessageToCollector(uint64_t scheduleId, const std::vector<uint8_t> &msg, PinAlgoParam &pinAlgoParam);
71094332d3Sopenharmony_ci    int32_t SetDataToCollector(uint64_t scheduleId, const std::vector<uint8_t> &data, std::vector<uint8_t> &msg);
72094332d3Sopenharmony_ci
73094332d3Sopenharmony_ci    // for collector executor
74094332d3Sopenharmony_ci    int32_t SetVerifierFwkParam(const std::vector<uint8_t> &frameworkPublicKey);
75094332d3Sopenharmony_ci    int32_t VerifierAuth(
76094332d3Sopenharmony_ci        uint64_t scheduleId, uint64_t templateId, const std::vector<uint8_t> &extraInfo, std::vector<uint8_t> &msgOut);
77094332d3Sopenharmony_ci    int32_t CancelVerifierAuth();
78094332d3Sopenharmony_ci    int32_t SendMessageToVerifier(uint64_t scheduleId,
79094332d3Sopenharmony_ci        const std::vector<uint8_t> &msgIn, std::vector<uint8_t> &msgOut, bool &isAuthEnd, int32_t &compareResult);
80094332d3Sopenharmony_ci
81094332d3Sopenharmony_ciprivate:
82094332d3Sopenharmony_ci    int32_t SetVectorByBuffer(std::vector<uint8_t> &vec, const uint8_t *buf, uint32_t bufSize);
83094332d3Sopenharmony_ci    int32_t PinResultToCoAuthResult(int32_t resultCode);
84094332d3Sopenharmony_ci    std::mutex mutex_;
85094332d3Sopenharmony_ci};
86094332d3Sopenharmony_ci} // namespace PinAuth
87094332d3Sopenharmony_ci} // namespace UserIam
88094332d3Sopenharmony_ci} // namespace OHOS
89094332d3Sopenharmony_ci#endif // PIN_AUTH_H
90