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