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 typedef ListNode SessionKeyList; 41 42 void InitSessionKeyList(SessionKeyList *list); 43 void DestroySessionKeyList(SessionKeyList *list); 44 int32_t DupSessionKeyList(const SessionKeyList *srcList, SessionKeyList *dstList); 45 46 bool HasSessionKey(const SessionKeyList *list); 47 int32_t AddSessionKey(SessionKeyList *list, int32_t index, const SessionKey *key); 48 int32_t GetLatestSessionKey(const SessionKeyList *list, int32_t *index, SessionKey *key); 49 int32_t GetSessionKeyByIndex(const SessionKeyList *list, int32_t index, SessionKey *key); 50 void RemoveSessionkeyByIndex(SessionKeyList *list, int32_t index); 51 52 int32_t EncryptInner(const SessionKeyList *list, const uint8_t *inData, uint32_t inLen, 53 uint8_t **outData, uint32_t *outLen); 54 int32_t DecryptInner(const SessionKeyList *list, const uint8_t *inData, uint32_t inLen, 55 uint8_t **outData, uint32_t *outLen); 56 57 int32_t EncryptData(const SessionKeyList *list, const uint8_t *inData, uint32_t inLen, 58 uint8_t *outData, uint32_t *outLen); 59 int32_t DecryptData(const SessionKeyList *list, const uint8_t *inData, uint32_t inLen, 60 uint8_t *outData, uint32_t *outLen); 61 62 void ScheduleUpdateSessionKey(int64_t authId, uint64_t delatMs); 63 void CancelUpdateSessionKey(int64_t authId); 64 65 /* For Debug */ 66 void DumpSessionkeyList(const SessionKeyList *list); 67 68 #ifdef __cplusplus 69 #if __cplusplus 70 } 71 #endif 72 #endif 73 #endif /* AUTH_SESSION_KEY_H */ 74