1/*
2 * Copyright (c) 2022-2024 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#ifndef PIN_AUTH_H
17#define PIN_AUTH_H
18
19#include <cstdint>
20#include <mutex>
21#include <vector>
22#include "nocopyable.h"
23
24namespace OHOS {
25namespace UserIam {
26namespace PinAuth {
27struct PinCredentialInfo {
28    uint64_t subType;
29    uint32_t remainTimes;
30    uint32_t freezingTime;
31    int32_t nextFailLockoutDuration;
32};
33
34struct PinAlgoParam {
35    uint32_t algoVersion;
36    uint64_t subType;
37    std::vector<uint8_t> algoParameter;
38    std::vector<uint8_t> challenge;
39};
40
41class PinAuth {
42public:
43    DISALLOW_COPY_AND_MOVE(PinAuth);
44    PinAuth() = default;
45    ~PinAuth() = default;
46    int32_t Init();
47    int32_t Close();
48
49    int32_t GetExecutorInfo(int32_t executorRole, std::vector<uint8_t> &pubKey, uint32_t &esl,
50        uint32_t &maxTemplateAcl);
51
52    // for all in one executor
53    int32_t SetAllInOneFwkParam(
54        const std::vector<uint64_t> &templateIdList, const std::vector<uint8_t> &frameworkPublicKey);
55    int32_t EnrollPin(uint64_t scheduleId, uint64_t subType, std::vector<uint8_t> &salt,
56        const std::vector<uint8_t> &pinData, std::vector<uint8_t> &result);
57    int32_t AuthPin(uint64_t scheduleId, uint64_t templateId, const std::vector<uint8_t> &pinData,
58        std::vector<uint8_t> &result);
59    void WriteAntiBrute(uint64_t templateId);
60    int32_t QueryPinInfo(uint64_t templateId, PinCredentialInfo &pinCredentialInfoRet);
61    int32_t DeleteTemplate(uint64_t templateId);
62    int32_t GenerateAlgoParameter(std::vector<uint8_t> &algoParameter, uint32_t &algoVersion);
63    int32_t AllInOneAuth(
64        uint64_t scheduleId, uint64_t templateId, const std::vector<uint8_t> &extraInfo, PinAlgoParam &pinAlgoParam);
65
66    // for collector executor
67    int32_t SetCollectorFwkParam(const std::vector<uint8_t> &frameworkPublicKey);
68    int32_t Collect(uint64_t scheduleId, const std::vector<uint8_t> &extraInfo, std::vector<uint8_t> &msg);
69    int32_t CancelCollect();
70    int32_t SendMessageToCollector(uint64_t scheduleId, const std::vector<uint8_t> &msg, PinAlgoParam &pinAlgoParam);
71    int32_t SetDataToCollector(uint64_t scheduleId, const std::vector<uint8_t> &data, std::vector<uint8_t> &msg);
72
73    // for collector executor
74    int32_t SetVerifierFwkParam(const std::vector<uint8_t> &frameworkPublicKey);
75    int32_t VerifierAuth(
76        uint64_t scheduleId, uint64_t templateId, const std::vector<uint8_t> &extraInfo, std::vector<uint8_t> &msgOut);
77    int32_t CancelVerifierAuth();
78    int32_t SendMessageToVerifier(uint64_t scheduleId,
79        const std::vector<uint8_t> &msgIn, std::vector<uint8_t> &msgOut, bool &isAuthEnd, int32_t &compareResult);
80
81private:
82    int32_t SetVectorByBuffer(std::vector<uint8_t> &vec, const uint8_t *buf, uint32_t bufSize);
83    int32_t PinResultToCoAuthResult(int32_t resultCode);
84    std::mutex mutex_;
85};
86} // namespace PinAuth
87} // namespace UserIam
88} // namespace OHOS
89#endif // PIN_AUTH_H
90