1 /* 2 * Copyright (c) 2022 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_ACCESS_CONTROL_TEST_COMMON_H 17 #define HKS_ACCESS_CONTROL_TEST_COMMON_H 18 19 #include "hks_three_stage_test_common.h" 20 #include "hks_core_hal_api.h" 21 22 #include <vector> 23 #include <string> 24 25 #define SHA256_SIGN_LEN 32 26 #define SHA256_KEY_LEN 32 27 #define AUTH_TOKEN_LEN sizeof(struct HksUserAuthToken) 28 #define AUTH_TOKEN_DATA_LEN (AUTH_TOKEN_LEN - SHA256_SIGN_LEN) 29 #define TOKEN_CHALLENGE_LEN 32 30 #define TOKEN_CHALLENGE_LEN_PER_POS 8 31 #define HKS_DEFAULT_USER_AT_KEY "huks_default_user_auth_token_key" 32 33 struct IDMParams { 34 uint64_t secureUid; 35 uint64_t enrolledId; 36 uint64_t time; 37 uint32_t authType; 38 }; 39 40 namespace Unittest::HksAccessControlPartTest { 41 static const std::string g_inData = "Hks_Authtoken_Test_00000000000000000000000000000000000000000000000000000000000" 42 "000000000000000000000000000000000000000000000000000000000000000000000000000000000" 43 "00000000000000000000000000000000000000000000000000000000000000000000000000_string"; 44 45 static const uint32_t IV_SIZE = 16; 46 47 const uint32_t KEY_PARAMSET_SIZE = 1024; 48 49 const uint32_t HMAC_COMMON_SIZE = 256; 50 51 const uint32_t DATA_COMMON_SIZE = 1024; 52 53 const uint32_t RSA_COMMON_SIZE = 1024; 54 55 const uint32_t ECDH_COMMON_SIZE = 1024; 56 57 const uint32_t DERIVE_KEY_SIZE_32 = 32; 58 59 const uint32_t DERIVE_ITERATION = 1000; 60 61 const uint32_t DERIVE_COMMON_SIZE = 2048; 62 63 const uint32_t DSA_COMMON_SIZE = 1024; 64 65 static uint8_t IV[IV_SIZE] = {0}; 66 67 static uint8_t g_saltdata[16] = {0}; 68 69 struct TestAccessCaseParams { 70 std::vector<HksParam> genParams; 71 std::vector<HksParam> initParams; 72 HksErrorCode initResult = HksErrorCode::HKS_SUCCESS; 73 }; 74 75 struct TestDsaKeyParams { 76 struct HksBlob *xData; 77 struct HksBlob *yData; 78 struct HksBlob *pData; 79 struct HksBlob *qData; 80 struct HksBlob *gData; 81 }; 82 83 struct HksTestGenAuthTokenParams { 84 struct HksBlob *authChallenge; 85 uint64_t secureUid; 86 uint64_t enrolledId; 87 uint64_t credentialId; 88 uint64_t time; 89 uint32_t authType; 90 }; 91 92 int32_t AddAuthtokenUpdateFinish(struct HksBlob *handle, struct HksParamSet *initParamSet, uint32_t posNum); 93 94 int32_t CheckAccessCipherTest(const TestAccessCaseParams &testCaseParams, 95 const IDMParams &testIDMParams); 96 97 int32_t CheckAccessHmacTest(const TestAccessCaseParams &testCaseParams, 98 const IDMParams &testIDMParams); 99 100 int32_t CheckAccessAgreeTest(const TestAccessCaseParams &testCaseParams, struct HksParamSet *finishParamSet, 101 const IDMParams &testIDMParams); 102 103 int32_t CheckAccessDeriveTest(const TestAccessCaseParams &testCaseParams, struct HksParamSet *finishParamSet, 104 const IDMParams &testIDMParams); 105 106 int32_t AuthTokenImportKey(); 107 108 int32_t AuthTokenSign(struct HksBlob *challenge, const IDMParams &testIDMParams, std::vector<uint8_t>& token); 109 110 int32_t AuthTokenMac(const struct HksBlob *keyAlias, const struct HksBlob *inData, HksUserAuthToken *authTokenHal); 111 112 int32_t HksBuildAuthtoken(const struct HksParamSet *initParamSet, uint8_t *authChallenge, 113 const IDMParams &testIDMParams); 114 115 int32_t HksBuildAuthTokenSecure(struct HksParamSet *paramSet, 116 struct HksTestGenAuthTokenParams *genAuthTokenParams, struct HksParamSet **outParamSet); 117 118 int32_t ConstructRsaKeyPair(const struct HksBlob *nDataBlob, const struct HksBlob *dDataBlob, 119 const struct HksBlob *eDataBlob, uint32_t keySize, struct HksBlob *outKey); 120 121 int32_t ConstructEd25519KeyPair(uint32_t keySize, uint32_t alg, struct HksBlob *ed25519PubData, 122 struct HksBlob *ed25519PrivData, struct HksBlob *outKey); 123 124 int32_t ConstructDsaKeyPair(uint32_t keySize, const struct TestDsaKeyParams *params, struct HksBlob *outKey); 125 126 int32_t GenParamSetAuthTest(struct HksParamSet **paramOutSet, const struct HksParamSet *genParamSet); 127 } 128 #endif // HKS_THREE_STAGE_TEST_COMMON_H