• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 };
53 
GetMdClass(void)54 static const char *GetMdClass(void)
55 {
56     return "Md";
57 }
58 
FindAbility(const char * algoName)59 static HcfMdSpiCreateFunc FindAbility(const char *algoName)
60 {
61     for (uint32_t i = 0; i < (sizeof(MD_ABILITY_SET) / sizeof(MD_ABILITY_SET[0])); i++) {
62         if (strcmp(MD_ABILITY_SET[i].algoName, algoName) == 0) {
63             return MD_ABILITY_SET[i].createSpifunc;
64         }
65     }
66     LOGE("Algo not support! [Algo]: %s", algoName);
67     return NULL;
68 }
69 
Update(HcfMd * self,HcfBlob * input)70 static HcfResult Update(HcfMd *self, HcfBlob *input)
71 {
72     if ((self == NULL) || (!IsBlobValid(input))) {
73         LOGE("The input self ptr or dataBlob is NULL!");
74         return HCF_INVALID_PARAMS;
75     }
76     if (!IsClassMatch((HcfObjectBase *)self, GetMdClass())) {
77         LOGE("Class is not match.");
78         return HCF_INVALID_PARAMS;
79     }
80     return ((HcfMdImpl *)self)->spiObj->engineUpdateMd(
81         ((HcfMdImpl *)self)->spiObj, input);
82 }
83 
DoFinal(HcfMd * self,HcfBlob * output)84 static HcfResult DoFinal(HcfMd *self, HcfBlob *output)
85 {
86     if ((self == NULL) || (output == NULL)) {
87         LOGE("The input self ptr or dataBlob is NULL!");
88         return HCF_INVALID_PARAMS;
89     }
90     if (!IsClassMatch((HcfObjectBase *)self, GetMdClass())) {
91         LOGE("Class is not match.");
92         return HCF_INVALID_PARAMS;
93     }
94     return ((HcfMdImpl *)self)->spiObj->engineDoFinalMd(
95         ((HcfMdImpl *)self)->spiObj, output);
96 }
97 
GetMdLength(HcfMd * self)98 static uint32_t GetMdLength(HcfMd *self)
99 {
100     if (self == NULL) {
101         LOGE("The input self ptr is NULL!");
102         return 0;
103     }
104     if (!IsClassMatch((HcfObjectBase *)self, GetMdClass())) {
105         LOGE("Class is not match.");
106         return 0;
107     }
108     return ((HcfMdImpl *)self)->spiObj->engineGetMdLength(
109         ((HcfMdImpl *)self)->spiObj);
110 }
111 
GetAlgoName(HcfMd * self)112 static const char *GetAlgoName(HcfMd *self)
113 {
114     if (self == NULL) {
115         LOGE("The input self ptr is NULL!");
116         return NULL;
117     }
118     if (!IsClassMatch((HcfObjectBase *)self, GetMdClass())) {
119         LOGE("Class is not match.");
120         return NULL;
121     }
122     return ((HcfMdImpl *)self)->algoName;
123 }
124 
MdDestroy(HcfObjectBase * self)125 static void MdDestroy(HcfObjectBase *self)
126 {
127     if (self == NULL) {
128         LOGE("The input self ptr is NULL!");
129         return;
130     }
131     if (!IsClassMatch((HcfObjectBase *)self, GetMdClass())) {
132         LOGE("Class is not match.");
133         return;
134     }
135     HcfMdImpl *impl = (HcfMdImpl *)self;
136     HcfObjDestroy(impl->spiObj);
137     HcfFree(impl);
138 }
139 
HcfMdCreate(const char * algoName,HcfMd ** md)140 HcfResult HcfMdCreate(const char *algoName, HcfMd **md)
141 {
142     if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (md == NULL)) {
143         LOGE("Invalid input params while creating md!");
144         return HCF_INVALID_PARAMS;
145     }
146     HcfMdSpiCreateFunc createSpifunc = FindAbility(algoName);
147     if (createSpifunc == NULL) {
148         LOGE("Algo not supported!");
149         return HCF_NOT_SUPPORT;
150     }
151     HcfMdImpl *returnMdApi = (HcfMdImpl *)HcfMalloc(sizeof(HcfMdImpl), 0);
152     if (returnMdApi == NULL) {
153         LOGE("Failed to allocate Md Obj memory!");
154         return HCF_ERR_MALLOC;
155     }
156     if (strcpy_s(returnMdApi->algoName, HCF_MAX_ALGO_NAME_LEN, algoName) != EOK) {
157         LOGE("Failed to copy algoName!");
158         HcfFree(returnMdApi);
159         return HCF_ERR_COPY;
160     }
161     HcfMdSpi *spiObj = NULL;
162     HcfResult res = createSpifunc(algoName, &spiObj);
163     if (res != HCF_SUCCESS) {
164         LOGE("Failed to create spi object!");
165         HcfFree(returnMdApi);
166         return res;
167     }
168     returnMdApi->base.base.getClass = GetMdClass;
169     returnMdApi->base.base.destroy = MdDestroy;
170     returnMdApi->base.update = Update;
171     returnMdApi->base.doFinal = DoFinal;
172     returnMdApi->base.getMdLength = GetMdLength;
173     returnMdApi->base.getAlgoName = GetAlgoName;
174     returnMdApi->spiObj = spiObj;
175     *md = (HcfMd *)returnMdApi;
176     return HCF_SUCCESS;
177 }