1 /*
2 * Copyright (c) 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 #include <stdbool.h>
16 #include <stdlib.h>
17
18 #include <securec.h>
19
20 #include "lnn_common_utils.h"
21 #include "lnn_log.h"
22 #include "lnn_oobe_manager.h"
23 #include "softbus_adapter_crypto.h"
24 #include "softbus_adapter_mem.h"
25 #include "softbus_def.h"
26 #include "softbus_errcode.h"
27 #include "softbus_utils.h"
28
IsEnableSoftBusHeartbeat(void)29 bool IsEnableSoftBusHeartbeat(void)
30 {
31 return true;
32 }
33
IsOOBEState(void)34 bool IsOOBEState(void)
35 {
36 SoftBusOOBEState state = SOFTBUS_OOBE_RUNNING;
37 if (LnnGetOOBEState(&state) != SOFTBUS_OK) {
38 LNN_LOGE(LNN_STATE, "get oobe state fail");
39 return true;
40 }
41 return state != SOFTBUS_OOBE_END;
42 }
43
IsScreenUnlock(void)44 bool IsScreenUnlock(void)
45 {
46 return true;
47 }
48
LnnEncryptAesGcm(AesGcmInputParam * in,int32_t keyIndex,uint8_t ** out,uint32_t * outLen)49 int32_t LnnEncryptAesGcm(AesGcmInputParam *in, int32_t keyIndex, uint8_t **out, uint32_t *outLen)
50 {
51 if (in == NULL || out == NULL || outLen == NULL) {
52 return SOFTBUS_INVALID_PARAM;
53 }
54 (void)keyIndex;
55 if (in->dataLen >= UINT32_MAX - OVERHEAD_LEN) {
56 LNN_LOGE(LNN_STATE, "dataLen is invalid");
57 return SOFTBUS_INVALID_PARAM;
58 }
59 uint32_t encDataLen = in->dataLen + OVERHEAD_LEN;
60 uint8_t *encData = (uint8_t *)SoftBusCalloc(encDataLen);
61 if (encData == NULL) {
62 LNN_LOGE(LNN_STATE, "calloc encrypt data fail");
63 return SOFTBUS_MALLOC_ERR;
64 }
65 AesGcmCipherKey cipherKey = {.keyLen = in->keyLen};
66 if (memcpy_s(cipherKey.key, sizeof(cipherKey.key), in->key, in->keyLen) != EOK) {
67 LNN_LOGE(LNN_STATE, "copy session key fail");
68 SoftBusFree(encData);
69 return SOFTBUS_MEM_ERR;
70 }
71 int32_t ret = SoftBusEncryptData(&cipherKey, in->data, in->dataLen, encData, &encDataLen);
72 (void)memset_s(&cipherKey, sizeof(AesGcmCipherKey), 0, sizeof(AesGcmCipherKey));
73 if (ret != SOFTBUS_OK) {
74 LNN_LOGE(LNN_STATE, "SoftBusEncryptData fail=%{public}d", ret);
75 SoftBusFree(encData);
76 return SOFTBUS_ENCRYPT_ERR;
77 }
78 *out = encData;
79 *outLen = encDataLen;
80 return SOFTBUS_OK;
81 }
82
LnnDecryptAesGcm(AesGcmInputParam * in,uint8_t ** out,uint32_t * outLen)83 int32_t LnnDecryptAesGcm(AesGcmInputParam *in, uint8_t **out, uint32_t *outLen)
84 {
85 if (in == NULL || out == NULL || outLen == NULL) {
86 return SOFTBUS_INVALID_PARAM;
87 }
88 if (in->dataLen <= OVERHEAD_LEN) {
89 LNN_LOGE(LNN_STATE, "dataLen is invalid");
90 return SOFTBUS_INVALID_PARAM;
91 }
92 uint32_t decDataLen = in->dataLen - OVERHEAD_LEN;
93 uint8_t *decData = (uint8_t *)SoftBusCalloc(decDataLen);
94 if (decData == NULL) {
95 LNN_LOGE(LNN_STATE, "malloc decrypt data fail");
96 return SOFTBUS_MALLOC_ERR;
97 }
98 AesGcmCipherKey cipherKey = {.keyLen = in->keyLen};
99 if (memcpy_s(cipherKey.key, sizeof(cipherKey.key), in->key, in->keyLen) != EOK) {
100 LNN_LOGE(LNN_STATE, "copy session key fail");
101 SoftBusFree(decData);
102 return SOFTBUS_MEM_ERR;
103 }
104 int32_t ret = SoftBusDecryptData(&cipherKey, in->data, in->dataLen, decData, &decDataLen);
105 (void)memset_s(&cipherKey, sizeof(AesGcmCipherKey), 0, sizeof(AesGcmCipherKey));
106 if (ret != SOFTBUS_OK) {
107 LNN_LOGE(LNN_STATE, "SoftBusDecryptData fail=%{public}d", ret);
108 SoftBusFree(decData);
109 return SOFTBUS_ENCRYPT_ERR;
110 }
111 *out = decData;
112 *outLen = decDataLen;
113 return SOFTBUS_OK;
114 }
115