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