• 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 "huks_three_stage_test_common.h"
17 
18 static struct OH_Huks_Param g_genParamsTest001[] = {
19     {.tag = OH_HUKS_TAG_ALGORITHM,
20      .uint32Param = OH_HUKS_ALG_DSA},
21     {.tag = OH_HUKS_TAG_PURPOSE,
22      .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN | OH_HUKS_KEY_PURPOSE_VERIFY},
23     {.tag = OH_HUKS_TAG_KEY_SIZE,
24      .uint32Param = 1024},
25     {.tag = OH_HUKS_TAG_DIGEST,
26      .uint32Param = OH_HUKS_DIGEST_SHA1}
27 };
28 
InitParamSet(struct OH_Huks_ParamSet ** paramSet,const struct OH_Huks_Param * params,uint32_t paramcount)29 OH_Huks_Result InitParamSet(
30     struct OH_Huks_ParamSet **paramSet,
31     const struct OH_Huks_Param *params,
32     uint32_t paramcount)
33 {
34     OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
35     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
36     {
37         return ret;
38     }
39 
40     ret = OH_Huks_AddParams(*paramSet, params, paramcount);
41     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
42     {
43         OH_Huks_FreeParamSet(paramSet);
44         return ret;
45     }
46 
47     ret = OH_Huks_BuildParamSet(paramSet);
48     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
49     {
50         OH_Huks_FreeParamSet(paramSet);
51         return ret;
52     }
53 
54     return ret;
55 }
56 
TestLessThanMaxSeg(const struct OH_Huks_Blob * handle,const struct OH_Huks_ParamSet * paramSet,uint32_t purpose,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData)57 static OH_Huks_Result TestLessThanMaxSeg(
58     const struct OH_Huks_Blob *handle,
59     const struct OH_Huks_ParamSet *paramSet,
60     uint32_t purpose,
61     const struct OH_Huks_Blob *inData,
62     struct OH_Huks_Blob *outData)
63 {
64     struct OH_Huks_Result ret;
65     ret.errorCode = OH_HUKS_SUCCESS;
66     struct OH_Huks_Blob tmpOutData = {
67         .size = MAX_OUTDATA_SIZE,
68         .data = NULL};
69     if (MallocAndCheckBlobData(&tmpOutData, tmpOutData.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
70     {
71         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
72         return ret;
73     }
74     ret = OH_Huks_UpdateSession(handle, paramSet, inData, &tmpOutData);
75     HksFree(tmpOutData.data);
76     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
77     {
78         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
79         return ret;
80     }
81     struct OH_Huks_Blob tmpInData = {
82         .size = 0,
83         .data = NULL};
84     if (MallocAndCheckBlobData(&tmpInData, MAX_UPDATE_SIZE).errorCode != (int32_t)OH_HUKS_SUCCESS)
85     {
86         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
87         return ret;
88     }
89 
90     if (purpose == OH_HUKS_KEY_PURPOSE_VERIFY)
91     {
92         ret = OH_Huks_FinishSession(handle, paramSet, outData, &tmpInData);
93     }
94     else
95     {
96         ret = OH_Huks_FinishSession(handle, paramSet, &tmpInData, outData);
97     }
98     HksFree(tmpInData.data);
99     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
100     {
101         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
102         return ret;
103     }
104     return ret;
105 }
106 
HksTestUpdate(const struct OH_Huks_Blob * handle,const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData)107 OH_Huks_Result HksTestUpdate(
108     const struct OH_Huks_Blob *handle,
109     const struct OH_Huks_ParamSet *paramSet,
110     const struct OH_Huks_Blob *inData)
111 {
112     struct OH_Huks_Blob inDataSeg = *inData;
113     inDataSeg.size = MAX_UPDATE_SIZE;
114 
115     struct OH_Huks_Result ret;
116     ret.errorCode = OH_HUKS_SUCCESS;
117 
118     uint8_t *lastPtr = inData->data + inData->size - 1;
119     struct OH_Huks_Blob outDataSeg = {
120         .size = MAX_OUTDATA_SIZE,
121         .data = NULL};
122 
123     bool isFinished = false;
124 
125     while (inDataSeg.data <= lastPtr)
126     {
127         if (inDataSeg.data + MAX_UPDATE_SIZE <= lastPtr)
128         {
129             outDataSeg.size = MAX_OUTDATA_SIZE;
130         }
131         else
132         {
133             isFinished = true;
134             inDataSeg.size = lastPtr - inDataSeg.data + 1;
135             outDataSeg.size = inDataSeg.size + MAX_UPDATE_SIZE;
136         }
137         if (MallocAndCheckBlobData(&outDataSeg, outDataSeg.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
138         {
139             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
140             return ret;
141         }
142         if (OH_Huks_UpdateSession(handle, paramSet, &inDataSeg, &outDataSeg).errorCode != (int32_t)OH_HUKS_SUCCESS)
143         {
144             HksFree(outDataSeg.data);
145             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
146             return ret;
147         }
148         HksFree(outDataSeg.data);
149         if ((isFinished == false) && (inDataSeg.data + MAX_UPDATE_SIZE > lastPtr))
150         {
151             return ret;
152         }
153         inDataSeg.data += MAX_UPDATE_SIZE;
154     }
155     return ret;
156 }
157 
TestUpdateLoopFinish(const struct OH_Huks_Blob * handle,const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData)158 OH_Huks_Result TestUpdateLoopFinish(const struct OH_Huks_Blob *handle,
159                                     const struct OH_Huks_ParamSet *paramSet,
160                                     const struct OH_Huks_Blob *inData,
161                                     struct OH_Huks_Blob *outData)
162 {
163     struct OH_Huks_Result ret;
164     ret.errorCode = OH_HUKS_SUCCESS;
165 
166     struct OH_Huks_Blob inDataSeg = *inData;
167     uint8_t *lastPtr = inData->data + inData->size - 1;
168     struct OH_Huks_Blob outDataSeg = {MAX_OUTDATA_SIZE, NULL};
169     uint8_t *cur = outData->data;
170     outData->size = 0;
171 
172     inDataSeg.size = MAX_UPDATE_SIZE;
173 
174     bool isFinished = false;
175 
176     while (inDataSeg.data <= lastPtr)
177     {
178         if (inDataSeg.data + MAX_UPDATE_SIZE <= lastPtr)
179         {
180             outDataSeg.size = MAX_OUTDATA_SIZE;
181         }
182         else
183         {
184             isFinished = true;
185             inDataSeg.size = lastPtr - inDataSeg.data + 1;
186             break;
187         }
188         if (MallocAndCheckBlobData(&outDataSeg, outDataSeg.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
189         {
190             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
191             return ret;
192         }
193         if (OH_Huks_UpdateSession(handle, paramSet, &inDataSeg, &outDataSeg).errorCode != (int32_t)OH_HUKS_SUCCESS)
194         {
195             HksFree(outDataSeg.data);
196             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
197             return ret;
198         }
199         (void)memcpy_s(cur, outDataSeg.size, outDataSeg.data, outDataSeg.size);
200         cur += outDataSeg.size;
201         outData->size += outDataSeg.size;
202         HksFree(outDataSeg.data);
203         if ((isFinished == false) && (inDataSeg.data + MAX_UPDATE_SIZE > lastPtr))
204         {
205             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
206             return ret;
207         }
208         inDataSeg.data += MAX_UPDATE_SIZE;
209     }
210 
211     struct OH_Huks_Blob outDataFinish = {inDataSeg.size * TIMES, NULL};
212     if (MallocAndCheckBlobData(&outDataFinish, outDataFinish.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
213     {
214         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
215         return ret;
216     }
217     if (OH_Huks_FinishSession(handle, paramSet, &inDataSeg, &outDataFinish).errorCode != (int32_t)OH_HUKS_SUCCESS)
218     {
219         HksFree(outDataFinish.data);
220         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
221         return ret;
222     }
223     (void)memcpy_s(cur, outDataFinish.size, outDataFinish.data, outDataFinish.size);
224     outData->size += outDataFinish.size;
225     HksFree(outDataFinish.data);
226 
227     return ret;
228 }
229 
TestUpdateFinish(const struct OH_Huks_Blob * handle,const struct OH_Huks_ParamSet * paramSet,uint32_t purpose,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData)230 OH_Huks_Result TestUpdateFinish(
231     const struct OH_Huks_Blob *handle,
232     const struct OH_Huks_ParamSet *paramSet,
233     uint32_t purpose,
234     const struct OH_Huks_Blob *inData,
235     struct OH_Huks_Blob *outData)
236 {
237     struct OH_Huks_Result ret;
238     ret.errorCode = OH_HUKS_SUCCESS;
239 
240     struct OH_Huks_Blob inDataSeg = *inData;
241     inDataSeg.size = MAX_UPDATE_SIZE;
242 
243     uint8_t *lastPtr = inData->data + inData->size - 1;
244     struct OH_Huks_Blob outDataSeg = {MAX_OUTDATA_SIZE, NULL};
245 
246     bool isFinished = false;
247 
248     if (inData->size <= MAX_UPDATE_SIZE)
249     {
250         return TestLessThanMaxSeg(handle, paramSet, purpose, inData, outData);
251     }
252     while (inDataSeg.data <= lastPtr)
253     {
254         if (inDataSeg.data + MAX_UPDATE_SIZE <= lastPtr)
255         {
256             outDataSeg.size = MAX_OUTDATA_SIZE;
257             if (MallocAndCheckBlobData(&outDataSeg, outDataSeg.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
258             {
259                 ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
260                 return ret;
261             }
262         }
263         else
264         {
265             isFinished = true;
266             inDataSeg.size = lastPtr - inDataSeg.data + 1;
267             break;
268         }
269         if (OH_Huks_UpdateSession(handle, paramSet, &inDataSeg, &outDataSeg).errorCode != (int32_t)OH_HUKS_SUCCESS)
270         {
271             HksFree(outDataSeg.data);
272             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
273             return ret;
274         }
275         HksFree(outDataSeg.data);
276         if ((isFinished == false) && (inDataSeg.data + MAX_UPDATE_SIZE > lastPtr))
277         {
278             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
279             return ret;
280         }
281         inDataSeg.data += MAX_UPDATE_SIZE;
282     }
283 
284     if (purpose != OH_HUKS_KEY_PURPOSE_VERIFY)
285     {
286         if (OH_Huks_FinishSession(handle, paramSet, &inDataSeg, outData).errorCode != (int32_t)OH_HUKS_SUCCESS)
287         {
288             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
289             return ret;
290         }
291     }
292     else
293     {
294         uint8_t tmp[] = "temp";
295         struct OH_Huks_Blob tempBlob = {sizeof(tmp), tmp};
296         if (OH_Huks_UpdateSession(handle, paramSet, &inDataSeg, &tempBlob).errorCode != (int32_t)OH_HUKS_SUCCESS)
297         {
298             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
299             return ret;
300         }
301         if (OH_Huks_FinishSession(handle, paramSet, outData, &tempBlob).errorCode != (int32_t)OH_HUKS_SUCCESS)
302         {
303             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
304             return ret;
305         }
306     }
307     return ret;
308 }
309 
MallocAndCheckBlobData(struct OH_Huks_Blob * blob,const uint32_t blobSize)310 OH_Huks_Result MallocAndCheckBlobData(
311     struct OH_Huks_Blob *blob,
312     const uint32_t blobSize)
313 {
314     struct OH_Huks_Result ret;
315     ret.errorCode = OH_HUKS_SUCCESS;
316 
317     blob->data = (uint8_t *)HksMalloc(blobSize);
318     if (blob->data == NULL)
319     {
320         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
321     }
322 
323     return ret;
324 }
325 
TestCmpKeyAliasHash(const struct OH_Huks_Blob * srcData1,const struct OH_Huks_Blob * srcData2)326 OH_Huks_Result TestCmpKeyAliasHash(
327     const struct OH_Huks_Blob *srcData1,
328     const struct OH_Huks_Blob *srcData2)
329 {
330     struct OH_Huks_Result ret;
331     ret.errorCode = OH_HUKS_SUCCESS;
332 
333     struct OH_Huks_Param hashParam = {
334         .tag = OH_HUKS_TAG_DIGEST,
335         .uint32Param = OH_HUKS_DIGEST_SHA256};
336     struct OH_Huks_ParamSet *hashParamSet = NULL;
337     ret = InitParamSet(&hashParamSet, &hashParam, sizeof(hashParam) / sizeof(struct OH_Huks_Param));
338     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
339     {
340         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
341         return ret;
342     }
343 
344     struct OH_Huks_Blob hash1 = {MAX_OUTDATA_SIZE, NULL};
345     if (MallocAndCheckBlobData(&hash1, hash1.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
346     {
347         OH_Huks_FreeParamSet(&hashParamSet);
348         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
349         return ret;
350     }
351     ret = HuksHash(hashParamSet, srcData1, &hash1);
352     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
353     {
354         OH_Huks_FreeParamSet(&hashParamSet);
355         HksFree(hash1.data);
356         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
357         return ret;
358     }
359 
360     struct OH_Huks_Blob hash2 = {MAX_OUTDATA_SIZE, NULL};
361     if (MallocAndCheckBlobData(&hash2, hash2.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
362     {
363         HksFree(hash1.data);
364         OH_Huks_FreeParamSet(&hashParamSet);
365         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
366         return ret;
367     }
368     ret = HuksHash(hashParamSet, srcData2, &hash2);
369     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
370     {
371         OH_Huks_FreeParamSet(&hashParamSet);
372         HksFree(hash1.data);
373         HksFree(hash2.data);
374         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
375         return ret;
376     }
377 
378     int32_t ret1 = HksMemCmp(hash1.data, hash2.data, hash2.size);
379     ret.errorCode = ret1;
380     OH_Huks_FreeParamSet(&hashParamSet);
381     HksFree(hash1.data);
382     HksFree(hash2.data);
383 
384     return ret;
385 }
386 
HksEncryptLoopUpdate(const struct OH_Huks_Blob * handle,const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData)387 static OH_Huks_Result HksEncryptLoopUpdate(const struct OH_Huks_Blob *handle, const struct OH_Huks_ParamSet *paramSet,
388                                            const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
389 {
390     struct OH_Huks_Result ret;
391     ret.errorCode = OH_HUKS_SUCCESS;
392 
393     struct OH_Huks_Blob inDataSeg = *inData;
394     uint8_t *lastPtr = inData->data + inData->size - 1;
395     struct OH_Huks_Blob outDataSeg = {MAX_OUTDATA_SIZE, NULL};
396     uint8_t *cur = outData->data;
397     outData->size = 0;
398 
399     inDataSeg.size = MAX_UPDATE_SIZE;
400     bool isFinished = false;
401 
402     while (inDataSeg.data <= lastPtr)
403     {
404         if (inDataSeg.data + MAX_UPDATE_SIZE <= lastPtr)
405         {
406             outDataSeg.size = MAX_OUTDATA_SIZE;
407         }
408         else
409         {
410             isFinished = true;
411             inDataSeg.size = lastPtr - inDataSeg.data + 1;
412             break;
413         }
414         if (MallocAndCheckBlobData(&outDataSeg, outDataSeg.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
415         {
416             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
417             return ret;
418         }
419         ret = OH_Huks_UpdateSession(handle, paramSet, &inDataSeg, &outDataSeg);
420         if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
421         {
422             free(outDataSeg.data);
423             return ret;
424         }
425         (void)memcpy_s(cur, outDataSeg.size, outDataSeg.data, outDataSeg.size);
426         cur += outDataSeg.size;
427         outData->size += outDataSeg.size;
428         free(outDataSeg.data);
429         if ((isFinished == false) && (inDataSeg.data + MAX_UPDATE_SIZE > lastPtr))
430         {
431             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
432             return ret;
433         }
434         inDataSeg.data += MAX_UPDATE_SIZE;
435     }
436 
437     struct OH_Huks_Blob outDataFinish = {inDataSeg.size * TIMES, NULL};
438     if (MallocAndCheckBlobData(&outDataFinish, outDataFinish.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
439     {
440         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
441         return ret;
442     }
443 
444     ret = OH_Huks_FinishSession(handle, paramSet, &inDataSeg, &outDataFinish);
445     if (ret.errorCode != OH_HUKS_SUCCESS)
446     {
447         free(outDataFinish.data);
448         return ret;
449     }
450     (void)memcpy_s(cur, outDataFinish.size, outDataFinish.data, outDataFinish.size);
451     outData->size += outDataFinish.size;
452     free(outDataFinish.data);
453 
454     return ret;
455 }
456 
HuksEncrypt(const struct OH_Huks_Blob * key,const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * plainText,struct OH_Huks_Blob * cipherText)457 OH_Huks_Result HuksEncrypt(const struct OH_Huks_Blob *key, const struct OH_Huks_ParamSet *paramSet,
458                            const struct OH_Huks_Blob *plainText, struct OH_Huks_Blob *cipherText)
459 {
460 
461     uint8_t handle[sizeof(uint64_t)] = {0};
462     struct OH_Huks_Blob handleBlob = {sizeof(uint64_t), handle};
463     OH_Huks_Result ret = OH_Huks_InitSession(key, paramSet, &handleBlob, nullptr);
464     if (ret.errorCode != OH_HUKS_SUCCESS)
465     {
466         return ret;
467     }
468 
469     ret = HksEncryptLoopUpdate(&handleBlob, paramSet, plainText, cipherText);
470     return ret;
471 }
472 
HksDecryptLoopUpdate(const struct OH_Huks_Blob * handle,const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData)473 static OH_Huks_Result HksDecryptLoopUpdate(const struct OH_Huks_Blob *handle, const struct OH_Huks_ParamSet *paramSet,
474                                            const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
475 {
476     struct OH_Huks_Result ret;
477     ret.errorCode = OH_HUKS_SUCCESS;
478 
479     struct OH_Huks_Blob inDataSeg = *inData;
480     uint8_t *lastPtr = inData->data + inData->size - 1;
481     struct OH_Huks_Blob outDataSeg = {MAX_OUTDATA_SIZE, NULL};
482     uint8_t *cur = outData->data;
483     outData->size = 0;
484 
485     inDataSeg.size = MAX_UPDATE_SIZE;
486 
487     bool isFinished = false;
488 
489     while (inDataSeg.data <= lastPtr)
490     {
491         if (inDataSeg.data + MAX_UPDATE_SIZE <= lastPtr)
492         {
493             outDataSeg.size = MAX_OUTDATA_SIZE;
494         }
495         else
496         {
497             isFinished = true;
498             inDataSeg.size = lastPtr - inDataSeg.data + 1;
499             break;
500         }
501         if (MallocAndCheckBlobData(&outDataSeg, outDataSeg.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
502         {
503             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
504             return ret;
505         }
506         struct OH_Huks_Result result = OH_Huks_UpdateSession(handle, paramSet, &inDataSeg, &outDataSeg);
507         if (result.errorCode != OH_HUKS_SUCCESS)
508         {
509             free(outDataSeg.data);
510             return result;
511         }
512         (void)memcpy_s(cur, outDataSeg.size, outDataSeg.data, outDataSeg.size);
513         cur += outDataSeg.size;
514         outData->size += outDataSeg.size;
515         free(outDataSeg.data);
516         if ((isFinished == false) && (inDataSeg.data + MAX_UPDATE_SIZE > lastPtr))
517         {
518             ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
519             return ret;
520         }
521         inDataSeg.data += MAX_UPDATE_SIZE;
522     }
523 
524     struct OH_Huks_Blob outDataFinish = {inDataSeg.size * TIMES, NULL};
525     if (MallocAndCheckBlobData(&outDataFinish, outDataFinish.size).errorCode != (int32_t)OH_HUKS_SUCCESS)
526     {
527         ret.errorCode = OH_HUKS_ERR_CODE_INTERNAL_ERROR;
528         return ret;
529     }
530 
531     struct OH_Huks_Result result2 = OH_Huks_FinishSession(handle, paramSet, &inDataSeg, &outDataFinish);
532     if (result2.errorCode != OH_HUKS_SUCCESS)
533     {
534         free(outDataFinish.data);
535         return result2;
536     }
537     (void)memcpy_s(cur, outDataFinish.size, outDataFinish.data, outDataFinish.size);
538     outData->size += outDataFinish.size;
539     free(outDataFinish.data);
540 
541     return ret;
542 }
543 
HuksDecrypt(const struct OH_Huks_Blob * key,const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * cipherText,struct OH_Huks_Blob * plainText)544 OH_Huks_Result HuksDecrypt(const struct OH_Huks_Blob *key, const struct OH_Huks_ParamSet *paramSet,
545                            const struct OH_Huks_Blob *cipherText, struct OH_Huks_Blob *plainText)
546 {
547 
548     uint8_t handle[sizeof(uint64_t)] = {0};
549     struct OH_Huks_Blob handleBlob = {sizeof(uint64_t), handle};
550     struct OH_Huks_Result result = OH_Huks_InitSession(key, paramSet, &handleBlob, nullptr);
551     if (result.errorCode != OH_HUKS_SUCCESS)
552     {
553         return result;
554     }
555 
556     OH_Huks_Result ret = HksDecryptLoopUpdate(&handleBlob, paramSet, cipherText, plainText);
557     return ret;
558 }
559 
HksGetDigestLen(uint32_t digest,uint32_t * digestLen)560 static int32_t HksGetDigestLen(uint32_t digest, uint32_t *digestLen)
561 {
562     switch (digest)
563     {
564     case OH_HUKS_DIGEST_MD5:
565         *digestLen = HKS_DIGEST_MD5_LEN;
566         break;
567     case OH_HUKS_DIGEST_SHA1:
568         *digestLen = HKS_DIGEST_SHA1_LEN;
569         break;
570     case OH_HUKS_DIGEST_SHA224:
571         *digestLen = HKS_DIGEST_SHA224_LEN;
572         break;
573     case OH_HUKS_DIGEST_SHA256:
574         *digestLen = HKS_DIGEST_SHA256_LEN;
575         break;
576     case OH_HUKS_DIGEST_SHA384:
577         *digestLen = HKS_DIGEST_SHA384_LEN;
578         break;
579     case OH_HUKS_DIGEST_SHA512:
580         *digestLen = HKS_DIGEST_SHA512_LEN;
581         break;
582     case OH_HUKS_DIGEST_SM3:
583         *digestLen = HKS_DIGEST_SM3_LEN;
584         break;
585     default:
586         return OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
587     }
588     return OH_HUKS_SUCCESS;
589 }
590 
GetOpensslAlg(uint32_t alg)591 static const EVP_MD *GetOpensslAlg(uint32_t alg)
592 {
593     switch (alg)
594     {
595     case OH_HUKS_DIGEST_MD5:
596         return EVP_md5();
597     case OH_HUKS_DIGEST_SHA1:
598         return EVP_sha1();
599     case OH_HUKS_DIGEST_SHA224:
600         return EVP_sha224();
601     case OH_HUKS_DIGEST_SHA256:
602         return EVP_sha256();
603     case OH_HUKS_DIGEST_SHA384:
604         return EVP_sha384();
605     case OH_HUKS_DIGEST_SHA512:
606         return EVP_sha512();
607     default:
608         return NULL;
609     }
610 }
611 
HksOpensslHash(uint32_t alg,const struct OH_Huks_Blob * msg,struct OH_Huks_Blob * hash)612 static int32_t HksOpensslHash(uint32_t alg, const struct OH_Huks_Blob *msg, struct OH_Huks_Blob *hash)
613 {
614     const EVP_MD *opensslAlg = GetOpensslAlg(alg);
615     if (opensslAlg == NULL)
616     {
617         return OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
618     }
619 
620     int32_t ret = EVP_Digest(msg->data, msg->size, hash->data, &hash->size, opensslAlg, NULL);
621     if (ret != 1)
622     {
623         return OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
624     }
625     return (int32_t)OH_HUKS_SUCCESS;
626 }
627 
HuksHash(const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * srcData,struct OH_Huks_Blob * hash)628 OH_Huks_Result HuksHash(const struct OH_Huks_ParamSet *paramSet,
629                         const struct OH_Huks_Blob *srcData, struct OH_Huks_Blob *hash)
630 {
631     OH_Huks_Result ret;
632 
633     if ((paramSet == NULL) || (srcData == NULL) || (hash == NULL))
634     {
635         ret.errorCode = OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
636         return ret;
637     }
638 
639     struct OH_Huks_Param *digestAlg = NULL;
640     ret = OH_Huks_GetParam(paramSet, OH_HUKS_TAG_DIGEST, &digestAlg);
641     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
642     {
643         return ret;
644     }
645 
646     uint32_t digestLen;
647     int32_t ret1 = HksGetDigestLen(digestAlg->uint32Param, &digestLen);
648     ret.errorCode = ret1;
649     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
650     {
651         return ret;
652     }
653 
654     if (hash->size < digestLen)
655     {
656         ret.errorCode = OH_HUKS_ERR_CODE_ILLEGAL_ARGUMENT;
657         return ret;
658     }
659     ret1 = HksOpensslHash(digestAlg->uint32Param, srcData, hash);
660     ret.errorCode = ret1;
661     return ret;
662 }
663 
HuksAgreeKey(const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_Blob * peerPublicKey,struct OH_Huks_Blob * agreedKey)664 OH_Huks_Result HuksAgreeKey(const struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *keyAlias,
665                             const struct OH_Huks_Blob *peerPublicKey, struct OH_Huks_Blob *agreedKey)
666 {
667     uint8_t temp[10] = {0};
668     struct OH_Huks_Blob inData = {
669         sizeof(temp),
670         temp};
671 
672     uint8_t handleU[sizeof(uint64_t)] = {0};
673     struct OH_Huks_Blob handle = {sizeof(uint64_t), handleU};
674     OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, paramSet, &handle, nullptr);
675     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
676     {
677         return ret;
678     }
679 
680     uint8_t outDataU[1024] = {0};
681     struct OH_Huks_Blob outDataUpdate = {1024, outDataU};
682     ret = OH_Huks_UpdateSession(&handle, paramSet, peerPublicKey, &outDataUpdate);
683     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
684     {
685         return ret;
686     }
687 
688     ret = OH_Huks_FinishSession(&handle, paramSet, &inData, agreedKey);
689     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS)
690     {
691         return ret;
692     }
693     return ret;
694 }
695 
checkUseSoftware()696 bool checkUseSoftware()
697 {
698     const char *keyAliasString = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
699     struct OH_Huks_ParamSet *genParamSet = nullptr;
700     struct OH_Huks_Blob keyAlias = {strlen(keyAliasString), (uint8_t *)keyAliasString};
701     InitParamSet(&genParamSet, g_genParamsTest001, sizeof(g_genParamsTest001) / sizeof(OH_Huks_Param));
702     OH_Huks_Result ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
703     if(ret.errorCode == 0){
704         OH_Huks_DeleteKeyItem(&keyAlias, genParamSet);
705         OH_Huks_FreeParamSet(&genParamSet);
706         return true;
707     }
708     return false;
709 }