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