• 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 #include "softbus_adapter_crypto.h"
17 
18 #include <securec.h>
19 
20 #include <openssl/evp.h>
21 #include <openssl/rand.h>
22 #include "softbus_adapter_file.h"
23 #include "softbus_adapter_log.h"
24 #include "softbus_errcode.h"
25 
26 static SoftBusMutex g_randomLock;
27 
28 #define OPENSSL_EVP_PADDING_FUNC_OPEN (1)
29 #define OPENSSL_EVP_PADDING_FUNC_CLOSE (0)
30 
31 #define EVP_AES_128_KEYLEN 16
32 #define EVP_AES_256_KEYLEN 32
33 
GetGcmAlgorithmByKeyLen(uint32_t keyLen)34 static EVP_CIPHER *GetGcmAlgorithmByKeyLen(uint32_t keyLen)
35 {
36     switch (keyLen) {
37         case EVP_AES_128_KEYLEN:
38             return (EVP_CIPHER *)EVP_aes_128_gcm();
39         case EVP_AES_256_KEYLEN:
40             return (EVP_CIPHER *)EVP_aes_256_gcm();
41         default:
42             return NULL;
43     }
44     return NULL;
45 }
46 
GetCtrAlgorithmByKeyLen(uint32_t keyLen)47 static EVP_CIPHER *GetCtrAlgorithmByKeyLen(uint32_t keyLen)
48 {
49     switch (keyLen) {
50         case EVP_AES_128_KEYLEN:
51             return (EVP_CIPHER *)EVP_aes_128_ctr();
52         case EVP_AES_256_KEYLEN:
53             return (EVP_CIPHER *)EVP_aes_256_ctr();
54         default:
55             return NULL;
56     }
57     return NULL;
58 }
59 
OpensslEvpInit(EVP_CIPHER_CTX ** ctx,const AesGcmCipherKey * cipherkey,bool mode)60 static int32_t OpensslEvpInit(EVP_CIPHER_CTX **ctx, const AesGcmCipherKey *cipherkey, bool mode)
61 {
62     EVP_CIPHER *cipher = GetGcmAlgorithmByKeyLen(cipherkey->keyLen);
63     if (cipher == NULL) {
64         HILOG_ERROR(SOFTBUS_HILOG_ID, "get cipher fail.");
65         return SOFTBUS_DECRYPT_ERR;
66     }
67     int32_t ret;
68     *ctx = EVP_CIPHER_CTX_new();
69     if (*ctx == NULL) {
70         return SOFTBUS_DECRYPT_ERR;
71     }
72     EVP_CIPHER_CTX_set_padding(*ctx, OPENSSL_EVP_PADDING_FUNC_OPEN);
73     if (mode == true) {
74         ret = EVP_EncryptInit_ex(*ctx, cipher, NULL, NULL, NULL);
75         if (ret != 1) {
76             HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP_EncryptInit_ex fail.");
77             EVP_CIPHER_CTX_free(*ctx);
78             return SOFTBUS_DECRYPT_ERR;
79         }
80     } else {
81         ret = EVP_DecryptInit_ex(*ctx, cipher, NULL, NULL, NULL);
82         if (ret != 1) {
83             HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP_DecryptInit_ex fail.");
84             EVP_CIPHER_CTX_free(*ctx);
85             return SOFTBUS_DECRYPT_ERR;
86         }
87     }
88     ret = EVP_CIPHER_CTX_ctrl(*ctx, EVP_CTRL_GCM_SET_IVLEN, GCM_IV_LEN, NULL);
89     if (ret != 1) {
90         HILOG_ERROR(SOFTBUS_HILOG_ID, "Set iv len fail.");
91         EVP_CIPHER_CTX_free(*ctx);
92         return SOFTBUS_DECRYPT_ERR;
93     }
94     return SOFTBUS_OK;
95 }
96 
PackIvAndTag(EVP_CIPHER_CTX * ctx,const AesGcmCipherKey * cipherkey,uint32_t dataLen,unsigned char * cipherText,uint32_t cipherTextLen)97 static int32_t PackIvAndTag(EVP_CIPHER_CTX *ctx, const AesGcmCipherKey *cipherkey,
98     uint32_t dataLen, unsigned char *cipherText, uint32_t cipherTextLen)
99 {
100     if ((dataLen + OVERHEAD_LEN) > cipherTextLen) {
101         HILOG_ERROR(SOFTBUS_HILOG_ID, "Encrypt invalid para.");
102         return SOFTBUS_ENCRYPT_ERR;
103     }
104     if (memcpy_s(cipherText, cipherTextLen - dataLen, cipherkey->iv, GCM_IV_LEN) != EOK) {
105         HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP memcpy iv fail.");
106         return SOFTBUS_ENCRYPT_ERR;
107     }
108     char tagbuf[TAG_LEN];
109     int ret = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, TAG_LEN, (void *)tagbuf);
110     if (ret != 1) {
111         HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP_CIPHER_CTX_ctrl fail.");
112         return SOFTBUS_DECRYPT_ERR;
113     }
114     if (memcpy_s(cipherText + dataLen + GCM_IV_LEN,
115         cipherTextLen - dataLen - GCM_IV_LEN, tagbuf, TAG_LEN) != EOK) {
116         HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP memcpy tag fail.");
117         return SOFTBUS_ENCRYPT_ERR;
118     }
119     return SOFTBUS_OK;
120 }
121 
SslAesGcmEncrypt(const AesGcmCipherKey * cipherkey,const unsigned char * plainText,uint32_t plainTextSize,unsigned char * cipherText,uint32_t cipherTextLen)122 static int32_t SslAesGcmEncrypt(const AesGcmCipherKey *cipherkey, const unsigned char *plainText,
123     uint32_t plainTextSize, unsigned char *cipherText, uint32_t cipherTextLen)
124 {
125     if ((cipherkey == NULL) || (plainText == NULL) || (plainTextSize == 0) || cipherText == NULL ||
126         (cipherTextLen < plainTextSize + OVERHEAD_LEN)) {
127         HILOG_ERROR(SOFTBUS_HILOG_ID, "Encrypt invalid para.");
128         return SOFTBUS_INVALID_PARAM;
129     }
130 
131     int32_t outlen = 0;
132     int32_t outbufLen;
133     EVP_CIPHER_CTX *ctx = NULL;
134     int32_t ret = OpensslEvpInit(&ctx, cipherkey, true);
135     if (ret != SOFTBUS_OK) {
136         HILOG_ERROR(SOFTBUS_HILOG_ID, "OpensslEvpInit fail.");
137         return SOFTBUS_DECRYPT_ERR;
138     }
139     ret = EVP_EncryptInit_ex(ctx, NULL, NULL, cipherkey->key, cipherkey->iv);
140     if (ret != 1) {
141         HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP_EncryptInit_ex fail.");
142         EVP_CIPHER_CTX_free(ctx);
143         return SOFTBUS_DECRYPT_ERR;
144     }
145     ret = EVP_EncryptUpdate(ctx, cipherText + GCM_IV_LEN,
146         (int32_t *)&outbufLen, plainText, plainTextSize);
147     if (ret != 1) {
148         HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP_EncryptUpdate fail.");
149         EVP_CIPHER_CTX_free(ctx);
150         return SOFTBUS_DECRYPT_ERR;
151     }
152     outlen += outbufLen;
153     ret = EVP_EncryptFinal_ex(ctx, cipherText + GCM_IV_LEN + outbufLen, (int32_t *)&outbufLen);
154     if (ret != 1) {
155         HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP_EncryptFinal_ex fail.");
156         EVP_CIPHER_CTX_free(ctx);
157         return SOFTBUS_DECRYPT_ERR;
158     }
159     outlen += outbufLen;
160     ret = PackIvAndTag(ctx, cipherkey, outlen, cipherText, cipherTextLen);
161     if (ret != SOFTBUS_OK) {
162         HILOG_ERROR(SOFTBUS_HILOG_ID, "pack iv and tag fail.");
163         EVP_CIPHER_CTX_free(ctx);
164         return SOFTBUS_DECRYPT_ERR;
165     }
166     EVP_CIPHER_CTX_free(ctx);
167     return (outlen + OVERHEAD_LEN);
168 }
169 
SslAesGcmDecrypt(const AesGcmCipherKey * cipherkey,const unsigned char * cipherText,uint32_t cipherTextSize,unsigned char * plain,uint32_t plainLen)170 static int32_t SslAesGcmDecrypt(const AesGcmCipherKey *cipherkey, const unsigned char *cipherText,
171     uint32_t cipherTextSize, unsigned char *plain, uint32_t plainLen)
172 {
173     if ((cipherkey == NULL) || (cipherText == NULL) || (cipherTextSize <= OVERHEAD_LEN) || plain == NULL ||
174         (plainLen < cipherTextSize - OVERHEAD_LEN)) {
175         HILOG_ERROR(SOFTBUS_HILOG_ID, "Decrypt invalid para.");
176         return SOFTBUS_INVALID_PARAM;
177     }
178 
179     int32_t outlen = 0;
180     EVP_CIPHER_CTX *ctx = NULL;
181     int32_t ret = OpensslEvpInit(&ctx, cipherkey, false);
182     if (ret != SOFTBUS_OK) {
183         HILOG_ERROR(SOFTBUS_HILOG_ID, "OpensslEvpInit fail.");
184         return SOFTBUS_DECRYPT_ERR;
185     }
186     ret = EVP_DecryptInit_ex(ctx, NULL, NULL, cipherkey->key, cipherkey->iv);
187     if (ret != 1) {
188         HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP_EncryptInit_ex fail.");
189         EVP_CIPHER_CTX_free(ctx);
190         return SOFTBUS_DECRYPT_ERR;
191     }
192     ret = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, TAG_LEN,
193         (void *)(cipherText + (cipherTextSize - TAG_LEN)));
194     if (ret != 1) {
195         HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP_DecryptUpdate fail.");
196         EVP_CIPHER_CTX_free(ctx);
197         return SOFTBUS_DECRYPT_ERR;
198     }
199     ret = EVP_DecryptUpdate(ctx, plain, (int32_t *)&plainLen,
200         cipherText + GCM_IV_LEN, cipherTextSize - OVERHEAD_LEN);
201     if (ret != 1) {
202         HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP_DecryptUpdate fail.");
203         EVP_CIPHER_CTX_free(ctx);
204         return SOFTBUS_DECRYPT_ERR;
205     }
206     outlen += plainLen;
207     ret = EVP_DecryptFinal_ex(ctx, plain + plainLen, (int32_t *)&plainLen);
208     if (ret != 1) {
209         HILOG_ERROR(SOFTBUS_HILOG_ID, "EVP_DecryptFinal_ex fail.");
210         EVP_CIPHER_CTX_free(ctx);
211         return SOFTBUS_DECRYPT_ERR;
212     }
213     outlen += plainLen;
214     EVP_CIPHER_CTX_free(ctx);
215     return outlen;
216 }
217 
HandleError(EVP_CIPHER_CTX * ctx,const char * buf)218 static int32_t HandleError(EVP_CIPHER_CTX *ctx, const char *buf)
219 {
220     if (buf != NULL) {
221         HILOG_ERROR(SOFTBUS_HILOG_ID, "%{public}s", buf);
222     }
223     if (ctx != NULL) {
224         EVP_CIPHER_CTX_free(ctx);
225     }
226     return SOFTBUS_DECRYPT_ERR;
227 }
228 
SoftBusBase64Encode(unsigned char * dst,size_t dlen,size_t * olen,const unsigned char * src,size_t slen)229 int32_t SoftBusBase64Encode(unsigned char *dst, size_t dlen,
230     size_t *olen, const unsigned char *src, size_t slen)
231 {
232     if (dst == NULL || dlen == 0 || olen == NULL || src == NULL || slen == 0) {
233         return SOFTBUS_INVALID_PARAM;
234     }
235     *olen = 0;
236     int32_t outlen;
237     EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
238     if (ctx == NULL) {
239         return SOFTBUS_DECRYPT_ERR;
240     }
241     EVP_EncodeInit(ctx);
242     int32_t ret = EVP_EncodeUpdate(ctx, dst, &outlen, src, slen);
243     if (ret != 1) {
244         HILOG_ERROR(SOFTBUS_HILOG_ID, "[TRANS] EVP_EncodeUpdate fail.");
245         EVP_ENCODE_CTX_free(ctx);
246         return SOFTBUS_DECRYPT_ERR;
247     }
248     *olen += outlen;
249     EVP_EncodeFinal(ctx, dst + outlen, &outlen);
250     *olen += outlen;
251     if ((*olen > 0) && (dst[*olen - 1] == '\n')) {
252         (*olen)--;
253         dst[*olen] = 0;
254     }
255     EVP_ENCODE_CTX_free(ctx);
256     return SOFTBUS_OK;
257 }
258 
SoftBusBase64Decode(unsigned char * dst,size_t dlen,size_t * olen,const unsigned char * src,size_t slen)259 int32_t SoftBusBase64Decode(unsigned char *dst, size_t dlen,
260     size_t *olen, const unsigned char *src, size_t slen)
261 {
262     if (dst == NULL || dlen == 0 || olen == NULL || src == NULL || slen == 0) {
263         return SOFTBUS_INVALID_PARAM;
264     }
265     *olen = 0;
266     int32_t outlen;
267     EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
268     if (ctx == NULL) {
269         return SOFTBUS_DECRYPT_ERR;
270     }
271     EVP_DecodeInit(ctx);
272     int32_t ret = EVP_DecodeUpdate(ctx, dst, &outlen, src, slen);
273     if (ret == -1) {
274         HILOG_ERROR(SOFTBUS_HILOG_ID, "[TRANS] EVP_DecodeUpdate fail.");
275         EVP_ENCODE_CTX_free(ctx);
276         return SOFTBUS_DECRYPT_ERR;
277     }
278     *olen += outlen;
279     ret = EVP_DecodeFinal(ctx, dst + outlen, &outlen);
280     if (ret != 1) {
281         HILOG_ERROR(SOFTBUS_HILOG_ID, "[TRANS] EVP_DecodeFinal fail.");
282         EVP_ENCODE_CTX_free(ctx);
283         return SOFTBUS_DECRYPT_ERR;
284     }
285     *olen += outlen;
286     EVP_ENCODE_CTX_free(ctx);
287     return SOFTBUS_OK;
288 }
289 
SoftBusGenerateStrHash(const unsigned char * str,uint32_t len,unsigned char * hash)290 int32_t SoftBusGenerateStrHash(const unsigned char *str, uint32_t len, unsigned char *hash)
291 {
292     if (str == NULL || hash == NULL || len == 0) {
293         return SOFTBUS_INVALID_PARAM;
294     }
295     uint32_t olen;
296     int32_t ret = EVP_Digest(str, len, hash, &olen, EVP_sha256(), NULL);
297     if (ret != 1) {
298         HILOG_ERROR(SOFTBUS_HILOG_ID, "[TRANS] Get Openssl Hash fail.");
299         return SOFTBUS_DECRYPT_ERR;
300     }
301     return SOFTBUS_OK;
302 }
303 
SoftBusGenerateRandomArray(unsigned char * randStr,uint32_t len)304 int32_t SoftBusGenerateRandomArray(unsigned char *randStr, uint32_t len)
305 {
306     if (randStr == NULL || len == 0) {
307         return SOFTBUS_INVALID_PARAM;
308     }
309 
310     static bool initFlag = false;
311     int32_t ret;
312 
313     if (SoftBusMutexInit(&g_randomLock, NULL) != SOFTBUS_OK) {
314         HILOG_ERROR(SOFTBUS_HILOG_ID, "init mutex failed.");
315         return SOFTBUS_ERR;
316     }
317 
318     if (SoftBusMutexLock(&g_randomLock) != SOFTBUS_OK) {
319         HILOG_ERROR(SOFTBUS_HILOG_ID, "lock mutex failed");
320         return SOFTBUS_ERR;
321     }
322     if (initFlag == false) {
323         RAND_seed(randStr, (int32_t)len);
324         initFlag = true;
325     }
326 
327     ret = RAND_bytes(randStr, (int32_t)len);
328     SoftBusMutexUnlock(&g_randomLock);
329     if (ret != 1) {
330         HILOG_ERROR(SOFTBUS_HILOG_ID, "gen random error, ret[%d]", ret);
331         return SOFTBUS_ERR;
332     }
333     return SOFTBUS_OK;
334 }
335 
SoftBusGenerateSessionKey(char * key,uint32_t len)336 int32_t SoftBusGenerateSessionKey(char *key, uint32_t len)
337 {
338     if (SoftBusGenerateRandomArray((unsigned char *)key, len) != SOFTBUS_OK) {
339         HILOG_ERROR(SOFTBUS_HILOG_ID, "generate sessionKey error.");
340         return SOFTBUS_ENCRYPT_ERR;
341     }
342     return SOFTBUS_OK;
343 }
344 
SoftBusEncryptData(AesGcmCipherKey * cipherKey,const unsigned char * input,uint32_t inLen,unsigned char * encryptData,uint32_t * encryptLen)345 int32_t SoftBusEncryptData(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen,
346     unsigned char *encryptData, uint32_t *encryptLen)
347 {
348     if (cipherKey == NULL || input == NULL || inLen == 0 || encryptData == NULL || encryptLen == NULL) {
349         return SOFTBUS_INVALID_PARAM;
350     }
351 
352     if (SoftBusGenerateRandomArray(cipherKey->iv, sizeof(cipherKey->iv)) != SOFTBUS_OK) {
353         HILOG_ERROR(SOFTBUS_HILOG_ID, "generate random iv error.");
354         return SOFTBUS_ENCRYPT_ERR;
355     }
356     uint32_t outLen = inLen + OVERHEAD_LEN;
357     int32_t result = SslAesGcmEncrypt(cipherKey, input, inLen, encryptData, outLen);
358     if (result <= 0) {
359         return SOFTBUS_ENCRYPT_ERR;
360     }
361     *encryptLen = result;
362     return SOFTBUS_OK;
363 }
364 
SoftBusEncryptDataWithSeq(AesGcmCipherKey * cipherKey,const unsigned char * input,uint32_t inLen,unsigned char * encryptData,uint32_t * encryptLen,int32_t seqNum)365 int32_t SoftBusEncryptDataWithSeq(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen,
366     unsigned char *encryptData, uint32_t *encryptLen, int32_t seqNum)
367 {
368     if (cipherKey == NULL || input == NULL || inLen == 0 || encryptData == NULL || encryptLen == NULL) {
369         return SOFTBUS_INVALID_PARAM;
370     }
371     if (SoftBusGenerateRandomArray(cipherKey->iv, sizeof(cipherKey->iv)) != SOFTBUS_OK) {
372         HILOG_ERROR(SOFTBUS_HILOG_ID, "generate random iv error.");
373         return SOFTBUS_ENCRYPT_ERR;
374     }
375     if (memcpy_s(cipherKey->iv, sizeof(int32_t), &seqNum, sizeof(int32_t)) != EOK) {
376         return SOFTBUS_ENCRYPT_ERR;
377     }
378     uint32_t outLen = inLen + OVERHEAD_LEN;
379     int32_t result = SslAesGcmEncrypt(cipherKey, input, inLen, encryptData, outLen);
380     if (result <= 0) {
381         return SOFTBUS_ENCRYPT_ERR;
382     }
383     *encryptLen = result;
384     return SOFTBUS_OK;
385 }
386 
SoftBusDecryptData(AesGcmCipherKey * cipherKey,const unsigned char * input,uint32_t inLen,unsigned char * decryptData,uint32_t * decryptLen)387 int32_t SoftBusDecryptData(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen,
388     unsigned char *decryptData, uint32_t *decryptLen)
389 {
390     if (cipherKey == NULL || input == NULL || inLen < GCM_IV_LEN || decryptData == NULL || decryptLen == NULL) {
391         return SOFTBUS_INVALID_PARAM;
392     }
393 
394     if (memcpy_s(cipherKey->iv, sizeof(cipherKey->iv), input, GCM_IV_LEN) != EOK) {
395         HILOG_ERROR(SOFTBUS_HILOG_ID, "copy iv failed.");
396         return SOFTBUS_ENCRYPT_ERR;
397     }
398     uint32_t outLen = inLen - OVERHEAD_LEN;
399     int32_t result = SslAesGcmDecrypt(cipherKey, input, inLen, decryptData, outLen);
400     if (result <= 0) {
401         return SOFTBUS_ENCRYPT_ERR;
402     }
403     *decryptLen = (uint32_t)result;
404     return SOFTBUS_OK;
405 }
406 
SoftBusDecryptDataWithSeq(AesGcmCipherKey * cipherKey,const unsigned char * input,uint32_t inLen,unsigned char * decryptData,uint32_t * decryptLen,int32_t seqNum)407 int32_t SoftBusDecryptDataWithSeq(AesGcmCipherKey *cipherKey, const unsigned char *input, uint32_t inLen,
408     unsigned char *decryptData, uint32_t *decryptLen, int32_t seqNum)
409 {
410     (void)seqNum;
411     return SoftBusDecryptData(cipherKey, input, inLen, decryptData, decryptLen);
412 }
413 
SoftBusCryptoRand(void)414 uint32_t SoftBusCryptoRand(void)
415 {
416     int32_t fd = SoftBusOpenFile("/dev/urandom", SOFTBUS_O_RDONLY);
417     if (fd < 0) {
418         HILOG_ERROR(SOFTBUS_HILOG_ID, "CryptoRand open file fail");
419         return 0;
420     }
421     uint32_t value = 0;
422     int32_t len = SoftBusReadFile(fd, &value, sizeof(uint32_t));
423     if (len < 0) {
424         HILOG_ERROR(SOFTBUS_HILOG_ID, "CryptoRand read file fail");
425         return 0;
426     }
427     SoftBusCloseFile(fd);
428     return value;
429 }
430 
SoftBusEncryptDataByCtr(AesCtrCipherKey * key,const unsigned char * input,uint32_t inLen,unsigned char * encryptData,uint32_t * encryptLen)431 int32_t SoftBusEncryptDataByCtr(AesCtrCipherKey *key, const unsigned char *input, uint32_t inLen,
432     unsigned char *encryptData, uint32_t *encryptLen)
433 {
434     if (key == NULL || input == NULL || inLen == 0 || encryptData == NULL || encryptLen == NULL) {
435         return SOFTBUS_INVALID_PARAM;
436     }
437     EVP_CIPHER_CTX *ctx = NULL;
438     int32_t len = 0;
439     *encryptLen = 0;
440     EVP_CIPHER *cipher = NULL;
441     if (!(cipher = GetCtrAlgorithmByKeyLen(key->keyLen))) {
442         return HandleError(ctx, "get cipher failed");
443     }
444     if (!(ctx = EVP_CIPHER_CTX_new())) {
445         return HandleError(ctx, "EVP_CIPHER_CTX_new ctr failed");
446     }
447     if (EVP_EncryptInit_ex(ctx, cipher, NULL, key->key, key->iv) != 1) {
448         return HandleError(ctx, "EVP_EncryptInit_ex ctr failed");
449     }
450     if (EVP_EncryptUpdate(ctx, encryptData, &len, input, inLen) != 1) {
451         return HandleError(ctx, "EVP_EncryptUpdate ctr failed");
452     }
453     *encryptLen += len;
454     if (EVP_EncryptFinal_ex(ctx, encryptData + len, &len) != 1) {
455         return HandleError(ctx, "EVP_EncryptFinal_ex ctr failed");
456     }
457     *encryptLen += len;
458     EVP_CIPHER_CTX_free(ctx);
459     return SOFTBUS_OK;
460 }
461 
SoftBusDecryptDataByCtr(AesCtrCipherKey * key,const unsigned char * input,uint32_t inLen,unsigned char * decryptData,uint32_t * decryptLen)462 int32_t SoftBusDecryptDataByCtr(AesCtrCipherKey *key, const unsigned char *input, uint32_t inLen,
463     unsigned char *decryptData, uint32_t *decryptLen)
464 {
465     if (key == NULL || input == NULL || inLen == 0 || decryptData == NULL || decryptLen == NULL) {
466         return SOFTBUS_INVALID_PARAM;
467     }
468     EVP_CIPHER_CTX *ctx = NULL;
469     int32_t len = 0;
470     *decryptLen = 0;
471     EVP_CIPHER *cipher = NULL;
472     if (!(cipher = GetCtrAlgorithmByKeyLen(key->keyLen))) {
473         return HandleError(ctx, "get cipher failed");
474     }
475     if (!(ctx = EVP_CIPHER_CTX_new())) {
476         return HandleError(ctx, "EVP_CIPHER_CTX_new ctr failed");
477     }
478     if (EVP_DecryptInit_ex(ctx, cipher, NULL, key->key, key->iv) != 1) {
479         return HandleError(ctx, "EVP_DecryptInit_ex ctr failed");
480     }
481     if (EVP_DecryptUpdate(ctx, decryptData, &len, input, inLen) != 1) {
482         return HandleError(ctx, "EVP_DecryptUpdate ctr failed");
483     }
484     *decryptLen += len;
485     if (EVP_DecryptFinal_ex(ctx, decryptData + len, &len) != 1) {
486         return HandleError(ctx, "EVP_DecryptFinal_ex ctr failed");
487     }
488     *decryptLen += len;
489     EVP_CIPHER_CTX_free(ctx);
490     return SOFTBUS_OK;
491 }
492