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