• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #define HUKS_DISABLE_LOG_AT_FILE_TO_REDUCE_ROM_SIZE
16 
17 #ifdef HKS_CONFIG_FILE
18 #include HKS_CONFIG_FILE
19 #else
20 #include "hks_config.h"
21 #endif
22 
23 #include "hks_base_check.h"
24 #include "hks_cmd_id.h"
25 #include "hks_common_check.h"
26 #include "hks_log.h"
27 #include "hks_template.h"
28 #include "hks_base_check_policy.c"
29 
30 #include "securec.h"
31 
32 #ifndef _CUT_AUTHENTICATE_
33 #ifndef _STORAGE_LITE_
CheckAndGetKeySize(const struct HksBlob * key,const uint32_t * expectKeySize,uint32_t expectCnt,uint32_t * keySize)34 static int32_t CheckAndGetKeySize(const struct HksBlob *key, const uint32_t *expectKeySize,
35     uint32_t expectCnt, uint32_t *keySize)
36 {
37     if (key->size < sizeof(struct HksParamSet)) {
38         HKS_LOG_E("check key size: invalid keyfile size: %" LOG_PUBLIC "u", key->size);
39         return HKS_ERROR_INVALID_KEY_FILE;
40     }
41 
42     struct HksParamSet *keyParamSet = (struct HksParamSet *)key->data;
43     int32_t ret = HksCheckParamSetValidity(keyParamSet);
44     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_KEY_FILE, "check key size: paramset invalid failed")
45 
46     struct HksParam *keySizeParam = NULL;
47     ret = HksGetParam(keyParamSet, HKS_TAG_KEY_SIZE, &keySizeParam);
48     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_KEY_FILE,
49         "check key size: get param get tag:0x%" LOG_PUBLIC "x failed", HKS_TAG_KEY_SIZE)
50 
51     ret = HksCheckValue(keySizeParam->uint32Param, expectKeySize, expectCnt);
52     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_KEY_FILE,
53         "check key size: key size value %" LOG_PUBLIC "u not expected", keySizeParam->uint32Param)
54     *keySize = keySizeParam->uint32Param;
55     return ret;
56 }
57 #else
CheckAndGetKeySize(const struct HksBlob * key,const uint32_t * expectKeySize,uint32_t expectCnt,uint32_t * keySize)58 static int32_t CheckAndGetKeySize(const struct HksBlob *key, const uint32_t *expectKeySize,
59     uint32_t expectCnt, uint32_t *keySize)
60 {
61     if (key->size < sizeof(struct HksStoreKeyInfo)) {
62         HKS_LOG_E("check key size: invalid keyfile size: %" LOG_PUBLIC "u", key->size);
63         return HKS_ERROR_INVALID_KEY_FILE;
64     }
65 
66     struct HksStoreKeyInfo *keyInfo = (struct HksStoreKeyInfo *)key->data;
67     uint32_t keyLen = keyInfo->keyLen;
68     int32_t ret = HksCheckValue(keyLen, expectKeySize, expectCnt);
69     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_KEY_FILE,
70         "check key size: keySize value %" LOG_PUBLIC "u not expected", keyLen)
71     *keySize = keyLen;
72     return ret;
73 }
74 #endif
75 
76 #ifdef HKS_SUPPORT_RSA_C
77 #ifdef HKS_SUPPORT_RSA_C_FLEX_KEYSIZE
CheckRsaKeySize(uint32_t keyLen)78 int32_t CheckRsaKeySize(uint32_t keyLen)
79 {
80     if ((keyLen >= HKS_RSA_KEY_SIZE_1024) &&
81         (keyLen <= HKS_RSA_KEY_SIZE_2048) &&
82         ((keyLen % HKS_RSA_KEY_BLOCK_SIZE) == 0)) {
83         return HKS_SUCCESS;
84     } else {
85         return HKS_ERROR_INVALID_KEY_FILE;
86     }
87 }
88 
CheckAndGetRsaKeySize(const struct HksBlob * key,uint32_t * keySize)89 static int32_t CheckAndGetRsaKeySize(const struct HksBlob *key, uint32_t *keySize)
90 {
91     if (key->size < sizeof(struct HksParamSet)) {
92         HKS_LOG_E("check key size: invalid keyfile size: %" LOG_PUBLIC "u", key->size);
93         return HKS_ERROR_INVALID_KEY_FILE;
94     }
95 
96     struct HksParamSet *keyParamSet = (struct HksParamSet *)key->data;
97     int32_t ret = HksCheckParamSetValidity(keyParamSet);
98     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_KEY_FILE, "check key size: paramset invalid failed")
99 
100     struct HksParam *keySizeParam = NULL;
101     ret = HksGetParam(keyParamSet, HKS_TAG_KEY_SIZE, &keySizeParam);
102     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_KEY_FILE,
103         "check key size: get param get tag:0x%" LOG_PUBLIC "x failed", HKS_TAG_KEY_SIZE)
104     ret = CheckRsaKeySize(keySizeParam->uint32Param);
105     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_KEY_FILE,
106         "check key size: key size value %" LOG_PUBLIC "u not expected", keySizeParam->uint32Param)
107     *keySize = keySizeParam->uint32Param;
108 
109     return ret;
110 }
111 #endif
112 #endif
113 
CheckPurposeUnique(uint32_t inputPurpose)114 static int32_t CheckPurposeUnique(uint32_t inputPurpose)
115 {
116     /* key usage uniqueness */
117     uint32_t purposeCipher = inputPurpose & (HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT);
118     uint32_t purposeSign = inputPurpose & (HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY);
119     uint32_t purposeDerive = inputPurpose & HKS_KEY_PURPOSE_DERIVE;
120     uint32_t purposeWrap = inputPurpose & (HKS_KEY_PURPOSE_WRAP | HKS_KEY_PURPOSE_UNWRAP);
121     uint32_t purposeMac = inputPurpose & HKS_KEY_PURPOSE_MAC;
122     uint32_t purposeAgree = inputPurpose & HKS_KEY_PURPOSE_AGREE;
123 
124     uint32_t purposeCount = (purposeCipher != 0) ? 1 : 0;
125     purposeCount += (purposeSign != 0) ? 1 : 0;
126     purposeCount += (purposeDerive != 0) ? 1 : 0;
127     purposeCount += (purposeWrap != 0) ? 1 : 0;
128     purposeCount += (purposeMac != 0) ? 1 : 0;
129     purposeCount += (purposeAgree != 0) ? 1 : 0;
130 
131     return (purposeCount == 1) ? HKS_SUCCESS : HKS_ERROR_INVALID_PURPOSE;
132 }
133 
GetInvalidPurpose(uint32_t alg,uint32_t * inputPurpose,uint32_t keyFlag)134 static int32_t GetInvalidPurpose(uint32_t alg, uint32_t *inputPurpose, uint32_t keyFlag)
135 {
136     int32_t result = HKS_ERROR_INVALID_ALGORITHM;
137     if (sizeof(g_invalidPurpose) == 0) {
138         return result;
139     }
140     uint32_t count = sizeof(g_invalidPurpose) / sizeof(g_invalidPurpose[0]);
141     for (uint32_t i = 0; i < count; i++) {
142         if (alg == g_invalidPurpose[i][0]) {
143             result = HKS_SUCCESS;
144             *inputPurpose = g_invalidPurpose[i][1];
145             break;
146         }
147     }
148     if ((keyFlag != HKS_KEY_FLAG_IMPORT_KEY) || (sizeof(g_invalidImportKeyPurpose) == 0)) {
149         return result;
150     }
151     // add invalid purpose for import key additionally
152     count = sizeof(g_invalidImportKeyPurpose) / sizeof(g_invalidImportKeyPurpose[0]);
153     for (uint32_t i = 0; i < count; i++) {
154         if (alg == g_invalidImportKeyPurpose[i][0]) {
155             *inputPurpose |= g_invalidImportKeyPurpose[i][1];
156             break;
157         }
158     }
159     return result;
160 }
161 
CheckPurposeValid(uint32_t alg,uint32_t inputPurpose,uint32_t keyFlag)162 static int32_t CheckPurposeValid(uint32_t alg, uint32_t inputPurpose, uint32_t keyFlag)
163 {
164     uint32_t invalidPurpose = 0;
165 
166     int32_t result = GetInvalidPurpose(alg, &invalidPurpose, keyFlag);
167     HKS_IF_NOT_SUCC_RETURN(result, result)
168 
169     if ((inputPurpose & invalidPurpose) != 0) {
170         return HKS_ERROR_INVALID_PURPOSE;
171     }
172 
173     return HKS_SUCCESS;
174 }
175 #endif /* _CUT_AUTHENTICATE_ */
176 
177 // If tag is optional param, when tag is empty, it is supported.
GetOptionalParams(const struct HksParamSet * paramSet,uint32_t tag,bool needCheck,uint32_t * value,bool * isAbsent)178 static int32_t GetOptionalParams(const struct HksParamSet *paramSet, uint32_t tag, bool needCheck, uint32_t* value,
179     bool* isAbsent)
180 {
181     if (needCheck) {
182         struct HksParam *param;
183         int32_t ret = HksGetParam(paramSet, tag, &param);
184         if (ret == HKS_SUCCESS) {
185             *value = param->uint32Param;
186             return ret;
187         }
188         if (ret == HKS_ERROR_PARAM_NOT_EXIST) {
189             HKS_LOG_D("tag [%" LOG_PUBLIC "u] is empty, but it is supported!", tag);
190             *isAbsent = true;
191             return HKS_SUCCESS;
192         }
193         return HKS_ERROR_INVALID_ARGUMENT;
194     }
195     return HKS_SUCCESS;
196 }
197 
GetInputParams(const struct HksParamSet * paramSet,struct ParamsValues * inputParams)198 int32_t GetInputParams(const struct HksParamSet *paramSet, struct ParamsValues *inputParams)
199 {
200     int32_t ret = HKS_SUCCESS;
201     struct HksParam *checkParam = NULL;
202     if (inputParams->keyLen.needCheck) {
203         ret = HksGetParam(paramSet, HKS_TAG_KEY_SIZE, &checkParam);
204         HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_KEY_SIZE_FAIL,
205             "get Param get tag:0x%" LOG_PUBLIC "x failed", HKS_TAG_KEY_SIZE);
206         inputParams->keyLen.value = checkParam->uint32Param;
207     }
208 
209     if (inputParams->purpose.needCheck) {
210         ret = HksGetParam(paramSet, HKS_TAG_PURPOSE, &checkParam);
211         HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_PURPOSE_FAIL,
212             "get Param get tag:0x%" LOG_PUBLIC "x failed", HKS_TAG_PURPOSE);
213         inputParams->purpose.value = checkParam->uint32Param;
214     }
215 
216     ret = GetOptionalParams(paramSet, HKS_TAG_PADDING, inputParams->padding.needCheck, &inputParams->padding.value,
217         &inputParams->padding.isAbsent);
218     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_PADDING_FAIL,
219         "get Param get tag:0x%" LOG_PUBLIC "x failed", HKS_TAG_PADDING);
220     ret = GetOptionalParams(paramSet, HKS_TAG_DIGEST, inputParams->digest.needCheck, &inputParams->digest.value,
221         &inputParams->digest.isAbsent);
222     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_DIGEST_FAIL,
223         "get Param get tag:0x%" LOG_PUBLIC "x failed", HKS_TAG_DIGEST);
224     ret = GetOptionalParams(paramSet, HKS_TAG_BLOCK_MODE, inputParams->mode.needCheck, &inputParams->mode.value,
225         &inputParams->mode.isAbsent);
226     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_MODE_FAIL,
227         "get Param get tag:0x%" LOG_PUBLIC "x failed", HKS_TAG_BLOCK_MODE);
228     return ret;
229 }
230 
InitInputParams(enum CheckKeyType checkType,struct ParamsValues * inputParams,const struct ParamsValuesChecker * checkSet,uint32_t checkSetSize)231 static int32_t InitInputParams(enum CheckKeyType checkType, struct ParamsValues *inputParams,
232     const struct ParamsValuesChecker *checkSet, uint32_t checkSetSize)
233 {
234     for (uint32_t i = 0; i < checkSetSize; ++i) {
235         if (checkType == checkSet[i].checkType) {
236             (void)memcpy_s(inputParams, sizeof(*inputParams), &checkSet[i].paramValues,
237                 sizeof(checkSet[i].paramValues));
238             return HKS_SUCCESS;
239         }
240     }
241     return HKS_ERROR_NOT_SUPPORTED;
242 }
243 
InitInputParamsByAlg(uint32_t alg,enum CheckKeyType checkType,struct ParamsValues * inputParams)244 int32_t InitInputParamsByAlg(uint32_t alg, enum CheckKeyType checkType, struct ParamsValues *inputParams)
245 {
246     for (uint32_t i = 0; i < HKS_ARRAY_SIZE(g_hksAlgParamSetHandlerPart1); i++) {
247         if (alg == g_hksAlgParamSetHandlerPart1[i].alg) {
248             return InitInputParams(checkType, inputParams, g_hksAlgParamSetHandlerPart1[i].algParamSet,
249                 g_hksAlgParamSetHandlerPart1[i].algParamSetCnt);
250         }
251     }
252 
253     for (uint32_t i = 0; i < HKS_ARRAY_SIZE(g_hksAlgParamSetHandlerPart2); i++) {
254         if (alg == g_hksAlgParamSetHandlerPart2[i].alg) {
255             return InitInputParams(checkType, inputParams, g_hksAlgParamSetHandlerPart2[i].algParamSet,
256                 g_hksAlgParamSetHandlerPart2[i].algParamSetCnt);
257         }
258     }
259 
260     HKS_LOG_E("init input params by alg fail, alg: %" LOG_PUBLIC "u, checkType: %" LOG_PUBLIC "u", alg, checkType);
261     return HKS_ERROR_INVALID_ALGORITHM;
262 }
263 
InitExpectParams(enum CheckKeyType checkType,struct ExpectParamsValues * expectValues,const struct ExpectParamsValuesChecker * checkSet,uint32_t checkSetSize)264 static int32_t InitExpectParams(enum CheckKeyType checkType, struct ExpectParamsValues *expectValues,
265     const struct ExpectParamsValuesChecker *checkSet, uint32_t checkSetSize)
266 {
267     for (uint32_t i = 0; i < checkSetSize; ++i) {
268         if (checkType == checkSet[i].checkType) {
269             (void)memcpy_s(expectValues, sizeof(*expectValues), &checkSet[i].paramValues,
270                 sizeof(checkSet[i].paramValues));
271             return HKS_SUCCESS;
272         }
273     }
274     return HKS_ERROR_NOT_SUPPORTED;
275 }
276 
GetExpectParams(uint32_t alg,enum CheckKeyType checkType,struct ExpectParamsValues * expectValues)277 int32_t GetExpectParams(uint32_t alg, enum CheckKeyType checkType, struct ExpectParamsValues *expectValues)
278 {
279     for (uint32_t i = 0; i < HKS_ARRAY_SIZE(g_hksAlgParamSetHandlerPart1); i++) {
280         if (alg == g_hksAlgParamSetHandlerPart1[i].alg) {
281             return InitExpectParams(checkType, expectValues, g_hksAlgParamSetHandlerPart1[i].expectParams,
282                 g_hksAlgParamSetHandlerPart1[i].expectParamsCnt);
283         }
284     }
285 
286     for (uint32_t i = 0; i < HKS_ARRAY_SIZE(g_hksAlgParamSetHandlerPart2); i++) {
287         if (alg == g_hksAlgParamSetHandlerPart2[i].alg) {
288             return InitExpectParams(checkType, expectValues, g_hksAlgParamSetHandlerPart2[i].expectParams,
289                 g_hksAlgParamSetHandlerPart2[i].expectParamsCnt);
290         }
291     }
292 
293     HKS_LOG_E("get expect params fail, alg: %" LOG_PUBLIC "u, checkType: %" LOG_PUBLIC "u", alg, checkType);
294     return HKS_ERROR_INVALID_ALGORITHM;
295 }
296 
297 #ifdef HKS_SUPPORT_ECC_C
CheckEccSignature(uint32_t cmdId,uint32_t keySize,const struct HksBlob * signature)298 static int32_t CheckEccSignature(uint32_t cmdId, uint32_t keySize, const struct HksBlob *signature)
299 {
300     /*
301      * ecc sign format: 0x30 + len1 + 0x02 + len2 + 0x00 (optional) + r + 0x02 + len3 + 0x00(optional) + s
302      * sign: signSize no less than 2*keySize/8 + 8;
303      * verify: signSize no greater than 2*keySize/8 + 8
304      */
305     uint32_t eccSignRSize = keySize / HKS_BITS_PER_BYTE + keySize % HKS_BITS_PER_BYTE;
306     uint32_t eccSignSSize = eccSignRSize;
307     switch (cmdId) {
308         case HKS_CMD_ID_SIGN:
309             if (signature->size < (eccSignRSize + eccSignSSize + HKS_ECC_SIGN_MAX_TL_SIZE)) {
310                 HKS_LOG_E("eccsign: signature size too small, keySize %" LOG_PUBLIC "u, signatureSize %" LOG_PUBLIC "u",
311                     keySize, signature->size);
312                 return HKS_ERROR_BUFFER_TOO_SMALL;
313             }
314             break;
315         case HKS_CMD_ID_VERIFY:
316             if (signature->size > (eccSignRSize + eccSignSSize + HKS_ECC_SIGN_MAX_TL_SIZE)) {
317                 HKS_LOG_E("eccverfiy: invalid signature size, keySize %" LOG_PUBLIC "u, signatureSize %" LOG_PUBLIC "u",
318                     keySize, signature->size);
319                 return HKS_ERROR_INVALID_SIGNATURE_SIZE;
320             }
321             break;
322         default:
323             return HKS_ERROR_INVALID_ARGUMENT;
324     }
325 
326     return HKS_SUCCESS;
327 }
328 #endif
329 
330 #ifdef HKS_SUPPORT_ED25519_C
CheckEd25519Signature(uint32_t cmdId,const struct HksBlob * signature)331 static int32_t CheckEd25519Signature(uint32_t cmdId, const struct HksBlob *signature)
332 {
333     switch (cmdId) {
334         case HKS_CMD_ID_SIGN:
335             if (signature->size < HKS_SIGNATURE_MIN_SIZE) {
336                 HKS_LOG_E("ed25519 sign: signature size too small, signatureSize %" LOG_PUBLIC "u", signature->size);
337                 return HKS_ERROR_BUFFER_TOO_SMALL;
338             }
339             break;
340         case HKS_CMD_ID_VERIFY:
341             if (signature->size < HKS_SIGNATURE_MIN_SIZE) {
342                 HKS_LOG_E("ed25519 verfiy: invalid signature size, signatureSize %" LOG_PUBLIC "u", signature->size);
343                 return HKS_ERROR_INVALID_SIGNATURE_SIZE;
344             }
345             break;
346         default:
347             return HKS_ERROR_INVALID_ARGUMENT;
348     }
349 
350     return HKS_SUCCESS;
351 }
352 #endif
353 
354 #ifdef HKS_SUPPORT_RSA_C
CheckRsaGenKeyPadding(const struct ParamsValues * inputParams)355 static int32_t CheckRsaGenKeyPadding(const struct ParamsValues *inputParams)
356 {
357     if (inputParams->padding.isAbsent) {
358         return HKS_SUCCESS;
359     }
360     if ((inputParams->purpose.value & (HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT)) != 0) {
361         return HksCheckValue(inputParams->padding.value, g_rsaCipherPadding, HKS_ARRAY_SIZE(g_rsaCipherPadding));
362     } else if ((inputParams->purpose.value & (HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY)) != 0) {
363         return HksCheckValue(inputParams->padding.value, g_rsaSignPadding, HKS_ARRAY_SIZE(g_rsaSignPadding));
364     }
365     return HKS_SUCCESS;
366 }
367 
368 #ifdef HKS_SUPPORT_RSA_SIGN_VERIFY
CheckRsaSignature(uint32_t cmdId,uint32_t keySize,const struct HksBlob * signature)369 static int32_t CheckRsaSignature(uint32_t cmdId, uint32_t keySize, const struct HksBlob *signature)
370 {
371     /*
372      * k: the length of the RSA modulus n
373      * sign: signSize no less than k; verify: signSize is same as k, thus no greater than keySize / 8
374      */
375     switch (cmdId) {
376         case HKS_CMD_ID_SIGN:
377             if (signature->size < keySize / HKS_BITS_PER_BYTE) {
378                 HKS_LOG_E("rsasign: signature size too small, keySize %" LOG_PUBLIC "u, signatureSize %" LOG_PUBLIC "u",
379                     keySize, signature->size);
380                 return HKS_ERROR_BUFFER_TOO_SMALL;
381             }
382             break;
383         case HKS_CMD_ID_VERIFY:
384             if (signature->size > keySize / HKS_BITS_PER_BYTE) {
385                 HKS_LOG_E("rsaverfiy: invalid signature size, keySize %" LOG_PUBLIC "u, signatureSize %" LOG_PUBLIC "u",
386                     keySize, signature->size);
387                 return HKS_ERROR_INVALID_SIGNATURE_SIZE;
388             }
389             break;
390         default:
391             return HKS_ERROR_INVALID_ARGUMENT;
392     }
393 
394     return HKS_SUCCESS;
395 }
396 #endif
397 
398 #ifdef HKS_SUPPORT_RSA_CRYPT
CheckRsaNoPadCipherData(uint32_t keySize,const struct HksBlob * inData,const struct HksBlob * outData)399 static int32_t CheckRsaNoPadCipherData(uint32_t keySize, const struct HksBlob *inData,
400     const struct HksBlob *outData)
401 {
402     /* encrypt/decrypt: inSize no greater than keySize, outSize no less than keySize */
403     if (inData->size > keySize) {
404         HKS_LOG_E("invalid inData size: %" LOG_PUBLIC "u, keySize: %" LOG_PUBLIC "u", inData->size, keySize);
405         return HKS_ERROR_INVALID_ARGUMENT;
406     }
407 
408     if (outData->size < keySize) {
409         HKS_LOG_E("outData buffer too small size: %" LOG_PUBLIC "u, keySize: %" LOG_PUBLIC "u",
410             outData->size, keySize);
411         return HKS_ERROR_BUFFER_TOO_SMALL;
412     }
413 
414     return HKS_SUCCESS;
415 }
416 
CheckRsaOaepCipherData(uint32_t cmdId,uint32_t keySize,uint32_t digest,const struct HksBlob * inData,const struct HksBlob * outData)417 static int32_t CheckRsaOaepCipherData(uint32_t cmdId, uint32_t keySize, uint32_t digest,
418     const struct HksBlob *inData, const struct HksBlob *outData)
419 {
420     uint32_t digestLen;
421     if (digest == HKS_DIGEST_NONE) {
422         digest = HKS_DIGEST_SHA1;
423     }
424     int32_t ret = HksGetDigestLen(digest, &digestLen);
425     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "GetDigestLen failed, ret = %" LOG_PUBLIC "x", ret)
426 
427     /*
428      * encrypt: inSize no greater than keySize - 2*digestLen - 2, outSize no less than keySize (in: plain; out: cipher)
429      * decrypt: inSize no greater than keySize, outSize no less than keySize - 2*digestLen - 2 (in: cipher; out: plain)
430      */
431     if (keySize <= (HKS_RSA_OAEP_DIGEST_NUM * digestLen + HKS_RSA_OAEP_DIGEST_NUM)) {
432         return HKS_ERROR_INVALID_KEY_FILE;
433     }
434     uint32_t size = keySize - HKS_RSA_OAEP_DIGEST_NUM * digestLen - HKS_RSA_OAEP_DIGEST_NUM;
435     if (cmdId == HKS_CMD_ID_ENCRYPT) {
436         if (inData->size > size) {
437             HKS_LOG_E("encrypt, invalid insize: %" LOG_PUBLIC "u, keySize: %" LOG_PUBLIC "u, "
438                 "digestLen: %" LOG_PUBLIC "u", inData->size, keySize, digestLen);
439             return HKS_ERROR_INVALID_ARGUMENT;
440         }
441         if (outData->size < keySize) {
442             HKS_LOG_E("encrypt, outData buffer too small size: %" LOG_PUBLIC "u, keySize: %" LOG_PUBLIC "u",
443                 outData->size, keySize);
444             return HKS_ERROR_BUFFER_TOO_SMALL;
445         }
446     } else if (cmdId == HKS_CMD_ID_DECRYPT) {
447         if (inData->size > keySize) {
448             HKS_LOG_E("decrypt, invalid inData size: %" LOG_PUBLIC "u, keySize: %" LOG_PUBLIC "u",
449                 inData->size, keySize);
450             return HKS_ERROR_INVALID_ARGUMENT;
451         }
452         if (outData->size < size) {
453             HKS_LOG_E("decrypt, outData buffer too small size: %" LOG_PUBLIC "u, keySize: %" LOG_PUBLIC "u",
454                 outData->size, keySize);
455             return HKS_ERROR_BUFFER_TOO_SMALL;
456         }
457     }
458 
459     return HKS_SUCCESS;
460 }
461 
CheckRsaCipherData(uint32_t cmdId,const struct ParamsValues * inputParams,const struct HksBlob * inData,const struct HksBlob * outData)462 static int32_t CheckRsaCipherData(uint32_t cmdId, const struct ParamsValues *inputParams,
463     const struct HksBlob *inData, const struct HksBlob *outData)
464 {
465     uint32_t padding = inputParams->padding.value;
466     uint32_t keySize = inputParams->keyLen.value / HKS_BITS_PER_BYTE;
467     int32_t ret = HKS_SUCCESS;
468 
469     if (padding == HKS_PADDING_NONE) {
470         ret = CheckRsaNoPadCipherData(keySize, inData, outData);
471     } else if (padding == HKS_PADDING_OAEP) {
472         ret = CheckRsaOaepCipherData(cmdId, keySize, inputParams->digest.value, inData, outData);
473     }
474 
475     HKS_IF_NOT_SUCC_LOGE(ret, "Check Rsa CipherData fail, cmdId: %" LOG_PUBLIC "u, padding: %" LOG_PUBLIC
476         "u, keyLen: %" LOG_PUBLIC "u", cmdId, padding, keySize)
477     HKS_IF_NOT_SUCC_LOGE(ret, "Check Rsa CipherData fail, inData sz: %" LOG_PUBLIC "u, outData sz: %" LOG_PUBLIC "u",
478         inData->size, outData->size)
479 
480     return ret;
481 }
482 #endif
483 #endif
484 
485 #ifdef HKS_SUPPORT_AES_C
486 static int32_t CheckAesAeCipherData(uint32_t cmdId, const struct HksBlob *inData, const struct HksBlob *outData);
487 #endif
488 
489 #if defined(HKS_SUPPORT_AES_C) || defined(HKS_SUPPORT_DES_C) || defined(HKS_SUPPORT_3DES_C) || \
490     defined(HKS_SUPPORT_SM4_C)
CheckBlockCbcCipherData(uint32_t mode,uint32_t cmdId,uint32_t padding,const struct HksBlob * inData,const struct HksBlob * outData)491 static int32_t CheckBlockCbcCipherData(uint32_t mode, uint32_t cmdId, uint32_t padding,
492     const struct HksBlob *inData, const struct HksBlob *outData)
493 {
494     /*
495      * encrypt: inSize greater than 0(has been checked), no-padding: inSize need to be integer multiple of 16
496      *          outSize no less than inSize + (16 - inSize % 16) (in: plain; out: cipher)
497      * decrypt: inSize greater than 0(has been checked) && inSize is integer multiple of 16
498      *          outSize no less than inSize (in: cipher; out: plain)
499      */
500     switch (cmdId) {
501         case HKS_CMD_ID_ENCRYPT: {
502             uint32_t paddingSize = 0;
503             if (padding == HKS_PADDING_NONE) {
504                 if ((mode == HKS_MODE_CBC || mode == HKS_MODE_ECB) &&
505                     inData->size % HKS_BLOCK_CIPHER_CBC_BLOCK_SIZE != 0) {
506                     HKS_LOG_E("encrypt, mode id: %" LOG_PUBLIC "u, no-padding, invalid inSize: %" LOG_PUBLIC "u",
507                         mode, inData->size);
508                     return HKS_ERROR_INVALID_ARGUMENT;
509                 }
510             } else {
511                 paddingSize = HKS_BLOCK_CIPHER_CBC_BLOCK_SIZE - inData->size % HKS_BLOCK_CIPHER_CBC_BLOCK_SIZE;
512                 if (inData->size > (UINT32_MAX - paddingSize)) {
513                     HKS_LOG_E("encrypt, invalid inData size: %" LOG_PUBLIC "u", inData->size);
514                     return HKS_ERROR_INVALID_ARGUMENT;
515                 }
516             }
517             if (outData->size < (inData->size + paddingSize)) {
518                 HKS_LOG_E("encrypt, outData buffer too small size: %" LOG_PUBLIC "u, need: %" LOG_PUBLIC "u",
519                     outData->size, inData->size + paddingSize);
520                 return HKS_ERROR_BUFFER_TOO_SMALL;
521             }
522             break;
523         }
524         case HKS_CMD_ID_DECRYPT:
525             if ((mode == HKS_MODE_CBC || mode == HKS_MODE_ECB) && inData->size % HKS_BLOCK_CIPHER_CBC_BLOCK_SIZE != 0) {
526                 HKS_LOG_E("decrypt, mode id: %" LOG_PUBLIC "u, invalid inData size: %" LOG_PUBLIC "u",
527                     mode, inData->size);
528                 return HKS_ERROR_INVALID_ARGUMENT;
529             }
530             if (outData->size < inData->size) {
531                 HKS_LOG_E("decrypt, outData buffer too small size: %" LOG_PUBLIC "u, inDataSize: %" LOG_PUBLIC "u",
532                     outData->size, inData->size);
533                 return HKS_ERROR_BUFFER_TOO_SMALL;
534             }
535             break;
536         default:
537             return HKS_ERROR_INVALID_ARGUMENT;
538     }
539 
540     return HKS_SUCCESS;
541 }
542 
CheckBlockCipherData(uint32_t cmdId,const struct ParamsValues * inputParams,const struct HksBlob * inData,const struct HksBlob * outData,uint32_t alg)543 static int32_t CheckBlockCipherData(uint32_t cmdId, const struct ParamsValues *inputParams,
544     const struct HksBlob *inData, const struct HksBlob *outData, uint32_t alg)
545 {
546     uint32_t mode = inputParams->mode.value;
547 
548 #if defined(HKS_SUPPORT_AES_C)
549     if (alg == HKS_ALG_AES) {
550         if (mode == HKS_MODE_CBC || mode == HKS_MODE_CTR || mode == HKS_MODE_ECB) {
551             uint32_t padding = inputParams->padding.value;
552             return CheckBlockCbcCipherData(mode, cmdId, padding, inData, outData);
553         } else if (mode == HKS_MODE_GCM || mode == HKS_MODE_CCM) {
554             return CheckAesAeCipherData(cmdId, inData, outData);
555         }
556     }
557 #endif
558 
559 #if defined(HKS_SUPPORT_DES_C)
560     if (alg == HKS_ALG_DES) {
561         if (mode == HKS_MODE_CBC || mode == HKS_MODE_ECB) {
562             uint32_t padding = inputParams->padding.value;
563             return CheckBlockCbcCipherData(mode, cmdId, padding, inData, outData);
564         }
565     }
566 #endif
567 
568 #if defined(HKS_SUPPORT_3DES_C)
569     if (alg == HKS_ALG_3DES) {
570         if (mode == HKS_MODE_CBC || mode == HKS_MODE_ECB) {
571             uint32_t padding = inputParams->padding.value;
572             return CheckBlockCbcCipherData(mode, cmdId, padding, inData, outData);
573         }
574     }
575 #endif
576 
577 #if defined(HKS_SUPPORT_SM4_C)
578     if (alg == HKS_ALG_SM4) {
579         for (uint32_t i = 0; i < HKS_ARRAY_SIZE(g_sm4Mode); i++) {
580             if (mode == g_sm4Mode[i]) {
581                 uint32_t padding = inputParams->padding.value;
582                 return CheckBlockCbcCipherData(mode, cmdId, padding, inData, outData);
583             }
584         }
585     }
586 #endif
587 
588     return HKS_ERROR_INVALID_MODE;
589 }
590 
CheckBlockCipherIvMaterial(const struct HksParamSet * paramSet)591 static int32_t CheckBlockCipherIvMaterial(const struct HksParamSet *paramSet)
592 {
593     struct HksParam *ivParam = NULL;
594     int32_t ret = HksGetParam(paramSet, HKS_TAG_IV, &ivParam);
595     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_IV_FAIL, "cipher get iv param failed!")
596 
597     if ((ivParam->blob.size != HKS_BLOCK_CIPHER_CBC_IV_LEN) || (ivParam->blob.data == NULL)) {
598         HKS_LOG_E("cbc iv param invalid");
599         return HKS_ERROR_INVALID_IV;
600     }
601 
602     return ret;
603 }
604 #endif // defined(HKS_SUPPORT_AES_C) || defined(HKS_SUPPORT_DES_C) || defined(HKS_SUPPORT_3DES_C)
605        // || defined(HKS_SUPPORT_SM4_C)
606 
607 #ifdef HKS_SUPPORT_AES_C
CheckAesPadding(const struct ParamsValues * inputParams)608 static int32_t CheckAesPadding(const struct ParamsValues *inputParams)
609 {
610     if ((inputParams->mode.isAbsent) || (inputParams->padding.isAbsent)) {
611         return HKS_SUCCESS;
612     }
613     uint32_t mode = inputParams->mode.value;
614     uint32_t padding = inputParams->padding.value;
615     if (mode == HKS_MODE_CBC) {
616         return HksCheckValue(padding, g_aesCbcPadding, HKS_ARRAY_SIZE(g_aesCbcPadding));
617     }
618 
619     if (mode == HKS_MODE_CTR) {
620         return HksCheckValue(padding, g_aesCtrPadding, HKS_ARRAY_SIZE(g_aesCtrPadding));
621     }
622 
623     if (mode == HKS_MODE_ECB) {
624         return HksCheckValue(padding, g_aesEcbPadding, HKS_ARRAY_SIZE(g_aesEcbPadding));
625     }
626 
627     if ((mode == HKS_MODE_GCM) || (mode == HKS_MODE_CCM)) {
628         return HksCheckValue(padding, g_aesAeadPadding, HKS_ARRAY_SIZE(g_aesAeadPadding));
629     }
630 
631     return HKS_SUCCESS;
632 }
633 
CheckAesAeCipherData(uint32_t cmdId,const struct HksBlob * inData,const struct HksBlob * outData)634 static int32_t CheckAesAeCipherData(uint32_t cmdId, const struct HksBlob *inData, const struct HksBlob *outData)
635 {
636     /*
637      * encrypt: inSize greater than 0(has been checked),
638      *          outSize no less than inSize + 16(tagLen) (in: plain; out: cipher)
639      * decrypt: inSize greater than 16(tagLen), outSize no less than inSize - 16(tagLen)
640      * decryptFinal: inSize greater than 0(has been checked), outSize no less than inSize (in: cipher; out: plain)
641      */
642     switch (cmdId) {
643         case HKS_CMD_ID_ENCRYPT:
644             if (inData->size > (UINT32_MAX - HKS_AE_TAG_LEN)) {
645                 HKS_LOG_E("encrypt, invalid inSize: %" LOG_PUBLIC "u", inData->size);
646                 return HKS_ERROR_INVALID_ARGUMENT;
647             }
648             if (outData->size < (inData->size + HKS_AE_TAG_LEN)) {
649                 HKS_LOG_E("encrypt, out buffer too small size: %" LOG_PUBLIC "u, inSize: %" LOG_PUBLIC "u",
650                     outData->size, inData->size);
651                 return HKS_ERROR_BUFFER_TOO_SMALL;
652             }
653             break;
654         case HKS_CMD_ID_DECRYPT:
655             if ((inData->size < HKS_AE_TAG_LEN) || (outData->size < inData->size - HKS_AE_TAG_LEN)) {
656                 HKS_LOG_E("decryptfinal, out buffer too small size: %" LOG_PUBLIC "u, inSize: %" LOG_PUBLIC "u",
657                     outData->size, inData->size);
658                 return HKS_ERROR_BUFFER_TOO_SMALL;
659             }
660             break;
661         default:
662             return HKS_ERROR_INVALID_ARGUMENT;
663     }
664 
665     return HKS_SUCCESS;
666 }
667 
CheckCipherAeAadMaterial(uint32_t mode,const struct HksParamSet * paramSet)668 static int32_t CheckCipherAeAadMaterial(uint32_t mode, const struct HksParamSet *paramSet)
669 {
670     struct HksParam *aadParam = NULL;
671     int32_t ret = HksGetParam(paramSet, HKS_TAG_ASSOCIATED_DATA, &aadParam);
672     if (mode == HKS_MODE_GCM && ret == HKS_ERROR_PARAM_NOT_EXIST) {
673         HKS_LOG_W("gcm no input aad");
674         return HKS_SUCCESS;
675     } else if (ret != HKS_SUCCESS) {
676         HKS_LOG_E("cipher get aad param failed!");
677         return HKS_ERROR_CHECK_GET_AAD_FAIL;
678     }
679     HKS_IF_NOT_SUCC_RETURN(CheckBlob(&aadParam->blob), HKS_ERROR_INVALID_AAD)
680 
681     /* gcmMode: aadSize greater than 0 (has been checked); ccmMode: aadSize no less than 4 */
682     if (mode == HKS_MODE_CCM) {
683         if (aadParam->blob.size < HKS_AES_CCM_AAD_LEN_MIN) {
684             HKS_LOG_E("ccm invalid aad size, aad size = %" LOG_PUBLIC "u", aadParam->blob.size);
685             return HKS_ERROR_INVALID_AAD;
686         }
687     }
688 
689     return HKS_SUCCESS;
690 }
691 
CheckCipherAeNonceMaterial(uint32_t mode,const struct HksParamSet * paramSet)692 static int32_t CheckCipherAeNonceMaterial(uint32_t mode, const struct HksParamSet *paramSet)
693 {
694     struct HksParam *nonceParam = NULL;
695     int32_t ret = HksGetParam(paramSet, HKS_TAG_NONCE, &nonceParam);
696     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_CHECK_GET_NONCE_FAIL, "cipher get nonce param failed!")
697     HKS_IF_NOT_SUCC_RETURN(CheckBlob(&nonceParam->blob), HKS_ERROR_INVALID_NONCE)
698 
699     /* gcmMode: nonceSize no less than 12; ccmMode: nonceSize no less than 7, and no greater than 13 */
700     if (mode == HKS_MODE_GCM) {
701         if (nonceParam->blob.size < HKS_AES_GCM_NONCE_LEN_MIN) {
702             HKS_LOG_E("gcm invalid nonce size, nonce size = %" LOG_PUBLIC "u", nonceParam->blob.size);
703             return HKS_ERROR_INVALID_NONCE;
704         }
705     } else if (mode == HKS_MODE_CCM) {
706         HKS_IF_TRUE_LOGE_RETURN((nonceParam->blob.size < HKS_AES_CCM_NONCE_LEN_MIN) ||
707             (nonceParam->blob.size > HKS_AES_CCM_NONCE_LEN_MAX), HKS_ERROR_INVALID_NONCE,
708             "ccm invalid nonce size, nonce size = %" LOG_PUBLIC "u", nonceParam->blob.size);
709     }
710 
711     return HKS_SUCCESS;
712 }
713 
CheckCipherAeMaterial(uint32_t mode,const struct HksParamSet * paramSet)714 static int32_t CheckCipherAeMaterial(uint32_t mode, const struct HksParamSet *paramSet)
715 {
716     int32_t ret = CheckCipherAeAadMaterial(mode, paramSet);
717     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check ae cipher aad failed!")
718 
719     ret = CheckCipherAeNonceMaterial(mode, paramSet);
720     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "check ae cipher nonce failed!")
721 
722     return ret;
723 }
724 #endif
725 
726 #ifdef HKS_SUPPORT_DES_C
CheckDesPadding(const struct ParamsValues * inputParams)727 static int32_t CheckDesPadding(const struct ParamsValues *inputParams)
728 {
729     if ((inputParams->mode.isAbsent) || (inputParams->padding.isAbsent)) {
730         return HKS_SUCCESS;
731     }
732     uint32_t mode = inputParams->mode.value;
733     uint32_t padding = inputParams->padding.value;
734     if (mode == HKS_MODE_CBC) {
735         return HksCheckValue(padding, g_desCbcPadding, HKS_ARRAY_SIZE(g_desCbcPadding));
736     }
737 
738     if (mode == HKS_MODE_ECB) {
739         return HksCheckValue(padding, g_desEcbPadding, HKS_ARRAY_SIZE(g_desEcbPadding));
740     }
741 
742     return HKS_SUCCESS;
743 }
744 #endif
745 
746 #ifdef HKS_SUPPORT_3DES_C
Check3DesPadding(const struct ParamsValues * inputParams)747 static int32_t Check3DesPadding(const struct ParamsValues *inputParams)
748 {
749     if ((inputParams->mode.isAbsent) || (inputParams->padding.isAbsent)) {
750         return HKS_SUCCESS;
751     }
752     uint32_t mode = inputParams->mode.value;
753     uint32_t padding = inputParams->padding.value;
754     if (mode == HKS_MODE_CBC) {
755         return HksCheckValue(padding, g_3desCbcPadding, HKS_ARRAY_SIZE(g_3desCbcPadding));
756     }
757 
758     if (mode == HKS_MODE_ECB) {
759         return HksCheckValue(padding, g_3desEcbPadding, HKS_ARRAY_SIZE(g_3desEcbPadding));
760     }
761 
762     return HKS_SUCCESS;
763 }
764 #endif
765 
766 #ifdef HKS_SUPPORT_SM4_C
CheckSm4Padding(const struct ParamsValues * inputParams)767 static int32_t CheckSm4Padding(const struct ParamsValues *inputParams)
768 {
769     if ((inputParams->mode.isAbsent) || (inputParams->padding.isAbsent)) {
770         return HKS_SUCCESS;
771     }
772     uint32_t mode = inputParams->mode.value;
773     uint32_t padding = inputParams->padding.value;
774     if (mode == HKS_MODE_CBC) {
775         return HksCheckValue(padding, g_sm4CbcPadding, HKS_ARRAY_SIZE(g_sm4CbcPadding));
776     }
777 
778     if (mode == HKS_MODE_CTR) {
779         return HksCheckValue(padding, g_sm4CtrPadding, HKS_ARRAY_SIZE(g_sm4CtrPadding));
780     }
781 
782     if (mode == HKS_MODE_ECB) {
783         return HksCheckValue(padding, g_sm4EcbPadding, HKS_ARRAY_SIZE(g_sm4EcbPadding));
784     }
785 
786     if (mode == HKS_MODE_CFB) {
787         return HksCheckValue(padding, g_sm4CfbPadding, HKS_ARRAY_SIZE(g_sm4CfbPadding));
788     }
789 
790     if (mode == HKS_MODE_OFB) {
791         return HksCheckValue(padding, g_sm4OfbPadding, HKS_ARRAY_SIZE(g_sm4OfbPadding));
792     }
793 
794     return HKS_ERROR_INVALID_ARGUMENT;
795 }
796 #endif
797 
HksCheckValue(uint32_t inputValue,const uint32_t * expectValues,uint32_t valuesCount)798 int32_t HksCheckValue(uint32_t inputValue, const uint32_t *expectValues, uint32_t valuesCount)
799 {
800     for (uint32_t i = 0; i < valuesCount; ++i) {
801         if (inputValue == expectValues[i]) {
802             return HKS_SUCCESS;
803         }
804     }
805     return HKS_ERROR_INVALID_ARGUMENT;
806 }
807 
808 #ifndef _CUT_AUTHENTICATE_
HksCheckGenKeyPurpose(uint32_t alg,uint32_t inputPurpose,uint32_t keyFlag)809 int32_t HksCheckGenKeyPurpose(uint32_t alg, uint32_t inputPurpose, uint32_t keyFlag)
810 {
811     int32_t ret = CheckPurposeUnique(inputPurpose);
812     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "gen key purpose not unique")
813 
814     return CheckPurposeValid(alg, inputPurpose, keyFlag);
815 }
816 
817 #ifdef HKS_SUPPORT_DSA_C
HksGetDsaKeySize(const struct HksBlob * key,uint32_t * keySize)818 static int32_t HksGetDsaKeySize(const struct HksBlob *key, uint32_t *keySize)
819 {
820     HKS_IF_TRUE_LOGE_RETURN(key->size < sizeof(struct HksParamSet), HKS_ERROR_INVALID_KEY_FILE,
821         "check dsa key size: invalid keyfile size: %" LOG_PUBLIC "u", key->size);
822 
823     struct HksParamSet *keyParamSet = (struct HksParamSet *)key->data;
824     int32_t ret = HksCheckParamSetValidity(keyParamSet);
825     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_KEY_FILE, "check dsa key size: paramset invalid failed")
826 
827     struct HksParam *keySizeParam = NULL;
828     ret = HksGetParam(keyParamSet, HKS_TAG_KEY_SIZE, &keySizeParam);
829     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_KEY_FILE,
830         "check dsa key size: get param get tag:0x%" LOG_PUBLIC "x failed", HKS_TAG_KEY_SIZE)
831     *keySize = keySizeParam->uint32Param;
832     return ret;
833 }
834 #endif
835 
HksGetKeySize(uint32_t alg,const struct HksBlob * key,uint32_t * keySize)836 int32_t HksGetKeySize(uint32_t alg, const struct HksBlob *key, uint32_t *keySize)
837 {
838     int32_t ret = HKS_ERROR_INVALID_ALGORITHM;
839     switch (alg) {
840 #ifdef HKS_SUPPORT_RSA_C
841         case HKS_ALG_RSA:
842             ret = CheckAndGetKeySize(key, g_rsaKeySize, HKS_ARRAY_SIZE(g_rsaKeySize), keySize);
843 #ifdef HKS_SUPPORT_RSA_C_FLEX_KEYSIZE
844             if (ret != HKS_SUCCESS) {
845                 ret = CheckAndGetRsaKeySize(key, keySize);
846             }
847 #endif
848             return ret;
849 #endif
850 #ifdef HKS_SUPPORT_DSA_C
851         case HKS_ALG_DSA:
852 #ifndef _STORAGE_LITE_
853             return HksGetDsaKeySize(key, keySize);
854 #else
855             return HKS_ERROR_INVALID_ALGORITHM;
856 #endif
857 #endif
858 #ifdef HKS_SUPPORT_ECC_C
859         case HKS_ALG_ECC:
860             return CheckAndGetKeySize(key, g_eccKeySize, HKS_ARRAY_SIZE(g_eccKeySize), keySize);
861 #endif
862 #ifdef HKS_SUPPORT_ECDH_C
863         case HKS_ALG_ECDH:
864             return CheckAndGetKeySize(key, g_ecdhKeySize, HKS_ARRAY_SIZE(g_ecdhKeySize), keySize);
865 #endif
866 #if defined(HKS_SUPPORT_X25519_C) || defined(HKS_SUPPORT_ED25519_C)
867         case HKS_ALG_X25519:
868         case HKS_ALG_ED25519:
869             return CheckAndGetKeySize(key, g_curve25519KeySize, HKS_ARRAY_SIZE(g_curve25519KeySize), keySize);
870 #endif
871 #ifdef HKS_SUPPORT_DH_C
872         case HKS_ALG_DH:
873             return CheckAndGetKeySize(key, g_dhKeySize, HKS_ARRAY_SIZE(g_dhKeySize), keySize);
874 #endif
875 #ifdef HKS_SUPPORT_SM4_C
876         case HKS_ALG_SM4:
877             return CheckAndGetKeySize(key, g_sm4KeySize, HKS_ARRAY_SIZE(g_sm4KeySize), keySize);
878 #endif
879 #ifdef HKS_SUPPORT_SM2_C
880         case HKS_ALG_SM2:
881             return CheckAndGetKeySize(key, g_sm2KeySize, HKS_ARRAY_SIZE(g_sm2KeySize), keySize);
882 #endif
883         default:
884             return ret;
885     }
886 }
887 #endif /* _CUT_AUTHENTICATE_ */
888 
889 #ifndef _CUT_AUTHENTICATE_
HksCheckGenKeyMutableParams(uint32_t alg,const struct ParamsValues * inputParams)890 int32_t HksCheckGenKeyMutableParams(uint32_t alg, const struct ParamsValues *inputParams)
891 {
892     int32_t ret = HKS_SUCCESS;
893     switch (alg) {
894 #ifdef HKS_SUPPORT_RSA_C
895         case HKS_ALG_RSA:
896             ret = CheckRsaGenKeyPadding(inputParams);
897             HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_PADDING,
898                 "Check padding not expected, padding = %" LOG_PUBLIC "u", inputParams->padding.value);
899             break;
900 #endif
901 #ifdef HKS_SUPPORT_AES_C
902         case HKS_ALG_AES:
903             ret = CheckAesPadding(inputParams);
904             HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_PADDING,
905                 "Check padding not expected, padding = %" LOG_PUBLIC "u", inputParams->padding.value);
906             break;
907 #endif
908 #ifdef HKS_SUPPORT_DES_C
909         case HKS_ALG_DES:
910             ret = CheckDesPadding(inputParams);
911             HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_PADDING,
912                 "Check padding not expected, padding = %" LOG_PUBLIC "u", inputParams->padding.value);
913             break;
914 #endif
915 #ifdef HKS_SUPPORT_3DES_C
916         case HKS_ALG_3DES:
917             ret = Check3DesPadding(inputParams);
918             HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_PADDING,
919                 "Check padding not expected, padding = %" LOG_PUBLIC "u", inputParams->padding.value);
920             break;
921 #endif
922 #ifdef HKS_SUPPORT_SM4_C
923         case HKS_ALG_SM4:
924             ret = CheckSm4Padding(inputParams);
925             HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_PADDING,
926                 "Check padding not expected, padding = %" LOG_PUBLIC "u", inputParams->padding.value);
927             break;
928 #endif
929         default:
930             /* other alg no need check padding */
931             break;
932     }
933 
934     return ret;
935 }
936 
CheckImportMutableParams(uint32_t alg,const struct ParamsValues * params)937 int32_t CheckImportMutableParams(uint32_t alg, const struct ParamsValues *params)
938 {
939     if (((alg == HKS_ALG_DSA) || (alg == HKS_ALG_ED25519)) &&
940         (params->purpose.value != HKS_KEY_PURPOSE_VERIFY)) {
941         HKS_LOG_E("Import key check purpose failed.");
942         return HKS_ERROR_INVALID_PURPOSE;
943     }
944 
945     if ((alg == HKS_ALG_SM2) &&
946         ((params->purpose.value != HKS_KEY_PURPOSE_VERIFY) && (params->purpose.value != HKS_KEY_PURPOSE_ENCRYPT))) {
947         HKS_LOG_E("Import key check purpose failed.");
948         return HKS_ERROR_INVALID_PURPOSE;
949     }
950 
951     if ((alg == HKS_ALG_ECC) &&
952         ((params->purpose.value != HKS_KEY_PURPOSE_VERIFY) && (params->purpose.value != HKS_KEY_PURPOSE_UNWRAP) &&
953         (params->purpose.value != HKS_KEY_PURPOSE_AGREE))) {
954         HKS_LOG_E("Import key check purpose failed.");
955         return HKS_ERROR_INVALID_PURPOSE;
956     }
957 
958     if ((alg == HKS_ALG_RSA) &&
959         ((params->purpose.value != HKS_KEY_PURPOSE_VERIFY) && (params->purpose.value != HKS_KEY_PURPOSE_ENCRYPT))) {
960         HKS_LOG_E("Import key check purpose failed.");
961         return HKS_ERROR_INVALID_PURPOSE;
962     }
963 
964     if (alg == HKS_ALG_RSA) {
965 #ifdef HKS_SUPPORT_RSA_C
966         if (params->padding.isAbsent) {
967             return HKS_SUCCESS;
968         }
969         if (params->purpose.value == HKS_KEY_PURPOSE_ENCRYPT) {
970             return HksCheckValue(params->padding.value, g_rsaCipherPadding, HKS_ARRAY_SIZE(g_rsaCipherPadding));
971         } else if (params->purpose.value == HKS_KEY_PURPOSE_VERIFY) {
972             return HksCheckValue(params->padding.value, g_rsaSignPadding, HKS_ARRAY_SIZE(g_rsaSignPadding));
973         }
974 #else
975         return HKS_ERROR_NOT_SUPPORTED;
976 #endif
977     }
978 
979     return HKS_SUCCESS;
980 }
981 
HksCheckSignature(uint32_t cmdId,uint32_t alg,uint32_t keySize,const struct HksBlob * signature)982 int32_t HksCheckSignature(uint32_t cmdId, uint32_t alg, uint32_t keySize, const struct HksBlob *signature)
983 {
984     (void)cmdId;
985     (void)keySize;
986     (void)signature;
987     int32_t ret = HKS_ERROR_INVALID_ALGORITHM;
988     switch (alg) {
989 #if defined(HKS_SUPPORT_RSA_C) && defined(HKS_SUPPORT_RSA_SIGN_VERIFY)
990         case HKS_ALG_RSA:
991             ret = HksCheckValue(keySize, g_rsaKeySize, HKS_ARRAY_SIZE(g_rsaKeySize));
992 #ifdef HKS_SUPPORT_RSA_C_FLEX_KEYSIZE
993             if (ret != HKS_SUCCESS) {
994                 ret = CheckRsaKeySize(keySize);
995             }
996 #endif
997             HKS_IF_NOT_SUCC_LOGE_RETURN(ret,
998                 HKS_ERROR_INVALID_ARGUMENT, "check key size: key size value %" LOG_PUBLIC "u not expected", keySize)
999             return CheckRsaSignature(cmdId, keySize, signature);
1000 #endif
1001 #if defined(HKS_SUPPORT_DSA_C) && defined(HKS_SUPPORT_DSA_SIGN_VERIFY)
1002         case HKS_ALG_DSA:
1003             return HKS_SUCCESS;
1004 #endif
1005 #ifdef HKS_SUPPORT_ECC_C
1006         case HKS_ALG_ECC:
1007             HKS_IF_NOT_SUCC_LOGE_RETURN(HksCheckValue(keySize, g_eccKeySize, HKS_ARRAY_SIZE(g_eccKeySize)),
1008                 HKS_ERROR_INVALID_ARGUMENT, "check key size: key size value %" LOG_PUBLIC "u not expected", keySize)
1009             return CheckEccSignature(cmdId, keySize, signature);
1010 #endif
1011 #ifdef HKS_SUPPORT_ED25519_C
1012         case HKS_ALG_ED25519:
1013             return CheckEd25519Signature(cmdId, signature);
1014 #endif
1015 #ifdef HKS_SUPPORT_SM2_C
1016         case HKS_ALG_SM2:
1017             HKS_IF_NOT_SUCC_LOGE_RETURN(HksCheckValue(keySize, g_sm2KeySize, HKS_ARRAY_SIZE(g_sm2KeySize)),
1018                 HKS_ERROR_INVALID_ARGUMENT, "check key size: key size value %" LOG_PUBLIC "u not expected", keySize)
1019             return CheckEccSignature(cmdId, keySize, signature);
1020 #endif
1021         default:
1022             return ret;
1023     }
1024 }
1025 
HksCheckSignVerifyMutableParams(uint32_t cmdId,uint32_t alg,const struct ParamsValues * inputParams)1026 int32_t HksCheckSignVerifyMutableParams(uint32_t cmdId, uint32_t alg, const struct ParamsValues *inputParams)
1027 {
1028     switch (cmdId) {
1029         case HKS_CMD_ID_SIGN:
1030             if ((inputParams->purpose.value & HKS_KEY_PURPOSE_SIGN) == 0) {
1031                 return HKS_ERROR_INVALID_PURPOSE;
1032             }
1033             break;
1034         case HKS_CMD_ID_VERIFY:
1035             if ((inputParams->purpose.value & HKS_KEY_PURPOSE_VERIFY) == 0) {
1036                 return HKS_ERROR_INVALID_PURPOSE;
1037             }
1038             break;
1039         default:
1040             return HKS_ERROR_INVALID_ARGUMENT;
1041     }
1042 
1043     switch (alg) {
1044 #ifdef HKS_SUPPORT_RSA_C
1045         case HKS_ALG_RSA:
1046             HKS_IF_NOT_SUCC_RETURN(HksCheckValue(inputParams->padding.value, g_rsaSignPadding,
1047                 HKS_ARRAY_SIZE(g_rsaSignPadding)), HKS_ERROR_INVALID_PADDING)
1048             break;
1049 #endif
1050 #ifdef HKS_SUPPORT_DSA_C
1051         case HKS_ALG_DSA:
1052             break;
1053 #endif
1054 #ifdef HKS_SUPPORT_ECC_C
1055         case HKS_ALG_ECC:
1056             break;
1057 #endif
1058         default:
1059             /* other alg no need check padding */
1060             break;
1061     }
1062     return HKS_SUCCESS;
1063 }
1064 #endif /* _CUT_AUTHENTICATE_ */
1065 
1066 #if defined(HKS_SUPPORT_DES_C) || defined(HKS_SUPPORT_3DES_C)
HksCheckCipherMutableParamsByAlg(uint32_t alg,const struct ParamsValues * inputParams)1067 static int32_t HksCheckCipherMutableParamsByAlg(uint32_t alg, const struct ParamsValues *inputParams)
1068 {
1069     int32_t ret = HKS_ERROR_INVALID_PADDING;
1070     switch (alg) {
1071 #ifdef HKS_SUPPORT_DES_C
1072         case HKS_ALG_DES:
1073             ret = CheckDesPadding(inputParams);
1074             break;
1075 #endif
1076 #ifdef HKS_SUPPORT_3DES_C
1077         case HKS_ALG_3DES:
1078             ret = Check3DesPadding(inputParams);
1079             break;
1080 #endif
1081         default:
1082             return HKS_ERROR_INVALID_ALGORITHM;
1083     }
1084 
1085     return ret;
1086 }
1087 #endif
1088 
HksCheckCipherMutableParams(uint32_t cmdId,uint32_t alg,const struct ParamsValues * inputParams)1089 int32_t HksCheckCipherMutableParams(uint32_t cmdId, uint32_t alg, const struct ParamsValues *inputParams)
1090 {
1091     switch (cmdId) {
1092         case HKS_CMD_ID_ENCRYPT:
1093             if ((inputParams->purpose.value & HKS_KEY_PURPOSE_ENCRYPT) == 0) {
1094                 return HKS_ERROR_INVALID_PURPOSE;
1095             }
1096             break;
1097         case HKS_CMD_ID_DECRYPT:
1098             if ((inputParams->purpose.value & HKS_KEY_PURPOSE_DECRYPT) == 0) {
1099                 return HKS_ERROR_INVALID_PURPOSE;
1100             }
1101             break;
1102         default:
1103             return HKS_ERROR_INVALID_ARGUMENT;
1104     }
1105 
1106     int32_t ret = HKS_ERROR_INVALID_PADDING;
1107     switch (alg) {
1108 #ifdef HKS_SUPPORT_RSA_C
1109         case HKS_ALG_RSA:
1110             ret = HksCheckValue(inputParams->padding.value, g_rsaCipherPadding, HKS_ARRAY_SIZE(g_rsaCipherPadding));
1111             break;
1112 #endif
1113 #ifdef HKS_SUPPORT_AES_C
1114         case HKS_ALG_AES:
1115             ret = CheckAesPadding(inputParams);
1116             break;
1117 #endif
1118 #ifdef HKS_SUPPORT_SM4_C
1119         case HKS_ALG_SM4:
1120             ret = CheckSm4Padding(inputParams);
1121             break;
1122 #endif
1123 #ifdef HKS_SUPPORT_SM2_C
1124         case HKS_ALG_SM2:
1125             ret = HksCheckValue(inputParams->padding.value, g_sm2CipherPadding, HKS_ARRAY_SIZE(g_sm2CipherPadding));
1126             break;
1127 #endif
1128         default:
1129 #if defined(HKS_SUPPORT_DES_C) || defined(HKS_SUPPORT_3DES_C)
1130             ret = HksCheckCipherMutableParamsByAlg(alg, inputParams);
1131             if (ret == HKS_ERROR_INVALID_ALGORITHM) {
1132                 return HKS_ERROR_INVALID_ALGORITHM;
1133             }
1134 #endif
1135             break;
1136     }
1137     HKS_IF_NOT_SUCC_RETURN(ret, HKS_ERROR_INVALID_PADDING)
1138     return ret;
1139 }
1140 
HksCheckCipherData(uint32_t cmdId,uint32_t alg,const struct ParamsValues * inputParams,const struct HksBlob * inData,const struct HksBlob * outData)1141 int32_t HksCheckCipherData(uint32_t cmdId, uint32_t alg, const struct ParamsValues *inputParams,
1142     const struct HksBlob *inData, const struct HksBlob *outData)
1143 {
1144     switch (alg) {
1145 #if defined(HKS_SUPPORT_RSA_C) && defined(HKS_SUPPORT_RSA_CRYPT)
1146         case HKS_ALG_RSA:
1147             return CheckRsaCipherData(cmdId, inputParams, inData, outData);
1148 #endif
1149 #ifdef HKS_SUPPORT_AES_C
1150         case HKS_ALG_AES:
1151             return CheckBlockCipherData(cmdId, inputParams, inData, outData, HKS_ALG_AES);
1152 #endif
1153 #ifdef HKS_SUPPORT_DES_C
1154         case HKS_ALG_DES:
1155             return CheckBlockCipherData(cmdId, inputParams, inData, outData, HKS_ALG_DES);
1156 #endif
1157 #ifdef HKS_SUPPORT_3DES_C
1158         case HKS_ALG_3DES:
1159             return CheckBlockCipherData(cmdId, inputParams, inData, outData, HKS_ALG_3DES);
1160 #endif
1161 #ifdef HKS_SUPPORT_SM4_C
1162         case HKS_ALG_SM4:
1163             return CheckBlockCipherData(cmdId, inputParams, inData, outData, HKS_ALG_SM4);
1164 #endif
1165 #ifdef HKS_SUPPORT_SM2_C
1166         case HKS_ALG_SM2:
1167             return HKS_SUCCESS;
1168 #endif
1169         default:
1170             return HKS_ERROR_INVALID_ALGORITHM;
1171     }
1172 }
1173 
HksCheckCipherMaterialParams(uint32_t alg,const struct ParamsValues * inputParams,const struct HksParamSet * paramSet)1174 int32_t HksCheckCipherMaterialParams(uint32_t alg, const struct ParamsValues *inputParams,
1175     const struct HksParamSet *paramSet)
1176 {
1177 #ifdef HKS_SUPPORT_AES_C
1178     if (alg == HKS_ALG_AES) {
1179         uint32_t mode = inputParams->mode.value;
1180         if (mode == HKS_MODE_CBC) {
1181             return CheckBlockCipherIvMaterial(paramSet);
1182         } else if ((mode == HKS_MODE_CCM) || (mode == HKS_MODE_GCM)) {
1183             return CheckCipherAeMaterial(mode, paramSet);
1184         }
1185     }
1186 #endif
1187 #ifdef HKS_SUPPORT_DES_C
1188     if (alg == HKS_ALG_DES) {
1189         uint32_t mode = inputParams->mode.value;
1190         if (mode == HKS_MODE_CBC) {
1191             return CheckBlockCipherIvMaterial(paramSet);
1192         }
1193     }
1194 #endif
1195 #ifdef HKS_SUPPORT_3DES_C
1196     if (alg == HKS_ALG_3DES) {
1197         uint32_t mode = inputParams->mode.value;
1198         if (mode == HKS_MODE_CBC) {
1199             return CheckBlockCipherIvMaterial(paramSet);
1200         }
1201     }
1202 #endif
1203 #ifdef HKS_SUPPORT_SM4_C
1204     if (alg == HKS_ALG_SM4) {
1205         uint32_t mode = inputParams->mode.value;
1206         HKS_IF_TRUE_RETURN(mode == HKS_MODE_CBC || mode == HKS_MODE_CTR ||
1207             mode == HKS_MODE_CFB || mode == HKS_MODE_OFB,
1208             CheckBlockCipherIvMaterial(paramSet));
1209     }
1210 #endif
1211     return HKS_SUCCESS;
1212 }
1213 
1214 #ifdef HKS_SUPPORT_USER_AUTH_ACCESS_CONTROL
HasValidAuthAccessType(const struct ExpectParams allowAuthAccessTypes,uint32_t authAccessType,uint32_t * matchType)1215 static int32_t HasValidAuthAccessType(const struct ExpectParams allowAuthAccessTypes,
1216     uint32_t authAccessType, uint32_t *matchType)
1217 {
1218     for (uint32_t i = 0; i < allowAuthAccessTypes.valueCnt; i++) {
1219         if ((authAccessType & allowAuthAccessTypes.values[i]) != 0) {
1220             *matchType = allowAuthAccessTypes.values[i];
1221             return HKS_SUCCESS;
1222         }
1223     }
1224     return HKS_ERROR_INVALID_ARGUMENT;
1225 }
1226 
CheckTuiPinAccessType(uint32_t authAccessType)1227 static int32_t CheckTuiPinAccessType(uint32_t authAccessType)
1228 {
1229     if (authAccessType != HKS_AUTH_ACCESS_ALWAYS_VALID) {
1230         HKS_LOG_E("invalid authAccessType for TUI PIN, authAccessType = %" LOG_PUBLIC "d", authAccessType);
1231         return HKS_ERROR_INVALID_ACCESS_TYPE;
1232     }
1233 
1234     return HKS_SUCCESS;
1235 }
1236 
HksCheckAuthAccessTypeByUserAuthType(uint32_t userAuthType,uint32_t authAccessType)1237 static int32_t HksCheckAuthAccessTypeByUserAuthType(uint32_t userAuthType, uint32_t authAccessType)
1238 {
1239     if ((userAuthType & HKS_USER_AUTH_TYPE_TUI_PIN) != 0) {
1240         return CheckTuiPinAccessType(authAccessType);
1241     }
1242     uint32_t valuesCnt = HKS_ARRAY_SIZE(g_expectAuthAccessParams);
1243     uint32_t validAuthAccessType = 0;
1244     uint32_t tempType = 0;
1245     for (uint32_t i = 0; i < valuesCnt; i++) {
1246         struct AuthAccessTypeChecker checker = g_expectAuthAccessParams[i];
1247         if ((checker.userAuthType & userAuthType) != 0 &&
1248             HasValidAuthAccessType(checker.allowAuthAccessTypes, authAccessType, &tempType) == HKS_SUCCESS) {
1249             validAuthAccessType |= tempType;
1250         }
1251     }
1252     if ((authAccessType != 0) && (authAccessType == validAuthAccessType)) {
1253         HKS_IF_TRUE_LOGE_RETURN((authAccessType & HKS_AUTH_ACCESS_ALWAYS_VALID) != 0 &&
1254             (authAccessType &(~HKS_AUTH_ACCESS_ALWAYS_VALID)) != 0, HKS_ERROR_INVALID_ACCESS_TYPE,
1255             "auth access type is invalid: ALWAYS_VALID cannot coexist with other type");
1256         return HKS_SUCCESS;
1257     }
1258     HKS_LOG_E("authAccessType %" LOG_PUBLIC "u is not equal to validAuthAccessType %" LOG_PUBLIC "u or is 0",
1259         authAccessType, validAuthAccessType);
1260     return HKS_ERROR_INVALID_ACCESS_TYPE;
1261 }
1262 #endif
1263 
HksCheckUserAuthParams(uint32_t userAuthType,uint32_t authAccessType,uint32_t challengeType)1264 int32_t HksCheckUserAuthParams(uint32_t userAuthType, uint32_t authAccessType, uint32_t challengeType)
1265 {
1266 #ifdef HKS_SUPPORT_USER_AUTH_ACCESS_CONTROL
1267     int32_t ret = HksCheckValue(userAuthType, g_supportUserAuthTypes, HKS_ARRAY_SIZE(g_supportUserAuthTypes));
1268     HKS_IF_NOT_SUCC_RETURN(ret, HKS_ERROR_INVALID_AUTH_TYPE)
1269 
1270     ret = HksCheckValue(challengeType, g_userAuthChallengeType, HKS_ARRAY_SIZE(g_userAuthChallengeType));
1271     HKS_IF_NOT_SUCC_RETURN(ret, HKS_ERROR_INVALID_CHALLENGE_TYPE)
1272 
1273     return HksCheckAuthAccessTypeByUserAuthType(userAuthType, authAccessType);
1274 #else
1275     (void)userAuthType;
1276     (void)authAccessType;
1277     (void)challengeType;
1278     return HKS_SUCCESS;
1279 #endif
1280 }
1281 
HksCheckSecureSignParams(uint32_t secureSignType)1282 int32_t HksCheckSecureSignParams(uint32_t secureSignType)
1283 {
1284 #ifdef HKS_SUPPORT_USER_AUTH_ACCESS_CONTROL
1285     return HksCheckValue(secureSignType, g_supportSecureSignType, HKS_ARRAY_SIZE(g_supportSecureSignType));
1286 #else
1287     (void)secureSignType;
1288     return HKS_SUCCESS;
1289 #endif
1290 }
1291 
1292 /* If the algorithm is ed25519, the plaintext is directly cached, and if the digest is HKS_DIGEST_NONE, the
1293    hash value has been passed in by the user. So the hash value does not need to be free.
1294 */
HksCheckNeedCache(uint32_t alg,uint32_t digest)1295 int32_t HksCheckNeedCache(uint32_t alg, uint32_t digest)
1296 {
1297     if ((alg == HKS_ALG_ED25519) || (digest == HKS_DIGEST_NONE)) {
1298         HKS_LOG_I("need to cache the data");
1299         return HKS_SUCCESS;
1300     }
1301     return HKS_FAILURE;
1302 }
1303 
1304 #ifdef HKS_SUPPORT_USER_AUTH_ACCESS_CONTROL
CheckUserAuthKeyInfoValidity(const struct HksParamSet * paramSet,const struct KeyInfoParams * params,uint32_t paramsCnt)1305 static int32_t CheckUserAuthKeyInfoValidity(const struct HksParamSet *paramSet,
1306     const struct KeyInfoParams *params, uint32_t paramsCnt)
1307 {
1308     for (uint32_t i = 0; i < paramsCnt; i++) {
1309         if (params[i].needCheck) {
1310             struct HksParam *param = NULL;
1311             int32_t ret = HksGetParam(paramSet, params[i].tag, &param);
1312             HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_SUCCESS, "tag is empty and no need to check!")
1313 
1314             ret = HksCheckValue(param->uint32Param, params[i].values, params[i].valueCnt);
1315             HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "not support tag %" LOG_PUBLIC "u and value is %" LOG_PUBLIC "u",
1316                 params[i].tag, param->uint32Param)
1317         }
1318     }
1319     return HKS_SUCCESS;
1320 }
1321 #endif
1322 
HksCheckUserAuthKeyInfoValidity(const struct HksParamSet * paramSet)1323 int32_t HksCheckUserAuthKeyInfoValidity(const struct HksParamSet *paramSet)
1324 {
1325 #ifdef HKS_SUPPORT_USER_AUTH_ACCESS_CONTROL
1326     HKS_IF_NOT_SUCC_LOGE_RETURN(HksCheckParamSet(paramSet, paramSet->paramSetSize),
1327     HKS_ERROR_INVALID_ARGUMENT, "invalid paramSet!")
1328 
1329     struct HksParam *algParam = NULL;
1330     int32_t ret = HksGetParam(paramSet, HKS_TAG_ALGORITHM, &algParam);
1331     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, ret, "get alg param failed!")
1332 
1333     for (uint32_t i = 0; i < HKS_ARRAY_SIZE(g_validKeyInfo); i++) {
1334         if (algParam->uint32Param == g_validKeyInfo[i].keyAlg) {
1335             ret = CheckUserAuthKeyInfoValidity(paramSet, g_validKeyInfo[i].params, g_validKeyInfo[i].paramsCnt);
1336             HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_NOT_SUPPORTED, "not support set key auth purpose!")
1337         }
1338     }
1339     HKS_LOG_I("support set key auth purpose!");
1340     return ret;
1341 #else
1342     (void)paramSet;
1343     return HKS_SUCCESS;
1344 #endif
1345 }