1 /* 2 * Copyright (c) 2021-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 #ifndef ACCESS_TOKEN_RANDOM_MBEDTLS 17 #define ACCESS_TOKEN_RANDOM_MBEDTLS 18 19 #include "rwlock.h" 20 #include "mbedtls/ctr_drbg.h" 21 #include "mbedtls/entropy.h" 22 23 namespace OHOS { 24 namespace Security { 25 namespace AccessToken { 26 class RandomMbedtls { 27 public: 28 static RandomMbedtls& GetInstance(); 29 int GenerateRandomArray(unsigned char *randStr, unsigned int len); ~RandomMbedtls()30 ~RandomMbedtls() {} 31 static unsigned int GetRandomUint32(); 32 33 private: RandomMbedtls()34 RandomMbedtls() : initFlag_(false) {} 35 mbedtls_entropy_context entropy_; 36 mbedtls_ctr_drbg_context ctrDrbg_; 37 OHOS::Utils::RWLock randomLock_; 38 bool initFlag_; 39 }; 40 } // namespace AccessToken 41 } // namespace Security 42 } // namespace OHOS 43 #endif // ACCESS_TOKEN_RANDOM_MBEDTLS 44