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