1 /*
2 * Copyright (c) 2021-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 <gtest/gtest.h>
17 #include <iostream>
18
19 #include "hks_ability.h"
20 #include "hks_config.h"
21 #include "hks_crypto_hal.h"
22 #include "hks_crypto_hal_common.h"
23 #include "hks_mem.h"
24
25 #ifdef HKS_SUPPORT_DH_C
26
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace Security {
30 namespace Huks {
31 namespace UnitTest {
32 namespace {
33 struct TestCaseParams {
34 HksKeySpec spec = {0};
35
36 HksErrorCode generateKeyResult = HksErrorCode::HKS_SUCCESS;
37 HksErrorCode agreeResult = HksErrorCode::HKS_SUCCESS;
38 };
39
40 const uint32_t ALISE_KEY_SIZE = 4096;
41 const uint32_t BOB_KEY_SIZE = 4096;
42 const TestCaseParams HKS_CRYPTO_HAL_DH_AGREE_001_PARAMS = {
43 .spec = {
44 .algType = HKS_ALG_DH,
45 .keyLen = HKS_DH_KEY_SIZE_2048,
46 .algParam = nullptr,
47 },
48 .generateKeyResult = HKS_SUCCESS,
49 .agreeResult = HKS_SUCCESS,
50 };
51
52 const TestCaseParams HKS_CRYPTO_HAL_DH_AGREE_002_PARAMS = {
53 .spec = {
54 .algType = HKS_ALG_DH,
55 .keyLen = HKS_DH_KEY_SIZE_3072,
56 .algParam = nullptr,
57 },
58 .generateKeyResult = HKS_SUCCESS,
59 .agreeResult = HKS_SUCCESS,
60 };
61
62 const TestCaseParams HKS_CRYPTO_HAL_DH_AGREE_003_PARAMS = {
63 .spec = {
64 .algType = HKS_ALG_DH,
65 .keyLen = HKS_DH_KEY_SIZE_4096,
66 .algParam = nullptr,
67 },
68 .generateKeyResult = HKS_SUCCESS,
69 .agreeResult = HKS_SUCCESS,
70 };
71 } // namespace
72
73 class HksCryptoHalDhAgree : public HksCryptoHalCommon, public testing::Test {
74 public:
75 static void SetUpTestCase(void);
76 static void TearDownTestCase(void);
77 void SetUp();
78 void TearDown();
79 protected:
RunTestCase(const TestCaseParams & testCaseParams) const80 void RunTestCase(const TestCaseParams &testCaseParams) const
81 {
82 HksBlob alise = { .size = 0, .data = nullptr };
83 HksBlob bob = { .size = 0, .data = nullptr };
84
85 EXPECT_EQ(HksCryptoHalGenerateKey(&testCaseParams.spec, &alise), HKS_SUCCESS);
86 EXPECT_EQ(HksCryptoHalGenerateKey(&testCaseParams.spec, &bob), HKS_SUCCESS);
87
88 struct HksBlob pubKeyAlise = { .size = ALISE_KEY_SIZE, .data = (uint8_t *)HksMalloc(ALISE_KEY_SIZE) };
89 ASSERT_NE(pubKeyAlise.data, nullptr);
90 struct HksBlob pubKeyBob = { .size = BOB_KEY_SIZE, .data = (uint8_t *)HksMalloc(BOB_KEY_SIZE) };
91 ASSERT_NE(pubKeyBob.data, nullptr);
92
93 EXPECT_EQ(HksCryptoHalGetPubKey(&alise, &pubKeyAlise), HKS_SUCCESS);
94 EXPECT_EQ(HksCryptoHalGetPubKey(&bob, &pubKeyBob), HKS_SUCCESS);
95
96 struct HksBlob agreeKeyAlise = { .size = ALISE_KEY_SIZE, .data = (uint8_t *)HksMalloc(ALISE_KEY_SIZE) };
97 ASSERT_NE(agreeKeyAlise.data, nullptr);
98 struct HksBlob agreeKeyBob = { .size = BOB_KEY_SIZE, .data = (uint8_t *)HksMalloc(BOB_KEY_SIZE) };
99 ASSERT_NE(agreeKeyBob.data, nullptr);
100
101 EXPECT_EQ(HksCryptoHalAgreeKey(&alise, &pubKeyBob, &testCaseParams.spec, &agreeKeyAlise), HKS_SUCCESS);
102 EXPECT_EQ(HksCryptoHalAgreeKey(&bob, &pubKeyAlise, &testCaseParams.spec, &agreeKeyBob), HKS_SUCCESS);
103
104 EXPECT_EQ(agreeKeyAlise.size, agreeKeyBob.size);
105 EXPECT_EQ(HksMemCmp(agreeKeyAlise.data, agreeKeyBob.data, agreeKeyAlise.size), HKS_SUCCESS);
106
107 HksFree(alise.data);
108 HksFree(bob.data);
109 HksFree(pubKeyAlise.data);
110 HksFree(pubKeyBob.data);
111 HksFree(agreeKeyAlise.data);
112 HksFree(agreeKeyBob.data);
113 }
114 };
115
SetUpTestCase(void)116 void HksCryptoHalDhAgree::SetUpTestCase(void)
117 {
118 }
119
TearDownTestCase(void)120 void HksCryptoHalDhAgree::TearDownTestCase(void)
121 {
122 }
123
SetUp()124 void HksCryptoHalDhAgree::SetUp()
125 {
126 EXPECT_EQ(HksCryptoAbilityInit(), 0);
127 }
128
TearDown()129 void HksCryptoHalDhAgree::TearDown()
130 {
131 }
132
133 /**
134 * @tc.number : HksCryptoHalDhAgree_001
135 * @tc.name : HksCryptoHalDhAgree_001
136 * @tc.desc : Using HksCryptoHalAgreeKey Agree DH-2048 key.
137 */
138 HWTEST_F(HksCryptoHalDhAgree, HksCryptoHalDhAgree_001, Function | SmallTest | Level0)
139 {
140 RunTestCase(HKS_CRYPTO_HAL_DH_AGREE_001_PARAMS);
141 }
142
143 /**
144 * @tc.number : HksCryptoHalDhAgree_002
145 * @tc.name : HksCryptoHalDhAgree_002
146 * @tc.desc : Using HksCryptoHalAgreeKey Agree DH-3072 key.
147 */
148 HWTEST_F(HksCryptoHalDhAgree, HksCryptoHalDhAgree_002, Function | SmallTest | Level0)
149 {
150 RunTestCase(HKS_CRYPTO_HAL_DH_AGREE_002_PARAMS);
151 }
152
153 /**
154 * @tc.number : HksCryptoHalDhAgree_003
155 * @tc.name : HksCryptoHalDhAgree_003
156 * @tc.desc : Using HksCryptoHalAgreeKey Agree DH-4096 key.
157 */
158 HWTEST_F(HksCryptoHalDhAgree, HksCryptoHalDhAgree_003, Function | SmallTest | Level0)
159 {
160 RunTestCase(HKS_CRYPTO_HAL_DH_AGREE_003_PARAMS);
161 }
162 } // namespace UnitTest
163 } // namespace Huks
164 } // namespace Security
165 } // namespace OHOS
166 #endif