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 #include "huks_derive_hkdf_test.h"
17
18 #include <gtest/gtest.h>
19
20 #include "huks_derive_hkdf_test_common.h"
21
22 using namespace testing::ext;
23 namespace Unittest::HkdfDerive
24 {
25 class HuksDeriveHKDFTest : public testing::Test
26 {
27 public:
28 static void SetUpTestCase(void);
29
30 static void TearDownTestCase(void);
31
32 void SetUp();
33
34 void TearDown();
35 };
36
SetUpTestCase(void)37 void HuksDeriveHKDFTest::SetUpTestCase(void) {}
38
TearDownTestCase(void)39 void HuksDeriveHKDFTest::TearDownTestCase(void) {}
40
SetUp()41 void HuksDeriveHKDFTest::SetUp() {}
42
TearDown()43 void HuksDeriveHKDFTest::TearDown() {}
44 static struct OH_Huks_Param g_genParams001[] = {
45 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES},
46 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE},
47 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA256},
48 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_AES_KEY_SIZE_256},
49 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
50 static struct OH_Huks_Param g_hkdfParams001[] = {
51 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_HKDF},
52 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE},
53 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA256},
54 {.tag = OH_HUKS_TAG_DERIVE_KEY_SIZE, .uint32Param = DERIVE_KEY_SIZE_32},
55 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
56 static struct OH_Huks_Param g_hkdfFinishParams001[] = {
57 {.tag = OH_HUKS_TAG_KEY_STORAGE_FLAG, .uint32Param = OH_HUKS_STORAGE_PERSISTENT},
58 {.tag = OH_HUKS_TAG_KEY_ALIAS,
59 .blob = {strlen("HksHKDFDeriveKeyAliasFinalTest001"), (uint8_t *)"HksHKDFDeriveKeyAliasFinalTest001"}},
60 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES},
61 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = 256},
62 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE},
63 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA256},
64 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
65
66 /**
67 * @tc.name: HuksDeriveHKDFTest.Security_HUKS_NAPI_Derive_hkdf_0100
68 * @tc.desc: alg-HKDF pur-Derive dig-SHA256 KEY_SIZE-128
69 * @tc.type: FUNC
70 */
71 HWTEST_F(HuksDeriveHKDFTest, Security_HUKS_NAPI_Derive_hkdf_0100, TestSize.Level1)
72 {
73 struct OH_Huks_Blob keyAlias = {strlen("HksHKDFDeriveKeyAliasTest001"), (uint8_t *)"HksHKDFDeriveKeyAliasTest001"};
74
75 /* 1. Generate Key */
76 struct OH_Huks_ParamSet *genParamSet = nullptr;
77 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(OH_Huks_Param));
78 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
79
80 /* 2. HKDF Three Stage */
81 struct OH_Huks_ParamSet *hkdfParamSet = nullptr;
82 struct OH_Huks_ParamSet *hkdfFinishParamSet = nullptr;
83 ret = InitParamSet(&hkdfParamSet, g_hkdfParams001, sizeof(g_hkdfParams001) / sizeof(OH_Huks_Param));
84 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
85 // finish paramset
86 ret =
87 InitParamSet(&hkdfFinishParamSet, g_hkdfFinishParams001, sizeof(g_hkdfFinishParams001) / sizeof(OH_Huks_Param));
88 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
89
90 // Init-Update-final
91 HksHkdfDeriveTestNormalCase(keyAlias, genParamSet, hkdfParamSet, hkdfFinishParamSet);
92 /* 3. Delete Key */
93 ret = OH_Huks_DeleteKeyItem(&keyAlias, genParamSet);
94 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
95 struct OH_Huks_Blob deleteKeyAlias = {.size = strlen("HksHKDFDeriveKeyAliasFinalTest001"),
96 .data = (uint8_t *)"HksHKDFDeriveKeyAliasFinalTest001"};
97 ret = OH_Huks_DeleteKeyItem(&deleteKeyAlias, hkdfFinishParamSet);
98 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Delete Final Key failed.";
99
100 OH_Huks_FreeParamSet(&genParamSet);
101 OH_Huks_FreeParamSet(&hkdfParamSet);
102 OH_Huks_FreeParamSet(&hkdfFinishParamSet);
103 }
104 } // namespace Unittest::HkdfDerive