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