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_agree_ecdh_test.h"
17
18 #include <gtest/gtest.h>
19
20 #include "huks_agree_ecdh_test_common.h"
21
22 using namespace testing::ext;
23 namespace Unittest::EcdhAgree
24 {
25 class HuksAgreeECDHTest : 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 HuksAgreeECDHTest::SetUpTestCase(void) {}
38
TearDownTestCase(void)39 void HuksAgreeECDHTest::TearDownTestCase(void) {}
40
SetUp()41 void HuksAgreeECDHTest::SetUp() {}
42
TearDown()43 void HuksAgreeECDHTest::TearDown() {}
44
45 static struct OH_Huks_Blob g_keyAlias01001 = {strlen("HksECDHAgreeKeyAliasTest001_1"),
46 (uint8_t *)"HksECDHAgreeKeyAliasTest001_1"};
47 static struct OH_Huks_Blob g_keyAlias02001 = {strlen("HksECDHAgreeKeyAliasTest001_2"),
48 (uint8_t *)"HksECDHAgreeKeyAliasTest001_2"};
49 static struct OH_Huks_Blob g_keyAliasFinal1001 = {strlen("HksECDHAgreeKeyAliasTest001_1_final"),
50 (uint8_t *)"HksECDHAgreeKeyAliasTest001_1_final"};
51 static struct OH_Huks_Blob g_keyAliasFinal2001 = {strlen("HksECDHAgreeKeyAliasTest001_2_final"),
52 (uint8_t *)"HksECDHAgreeKeyAliasTest001_2_final"};
53 static struct OH_Huks_Param g_genParams001[] = {
54 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECC},
55 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE},
56 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256},
57 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE},
58 {.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_NONE},
59 {.tag = OH_HUKS_TAG_BLOCK_MODE, .uint32Param = OH_HUKS_MODE_CBC},
60 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
61 static struct OH_Huks_Param g_agreeParams01Init001[] = {
62 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECDH},
63 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE},
64 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256},
65 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
66 static struct OH_Huks_Param g_agreeParams01Finish001[] = {
67 {.tag = OH_HUKS_TAG_KEY_STORAGE_FLAG, .uint32Param = OH_HUKS_STORAGE_PERSISTENT},
68 {.tag = OH_HUKS_TAG_IS_KEY_ALIAS, .boolParam = true},
69 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES},
70 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_AES_KEY_SIZE_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_KEY_ALIAS, .blob = g_keyAliasFinal1001},
74 {.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_NONE},
75 {.tag = OH_HUKS_TAG_BLOCK_MODE, .uint32Param = OH_HUKS_MODE_CBC},
76 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
77 static struct OH_Huks_Param g_agreeParams02Init001[] = {
78 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECDH},
79 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE},
80 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256},
81 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
82 static struct OH_Huks_Param g_agreeParams02Finish001[] = {
83 {.tag = OH_HUKS_TAG_KEY_STORAGE_FLAG, .uint32Param = OH_HUKS_STORAGE_PERSISTENT},
84 {.tag = OH_HUKS_TAG_IS_KEY_ALIAS, .boolParam = true},
85 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES},
86 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_AES_KEY_SIZE_256},
87 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_DERIVE},
88 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA256},
89 {.tag = OH_HUKS_TAG_KEY_ALIAS, .blob = g_keyAliasFinal2001},
90 {.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_NONE},
91 {.tag = OH_HUKS_TAG_BLOCK_MODE, .uint32Param = OH_HUKS_MODE_CBC},
92 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
93
94 /**
95 * @tc.name: HuksAgreeECDHTest.Security_HUKS_NAPI_Agree_ECDH_0100
96 * @tc.desc: alg-ECDH, pur-AGREE
97 * @tc.type: FUNC
98 */
99 HWTEST_F(HuksAgreeECDHTest, Security_HUKS_NAPI_Agree_ECDH_0100, TestSize.Level1)
100 {
101 struct OH_Huks_ParamSet *genParamSet = nullptr;
102 struct OH_Huks_ParamSet *initParamSet01 = nullptr;
103 struct OH_Huks_ParamSet *finishParamSet01 = nullptr;
104 struct OH_Huks_ParamSet *initParamSet02 = nullptr;
105 struct OH_Huks_ParamSet *finishParamSet02 = nullptr;
106 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(OH_Huks_Param));
107 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(gen) failed.";
108 ret = InitParamSet(&initParamSet01, g_agreeParams01Init001, sizeof(g_agreeParams01Init001) / sizeof(OH_Huks_Param));
109 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(init & update)01 failed.";
110 ret = InitParamSet(&finishParamSet01, g_agreeParams01Finish001,
111 sizeof(g_agreeParams01Finish001) / sizeof(OH_Huks_Param));
112 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(finish)01 failed.";
113 ret = InitParamSet(&initParamSet02, g_agreeParams02Init001, sizeof(g_agreeParams02Init001) / sizeof(OH_Huks_Param));
114 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(init & update)02 failed.";
115 ret = InitParamSet(&finishParamSet02, g_agreeParams02Finish001,
116 sizeof(g_agreeParams02Finish001) / sizeof(OH_Huks_Param));
117 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(finish)02 failed.";
118
119 OH_Huks_Result ret1 = OH_Huks_GenerateKeyItem(&g_keyAlias01001, genParamSet, nullptr);
120 EXPECT_EQ(ret1.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey01 failed.";
121 ret1 = OH_Huks_GenerateKeyItem(&g_keyAlias02001, genParamSet, nullptr);
122 EXPECT_EQ(ret1.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey02 failed.";
123
124 struct OH_Huks_Blob publicKey01 = {.size = OH_HUKS_ECC_KEY_SIZE_256, .data = nullptr};
125 struct OH_Huks_Blob publicKey02 = {.size = OH_HUKS_ECC_KEY_SIZE_256, .data = nullptr};
126 ret = MallocAndCheckBlobData(&publicKey01, publicKey01.size);
127 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc publicKey01 failed.";
128 ret = MallocAndCheckBlobData(&publicKey02, publicKey02.size);
129 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc publicKey02 failed.";
130 ret = HksEcdhAgreeExport(&g_keyAlias01001, &g_keyAlias02001, &publicKey01, &publicKey02, genParamSet);
131 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "ExportKey failed.";
132
133 struct OH_Huks_Blob outData01 = {.size = ECDH_COMMON_SIZE, .data = nullptr};
134 struct OH_Huks_Blob outData02 = {.size = ECDH_COMMON_SIZE, .data = nullptr};
135 ret = MallocAndCheckBlobData(&outData01, outData01.size);
136 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc outData01 failed.";
137 ret = MallocAndCheckBlobData(&outData02, outData02.size);
138 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Malloc outData02 failed.";
139 ret = HksEcdhAgreeFinish(&g_keyAlias01001, &publicKey02, initParamSet01, finishParamSet01, &outData01);
140 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksEcdhAgreeFinish01 failed.";
141 ret = HksEcdhAgreeFinish(&g_keyAlias02001, &publicKey01, initParamSet02, finishParamSet02, &outData02);
142 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "HksEcdhAgreeFinish02 failed.";
143
144 OH_Huks_DeleteKeyItem(&g_keyAlias01001, genParamSet);
145 OH_Huks_DeleteKeyItem(&g_keyAlias02001, genParamSet);
146 OH_Huks_DeleteKeyItem(&g_keyAliasFinal1001, finishParamSet01);
147 OH_Huks_DeleteKeyItem(&g_keyAliasFinal2001, finishParamSet02);
148 HksEcdhAgreeFreeParamSet(genParamSet, initParamSet01, finishParamSet01, initParamSet02, finishParamSet02);
149 HksEcdhAgreeFreeBlob(&publicKey01, &publicKey02, &outData01, &outData02);
150 }
151 } // namespace Unittest::EcdhAgree