未验证 提交 d7b1a5eb 编写于 作者: O openharmony_ci 提交者: Gitee

!9531 【OpenHarmony 4.0.9.1】【安全子系统】【master】Delete Huks Use Case

Merge pull request !9531 from qiaozzzh/07271_1
/*
* Copyright (C) 2021 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.
*/
#ifndef HUKS_MODIFY_KEY_TEST_H
#define HUKS_MODIFY_KEY_TEST_H
int HksModifyKeyTest001(void);
#endif
/*
* Copyright (C) 2021 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.
*/
#ifndef HUKS_SAFE_CIPHER_KEY_TEST_H
#define HUKS_SAFE_CIPHER_KEY_TEST_H
int HksSafeCipherKeyTest001(void);
#endif
/*
* Copyright (C) 2021 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.
*/
#ifndef HUKS_SAFE_COMPARE_KEY_TEST_H
#define HUKS_SAFE_COMPARE_KEY_TEST_H
int HksSafeCompareKeyTest001(void);
#endif
...@@ -33,10 +33,7 @@ hcpptest_suite("ActsHuksLiteFunctionTest") { ...@@ -33,10 +33,7 @@ hcpptest_suite("ActsHuksLiteFunctionTest") {
"hks_generate_random_test.cpp", "hks_generate_random_test.cpp",
"hks_hash_test.cpp", "hks_hash_test.cpp",
"hks_mac_test.cpp", "hks_mac_test.cpp",
"hks_modify_key_test.cpp",
"hks_others_test.cpp", "hks_others_test.cpp",
"hks_safe_cipher_key_test.cpp",
"hks_safe_compare_key_test.cpp",
] ]
include_dirs = [ include_dirs = [
......
/*
* Copyright (C) 2021 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 <gtest/gtest.h>
#include "hks_modify_key_test.h"
#include "hks_api.h"
#include "hks_param.h"
#include "hks_test_api_performance.h"
#include "hks_test_cipher.h"
#include "hks_test_common.h"
#include "hks_test_file_operator.h"
#include "hks_test_log.h"
#include "hks_test_mem.h"
#include "securec.h"
using namespace testing::ext;
namespace {
#ifndef _CUT_AUTHENTICATE_
class HksModifyKeyTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void HksModifyKeyTest::SetUpTestCase(void)
{
}
void HksModifyKeyTest::TearDownTestCase(void)
{
}
void HksModifyKeyTest::SetUp()
{
EXPECT_EQ(HksInitialize(), 0);
}
void HksModifyKeyTest::TearDown()
{
}
const int DEFAULT_AES_CIPHER_PLAIN_SIZE = 1000;
const int AES_DEFAULT_GCM_NONCE_LENGTH = 12;
const int AES_DEFAULT_AAD_LEN = 4;
const char *g_storePath = "/storage/data/service/el1/public/huks_service/maindata/+0+0+0+0/key";
const char *g_testName = "TestName";
const struct HksTestCipherParams g_testCipherParams[] = {
/* success: aes256-gcm-none */
{ 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
{
true, /* genKey params */
true, HKS_ALG_AES,
true, HKS_AES_KEY_SIZE_256,
true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT,
false, 0,
true, HKS_PADDING_NONE,
true, HKS_MODE_GCM,
false, 0
},
{ false, 0 },
{
HKS_TEST_CIPHER_TYPE_AES, true, /* encrypt params */
true, HKS_ALG_AES,
true, HKS_KEY_PURPOSE_ENCRYPT,
false, 0,
true, HKS_PADDING_NONE,
true, HKS_MODE_GCM,
false, 0,
true, AES_DEFAULT_GCM_NONCE_LENGTH,
true, AES_DEFAULT_AAD_LEN
},
{
HKS_TEST_CIPHER_TYPE_AES, true, /* decrypt params */
true, HKS_ALG_AES,
true, HKS_KEY_PURPOSE_DECRYPT,
false, 0,
true, HKS_PADDING_NONE,
true, HKS_MODE_GCM,
false, 0,
true, AES_DEFAULT_GCM_NONCE_LENGTH,
true, AES_DEFAULT_AAD_LEN
},
{ true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE },
{ true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 },
{ true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE },
{ false, 0, false, 0 }
},
};
static int32_t ConstructDataToBlob(struct HksBlob **inData, struct HksBlob **outData,
const struct HksTestBlobParams *inTextParams, const struct HksTestBlobParams *outTextParams)
{
int32_t ret = TestConstuctBlob(inData,
inTextParams->blobExist,
inTextParams->blobSize,
inTextParams->blobDataExist,
inTextParams->blobDataSize);
HKS_TEST_ASSERT(ret == 0);
ret = TestConstuctBlob(outData,
outTextParams->blobExist,
outTextParams->blobSize,
outTextParams->blobDataExist,
outTextParams->blobDataSize);
HKS_TEST_ASSERT(ret == 0);
return ret;
}
static int32_t Encrypt(struct CipherEncryptStructure *encryptStruct)
{
int32_t ret;
struct HksParamSet *encryptParamSet = NULL;
uint32_t ivSize = encryptStruct->cipherParms->ivSize;
uint32_t nonceSize = encryptStruct->cipherParms->nonceSize;
uint32_t aadSize = encryptStruct->cipherParms->aadSize;
if (ivSize != 0) {
ret = TestConstuctBlob(encryptStruct->ivData, true, ivSize, true, ivSize);
HKS_TEST_ASSERT(ret == 0);
}
if (nonceSize != 0) {
ret = TestConstuctBlob(encryptStruct->nonceData, true, nonceSize, true, nonceSize);
HKS_TEST_ASSERT(ret == 0);
}
if (aadSize != 0) {
ret = TestConstuctBlob(encryptStruct->aadData, true, aadSize, true, aadSize);
HKS_TEST_ASSERT(ret == 0);
}
struct AesCipherParamSetStructure enParamStruct = {
&encryptParamSet,
encryptStruct->cipherParms->paramSetExist,
encryptStruct->cipherParms->setAlg, encryptStruct->cipherParms->alg,
encryptStruct->cipherParms->setPurpose, encryptStruct->cipherParms->purpose,
encryptStruct->cipherParms->setPadding, encryptStruct->cipherParms->padding,
encryptStruct->cipherParms->setBlockMode, encryptStruct->cipherParms->mode,
encryptStruct->cipherParms->setIv, *(encryptStruct->ivData),
encryptStruct->cipherParms->setNonce, *(encryptStruct->nonceData),
encryptStruct->cipherParms->setAad, *(encryptStruct->aadData),
encryptStruct->cipherParms->setIsKeyAlias, encryptStruct->cipherParms->isKeyAlias
};
ret = TestConstructAesCipherParamSet(&enParamStruct);
HKS_TEST_ASSERT(ret == 0);
ret = HksEncryptRun(encryptStruct->keyAlias, encryptParamSet, encryptStruct->plainData, encryptStruct->cipherData,
encryptStruct->performTimes);
HksFreeParamSet(&encryptParamSet);
return ret;
}
static int32_t DecryptCipher(struct CipherDecryptStructure *decryptStruct)
{
int32_t ret = TestConstuctBlob(decryptStruct->decryptedData,
decryptStruct->cipherParms->decryptedTextParams.blobExist,
decryptStruct->cipherParms->decryptedTextParams.blobSize,
decryptStruct->cipherParms->decryptedTextParams.blobDataExist,
decryptStruct->cipherParms->decryptedTextParams.blobDataSize);
HKS_TEST_ASSERT(ret == 0);
struct HksParamSet *decryptParamSet = NULL;
struct AesCipherParamSetStructure deParamStruct = {
&decryptParamSet,
decryptStruct->cipherParms->decryptParamSetParams.paramSetExist,
decryptStruct->cipherParms->decryptParamSetParams.setAlg,
decryptStruct->cipherParms->decryptParamSetParams.alg,
decryptStruct->cipherParms->decryptParamSetParams.setPurpose,
decryptStruct->cipherParms->decryptParamSetParams.purpose,
decryptStruct->cipherParms->decryptParamSetParams.setPadding,
decryptStruct->cipherParms->decryptParamSetParams.padding,
decryptStruct->cipherParms->decryptParamSetParams.setBlockMode,
decryptStruct->cipherParms->decryptParamSetParams.mode,
decryptStruct->cipherParms->decryptParamSetParams.setIv, decryptStruct->ivData,
decryptStruct->cipherParms->decryptParamSetParams.setNonce, decryptStruct->nonceData,
decryptStruct->cipherParms->decryptParamSetParams.setAad, decryptStruct->aadData,
decryptStruct->cipherParms->decryptParamSetParams.setIsKeyAlias,
decryptStruct->cipherParms->decryptParamSetParams.isKeyAlias
};
ret = TestConstructAesCipherParamSet(&deParamStruct);
HKS_TEST_ASSERT(ret == 0);
ret = HksDecryptRun(decryptStruct->keyAlias, decryptParamSet, decryptStruct->cipherData,
*(decryptStruct->decryptedData), decryptStruct->performTimes);
HksFreeParamSet(&decryptParamSet);
return ret;
}
int32_t GenerateKeyTwo(struct HksBlob *keyAlias, const struct HksTestBlobParams *keyAliasParams,
const struct HksTestGenKeyParamsParamSet *genKeyParamSetParams,
const struct HksTestGenKeyParamsParamSetOut *genKeyParamSetParamsOut)
{
struct HksParamSet *paramSet = NULL;
struct GenerateKeyParamSetStructure paramStruct = {
&paramSet,
genKeyParamSetParams->paramSetExist,
genKeyParamSetParams->setAlg, genKeyParamSetParams->alg,
genKeyParamSetParams->setKeySize, genKeyParamSetParams->keySize,
genKeyParamSetParams->setPurpose, genKeyParamSetParams->purpose,
genKeyParamSetParams->setDigest, genKeyParamSetParams->digest,
genKeyParamSetParams->setPadding, genKeyParamSetParams->padding,
genKeyParamSetParams->setBlockMode, genKeyParamSetParams->mode,
genKeyParamSetParams->setKeyStorageFlag, genKeyParamSetParams->keyStorageFlag
};
int32_t ret = TestConstructGenerateKeyParamSet(&paramStruct);
HKS_TEST_ASSERT(ret == 0);
struct HksParamSet *paramSetOut = NULL;
if (genKeyParamSetParamsOut != NULL) {
ret = TestConstructGenerateKeyParamSetOut(&paramSet,
genKeyParamSetParamsOut->paramSetExist, genKeyParamSetParamsOut->paramSetSize);
HKS_TEST_ASSERT(ret == 0);
}
ret = HksGenerateKey(keyAlias, paramSet, paramSetOut);
HKS_TEST_ASSERT(ret == 0);
HksFreeParamSet(&paramSet);
return ret;
}
static int32_t ModifyKey(struct HksBlob *keyAlias)
{
uint32_t sizeOne = HksTestFileSize(g_storePath, (char *)keyAlias->data);
uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne);
if (bufOne == NULL) {
return HKS_ERROR_MALLOC_FAIL;
}
uint32_t sizeRead = HksTestFileRead(g_storePath, (char *)keyAlias->data, 0, bufOne, sizeOne);
(void)memset_s(bufOne, sizeRead, 0, sizeRead);
int32_t ret = HksTestFileWrite(g_storePath, (char *)keyAlias->data, 0, bufOne, sizeOne);
HksTestFree(bufOne);
return ret;
}
int32_t BaseTestCipherProcess(struct HksBlob *keyAlias, uint32_t index)
{
struct HksBlob *plainData = NULL;
struct HksBlob *cipherData = NULL;
int32_t ret = ConstructDataToBlob(&plainData, &cipherData,
&g_testCipherParams[index].plainTextParams, &g_testCipherParams[index].cipherTextParams);
HKS_TEST_ASSERT(ret == 0);
struct HksBlob *ivData = NULL;
struct HksBlob *nonceData = NULL;
struct HksBlob *aadData = NULL;
struct HksBlob *decryptedData = NULL;
/* 2. encrypt */
do {
struct CipherEncryptStructure testEncryptStruct = {
keyAlias, &g_testCipherParams[index].encryptParamSetParams,
plainData, cipherData, &ivData, &nonceData, &aadData, 1
};
ret = Encrypt(&testEncryptStruct);
if (ret != g_testCipherParams[index].expectResult) {
break;
}
/* 3. decrypt */
struct CipherDecryptStructure testDecryptStruct = {
keyAlias, &g_testCipherParams[index], cipherData,
&decryptedData, ivData, nonceData, aadData, 1
};
ret = DecryptCipher(&testDecryptStruct);
if (ret != g_testCipherParams[index].expectResult) {
break;
}
if (ret == g_testCipherParams[index].expectResult) {
if (plainData->size != decryptedData->size) {
break;
};
ret = memcmp(plainData->data, decryptedData->data, plainData->size);
}
} while (0);
TestFreeBlob(&plainData);
TestFreeBlob(&cipherData);
TestFreeBlob(&decryptedData);
TestFreeBlob(&ivData);
TestFreeBlob(&nonceData);
TestFreeBlob(&aadData);
return ret;
}
/**
* @tc.name: HksModifyKeyTest.HksModifyKeyTest001
* @tc.desc: The static function will return true;
* @tc.type: FUNC
*/
HWTEST_F(HksModifyKeyTest, HksModifyKeyTest001, TestSize.Level1)
{
uint32_t index = 0;
struct HksBlob keyAlias = { strlen(g_testName), (uint8_t *)g_testName };
int32_t ret = GenerateKeyTwo(&keyAlias, &g_testCipherParams[index].keyAliasParams,
&g_testCipherParams[index].genKeyParamSetParams, &g_testCipherParams[index].genKeyParamSetParamsOut);
EXPECT_EQ(ret, 0);
ret = BaseTestCipherProcess(&keyAlias, 0);
EXPECT_EQ(ret, 0);
struct HksBlob *plainData = NULL;
struct HksBlob *cipherData = NULL;
ret = ConstructDataToBlob(&plainData, &cipherData,
&g_testCipherParams[index].plainTextParams, &g_testCipherParams[index].cipherTextParams);
EXPECT_EQ(ret, 0);
struct HksBlob *ivData = NULL;
struct HksBlob *nonceData = NULL;
struct HksBlob *aadData = NULL;
/* 2. encrypt */
struct CipherEncryptStructure testEncryptStruct = {
&keyAlias, &g_testCipherParams[index].encryptParamSetParams,
plainData, cipherData, &ivData, &nonceData, &aadData, 1
};
ret = Encrypt(&testEncryptStruct);
EXPECT_EQ(ret, 0);
ret = ModifyKey(&keyAlias);
EXPECT_EQ(ret, 0);
/* 3. decrypt */
struct HksBlob *decryptedData = NULL;
struct CipherDecryptStructure testDecryptStruct = {
&keyAlias, &g_testCipherParams[index], cipherData,
&decryptedData, ivData, nonceData, aadData, 1
};
ret = DecryptCipher(&testDecryptStruct);
HKS_TEST_ASSERT(ret != g_testCipherParams[index].expectResult);
TestFreeBlob(&plainData);
TestFreeBlob(&cipherData);
TestFreeBlob(&decryptedData);
TestFreeBlob(&ivData);
TestFreeBlob(&nonceData);
TestFreeBlob(&aadData);
EXPECT_NE(ret, 0);
}
#endif /* _CUT_AUTHENTICATE_ */
}
/*
* Copyright (C) 2021 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 <gtest/gtest.h>
#include "hks_safe_cipher_key_test.h"
#include "hks_api.h"
#include "hks_param.h"
#include "hks_test_curve25519.h"
#include "hks_test_file_operator.h"
#include "hks_test_mem.h"
#include "securec.h"
using namespace testing::ext;
namespace {
#ifndef _CUT_AUTHENTICATE_
class HksSafeCipherKeyTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void HksSafeCipherKeyTest::SetUpTestCase(void)
{
}
void HksSafeCipherKeyTest::TearDownTestCase(void)
{
}
void HksSafeCipherKeyTest::SetUp()
{
EXPECT_EQ(HksInitialize(), 0);
}
void HksSafeCipherKeyTest::TearDown()
{
}
const char *g_testEd25519 = "test_ed25519";
/**
* @tc.name: HksSafeCipherKeyTest.HksSafeCipherKeyTest001
* @tc.desc: The static function will return true;
* @tc.type: FUNC
*/
HWTEST_F(HksSafeCipherKeyTest, HksSafeCipherKeyTest001, TestSize.Level1)
{
struct HksBlob ed25519Alias = { strlen(g_testEd25519), (uint8_t *)g_testEd25519 };
int32_t ret = TestGenerateEd25519Key(ed25519Alias);
uint8_t pubKey[32] = {0};
uint32_t pubKeyLen = 32;
struct HksBlob pubKeyInfo = { pubKeyLen, pubKey };
ret = HksExportPublicKey(&ed25519Alias, NULL, &pubKeyInfo);
EXPECT_EQ(ret, 0);
ret = HksDeleteKey(&ed25519Alias, NULL);
EXPECT_EQ(ret, 0);
struct HksBlob newAliasOne = { strlen("test_ed25519_1"), (uint8_t *)"test_ed25519_1" };
ret = TestImportEd25519(newAliasOne, &pubKeyInfo);
EXPECT_EQ(ret, 0);
struct HksBlob newAliasTwo = { strlen("test_ed25519_2"), (uint8_t *)"test_ed25519_2" };
ret = TestImportEd25519(newAliasTwo, &pubKeyInfo);
EXPECT_EQ(ret, 0);
struct HksBlob X25519PubKey1 = { .size = HKS_CURVE25519_KEY_SIZE_256, .data = nullptr};
struct HksBlob X25519PubKey2 = { .size = HKS_CURVE25519_KEY_SIZE_256, .data = nullptr};
X25519PubKey1.data = (uint8_t *)malloc(X25519PubKey1.size);
X25519PubKey2.data = (uint8_t *)malloc(X25519PubKey2.size);
ret = HksExportPublicKey(&newAliasOne, nullptr, &X25519PubKey1);
EXPECT_EQ(ret, 0)<<"export alies1 fail";
ret = HksExportPublicKey(&newAliasTwo, nullptr, &X25519PubKey2);
EXPECT_EQ(ret, 0)<<"export alies2 fail";
ret = memcmp(X25519PubKey1.data, X25519PubKey2.data, HKS_CURVE25519_KEY_SIZE_256);
EXPECT_NE(ret, 0);
ret = HksDeleteKey(&newAliasOne, nullptr);
EXPECT_EQ(ret, 0);
ret = HksDeleteKey(&newAliasTwo, nullptr);
EXPECT_EQ(ret, 0);
}
#endif /* _CUT_AUTHENTICATE_ */
}
\ No newline at end of file
/*
* Copyright (C) 2021 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 <gtest/gtest.h>
#include "hks_safe_compare_key_test.h"
#include "hks_api.h"
#include "hks_param.h"
#include "hks_test_api_performance.h"
#include "hks_test_common.h"
#include "hks_test_file_operator.h"
#include "hks_test_log.h"
#include "hks_test_mem.h"
using namespace testing::ext;
namespace {
#ifndef _CUT_AUTHENTICATE_
class HksSafeCompareKeyTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void HksSafeCompareKeyTest::SetUpTestCase(void)
{
}
void HksSafeCompareKeyTest::TearDownTestCase(void)
{
}
void HksSafeCompareKeyTest::SetUp()
{
EXPECT_EQ(HksInitialize(), 0);
}
void HksSafeCompareKeyTest::TearDown()
{
}
const char *g_storePath = "/storage/data/service/el1/public/huks_service/maindata/+0+0+0+0/key";
const char *g_testOne = "TestOne";
const char *g_testTwo = "TestTwo";
const struct HksTestGenKeyParams g_testGenKeyParams[] = {
/* x25519: tee sign/verify */
{ 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
{
true,
true, HKS_ALG_X25519,
true, HKS_CURVE25519_KEY_SIZE_256,
true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY,
false, 0,
false, 0,
false, 0,
false, 0
},
{ false, 0 },
},
};
static int32_t SafeTestGenerateKey(struct HksBlob *keyAlias)
{
uint32_t index = 0;
uint32_t performTimes = 1;
struct HksParamSet *paramSet = NULL;
struct GenerateKeyParamSetStructure paramStruct = {
&paramSet,
g_testGenKeyParams[index].paramSetParams.paramSetExist,
g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg,
g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize,
g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose,
g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest,
g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding,
g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode,
g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag,
g_testGenKeyParams[index].paramSetParams.keyStorageFlag
};
int32_t ret = TestConstructGenerateKeyParamSet(&paramStruct);
HKS_TEST_ASSERT(ret == 0);
struct HksParamSet *paramSetOut = NULL;
ret = TestConstructGenerateKeyParamSetOut(&paramSetOut,
g_testGenKeyParams[index].paramSetParamsOut.paramSetExist,
g_testGenKeyParams[index].paramSetParamsOut.paramSetSize);
HKS_TEST_ASSERT(ret == 0);
ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes);
if (ret != g_testGenKeyParams[index].expectResult) {
HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret);
}
EXPECT_EQ(ret, g_testGenKeyParams[index].expectResult);
if (ret == g_testGenKeyParams[index].expectResult) {
ret = 0;
} else {
ret = 1;
}
HksFreeParamSet(&paramSet);
HksFreeParamSet(&paramSetOut);
return ret;
}
static int32_t CompareKeyData(struct HksBlob *keyAliasOne, struct HksBlob *keyAliasTwo)
{
uint32_t sizeOne = HksTestFileSize(g_storePath, (char *)keyAliasOne->data);
uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne);
if (bufOne == NULL) {
return HKS_ERROR_MALLOC_FAIL;
}
uint32_t sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasOne->data, 0, bufOne, sizeOne);
uint32_t sizeTwo = HksTestFileSize(g_storePath, (char *)keyAliasTwo->data);
uint8_t *bufTwo = (uint8_t *)HksTestMalloc(sizeTwo);
if (bufTwo == NULL) {
HksTestFree(bufOne);
return HKS_ERROR_MALLOC_FAIL;
}
sizeRead = HksTestFileRead(g_storePath, (char *)keyAliasTwo->data, 0, bufTwo, sizeOne);
int32_t ret = memcmp(bufOne, bufTwo, sizeOne);
HksTestFree(bufOne);
HksTestFree(bufTwo);
return ret;
}
/**
* @tc.name: HksSafeCompareKeyTest.HksSafeCompareKeyTest001
* @tc.desc: The static function will return true;
* @tc.type: FUNC
*/
HWTEST_F(HksSafeCompareKeyTest, HksSafeCompareKeyTest001, TestSize.Level1)
{
struct HksBlob keyAliasOne = { strlen(g_testOne), (uint8_t *)g_testOne };
int32_t ret = SafeTestGenerateKey(&keyAliasOne);
EXPECT_EQ(ret, 0);
struct HksBlob keyAliasTwo = { strlen(g_testTwo), (uint8_t *)g_testTwo };
ret = SafeTestGenerateKey(&keyAliasTwo);
EXPECT_EQ(ret, 0);
ret = CompareKeyData(&keyAliasOne, &keyAliasTwo);
EXPECT_NE(ret, 0);
}
#endif /* _CUT_AUTHENTICATE_ */
}
\ No newline at end of file
...@@ -31,10 +31,7 @@ hctest_suite("ActsHuksHalFunctionTest") { ...@@ -31,10 +31,7 @@ hctest_suite("ActsHuksHalFunctionTest") {
"hks_generate_random_test.c", "hks_generate_random_test.c",
"hks_hash_test.c", "hks_hash_test.c",
"hks_mac_test.c", "hks_mac_test.c",
"hks_modify_key_test.c",
"hks_others_test.c", "hks_others_test.c",
"hks_safe_cipher_key_test.c",
"hks_safe_compare_key_test.c",
] ]
include_dirs = [ include_dirs = [
"//base/security/huks/interfaces/inner_api/huks_standard/main/include", "//base/security/huks/interfaces/inner_api/huks_standard/main/include",
......
/*
* Copyright (C) 2021 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.
*/
#ifndef _CUT_AUTHENTICATE_
#include "hks_modify_key_test.h"
#include <hctest.h>
#include <unistd.h>
#include "hks_api.h"
#include "hks_param.h"
#include "hks_test_api_performance.h"
#include "hks_test_cipher.h"
#include "hks_test_common.h"
#include "hks_test_log.h"
#include "stdlib.h"
#include "cmsis_os2.h"
#include "ohos_types.h"
#define DEFAULT_AES_CIPHER_PLAIN_SIZE 1000
#define AES_DEFAULT_GCM_NONCE_LENGTH 12
#define AES_DEFAULT_AAD_LEN 4
static const char *g_storePath = "./hks_store/";
static const char *g_testName = "TestName";
#define TEST_TASK_STACK_SIZE 0x2000
#define WAIT_TO_TEST_DONE 4
static osPriority_t g_setPriority;
static const struct HksTestCipherParams g_testCipherParams[] = {
/* success: aes256-gcm-none */
{ 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
{
true, /* genKey params */
true, HKS_ALG_AES,
true, HKS_AES_KEY_SIZE_256,
true, HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT,
false, 0,
true, HKS_PADDING_NONE,
true, HKS_MODE_GCM,
false, 0
},
{ false, 0 },
{
HKS_TEST_CIPHER_TYPE_AES, true, /* encrypt params */
true, HKS_ALG_AES,
true, HKS_KEY_PURPOSE_ENCRYPT,
false, 0,
true, HKS_PADDING_NONE,
true, HKS_MODE_GCM,
false, 0,
true, AES_DEFAULT_GCM_NONCE_LENGTH,
true, AES_DEFAULT_AAD_LEN
},
{
HKS_TEST_CIPHER_TYPE_AES, true, /* decrypt params */
true, HKS_ALG_AES,
true, HKS_KEY_PURPOSE_DECRYPT,
false, 0,
true, HKS_PADDING_NONE,
true, HKS_MODE_GCM,
false, 0,
true, AES_DEFAULT_GCM_NONCE_LENGTH,
true, AES_DEFAULT_AAD_LEN
},
{ true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE },
{ true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16, true, DEFAULT_AES_CIPHER_PLAIN_SIZE + 16 },
{ true, DEFAULT_AES_CIPHER_PLAIN_SIZE, true, DEFAULT_AES_CIPHER_PLAIN_SIZE },
{ false, 0, false, 0 }
},
};
/*
* @tc.register: register a test suit named "CalcMultiTest"
* @param: test subsystem name
* @param: c_example module name
* @param: CalcMultiTest test suit name
*/
LITE_TEST_SUIT(security, securityData, HksModifyKeyTest);
static void ExecHksInitialize(void const *argument)
{
LiteTestPrint("HksInitialize Begin!\n");
TEST_ASSERT_TRUE(HksInitialize() == 0);
LiteTestPrint("HksInitialize End!\n");
osThreadExit();
}
/**
* @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
* @return: true——setup success
*/
static BOOL HksModifyKeyTestSetUp()
{
LiteTestPrint("setup\n");
osThreadId_t id;
osThreadAttr_t attr;
g_setPriority = osPriorityAboveNormal6;
attr.name = "test";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = TEST_TASK_STACK_SIZE;
attr.priority = g_setPriority;
id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
sleep(WAIT_TO_TEST_DONE);
LiteTestPrint("HksModifyKeyTestSetUp End2!\n");
return TRUE;
}
static void HksTestRemoveFile()
{
}
/**
* @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
* @return: true——teardown success
*/
static BOOL HksModifyKeyTestTearDown()
{
LiteTestPrint("tearDown\n");
HksTestRemoveFile();
return TRUE;
}
static int32_t ConstructDataToBlob(struct HksBlob **inData, struct HksBlob **outData,
const struct HksTestBlobParams *inTextParams, const struct HksTestBlobParams *outTextParams)
{
int32_t ret = TestConstuctBlob(inData,
inTextParams->blobExist,
inTextParams->blobSize,
inTextParams->blobDataExist,
inTextParams->blobDataSize);
HKS_TEST_ASSERT(ret == 0);
ret = TestConstuctBlob(outData,
outTextParams->blobExist,
outTextParams->blobSize,
outTextParams->blobDataExist,
outTextParams->blobDataSize);
HKS_TEST_ASSERT(ret == 0);
return ret;
}
static int32_t Encrypt(struct CipherEncryptStructure *encryptStruct)
{
int32_t ret;
struct HksParamSet *encryptParamSet = NULL;
uint32_t ivSize = encryptStruct->cipherParms->ivSize;
uint32_t nonceSize = encryptStruct->cipherParms->nonceSize;
uint32_t aadSize = encryptStruct->cipherParms->aadSize;
if (ivSize != 0) {
ret = TestConstuctBlob(encryptStruct->ivData, true, ivSize, true, ivSize);
HKS_TEST_ASSERT(ret == 0);
}
if (nonceSize != 0) {
ret = TestConstuctBlob(encryptStruct->nonceData, true, nonceSize, true, nonceSize);
HKS_TEST_ASSERT(ret == 0);
}
if (aadSize != 0) {
ret = TestConstuctBlob(encryptStruct->aadData, true, aadSize, true, aadSize);
HKS_TEST_ASSERT(ret == 0);
}
struct AesCipherParamSetStructure enParamStruct = {
&encryptParamSet,
encryptStruct->cipherParms->paramSetExist,
encryptStruct->cipherParms->setAlg, encryptStruct->cipherParms->alg,
encryptStruct->cipherParms->setPurpose, encryptStruct->cipherParms->purpose,
encryptStruct->cipherParms->setPadding, encryptStruct->cipherParms->padding,
encryptStruct->cipherParms->setBlockMode, encryptStruct->cipherParms->mode,
encryptStruct->cipherParms->setIv, *(encryptStruct->ivData),
encryptStruct->cipherParms->setNonce, *(encryptStruct->nonceData),
encryptStruct->cipherParms->setAad, *(encryptStruct->aadData),
encryptStruct->cipherParms->setIsKeyAlias, encryptStruct->cipherParms->isKeyAlias
};
ret = TestConstructAesCipherParamSet(&enParamStruct);
HKS_TEST_ASSERT(ret == 0);
ret = HksEncryptRun(encryptStruct->keyAlias, encryptParamSet, encryptStruct->plainData, encryptStruct->cipherData,
encryptStruct->performTimes);
HksFreeParamSet(&encryptParamSet);
return ret;
}
static int32_t DecryptCipher(struct CipherDecryptStructure *decryptStruct)
{
int32_t ret = TestConstuctBlob(decryptStruct->decryptedData,
decryptStruct->cipherParms->decryptedTextParams.blobExist,
decryptStruct->cipherParms->decryptedTextParams.blobSize,
decryptStruct->cipherParms->decryptedTextParams.blobDataExist,
decryptStruct->cipherParms->decryptedTextParams.blobDataSize);
HKS_TEST_ASSERT(ret == 0);
struct HksParamSet *decryptParamSet = NULL;
struct AesCipherParamSetStructure deParamStruct = {
&decryptParamSet,
decryptStruct->cipherParms->decryptParamSetParams.paramSetExist,
decryptStruct->cipherParms->decryptParamSetParams.setAlg,
decryptStruct->cipherParms->decryptParamSetParams.alg,
decryptStruct->cipherParms->decryptParamSetParams.setPurpose,
decryptStruct->cipherParms->decryptParamSetParams.purpose,
decryptStruct->cipherParms->decryptParamSetParams.setPadding,
decryptStruct->cipherParms->decryptParamSetParams.padding,
decryptStruct->cipherParms->decryptParamSetParams.setBlockMode,
decryptStruct->cipherParms->decryptParamSetParams.mode,
decryptStruct->cipherParms->decryptParamSetParams.setIv, decryptStruct->ivData,
decryptStruct->cipherParms->decryptParamSetParams.setNonce, decryptStruct->nonceData,
decryptStruct->cipherParms->decryptParamSetParams.setAad, decryptStruct->aadData,
decryptStruct->cipherParms->decryptParamSetParams.setIsKeyAlias,
decryptStruct->cipherParms->decryptParamSetParams.isKeyAlias
};
ret = TestConstructAesCipherParamSet(&deParamStruct);
HKS_TEST_ASSERT(ret == 0);
ret = HksDecryptRun(decryptStruct->keyAlias, decryptParamSet, decryptStruct->cipherData,
*(decryptStruct->decryptedData), decryptStruct->performTimes);
HksFreeParamSet(&decryptParamSet);
return ret;
}
int32_t GenerateKeyTwo(const struct HksBlob *keyAlias, const struct HksTestBlobParams *keyAliasParams,
const struct HksTestGenKeyParamsParamSet *genKeyParamSetParams,
const struct HksTestGenKeyParamsParamSetOut *genKeyParamSetParamsOut)
{
struct HksParamSet *paramSet = NULL;
struct GenerateKeyParamSetStructure paramStruct = {
&paramSet,
genKeyParamSetParams->paramSetExist,
genKeyParamSetParams->setAlg, genKeyParamSetParams->alg,
genKeyParamSetParams->setKeySize, genKeyParamSetParams->keySize,
genKeyParamSetParams->setPurpose, genKeyParamSetParams->purpose,
genKeyParamSetParams->setDigest, genKeyParamSetParams->digest,
genKeyParamSetParams->setPadding, genKeyParamSetParams->padding,
genKeyParamSetParams->setBlockMode, genKeyParamSetParams->mode,
genKeyParamSetParams->setKeyStorageFlag, genKeyParamSetParams->keyStorageFlag
};
int32_t ret = TestConstructGenerateKeyParamSet(&paramStruct);
HKS_TEST_ASSERT(ret == 0);
struct HksParamSet *paramSetOut = NULL;
if (genKeyParamSetParamsOut != NULL) {
ret = TestConstructGenerateKeyParamSetOut(&paramSet,
genKeyParamSetParamsOut->paramSetExist, genKeyParamSetParamsOut->paramSetSize);
HKS_TEST_ASSERT(ret == 0);
}
ret = HksGenerateKey(keyAlias, paramSet, paramSetOut);
HKS_TEST_ASSERT(ret == 0);
HksFreeParamSet(&paramSet);
return ret;
}
int32_t BaseTestCipherProcess(const struct HksBlob *keyAlias, uint32_t index)
{
struct HksBlob *plainData = NULL;
struct HksBlob *cipherData = NULL;
int32_t ret = ConstructDataToBlob(&plainData, &cipherData,
&g_testCipherParams[index].plainTextParams, &g_testCipherParams[index].cipherTextParams);
HKS_TEST_ASSERT(ret == 0);
struct HksBlob *ivData = NULL;
struct HksBlob *nonceData = NULL;
struct HksBlob *aadData = NULL;
struct HksBlob *decryptedData = NULL;
/* 2. encrypt */
do {
struct CipherEncryptStructure testEncryptStruct = {
keyAlias, &g_testCipherParams[index].encryptParamSetParams,
plainData, cipherData, &ivData, &nonceData, &aadData, 1
};
ret = Encrypt(&testEncryptStruct);
if (ret != g_testCipherParams[index].expectResult) {
break;
}
/* 3. decrypt */
struct CipherDecryptStructure testDecryptStruct = {
(struct HksBlob *)(keyAlias), &g_testCipherParams[index], cipherData,
&decryptedData, ivData, nonceData, aadData, 1
};
ret = DecryptCipher(&testDecryptStruct);
if (ret != g_testCipherParams[index].expectResult) {
break;
}
if (ret == g_testCipherParams[index].expectResult) {
if (plainData->size != decryptedData->size) {
break;
};
ret = memcmp(plainData->data, decryptedData->data, plainData->size);
}
} while (0);
TestFreeBlob(&plainData);
TestFreeBlob(&cipherData);
TestFreeBlob(&decryptedData);
TestFreeBlob(&ivData);
TestFreeBlob(&nonceData);
TestFreeBlob(&aadData);
return ret;
}
enum HksStorageType {
HKS_STORAGE_TYPE_KEY = 0,
HKS_STORAGE_TYPE_CERTCHAIN,
HKS_STORAGE_TYPE_ROOT_KEY,
};
extern int32_t HksStoreKeyBlob(const struct HksBlob *processName, const struct HksBlob *keyAlias,
enum HksStorageType storageType, const struct HksBlob *keyBlob);
int32_t __attribute__((weak)) HksStoreKeyBlob(const struct HksBlob *processName, const struct HksBlob *keyAlias,
enum HksStorageType storageType, const struct HksBlob *keyBlob)
{
(void)processName;
(void)keyAlias;
(void)storageType;
(void)keyBlob;
return HKS_SUCCESS;
}
static void ExecHksModifyKeyTest001(void const *argument)
{
LiteTestPrint("HksModifyKeyTest001 Begin!\n");
uint32_t index = 0;
struct HksBlob keyAlias = { strlen(g_testName), (uint8_t *)g_testName };
int32_t ret = GenerateKeyTwo(&keyAlias, &g_testCipherParams[index].keyAliasParams,
&g_testCipherParams[index].genKeyParamSetParams,
&g_testCipherParams[index].genKeyParamSetParamsOut);
TEST_ASSERT_TRUE(ret == 0);
ret = BaseTestCipherProcess(&keyAlias, 0);
TEST_ASSERT_TRUE(ret == 0);
struct HksBlob *plainData = NULL;
struct HksBlob *cipherData = NULL;
ret = ConstructDataToBlob(&plainData, &cipherData,
&g_testCipherParams[index].plainTextParams, &g_testCipherParams[index].cipherTextParams);
TEST_ASSERT_TRUE(ret == 0);
struct HksBlob *ivData = NULL;
struct HksBlob *nonceData = NULL;
struct HksBlob *aadData = NULL;
/* 2. encrypt */
struct CipherEncryptStructure testEncryptStruct = {
&keyAlias, &g_testCipherParams[index].encryptParamSetParams,
plainData, cipherData, &ivData, &nonceData, &aadData, 1
};
ret = Encrypt(&testEncryptStruct);
TEST_ASSERT_TRUE(ret == 0);
ret = GenerateKeyTwo(&keyAlias, &g_testCipherParams[index].keyAliasParams,
&g_testCipherParams[index].genKeyParamSetParams,
&g_testCipherParams[index].genKeyParamSetParamsOut);
TEST_ASSERT_TRUE(ret == 0);
/* 3. decrypt */
struct HksBlob *decryptedData = NULL;
struct CipherDecryptStructure testDecryptStruct = {
&keyAlias, &g_testCipherParams[index], cipherData,
&decryptedData, ivData, nonceData, aadData, 1
};
ret = DecryptCipher(&testDecryptStruct);
HKS_TEST_ASSERT(ret != g_testCipherParams[index].expectResult);
TestFreeBlob(&plainData);
TestFreeBlob(&cipherData);
TestFreeBlob(&decryptedData);
TestFreeBlob(&ivData);
TestFreeBlob(&nonceData);
TestFreeBlob(&aadData);
TEST_ASSERT_TRUE(ret != 0);
LiteTestPrint("HksModifyKeyTest001 End!\n");
osThreadExit();
}
/**
* @tc.name: HksModifyKeyTest.HksModifyKeyTest001
* @tc.desc: The static function will return true;
* @tc.type: FUNC
*/
LITE_TEST_CASE(HksModifyKeyTest, HksModifyKeyTest001, Level1)
{
osThreadId_t id;
osThreadAttr_t attr;
g_setPriority = osPriorityAboveNormal6;
attr.name = "test";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = TEST_TASK_STACK_SIZE;
attr.priority = g_setPriority;
id = osThreadNew((osThreadFunc_t)ExecHksModifyKeyTest001, NULL, &attr);
sleep(WAIT_TO_TEST_DONE);
LiteTestPrint("HksModifyKeyTest001 End2!\n");
}
RUN_TEST_SUITE(HksModifyKeyTest);
#endif /* _CUT_AUTHENTICATE_ */
/*
* Copyright (C) 2021 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.
*/
#ifndef _CUT_AUTHENTICATE_
#ifndef _CUT_ED25519_
#include "hks_safe_cipher_key_test.h"
#include <hctest.h>
#include "hks_api.h"
#include "hks_param.h"
#include "hks_test_curve25519.h"
#include "hks_test_log.h"
#include "hks_test_mem.h"
#include "stdlib.h"
const char *g_storePath = "/storage/";
const char *g_testEd25519 = "test_ed25519";
#define TEST_TASK_STACK_SIZE 0x2000
#define WAIT_TO_TEST_DONE 4
static osPriority_t g_setPriority;
/*
* @tc.register: register a test suit named "CalcMultiTest"
* @param: test subsystem name
* @param: c_example module name
* @param: CalcMultiTest test suit name
*/
LITE_TEST_SUIT(security, securityData, HksSafeCipherKeyTest);
static void ExecHksInitialize(void const *argument)
{
LiteTestPrint("HksInitialize Begin!\n");
TEST_ASSERT_TRUE(HksInitialize() == 0);
LiteTestPrint("HksInitialize End!\n");
osThreadExit();
}
/**
* @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
* @return: true——setup success
*/
static BOOL HksSafeCipherKeyTestSetUp()
{
LiteTestPrint("setup\n");
osThreadId_t id;
osThreadAttr_t attr;
g_setPriority = osPriorityAboveNormal6;
attr.name = "test";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = TEST_TASK_STACK_SIZE;
attr.priority = g_setPriority;
id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
sleep(WAIT_TO_TEST_DONE);
LiteTestPrint("HksSafeCipherKeyTestSetUp End2!\n");
return TRUE;
}
/**
* @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
* @return: true——teardown success
*/
static BOOL HksSafeCipherKeyTestTearDown()
{
LiteTestPrint("tearDown\n");
return TRUE;
}
static struct HksBlob g_storageImageBuffer;
static int32_t GetKeyOffsetByKeyAlias(const struct HksBlob *keyAlias, uint32_t *keyOffset)
{
struct HksBlob storageBuf = g_storageImageBuffer;
/* 1. get imageBuffer total Len */
struct HksStoreHeaderInfo *keyInfoHead = (struct HksStoreHeaderInfo *)storageBuf.data;
uint32_t keyCount = keyInfoHead->keyCount;
uint32_t totalLen = keyInfoHead->totalLen;
/* 2. traverse imageBuffer to search for keyAlias */
uint32_t offset = sizeof(*keyInfoHead);
for (uint32_t i = 0; i < keyCount; ++i) {
uint8_t *tmpBuf = storageBuf.data + offset;
struct HksStoreKeyInfo *keyInfo = (struct HksStoreKeyInfo *)tmpBuf;
if (keyInfo->aliasSize == keyAlias->size) {
if (HksMemCmp(keyAlias->data, tmpBuf + sizeof(*keyInfo), keyAlias->size) == 0) {
*keyOffset = offset;
return HKS_SUCCESS;
}
}
offset += keyInfo->keyInfoLen;
}
return HKS_ERROR_NOT_EXIST;
}
static int32_t CompareTwoKey(const struct HksBlob *keyAliasOne, const struct HksBlob *keyAliasTwo)
{
uint32_t sizeOne = HksTestFileSize(g_storePath, "hks_keystore");
uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne);
if (bufOne == NULL) {
return HKS_ERROR_MALLOC_FAIL;
}
uint32_t sizeRead = HksTestFileRead(g_storePath, "hks_keystore", 0, bufOne, sizeOne);
TEST_ASSERT_TRUE(sizeRead > 0);
g_storageImageBuffer.data = bufOne;
g_storageImageBuffer.size = sizeOne;
int32_t offset1;
int ret = GetKeyOffsetByKeyAlias(keyAliasOne, &offset1);
TEST_ASSERT_TRUE(ret == 0);
struct HksStoreKeyInfo *keyInfo1 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset1);
int32_t offset2;
ret = GetKeyOffsetByKeyAlias(keyAliasTwo, &offset2);
TEST_ASSERT_TRUE(ret == 0);
struct HksStoreKeyInfo *keyInfo2 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset2);
TEST_ASSERT_TRUE(keyInfo1->keyInfoLen == keyInfo2->keyInfoLen);
ret = memcmp(keyInfo1, keyInfo2, keyInfo1->keyInfoLen);
HksTestFree(bufOne);
return ret;
}
/**
* @tc.name: HksSafeCipherKeyTest.HksSafeCipherKeyTest001
* @tc.desc: The static function will return true;
* @tc.type: FUNC
*/
LITE_TEST_CASE(HksSafeCipherKeyTest, HksSafeCipherKeyTest001, Level1)
{
struct HksBlob ed25519Alias = { strlen(g_testEd25519), (uint8_t *)g_testEd25519 };
uint8_t pubKey[32] = {0};
uint32_t pubKeyLen = 32;
struct HksBlob pubKeyInfo = { pubKeyLen, pubKey };
int32_t ret = TestGenerateEd25519Key(ed25519Alias);
TEST_ASSERT_TRUE(ret == 0);
ret = HksExportPublicKey(&ed25519Alias, NULL, &pubKeyInfo);
TEST_ASSERT_TRUE(ret == 0);
ret = HksDeleteKey(&ed25519Alias, NULL);
TEST_ASSERT_TRUE(ret == 0);
struct HksBlob newAliasOne = { strlen("test_ed25519_1"), (uint8_t *)"test_ed25519_1" };
ret = TestImportEd25519(newAliasOne, &pubKeyInfo);
TEST_ASSERT_TRUE(ret == 0);
struct HksBlob newAliasTwo = { strlen("test_ed25519_2"), (uint8_t *)"test_ed25519_2" };
ret = TestImportEd25519(newAliasTwo, &pubKeyInfo);
TEST_ASSERT_TRUE(ret == 0);
ret = CompareTwoKey(&newAliasOne, &newAliasTwo);
TEST_ASSERT_TRUE(ret != 0);
ret = HksDeleteKey(&newAliasOne, NULL);
HKS_TEST_ASSERT(ret == 0);
TEST_ASSERT_TRUE(ret == 0);
ret = HksDeleteKey(&newAliasTwo, NULL);
TEST_ASSERT_TRUE(ret == 0);
}
RUN_TEST_SUITE(HksSafeCipherKeyTest);
#endif
#endif /* _CUT_AUTHENTICATE_ */
/*
* Copyright (C) 2021 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.
*/
#ifndef _CUT_AUTHENTICATE_
#include "hks_safe_compare_key_test.h"
#include <hctest.h>
#include <unistd.h>
#include "hks_api.h"
#include "hks_mem.h"
#include "hks_param.h"
#include "hks_test_api_performance.h"
#include "hks_test_common.h"
#include "hks_test_file_operator.h"
#include "hks_test_log.h"
#include "hks_test_mem.h"
#include "cmsis_os2.h"
#include "ohos_types.h"
static const char *g_storePath = "/storage/";
static const char *g_testOne = "TestOne";
static const char *g_testTwo = "TestTwo";
#define TEST_TASK_STACK_SIZE 0x2000
#define WAIT_TO_TEST_DONE 4
static osPriority_t g_setPriority;
/*
* @tc.register: register a test suit named "CalcMultiTest"
* @param: test subsystem name
* @param: c_example module name
* @param: CalcMultiTest test suit name
*/
LITE_TEST_SUIT(security, securityData, HksSafeCompareKeyTest);
/**
* @tc.setup: define a setup for test suit, format:"CalcMultiTest + SetUp"
* @return: true——setup success
*/
static void ExecHksInitialize(void const *argument)
{
LiteTestPrint("HksInitialize Begin!\n");
TEST_ASSERT_TRUE(HksInitialize() == 0);
LiteTestPrint("HksInitialize End!\n");
osThreadExit();
}
static BOOL HksSafeCompareKeyTestSetUp()
{
LiteTestPrint("setup\n");
osThreadId_t id;
osThreadAttr_t attr;
g_setPriority = osPriorityAboveNormal6;
attr.name = "test";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = TEST_TASK_STACK_SIZE;
attr.priority = g_setPriority;
id = osThreadNew((osThreadFunc_t)ExecHksInitialize, NULL, &attr);
sleep(WAIT_TO_TEST_DONE);
LiteTestPrint("HksSafeCompareKeyTestSetUp End2!\n");
return TRUE;
}
/**
* @tc.teardown: define a setup for test suit, format:"CalcMultiTest + TearDown"
* @return: true——teardown success
*/
static BOOL HksSafeCompareKeyTestTearDown()
{
LiteTestPrint("tearDown\n");
return TRUE;
}
static const struct HksTestGenKeyParams g_testGenKeyParams[] = {
/* x25519: tee sign/verify */
{ 0, HKS_SUCCESS, { true, DEFAULT_KEY_ALIAS_SIZE, true, DEFAULT_KEY_ALIAS_SIZE },
{
true,
true, HKS_ALG_X25519,
true, HKS_CURVE25519_KEY_SIZE_256,
true, HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY,
false, 0,
false, 0,
false, 0,
false, 0
},
{ false, 0 },
},
};
static int32_t SafeTestGenerateKey(struct HksBlob *keyAlias)
{
uint32_t index = 0;
uint32_t performTimes = 1;
struct HksParamSet *paramSet = NULL;
struct GenerateKeyParamSetStructure paramStruct = {
&paramSet,
g_testGenKeyParams[index].paramSetParams.paramSetExist,
g_testGenKeyParams[index].paramSetParams.setAlg, g_testGenKeyParams[index].paramSetParams.alg,
g_testGenKeyParams[index].paramSetParams.setKeySize, g_testGenKeyParams[index].paramSetParams.keySize,
g_testGenKeyParams[index].paramSetParams.setPurpose, g_testGenKeyParams[index].paramSetParams.purpose,
g_testGenKeyParams[index].paramSetParams.setDigest, g_testGenKeyParams[index].paramSetParams.digest,
g_testGenKeyParams[index].paramSetParams.setPadding, g_testGenKeyParams[index].paramSetParams.padding,
g_testGenKeyParams[index].paramSetParams.setBlockMode, g_testGenKeyParams[index].paramSetParams.mode,
g_testGenKeyParams[index].paramSetParams.setKeyStorageFlag,
g_testGenKeyParams[index].paramSetParams.keyStorageFlag
};
int32_t ret = TestConstructGenerateKeyParamSet(&paramStruct);
HKS_TEST_ASSERT(ret == 0);
struct HksParamSet *paramSetOut = NULL;
ret = TestConstructGenerateKeyParamSetOut(&paramSetOut,
g_testGenKeyParams[index].paramSetParamsOut.paramSetExist,
g_testGenKeyParams[index].paramSetParamsOut.paramSetSize);
HKS_TEST_ASSERT(ret == 0);
ret = HksGenerateKeyRun(keyAlias, paramSet, paramSetOut, performTimes);
if (ret != g_testGenKeyParams[index].expectResult) {
HKS_TEST_LOG_I("failed, ret[%u] = %d", g_testGenKeyParams[index].testId, ret);
}
HKS_TEST_ASSERT(ret == g_testGenKeyParams[index].expectResult);
if (ret == g_testGenKeyParams[index].expectResult) {
ret = 0;
}
HksFreeParamSet(&paramSet);
HksFreeParamSet(&paramSetOut);
return ret;
}
static struct HksBlob g_storageImageBuffer;
static int32_t GetKeyOffsetByKeyAlias(const struct HksBlob *keyAlias, uint32_t *keyOffset)
{
struct HksBlob storageBuf = g_storageImageBuffer;
/* 1. get imageBuffer total Len */
struct HksStoreHeaderInfo *keyInfoHead = (struct HksStoreHeaderInfo *)storageBuf.data;
uint32_t keyCount = keyInfoHead->keyCount;
uint32_t totalLen = keyInfoHead->totalLen;
/* 2. traverse imageBuffer to search for keyAlias */
uint32_t offset = sizeof(*keyInfoHead);
for (uint32_t i = 0; i < keyCount; ++i) {
uint8_t *tmpBuf = storageBuf.data + offset;
struct HksStoreKeyInfo *keyInfo = (struct HksStoreKeyInfo *)tmpBuf;
if (keyInfo->aliasSize == keyAlias->size) {
if (HksMemCmp(keyAlias->data, tmpBuf + sizeof(*keyInfo), keyAlias->size) == 0) {
*keyOffset = offset;
return HKS_SUCCESS;
}
}
offset += keyInfo->keyInfoLen;
}
return HKS_ERROR_NOT_EXIST;
}
static int32_t CompareKeyData(struct HksBlob *keyAliasOne, struct HksBlob *keyAliasTwo)
{
uint32_t sizeOne = HksTestFileSize(g_storePath, "hks_keystore");
if (sizeOne == 0) {
return 0;
}
uint8_t *bufOne = (uint8_t *)HksTestMalloc(sizeOne);
if (bufOne == NULL) {
return HKS_ERROR_MALLOC_FAIL;
}
uint32_t sizeRead = HksTestFileRead(g_storePath, "hks_keystore", 0, bufOne, sizeOne);
TEST_ASSERT_TRUE(sizeRead > 0);
g_storageImageBuffer.data = bufOne;
g_storageImageBuffer.size = sizeOne;
int32_t offset1;
int ret = GetKeyOffsetByKeyAlias(keyAliasOne, (uint32_t *)(&offset1));
TEST_ASSERT_TRUE(ret == 0);
struct HksStoreKeyInfo *keyInfo1 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset1);
int32_t offset2;
ret = GetKeyOffsetByKeyAlias(keyAliasTwo, (uint32_t *)(&offset2));
TEST_ASSERT_TRUE(ret == 0);
struct HksStoreKeyInfo *keyInfo2 = (struct HksStoreKeyInfo *)(g_storageImageBuffer.data + offset2);
TEST_ASSERT_TRUE(keyInfo1->keyInfoLen == keyInfo2->keyInfoLen);
ret = memcmp(keyInfo1, keyInfo2, keyInfo1->keyInfoLen);
HksTestFree(bufOne);
return ret;
}
static void ExcHksSafeCompareKeyTest001(void const *argument)
{
LiteTestPrint("HksSafeCompareKeyTest001 Begin!\n");
struct HksBlob keyAliasOne = { strlen(g_testOne), (uint8_t *)g_testOne };
int32_t ret = SafeTestGenerateKey(&keyAliasOne);
HKS_TEST_ASSERT(ret == 0);
struct HksBlob keyAliasTwo = { strlen(g_testTwo), (uint8_t *)g_testTwo };
ret = SafeTestGenerateKey(&keyAliasTwo);
HKS_TEST_ASSERT(ret == 0);
ret = CompareKeyData(&keyAliasOne, &keyAliasTwo);
HKS_TEST_ASSERT(ret != 0);
TEST_ASSERT_TRUE(ret != 0);
LiteTestPrint("HksSafeCompareKeyTest001 End!\n");
osThreadExit();
}
/**
* @tc.name: HksSafeCompareKeyTest.HksSafeCompareKeyTest001
* @tc.desc: The static function will return true;
* @tc.type: FUNC
*/
LITE_TEST_CASE(HksSafeCompareKeyTest, HksSafeCompareKeyTest001, Level1)
{
osThreadId_t id;
osThreadAttr_t attr;
g_setPriority = osPriorityAboveNormal6;
attr.name = "test";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = TEST_TASK_STACK_SIZE;
attr.priority = g_setPriority;
id = osThreadNew((osThreadFunc_t)ExcHksSafeCompareKeyTest001, NULL, &attr);
sleep(WAIT_TO_TEST_DONE);
LiteTestPrint("HksSafeCompareKeyTest001 End2!\n");
}
RUN_TEST_SUITE(HksSafeCompareKeyTest);
#endif /* _CUT_AUTHENTICATE_ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册