1 /*
2  * Copyright (c) 2023-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 #include "softbus_aes_encrypt.h"
17 
18 #include <openssl/evp.h>
19 #include <openssl/hmac.h>
20 #include <openssl/rand.h>
21 #include <securec.h>
22 
23 #include "openssl/aes.h"
24 
25 #include "comm_log.h"
26 #include "softbus_adapter_crypto.h"
27 #include "softbus_adapter_mem.h"
28 #include "softbus_errcode.h"
29 
30 #define AES_128_CFB_KEYLEN             16
31 #define AES_256_CFB_KEYLEN             32
32 #define AES_128_GCM_KEYLEN             16
33 #define AES_256_GCM_KEYLEN             32
34 #define AES_128_CFB_BITS_LEN           128
35 #define AES_256_CFB_BITS_LEN           256
36 #define OPENSSL_EVP_PADDING_FUNC_OPEN  (1)
37 #define OPENSSL_EVP_PADDING_FUNC_CLOSE (0)
38 
SoftBusGenerateHmacHash(const EncryptKey * randomKey,const uint8_t * rootKey,uint32_t rootKeyLen,uint8_t * hash,uint32_t hashLen)39 int32_t SoftBusGenerateHmacHash(
40     const EncryptKey *randomKey, const uint8_t *rootKey, uint32_t rootKeyLen, uint8_t *hash, uint32_t hashLen)
41 {
42     uint32_t outBufLen;
43     uint8_t tempOutputData[EVP_MAX_MD_SIZE];
44 
45     if (randomKey == NULL || rootKey == NULL || rootKeyLen == 0 || hash == NULL || hashLen < SHA256_MAC_LEN) {
46         COMM_LOGE(COMM_ADAPTER, "invalid param.");
47         return SOFTBUS_INVALID_PARAM;
48     }
49     HMAC_CTX *ctx = HMAC_CTX_new();
50     if (ctx == NULL) {
51         COMM_LOGE(COMM_ADAPTER, "HMAC_CTX_new failed.");
52         return SOFTBUS_HMAC_ERR;
53     }
54     if (HMAC_CTX_reset(ctx) != 1) {
55         COMM_LOGE(COMM_ADAPTER, "HMAC_CTX_reset failed.");
56         HMAC_CTX_free(ctx);
57         return SOFTBUS_HMAC_ERR;
58     }
59     if (HMAC_Init_ex(ctx, rootKey, rootKeyLen, EVP_sha256(), NULL) != 1) {
60         COMM_LOGE(COMM_ADAPTER, "HMAC_Init_ex failed.");
61         HMAC_CTX_free(ctx);
62         return SOFTBUS_HMAC_ERR;
63     }
64     if (HMAC_Update(ctx, randomKey->key, (size_t)randomKey->len) != 1) {
65         COMM_LOGE(COMM_ADAPTER, "HMAC_Update failed.");
66         HMAC_CTX_free(ctx);
67         return SOFTBUS_HMAC_ERR;
68     }
69     if (HMAC_Final(ctx, tempOutputData, &outBufLen) != 1) {
70         COMM_LOGE(COMM_ADAPTER, "HMAC_Final failed.");
71         HMAC_CTX_free(ctx);
72         return SOFTBUS_HMAC_ERR;
73     }
74     HMAC_CTX_free(ctx);
75     if (outBufLen != SHA256_MAC_LEN) {
76         COMM_LOGE(COMM_ADAPTER, "outBufLen is invalid length for hash.");
77         (void)memset_s(tempOutputData, sizeof(tempOutputData), 0, sizeof(tempOutputData));
78         return SOFTBUS_HMAC_ERR;
79     }
80     if (memcpy_s(hash, hashLen, tempOutputData, outBufLen) != EOK) {
81         COMM_LOGE(COMM_ADAPTER, "hash result memcpy_s failed.");
82         (void)memset_s(tempOutputData, sizeof(tempOutputData), 0, sizeof(tempOutputData));
83         return SOFTBUS_MEM_ERR;
84     }
85     (void)memset_s(tempOutputData, sizeof(tempOutputData), 0, sizeof(tempOutputData));
86     return SOFTBUS_OK;
87 }
88 
OpensslAesCfbEncrypt(AesCipherKey * cipherKey,const AesInputData * inData,int32_t encMode,AesOutputData * outData)89 static int32_t OpensslAesCfbEncrypt(
90     AesCipherKey *cipherKey, const AesInputData *inData, int32_t encMode, AesOutputData *outData)
91 {
92     int32_t num = 0;
93     AES_KEY aes;
94 
95     if (cipherKey == NULL || cipherKey->ivLen != AES_IV_LENGTH || inData == NULL || inData->data == NULL ||
96         outData == NULL || (encMode != ENCRYPT_MODE && encMode != DECRYPT_MODE)) {
97         COMM_LOGE(COMM_ADAPTER, "invalid param.");
98         return SOFTBUS_INVALID_PARAM;
99     }
100     int32_t bits = 0;
101     switch (cipherKey->keyLen) {
102         case AES_128_CFB_KEYLEN:
103             bits = AES_128_CFB_BITS_LEN;
104             break;
105         case AES_256_CFB_KEYLEN:
106             bits = AES_256_CFB_BITS_LEN;
107             break;
108         default:
109             COMM_LOGE(COMM_ADAPTER, "cipherKey->keyLen unable to get encryption bits.");
110             return SOFTBUS_INVALID_PARAM;
111     }
112     if (AES_set_encrypt_key(cipherKey->key, bits, &aes) < 0) {
113         COMM_LOGE(COMM_ADAPTER, "SoftbusAesCfbEncrypt unable to set encryption key in AES.");
114         return SOFTBUS_ENCRYPT_ERR;
115     }
116     if (encMode == ENCRYPT_MODE) {
117         AES_cfb128_encrypt(inData->data, outData->data, inData->len, &aes, cipherKey->iv, &num, ENCRYPT_MODE);
118     } else {
119         AES_cfb128_encrypt(inData->data, outData->data, inData->len, &aes, cipherKey->iv, &num, DECRYPT_MODE);
120     }
121     outData->len = inData->len;
122     OPENSSL_cleanse(&aes, sizeof(aes));
123     return SOFTBUS_OK;
124 }
125 
RootKeyGenerateIvAndSessionKey(const EncryptKey * randomKey,EncryptKey * rootKey,AesCipherKey * cipherKey)126 static int32_t RootKeyGenerateIvAndSessionKey(const EncryptKey *randomKey, EncryptKey *rootKey, AesCipherKey *cipherKey)
127 {
128     uint8_t result[SHA256_MAC_LEN] = { 0 };
129     if (SoftBusGenerateHmacHash(randomKey, rootKey->key, rootKey->len, result, sizeof(result)) != SOFTBUS_OK) {
130         COMM_LOGE(COMM_ADAPTER, "SslHmacSha256 failed.");
131         return SOFTBUS_HMAC_ERR;
132     }
133     if (memcpy_s(cipherKey->key, cipherKey->keyLen, result, AES_SESSION_KEY_LENGTH) != EOK) {
134         COMM_LOGE(COMM_ADAPTER, "RootKeyGenerateIvAndSessionKey fill sessionKey failed!");
135         (void)memset_s(result, sizeof(result), 0, sizeof(result));
136         return SOFTBUS_MEM_ERR;
137     }
138     if (memcpy_s(cipherKey->iv, cipherKey->ivLen, result + AES_SESSION_KEY_LENGTH,
139             SHA256_MAC_LEN - AES_SESSION_KEY_LENGTH) != EOK) {
140         COMM_LOGE(COMM_ADAPTER, "RootKeyGenerateIvAndSessionKey fill iv failed!");
141         (void)memset_s(result, sizeof(result), 0, sizeof(result));
142         return SOFTBUS_MEM_ERR;
143     }
144     (void)memset_s(result, sizeof(result), 0, sizeof(result));
145     return SOFTBUS_OK;
146 }
147 
GenerateIvAndSessionKey(const EncryptKey * randomKey,EncryptKey * rootKey,AesCipherKey * cipherKey)148 static int32_t GenerateIvAndSessionKey(const EncryptKey *randomKey, EncryptKey *rootKey, AesCipherKey *cipherKey)
149 {
150     if (cipherKey == NULL) {
151         COMM_LOGE(COMM_ADAPTER, "invalid param.");
152         return SOFTBUS_INVALID_PARAM;
153     }
154     cipherKey->keyLen = AES_SESSION_KEY_LENGTH;
155     cipherKey->ivLen = AES_IV_LENGTH;
156     cipherKey->key = (uint8_t *)SoftBusCalloc(cipherKey->keyLen);
157     if (cipherKey->key == NULL) {
158         return SOFTBUS_MALLOC_ERR;
159     }
160     cipherKey->iv = (uint8_t *)SoftBusCalloc(cipherKey->ivLen);
161     if (cipherKey->iv == NULL) {
162         SoftBusFree(cipherKey->key);
163         return SOFTBUS_MALLOC_ERR;
164     }
165     if (RootKeyGenerateIvAndSessionKey(randomKey, rootKey, cipherKey) != SOFTBUS_OK) {
166         COMM_LOGE(COMM_ADAPTER, "RootKeyGenerateIvAndSessionKey failed!");
167         (void)memset_s(cipherKey->key, cipherKey->keyLen, 0, cipherKey->keyLen);
168         (void)memset_s(cipherKey->iv, cipherKey->ivLen, 0, cipherKey->ivLen);
169         SoftBusFree(cipherKey->key);
170         SoftBusFree(cipherKey->iv);
171         return SOFTBUS_GENERATE_KEY_FAIL;
172     }
173     return SOFTBUS_OK;
174 }
175 
SoftBusAesCfbRootEncrypt(const AesInputData * inData,const EncryptKey * randomKey,EncryptKey * rootKey,int32_t encMode,AesOutputData * outData)176 int32_t SoftBusAesCfbRootEncrypt(const AesInputData *inData, const EncryptKey *randomKey, EncryptKey *rootKey,
177     int32_t encMode, AesOutputData *outData)
178 {
179     if (inData == NULL || inData->data == NULL || randomKey == NULL || randomKey->key == NULL || rootKey == NULL ||
180         rootKey->key == NULL || outData == NULL || (encMode != ENCRYPT_MODE && encMode != DECRYPT_MODE)) {
181         COMM_LOGE(COMM_ADAPTER, "invalid param.");
182         return SOFTBUS_INVALID_PARAM;
183     }
184     AesCipherKey cipherKey = { 0 };
185     AesOutputData encryptData = { .data = (uint8_t *)SoftBusCalloc(inData->len), .len = inData->len };
186     if (encryptData.data == NULL) {
187         COMM_LOGE(COMM_ADAPTER, "encryptData calloc failed.");
188         return SOFTBUS_MEM_ERR;
189     }
190     if (GenerateIvAndSessionKey(randomKey, rootKey, &cipherKey) != SOFTBUS_OK) {
191         COMM_LOGE(COMM_ADAPTER, "GenerateIvAndSessionKey failed!");
192         SoftBusFree(encryptData.data);
193         return SOFTBUS_GENERATE_KEY_FAIL;
194     }
195     if (OpensslAesCfbEncrypt(&cipherKey, inData, encMode, &encryptData) != SOFTBUS_OK) {
196         COMM_LOGE(COMM_ADAPTER, "OpensslAesCfb encrypt or decrypt by root key failed.");
197         (void)memset_s(cipherKey.key, cipherKey.keyLen, 0, cipherKey.keyLen);
198         (void)memset_s(cipherKey.iv, cipherKey.ivLen, 0, cipherKey.ivLen);
199         SoftBusFree(cipherKey.key);
200         SoftBusFree(cipherKey.iv);
201         SoftBusFree(encryptData.data);
202         encryptData.data = NULL;
203         return SOFTBUS_ENCRYPT_ERR;
204     }
205     (void)memset_s(cipherKey.key, cipherKey.keyLen, 0, cipherKey.keyLen);
206     (void)memset_s(cipherKey.iv, cipherKey.ivLen, 0, cipherKey.ivLen);
207     SoftBusFree(cipherKey.key);
208     SoftBusFree(cipherKey.iv);
209     outData->len = encryptData.len;
210     outData->data = encryptData.data;
211     return SOFTBUS_OK;
212 }
213 
SoftBusAesCfbEncrypt(const AesInputData * inData,AesCipherKey * cipherKey,int32_t encMode,AesOutputData * outData)214 int32_t SoftBusAesCfbEncrypt(
215     const AesInputData *inData, AesCipherKey *cipherKey, int32_t encMode, AesOutputData *outData)
216 {
217     uint8_t random[RANDOM_LENGTH] = { 0 };
218     uint8_t result[SHA256_MAC_LEN] = { 0 };
219 
220     if (inData == NULL || inData->data == NULL || cipherKey == NULL || cipherKey->ivLen < RANDOM_LENGTH ||
221         outData == NULL || (encMode != ENCRYPT_MODE && encMode != DECRYPT_MODE)) {
222         COMM_LOGE(COMM_ADAPTER, "invalid param.");
223         return SOFTBUS_INVALID_PARAM;
224     }
225     if (memcpy_s(random, sizeof(random), cipherKey->iv, sizeof(random)) != EOK) {
226         COMM_LOGE(COMM_ADAPTER, "random memcpy_s failed!");
227         return SOFTBUS_MEM_ERR;
228     }
229     EncryptKey key = { cipherKey->key, cipherKey->keyLen };
230     if (SoftBusGenerateHmacHash(&key, random, sizeof(random), result, SHA256_MAC_LEN) != SOFTBUS_OK) {
231         COMM_LOGE(COMM_ADAPTER, "SslHmacSha256 failed.");
232         (void)memset_s(random, sizeof(random), 0, sizeof(random));
233         return SOFTBUS_HMAC_ERR;
234     }
235     (void)memset_s(cipherKey->key, cipherKey->keyLen, 0, cipherKey->keyLen);
236     if (memcpy_s(cipherKey->key, cipherKey->keyLen, result, SHA256_MAC_LEN) != EOK) {
237         COMM_LOGE(COMM_ADAPTER, "fill cipherKey->key failed!");
238         (void)memset_s(random, sizeof(random), 0, sizeof(random));
239         (void)memset_s(result, sizeof(result), 0, sizeof(result));
240         return SOFTBUS_MEM_ERR;
241     }
242     AesOutputData encryptData = { .data = (uint8_t *)SoftBusCalloc(inData->len), .len = inData->len };
243     if (encryptData.data == NULL) {
244         COMM_LOGE(COMM_ADAPTER, "encryptData calloc failed.");
245         (void)memset_s(random, sizeof(random), 0, sizeof(random));
246         (void)memset_s(result, sizeof(result), 0, sizeof(result));
247         return SOFTBUS_MALLOC_ERR;
248     }
249     if (OpensslAesCfbEncrypt(cipherKey, inData, encMode, &encryptData) != SOFTBUS_OK) {
250         COMM_LOGE(COMM_ADAPTER, "OpensslAesCfbEncrypt failed.");
251         (void)memset_s(random, sizeof(random), 0, sizeof(random));
252         (void)memset_s(result, sizeof(result), 0, sizeof(result));
253         SoftBusFree(encryptData.data);
254         encryptData.data = NULL;
255         return SOFTBUS_ENCRYPT_ERR;
256     }
257 
258     outData->data = encryptData.data;
259     outData->len = encryptData.len;
260     return SOFTBUS_OK;
261 }
262 
GetSslGcmAlgorithmByKeyLen(uint32_t keyLen)263 static EVP_CIPHER *GetSslGcmAlgorithmByKeyLen(uint32_t keyLen)
264 {
265     switch (keyLen) {
266         case AES_128_GCM_KEYLEN:
267             return (EVP_CIPHER *)EVP_aes_128_gcm();
268         case AES_256_GCM_KEYLEN:
269             return (EVP_CIPHER *)EVP_aes_256_gcm();
270         default:
271             COMM_LOGE(COMM_ADAPTER, "Get SslGcmAlgorithm ByKeyLen failed.");
272             return NULL;
273     }
274     return NULL;
275 }
276 
GcmOpensslEvpInit(EVP_CIPHER_CTX ** ctx,uint32_t keyLen,int32_t encMode)277 static int32_t GcmOpensslEvpInit(EVP_CIPHER_CTX **ctx, uint32_t keyLen, int32_t encMode)
278 {
279     if (ctx == NULL || keyLen == 0 || (encMode != ENCRYPT_MODE && encMode != DECRYPT_MODE)) {
280         COMM_LOGE(COMM_ADAPTER, "invalid param.");
281         return SOFTBUS_INVALID_PARAM;
282     }
283     EVP_CIPHER *cipher = GetSslGcmAlgorithmByKeyLen(keyLen);
284     if (cipher == NULL) {
285         COMM_LOGE(COMM_ADAPTER, "GetSslGcmAlgorithmByKeyLen failed.");
286         return SOFTBUS_INVALID_PARAM;
287     }
288     *ctx = EVP_CIPHER_CTX_new();
289     if (*ctx == NULL) {
290         COMM_LOGE(COMM_ADAPTER, "EVP_CIPHER_CTX_new failed.");
291         return SOFTBUS_MALLOC_ERR;
292     }
293     EVP_CIPHER_CTX_set_padding(*ctx, OPENSSL_EVP_PADDING_FUNC_CLOSE);
294     if (encMode == ENCRYPT_MODE) {
295         if (EVP_EncryptInit_ex(*ctx, cipher, NULL, NULL, NULL) != 1) {
296             COMM_LOGE(COMM_ADAPTER, "EVP_EncryptInit_ex failed.");
297             EVP_CIPHER_CTX_free(*ctx);
298             *ctx = NULL;
299             return SOFTBUS_ENCRYPT_ERR;
300         }
301     } else {
302         if (EVP_DecryptInit_ex(*ctx, cipher, NULL, NULL, NULL) != 1) {
303             COMM_LOGE(COMM_ADAPTER, "EVP_DecryptInit_ex failed.");
304             EVP_CIPHER_CTX_free(*ctx);
305             *ctx = NULL;
306             return SOFTBUS_DECRYPT_ERR;
307         }
308     }
309     if (EVP_CIPHER_CTX_ctrl(*ctx, EVP_CTRL_GCM_SET_IVLEN, AES_IV_LENGTH, NULL) != 1) {
310         COMM_LOGE(COMM_ADAPTER, "EVP_CIPHER_CTX_ctrl failed.");
311         EVP_CIPHER_CTX_free(*ctx);
312         *ctx = NULL;
313         return SOFTBUS_GCM_SET_IV_FAIL;
314     }
315     return SOFTBUS_OK;
316 }
317 
OpensslAesGcmEncrypt(const uint8_t * srcData,uint32_t srcDataLen,AesCipherKey * cipherKey,uint8_t * outData,uint32_t * outDataLen)318 static int32_t OpensslAesGcmEncrypt(
319     const uint8_t *srcData, uint32_t srcDataLen, AesCipherKey *cipherKey, uint8_t *outData, uint32_t *outDataLen)
320 {
321     if (srcData == NULL || srcDataLen == 0 || cipherKey == NULL || outData == NULL || outDataLen == NULL ||
322         *outDataLen < (srcDataLen + AES_GCM_TAG_LEN)) {
323         COMM_LOGE(COMM_ADAPTER, "invalid param.");
324         return SOFTBUS_INVALID_PARAM;
325     }
326     EVP_CIPHER_CTX *ctx = NULL;
327     int32_t ret = GcmOpensslEvpInit(&ctx, cipherKey->keyLen, ENCRYPT_MODE);
328     COMM_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_ENCRYPT_ERR, COMM_ADAPTER, "GcmOpensslEvpInit failed.");
329     if (EVP_EncryptInit_ex(ctx, NULL, NULL, cipherKey->key, cipherKey->iv) != 1) {
330         COMM_LOGE(COMM_ADAPTER, "EVP_EncryptInit_ex failed.");
331         goto EXIT;
332     }
333     int32_t outLen = 0;
334     int32_t outBufLen = 0;
335     if (EVP_EncryptUpdate(ctx, outData, &outBufLen, srcData, srcDataLen) != 1) {
336         COMM_LOGE(COMM_ADAPTER, "EVP_EncryptUpdate failed.");
337         goto EXIT;
338     }
339     outLen += outBufLen;
340     if (EVP_EncryptFinal_ex(ctx, outData + outBufLen, &outBufLen) != 1) {
341         COMM_LOGE(COMM_ADAPTER, "EVP_EncryptFinal_ex failed.");
342         goto EXIT;
343     }
344     if (outBufLen > INT32_MAX - outLen) {
345         COMM_LOGE(COMM_ADAPTER, "outLen convert overflow.");
346         goto EXIT;
347     }
348     outLen += outBufLen;
349     if (*outDataLen < ((uint32_t)outLen + AES_GCM_TAG_LEN)) {
350         COMM_LOGE(COMM_ADAPTER, "invalid param. *outDataLen=%{public}u, outLen=%{public}u", *outDataLen,
351             (uint32_t)outLen);
352         goto EXIT;
353     }
354     uint8_t tagbuf[AES_GCM_TAG_LEN]; // outData has two part: EncryptedData & AES-GCM-TAG
355     if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, AES_GCM_TAG_LEN, (void *)tagbuf) != 1) {
356         COMM_LOGE(COMM_ADAPTER, "EVP_CTRL_GCM_GET_TAG failed.");
357         goto EXIT;
358     }
359     if (memcpy_s(outData + outLen, *outDataLen - outLen, tagbuf, AES_GCM_TAG_LEN) != EOK) {
360         COMM_LOGE(COMM_ADAPTER, "tag memcpy_s failed.");
361         goto EXIT;
362     }
363     *outDataLen = outLen + AES_GCM_TAG_LEN;
364     EVP_CIPHER_CTX_free(ctx);
365     return SOFTBUS_OK;
366 EXIT:
367     EVP_CIPHER_CTX_free(ctx);
368     return SOFTBUS_ENCRYPT_ERR;
369 }
370 
OpensslAesGcmDecrypt(const uint8_t * srcData,uint32_t srcDataLen,AesCipherKey * cipherKey,uint8_t * outData,uint32_t * outDataLen)371 static int32_t OpensslAesGcmDecrypt(
372     const uint8_t *srcData, uint32_t srcDataLen, AesCipherKey *cipherKey, uint8_t *outData, uint32_t *outDataLen)
373 {
374     if (srcData == NULL || srcDataLen <= AES_GCM_TAG_LEN || cipherKey == NULL || outData == NULL ||
375         outDataLen == NULL || *outDataLen < (srcDataLen - AES_GCM_TAG_LEN)) {
376         COMM_LOGE(COMM_ADAPTER, "invalid param.");
377         return SOFTBUS_INVALID_PARAM;
378     }
379     EVP_CIPHER_CTX *ctx = NULL;
380     if (GcmOpensslEvpInit(&ctx, cipherKey->keyLen, DECRYPT_MODE) != SOFTBUS_OK) {
381         COMM_LOGE(COMM_ADAPTER, "GcmOpensslEvpInit failed.");
382         return SOFTBUS_DECRYPT_ERR;
383     }
384     if (EVP_DecryptInit_ex(ctx, NULL, NULL, cipherKey->key, cipherKey->iv) != 1) {
385         COMM_LOGE(COMM_ADAPTER, "EVP_DecryptInit_ex failed.");
386         EVP_CIPHER_CTX_free(ctx);
387         return SOFTBUS_DECRYPT_ERR;
388     }
389     int32_t outLen = 0;
390     int32_t outBufLen = 0;
391     uint8_t trueEncryptedData[srcDataLen - AES_GCM_TAG_LEN];
392     if (memcpy_s(trueEncryptedData, srcDataLen - AES_GCM_TAG_LEN, srcData, srcDataLen - AES_GCM_TAG_LEN) != EOK) {
393         COMM_LOGE(COMM_ADAPTER, "trueEncryptedData memcpy_s failed.");
394         goto EXIT;
395     }
396     if (EVP_CIPHER_CTX_ctrl(
397         ctx, EVP_CTRL_GCM_SET_TAG, AES_GCM_TAG_LEN, (void *)(srcData + (srcDataLen - AES_GCM_TAG_LEN))) != 1) {
398         COMM_LOGE(COMM_ADAPTER, "EVP_CTRL_GCM_SET_TAG failed.");
399         goto EXIT;
400     }
401     if (EVP_DecryptUpdate(ctx, outData, &outBufLen, trueEncryptedData, srcDataLen - AES_GCM_TAG_LEN) != 1) {
402         COMM_LOGE(COMM_ADAPTER, "EVP_DecryptUpdate failed.");
403         goto EXIT;
404     }
405     outLen += outBufLen;
406     if (EVP_DecryptFinal_ex(ctx, outData + outBufLen, &outBufLen) != 1) {
407         COMM_LOGE(COMM_ADAPTER, "EVP_DecryptFinal_ex failed.");
408         goto EXIT;
409     }
410     if (outBufLen > INT32_MAX - outLen) {
411         COMM_LOGE(COMM_ADAPTER, "outLen convert overflow.");
412         goto EXIT;
413     }
414     outLen += outBufLen;
415     *outDataLen = outLen;
416     EVP_CIPHER_CTX_free(ctx);
417     return SOFTBUS_OK;
418 EXIT:
419     EVP_CIPHER_CTX_free(ctx);
420     return SOFTBUS_DECRYPT_ERR;
421 }
422 
SoftBusAesGcmEncrypt(const AesInputData * inData,AesCipherKey * cipherKey,int32_t encMode,AesOutputData * outData)423 int32_t SoftBusAesGcmEncrypt(
424     const AesInputData *inData, AesCipherKey *cipherKey, int32_t encMode, AesOutputData *outData)
425 {
426     if (inData == NULL || inData->data == NULL || cipherKey == NULL || cipherKey->key == NULL ||
427         cipherKey->iv == NULL || outData == NULL || (encMode != ENCRYPT_MODE && encMode != DECRYPT_MODE)) {
428         COMM_LOGE(COMM_ADAPTER, "invalid param.");
429         return SOFTBUS_INVALID_PARAM;
430     }
431     uint32_t encryptDataLen = inData->len + AES_GCM_TAG_LEN;
432     uint8_t *encryptData = (uint8_t *)SoftBusCalloc(encryptDataLen);
433     if (encryptData == NULL) {
434         COMM_LOGE(COMM_ADAPTER, "encrypt data calloc fail.");
435         return SOFTBUS_MALLOC_ERR;
436     }
437     if (encMode == ENCRYPT_MODE) {
438         if (OpensslAesGcmEncrypt(inData->data, inData->len, cipherKey, encryptData, &encryptDataLen) != SOFTBUS_OK) {
439             COMM_LOGE(COMM_ADAPTER, "OpensslAesGcmEncrypt failed.");
440             SoftBusFree(encryptData);
441             encryptData = NULL;
442             return SOFTBUS_ENCRYPT_ERR;
443         }
444     } else {
445         if (OpensslAesGcmDecrypt(inData->data, inData->len, cipherKey, encryptData, &encryptDataLen) != SOFTBUS_OK) {
446             COMM_LOGE(COMM_ADAPTER, "OpensslAesGcmDecrypt failed.");
447             SoftBusFree(encryptData);
448             encryptData = NULL;
449             return SOFTBUS_DECRYPT_ERR;
450         }
451     }
452     outData->data = encryptData;
453     outData->len = encryptDataLen;
454     return SOFTBUS_OK;
455 }