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 "mac.h"
17
18 #include <securec.h>
19
20 #include "mac_spi.h"
21 #include "mac_openssl.h"
22
23 #include "log.h"
24 #include "config.h"
25 #include "memory.h"
26 #include "utils.h"
27
28 typedef HcfResult (*HcfMacSpiCreateFunc)(const char *, HcfMacSpi **);
29
30 typedef struct {
31 HcfMac base;
32
33 HcfMacSpi *spiObj;
34
35 char algoName[HCF_MAX_ALGO_NAME_LEN];
36 } HcfMacImpl;
37
38 typedef struct {
39 char *algoName;
40
41 HcfMacSpiCreateFunc createSpifunc;
42 } HcfMacAbility;
43
44 static const HcfMacAbility MAC_ABILITY_SET[] = {
45 { "SHA1", OpensslMacSpiCreate },
46 { "SHA224", OpensslMacSpiCreate },
47 { "SHA256", OpensslMacSpiCreate },
48 { "SHA384", OpensslMacSpiCreate },
49 { "SHA512", OpensslMacSpiCreate },
50 };
51
GetMacClass(void)52 static const char *GetMacClass(void)
53 {
54 return "HMAC";
55 }
56
FindAbility(const char * algoName)57 static HcfMacSpiCreateFunc FindAbility(const char *algoName)
58 {
59 for (uint32_t i = 0; i < (sizeof(MAC_ABILITY_SET) / sizeof(MAC_ABILITY_SET[0])); i++) {
60 if (strcmp(MAC_ABILITY_SET[i].algoName, algoName) == 0) {
61 return MAC_ABILITY_SET[i].createSpifunc;
62 }
63 }
64 LOGE("Algo not support! [Algo]: %s", algoName);
65 return NULL;
66 }
67
Init(HcfMac * self,const HcfSymKey * key)68 static HcfResult Init(HcfMac *self, const HcfSymKey *key)
69 {
70 if ((self == NULL) || (key == NULL)) {
71 LOGE("The input self ptr or key is NULL!");
72 return HCF_INVALID_PARAMS;
73 }
74 if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
75 LOGE("Class is not match.");
76 return HCF_INVALID_PARAMS;
77 }
78 return ((HcfMacImpl *)self)->spiObj->engineInitMac(
79 ((HcfMacImpl *)self)->spiObj, key);
80 }
81
Update(HcfMac * self,HcfBlob * input)82 static HcfResult Update(HcfMac *self, HcfBlob *input)
83 {
84 if ((self == NULL) || (!IsBlobValid(input))) {
85 LOGE("The input self ptr or dataBlob is NULL!");
86 return HCF_INVALID_PARAMS;
87 }
88 if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
89 LOGE("Class is not match.");
90 return HCF_INVALID_PARAMS;
91 }
92 return ((HcfMacImpl *)self)->spiObj->engineUpdateMac(
93 ((HcfMacImpl *)self)->spiObj, input);
94 }
95
DoFinal(HcfMac * self,HcfBlob * output)96 static HcfResult DoFinal(HcfMac *self, HcfBlob *output)
97 {
98 if ((self == NULL) || (output == NULL)) {
99 LOGE("The input self ptr or dataBlob is NULL!");
100 return HCF_INVALID_PARAMS;
101 }
102 if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
103 LOGE("Class is not match.");
104 return HCF_INVALID_PARAMS;
105 }
106 return ((HcfMacImpl *)self)->spiObj->engineDoFinalMac(
107 ((HcfMacImpl *)self)->spiObj, output);
108 }
109
GetMacLength(HcfMac * self)110 static uint32_t GetMacLength(HcfMac *self)
111 {
112 if (self == NULL) {
113 LOGE("The input self ptr is NULL!");
114 return 0;
115 }
116 if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
117 LOGE("Class is not match.");
118 return 0;
119 }
120 return ((HcfMacImpl *)self)->spiObj->engineGetMacLength(
121 ((HcfMacImpl *)self)->spiObj);
122 }
123
GetAlgoName(HcfMac * self)124 static const char *GetAlgoName(HcfMac *self)
125 {
126 if (self == NULL) {
127 LOGE("The input self ptr is NULL!");
128 return NULL;
129 }
130 if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
131 LOGE("Class is not match.");
132 return NULL;
133 }
134 return ((HcfMacImpl *)self)->algoName;
135 }
136
MacDestroy(HcfObjectBase * self)137 static void MacDestroy(HcfObjectBase *self)
138 {
139 if (self == NULL) {
140 LOGE("The input self ptr is NULL!");
141 return;
142 }
143 if (!IsClassMatch((HcfObjectBase *)self, GetMacClass())) {
144 LOGE("Class is not match.");
145 return;
146 }
147 HcfMacImpl *impl = (HcfMacImpl *)self;
148 HcfObjDestroy(impl->spiObj);
149 HcfFree(impl);
150 }
151
HcfMacCreate(const char * algoName,HcfMac ** mac)152 HcfResult HcfMacCreate(const char *algoName, HcfMac **mac)
153 {
154 if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (mac == NULL)) {
155 LOGE("Invalid input params while creating mac!");
156 return HCF_INVALID_PARAMS;
157 }
158 HcfMacSpiCreateFunc createSpifunc = FindAbility(algoName);
159 if (createSpifunc == NULL) {
160 LOGE("Algo not supported!");
161 return HCF_NOT_SUPPORT;
162 }
163 HcfMacImpl *returnMacApi = (HcfMacImpl *)HcfMalloc(sizeof(HcfMacImpl), 0);
164 if (returnMacApi == NULL) {
165 LOGE("Failed to allocate Mac Obj memory!");
166 return HCF_ERR_MALLOC;
167 }
168 if (strcpy_s(returnMacApi->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) {
169 LOGE("Failed to copy algoName!");
170 HcfFree(returnMacApi);
171 return HCF_ERR_COPY;
172 }
173 HcfMacSpi *spiObj = NULL;
174 HcfResult res = createSpifunc(algoName, &spiObj);
175 if (res != HCF_SUCCESS) {
176 LOGE("Failed to create spi object!");
177 HcfFree(returnMacApi);
178 return res;
179 }
180 returnMacApi->base.base.getClass = GetMacClass;
181 returnMacApi->base.base.destroy = MacDestroy;
182 returnMacApi->base.init = Init;
183 returnMacApi->base.update = Update;
184 returnMacApi->base.doFinal = DoFinal;
185 returnMacApi->base.getMacLength = GetMacLength;
186 returnMacApi->base.getAlgoName = GetAlgoName;
187 returnMacApi->spiObj = spiObj;
188 *mac = (HcfMac *)returnMacApi;
189 return HCF_SUCCESS;
190 }