1 /*
2 * Copyright (c) 2021-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 #ifdef HKS_CONFIG_FILE
17 #include HKS_CONFIG_FILE
18 #else
19 #include "hks_config.h"
20 #endif
21
22 #include "hks_check_paramset.h"
23
24 #ifdef L2_STANDARD
25 #include "hks_openssl_dh.h"
26 #include "hks_openssl_rsa.h"
27 #endif
28
29 #include <stddef.h>
30
31 #include "hks_base_check.h"
32 #include "hks_common_check.h"
33 #include "hks_crypto_hal.h"
34 #include "hks_log.h"
35 #include "hks_param.h"
36 #include "hks_template.h"
37 #include "securec.h"
38
39 #ifdef _CUT_AUTHENTICATE_
40 #undef HKS_SUPPORT_HASH_C
41 #undef HKS_SUPPORT_RSA_C
42 #undef HKS_SUPPORT_ECC_C
43 #undef HKS_SUPPORT_X25519_C
44 #undef HKS_SUPPORT_ED25519_C
45 #undef HKS_SUPPORT_KDF_PBKDF2
46 #endif /* _CUT_AUTHENTICATE_ */
47
48 #define HKS_DEFAULT_PBKDF2_ITERATION 1000
49 #define HKS_MAX_PBKDF2_ITERATION 0x80000U
50 #define HKS_DEFAULT_PBKDF2_SALT_SIZE 16
51
52 #ifndef _CUT_AUTHENTICATE_
53 static uint32_t g_genKeyAlg[] = {
54 #ifdef HKS_SUPPORT_RSA_C
55 HKS_ALG_RSA,
56 #endif
57 #ifdef HKS_SUPPORT_AES_C
58 HKS_ALG_AES,
59 #endif
60 #ifdef HKS_SUPPORT_ECC_C
61 HKS_ALG_ECC,
62 #endif
63 #ifdef HKS_SUPPORT_HMAC_C
64 HKS_ALG_HMAC,
65 #endif
66 #ifdef HKS_SUPPORT_ED25519_C
67 HKS_ALG_ED25519,
68 #endif
69 #ifdef HKS_SUPPORT_X25519_C
70 HKS_ALG_X25519,
71 #endif
72 #ifdef HKS_SUPPORT_DSA_C
73 HKS_ALG_DSA,
74 #endif
75 #ifdef HKS_SUPPORT_DH_C
76 HKS_ALG_DH,
77 #endif
78 #ifdef HKS_SUPPORT_ECDH_C
79 HKS_ALG_ECDH,
80 #endif
81 #ifdef HKS_SUPPORT_SM3_C
82 HKS_ALG_SM3,
83 #endif
84 #ifdef HKS_SUPPORT_SM2_C
85 HKS_ALG_SM2,
86 #endif
87 #ifdef HKS_SUPPORT_SM4_C
88 HKS_ALG_SM4,
89 #endif
90 };
91
92 static uint32_t g_importKeyAlg[] = {
93 #ifdef HKS_SUPPORT_RSA_C
94 HKS_ALG_RSA,
95 #endif
96 #ifdef HKS_SUPPORT_AES_C
97 HKS_ALG_AES,
98 #endif
99 #ifdef HKS_SUPPORT_ECC_C
100 HKS_ALG_ECC,
101 #endif
102 #ifdef HKS_SUPPORT_X25519_C
103 HKS_ALG_X25519,
104 #endif
105 #ifdef HKS_SUPPORT_ED25519_C
106 HKS_ALG_ED25519,
107 #endif
108 #ifdef HKS_SUPPORT_DSA_C
109 HKS_ALG_DSA,
110 #endif
111 #ifdef HKS_SUPPORT_DH_C
112 HKS_ALG_DH,
113 #endif
114 #ifdef HKS_SUPPORT_HMAC_C
115 HKS_ALG_HMAC,
116 #endif
117 #ifdef HKS_SUPPORT_SM2_C
118 HKS_ALG_SM2,
119 #endif
120 #ifdef HKS_SUPPORT_SM3_C
121 HKS_ALG_SM3,
122 #endif
123 #ifdef HKS_SUPPORT_SM4_C
124 HKS_ALG_SM4,
125 #endif
126 };
127
128 static uint32_t g_cipherAlg[] = {
129 #ifdef HKS_SUPPORT_RSA_C
130 HKS_ALG_RSA,
131 #endif
132 #ifdef HKS_SUPPORT_AES_C
133 HKS_ALG_AES,
134 #endif
135 #ifdef HKS_SUPPORT_SM2_C
136 HKS_ALG_SM2,
137 #endif
138 #ifdef HKS_SUPPORT_SM4_C
139 HKS_ALG_SM4,
140 #endif
141 };
142 #ifdef HKS_SUPPORT_API_SIGN_VERIFY
143 static uint32_t g_signAlg[] = {
144 #ifdef HKS_SUPPORT_RSA_C
145 HKS_ALG_RSA,
146 #endif
147 #ifdef HKS_SUPPORT_DSA_C
148 HKS_ALG_DSA,
149 #endif
150 #ifdef HKS_SUPPORT_ECC_C
151 HKS_ALG_ECC,
152 #endif
153 #ifdef HKS_SUPPORT_ED25519_C
154 HKS_ALG_ED25519,
155 #endif
156 #ifdef HKS_SUPPORT_SM2_C
157 HKS_ALG_SM2,
158 #endif
159 };
160 #endif
161
162 static uint32_t g_agreeAlg[] = {
163 #ifdef HKS_SUPPORT_X25519_C
164 HKS_ALG_X25519,
165 #endif
166 #ifdef HKS_SUPPORT_ECDH_C
167 HKS_ALG_ECDH,
168 #endif
169 #ifdef HKS_SUPPORT_DH_C
170 HKS_ALG_DH,
171 #endif
172 };
173
174 static uint32_t g_agreeAlgLocal[] = {
175 #ifdef HKS_SUPPORT_ECDH_C
176 HKS_ALG_ECDH,
177 #endif
178 #ifdef HKS_SUPPORT_X25519_C
179 HKS_ALG_X25519,
180 #endif
181 #ifdef HKS_SUPPORT_DH_C
182 HKS_ALG_DH,
183 #endif
184 };
185
186 static uint32_t g_unwrapSuite[] = {
187 #if defined(HKS_SUPPORT_X25519_C) && defined(HKS_SUPPORT_AES_GCM)
188 HKS_UNWRAP_SUITE_X25519_AES_256_GCM_NOPADDING,
189 #endif
190 #if defined(HKS_SUPPORT_ECDH_C) && defined(HKS_SUPPORT_AES_GCM)
191 HKS_UNWRAP_SUITE_ECDH_AES_256_GCM_NOPADDING,
192 #endif
193 #if defined(HKS_SUPPORT_SM2_C) && defined(HKS_SUPPORT_SM4_C)
194 HKS_UNWRAP_SUITE_SM2_SM4_128_CBC_PKCS7_WITH_VERIFY_DIG_SM3,
195 HKS_UNWRAP_SUITE_SM2_SM4_128_CBC_PKCS7
196 #endif
197 };
198 #endif /* _CUT_AUTHENTICATE_ */
199
200 static uint32_t g_deriveAlg[] = {
201 #ifdef HKS_SUPPORT_KDF_HKDF
202 HKS_ALG_HKDF,
203 #endif
204 #ifdef HKS_SUPPORT_KDF_PBKDF2
205 HKS_ALG_PBKDF2,
206 #endif
207 #ifdef HKS_SUPPORT_KDF_SM3
208 HKS_ALG_GMKDF,
209 #endif
210 };
211
212 static uint32_t g_deriveAlgLocal[] = {
213 #ifdef HKS_SUPPORT_KDF_HKDF
214 HKS_ALG_HKDF,
215 #endif
216 };
217
218 static uint32_t g_digest[] = {
219 HKS_DIGEST_SHA1,
220 HKS_DIGEST_SHA224,
221 HKS_DIGEST_SHA256,
222 HKS_DIGEST_SHA384,
223 HKS_DIGEST_SHA512,
224 HKS_DIGEST_SM3
225 };
226 static uint32_t g_macDigest[] = {
227 HKS_DIGEST_SHA1,
228 HKS_DIGEST_SHA224,
229 HKS_DIGEST_SHA256,
230 HKS_DIGEST_SHA384,
231 HKS_DIGEST_SHA512,
232 HKS_DIGEST_SM3
233 };
234 #ifdef HKS_SUPPORT_AES_C
235 static uint32_t g_aesKeySizeLocal[] = {
236 HKS_AES_KEY_SIZE_128,
237 HKS_AES_KEY_SIZE_192,
238 HKS_AES_KEY_SIZE_256,
239 };
240 #endif
241 #ifdef HKS_SUPPORT_RSA_C
242 static uint32_t g_rsaKeySizeLocal[] = {
243 HKS_RSA_KEY_SIZE_512,
244 HKS_RSA_KEY_SIZE_768,
245 HKS_RSA_KEY_SIZE_1024,
246 HKS_RSA_KEY_SIZE_2048,
247 HKS_RSA_KEY_SIZE_3072,
248 HKS_RSA_KEY_SIZE_4096,
249 };
250 #endif
251
252 static uint32_t g_cipherAlgLocal[] = {
253 #ifdef HKS_SUPPORT_AES_C
254 HKS_ALG_AES,
255 #endif
256 #ifdef HKS_SUPPORT_RSA_C
257 HKS_ALG_RSA,
258 #endif
259 };
260
261 static uint32_t g_symmetricAlgorithm[] = {
262 #ifdef HKS_SUPPORT_AES_C
263 HKS_ALG_AES,
264 #endif
265 #ifdef HKS_SUPPORT_HMAC_C
266 HKS_ALG_HMAC,
267 #endif
268 #ifdef HKS_SUPPORT_SM3_C
269 HKS_ALG_SM3,
270 #endif
271 #ifdef HKS_SUPPORT_SM4_C
272 HKS_ALG_SM4,
273 #endif
274 };
275
CheckAndGetAlgorithm(const struct HksParamSet * paramSet,const uint32_t * expectAlg,uint32_t expectCnt,uint32_t * alg)276 static int32_t CheckAndGetAlgorithm(
277 const struct HksParamSet *paramSet, const uint32_t *expectAlg, uint32_t expectCnt, uint32_t *alg)
278 {
279 struct HksParam *algParam = NULL;
280 int32_t ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algParam);
281 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL,
282 "get param 0x%" LOG_PUBLIC "x failed!", HKS_TAG_ALGORITHM)
283
284 ret = HksCheckValue(algParam->uint32Param, expectAlg, expectCnt);
285 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_ALGORITHM,
286 "alg value %" LOG_PUBLIC "u not expected", algParam->uint32Param)
287
288 *alg = algParam->uint32Param;
289 return ret;
290 }
291
CheckAndGetDigest(const struct HksParamSet * paramSet,const uint32_t * expectDigest,uint32_t expectCnt,uint32_t * digest)292 static int32_t CheckAndGetDigest(
293 const struct HksParamSet *paramSet, const uint32_t *expectDigest, uint32_t expectCnt, uint32_t *digest)
294 {
295 struct HksParam *digestParam = NULL;
296 int32_t ret = HksGetParam(paramSet, HKS_TAG_DIGEST, &digestParam);
297 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_DIGEST_FAIL,
298 "get param get 0x%" LOG_PUBLIC "x failed!", HKS_TAG_DIGEST)
299
300 ret = HksCheckValue(digestParam->uint32Param, expectDigest, expectCnt);
301 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_DIGEST,
302 "digest value %" LOG_PUBLIC "u not expected", digestParam->uint32Param)
303
304 *digest = digestParam->uint32Param;
305 return ret;
306 }
307
308 #ifndef _CUT_AUTHENTICATE_
CheckGenKeyParamsByAlg(uint32_t alg,const struct HksParamSet * paramSet,struct ParamsValues * params,uint32_t keyFlag)309 static int32_t CheckGenKeyParamsByAlg(uint32_t alg, const struct HksParamSet *paramSet,
310 struct ParamsValues *params, uint32_t keyFlag)
311 {
312 int32_t ret = HksGetInputParmasByAlg(alg, HKS_CHECK_TYPE_GEN_KEY, paramSet, params);
313 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret,
314 "get input params by algorithm failed, ret = %" LOG_PUBLIC "d", ret)
315
316 ret = HksCheckFixedParams(alg, HKS_CHECK_TYPE_GEN_KEY, params);
317 HKS_IF_NOT_SUCC_RETURN(ret, ret)
318
319 ret = HksCheckGenKeyPurpose(alg, params->purpose.value, keyFlag);
320 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret,
321 "check purpose not expected, purpose = %" LOG_PUBLIC "d", params->purpose.value);
322 return HksCheckGenKeyMutableParams(alg, params);
323 }
324
CheckGenKeyMacDeriveParams(uint32_t alg,uint32_t inputPurpose,const struct HksParamSet * paramSet,struct ParamsValues * params,uint32_t keyFlag)325 static int32_t CheckGenKeyMacDeriveParams(
326 uint32_t alg, uint32_t inputPurpose, const struct HksParamSet *paramSet, struct ParamsValues *params,
327 uint32_t keyFlag)
328 {
329 if (alg != HKS_ALG_AES && alg != HKS_ALG_HMAC && alg != HKS_ALG_SM3 && alg != HKS_ALG_SM4) {
330 HKS_LOG_E("check mac or derive, not valid alg, alg: %" LOG_PUBLIC "u", alg);
331 return HKS_ERROR_INVALID_PURPOSE;
332 }
333
334 int32_t ret = HksCheckGenKeyPurpose(alg, inputPurpose, keyFlag);
335 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check purpose invalid, purpose 0x%" LOG_PUBLIC "x", inputPurpose)
336
337 if (inputPurpose == HKS_KEY_PURPOSE_MAC) {
338 ret = HksGetInputParmasByAlg(alg, HKS_CHECK_TYPE_GEN_MAC_KEY, paramSet, params);
339 } else {
340 ret = HksGetInputParmasByAlg(alg, HKS_CHECK_TYPE_GEN_DERIVE_KEY, paramSet, params);
341 }
342 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get input params by algorithm failed, ret = %" LOG_PUBLIC "d", ret)
343
344 if (inputPurpose == HKS_KEY_PURPOSE_MAC) {
345 ret = HksCheckFixedParams(alg, HKS_CHECK_TYPE_GEN_MAC_KEY, params);
346 } else {
347 ret = HksCheckFixedParams(alg, HKS_CHECK_TYPE_GEN_DERIVE_KEY, params);
348 }
349 HKS_IF_NOT_SUCC_LOGE(ret, "get input params by algorithm failed, ret = %" LOG_PUBLIC "d", ret)
350
351 return ret;
352 }
353
CoreCheckGenKeyParams(const struct HksParamSet * paramSet,struct ParamsValues * params,uint32_t keyFlag)354 static int32_t CoreCheckGenKeyParams(const struct HksParamSet *paramSet, struct ParamsValues *params, uint32_t keyFlag)
355 {
356 uint32_t alg;
357 int32_t ret = HksCheckParamSetTag(paramSet);
358 HKS_IF_NOT_SUCC_RETURN(ret, ret)
359
360 ret = CheckAndGetAlgorithm(paramSet, g_genKeyAlg, HKS_ARRAY_SIZE(g_genKeyAlg), &alg);
361 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check and get alg failed")
362
363 struct HksParam *purposeParam = NULL;
364 struct HksParam *batchPurposeParam = NULL;
365 ret = HksGetParam(paramSet, HKS_TAG_PURPOSE, &purposeParam);
366 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_PURPOSE_FAIL,
367 "get param get 0x%" LOG_PUBLIC "x failed", HKS_TAG_PURPOSE)
368 ret = HksGetParam(paramSet, HKS_TAG_BATCH_PURPOSE, &batchPurposeParam);
369 if (ret == HKS_SUCCESS) {
370 if ((purposeParam->uint32Param | batchPurposeParam->uint32Param) != purposeParam->uint32Param) {
371 HKS_LOG_E("batchPurposeParam should fall within the scope of purposeParam.");
372 return HKS_ERROR_INVALID_PURPOSE;
373 }
374 }
375
376 if (((purposeParam->uint32Param & HKS_KEY_PURPOSE_DERIVE) != 0) ||
377 ((purposeParam->uint32Param & HKS_KEY_PURPOSE_MAC) != 0)) {
378 return CheckGenKeyMacDeriveParams(alg, purposeParam->uint32Param, paramSet, params, keyFlag);
379 }
380
381 return CheckGenKeyParamsByAlg(alg, paramSet, params, keyFlag);
382 }
383
CheckImportKeySize(uint32_t alg,const struct ParamsValues * params,const struct HksBlob * key)384 static int32_t CheckImportKeySize(uint32_t alg, const struct ParamsValues *params, const struct HksBlob *key)
385 {
386 int32_t ret = HKS_SUCCESS;
387 switch (alg) {
388 case HKS_ALG_ED25519:
389 case HKS_ALG_X25519:
390 case HKS_ALG_RSA:
391 case HKS_ALG_ECC:
392 case HKS_ALG_SM2:
393 case HKS_ALG_DH: {
394 if (key->size < sizeof(struct HksPubKeyInfo)) {
395 ret = HKS_ERROR_INVALID_KEY_INFO;
396 break;
397 }
398 struct HksPubKeyInfo *keyMaterial = (struct HksPubKeyInfo *)(key->data);
399 if ((keyMaterial->keyAlg != alg) || (keyMaterial->keySize != params->keyLen.value)) {
400 ret = HKS_ERROR_INVALID_KEY_INFO;
401 }
402 break;
403 }
404 #ifdef HKS_SUPPORT_DSA_C
405 case HKS_ALG_DSA:
406 break;
407 #endif
408 default:
409 return HKS_ERROR_INVALID_ALGORITHM;
410 }
411 return ret;
412 }
413
CheckAndGetWrappedKeyUnwrapAlgSuite(const struct HksParamSet * paramSet,uint32_t * algSuite)414 static int32_t CheckAndGetWrappedKeyUnwrapAlgSuite(const struct HksParamSet *paramSet, uint32_t *algSuite)
415 {
416 struct HksParam *algorithmSuite = NULL;
417 int32_t ret = HksGetParam(paramSet, HKS_TAG_UNWRAP_ALGORITHM_SUITE, &algorithmSuite);
418 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_ALG_FAIL, "get unwrap algorithm suite fail")
419
420 ret = HksCheckValue(algorithmSuite->uint32Param, g_unwrapSuite, HKS_ARRAY_SIZE(g_unwrapSuite));
421 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_ALGORITHM,
422 "unwrap algorithm suite value %" LOG_PUBLIC "u not expected", algorithmSuite->uint32Param)
423
424 *algSuite = algorithmSuite->uint32Param;
425 return HKS_SUCCESS;
426 }
427
428 #ifdef HKS_SUPPORT_API_SIGN_VERIFY
CheckSignVerifyParamsByAlg(uint32_t cmdId,uint32_t alg,const struct ParamsValues * inputParams)429 static int32_t CheckSignVerifyParamsByAlg(uint32_t cmdId, uint32_t alg, const struct ParamsValues *inputParams)
430 {
431 int32_t ret = HksCheckFixedParams(alg, HKS_CHECK_TYPE_USE_KEY, inputParams);
432 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check sign or verify fixed params failed, ret = %" LOG_PUBLIC "d", ret)
433
434 ret = HksCheckSignVerifyMutableParams(cmdId, alg, inputParams);
435 HKS_IF_NOT_SUCC_LOGE(ret, "check sign or verify mutable params failed, ret = %" LOG_PUBLIC "d", ret)
436
437 return ret;
438 }
439 #endif
440 #endif /* _CUT_AUTHENTICATE_ */
441
CheckCipherParamsByAlg(uint32_t cmdId,uint32_t alg,const struct HksParamSet * paramSet,const struct ParamsValues * inputParams)442 static int32_t CheckCipherParamsByAlg(
443 uint32_t cmdId, uint32_t alg, const struct HksParamSet *paramSet, const struct ParamsValues *inputParams)
444 {
445 int32_t ret = HksCheckFixedParams(alg, HKS_CHECK_TYPE_USE_KEY, inputParams);
446 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "cipher check fixed params failed, ret = %" LOG_PUBLIC "d", ret)
447
448 ret = HksCheckCipherMutableParams(cmdId, alg, inputParams);
449 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "cipher check mutable params failed, ret = %" LOG_PUBLIC "d", ret)
450
451 ret = HksCheckCipherMaterialParams(alg, inputParams, paramSet);
452 HKS_IF_NOT_SUCC_LOGE(ret, "cipher check material params failed, ret = %" LOG_PUBLIC "d", ret)
453
454 return ret;
455 }
456
457 #ifndef _CUT_AUTHENTICATE_
458 #ifdef HKS_SUPPORT_KDF_PBKDF2
CheckPbkdf2DeriveKeyParams(const struct HksParamSet * paramSet)459 static int32_t CheckPbkdf2DeriveKeyParams(const struct HksParamSet *paramSet)
460 {
461 struct HksParam *iterationParam = NULL;
462 int32_t ret = HksGetParam(paramSet, HKS_TAG_ITERATION, &iterationParam);
463 HKS_IF_NOT_SUCC_RETURN(ret, HKS_ERROR_CHECK_GET_ITERATION_FAIL)
464
465 if (iterationParam->uint32Param < HKS_DEFAULT_PBKDF2_ITERATION ||
466 iterationParam->uint32Param > HKS_MAX_PBKDF2_ITERATION) {
467 HKS_LOG_E("invalid iteration param %" LOG_PUBLIC "u", iterationParam->uint32Param);
468 return HKS_ERROR_INVALID_ITERATION;
469 }
470
471 struct HksParam *saltParam = NULL;
472 ret = HksGetParam(paramSet, HKS_TAG_SALT, &saltParam);
473 HKS_IF_NOT_SUCC_RETURN(ret, HKS_ERROR_CHECK_GET_SALT_FAIL)
474
475 if ((CheckBlob(&saltParam->blob) != HKS_SUCCESS) || (saltParam->blob.size < HKS_DEFAULT_PBKDF2_SALT_SIZE)) {
476 return HKS_ERROR_INVALID_SALT;
477 }
478
479 return HKS_SUCCESS;
480 }
481 #endif
482
HksCoreCheckGenKeyParams(const struct HksBlob * keyAlias,const struct HksParamSet * paramSet,const struct HksBlob * keyIn,const struct HksBlob * keyOut,uint32_t keyFlag)483 int32_t HksCoreCheckGenKeyParams(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
484 const struct HksBlob *keyIn, const struct HksBlob *keyOut, uint32_t keyFlag)
485 {
486 (void)keyAlias;
487 (void)keyIn;
488 (void)keyOut;
489 struct ParamsValues params;
490 (void)memset_s(¶ms, sizeof(params), 0, sizeof(params));
491 return CoreCheckGenKeyParams(paramSet, ¶ms, keyFlag);
492 }
493
CheckRsaKeyLen(uint32_t alg,uint32_t keyType,const struct ParamsValues * params,const struct HksBlob * key)494 static int32_t CheckRsaKeyLen(uint32_t alg, uint32_t keyType, const struct ParamsValues *params,
495 const struct HksBlob *key)
496 {
497 (void)keyType;
498 if (key->size < sizeof(struct HksKeyMaterialRsa)) {
499 HKS_LOG_E("invalid import key size: %" LOG_PUBLIC "u", key->size);
500 return HKS_ERROR_INVALID_KEY_INFO;
501 }
502
503 struct HksKeyMaterialRsa *keyMaterial = (struct HksKeyMaterialRsa *)(key->data);
504 if ((keyMaterial->keyAlg != alg) || (keyMaterial->keySize != params->keyLen.value)) {
505 HKS_LOG_E("invalid import key material");
506 return HKS_ERROR_INVALID_KEY_INFO;
507 }
508
509 if ((keyMaterial->nSize > HKS_RSA_KEY_SIZE_4096) || (keyMaterial->nSize == 0) ||
510 (keyMaterial->dSize > HKS_RSA_KEY_SIZE_4096) || (keyMaterial->dSize == 0) ||
511 (keyMaterial->eSize > HKS_RSA_KEY_SIZE_4096) || (keyMaterial->eSize == 0)) {
512 HKS_LOG_E("invalid import key material n/d/e size");
513 return HKS_ERROR_INVALID_KEY_INFO;
514 }
515
516 uint32_t keySize = sizeof(struct HksKeyMaterialRsa) + keyMaterial->nSize + keyMaterial->dSize + keyMaterial->eSize;
517 if (key->size < keySize) {
518 HKS_LOG_E("import key size[%" LOG_PUBLIC "u] smaller than keySize[%" LOG_PUBLIC "u]", key->size, keySize);
519 return HKS_ERROR_INVALID_KEY_INFO;
520 }
521
522 return HKS_SUCCESS;
523 }
524
CheckEccKeyLen(uint32_t alg,uint32_t keyType,const struct ParamsValues * params,const struct HksBlob * key)525 static int32_t CheckEccKeyLen(uint32_t alg, uint32_t keyType, const struct ParamsValues *params,
526 const struct HksBlob *key)
527 {
528 if (key->size < sizeof(struct HksKeyMaterialEcc)) {
529 HKS_LOG_E("invalid import key size: %" LOG_PUBLIC "u", key->size);
530 return HKS_ERROR_INVALID_KEY_INFO;
531 }
532
533 struct HksKeyMaterialEcc *keyMaterial = (struct HksKeyMaterialEcc *)(key->data);
534 if ((keyMaterial->keyAlg != alg) || (keyMaterial->keySize != params->keyLen.value)) {
535 HKS_LOG_E("invalid import key material");
536 return HKS_ERROR_INVALID_KEY_INFO;
537 }
538
539 if ((keyMaterial->xSize > HKS_ECC_KEY_SIZE_521) || (keyMaterial->ySize > HKS_ECC_KEY_SIZE_521) ||
540 (keyMaterial->zSize > HKS_ECC_KEY_SIZE_521)) {
541 HKS_LOG_E("invalid import key material x/y/z size, bigger than 521");
542 return HKS_ERROR_INVALID_KEY_INFO;
543 }
544
545 if (keyMaterial->zSize == 0) {
546 HKS_LOG_E("invalid import key material z size: 0");
547 return HKS_ERROR_INVALID_KEY_INFO;
548 }
549 if ((keyType == HKS_KEY_TYPE_KEY_PAIR) && ((keyMaterial->xSize == 0) || (keyMaterial->ySize == 0))) {
550 HKS_LOG_E("invalid import key material x/y size: 0");
551 return HKS_ERROR_INVALID_KEY_INFO;
552 }
553
554 uint32_t keySize = sizeof(struct HksKeyMaterialEcc) + keyMaterial->xSize + keyMaterial->ySize + keyMaterial->zSize;
555 if (key->size < keySize) {
556 HKS_LOG_E("import key size[%" LOG_PUBLIC "u] smaller than keySize[%" LOG_PUBLIC "u]", key->size, keySize);
557 return HKS_ERROR_INVALID_KEY_INFO;
558 }
559
560 return HKS_SUCCESS;
561 }
562
CheckDsaKeyLen(uint32_t alg,uint32_t keyType,const struct ParamsValues * params,const struct HksBlob * key)563 static int32_t CheckDsaKeyLen(uint32_t alg, uint32_t keyType, const struct ParamsValues *params,
564 const struct HksBlob *key)
565 {
566 if (key->size < sizeof(struct HksKeyMaterialDsa)) {
567 HKS_LOG_E("invalid import key size: %" LOG_PUBLIC "u", key->size);
568 return HKS_ERROR_INVALID_KEY_INFO;
569 }
570
571 struct HksKeyMaterialDsa *keyMaterial = (struct HksKeyMaterialDsa *)(key->data);
572 if ((keyMaterial->keyAlg != alg) || (keyMaterial->keySize != params->keyLen.value)) {
573 HKS_LOG_E("invalid import key material");
574 return HKS_ERROR_INVALID_KEY_INFO;
575 }
576
577 if ((keyMaterial->xSize > MAX_KEY_SIZE) || (keyMaterial->ySize > MAX_KEY_SIZE) ||
578 (keyMaterial->pSize > MAX_KEY_SIZE) || (keyMaterial->qSize > MAX_KEY_SIZE) ||
579 (keyMaterial->gSize > MAX_KEY_SIZE)) {
580 HKS_LOG_E("invalid import key material x/y/p/q/g size, bigger than 2048");
581 return HKS_ERROR_INVALID_KEY_INFO;
582 }
583
584 if ((keyMaterial->xSize == 0) ||
585 (keyMaterial->pSize == 0) || (keyMaterial->qSize == 0) || (keyMaterial->gSize == 0)) {
586 HKS_LOG_E("invalid import key material x/p/q/g size: 0");
587 return HKS_ERROR_INVALID_KEY_INFO;
588 }
589
590 if ((keyType == HKS_KEY_TYPE_KEY_PAIR) && (keyMaterial->ySize == 0)) {
591 HKS_LOG_E("invalid import key material y size: 0");
592 return HKS_ERROR_INVALID_KEY_INFO;
593 }
594
595 uint32_t keySize = sizeof(struct HksKeyMaterialDsa) + keyMaterial->xSize + keyMaterial->ySize +
596 keyMaterial->pSize + keyMaterial->qSize + keyMaterial->gSize;
597 if (key->size < keySize) {
598 HKS_LOG_E("import key size[%" LOG_PUBLIC "u] smaller than keySize[%" LOG_PUBLIC "u]", key->size, keySize);
599 return HKS_ERROR_INVALID_KEY_INFO;
600 }
601
602 return HKS_SUCCESS;
603 }
604
CheckCurve25519KeyLen(uint32_t alg,uint32_t keyType,const struct ParamsValues * params,const struct HksBlob * key)605 static int32_t CheckCurve25519KeyLen(uint32_t alg, uint32_t keyType, const struct ParamsValues *params,
606 const struct HksBlob *key)
607 {
608 if (key->size < sizeof(struct HksKeyMaterial25519)) {
609 HKS_LOG_E("invalid import Curve25519 key size: %" LOG_PUBLIC "u", key->size);
610 return HKS_ERROR_INVALID_KEY_INFO;
611 }
612
613 struct HksKeyMaterial25519 *keyMaterial = (struct HksKeyMaterial25519 *)(key->data);
614 if ((keyMaterial->keyAlg != alg) || (keyMaterial->keySize != params->keyLen.value)) {
615 HKS_LOG_E("invalid import Curve25519 key material");
616 return HKS_ERROR_INVALID_KEY_INFO;
617 }
618
619 if ((keyMaterial->pubKeySize > HKS_CURVE25519_KEY_SIZE_256) ||
620 (keyMaterial->priKeySize > HKS_CURVE25519_KEY_SIZE_256)) {
621 HKS_LOG_E("invalid import Curve25519 key material pubKey/priKey size, bigger than 256");
622 return HKS_ERROR_INVALID_KEY_INFO;
623 }
624
625 if (keyMaterial->priKeySize == 0) {
626 HKS_LOG_E("invalid import Curve25519 key material priKey size: 0");
627 return HKS_ERROR_INVALID_KEY_INFO;
628 }
629
630 if ((keyType == HKS_KEY_TYPE_KEY_PAIR) && (keyMaterial->pubKeySize == 0)) {
631 HKS_LOG_E("invalid import Curve25519 key material pubKey size: 0");
632 return HKS_ERROR_INVALID_KEY_INFO;
633 }
634
635 uint32_t keySize = sizeof(struct HksKeyMaterial25519) + keyMaterial->pubKeySize + keyMaterial->priKeySize;
636 if (key->size < keySize) {
637 HKS_LOG_E("import key size[%" LOG_PUBLIC "u] smaller than keySize[%" LOG_PUBLIC "u]", key->size, keySize);
638 return HKS_ERROR_INVALID_KEY_INFO;
639 }
640
641 return HKS_SUCCESS;
642 }
643
CheckDHKeyLen(uint32_t alg,uint32_t keyType,const struct ParamsValues * params,const struct HksBlob * key)644 static int32_t CheckDHKeyLen(uint32_t alg, uint32_t keyType, const struct ParamsValues *params,
645 const struct HksBlob *key)
646 {
647 if (key->size < sizeof(struct HksKeyMaterialDh)) {
648 HKS_LOG_E("invalid import DH key size: %" LOG_PUBLIC "u", key->size);
649 return HKS_ERROR_INVALID_KEY_INFO;
650 }
651
652 struct HksKeyMaterialDh *keyMaterial = (struct HksKeyMaterialDh *)(key->data);
653 if ((keyMaterial->keyAlg != alg) || (keyMaterial->keySize != params->keyLen.value)) {
654 HKS_LOG_E("invalid import DH key material");
655 return HKS_ERROR_INVALID_KEY_INFO;
656 }
657
658 if ((keyMaterial->pubKeySize > HKS_DH_KEY_SIZE_4096) || (keyMaterial->priKeySize > HKS_DH_KEY_SIZE_4096)) {
659 HKS_LOG_E("invalid import DH key material pubKey/priKey size, bigger than 4096");
660 return HKS_ERROR_INVALID_KEY_INFO;
661 }
662
663 if (keyMaterial->priKeySize == 0) {
664 HKS_LOG_E("invalid import DH key material priKey size: 0");
665 return HKS_ERROR_INVALID_KEY_INFO;
666 }
667
668 if ((keyType == HKS_KEY_TYPE_KEY_PAIR) && (keyMaterial->pubKeySize == 0)) {
669 HKS_LOG_E("invalid import DH key material pubKey size: 0");
670 return HKS_ERROR_INVALID_KEY_INFO;
671 }
672
673 uint32_t keySize = sizeof(struct HksKeyMaterialDh) + keyMaterial->pubKeySize + keyMaterial->priKeySize;
674 if (key->size < keySize) {
675 HKS_LOG_E("import key size[%" LOG_PUBLIC "u] smaller than keySize[%" LOG_PUBLIC "u]", key->size, keySize);
676 return HKS_ERROR_INVALID_KEY_INFO;
677 }
678
679 return HKS_SUCCESS;
680 }
681
CheckKeyLen(uint32_t alg,uint32_t keyType,const struct ParamsValues * params,const struct HksBlob * key)682 static int32_t CheckKeyLen(uint32_t alg, uint32_t keyType, const struct ParamsValues *params,
683 const struct HksBlob *key)
684 {
685 switch (alg) {
686 case HKS_ALG_RSA:
687 return CheckRsaKeyLen(alg, keyType, params, key);
688 case HKS_ALG_ECC:
689 case HKS_ALG_SM2:
690 return CheckEccKeyLen(alg, keyType, params, key);
691 case HKS_ALG_DSA:
692 return CheckDsaKeyLen(alg, keyType, params, key);
693 case HKS_ALG_X25519:
694 case HKS_ALG_ED25519:
695 return CheckCurve25519KeyLen(alg, keyType, params, key);
696 case HKS_ALG_DH:
697 return CheckDHKeyLen(alg, keyType, params, key);
698 default:
699 return HKS_ERROR_INVALID_ALGORITHM;
700 }
701 }
702
CheckMutableParams(uint32_t alg,uint32_t keyType,const struct ParamsValues * params)703 static int32_t CheckMutableParams(uint32_t alg, uint32_t keyType, const struct ParamsValues *params)
704 {
705 if (keyType == HKS_KEY_TYPE_KEY_PAIR) {
706 return HKS_SUCCESS;
707 }
708
709 switch (alg) {
710 case HKS_ALG_RSA:
711 if ((params->purpose.value != HKS_KEY_PURPOSE_SIGN) &&
712 (params->purpose.value != HKS_KEY_PURPOSE_DECRYPT)) {
713 HKS_LOG_E("Import rsa private key check purpose failed.");
714 return HKS_ERROR_INVALID_PURPOSE;
715 }
716 return HKS_SUCCESS;
717 case HKS_ALG_ECC:
718 if ((params->purpose.value != HKS_KEY_PURPOSE_SIGN) && (params->purpose.value != HKS_KEY_PURPOSE_AGREE) &&
719 (params->purpose.value != HKS_KEY_PURPOSE_UNWRAP)) {
720 HKS_LOG_E("Import ecc private key check purpose failed.");
721 return HKS_ERROR_INVALID_PURPOSE;
722 }
723 return HKS_SUCCESS;
724 case HKS_ALG_SM2:
725 case HKS_ALG_DSA:
726 case HKS_ALG_ED25519:
727 if (params->purpose.value != HKS_KEY_PURPOSE_SIGN) {
728 HKS_LOG_E("Import sm2 or dsa or ed25519 private key check purpose failed.");
729 return HKS_ERROR_INVALID_PURPOSE;
730 }
731 return HKS_SUCCESS;
732 case HKS_ALG_X25519:
733 case HKS_ALG_DH:
734 return HKS_SUCCESS;
735 default:
736 return HKS_ERROR_INVALID_ALGORITHM;
737 }
738 }
739
CheckImportKey(uint32_t alg,uint32_t keyType,const struct ParamsValues * params,const struct HksBlob * key)740 static int32_t CheckImportKey(uint32_t alg, uint32_t keyType, const struct ParamsValues *params,
741 const struct HksBlob *key)
742 {
743 int32_t ret = CheckKeyLen(alg, keyType, params, key);
744 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check key len failed, ret = %" LOG_PUBLIC "d", ret)
745
746 ret = CheckMutableParams(alg, keyType, params);
747 HKS_IF_NOT_SUCC_LOGE(ret, "check mutable params faile, ret = %" LOG_PUBLIC "d", ret)
748 return ret;
749 }
750
CheckImportSymmetricKeySize(const struct ParamsValues * params,const struct HksBlob * key)751 static int32_t CheckImportSymmetricKeySize(const struct ParamsValues *params, const struct HksBlob *key)
752 {
753 if (key->size != HKS_KEY_BYTES(params->keyLen.value)) {
754 return HKS_ERROR_INVALID_KEY_INFO;
755 }
756 return HKS_SUCCESS;
757 }
758
HksCoreCheckImportKeyParams(const struct HksBlob * keyAlias,const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * keyOut)759 int32_t HksCoreCheckImportKeyParams(const struct HksBlob *keyAlias, const struct HksBlob *key,
760 const struct HksParamSet *paramSet, const struct HksBlob *keyOut)
761 {
762 (void)keyAlias;
763 (void)keyOut;
764 /* import key paramset is subset of generate key paramset */
765 struct ParamsValues params;
766 (void)memset_s(¶ms, sizeof(params), 0, sizeof(params));
767 int32_t ret = CoreCheckGenKeyParams(paramSet, ¶ms, HKS_KEY_FLAG_IMPORT_KEY);
768 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "CheckImportKeyParams failed")
769
770 uint32_t alg;
771 ret = CheckAndGetAlgorithm(paramSet, g_importKeyAlg, HKS_ARRAY_SIZE(g_importKeyAlg), &alg);
772 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "import key check and get alg failed")
773
774 if ((alg == HKS_ALG_AES) || (alg == HKS_ALG_SM3) || (alg == HKS_ALG_SM4) || (alg == HKS_ALG_HMAC)) {
775 return CheckImportSymmetricKeySize(¶ms, key);
776 }
777
778 struct HksParam *importKeyTypeParam = NULL;
779 ret = HksGetParam(paramSet, HKS_TAG_IMPORT_KEY_TYPE, &importKeyTypeParam);
780 bool needCheckLater = true;
781 if (ret == HKS_SUCCESS && importKeyTypeParam->uint32Param != HKS_KEY_TYPE_PUBLIC_KEY) {
782 needCheckLater = false;
783 ret = CheckImportKey(alg, importKeyTypeParam->uint32Param, ¶ms, key);
784 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check import key invalid")
785 }
786 #ifdef L2_STANDARD
787 if (ret == HKS_SUCCESS) {
788 if (importKeyTypeParam->uint32Param != HKS_KEY_TYPE_PRIVATE_KEY) {
789 if (alg == HKS_ALG_DH) {
790 ret = HksOpensslCheckDhKey(key, (enum HksImportKeyType)importKeyTypeParam->uint32Param);
791 } else if (alg == HKS_ALG_RSA) {
792 ret = HksOpensslCheckRsaKey(key);
793 }
794 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "openssl check import key invalid")
795 }
796 }
797 #endif
798 if (!needCheckLater) {
799 return ret;
800 }
801
802 /* check public key params: 1. check keySize */
803 ret = CheckImportKeySize(alg, ¶ms, key);
804 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "import key check key size invalid")
805
806 /* check public key params: 2. check mutable params */
807 return CheckImportMutableParams(alg, ¶ms);
808 }
809
HksCoreCheckImportWrappedKeyParams(const struct HksBlob * key,const struct HksBlob * wrappedKeyData,const struct HksParamSet * paramSet,struct HksBlob * keyOut,uint32_t * outUnwrapSuite)810 int32_t HksCoreCheckImportWrappedKeyParams(const struct HksBlob *key, const struct HksBlob *wrappedKeyData,
811 const struct HksParamSet *paramSet, struct HksBlob *keyOut, uint32_t *outUnwrapSuite)
812 {
813 (void)keyOut;
814
815 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(key), HKS_ERROR_INVALID_ARGUMENT, "wrapping key is invalid")
816
817 /* first check wrapping-related params and wrapped key data */
818 uint32_t unwrapSuite = 0;
819 int32_t ret = CheckAndGetWrappedKeyUnwrapAlgSuite(paramSet, &unwrapSuite);
820 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check import wrapped params set failed")
821
822 ret = HksCheckWrappedDataFormatValidity(wrappedKeyData, HKS_IMPORT_WRAPPED_KEY_TOTAL_BLOBS, NULL);
823 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check import wrapped key data format failed")
824
825 /* then check the origin key paramset which is the same as import key */
826 struct ParamsValues params;
827 (void)memset_s(¶ms, sizeof(params), 0, sizeof(params));
828 ret = CoreCheckGenKeyParams(paramSet, ¶ms, HKS_KEY_FLAG_IMPORT_KEY);
829 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check origin key param set failed")
830
831 uint32_t alg;
832 ret = CheckAndGetAlgorithm(paramSet, g_importKeyAlg, HKS_ARRAY_SIZE(g_importKeyAlg), &alg);
833 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "CheckImportKeyParams get alg failed")
834 *outUnwrapSuite = unwrapSuite;
835 return HKS_SUCCESS;
836 }
837
HksCoreCheckSignVerifyParams(uint32_t cmdId,const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * srcData,const struct HksBlob * signature)838 int32_t HksCoreCheckSignVerifyParams(uint32_t cmdId, const struct HksBlob *key, const struct HksParamSet *paramSet,
839 const struct HksBlob *srcData, const struct HksBlob *signature)
840 {
841 #ifdef HKS_SUPPORT_API_SIGN_VERIFY
842 (void)srcData;
843 uint32_t alg;
844 int32_t ret = CheckAndGetAlgorithm(paramSet, g_signAlg, HKS_ARRAY_SIZE(g_signAlg), &alg);
845 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check and get alg failed")
846
847 struct ParamsValues params;
848 (void)memset_s(¶ms, sizeof(params), 0, sizeof(params));
849
850 ret = HksGetInputParmasByAlg(alg, HKS_CHECK_TYPE_USE_KEY, paramSet, ¶ms);
851 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "sign or verify get input params failed, ret = %" LOG_PUBLIC "d", ret)
852
853 ret = CheckSignVerifyParamsByAlg(cmdId, alg, ¶ms);
854 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "sign or verify check params failed, ret = %" LOG_PUBLIC "d", ret)
855
856 uint32_t keySize = 0;
857 ret = HksGetKeySize(alg, key, &keySize);
858 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get keySize failed!")
859
860 ret = HksCheckSignature(cmdId, alg, keySize, signature);
861 HKS_IF_NOT_SUCC_LOGE(ret, "check signature failed, ret = %" LOG_PUBLIC "d", ret)
862
863 return ret;
864 #else
865 (void)cmdId;
866 (void)key;
867 (void)paramSet;
868 (void)srcData;
869 (void)signature;
870 return HKS_ERROR_NOT_SUPPORTED;
871 #endif
872 }
873
HksLocalCheckSignVerifyParams(uint32_t cmdId,uint32_t keySize,const struct HksParamSet * paramSet,const struct HksBlob * srcData,const struct HksBlob * signature)874 int32_t HksLocalCheckSignVerifyParams(uint32_t cmdId, uint32_t keySize, const struct HksParamSet *paramSet,
875 const struct HksBlob *srcData, const struct HksBlob *signature)
876 {
877 #ifdef HKS_SUPPORT_API_SIGN_VERIFY
878 (void)srcData;
879 uint32_t alg;
880 int32_t ret = CheckAndGetAlgorithm(paramSet, g_signAlg, HKS_ARRAY_SIZE(g_signAlg), &alg);
881 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "local check and get alg failed")
882
883 struct ParamsValues params;
884 (void)memset_s(¶ms, sizeof(params), 0, sizeof(params));
885
886 ret = HksGetInputParmasByAlg(alg, HKS_CHECK_TYPE_USE_KEY, paramSet, ¶ms);
887 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "sign or verify get input params failed, ret = %" LOG_PUBLIC "d", ret)
888
889 ret = CheckSignVerifyParamsByAlg(cmdId, alg, ¶ms);
890 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "sign or verify local check params failed, ret = %" LOG_PUBLIC "d", ret)
891
892 ret = HksCheckSignature(cmdId, alg, keySize, signature);
893 HKS_IF_NOT_SUCC_LOGE(ret, "check signature failed, ret = %" LOG_PUBLIC "d", ret)
894
895 return ret;
896 #else
897 (void)cmdId;
898 (void)keySize;
899 (void)paramSet;
900 (void)srcData;
901 (void)signature;
902 return HKS_ERROR_NOT_SUPPORTED;
903 #endif
904 }
905
HksCoreCheckAgreeKeyParams(const struct HksParamSet * paramSet,const struct HksBlob * privateKey,const struct HksBlob * peerPublicKey,const struct HksBlob * agreedKey,bool isLocalCheck)906 int32_t HksCoreCheckAgreeKeyParams(const struct HksParamSet *paramSet, const struct HksBlob *privateKey,
907 const struct HksBlob *peerPublicKey, const struct HksBlob *agreedKey, bool isLocalCheck)
908 {
909 uint32_t alg;
910 int32_t ret;
911
912 if (isLocalCheck) {
913 ret = CheckAndGetAlgorithm(paramSet, g_agreeAlgLocal, HKS_ARRAY_SIZE(g_agreeAlgLocal), &alg);
914 } else {
915 ret = CheckAndGetAlgorithm(paramSet, g_agreeAlg, HKS_ARRAY_SIZE(g_agreeAlg), &alg);
916 }
917 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check alg failed")
918
919 uint32_t keySize = 0;
920 if (isLocalCheck) {
921 if (alg == HKS_ALG_ED25519) {
922 if ((privateKey->size != HKS_KEY_BYTES(HKS_CURVE25519_KEY_SIZE_256)) ||
923 (peerPublicKey->size != HKS_KEY_BYTES(HKS_CURVE25519_KEY_SIZE_256))) {
924 return HKS_ERROR_INVALID_KEY_SIZE;
925 }
926 }
927
928 if (alg == HKS_ALG_DH || alg == HKS_ALG_ECC || alg == HKS_ALG_ECDH) {
929 if (privateKey->size < sizeof(struct HksKeyMaterialHeader)) {
930 return HKS_ERROR_INVALID_ARGUMENT;
931 }
932 keySize = ((struct HksKeyMaterialHeader *)privateKey->data)->keySize;
933 } else if (alg == HKS_ALG_ED25519) {
934 keySize = privateKey->size * HKS_BITS_PER_BYTE;
935 }
936 } else {
937 ret = HksGetKeySize(alg, privateKey, &keySize);
938 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get key size failed")
939 }
940
941 uint32_t size = keySize / HKS_BITS_PER_BYTE + keySize % HKS_BITS_PER_BYTE;
942 if (agreedKey->size < size) {
943 HKS_LOG_E("agreeKey buffer too small, size %" LOG_PUBLIC "u", agreedKey->size);
944 return HKS_ERROR_BUFFER_TOO_SMALL;
945 }
946
947 return HKS_SUCCESS;
948 }
949
HksCoreCheckCipherParams(uint32_t cmdId,const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * inData,const struct HksBlob * outData)950 int32_t HksCoreCheckCipherParams(uint32_t cmdId, const struct HksBlob *key, const struct HksParamSet *paramSet,
951 const struct HksBlob *inData, const struct HksBlob *outData)
952 {
953 uint32_t alg;
954 int32_t ret = CheckAndGetAlgorithm(paramSet, g_cipherAlg, HKS_ARRAY_SIZE(g_cipherAlg), &alg);
955 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check alg failed")
956
957 struct ParamsValues params;
958 (void)memset_s(¶ms, sizeof(params), 0, sizeof(params));
959
960 ret = HksGetInputParmasByAlg(alg, HKS_CHECK_TYPE_USE_KEY, paramSet, ¶ms);
961 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "cipher get input params failed, ret = %" LOG_PUBLIC "d", ret)
962
963 if ((alg == HKS_ALG_RSA) || (alg == HKS_ALG_SM4) || (alg == HKS_ALG_SM2)) {
964 ret = HksGetKeySize(alg, key, ¶ms.keyLen.value);
965 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "rsa/SM4/SM2 cipher get key size failed")
966 }
967
968 ret = CheckCipherParamsByAlg(cmdId, alg, paramSet, ¶ms);
969 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "cipher check params failed, ret = %" LOG_PUBLIC "d", ret)
970
971 ret = HksCheckCipherData(cmdId, alg, ¶ms, inData, outData);
972 HKS_IF_NOT_SUCC_LOGE(ret, "cipher check input or output data failed, ret = %" LOG_PUBLIC "d", ret)
973
974 return ret;
975 }
976 #endif /* _CUT_AUTHENTICATE_ */
977
HksLocalCheckCipherParams(uint32_t cmdId,uint32_t keySize,const struct HksParamSet * paramSet,const struct HksBlob * inData,const struct HksBlob * outData)978 int32_t HksLocalCheckCipherParams(uint32_t cmdId, uint32_t keySize, const struct HksParamSet *paramSet,
979 const struct HksBlob *inData, const struct HksBlob *outData)
980 {
981 uint32_t alg;
982 int32_t ret = CheckAndGetAlgorithm(paramSet, g_cipherAlgLocal, HKS_ARRAY_SIZE(g_cipherAlgLocal), &alg);
983 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check alg failed")
984
985 if (alg == HKS_ALG_AES) {
986 #ifdef HKS_SUPPORT_AES_C
987 ret = HksCheckValue(keySize, g_aesKeySizeLocal, HKS_ARRAY_SIZE(g_aesKeySizeLocal));
988 #else
989 ret = HKS_ERROR_NOT_SUPPORTED;
990 #endif
991 } else if (alg == HKS_ALG_RSA) {
992 #ifdef HKS_SUPPORT_RSA_C
993 ret = HksCheckValue(keySize, g_rsaKeySizeLocal, HKS_ARRAY_SIZE(g_rsaKeySizeLocal));
994 #else
995 ret = HKS_ERROR_NOT_SUPPORTED;
996 #endif
997 }
998 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_KEY_SIZE,
999 "keySize value %" LOG_PUBLIC "u not expected", keySize)
1000
1001 struct ParamsValues params;
1002 (void)memset_s(¶ms, sizeof(params), 0, sizeof(params));
1003
1004 ret = HksGetInputParmasByAlg(alg, HKS_CHECK_TYPE_USE_KEY, paramSet, ¶ms);
1005 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "local cipher get input params failed, ret = %" LOG_PUBLIC "d", ret)
1006
1007 ret = CheckCipherParamsByAlg(cmdId, alg, paramSet, ¶ms);
1008 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "local cipher check params failed, ret = %" LOG_PUBLIC "d", ret)
1009
1010 ret = HksCheckCipherData(cmdId, alg, ¶ms, inData, outData);
1011 HKS_IF_NOT_SUCC_LOGE(ret, "local cipher check input or output data failed, ret = %" LOG_PUBLIC "d", ret)
1012
1013 return ret;
1014 }
1015
HksCoreCheckDeriveKeyParams(const struct HksParamSet * paramSet,const struct HksBlob * mainKey,const struct HksBlob * derivedKey,bool isLocalCheck)1016 int32_t HksCoreCheckDeriveKeyParams(const struct HksParamSet *paramSet, const struct HksBlob *mainKey,
1017 const struct HksBlob *derivedKey, bool isLocalCheck)
1018 {
1019 (void)mainKey;
1020 (void)derivedKey;
1021 uint32_t alg;
1022 int32_t ret;
1023 if (isLocalCheck) {
1024 ret = CheckAndGetAlgorithm(paramSet, g_deriveAlgLocal, HKS_ARRAY_SIZE(g_deriveAlgLocal), &alg);
1025 } else {
1026 ret = CheckAndGetAlgorithm(paramSet, g_deriveAlg, HKS_ARRAY_SIZE(g_deriveAlg), &alg);
1027 }
1028 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check alg failed")
1029
1030 struct HksParam *purposeParam = NULL;
1031 ret = HksGetParam(paramSet, HKS_TAG_PURPOSE, &purposeParam);
1032 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_PURPOSE_FAIL,
1033 "get param get 0x%" LOG_PUBLIC "x failed", HKS_TAG_PURPOSE)
1034
1035 if (purposeParam->uint32Param != HKS_KEY_PURPOSE_DERIVE) {
1036 return HKS_ERROR_INVALID_PURPOSE;
1037 }
1038
1039 /* according to RFC5869, HKDF no need check salt and info */
1040 uint32_t digest;
1041 ret = CheckAndGetDigest(paramSet, g_digest, HKS_ARRAY_SIZE(g_digest), &digest);
1042 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check digest failed")
1043
1044 #ifdef HKS_SUPPORT_KDF_PBKDF2
1045 if (alg == HKS_ALG_PBKDF2) {
1046 return CheckPbkdf2DeriveKeyParams(paramSet);
1047 }
1048 #endif
1049
1050 return HKS_SUCCESS;
1051 }
1052
CheckMacPurpose(const struct HksParamSet * paramSet)1053 static int32_t CheckMacPurpose(const struct HksParamSet *paramSet)
1054 {
1055 struct HksParam *purposeParam = NULL;
1056 int32_t ret = HksGetParam(paramSet, HKS_TAG_PURPOSE, &purposeParam);
1057 HKS_IF_NOT_SUCC_RETURN(ret, HKS_ERROR_CHECK_GET_PURPOSE_FAIL)
1058
1059 if (purposeParam->uint32Param != HKS_KEY_PURPOSE_MAC) {
1060 return HKS_ERROR_INVALID_PURPOSE;
1061 }
1062
1063 return HKS_SUCCESS;
1064 }
1065
CheckMacOutput(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * mac,bool isLocalCheck)1066 static int32_t CheckMacOutput(
1067 const struct HksBlob *key, const struct HksParamSet *paramSet, const struct HksBlob *mac, bool isLocalCheck)
1068 {
1069 uint32_t digest;
1070 int32_t ret;
1071 if (isLocalCheck) {
1072 ret = CheckAndGetDigest(paramSet, g_digest, HKS_ARRAY_SIZE(g_digest), &digest);
1073 } else {
1074 ret = CheckAndGetDigest(paramSet, g_macDigest, HKS_ARRAY_SIZE(g_macDigest), &digest);
1075 }
1076 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check digest failed")
1077
1078 uint32_t digestLen;
1079 ret = HksGetDigestLen(digest, &digestLen);
1080 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get digest length failed, ret = %" LOG_PUBLIC "d", ret)
1081
1082 if (mac->size < digestLen) {
1083 HKS_LOG_E("mac buffer too small, size %" LOG_PUBLIC "u", mac->size);
1084 return HKS_ERROR_BUFFER_TOO_SMALL;
1085 }
1086
1087 if ((isLocalCheck) && (key->size < digestLen)) { /* the unit of local engine input key size is byte */
1088 HKS_LOG_E("key size too small, size = %" LOG_PUBLIC "u", key->size);
1089 return HKS_ERROR_INVALID_KEY_SIZE;
1090 }
1091
1092 return HKS_SUCCESS;
1093 }
1094
HksCoreCheckMacParams(const struct HksBlob * key,const struct HksParamSet * paramSet,const struct HksBlob * srcData,const struct HksBlob * mac,bool isLocalCheck)1095 int32_t HksCoreCheckMacParams(const struct HksBlob *key, const struct HksParamSet *paramSet,
1096 const struct HksBlob *srcData, const struct HksBlob *mac, bool isLocalCheck)
1097 {
1098 (void)srcData;
1099 int32_t ret = CheckMacPurpose(paramSet);
1100 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check Mac purpose failed")
1101
1102 return CheckMacOutput(key, paramSet, mac, isLocalCheck);
1103 }
1104
CheckIsSymmetricAlgorithm(uint32_t alg)1105 static bool CheckIsSymmetricAlgorithm(uint32_t alg)
1106 {
1107 for (uint32_t i = 0; i < HKS_ARRAY_SIZE(g_symmetricAlgorithm); ++i) {
1108 if (alg == g_symmetricAlgorithm[i]) {
1109 return true;
1110 }
1111 }
1112 return false;
1113 }
1114
HksCoreCheckAgreeDeriveFinishParams(const struct HksBlob * key,const struct HksParamSet * paramSet)1115 int32_t HksCoreCheckAgreeDeriveFinishParams(const struct HksBlob *key, const struct HksParamSet *paramSet)
1116 {
1117 // check the key paramset is consistent with key real attributes, including key size and valid key algorithm
1118 struct HksParam *keySize = NULL;
1119 int32_t ret = HksGetParam(paramSet, HKS_TAG_KEY_SIZE, &keySize);
1120 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get key size from agree paramset failed!")
1121 if (HKS_KEY_BYTES(keySize->uint32Param) != key->size) {
1122 HKS_LOG_E("key size param from paramSet is not consistent with real key size, param size %" LOG_PUBLIC
1123 "u not equals to real key size %" LOG_PUBLIC "u", HKS_KEY_BYTES(keySize->uint32Param), key->size);
1124 return HKS_ERROR_INVALID_ARGUMENT;
1125 }
1126 struct HksParam *algorithm = NULL;
1127 ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algorithm);
1128 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get key algorithm from agree paramset failed!")
1129 if (!CheckIsSymmetricAlgorithm(algorithm->uint32Param)) {
1130 HKS_LOG_E("Agreed or derived key algorithm param can only be symmetric! Algorithm is %" LOG_PUBLIC "u",
1131 algorithm->uint32Param);
1132 return HKS_ERROR_INVALID_ARGUMENT;
1133 }
1134
1135 return HKS_SUCCESS;
1136 }
1137