• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ecc_key_util.h"
17 #include <securec.h>
18 #include "ecc_key_util_spi.h"
19 #include "config.h"
20 #include "ecc_common_param_spec_generator_openssl.h"
21 #include "key_utils.h"
22 #include "params_parser.h"
23 #include "log.h"
24 #include "memory.h"
25 #include "utils.h"
26 
27 typedef HcfResult (*HcfEccCommParamsSpecCreateFunc)(HcfAsyKeyGenParams *, HcfEccCommParamsSpecSpi **);
28 
29 typedef struct {
30     HcfAlgValue algo;
31 
32     HcfEccCommParamsSpecCreateFunc createSpiFunc;
33 } HcfEccCommParamsSpecAbility;
34 
35 static const HcfEccCommParamsSpecAbility ASY_KEY_GEN_ABILITY_SET[] = {
36     { HCF_ALG_ECC, HcfECCCommonParamSpecCreate },
37 };
38 
FindAbility(HcfAsyKeyGenParams * params)39 static HcfEccCommParamsSpecCreateFunc FindAbility(HcfAsyKeyGenParams *params)
40 {
41     if (params == NULL) {
42         LOGE("params is null");
43         return NULL;
44     }
45     for (uint32_t i = 0; i < sizeof(ASY_KEY_GEN_ABILITY_SET) / sizeof(ASY_KEY_GEN_ABILITY_SET[0]); i++) {
46         if (ASY_KEY_GEN_ABILITY_SET[i].algo == params->algo) {
47             return ASY_KEY_GEN_ABILITY_SET[i].createSpiFunc;
48         }
49     }
50     LOGE("Algo not support! [Algo]: %d", params->algo);
51     return NULL;
52 }
53 
54 
HcfEccKeyUtilCreate(const char * algName,HcfEccCommParamsSpec ** returnCommonParamSpec)55 HcfResult HcfEccKeyUtilCreate(const char *algName, HcfEccCommParamsSpec **returnCommonParamSpec)
56 {
57     if ((!IsStrValid(algName, HCF_MAX_ALGO_NAME_LEN)) || (returnCommonParamSpec == NULL)) {
58         LOGE("Failed to parse params!");
59         return HCF_INVALID_PARAMS;
60     }
61     HcfAsyKeyGenParams params = { 0 };
62     if (ParseCurveNameToParams(algName, &params) != HCF_SUCCESS) {
63         LOGE("Failed to parse params!");
64         return HCF_INVALID_PARAMS;
65     }
66 
67     HcfEccCommParamsSpecCreateFunc createSpiFunc = FindAbility(&params);
68     if (createSpiFunc == NULL) {
69         LOGE("Failed to find ability!");
70         return HCF_NOT_SUPPORT;
71     }
72 
73     HcfEccCommParamsSpecSpi *spiInstance = NULL;
74     HcfResult ret = createSpiFunc(&params, &spiInstance);
75     if (ret != HCF_SUCCESS) {
76         LOGE("Failed to create spi object!");
77         return ret;
78     }
79     if (CreateEccCommonSpecImpl(&(spiInstance->paramsSpec), returnCommonParamSpec) != HCF_SUCCESS) {
80         LOGE("Failed to create spi object!");
81         FreeEccCommParamsSpec(&(spiInstance->paramsSpec));
82         HcfFree(spiInstance);
83         return ret;
84     }
85     FreeEccCommParamsSpec(&(spiInstance->paramsSpec));
86     HcfFree(spiInstance);
87     return HCF_SUCCESS;
88 }