1 /*
2 * Copyright (c) 2023-2023 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 #include "hks_openssl_kdf.h"
26 #include "hks_log.h"
27
28 using namespace testing::ext;
29 namespace OHOS {
30 namespace Security {
31 namespace Huks {
32 namespace UnitTest {
33 namespace {
34
35 } // namespace
36
37 class HksCryptoHalSm3Kdf : public HksCryptoHalCommon, public testing::Test {
38 public:
39 static void SetUpTestCase(void);
40 static void TearDownTestCase(void);
41 void SetUp();
42 void TearDown();
43 protected:
44
RunTestCase(const struct HksBlob * mainKey,struct HksBlob * derivedKey) const45 void RunTestCase(const struct HksBlob *mainKey, struct HksBlob *derivedKey) const
46 {
47 #if defined(_USE_OPENSSL_)
48 struct HksKeyDerivationParam derParam = {
49 .info = {
50 .size = strlen("The factor1"),
51 .data = (uint8_t *)"The factor1"
52 },
53 .digestAlg = HKS_DIGEST_SM3
54 };
55 struct HksKeySpec derivationSpec = { HKS_ALG_GMKDF, HKS_KEY_BYTES(HKS_SM4_KEY_SIZE_128), &derParam };
56 EXPECT_EQ(HksOpensslSmKdf(mainKey, &derivationSpec, derivedKey), HKS_SUCCESS);
57 #endif
58 }
59 };
60
SetUpTestCase(void)61 void HksCryptoHalSm3Kdf::SetUpTestCase(void)
62 {
63 }
64
TearDownTestCase(void)65 void HksCryptoHalSm3Kdf::TearDownTestCase(void)
66 {
67 }
68
SetUp()69 void HksCryptoHalSm3Kdf::SetUp()
70 {
71 EXPECT_EQ(HksCryptoAbilityInit(), 0);
72 }
73
TearDown()74 void HksCryptoHalSm3Kdf::TearDown()
75 {
76 }
77
78 /**
79 * @tc.number : HksCryptoHalSm3Kdf_001
80 * @tc.name : HksCryptoHalSm3Kdf_001
81 * @tc.desc : Using HksOpensslSmKdf kdf key.
82 */
83 HWTEST_F(HksCryptoHalSm3Kdf, HksCryptoHalSm3Kdf_001, Function | SmallTest | Level0)
84 {
85 HKS_LOG_I("enter HksCryptoHalSm3Kdf_001");
86 std::string hexData = "64D20D27D0632957F8028C1E024F6B02EDF23102A566C932AE8BD613A8E865FE58D225EC"
87 "A784AE300A81A2D48281A828E1CEDF11C4219099840265375077BF78";
88 uint32_t dataLen = hexData.size() / HKS_COUNT_OF_HALF;
89
90 HksBlob message = { .size = dataLen, .data = (uint8_t *)HksMalloc(dataLen) };
91 ASSERT_NE(message.data, nullptr);
92 for (uint32_t ii = 0; ii < dataLen; ii++) {
93 message.data[ii] = ReadHex((const uint8_t *)&hexData[HKS_COUNT_OF_HALF * ii]);
94 }
95
96 uint8_t hashData[19] = {0};
97 struct HksBlob hash = { 19, hashData };
98 RunTestCase(&message, &hash);
99 HKS_FREE(message.data);
100 }
101
102 /**
103 * @tc.number : HksCryptoHalSm3Kdf_002
104 * @tc.name : HksCryptoHalSm3Kdf_002
105 * @tc.desc : Using HksOpensslSmKdf kdf key.
106 */
107 HWTEST_F(HksCryptoHalSm3Kdf, HksCryptoHalSm3Kdf_002, Function | SmallTest | Level0)
108 {
109 std::string hexData = "11223344556677881122334455667788";
110 uint32_t dataLen = hexData.size() / HKS_COUNT_OF_HALF;
111
112 HksBlob message = { .size = dataLen, .data = (uint8_t *)HksMalloc(dataLen) };
113 ASSERT_NE(message.data, nullptr);
114 for (uint32_t ii = 0; ii < dataLen; ii++) {
115 message.data[ii] = ReadHex((const uint8_t *)&hexData[HKS_COUNT_OF_HALF * ii]);
116 }
117
118 uint8_t hashData[32] = {0};
119 struct HksBlob hash = { 32, hashData };
120 RunTestCase(&message, &hash);
121 HKS_FREE(message.data);
122 }
123 /**
124 * @tc.number : HksCryptoHalSm3Kdf_003
125 * @tc.name : HksCryptoHalSm3Kdf_003
126 * @tc.desc : Using HksOpensslSmKdf kdf key.
127 */
128 HWTEST_F(HksCryptoHalSm3Kdf, HksCryptoHalSm3Kdf_003, Function | SmallTest | Level0)
129 {
130 std::string hexData = "EDF23102A566C932AE8BD613A8E865FE58D225ECA784AE300A81A2D48"
131 "281A828E1CEDF11C4219099840265375077BF78";
132 uint32_t dataLen = hexData.size() / HKS_COUNT_OF_HALF;
133
134 HksBlob message = { .size = dataLen, .data = (uint8_t *)HksMalloc(dataLen) };
135 ASSERT_NE(message.data, nullptr);
136 for (uint32_t ii = 0; ii < dataLen; ii++) {
137 message.data[ii] = ReadHex((const uint8_t *)&hexData[HKS_COUNT_OF_HALF * ii]);
138 }
139
140 uint8_t hashData[46] = {0};
141 struct HksBlob hash = { 46, hashData };
142 RunTestCase(&message, &hash);
143 HKS_FREE(message.data);
144 }
145 } // namespace UnitTest
146 } // namespace Huks
147 } // namespace Security
148 } // namespace OHOS