• 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 "dm_random.h"
17 
18 #include <random>
19 
20 #include "dm_constants.h"
21 #include "mbedtls/ctr_drbg.h"
22 #include "mbedtls/entropy.h"
23 
24 #if defined(__LITEOS_M__)
25 #include <time.h>
26 #endif
27 
28 namespace OHOS {
29 namespace DistributedHardware {
GenRandInt(int32_t randMin,int32_t randMax)30 int32_t GenRandInt(int32_t randMin, int32_t randMax)
31 {
32 #if defined(__LITEOS_M__)
33     srandom(time(NULL));
34     return (randMin + random() % (randMax - randMin));
35 #else
36     std::random_device randDevice;
37     std::mt19937 genRand(randDevice());
38     std::uniform_int_distribution<int> disRand(randMin, randMax);
39     return disRand(genRand);
40 #endif
41 }
42 
GenRandLongLong(int64_t randMin,int64_t randMax)43 int64_t GenRandLongLong(int64_t randMin, int64_t randMax)
44 {
45     std::random_device randDevice;
46     std::mt19937 genRand(randDevice());
47     std::uniform_int_distribution<long long> disRand(randMin, randMax);
48     return disRand(genRand);
49 }
50 
GetRandomData(uint8_t * randStr,uint32_t len)51 int32_t GetRandomData(uint8_t *randStr, uint32_t len)
52 {
53     mbedtls_entropy_context *entropy = nullptr;
54     mbedtls_ctr_drbg_context *ctrDrbg = nullptr;
55     int32_t ret = ERR_DM_FAILED;
56     do {
57         if (randStr == nullptr || len == 0) {
58             break;
59         }
60         entropy = reinterpret_cast<mbedtls_entropy_context *>(malloc(sizeof(mbedtls_entropy_context)));
61         if (entropy == nullptr) {
62             break;
63         }
64         ctrDrbg = reinterpret_cast<mbedtls_ctr_drbg_context *>(malloc(sizeof(mbedtls_ctr_drbg_context)));
65         if (ctrDrbg == nullptr) {
66             break;
67         }
68         mbedtls_ctr_drbg_init(ctrDrbg);
69         mbedtls_entropy_init(entropy);
70         ret = mbedtls_ctr_drbg_seed(ctrDrbg, mbedtls_entropy_func, entropy, nullptr, 0);
71         if (ret != 0) {
72             break;
73         }
74         ret = mbedtls_ctr_drbg_random(ctrDrbg, randStr, len);
75         if (ret != 0) {
76             break;
77         }
78         ret = DM_OK;
79     } while (false);
80     if (entropy != nullptr) {
81         free(entropy);
82         entropy = nullptr;
83     }
84     if (ctrDrbg != nullptr) {
85         free(ctrDrbg);
86         ctrDrbg = nullptr;
87     }
88     return ret;
89 }
90 } // namespace DistributedHardware
91 } // namespace OHOS
92