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