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