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