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 "file_ex.h"
20 #include "hks_ability.h"
21 #include "hks_config.h"
22 #include "hks_crypto_hal.h"
23 #include "hks_crypto_hal_common.h"
24 #include "hks_mem.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 generateKeyResult = HksErrorCode::HKS_SUCCESS;
38 };
39
40 const TestCaseParams HKS_CRYPTO_HAL_DH_001_PARAMS = {
41 .spec = { .algType = HKS_ALG_DH, .keyLen = HKS_DH_KEY_SIZE_2048, },
42 .generateKeyResult = HKS_SUCCESS,
43 };
44
45 const TestCaseParams HKS_CRYPTO_HAL_DH_002_PARAMS = {
46 .spec = { .algType = HKS_ALG_DH, .keyLen = HKS_DH_KEY_SIZE_3072, },
47 .generateKeyResult = HKS_SUCCESS,
48 };
49
50 const TestCaseParams HKS_CRYPTO_HAL_DH_003_PARAMS = {
51 .spec = { .algType = HKS_ALG_DH, .keyLen = HKS_DH_KEY_SIZE_4096, },
52 .generateKeyResult = HKS_SUCCESS,
53 };
54 } // namespace
55
56 class HksCryptoHalDh : public HksCryptoHalCommon, public testing::Test {
57 public:
58 static void SetUpTestCase(void);
59 static void TearDownTestCase(void);
60 void SetUp();
61 void TearDown();
62 protected:
RunTestCase(const TestCaseParams & testCaseParams) const63 void RunTestCase(const TestCaseParams &testCaseParams) const
64 {
65 HksBlob key = { 0, NULL };
66 EXPECT_EQ(HksCryptoHalGenerateKey(&testCaseParams.spec, &key), testCaseParams.generateKeyResult);
67 HKS_FREE_BLOB(key);
68 }
69 };
70
SetUpTestCase(void)71 void HksCryptoHalDh::SetUpTestCase(void)
72 {
73 }
74
TearDownTestCase(void)75 void HksCryptoHalDh::TearDownTestCase(void)
76 {
77 }
78
SetUp()79 void HksCryptoHalDh::SetUp()
80 {
81 EXPECT_EQ(HksCryptoAbilityInit(), 0);
82 }
83
TearDown()84 void HksCryptoHalDh::TearDown()
85 {
86 }
87
88 /**
89 * @tc.number : HksCryptoHalDh_001
90 * @tc.name : HksCryptoHalDh_001
91 * @tc.desc : Generate Dh-2048 key pair
92 */
93 HWTEST_F(HksCryptoHalDh, HksCryptoHalDh_001, Function | SmallTest | Level0)
94 {
95 RunTestCase(HKS_CRYPTO_HAL_DH_001_PARAMS);
96 }
97
98 /**
99 * @tc.number : HksCryptoHalDh_002
100 * @tc.name : HksCryptoHalDh_002
101 * @tc.desc : Generate Dh-3072 key pair
102 */
103 HWTEST_F(HksCryptoHalDh, HksCryptoHalDh_002, Function | SmallTest | Level0)
104 {
105 RunTestCase(HKS_CRYPTO_HAL_DH_002_PARAMS);
106 }
107
108 /**
109 * @tc.number : HksCryptoHalDh_003
110 * @tc.name : HksCryptoHalDh_003
111 * @tc.desc : Generate Dh-4096 key pair
112 */
113 HWTEST_F(HksCryptoHalDh, HksCryptoHalDh_003, Function | SmallTest | Level0)
114 {
115 RunTestCase(HKS_CRYPTO_HAL_DH_003_PARAMS);
116 }
117 } // namespace UnitTest
118 } // namespace Huks
119 } // namespace Security
120 } // namespace OHOS
121 #endif