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 = Openssl_RAND_priv_bytes(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 (!IsClassMatch((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 Openssl_RAND_seed(seed->data, seed->len);
79 }
80
DestroyRandOpenssl(HcfObjectBase * self)81 static void DestroyRandOpenssl(HcfObjectBase *self)
82 {
83 if (self == NULL) {
84 LOGE("Self ptr is NULL!");
85 return;
86 }
87 if (!IsClassMatch(self, GetRandOpenSSLClass())) {
88 LOGE("Class is not match.");
89 return;
90 }
91 HcfFree(self);
92 }
93
HcfRandSpiCreate(HcfRandSpi ** spiObj)94 HcfResult HcfRandSpiCreate(HcfRandSpi **spiObj)
95 {
96 if (spiObj == NULL) {
97 LOGE("Invalid input parameter.");
98 return HCF_INVALID_PARAMS;
99 }
100 HcfRandSpiImpl *returnSpiImpl = (HcfRandSpiImpl *)HcfMalloc(sizeof(HcfRandSpiImpl), 0);
101 if (returnSpiImpl == NULL) {
102 LOGE("Failed to allocate returnImpl memory!");
103 return HCF_ERR_MALLOC;
104 }
105 returnSpiImpl->base.base.getClass = GetRandOpenSSLClass;
106 returnSpiImpl->base.base.destroy = DestroyRandOpenssl;
107 returnSpiImpl->base.engineGenerateRandom = OpensslGenerateRandom;
108 returnSpiImpl->base.engineSetSeed = OpensslSetSeed;
109 returnSpiImpl->base.engineGetAlgoName = GetRandAlgoName;
110 *spiObj = (HcfRandSpi *)returnSpiImpl;
111 return HCF_SUCCESS;
112 }