1 /*
2 * Copyright (C) 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 "key_utils.h"
17 #include <securec.h>
18 #include "config.h"
19 #include "params_parser.h"
20 #include "log.h"
21 #include "memory.h"
22 #include "utils.h"
23
CopyAsyKeyParamsSpec(const HcfAsyKeyParamsSpec * srcSpec,HcfAsyKeyParamsSpec * destSpec)24 HcfResult CopyAsyKeyParamsSpec(const HcfAsyKeyParamsSpec *srcSpec, HcfAsyKeyParamsSpec *destSpec)
25 {
26 if (srcSpec == NULL || srcSpec->algName == NULL || destSpec == NULL) {
27 LOGE("Invalid input parameter.");
28 return HCF_INVALID_PARAMS;
29 }
30 size_t srcAlgNameLen = HcfStrlen(srcSpec->algName);
31 if (!srcAlgNameLen) {
32 LOGE("algName is empty!");
33 return HCF_INVALID_PARAMS;
34 }
35 destSpec->algName = (char *)HcfMalloc(srcAlgNameLen + 1, 0);
36 if (destSpec->algName == NULL) {
37 LOGE("Failed to allocate alg name memory");
38 return HCF_ERR_MALLOC;
39 }
40 (void)memcpy_s(destSpec->algName, srcAlgNameLen, srcSpec->algName, srcAlgNameLen);
41 destSpec->specType = srcSpec->specType;
42 return HCF_SUCCESS;
43 }
44
CopyPoint(const HcfPoint * src,HcfPoint * dest)45 HcfResult CopyPoint(const HcfPoint *src, HcfPoint *dest)
46 {
47 if (src == NULL || src->x.data == NULL || src->x.len == 0 ||
48 src->y.data == NULL || src->y.len == 0 || dest == NULL) {
49 LOGE("Invalid input parameter.");
50 return HCF_INVALID_PARAMS;
51 }
52 dest->x.data = (unsigned char *)HcfMalloc(src->x.len, 0);
53 if (dest->x.data == NULL) {
54 LOGE("Failed to allocate x data memory");
55 return HCF_ERR_MALLOC;
56 }
57 dest->y.data = (unsigned char *)HcfMalloc(src->y.len, 0);
58 if (dest->y.data == NULL) {
59 LOGE("Failed to allocate y data memory");
60 HcfFree(dest->x.data);
61 dest->x.data = NULL;
62 return HCF_ERR_MALLOC;
63 }
64 (void)memcpy_s(dest->x.data, src->x.len, src->x.data, src->x.len);
65 (void)memcpy_s(dest->y.data, src->y.len, src->y.data, src->y.len);
66 dest->x.len = src->x.len;
67 dest->y.len = src->y.len;
68 return HCF_SUCCESS;
69 }
70
CopyEcField(const HcfECField * src,HcfECField ** dest)71 static HcfResult CopyEcField(const HcfECField *src, HcfECField **dest)
72 {
73 if (src == NULL || src->fieldType == NULL || dest == NULL) {
74 LOGE("Invalid input parameter.");
75 return HCF_INVALID_PARAMS;
76 }
77 HcfECField *tmpField = (HcfECField *)HcfMalloc(sizeof(HcfECFieldFp), 0);
78 if (tmpField == NULL) {
79 LOGE("Alloc memory failed.");
80 return HCF_ERR_MALLOC;
81 }
82 size_t srcFieldTypeLen = HcfStrlen(src->fieldType);
83 if (!srcFieldTypeLen) {
84 LOGE("fieldType is empty!");
85 HcfFree(tmpField);
86 return HCF_INVALID_PARAMS;
87 }
88 tmpField->fieldType = (char *)HcfMalloc(srcFieldTypeLen + 1, 0);
89 if (tmpField->fieldType == NULL) {
90 LOGE("Failed to allocate field memory.");
91 HcfFree(tmpField);
92 return HCF_ERR_MALLOC;
93 }
94 HcfECFieldFp *tmpDest = (HcfECFieldFp *)(tmpField);
95 HcfECFieldFp *tmpSrc = (HcfECFieldFp *)(src);
96 tmpDest->p.data = (unsigned char *)HcfMalloc(tmpSrc->p.len, 0);
97 if (tmpDest->p.data == NULL) {
98 LOGE("Failed to allocate b data memory");
99 HcfFree(tmpField->fieldType);
100 HcfFree(tmpField);
101 return HCF_ERR_MALLOC;
102 }
103 (void)memcpy_s(tmpField->fieldType, srcFieldTypeLen, src->fieldType, srcFieldTypeLen);
104 (void)memcpy_s(tmpDest->p.data, tmpSrc->p.len, tmpSrc->p.data, tmpSrc->p.len);
105 tmpDest->p.len = tmpSrc->p.len;
106 *dest = tmpField;
107 return HCF_SUCCESS;
108 }
109
CopyEccCommonSpec(const HcfEccCommParamsSpec * srcSpec,HcfEccCommParamsSpec * destSpec)110 HcfResult CopyEccCommonSpec(const HcfEccCommParamsSpec *srcSpec, HcfEccCommParamsSpec *destSpec)
111 {
112 if (srcSpec == NULL || srcSpec->a.data == NULL || srcSpec->a.len == 0 || srcSpec->b.data == NULL ||
113 srcSpec->b.len == 0 || srcSpec->n.data == NULL || srcSpec->n.len == 0 || destSpec == NULL) {
114 LOGE("Invalid input parameter.");
115 return HCF_INVALID_PARAMS;
116 }
117 if (CopyAsyKeyParamsSpec(&(srcSpec->base), &(destSpec->base)) != HCF_SUCCESS) {
118 LOGE("Failed to copy src common spec");
119 return HCF_INVALID_PARAMS;
120 }
121 destSpec->a.data = (unsigned char *)HcfMalloc(srcSpec->a.len, 0);
122 if (destSpec->a.data == NULL) {
123 LOGE("Failed to allocate a data memory");
124 FreeEccCommParamsSpec(destSpec);
125 return HCF_ERR_MALLOC;
126 }
127 destSpec->b.data = (unsigned char *)HcfMalloc(srcSpec->b.len, 0);
128 if (destSpec->b.data == NULL) {
129 LOGE("Failed to allocate b data memory");
130 FreeEccCommParamsSpec(destSpec);
131 return HCF_ERR_MALLOC;
132 }
133 destSpec->n.data = (unsigned char *)HcfMalloc(srcSpec->n.len, 0);
134 if (destSpec->n.data == NULL) {
135 LOGE("Failed to allocate n data memory");
136 FreeEccCommParamsSpec(destSpec);
137 return HCF_ERR_MALLOC;
138 }
139 HcfResult res = CopyEcField(srcSpec->field, &(destSpec->field));
140 if (res != HCF_SUCCESS) {
141 LOGE("Failed to allocate field data memory");
142 FreeEccCommParamsSpec(destSpec);
143 return HCF_ERR_MALLOC;
144 }
145 res = CopyPoint(&(srcSpec->g), &(destSpec->g));
146 if (res != HCF_SUCCESS) {
147 LOGE("Failed to allocate field data memory");
148 FreeEccCommParamsSpec(destSpec);
149 return HCF_ERR_MALLOC;
150 }
151 destSpec->h = srcSpec->h;
152 (void)memcpy_s(destSpec->a.data, srcSpec->a.len, srcSpec->a.data, srcSpec->a.len);
153 (void)memcpy_s(destSpec->b.data, srcSpec->b.len, srcSpec->b.data, srcSpec->b.len);
154 (void)memcpy_s(destSpec->n.data, srcSpec->n.len, srcSpec->n.data, srcSpec->n.len);
155 destSpec->a.len = srcSpec->a.len;
156 destSpec->b.len = srcSpec->b.len;
157 destSpec->n.len = srcSpec->n.len;
158 return HCF_SUCCESS;
159 }
160
CreateEccCommonSpecImpl(const HcfEccCommParamsSpec * srcSpec,HcfEccCommParamsSpec ** destSpec)161 HcfResult CreateEccCommonSpecImpl(const HcfEccCommParamsSpec *srcSpec, HcfEccCommParamsSpec **destSpec)
162 {
163 if (srcSpec == NULL || destSpec == NULL) {
164 LOGE("Invalid input parameter.");
165 return HCF_INVALID_PARAMS;
166 }
167 HcfEccCommParamsSpec *tmpSpec = (HcfEccCommParamsSpec *)HcfMalloc(sizeof(HcfEccCommParamsSpec), 0);
168 if (tmpSpec == NULL) {
169 LOGE("Failed to allocate dest spec memory");
170 return HCF_ERR_MALLOC;
171 }
172 if (CopyEccCommonSpec(srcSpec, tmpSpec) != HCF_SUCCESS) {
173 LOGE("CreateEccCommonSpecImpl error!");
174 HcfFree(tmpSpec);
175 return HCF_INVALID_PARAMS;
176 }
177 *destSpec = tmpSpec;
178 return HCF_SUCCESS;
179 }
180
CopyDhCommonSpec(const HcfDhCommParamsSpec * srcSpec,HcfDhCommParamsSpec * destSpec)181 HcfResult CopyDhCommonSpec(const HcfDhCommParamsSpec *srcSpec, HcfDhCommParamsSpec *destSpec)
182 {
183 if (CopyAsyKeyParamsSpec(&(srcSpec->base), &(destSpec->base)) != HCF_SUCCESS) {
184 LOGE("Failed to copy src common spec");
185 return HCF_INVALID_PARAMS;
186 }
187 destSpec->p.data = (unsigned char *)HcfMalloc(srcSpec->p.len, 0);
188 if (destSpec->p.data == NULL) {
189 LOGE("Failed to allocate p data memory");
190 FreeDhCommParamsSpec(destSpec);
191 return HCF_ERR_MALLOC;
192 }
193
194 destSpec->g.data = (unsigned char *)HcfMalloc(srcSpec->g.len, 0);
195 if (destSpec->g.data == NULL) {
196 LOGE("Failed to allocate g data memory");
197 FreeDhCommParamsSpec(destSpec);
198 return HCF_ERR_MALLOC;
199 }
200 destSpec->length = srcSpec->length;
201 (void)memcpy_s(destSpec->p.data, srcSpec->p.len, srcSpec->p.data, srcSpec->p.len);
202 (void)memcpy_s(destSpec->g.data, srcSpec->g.len, srcSpec->g.data, srcSpec->g.len);
203 destSpec->p.len = srcSpec->p.len;
204 destSpec->g.len = srcSpec->g.len;
205 return HCF_SUCCESS;
206 }
207
CreateDhCommonSpecImpl(const HcfDhCommParamsSpec * srcSpec,HcfDhCommParamsSpec ** destSpec)208 HcfResult CreateDhCommonSpecImpl(const HcfDhCommParamsSpec *srcSpec, HcfDhCommParamsSpec **destSpec)
209 {
210 HcfDhCommParamsSpec *spec = (HcfDhCommParamsSpec *)HcfMalloc(sizeof(HcfDhCommParamsSpec), 0);
211 if (spec == NULL) {
212 LOGE("Failed to allocate dest spec memory");
213 return HCF_ERR_MALLOC;
214 }
215
216 if (CopyDhCommonSpec(srcSpec, spec) != HCF_SUCCESS) {
217 LOGE("Failed to copy src spec");
218 HcfFree(spec);
219 return HCF_INVALID_PARAMS;
220 }
221
222 *destSpec = spec;
223 return HCF_SUCCESS;
224 }
225