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 using namespace testing::ext;
27 namespace OHOS {
28 namespace Security {
29 namespace Huks {
30 namespace UnitTest {
31 namespace {
32 struct TestCaseParams {
33 HksKeySpec spec = {0};
34
35 HksErrorCode generateKeyResult = HksErrorCode::HKS_SUCCESS;
36 };
37
38 const TestCaseParams HKS_CRYPTO_HAL_AES_KEY_001_PARAMS = {
39 .spec = {
40 .algType = HKS_ALG_AES,
41 .keyLen = HKS_AES_KEY_SIZE_128,
42 .algParam = nullptr,
43 },
44 #if defined(HKS_SUPPORT_AES_C) && defined(HKS_SUPPORT_AES_GENERATE_KEY)
45 .generateKeyResult = HKS_SUCCESS,
46 #else
47 .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
48 #endif
49 };
50
51 const TestCaseParams HKS_CRYPTO_HAL_AES_KEY_002_PARAMS = {
52 .spec = {
53 .algType = HKS_ALG_AES,
54 .keyLen = HKS_AES_KEY_SIZE_192,
55 .algParam = nullptr,
56 },
57 #if defined(HKS_SUPPORT_AES_C) && defined(HKS_SUPPORT_AES_GENERATE_KEY)
58 .generateKeyResult = HKS_SUCCESS,
59 #else
60 .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
61 #endif
62 };
63
64 const TestCaseParams HKS_CRYPTO_HAL_AES_KEY_003_PARAMS = {
65 .spec = {
66 .algType = HKS_ALG_AES,
67 .keyLen = HKS_AES_KEY_SIZE_256,
68 .algParam = nullptr,
69 },
70 #if defined(HKS_SUPPORT_AES_C) && defined(HKS_SUPPORT_AES_GENERATE_KEY)
71 .generateKeyResult = HKS_SUCCESS,
72 #else
73 .generateKeyResult = HKS_ERROR_NOT_SUPPORTED,
74 #endif
75 };
76 } // namespace
77
78 class HksCryptoHalAesKey : public HksCryptoHalCommon, public testing::Test {
79 public:
80 static void SetUpTestCase(void);
81 static void TearDownTestCase(void);
82 void SetUp();
83 void TearDown();
84 protected:
RunTestCase(const TestCaseParams & testCaseParams) const85 void RunTestCase(const TestCaseParams &testCaseParams) const
86 {
87 HksBlob keyTest01 = { .size = 0, .data = nullptr };
88 ASSERT_EQ(HksCryptoHalGenerateKey(&testCaseParams.spec, &keyTest01), testCaseParams.generateKeyResult);
89 if (testCaseParams.generateKeyResult == HKS_SUCCESS) {
90 ASSERT_NE((uint32_t)0, keyTest01.size);
91 ASSERT_NE(nullptr, keyTest01.data);
92 HKS_FREE(keyTest01.data);
93 }
94 }
95 };
96
SetUpTestCase(void)97 void HksCryptoHalAesKey::SetUpTestCase(void)
98 {
99 }
100
TearDownTestCase(void)101 void HksCryptoHalAesKey::TearDownTestCase(void)
102 {
103 }
104
SetUp()105 void HksCryptoHalAesKey::SetUp()
106 {
107 EXPECT_EQ(HksCryptoAbilityInit(), 0);
108 }
109
TearDown()110 void HksCryptoHalAesKey::TearDown()
111 {
112 }
113
114 /**
115 * @tc.number : HksCryptoHalAesKey_001
116 * @tc.name : HksCryptoHalAesKey_001
117 * @tc.desc : Using HksCryptoHalGenerateKey Generate AES-128bit key.
118 */
119 HWTEST_F(HksCryptoHalAesKey, HksCryptoHalAesKey_001, Function | SmallTest | Level0)
120 {
121 RunTestCase(HKS_CRYPTO_HAL_AES_KEY_001_PARAMS);
122 }
123
124 /**
125 * @tc.number : HksCryptoHalAesKey_002
126 * @tc.name : HksCryptoHalAesKey_002
127 * @tc.desc : Using HksCryptoHalGenerateKey Generate AES-192bit key.
128 */
129 HWTEST_F(HksCryptoHalAesKey, HksCryptoHalAesKey_002, Function | SmallTest | Level0)
130 {
131 RunTestCase(HKS_CRYPTO_HAL_AES_KEY_002_PARAMS);
132 }
133
134 /**
135 * @tc.number : HksCryptoHalAesKey_003
136 * @tc.name : HksCryptoHalAesKey_003
137 * @tc.desc : Using HksCryptoHalGenerateKey Generate AES-256bit key.
138 */
139 HWTEST_F(HksCryptoHalAesKey, HksCryptoHalAesKey_003, Function | SmallTest | Level0)
140 {
141 RunTestCase(HKS_CRYPTO_HAL_AES_KEY_003_PARAMS);
142 }
143 } // namespace UnitTest
144 } // namespace Huks
145 } // namespace Security
146 } // namespace OHOS