• 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 #ifndef PIN_DB_H
17 #define PIN_DB_H
18 
19 #include "defines.h"
20 #include "buffer.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif // __cplusplus
25 
26 #define INVALID_TEMPLATE_ID 0xFFFFFFFFFFFFFFFF
27 #define PIN_DB_TWO_PARAMS 2
28 #define MAX_RANDOM_TIME 10
29 #define DEFAULT_FILE_HEAD "/data/service/el1/public/pinauth/"
30 #define MAX_UINT_LEN 21
31 #define MAX_CRYPTO_INFO_SIZE 33
32 #define CURRENT_VERSION 0
33 #define PIN_INDEX_NAME "/data/service/el1/public/pinauth/PinIndexDb"
34 #define MAX_FILE_NAME_LEN 256
35 #define CRYPTO_SUFFIX "_CryptoInfo"
36 #define ANTI_BRUTE_SUFFIX "_BruteForceCount"
37 #define SALT_SUFFIX "_salt"
38 #define SECRET_SUFFIX "_secret"
39 #define SALT_PREFIX "hkdf_salt"
40 #define CREDENTIAL_PREFIX "template_encryption_key"
41 #define INIT_AUTH_ERROR_COUNT 0
42 #define INIT_START_FREEZE_TIMES 0
43 #define DEFAULT_VALUE 1
44 #define REMAINING_TIMES_FREEZE 1
45 #define FIRST_ANTI_BRUTE_COUNT 5
46 #define SECOND_ANTI_BRUTE_COUNT 8
47 #define THIRD_ANTI_BRUTE_COUNT 11
48 #define ANTI_BRUTE_COUNT_FREQUENCY 3
49 #define ANTI_BRUTE_INTERVAL_COUNT 2
50 #define ATTI_BRUTE_FIRST_STAGE 100
51 #define ATTI_BRUTE_SECOND_STAGE 140
52 #define ONE_MIN_TIME 60
53 #define TEN_MIN_TIME 600
54 #define THIRTY_MIN_TIME 1800
55 #define ONE_HOUR_TIME 3600
56 #define ONE_DAY_TIME 86400
57 #define FIRST_EXPONENTIAL_PARA 30
58 #define SECOND_EXPONENTIAL_PARA 2
59 #define THIRD_EXPONENTIAL_PARA 10
60 #define MS_OF_S 1000ull
61 #define CONST_PIN_DATA_LEN 64U
62 #define CONST_PIN_DATA_EXPAND_LEN 92U
63 #define CONST_SALT_LEN 32U
64 #define CONST_PUB_KEY_LEN 32U
65 #define CONST_CREDENTIAL_PREFIX_LEN 32U
66 #define CONST_EXPAND_DATA_LEN 128U
67 #define RESULT_TLV_LEN 240U
68 
69 typedef struct {
70     uint64_t templateId;
71     uint64_t subType;
72 } __attribute__((__packed__)) PinInfo;
73 
74 typedef struct {
75     uint32_t authErrorCount;
76     uint64_t startFreezeTime;
77 } __attribute__((__packed__)) AntiBruteInfo;
78 
79 typedef struct {
80     PinInfo pinInfo;
81     AntiBruteInfo antiBruteInfo;
82 } __attribute__((__packed__)) PinIndex;
83 
84 typedef struct {
85     uint32_t version;
86     uint32_t pinIndexLen;
87     PinIndex *pinIndex;
88     bool isLoaded;
89 } __attribute__((__packed__)) PinDb;
90 
91 typedef struct {
92     uint64_t scheduleId;
93     uint64_t subType;
94     uint8_t salt[CONST_SALT_LEN];
95     uint8_t pinData[CONST_PIN_DATA_LEN];
96 } __attribute__((__packed__)) PinEnrollParam;
97 
98 void InitPinDb(void);
99 void DestroyPinDb(void);
100 
101 ResultCode AddPin(PinEnrollParam *pinEnrollParam, uint64_t *templateId, Buffer *outRootSecret);
102 ResultCode DoGetSalt(uint64_t templateId, uint8_t *salt, uint32_t *saltLen);
103 ResultCode DelPinById(uint64_t templateId);
104 ResultCode AuthPinById(const uint8_t *inputData, const uint32_t inputDataLen, uint64_t templateId,
105     Buffer *outRootSecret, ResultCode *compareRet);
106 ResultCode ComputeFreezeTime(uint64_t templateId, uint32_t *freezeTime, uint32_t count, uint64_t startFreezeTime);
107 ResultCode GetRemainTimes(uint64_t templateId, uint32_t *remainingAuthTimes, uint32_t authErrorCount);
108 ResultCode GetSubType(uint64_t templateId, uint64_t *subType);
109 ResultCode GetAntiBruteInfo(uint64_t templateId, uint32_t *authErrorCount, uint64_t *startFreezeTime);
110 ResultCode RefreshAntiBruteInfoToFile(uint64_t templateId);
111 ResultCode VerifyTemplateDataPin(const uint64_t *templateIdList, uint32_t templateIdListLen);
112 
113 #ifdef __cplusplus
114 }
115 #endif // __cplusplus
116 #endif  // PIN_DB_H