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_pbkdf_test.h"
17 #include "huks_derive_pbkdf_test_common.h"
18
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22 namespace Unittest::Pbkdf2Derive {
23 class HuksDerivePBKDFTest : 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 HuksDerivePBKDFTest::SetUpTestCase(void)
35 {
36 }
37
TearDownTestCase(void)38 void HuksDerivePBKDFTest::TearDownTestCase(void)
39 {
40 }
41
SetUp()42 void HuksDerivePBKDFTest::SetUp()
43 {
44
45 }
46
TearDown()47 void HuksDerivePBKDFTest::TearDown()
48 {
49 }
50
51 uint8_t g_saltgen[16] = {
52 0x14, 0x10, 0x11, 0x3a, 0x27, 0x9e, 0xc8, 0x5f, 0xe0, 0xf3, 0x36, 0x17, 0x57, 0x42, 0x8e, 0xff
53 };
54
55 static struct OH_Huks_Param g_genParams001[] = {
56 {
57 .tag = OH_HUKS_TAG_ALGORITHM,
58 .uint32Param = OH_HUKS_ALG_AES
59 }, {
60 .tag = OH_HUKS_TAG_PURPOSE,
61 .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE
62 }, {
63 .tag = OH_HUKS_TAG_DIGEST,
64 .uint32Param = OH_HUKS_DIGEST_SHA256
65 }, {
66 .tag = OH_HUKS_TAG_KEY_SIZE,
67 .uint32Param = OH_HUKS_AES_KEY_SIZE_256
68 }, {
69 .tag = OH_HUKS_TAG_ITERATION,
70 .uint32Param = 1000
71 }, {
72 .tag = OH_HUKS_TAG_SALT,
73 .blob = {
74 sizeof(g_saltgen),
75 (uint8_t *)g_saltgen
76 }
77 }
78 };
79 static struct OH_Huks_Param g_pbkdf2Params001[] = {
80 {
81 .tag = OH_HUKS_TAG_ALGORITHM,
82 .uint32Param = OH_HUKS_ALG_PBKDF2
83 }, {
84 .tag = OH_HUKS_TAG_PURPOSE,
85 .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE
86 }, {
87 .tag = OH_HUKS_TAG_DIGEST,
88 .uint32Param = OH_HUKS_DIGEST_SHA256
89 }, {
90 .tag = OH_HUKS_TAG_KEY_SIZE,
91 .uint32Param = 256
92 }, {
93 .tag = OH_HUKS_TAG_ITERATION,
94 .int32Param = DERIVE_ITERATION
95 }, {
96 .tag = OH_HUKS_TAG_SALT,
97 .blob = {
98 sizeof(g_saltgen),
99 (uint8_t *)g_saltgen
100 }
101 }, {
102 .tag = OH_HUKS_TAG_DERIVE_KEY_SIZE,
103 .uint32Param = DERIVE_KEY_SIZE_32
104 }
105 };
106 static struct OH_Huks_Param g_pbkdf2FinishParams001[] = {
107 {
108 .tag = OH_HUKS_TAG_KEY_STORAGE_FLAG,
109 .uint32Param = OH_HUKS_STORAGE_PERSISTENT
110 }, {
111 .tag = OH_HUKS_TAG_KEY_ALIAS,
112 .blob = {
113 strlen("HksPBKDF2DeriveKeyAliasTest001_2"),
114 (uint8_t *)"HksPBKDF2DeriveKeyAliasTest001_2"
115 }
116 }, {
117 .tag = OH_HUKS_TAG_ALGORITHM,
118 .uint32Param = OH_HUKS_ALG_AES
119 }, {
120 .tag = OH_HUKS_TAG_KEY_SIZE,
121 .uint32Param = 256
122 }, {
123 .tag = OH_HUKS_TAG_PURPOSE,
124 .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE
125 }, {
126 .tag = OH_HUKS_TAG_DIGEST,
127 .uint32Param = OH_HUKS_DIGEST_SHA256
128 }
129 };
130
131 /**
132 * @tc.name: HuksDerivePBKDFTest.Security_HUKS_NAPI_Derive_pbkdf_0100
133 * @tc.desc: alg-PBKDF2 pur-Derive dig-SHA256.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(HuksDerivePBKDFTest, Security_HUKS_NAPI_Derive_pbkdf_0100, TestSize.Level0)
137 {
138 struct OH_Huks_Blob keyAlias = { strlen("HksPBKDF2DeriveKeyAliasTest001_1"),
139 (uint8_t *)"HksPBKDF2DeriveKeyAliasTest001_1" };
140 /* 1. Generate Key */
141 struct OH_Huks_ParamSet *genParamSet = nullptr;
142 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(OH_Huks_Param));
143 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
144
145 /* 2. PBKDF2 Three Stage */
146 struct OH_Huks_ParamSet *pbkdf2ParamSet = nullptr;
147 struct OH_Huks_ParamSet *pbkdf2FinishParamSet = nullptr;
148 ret = InitParamSet(&pbkdf2ParamSet, g_pbkdf2Params001, sizeof(g_pbkdf2Params001) / sizeof(OH_Huks_Param));
149 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
150 // Finish paramset
151 ret = InitParamSet(&pbkdf2FinishParamSet, g_pbkdf2FinishParams001,
152 sizeof(g_pbkdf2FinishParams001) / sizeof(OH_Huks_Param));
153 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
154
155 // init-update-final
156 HksPbkdf2DeriveTestNormalCase(keyAlias, genParamSet, pbkdf2ParamSet, pbkdf2FinishParamSet);
157 /* 3. Delete Key */
158 ret = OH_Huks_DeleteKeyItem(&keyAlias, genParamSet);
159 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
160 struct OH_Huks_Blob deleteKeyAlias = { .size = strlen("HksPBKDF2DeriveKeyAliasTest001_2"),
161 .data = (uint8_t *)"HksPBKDF2DeriveKeyAliasTest001_2"};
162 ret = OH_Huks_DeleteKeyItem(&deleteKeyAlias, NULL);
163 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Delete Final Key failed.";
164
165 OH_Huks_FreeParamSet(&genParamSet);
166 OH_Huks_FreeParamSet(&pbkdf2ParamSet);
167 OH_Huks_FreeParamSet(&pbkdf2FinishParamSet);
168 }
169 } // namespace Unittest::Pbkdf2Derive