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 tmpField = NULL;
87 return HCF_INVALID_PARAMS;
88 }
89 tmpField->fieldType = (char *)HcfMalloc(srcFieldTypeLen + 1, 0);
90 if (tmpField->fieldType == NULL) {
91 LOGE("Failed to allocate field memory.");
92 HcfFree(tmpField);
93 tmpField = NULL;
94 return HCF_ERR_MALLOC;
95 }
96 HcfECFieldFp *tmpDest = (HcfECFieldFp *)(tmpField);
97 HcfECFieldFp *tmpSrc = (HcfECFieldFp *)(src);
98 tmpDest->p.data = (unsigned char *)HcfMalloc(tmpSrc->p.len, 0);
99 if (tmpDest->p.data == NULL) {
100 LOGE("Failed to allocate b data memory");
101 HcfFree(tmpField->fieldType);
102 tmpField->fieldType = NULL;
103 HcfFree(tmpField);
104 tmpField = NULL;
105 return HCF_ERR_MALLOC;
106 }
107 (void)memcpy_s(tmpField->fieldType, srcFieldTypeLen, src->fieldType, srcFieldTypeLen);
108 (void)memcpy_s(tmpDest->p.data, tmpSrc->p.len, tmpSrc->p.data, tmpSrc->p.len);
109 tmpDest->p.len = tmpSrc->p.len;
110 *dest = tmpField;
111 return HCF_SUCCESS;
112 }
113
CopyEccCommonSpec(const HcfEccCommParamsSpec * srcSpec,HcfEccCommParamsSpec * destSpec)114 HcfResult CopyEccCommonSpec(const HcfEccCommParamsSpec *srcSpec, HcfEccCommParamsSpec *destSpec)
115 {
116 if (srcSpec == NULL || srcSpec->a.data == NULL || srcSpec->a.len == 0 || srcSpec->b.data == NULL ||
117 srcSpec->b.len == 0 || srcSpec->n.data == NULL || srcSpec->n.len == 0 || destSpec == NULL) {
118 LOGE("Invalid input parameter.");
119 return HCF_INVALID_PARAMS;
120 }
121 if (CopyAsyKeyParamsSpec(&(srcSpec->base), &(destSpec->base)) != HCF_SUCCESS) {
122 LOGE("Failed to copy src common spec");
123 return HCF_INVALID_PARAMS;
124 }
125 destSpec->a.data = (unsigned char *)HcfMalloc(srcSpec->a.len, 0);
126 if (destSpec->a.data == NULL) {
127 LOGE("Failed to allocate a data memory");
128 FreeEccCommParamsSpec(destSpec);
129 return HCF_ERR_MALLOC;
130 }
131 destSpec->b.data = (unsigned char *)HcfMalloc(srcSpec->b.len, 0);
132 if (destSpec->b.data == NULL) {
133 LOGE("Failed to allocate b data memory");
134 FreeEccCommParamsSpec(destSpec);
135 return HCF_ERR_MALLOC;
136 }
137 destSpec->n.data = (unsigned char *)HcfMalloc(srcSpec->n.len, 0);
138 if (destSpec->n.data == NULL) {
139 LOGE("Failed to allocate n data memory");
140 FreeEccCommParamsSpec(destSpec);
141 return HCF_ERR_MALLOC;
142 }
143 HcfResult res = CopyEcField(srcSpec->field, &(destSpec->field));
144 if (res != HCF_SUCCESS) {
145 LOGE("Failed to allocate field data memory");
146 FreeEccCommParamsSpec(destSpec);
147 return HCF_ERR_MALLOC;
148 }
149 res = CopyPoint(&(srcSpec->g), &(destSpec->g));
150 if (res != HCF_SUCCESS) {
151 LOGE("Failed to allocate field data memory");
152 FreeEccCommParamsSpec(destSpec);
153 return HCF_ERR_MALLOC;
154 }
155 destSpec->h = srcSpec->h;
156 (void)memcpy_s(destSpec->a.data, srcSpec->a.len, srcSpec->a.data, srcSpec->a.len);
157 (void)memcpy_s(destSpec->b.data, srcSpec->b.len, srcSpec->b.data, srcSpec->b.len);
158 (void)memcpy_s(destSpec->n.data, srcSpec->n.len, srcSpec->n.data, srcSpec->n.len);
159 destSpec->a.len = srcSpec->a.len;
160 destSpec->b.len = srcSpec->b.len;
161 destSpec->n.len = srcSpec->n.len;
162 return HCF_SUCCESS;
163 }
164
CreateEccCommonSpecImpl(const HcfEccCommParamsSpec * srcSpec,HcfEccCommParamsSpec ** destSpec)165 HcfResult CreateEccCommonSpecImpl(const HcfEccCommParamsSpec *srcSpec, HcfEccCommParamsSpec **destSpec)
166 {
167 if (srcSpec == NULL || destSpec == NULL) {
168 LOGE("Invalid input parameter.");
169 return HCF_INVALID_PARAMS;
170 }
171 HcfEccCommParamsSpec *tmpSpec = (HcfEccCommParamsSpec *)HcfMalloc(sizeof(HcfEccCommParamsSpec), 0);
172 if (tmpSpec == NULL) {
173 LOGE("Failed to allocate dest spec memory");
174 return HCF_ERR_MALLOC;
175 }
176 if (CopyEccCommonSpec(srcSpec, tmpSpec) != HCF_SUCCESS) {
177 LOGE("CreateEccCommonSpecImpl error!");
178 HcfFree(tmpSpec);
179 tmpSpec = NULL;
180 return HCF_INVALID_PARAMS;
181 }
182 *destSpec = tmpSpec;
183 return HCF_SUCCESS;
184 }
185
CopyDhCommonSpec(const HcfDhCommParamsSpec * srcSpec,HcfDhCommParamsSpec * destSpec)186 HcfResult CopyDhCommonSpec(const HcfDhCommParamsSpec *srcSpec, HcfDhCommParamsSpec *destSpec)
187 {
188 if (CopyAsyKeyParamsSpec(&(srcSpec->base), &(destSpec->base)) != HCF_SUCCESS) {
189 LOGE("Failed to copy src common spec");
190 return HCF_INVALID_PARAMS;
191 }
192 destSpec->p.data = (unsigned char *)HcfMalloc(srcSpec->p.len, 0);
193 if (destSpec->p.data == NULL) {
194 LOGE("Failed to allocate p data memory");
195 FreeDhCommParamsSpec(destSpec);
196 return HCF_ERR_MALLOC;
197 }
198
199 destSpec->g.data = (unsigned char *)HcfMalloc(srcSpec->g.len, 0);
200 if (destSpec->g.data == NULL) {
201 LOGE("Failed to allocate g data memory");
202 FreeDhCommParamsSpec(destSpec);
203 return HCF_ERR_MALLOC;
204 }
205 destSpec->length = srcSpec->length;
206 (void)memcpy_s(destSpec->p.data, srcSpec->p.len, srcSpec->p.data, srcSpec->p.len);
207 (void)memcpy_s(destSpec->g.data, srcSpec->g.len, srcSpec->g.data, srcSpec->g.len);
208 destSpec->p.len = srcSpec->p.len;
209 destSpec->g.len = srcSpec->g.len;
210 return HCF_SUCCESS;
211 }
212
CreateDhCommonSpecImpl(const HcfDhCommParamsSpec * srcSpec,HcfDhCommParamsSpec ** destSpec)213 HcfResult CreateDhCommonSpecImpl(const HcfDhCommParamsSpec *srcSpec, HcfDhCommParamsSpec **destSpec)
214 {
215 HcfDhCommParamsSpec *spec = (HcfDhCommParamsSpec *)HcfMalloc(sizeof(HcfDhCommParamsSpec), 0);
216 if (spec == NULL) {
217 LOGE("Failed to allocate dest spec memory");
218 return HCF_ERR_MALLOC;
219 }
220
221 if (CopyDhCommonSpec(srcSpec, spec) != HCF_SUCCESS) {
222 LOGE("Failed to copy src spec");
223 HcfFree(spec);
224 spec = NULL;
225 return HCF_INVALID_PARAMS;
226 }
227
228 *destSpec = spec;
229 return HCF_SUCCESS;
230 }
231