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
18 #include <gtest/gtest.h>
19
20 #include "huks_derive_pbkdf_test_common.h"
21
22 using namespace testing::ext;
23 namespace Unittest::Pbkdf2Derive
24 {
25 class HuksDerivePBKDFTest : 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 HuksDerivePBKDFTest::SetUpTestCase(void) {}
38
TearDownTestCase(void)39 void HuksDerivePBKDFTest::TearDownTestCase(void) {}
40
SetUp()41 void HuksDerivePBKDFTest::SetUp() {}
42
TearDown()43 void HuksDerivePBKDFTest::TearDown() {}
44
45 uint8_t g_saltgen[16] = {0x14, 0x10, 0x11, 0x3a, 0x27, 0x9e, 0xc8, 0x5f,
46 0xe0, 0xf3, 0x36, 0x17, 0x57, 0x42, 0x8e, 0xff};
47
48 static struct OH_Huks_Param g_genParams001[] = {
49 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES},
50 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE},
51 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA256},
52 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_AES_KEY_SIZE_256},
53 {.tag = OH_HUKS_TAG_ITERATION, .uint32Param = 1000},
54 {.tag = OH_HUKS_TAG_SALT, .blob = {sizeof(g_saltgen), (uint8_t *)g_saltgen}},
55 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
56 static struct OH_Huks_Param g_pbkdf2Params001[] = {
57 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_PBKDF2},
58 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE},
59 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA256},
60 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = 256},
61 {.tag = OH_HUKS_TAG_ITERATION, .int32Param = DERIVE_ITERATION},
62 {.tag = OH_HUKS_TAG_SALT, .blob = {sizeof(g_saltgen), (uint8_t *)g_saltgen}},
63 {.tag = OH_HUKS_TAG_DERIVE_KEY_SIZE, .uint32Param = DERIVE_KEY_SIZE_32},
64 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
65 static struct OH_Huks_Param g_pbkdf2FinishParams001[] = {
66 {.tag = OH_HUKS_TAG_KEY_STORAGE_FLAG, .uint32Param = OH_HUKS_STORAGE_PERSISTENT},
67 {.tag = OH_HUKS_TAG_KEY_ALIAS,
68 .blob = {strlen("HksPBKDF2DeriveKeyAliasTest001_2"), (uint8_t *)"HksPBKDF2DeriveKeyAliasTest001_2"}},
69 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES},
70 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = 256},
71 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE},
72 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA256},
73 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
74
75 /**
76 * @tc.name: HuksDerivePBKDFTest.Security_HUKS_NAPI_Derive_pbkdf_0100
77 * @tc.desc: alg-PBKDF2 pur-Derive dig-SHA256.
78 * @tc.type: FUNC
79 */
80 HWTEST_F(HuksDerivePBKDFTest, Security_HUKS_NAPI_Derive_pbkdf_0100, TestSize.Level1)
81 {
82 struct OH_Huks_Blob keyAlias = {strlen("HksPBKDF2DeriveKeyAliasTest001_1"),
83 (uint8_t *)"HksPBKDF2DeriveKeyAliasTest001_1"};
84 /* 1. Generate Key */
85 struct OH_Huks_ParamSet *genParamSet = nullptr;
86 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(OH_Huks_Param));
87 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
88
89 /* 2. PBKDF2 Three Stage */
90 struct OH_Huks_ParamSet *pbkdf2ParamSet = nullptr;
91 struct OH_Huks_ParamSet *pbkdf2FinishParamSet = nullptr;
92 ret = InitParamSet(&pbkdf2ParamSet, g_pbkdf2Params001, sizeof(g_pbkdf2Params001) / sizeof(OH_Huks_Param));
93 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
94 // Finish paramset
95 ret = InitParamSet(&pbkdf2FinishParamSet, g_pbkdf2FinishParams001,
96 sizeof(g_pbkdf2FinishParams001) / sizeof(OH_Huks_Param));
97 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
98
99 // init-update-final
100 HksPbkdf2DeriveTestNormalCase(keyAlias, genParamSet, pbkdf2ParamSet, pbkdf2FinishParamSet);
101 /* 3. Delete Key */
102 ret = OH_Huks_DeleteKeyItem(&keyAlias, genParamSet);
103 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
104 struct OH_Huks_Blob deleteKeyAlias = {.size = strlen("HksPBKDF2DeriveKeyAliasTest001_2"),
105 .data = (uint8_t *)"HksPBKDF2DeriveKeyAliasTest001_2"};
106 ret = OH_Huks_DeleteKeyItem(&deleteKeyAlias, pbkdf2FinishParamSet);
107 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Delete Final Key failed.";
108
109 OH_Huks_FreeParamSet(&genParamSet);
110 OH_Huks_FreeParamSet(&pbkdf2ParamSet);
111 OH_Huks_FreeParamSet(&pbkdf2FinishParamSet);
112 }
113 } // namespace Unittest::Pbkdf2Derive