1 /*
2  * Copyright (C) 2022-2023 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 "rand_openssl.h"
17 
18 #include "openssl_adapter.h"
19 #include "openssl_common.h"
20 #include "securec.h"
21 #include "log.h"
22 #include "memory.h"
23 #include "utils.h"
24 
25 typedef struct {
26     HcfRandSpi base;
27 } HcfRandSpiImpl;
28 
GetRandOpenSSLClass(void)29 static const char *GetRandOpenSSLClass(void)
30 {
31     return "RandOpenssl";
32 }
33 
OpensslGenerateRandom(HcfRandSpi * self,int32_t numBytes,HcfBlob * random)34 static HcfResult OpensslGenerateRandom(HcfRandSpi *self, int32_t numBytes, HcfBlob *random)
35 {
36     if ((self == NULL) || (random == NULL)) {
37         LOGE("Invalid params!");
38         return HCF_INVALID_PARAMS;
39     }
40     if (numBytes <= 0) {
41         LOGE("Invalid numBytes!");
42         return HCF_INVALID_PARAMS;
43     }
44     random->data = (uint8_t *)HcfMalloc(numBytes, 0);
45     if (random->data == NULL) {
46         LOGE("Failed to allocate random->data memory!");
47         return HCF_ERR_MALLOC;
48     }
49     int32_t ret = OpensslRandPrivBytes(random->data, numBytes);
50     if (ret != HCF_OPENSSL_SUCCESS) {
51         LOGD("[error] RAND_bytes return error!");
52         HcfFree(random->data);
53         random->data = NULL;
54         HcfPrintOpensslError();
55         return HCF_ERR_CRYPTO_OPERATION;
56     }
57 
58     random->len = numBytes;
59     return HCF_SUCCESS;
60 }
61 
GetRandAlgoName(HcfRandSpi * self)62 static const char *GetRandAlgoName(HcfRandSpi *self)
63 {
64     if (self == NULL) {
65         LOGE("Invalid input parameter.");
66         return NULL;
67     }
68     if (!HcfIsClassMatch((HcfObjectBase *)self, GetRandOpenSSLClass())) {
69         LOGE("Class is not match.");
70         return NULL;
71     }
72 
73     return OPENSSL_RAND_ALGORITHM;
74 }
75 
OpensslSetSeed(HcfRandSpi * self,HcfBlob * seed)76 static void OpensslSetSeed(HcfRandSpi *self, HcfBlob *seed)
77 {
78     (void)self;
79     if (seed == NULL) {
80         LOGE("The seed is NULL!");
81         return;
82     }
83     OpensslRandSeed(seed->data, seed->len);
84 }
85 
DestroyRandOpenssl(HcfObjectBase * self)86 static void DestroyRandOpenssl(HcfObjectBase *self)
87 {
88     if (self == NULL) {
89         LOGE("Self ptr is NULL!");
90         return;
91     }
92     if (!HcfIsClassMatch(self, GetRandOpenSSLClass())) {
93         LOGE("Class is not match.");
94         return;
95     }
96     HcfFree(self);
97 }
98 
HcfRandSpiCreate(HcfRandSpi ** spiObj)99 HcfResult HcfRandSpiCreate(HcfRandSpi **spiObj)
100 {
101     if (spiObj == NULL) {
102         LOGE("Invalid input parameter.");
103         return HCF_INVALID_PARAMS;
104     }
105     HcfRandSpiImpl *returnSpiImpl = (HcfRandSpiImpl *)HcfMalloc(sizeof(HcfRandSpiImpl), 0);
106     if (returnSpiImpl == NULL) {
107         LOGE("Failed to allocate returnImpl memory!");
108         return HCF_ERR_MALLOC;
109     }
110     returnSpiImpl->base.base.getClass = GetRandOpenSSLClass;
111     returnSpiImpl->base.base.destroy = DestroyRandOpenssl;
112     returnSpiImpl->base.engineGenerateRandom = OpensslGenerateRandom;
113     returnSpiImpl->base.engineSetSeed = OpensslSetSeed;
114     returnSpiImpl->base.engineGetAlgoName = GetRandAlgoName;
115     *spiObj = (HcfRandSpi *)returnSpiImpl;
116     return HCF_SUCCESS;
117 }