1 /*
2 * Copyright (C) 2022-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 "openssl_common.h"
17
18 #include "securec.h"
19
20 #include <string.h>
21 #include <openssl/err.h>
22 #include "config.h"
23 #include "log.h"
24 #include "memory.h"
25 #include "openssl_adapter.h"
26 #include "result.h"
27 #include "params_parser.h"
28
29 #define PRIMES_2 2
30 #define PRIMES_3 3
31 #define PRIMES_4 4
32 #define PRIMES_5 5
33
34 #define HCF_OPENSSL_DIGEST_NONE_STR "NONE"
35 #define HCF_OPENSSL_DIGEST_MD5_STR "MD5"
36 #define HCF_OPENSSL_DIGEST_SM3_STR "SM3"
37 #define HCF_OPENSSL_DIGEST_SHA1_STR "SHA1"
38 #define HCF_OPENSSL_DIGEST_SHA224_STR "SHA224"
39 #define HCF_OPENSSL_DIGEST_SHA256_STR "SHA256"
40 #define HCF_OPENSSL_DIGEST_SHA384_STR "SHA384"
41 #define HCF_OPENSSL_DIGEST_SHA512_STR "SHA512"
42 #define HCF_OPENSSL_MGF1 "MGF1"
43
44 static const uint32_t ASCII_CODE_ZERO = 48;
45
GetOpensslCurveId(int32_t keyLen,int32_t * returnCurveId)46 HcfResult GetOpensslCurveId(int32_t keyLen, int32_t *returnCurveId)
47 {
48 switch (keyLen) {
49 case HCF_ALG_ECC_224:
50 *returnCurveId = NID_secp224r1;
51 break;
52 case HCF_ALG_ECC_256:
53 *returnCurveId = NID_X9_62_prime256v1;
54 break;
55 case HCF_ALG_SM2_256:
56 *returnCurveId = NID_sm2;
57 break;
58 case HCF_ALG_ECC_384:
59 *returnCurveId = NID_secp384r1;
60 break;
61 case HCF_ALG_ECC_521:
62 *returnCurveId = NID_secp521r1;
63 break;
64 default:
65 LOGE("invalid key size.");
66 return HCF_INVALID_PARAMS;
67 }
68
69 return HCF_SUCCESS;
70 }
71
GetOpensslDigestAlg(uint32_t alg,EVP_MD ** digestAlg)72 HcfResult GetOpensslDigestAlg(uint32_t alg, EVP_MD **digestAlg)
73 {
74 if (digestAlg == NULL) {
75 LOGE("Invalid MD pointer");
76 return HCF_INVALID_PARAMS;
77 }
78 switch (alg) {
79 case HCF_OPENSSL_DIGEST_NONE:
80 *digestAlg = NULL;
81 break;
82 case HCF_OPENSSL_DIGEST_MD5:
83 *digestAlg = (EVP_MD *)EVP_md5();
84 break;
85 case HCF_OPENSSL_DIGEST_SM3:
86 *digestAlg = (EVP_MD *)EVP_sm3();
87 break;
88 case HCF_OPENSSL_DIGEST_SHA1:
89 *digestAlg = (EVP_MD *)EVP_sha1();
90 break;
91 case HCF_OPENSSL_DIGEST_SHA224:
92 *digestAlg = (EVP_MD *)EVP_sha224();
93 break;
94 case HCF_OPENSSL_DIGEST_SHA256:
95 *digestAlg = (EVP_MD *)EVP_sha256();
96 break;
97 case HCF_OPENSSL_DIGEST_SHA384:
98 *digestAlg = (EVP_MD *)EVP_sha384();
99 break;
100 case HCF_OPENSSL_DIGEST_SHA512:
101 *digestAlg = (EVP_MD *)EVP_sha512();
102 break;
103 default:
104 LOGE("Invalid digest num is %u.", alg);
105 return HCF_INVALID_PARAMS;
106 }
107 return HCF_SUCCESS;
108 }
109
GetRsaSpecStringMd(const HcfAlgParaValue md,char ** returnString)110 HcfResult GetRsaSpecStringMd(const HcfAlgParaValue md, char **returnString)
111 {
112 if (returnString == NULL) {
113 LOGE("return string is null");
114 return HCF_INVALID_PARAMS;
115 }
116 char *tmp = NULL;
117 switch (md) {
118 case HCF_OPENSSL_DIGEST_MD5:
119 tmp = HCF_OPENSSL_DIGEST_MD5_STR;
120 break;
121 case HCF_OPENSSL_DIGEST_SM3:
122 tmp = HCF_OPENSSL_DIGEST_SM3_STR;
123 break;
124 case HCF_OPENSSL_DIGEST_SHA1:
125 tmp = HCF_OPENSSL_DIGEST_SHA1_STR;
126 break;
127 case HCF_OPENSSL_DIGEST_SHA224:
128 tmp = HCF_OPENSSL_DIGEST_SHA224_STR;
129 break;
130 case HCF_OPENSSL_DIGEST_SHA256:
131 tmp = HCF_OPENSSL_DIGEST_SHA256_STR;
132 break;
133 case HCF_OPENSSL_DIGEST_SHA384:
134 tmp = HCF_OPENSSL_DIGEST_SHA384_STR;
135 break;
136 case HCF_OPENSSL_DIGEST_SHA512:
137 tmp = HCF_OPENSSL_DIGEST_SHA512_STR;
138 break;
139 default:
140 LOGE("Invalid digest num is %u.", md);
141 return HCF_INVALID_PARAMS;
142 }
143 size_t mdLen = strlen(tmp);
144 char *mdStr = (char *)HcfMalloc(mdLen + 1, 0);
145 if (mdStr == NULL) {
146 LOGE("Failed to allocate md name memory");
147 return HCF_ERR_MALLOC;
148 }
149 (void)memcpy_s(mdStr, mdLen, tmp, mdLen);
150 *returnString = mdStr;
151 return HCF_SUCCESS;
152 }
153
GetRsaSpecStringMGF(char ** returnString)154 HcfResult GetRsaSpecStringMGF(char **returnString)
155 {
156 if (returnString == NULL) {
157 LOGE("return string is null");
158 return HCF_INVALID_PARAMS;
159 }
160 uint32_t mgf1Len = strlen(HCF_OPENSSL_MGF1);
161 char *mgf1Str = (char *)HcfMalloc(mgf1Len + 1, 0);
162 if (mgf1Str == NULL) {
163 LOGE("Failed to allocate mgf1 name memory");
164 return HCF_ERR_MALLOC;
165 }
166 (void)memcpy_s(mgf1Str, mgf1Len, HCF_OPENSSL_MGF1, mgf1Len);
167 *returnString = mgf1Str;
168 return HCF_SUCCESS;
169 }
170
HcfPrintOpensslError(void)171 void HcfPrintOpensslError(void)
172 {
173 char szErr[LOG_PRINT_MAX_LEN] = {0};
174 unsigned long errCode;
175
176 errCode = ERR_get_error();
177 ERR_error_string_n(errCode, szErr, LOG_PRINT_MAX_LEN);
178
179 LOGE("[Openssl]: engine fail, error code = %lu, error string = %s", errCode, szErr);
180 }
181
GetOpensslPadding(int32_t padding,int32_t * opensslPadding)182 HcfResult GetOpensslPadding(int32_t padding, int32_t *opensslPadding)
183 {
184 if (opensslPadding == NULL) {
185 LOGE("return openssl padding pointer is null");
186 return HCF_INVALID_PARAMS;
187 }
188 switch (padding) {
189 case HCF_ALG_NOPADDING:
190 *opensslPadding = RSA_NO_PADDING;
191 return HCF_SUCCESS;
192
193 case HCF_OPENSSL_RSA_PKCS1_PADDING:
194 *opensslPadding = RSA_PKCS1_PADDING;
195 return HCF_SUCCESS;
196
197 case HCF_OPENSSL_RSA_PKCS1_OAEP_PADDING:
198 *opensslPadding = RSA_PKCS1_OAEP_PADDING;
199 return HCF_SUCCESS;
200
201 case HCF_OPENSSL_RSA_PSS_PADDING:
202 *opensslPadding = RSA_PKCS1_PSS_PADDING;
203 return HCF_SUCCESS;
204
205 default:
206 LOGE("Invalid framwork padding = %d", padding);
207 return HCF_INVALID_PARAMS;
208 }
209 }
210
GetRealPrimes(int32_t primesFlag)211 int32_t GetRealPrimes(int32_t primesFlag)
212 {
213 switch (primesFlag) {
214 case HCF_OPENSSL_PRIMES_2:
215 return PRIMES_2;
216 case HCF_OPENSSL_PRIMES_3:
217 return PRIMES_3;
218 case HCF_OPENSSL_PRIMES_4:
219 return PRIMES_4;
220 case HCF_OPENSSL_PRIMES_5:
221 return PRIMES_5;
222 default:
223 LOGI("set default primes 2");
224 return PRIMES_2;
225 }
226 }
227
IsBigEndian(void)228 bool IsBigEndian(void)
229 {
230 uint32_t *pointer = (uint32_t *)&ASCII_CODE_ZERO;
231 char firstChar = *((char *)pointer);
232 if (firstChar == '0') {
233 return false;
234 } else {
235 return true;
236 }
237 }
238
BigIntegerToBigNum(const HcfBigInteger * src,BIGNUM ** dest)239 HcfResult BigIntegerToBigNum(const HcfBigInteger *src, BIGNUM **dest)
240 {
241 if (src == NULL || dest == NULL) {
242 LOGE("Invalid input parameter.");
243 return HCF_INVALID_PARAMS;
244 }
245
246 if (IsBigEndian()) {
247 *dest = Openssl_BN_bin2bn((src->data), (src->len), NULL);
248 } else {
249 *dest = Openssl_BN_lebin2bn((src->data), (src->len), NULL);
250 }
251
252 if (*dest == NULL) {
253 LOGE("translate BigInteger to BIGNUM failed.");
254 HcfPrintOpensslError();
255 return HCF_ERR_CRYPTO_OPERATION;
256 }
257 return HCF_SUCCESS;
258 }
259
BigNumToBigInteger(const BIGNUM * src,HcfBigInteger * dest)260 HcfResult BigNumToBigInteger(const BIGNUM *src, HcfBigInteger *dest)
261 {
262 if (src == NULL || dest == NULL) {
263 LOGE("Invalid input parameter.");
264 return HCF_INVALID_PARAMS;
265 }
266
267 int32_t len = Openssl_BN_num_bytes(src);
268 if (len <= 0) {
269 LOGE("Invalid input parameter.");
270 HcfPrintOpensslError();
271 return HCF_ERR_CRYPTO_OPERATION;
272 }
273 dest->data = (unsigned char *)HcfMalloc(len, 0);
274 if (dest->data == NULL) {
275 LOGE("Alloc dest->data memeory failed.");
276 return HCF_ERR_MALLOC;
277 }
278 dest->len = len;
279
280 int32_t resLen = -1;
281 if (IsBigEndian()) {
282 resLen = Openssl_BN_bn2binpad(src, dest->data, dest->len);
283 } else {
284 resLen = Openssl_BN_bn2lebinpad(src, dest->data, dest->len);
285 }
286
287 if (resLen != dest->len) {
288 LOGE("translate BIGNUM to BigInteger failed.");
289 HcfPrintOpensslError();
290 HcfFree(dest->data);
291 dest->data = NULL;
292 dest->len = 0;
293 return HCF_ERR_CRYPTO_OPERATION;
294 }
295 return HCF_SUCCESS;
296 }
297