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