1 /*
2  * Copyright (C) 2023-2024 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 
IsBigIntegerValid(const HcfBigInteger * bigInt)54 static bool IsBigIntegerValid(const HcfBigInteger *bigInt)
55 {
56     if (bigInt == NULL) {
57         LOGE("Invalid HcfBigInteger parameter");
58         return false;
59     }
60     if (bigInt->data == NULL) {
61         LOGE("BigInteger data is NULL");
62         return false;
63     }
64     if (bigInt->len == 0) {
65         LOGE("BigInteger length is 0");
66         return false;
67     }
68     return true;
69 }
70 
IsPointValid(const HcfPoint * point)71 static bool IsPointValid(const HcfPoint *point)
72 {
73     if (point == NULL) {
74         LOGE("Invalid point parameter");
75         return false;
76     }
77     if (!IsBigIntegerValid(&(point->x))) {
78         LOGE("Invalid x coordinate parameter");
79         return false;
80     }
81     if (!IsBigIntegerValid(&(point->y))) {
82         LOGE("Invalid y coordinate parameter");
83         return false;
84     }
85     return true;
86 }
87 
HcfConvertPoint(const char * curveName,HcfBlob * encodedPoint,HcfPoint * returnPoint)88 HcfResult HcfConvertPoint(const char *curveName, HcfBlob *encodedPoint, HcfPoint *returnPoint)
89 {
90     if (!HcfIsStrValid(curveName, HCF_MAX_ALGO_NAME_LEN)) {
91         LOGE("Failed to parse params: curveName is invalid!");
92         return HCF_INVALID_PARAMS;
93     }
94 
95     if (!HcfIsBlobValid(encodedPoint)) {
96         LOGE("Failed to parse params: encodedPoint is invalid!");
97         return HCF_INVALID_PARAMS;
98     }
99 
100     if (returnPoint == NULL) {
101         LOGE("Failed to parse params: returnPoint is NULL!");
102         return HCF_INVALID_PARAMS;
103     }
104 
105     HcfAlgParaValue algValue = 0;
106     HcfResult ret = GetAlgValueByCurveName(curveName, &algValue);
107     if (ret != HCF_SUCCESS) {
108         LOGE("Failed to get algValue.");
109         return ret;
110     }
111 
112     ret = HcfEngineConvertPoint(algValue, encodedPoint, returnPoint);
113     if (ret != HCF_SUCCESS) {
114         LOGE("Failed to create spi object!");
115         return ret;
116     }
117     return HCF_SUCCESS;
118 }
119 
HcfGetEncodedPoint(const char * curveName,HcfPoint * point,const char * format,HcfBlob * returnBlob)120 HcfResult HcfGetEncodedPoint(const char *curveName, HcfPoint *point, const char *format, HcfBlob *returnBlob)
121 {
122     if (!HcfIsStrValid(curveName, HCF_MAX_ALGO_NAME_LEN)) {
123         LOGE("Failed to parse params: curveName is invalid!");
124         return HCF_INVALID_PARAMS;
125     }
126 
127     if (!IsPointValid(point)) {
128         LOGE("Failed to parse params: point is invalid!");
129         return HCF_INVALID_PARAMS;
130     }
131 
132     if (format == NULL) {
133         LOGE("Failed to parse params: format is NULL!");
134         return HCF_INVALID_PARAMS;
135     }
136 
137     HcfFormatValue formatValue = 0;
138     HcfResult ret = GetFormatValueByFormatName(format, &formatValue);
139     if (ret != HCF_SUCCESS) {
140         LOGE("Failed to get formatValue.");
141         return ret;
142     }
143 
144     if (returnBlob == NULL) {
145         LOGE("Failed to parse params: returnBlob is NULL!");
146         return HCF_INVALID_PARAMS;
147     }
148 
149     HcfAlgParaValue algValue = 0;
150     ret = GetAlgValueByCurveName(curveName, &algValue);
151     if (ret != HCF_SUCCESS) {
152         LOGE("Failed to get algValue.");
153         return ret;
154     }
155 
156     ret = HcfEngineGetEncodedPoint(algValue, point, formatValue, returnBlob);
157     if (ret != HCF_SUCCESS) {
158         LOGE("Failed to create spi object!");
159         return ret;
160     }
161     return HCF_SUCCESS;
162 }
163 
HcfEccKeyUtilCreate(const char * algName,HcfEccCommParamsSpec ** returnCommonParamSpec)164 HcfResult HcfEccKeyUtilCreate(const char *algName, HcfEccCommParamsSpec **returnCommonParamSpec)
165 {
166     if ((!HcfIsStrValid(algName, HCF_MAX_ALGO_NAME_LEN)) || (returnCommonParamSpec == NULL)) {
167         LOGE("Failed to parse params!");
168         return HCF_INVALID_PARAMS;
169     }
170     HcfAsyKeyGenParams params = { 0 };
171     if (ParseCurveNameToParams(algName, &params) != HCF_SUCCESS) {
172         LOGE("Failed to parse params!");
173         return HCF_INVALID_PARAMS;
174     }
175 
176     HcfEccCommParamsSpecCreateFunc createSpiFunc = FindAbility(&params);
177     if (createSpiFunc == NULL) {
178         LOGE("Failed to find ability!");
179         return HCF_NOT_SUPPORT;
180     }
181 
182     HcfEccCommParamsSpecSpi *spiInstance = NULL;
183     HcfResult ret = createSpiFunc(&params, &spiInstance);
184     if (ret != HCF_SUCCESS) {
185         LOGE("Failed to create spi object!");
186         return ret;
187     }
188     ret = CreateEccCommonSpecImpl(&(spiInstance->paramsSpec), returnCommonParamSpec);
189     if (ret != HCF_SUCCESS) {
190         LOGE("Failed to create spi object!");
191     }
192     FreeEccCommParamsSpec(&(spiInstance->paramsSpec));
193     HcfFree(spiInstance);
194     return ret;
195 }
196