1 /* 2 * Copyright (c) 2021 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 #ifndef HKS_BASE_CHECK_H 17 #define HKS_BASE_CHECK_H 18 19 #include "hks_param.h" 20 #include "hks_type_inner.h" 21 22 enum CheckKeyType { 23 HKS_CHECK_TYPE_GEN_KEY, 24 HKS_CHECK_TYPE_USE_KEY, 25 HKS_CHECK_TYPE_GEN_MAC_KEY, 26 HKS_CHECK_TYPE_GEN_DERIVE_KEY, 27 }; 28 29 struct Params { 30 bool needCheck; 31 uint32_t value; 32 bool isAbsent; 33 }; 34 35 struct ParamsValues { 36 struct Params keyLen; 37 struct Params padding; 38 struct Params purpose; 39 struct Params digest; 40 struct Params mode; 41 }; 42 43 struct ParamsValuesChecker { 44 enum CheckKeyType checkType; 45 struct ParamsValues paramValues; 46 }; 47 48 struct ExpectParams { 49 bool needCheck; 50 const uint32_t *values; 51 uint32_t valueCnt; 52 }; 53 54 struct ExpectParamsValues { 55 const struct ExpectParams keyLen; 56 const struct ExpectParams padding; 57 const struct ExpectParams purpose; 58 const struct ExpectParams digest; 59 const struct ExpectParams mode; 60 }; 61 #define EXPECT_PARAMS_VALUES_INIT {{0}, {0}, {0}, {0}, {0}} 62 63 struct ExpectParamsValuesChecker { 64 enum CheckKeyType checkType; 65 const struct ExpectParamsValues paramValues; 66 }; 67 68 struct AuthAccessTypeChecker { 69 enum HksUserAuthType userAuthType; 70 const struct ExpectParams allowAuthAccessTypes; 71 }; 72 73 #ifdef __cplusplus 74 extern "C" { 75 #endif 76 77 int32_t HksCheckValue(uint32_t inputValue, const uint32_t *expectValues, uint32_t valuesCount); 78 79 int32_t HksGetKeySize(uint32_t alg, const struct HksBlob *key, uint32_t *keySize); 80 81 int32_t HksCheckGenKeyPurpose(uint32_t alg, uint32_t inputPurpose); 82 83 int32_t HksGetInputParmasByAlg(uint32_t alg, enum CheckKeyType checkType, const struct HksParamSet *paramSet, 84 struct ParamsValues *inputParams); 85 86 int32_t HksCheckFixedParams(uint32_t alg, enum CheckKeyType checkType, const struct ParamsValues *inputParams); 87 88 int32_t HksCheckGenKeyMutableParams(uint32_t alg, const struct ParamsValues *inputParams); 89 90 int32_t CheckImportMutableParams(uint32_t alg, const struct ParamsValues *params); 91 92 int32_t HksCheckSignature(uint32_t cmdId, uint32_t alg, uint32_t keySize, const struct HksBlob *signature); 93 94 int32_t HksCheckSignVerifyMutableParams(uint32_t cmdId, uint32_t alg, const struct ParamsValues *inputParams); 95 96 int32_t HksCheckCipherMutableParams(uint32_t cmdId, uint32_t alg, const struct ParamsValues *inputParams); 97 98 int32_t HksCheckCihperData(uint32_t cmdId, uint32_t alg, const struct ParamsValues *inputParams, 99 const struct HksBlob *inData, const struct HksBlob *outData); 100 101 int32_t HksCheckCipherMaterialParams(uint32_t alg, const struct ParamsValues *inputParams, 102 const struct HksParamSet *paramSet); 103 104 int32_t HksCheckUserAuthParams(uint32_t userAuthType, uint32_t authAccessType, uint32_t challengeType); 105 106 int32_t HksCheckSecureSignParams(uint32_t secureSignType); 107 108 int32_t GetInputParams(const struct HksParamSet *paramSet, struct ParamsValues *inputParams); 109 110 int32_t HksCheckOptionalParam(uint32_t tag, uint32_t alg, uint32_t purpose, bool isAbsent, struct HksParam *param); 111 112 int32_t HksCheckNeedCache(uint32_t alg, uint32_t digest); 113 #ifdef __cplusplus 114 } 115 #endif 116 117 #endif /* HKS_BASE_CHECK_H */ 118 119