/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "huks_signverify_dsa_test.h" #include using namespace testing::ext; namespace Unittest::DsaSignVerify { bool useSoftware = true; class HuksSignVerifyDSATest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(void); void SetUp(); void TearDown(); }; void HuksSignVerifyDSATest::SetUpTestCase(void) { useSoftware = checkUseSoftware(); } void HuksSignVerifyDSATest::TearDownTestCase(void) { } void HuksSignVerifyDSATest::SetUp() { } void HuksSignVerifyDSATest::TearDown() { } static struct OH_Huks_Param g_genParamsTest001[] = { { .tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_DSA }, { .tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN | OH_HUKS_KEY_PURPOSE_VERIFY }, { .tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = DSA_COMMON_SIZE }, { .tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA1 } }; static struct OH_Huks_Param g_signParamsTest001[] = { { .tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_DSA }, { .tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_SIGN }, { .tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA1 } }; static struct OH_Huks_Param g_verifyParamsTest001[] = { { .tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_DSA }, { .tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_VERIFY }, { .tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = DSA_COMMON_SIZE }, { .tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA1 } }; OH_Huks_Result HksTestSignVerify(struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData, bool isSign) { uint8_t tmpHandle[sizeof(uint64_t)] = {0}; struct OH_Huks_Blob handle = { sizeof(uint64_t), tmpHandle }; OH_Huks_Result ret = OH_Huks_InitSession(keyAlias, paramSet, &handle, nullptr); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed."; if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) { return ret; } struct OH_Huks_Param *tmpParam = NULL; ret = OH_Huks_GetParam(paramSet, OH_HUKS_TAG_PURPOSE, &tmpParam); if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) { return ret; } ret = TestUpdateFinish(&handle, paramSet, tmpParam->uint32Param, inData, outData); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "TestUpdateFinish failed."; if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) { return ret; } return ret; } OH_Huks_Result HksDsaSignVerifyTestNormalCase(struct OH_Huks_Blob keyAlias, struct OH_Huks_ParamSet *genParamSet, struct OH_Huks_ParamSet *signParamSet, struct OH_Huks_ParamSet *verifyParamSet) { struct OH_Huks_Blob inData = { g_inData.length(), (uint8_t *)g_inData.c_str() }; /* 1. Generate Key */ // Generate Key OH_Huks_Result ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed."; /* 2. Sign Three Stage */ uint8_t outDataS[DSA_COMMON_SIZE] = {0}; struct OH_Huks_Blob outDataSign = { DSA_COMMON_SIZE, outDataS }; ret = HksTestSignVerify(&keyAlias, signParamSet, &inData, &outDataSign, true); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Sign failed."; /* 3. Export Public Key */ uint8_t pubKey[DSA_COMMON_SIZE] = {0}; struct OH_Huks_Blob publicKey = { DSA_COMMON_SIZE, pubKey }; ret = OH_Huks_ExportPublicKeyItem(&keyAlias, genParamSet, &publicKey); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportPublicKey failed."; /* 4. Import Key */ char newKey[] = "DSA_Sign_Verify_Import_KeyAlias"; struct OH_Huks_Blob newKeyAlias = { .size = strlen(newKey), .data = (uint8_t *)newKey }; ret = OH_Huks_ImportKeyItem(&newKeyAlias, verifyParamSet, &publicKey); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ImportKey failed"; /* 5. Verify Three Stage */ ret = HksTestSignVerify(&newKeyAlias, verifyParamSet, &inData, &outDataSign, false); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Verify failed."; /* 6. Delete New Key */ ret = OH_Huks_DeleteKeyItem(&newKeyAlias, verifyParamSet); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Delete ImportKey failed."; return ret; } /** * @tc.name: HuksSignVerifyDSATest.Security_HUKS_NAPI_SignVerify_DSA_0100 * @tc.desc: alg-DSA pur-Sign-verify dig-SHA1 * @tc.type: FUNC */ HWTEST_F(HuksSignVerifyDSATest, Security_HUKS_NAPI_SignVerify_DSA_0100, TestSize.Level0) { if (useSoftware) { const char *keyAliasString = "HksDSASignVerifyKeyAliasTest001"; struct OH_Huks_ParamSet *genParamSet = nullptr; struct OH_Huks_ParamSet *signParamSet = nullptr; struct OH_Huks_ParamSet *verifyParamSet = nullptr; struct OH_Huks_Blob keyAlias = { strlen(keyAliasString), (uint8_t *)keyAliasString }; OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParamsTest001, sizeof(g_genParamsTest001) / sizeof(OH_Huks_Param)); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed."; ret = InitParamSet(&signParamSet, g_signParamsTest001, sizeof(g_signParamsTest001) / sizeof(OH_Huks_Param)); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed."; ret = InitParamSet(&verifyParamSet, g_verifyParamsTest001, sizeof(g_verifyParamsTest001) / sizeof(OH_Huks_Param)); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed."; if ((genParamSet != nullptr) || (signParamSet != nullptr) || (verifyParamSet != nullptr)) { ret = HksDsaSignVerifyTestNormalCase(keyAlias, genParamSet, signParamSet, verifyParamSet); } /* 5. Delete Key */ ret = OH_Huks_DeleteKeyItem(&keyAlias, genParamSet); EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed."; OH_Huks_FreeParamSet(&genParamSet); OH_Huks_FreeParamSet(&signParamSet); OH_Huks_FreeParamSet(&verifyParamSet); } ASSERT_TRUE(0 == 0); } } // namespace Unittest::DsaSignVerify