x25519 derive tdd

Signed-off-by: ailu1 <jinyanhong1@huawei.com>
This commit is contained in:
ailu1 2024-04-13 21:55:46 +08:00
parent 5336c0454d
commit 5581b803f2
6 changed files with 1050 additions and 0 deletions

View File

@ -82,6 +82,13 @@ ohos_unittest("huks_UT_test") {
"src/hks_import_rsa_test.cpp",
]
if (huks_security_level == "trusted_environment") {
sources += [
"src/hks_hkdf_derive_x25519_part1_test.cpp",
"src/hks_hkdf_derive_x25519_part2_test.cpp"
]
}
defines = [ "ENABLE_NETWORK" ]
defines += [ "TEMP_ISOLATION" ]
if (huks_security_level == "software") {

View File

@ -27,6 +27,9 @@ static const uint32_t DERIVE_KEY_SIZE_32 = 32;
static const uint32_t DERIVE_KEY_SIZE_48 = 48;
static const uint32_t DERIVE_KEY_SIZE_64 = 64;
static const uint32_t INFO_SIZE_32 = 32;
static const uint32_t SALT_SIZE_32 = 32;
int32_t HksHkdfDeriveTestNormalCase(const struct HksBlob keyAlias, const struct HksParamSet *genParamSet,
struct HksParamSet *DeriveParamSet, struct HksParamSet *DeriveFinalParamsSet, int32_t cmpRet);
int32_t HksHkdfDeriveTestCmpCase(const struct HksBlob keyAlias, const struct HksParamSet *genParamSet,

View File

@ -0,0 +1,30 @@
/*
* 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.
*/
#ifndef HKS_HKDF_DERIVE_X25519_PART1_TEST_H
#define HKS_HKDF_DERIVE_X25519_PART1_TEST_H
namespace Unittest::HkdfDerive {
int HksHKDFX25519Derive001(void);
int HksHKDFX25519Derive002(void);
int HksHKDFX25519Derive003(void);
int HksHKDFX25519Derive004(void);
int HksHKDFX25519Derive005(void);
}
#endif // HKS_HKDF_DERIVE_X25519_PART1_TEST_H

View File

@ -0,0 +1,27 @@
/*
* 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.
*/
#ifndef HKS_HKDF_DERIVE_X25519_PART2_TEST_H
#define HKS_HKDF_DERIVE_X25519_PART2_TEST_H
namespace Unittest::HkdfDerive {
int HksHKDFX25519Derive031(void);
int HksHKDFX25519Derive032(void);
int HksHKDFX25519Derive033(void);
int HksHKDFX25519Derive034(void);
}
#endif // HKS_HKDF_DERIVE_X25519_PART2_TEST_H

View File

@ -0,0 +1,534 @@
/*
* 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 "hks_hkdf_derive_x25519_part1_test.h"
#include "hks_agree_test_common.h"
#include "hks_hkdf_derive_test_common.h"
#include <gtest/gtest.h>
#ifdef L2_STANDARD
#include "file_ex.h"
#endif
using namespace testing::ext;
namespace Unittest::HkdfDerive {
class HksHkdfX25519DerivePart1Test : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void HksHkdfX25519DerivePart1Test::SetUpTestCase(void)
{
}
void HksHkdfX25519DerivePart1Test::TearDownTestCase(void)
{
}
void HksHkdfX25519DerivePart1Test::SetUp()
{
EXPECT_EQ(HksInitialize(), 0);
}
void HksHkdfX25519DerivePart1Test::TearDown()
{
}
static struct HksParam g_genParams001[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}
};
static struct HksParam g_hkdfParams001[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_HKDF
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_DERIVE_KEY_SIZE,
.uint32Param = DERIVE_KEY_SIZE_32
}
};
static struct HksParam g_hkdfFinishParams001[] = {
{
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}, {
.tag = HKS_TAG_KEY_ALIAS,
.blob = {
strlen("HksHKDFX25519DeriveKeyAliasFinalTest001"),
(uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest001"
}
}, {
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_AGREE
}
};
static struct HksParam g_genParams002[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA384
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}
};
static struct HksParam g_hkdfParams002[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_HKDF
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA384
}, {
.tag = HKS_TAG_DERIVE_KEY_SIZE,
.uint32Param = DERIVE_KEY_SIZE_32
}
};
static struct HksParam g_hkdfFinishParams002[] = {
{
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}, {
.tag = HKS_TAG_KEY_ALIAS,
.blob = {
strlen("HksHKDFX25519DeriveKeyAliasFinalTest002"),
(uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest002"
}
}, {
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_SIGN | HKS_KEY_PURPOSE_VERIFY
}
};
static struct HksParam g_genParams003[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA512
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}
};
static struct HksParam g_hkdfParams003[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_HKDF
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA512
}, {
.tag = HKS_TAG_DERIVE_KEY_SIZE,
.uint32Param = DERIVE_KEY_SIZE_32
}
};
static struct HksParam g_hkdfFinishParams003[] = {
{
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}, {
.tag = HKS_TAG_KEY_ALIAS,
.blob = {
strlen("HksHKDFX25519DeriveKeyAliasFinalTest003"),
(uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest003"
}
}, {
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}
};
static uint8_t g_info[] = "info1234123412341234123412341234";
static uint8_t g_salt[] = "salt1234123412341234123412341234";
static HksBlob g_infoBlob = { INFO_SIZE_32, g_info };
static HksBlob g_saltBlob = { SALT_SIZE_32, g_salt };
static struct HksParam g_genParams004[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}
};
static struct HksParam g_hkdfParams004[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_HKDF
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_DERIVE_KEY_SIZE,
.uint32Param = DERIVE_KEY_SIZE_32
} , {
.tag = HKS_TAG_INFO,
.blob = g_infoBlob
} , {
.tag = HKS_TAG_SALT,
.blob = g_saltBlob
}
};
static struct HksParam g_hkdfFinishParams004[] = {
{
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}, {
.tag = HKS_TAG_KEY_ALIAS,
.blob = {
strlen("HksHKDFX25519DeriveKeyAliasFinalTest004"),
(uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest004"
}
}, {
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_AGREE
}
};
static struct HksParam g_genParams005[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}
};
static struct HksParam g_hkdfParams005[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_HKDF
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_DERIVE_KEY_SIZE,
.uint32Param = DERIVE_KEY_SIZE_32
}
};
static struct HksParam g_hkdfFinishParams005[] = {
{
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ALLOW_KEY_EXPORTED
}, {
.tag = HKS_TAG_KEY_ALIAS,
.blob = {
strlen("HksHKDFX25519DeriveKeyAliasFinalTest005"),
(uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest005"
}
}, {
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_AGREE
}
};
/**
* @tc.name: HksHkdfX25519DerivePart1Test.HksHKDFX25519Derive001
* @tc.desc: alg-HKDF pur-Derive dig-SHA256 KEY_SIZE-128
* @tc.type: FUNC
*/
HWTEST_F(HksHkdfX25519DerivePart1Test, HksHKDFX25519Derive001, TestSize.Level0)
{
struct HksBlob keyAlias = { strlen("HksHKDFX25519DeriveKeyAliasTest001"), (uint8_t *)"HksHKDFX25519DeriveKeyAliasTest001" };
int32_t ret = HKS_FAILURE;
/* 1. Generate Key */
struct HksParamSet *genParamSet = nullptr;
ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
/* 2. HKDF Three Stage */
struct HksParamSet *hkdfParamSet = nullptr;
struct HksParamSet *hkdfFinishParamSet = nullptr;
ret = InitParamSet(&hkdfParamSet, g_hkdfParams001, sizeof(g_hkdfParams001) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// finish paramset
ret = InitParamSet(&hkdfFinishParamSet, g_hkdfFinishParams001, sizeof(g_hkdfFinishParams001) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Init-Update-final
HksHkdfDeriveTestNormalCase(keyAlias, genParamSet, hkdfParamSet, hkdfFinishParamSet, HKS_SUCCESS);
/* 3. Delete Key */
ret = HksDeleteKey(&keyAlias, genParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
struct HksBlob deleteKeyAlias = { .size = strlen("HksHKDFX25519DeriveKeyAliasFinalTest001"),
.data = (uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest001"};
ret = HksDeleteKey(&deleteKeyAlias, NULL);
EXPECT_EQ(ret, HKS_SUCCESS) << "Delete Final Key failed.";
HksFreeParamSet(&genParamSet);
HksFreeParamSet(&hkdfParamSet);
HksFreeParamSet(&hkdfFinishParamSet);
}
/**
* @tc.name: HksHkdfX25519DerivePart1Test.HksHKDFX25519Derive002
* @tc.desc: alg-HKDF pur-Derive dig-SHA256 KEY_SIZE-128
* @tc.type: FUNC
*/
HWTEST_F(HksHkdfX25519DerivePart1Test, HksHKDFX25519Derive002, TestSize.Level0)
{
struct HksBlob keyAlias = { strlen("HksHKDFX25519DeriveKeyAliasTest002"), (uint8_t *)"HksHKDFX25519DeriveKeyAliasTest002" };
int32_t ret = HKS_FAILURE;
/* 1. Generate Key */
struct HksParamSet *genParamSet = nullptr;
ret = InitParamSet(&genParamSet, g_genParams002, sizeof(g_genParams002) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
/* 2. HKDF Three Stage */
struct HksParamSet *hkdfParamSet = nullptr;
struct HksParamSet *hkdfFinishParamSet = nullptr;
ret = InitParamSet(&hkdfParamSet, g_hkdfParams002, sizeof(g_hkdfParams002) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// finish paramset
ret = InitParamSet(&hkdfFinishParamSet, g_hkdfFinishParams002, sizeof(g_hkdfFinishParams002) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Init-Update-final
HksHkdfDeriveTestNormalCase(keyAlias, genParamSet, hkdfParamSet, hkdfFinishParamSet, HKS_SUCCESS);
/* 3. Delete Key */
ret = HksDeleteKey(&keyAlias, genParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
struct HksBlob deleteKeyAlias = { .size = strlen("HksHKDFX25519DeriveKeyAliasFinalTest002"),
.data = (uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest002"};
ret = HksDeleteKey(&deleteKeyAlias, NULL);
EXPECT_EQ(ret, HKS_SUCCESS) << "Delete Final Key failed.";
HksFreeParamSet(&genParamSet);
HksFreeParamSet(&hkdfParamSet);
HksFreeParamSet(&hkdfFinishParamSet);
}
/**
* @tc.name: HksHkdfX25519DerivePart1Test.HksHKDFX25519Derive003
* @tc.desc: alg-HKDF pur-Derive dig-SHA256 KEY_SIZE-128
* @tc.type: FUNC
*/
HWTEST_F(HksHkdfX25519DerivePart1Test, HksHKDFX25519Derive003, TestSize.Level0)
{
struct HksBlob keyAlias = { strlen("HksHKDFX25519DeriveKeyAliasTest003"), (uint8_t *)"HksHKDFX25519DeriveKeyAliasTest003" };
int32_t ret = HKS_FAILURE;
/* 1. Generate Key */
struct HksParamSet *genParamSet = nullptr;
ret = InitParamSet(&genParamSet, g_genParams003, sizeof(g_genParams003) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
/* 2. HKDF Three Stage */
struct HksParamSet *hkdfParamSet = nullptr;
struct HksParamSet *hkdfFinishParamSet = nullptr;
ret = InitParamSet(&hkdfParamSet, g_hkdfParams003, sizeof(g_hkdfParams003) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// finish paramset
ret = InitParamSet(&hkdfFinishParamSet, g_hkdfFinishParams003, sizeof(g_hkdfFinishParams003) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Init-Update-final
HksHkdfDeriveTestNormalCase(keyAlias, genParamSet, hkdfParamSet, hkdfFinishParamSet, HKS_SUCCESS);
/* 3. Delete Key */
ret = HksDeleteKey(&keyAlias, genParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
struct HksBlob deleteKeyAlias = { .size = strlen("HksHKDFX25519DeriveKeyAliasFinalTest003"),
.data = (uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest003"};
ret = HksDeleteKey(&deleteKeyAlias, NULL);
EXPECT_EQ(ret, HKS_SUCCESS) << "Delete Final Key failed.";
HksFreeParamSet(&genParamSet);
HksFreeParamSet(&hkdfParamSet);
HksFreeParamSet(&hkdfFinishParamSet);
}
/**
* @tc.name: HksHkdfX25519DerivePart1Test.HksHKDFX25519Derive004
* @tc.desc: alg-HKDF pur-Derive dig-SHA256 KEY_SIZE-128
* @tc.type: FUNC
*/
HWTEST_F(HksHkdfX25519DerivePart1Test, HksHKDFX25519Derive004, TestSize.Level0)
{
struct HksBlob keyAlias = { strlen("HksHKDFX25519DeriveKeyAliasTest004"), (uint8_t *)"HksHKDFX25519DeriveKeyAliasTest004" };
int32_t ret = HKS_FAILURE;
/* 1. Generate Key */
struct HksParamSet *genParamSet = nullptr;
ret = InitParamSet(&genParamSet, g_genParams004, sizeof(g_genParams004) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
/* 2. HKDF Three Stage */
struct HksParamSet *hkdfParamSet = nullptr;
struct HksParamSet *hkdfFinishParamSet = nullptr;
ret = InitParamSet(&hkdfParamSet, g_hkdfParams004, sizeof(g_hkdfParams004) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// finish paramset
ret = InitParamSet(&hkdfFinishParamSet, g_hkdfFinishParams004, sizeof(g_hkdfFinishParams004) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Init-Update-final
HksHkdfDeriveTestNormalCase(keyAlias, genParamSet, hkdfParamSet, hkdfFinishParamSet, HKS_SUCCESS);
/* 3. Delete Key */
ret = HksDeleteKey(&keyAlias, genParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
struct HksBlob deleteKeyAlias = { .size = strlen("HksHKDFX25519DeriveKeyAliasFinalTest004"),
.data = (uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest004"};
ret = HksDeleteKey(&deleteKeyAlias, NULL);
EXPECT_EQ(ret, HKS_SUCCESS) << "Delete Final Key failed.";
HksFreeParamSet(&genParamSet);
HksFreeParamSet(&hkdfParamSet);
HksFreeParamSet(&hkdfFinishParamSet);
}
/**
* @tc.name: HksHkdfX25519DerivePart1Test.HksHKDFX25519Derive005
* @tc.desc: alg-HKDF pur-Derive dig-SHA256 KEY_SIZE-128
* @tc.type: FUNC
*/
HWTEST_F(HksHkdfX25519DerivePart1Test, HksHKDFX25519Derive005, TestSize.Level0)
{
struct HksBlob keyAlias = { strlen("HksHKDFX25519DeriveKeyAliasTest005"), (uint8_t *)"HksHKDFX25519DeriveKeyAliasTest005" };
int32_t ret = HKS_FAILURE;
/* 1. Generate Key */
struct HksParamSet *genParamSet = nullptr;
ret = InitParamSet(&genParamSet, g_genParams005, sizeof(g_genParams005) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
/* 2. HKDF Three Stage */
struct HksParamSet *hkdfParamSet = nullptr;
struct HksParamSet *hkdfFinishParamSet = nullptr;
ret = InitParamSet(&hkdfParamSet, g_hkdfParams005, sizeof(g_hkdfParams005) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// finish paramset
ret = InitParamSet(&hkdfFinishParamSet, g_hkdfFinishParams005, sizeof(g_hkdfFinishParams005) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Init-Update-final
HksHkdfDeriveTestNormalCase(keyAlias, genParamSet, hkdfParamSet, hkdfFinishParamSet, HKS_SUCCESS);
/* 3. Delete Key */
ret = HksDeleteKey(&keyAlias, genParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
struct HksBlob deleteKeyAlias = { .size = strlen("HksHKDFX25519DeriveKeyAliasFinalTest005"),
.data = (uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest005"};
ret = HksDeleteKey(&deleteKeyAlias, NULL);
EXPECT_EQ(ret, HKS_SUCCESS) << "Delete Final Key failed.";
HksFreeParamSet(&genParamSet);
HksFreeParamSet(&hkdfParamSet);
HksFreeParamSet(&hkdfFinishParamSet);
}
} // namespace Unittest::HkdfDerive

View File

@ -0,0 +1,449 @@
/*
* 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 "hks_hkdf_derive_x25519_part2_test.h"
#include "hks_hkdf_derive_test_common.h"
#include <gtest/gtest.h>
#ifdef L2_STANDARD
#include "file_ex.h"
#endif
using namespace testing::ext;
namespace Unittest::HkdfDerive {
class HksHkdfX25519DerivePart2Test : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void HksHkdfX25519DerivePart2Test::SetUpTestCase(void)
{
}
void HksHkdfX25519DerivePart2Test::TearDownTestCase(void)
{
}
void HksHkdfX25519DerivePart2Test::SetUp()
{
EXPECT_EQ(HksInitialize(), 0);
}
void HksHkdfX25519DerivePart2Test::TearDown()
{
}
static struct HksParam g_genParams031[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}
};
static struct HksParam g_hkdfParams031[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_HKDF
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA224
}, {
.tag = HKS_TAG_DERIVE_KEY_SIZE,
.uint32Param = DERIVE_KEY_SIZE_32
}
};
static struct HksParam g_genParams032[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}
};
static struct HksParam g_hkdfParams032[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_HKDF
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA224
}, {
.tag = HKS_TAG_DERIVE_KEY_SIZE,
.uint32Param = DERIVE_KEY_SIZE_48
}
};
static struct HksParam g_genParams033[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}
};
static struct HksParam g_hkdfParams033[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_HKDF
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_DERIVE_KEY_SIZE,
.uint32Param = DERIVE_KEY_SIZE_32
}
};
static struct HksParam g_hkdfFinishParams033[] = {
{
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}, {
.tag = HKS_TAG_KEY_ALIAS,
.blob = {
strlen("HksHKDFX25519DeriveKeyAliasFinalTest033"),
(uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest033"
}
}, {
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_AES_KEY_SIZE_192
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_AGREE
}
};
static struct HksParam g_genParams034[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}
};
static struct HksParam g_hkdfParams034[] = {
{
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_HKDF
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_DERIVE
}, {
.tag = HKS_TAG_DIGEST,
.uint32Param = HKS_DIGEST_SHA256
}, {
.tag = HKS_TAG_DERIVE_KEY_SIZE,
.uint32Param = DERIVE_KEY_SIZE_32
}
};
static struct HksParam g_hkdfFinishParams034[] = {
{
.tag = HKS_TAG_DERIVE_AGREE_KEY_STORAGE_FLAG,
.uint32Param = HKS_STORAGE_ONLY_USED_IN_HUKS
}, {
.tag = HKS_TAG_KEY_ALIAS,
.blob = {
strlen("HksHKDFX25519DeriveKeyAliasFinalTest034"),
(uint8_t *)"HksHKDFX25519DeriveKeyAliasFinalTest034"
}
}, {
.tag = HKS_TAG_ALGORITHM,
.uint32Param = HKS_ALG_X25519
}, {
.tag = HKS_TAG_KEY_SIZE,
.uint32Param = HKS_CURVE25519_KEY_SIZE_256
}, {
.tag = HKS_TAG_PURPOSE,
.uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT
}
};
/**
* @tc.name: HksHkdfX25519DerivePart2Test.HksHKDFX25519Derive031
* @tc.desc: alg-HKDF pur-Derive dig-SHA512. Abort
* @tc.type: FUNC
*/
HWTEST_F(HksHkdfX25519DerivePart2Test, HksHKDFX25519Derive031, TestSize.Level0)
{
struct HksBlob keyAlias = { strlen("HksHKDFX25519DeriveKeyAliasTest031"), (uint8_t *)"HksHKDFX25519DeriveKeyAliasTest031" };
struct HksBlob inData = { g_inData.length(),
(uint8_t *)g_inData.c_str() };
int32_t ret = HKS_FAILURE;
/* 1. Generate Key */
struct HksParamSet *genParamSet = nullptr;
ret = InitParamSet(&genParamSet, g_genParams031, sizeof(g_genParams031) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Generate Key
ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
/* 2. HKDF Three Stage */
struct HksParamSet *hkdfParamSet = nullptr;
ret = InitParamSet(&hkdfParamSet, g_hkdfParams031, sizeof(g_hkdfParams031) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Init
uint8_t handleU[sizeof(uint64_t)] = {0};
struct HksBlob handle = { sizeof(uint64_t), handleU };
ret = HksInit(&keyAlias, hkdfParamSet, &handle, nullptr);
EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
// Update
uint8_t outUpdateU[DERIVE_KEY_SIZE_64] = {0};
struct HksBlob outUpdate = { DERIVE_KEY_SIZE_64, outUpdateU };
ret = HksUpdate(&handle, hkdfParamSet, &inData, &outUpdate);
EXPECT_EQ(ret, HKS_ERROR_INVALID_ARGUMENT) << "Update failed.";
// Abort
ret = HksAbort(&handle, hkdfParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "Finish failed.";
/* 3. Delete Key */
ret = HksDeleteKey(&keyAlias, genParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
HksFreeParamSet(&genParamSet);
HksFreeParamSet(&hkdfParamSet);
}
/**
* @tc.name: HksHkdfX25519DerivePart2Test.HksHKDFX25519Derive032
* @tc.desc: alg-HKDF pur-Derive dig-SHA512. Abort
* @tc.type: FUNC
*/
HWTEST_F(HksHkdfX25519DerivePart2Test, HksHKDFX25519Derive032, TestSize.Level0)
{
struct HksBlob keyAlias = { strlen("HksHKDFX25519DeriveKeyAliasTest032"), (uint8_t *)"HksHKDFX25519DeriveKeyAliasTest032" };
struct HksBlob inData = { g_inData.length(),
(uint8_t *)g_inData.c_str() };
int32_t ret = HKS_FAILURE;
/* 1. Generate Key */
struct HksParamSet *genParamSet = nullptr;
ret = InitParamSet(&genParamSet, g_genParams032, sizeof(g_genParams032) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Generate Key
ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
/* 2. HKDF Three Stage */
struct HksParamSet *hkdfParamSet = nullptr;
ret = InitParamSet(&hkdfParamSet, g_hkdfParams032, sizeof(g_hkdfParams032) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Init
uint8_t handleU[sizeof(uint64_t)] = {0};
struct HksBlob handle = { sizeof(uint64_t), handleU };
ret = HksInit(&keyAlias, hkdfParamSet, &handle, nullptr);
EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
// Update
uint8_t outUpdateU[DERIVE_KEY_SIZE_64] = {0};
struct HksBlob outUpdate = { DERIVE_KEY_SIZE_64, outUpdateU };
ret = HksUpdate(&handle, hkdfParamSet, &inData, &outUpdate);
EXPECT_EQ(ret, HKS_ERROR_INVALID_ARGUMENT) << "Update failed.";
// Abort
ret = HksAbort(&handle, hkdfParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "Finish failed.";
/* 3. Delete Key */
ret = HksDeleteKey(&keyAlias, genParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
HksFreeParamSet(&genParamSet);
HksFreeParamSet(&hkdfParamSet);
}
/**
* @tc.name: HksHkdfX25519DerivePart2Test.HksHKDFX25519Derive033
* @tc.desc: alg-HKDF pur-Derive dig-SHA512. Abort
* @tc.type: FUNC
*/
HWTEST_F(HksHkdfX25519DerivePart2Test, HksHKDFX25519Derive033, TestSize.Level0)
{
struct HksBlob keyAlias = { strlen("HksHKDFX25519DeriveKeyAliasTest033"), (uint8_t *)"HksHKDFX25519DeriveKeyAliasTest033" };
struct HksBlob inData = { g_inData.length(),
(uint8_t *)g_inData.c_str() };
int32_t ret = HKS_FAILURE;
/* 1. Generate Key */
struct HksParamSet *genParamSet = nullptr;
ret = InitParamSet(&genParamSet, g_genParams033, sizeof(g_genParams033) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Generate Key
ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
/* 2. HKDF Three Stage */
struct HksParamSet *hkdfParamSet = nullptr;
struct HksParamSet *hkdfFinishParamSet = nullptr;
ret = InitParamSet(&hkdfParamSet, g_hkdfParams033, sizeof(g_hkdfParams033) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Init
uint8_t handleU[sizeof(uint64_t)] = {0};
struct HksBlob handle = { sizeof(uint64_t), handleU };
ret = HksInit(&keyAlias, hkdfParamSet, &handle, nullptr);
EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
// Update
uint8_t outUpdateU[DERIVE_KEY_SIZE_64] = {0};
struct HksBlob outUpdate = { DERIVE_KEY_SIZE_64, outUpdateU };
ret = HksUpdate(&handle, hkdfParamSet, &inData, &outUpdate);
EXPECT_EQ(ret, HKS_SUCCESS) << "Update failed.";
// Finish
uint8_t outFinishD[COMMON_SIZE] = {0};
struct HksBlob outFinishDerive = { COMMON_SIZE, outFinishD };
ret = InitParamSet(&hkdfFinishParamSet, g_hkdfFinishParams033, sizeof(g_hkdfFinishParams033) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
ret = HksFinish(&handle, hkdfFinishParamSet, &inData, &outFinishDerive);
EXPECT_EQ(ret, HKS_ERROR_INVALID_ARGUMENT) << "Finish failed.";
// Abort
ret = HksAbort(&handle, hkdfParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "Finish failed.";
/* 3. Delete Key */
ret = HksDeleteKey(&keyAlias, genParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
HksFreeParamSet(&genParamSet);
HksFreeParamSet(&hkdfParamSet);
HksFreeParamSet(&hkdfFinishParamSet);
}
/**
* @tc.name: HksHkdfX25519DerivePart2Test.HksHKDFX25519Derive034
* @tc.desc: alg-HKDF pur-Derive dig-SHA512. Abort
* @tc.type: FUNC
*/
HWTEST_F(HksHkdfX25519DerivePart2Test, HksHKDFX25519Derive034, TestSize.Level0)
{
struct HksBlob keyAlias = { strlen("HksHKDFX25519DeriveKeyAliasTest034"), (uint8_t *)"HksHKDFX25519DeriveKeyAliasTest034" };
struct HksBlob inData = { g_inData.length(),
(uint8_t *)g_inData.c_str() };
int32_t ret = HKS_FAILURE;
/* 1. Generate Key */
struct HksParamSet *genParamSet = nullptr;
ret = InitParamSet(&genParamSet, g_genParams034, sizeof(g_genParams034) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Generate Key
ret = HksGenerateKey(&keyAlias, genParamSet, nullptr);
EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
/* 2. HKDF Three Stage */
struct HksParamSet *hkdfParamSet = nullptr;
struct HksParamSet *hkdfFinishParamSet = nullptr;
ret = InitParamSet(&hkdfParamSet, g_hkdfParams034, sizeof(g_hkdfParams034) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
// Init
uint8_t handleU[sizeof(uint64_t)] = {0};
struct HksBlob handle = { sizeof(uint64_t), handleU };
ret = HksInit(&keyAlias, hkdfParamSet, &handle, nullptr);
EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
// Update
uint8_t outUpdateU[DERIVE_KEY_SIZE_64] = {0};
struct HksBlob outUpdate = { DERIVE_KEY_SIZE_64, outUpdateU };
ret = HksUpdate(&handle, hkdfParamSet, &inData, &outUpdate);
EXPECT_EQ(ret, HKS_SUCCESS) << "Update failed.";
// Finish
uint8_t outFinishD[COMMON_SIZE] = {0};
struct HksBlob outFinishDerive = { COMMON_SIZE, outFinishD };
ret = InitParamSet(&hkdfFinishParamSet, g_hkdfFinishParams034, sizeof(g_hkdfFinishParams034) / sizeof(HksParam));
EXPECT_EQ(ret, HKS_SUCCESS) << "InitParamSet failed.";
ret = HksFinish(&handle, hkdfFinishParamSet, &inData, &outFinishDerive);
EXPECT_EQ(ret, HKS_ERROR_INVALID_ARGUMENT) << "Finish failed.";
// Abort
ret = HksAbort(&handle, hkdfParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "Finish failed.";
/* 3. Delete Key */
ret = HksDeleteKey(&keyAlias, genParamSet);
EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
HksFreeParamSet(&genParamSet);
HksFreeParamSet(&hkdfParamSet);
HksFreeParamSet(&hkdfFinishParamSet);
}
} // namespace Unittest::HkdfDerive