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 AUTH_SESSION_KEY_H
17 #define AUTH_SESSION_KEY_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 
22 #include "auth_interface.h"
23 #include "common_list.h"
24 #include "softbus_adapter_crypto.h"
25 #include "softbus_def.h"
26 
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif
31 #endif
32 
33 #define ENCRYPT_INDEX_LEN 4
34 #define ENCRYPT_OVER_HEAD_LEN (OVERHEAD_LEN + ENCRYPT_INDEX_LEN)
35 
36 typedef struct {
37     uint8_t value[SESSION_KEY_LENGTH];
38     uint32_t len;
39 } SessionKey;
40 
41 typedef struct {
42     const uint8_t *inData;
43     uint32_t inLen;
44 } InDataInfo;
45 typedef ListNode SessionKeyList;
46 
47 void InitSessionKeyList(SessionKeyList *list);
48 void DestroySessionKeyList(SessionKeyList *list);
49 int32_t DupSessionKeyList(const SessionKeyList *srcList, SessionKeyList *dstList);
50 
51 uint64_t GetLatestAvailableSessionKeyTime(const SessionKeyList *list, AuthLinkType type);
52 bool HasSessionKey(const SessionKeyList *list);
53 AuthLinkType GetSessionKeyTypeByIndex(const SessionKeyList *list, int32_t index);
54 int32_t AddSessionKey(SessionKeyList *list, int32_t index, const SessionKey *key, AuthLinkType type,
55     bool isOldKey);
56 int32_t SetSessionKeyAvailable(SessionKeyList *list, int32_t index);
57 int32_t GetLatestSessionKey(const SessionKeyList *list, AuthLinkType type, int32_t *index, SessionKey *key);
58 int32_t GetSessionKeyByIndex(const SessionKeyList *list, int32_t index, AuthLinkType type, SessionKey *key);
59 int32_t SetSessionKeyAuthLinkType(const SessionKeyList *list, int32_t index, AuthLinkType type);
60 bool CheckSessionKeyListExistType(const SessionKeyList *list, AuthLinkType type);
61 bool CheckSessionKeyListHasOldKey(const SessionKeyList *list, AuthLinkType type);
62 int32_t ClearOldKey(const SessionKeyList *list, AuthLinkType type);
63 void RemoveSessionkeyByIndex(SessionKeyList *list, int32_t index, AuthLinkType type);
64 void ClearSessionkeyByAuthLinkType(int64_t authId, SessionKeyList *list, AuthLinkType type);
65 
66 int32_t EncryptInner(const SessionKeyList *list, AuthLinkType type, const InDataInfo *inDataInfo,
67     uint8_t **outData, uint32_t *outLen);
68 int32_t DecryptInner(const SessionKeyList *list, AuthLinkType type, const InDataInfo *inDataInfo,
69     uint8_t **outData, uint32_t *outLen);
70 
71 int32_t EncryptData(const SessionKeyList *list, AuthLinkType type, const InDataInfo *inDataInfo,
72     uint8_t *outData, uint32_t *outLen);
73 int32_t DecryptData(const SessionKeyList *list, AuthLinkType type, const InDataInfo *inDataInfo,
74     uint8_t *outData, uint32_t *outLen);
75 
76 void ScheduleUpdateSessionKey(AuthHandle authHandle, uint64_t delatMs);
77 void CancelUpdateSessionKey(int64_t authId);
78 
79 /* For Debug */
80 void DumpSessionkeyList(const SessionKeyList *list);
81 
82 #ifdef __cplusplus
83 #if __cplusplus
84 }
85 #endif
86 #endif
87 #endif /* AUTH_SESSION_KEY_H */
88