1 /* 2 * Copyright (c) 2024 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 #include "random_impl.h" 16 17 namespace OHOS { 18 namespace CryptoFramework { RandomImpl(HcfRand * randObj)19 RandomImpl::RandomImpl(HcfRand *randObj) 20 { 21 randObj_ = randObj; 22 } 23 ~RandomImpl()24 RandomImpl::~RandomImpl() 25 { 26 HcfObjDestroy(this->randObj_); 27 } 28 GetAlgName(int32_t * errCode)29 const char* RandomImpl::GetAlgName(int32_t* errCode) 30 { 31 if (randObj_ == nullptr) { 32 LOGE("fail to get rand obj!"); 33 *errCode = HCF_ERR_MALLOC; 34 return nullptr; 35 } 36 const char *algoName = randObj_->getAlgoName(randObj_); 37 *errCode = HCF_SUCCESS; 38 return algoName; 39 } 40 GenerateRandom(int32_t numBytes,int32_t * errCode)41 HcfBlob RandomImpl::GenerateRandom(int32_t numBytes, int32_t* errCode) 42 { 43 HcfBlob randBlob = { .data = nullptr, .len = 0}; 44 if (randObj_ == nullptr) { 45 *errCode = HCF_ERR_MALLOC; 46 LOGE("fail to get rand obj!"); 47 return randBlob; 48 } 49 HcfResult res = randObj_->generateRandom(randObj_, numBytes, &randBlob); 50 if (res != HCF_SUCCESS) { 51 LOGE("generateRandom failed!"); 52 } 53 *errCode = static_cast<int32_t>(res); 54 return randBlob; 55 } 56 SetSeed(HcfBlob * seed,int32_t * errCode)57 void RandomImpl::SetSeed(HcfBlob *seed, int32_t* errCode) 58 { 59 if (randObj_ == nullptr) { 60 *errCode = HCF_ERR_MALLOC; 61 LOGE("fail to get rand obj!"); 62 return; 63 } 64 HcfResult res = randObj_->setSeed(randObj_, seed); 65 if (res != HCF_SUCCESS) { 66 LOGE("set seed failed."); 67 } 68 *errCode = static_cast<int32_t>(res); 69 } 70 } 71 }