1 /*
2 * Copyright (C) 2025 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 "crypto_common.h"
18 #include "crypto_sym_key.h"
19 #include "crypto_mac.h"
20 #include "log.h"
21 #include "memory.h"
22 #include "memory_mock.h"
23
24 using namespace std;
25 using namespace testing::ext;
26
27 namespace {
28 class NativeMacTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp();
33 void TearDown();
34 };
35
SetUpTestCase()36 void NativeMacTest::SetUpTestCase() {}
TearDownTestCase()37 void NativeMacTest::TearDownTestCase() {}
38
SetUp()39 void NativeMacTest::SetUp() // add init here, this will be called before test.
40 {
41 }
42
TearDown()43 void NativeMacTest::TearDown() // add destroy here, this will be called when test case done.
44 {
45 }
46
GenSymKey(const char * algoName)47 static OH_CryptoSymKey *GenSymKey(const char *algoName)
48 {
49 OH_CryptoSymKeyGenerator *keyGen = nullptr;
50 OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create(algoName, &keyGen);
51 if (ret != CRYPTO_SUCCESS) {
52 return nullptr;
53 }
54 OH_CryptoSymKey *keyCtx = nullptr;
55 ret = OH_CryptoSymKeyGenerator_Generate(keyGen, &keyCtx);
56 OH_CryptoSymKeyGenerator_Destroy(keyGen);
57 if (ret != CRYPTO_SUCCESS) {
58 return nullptr;
59 }
60 return keyCtx;
61 }
62
CalculateMacTest(OH_CryptoMac * ctx,OH_CryptoSymKey * keyCtx)63 static OH_Crypto_ErrCode CalculateMacTest(OH_CryptoMac *ctx, OH_CryptoSymKey *keyCtx)
64 {
65 OH_Crypto_ErrCode ret = OH_CryptoMac_Init(ctx, keyCtx);
66 if (ret != CRYPTO_SUCCESS) {
67 return ret;
68 }
69 const char *data = "hello world";
70 const char *data2 = "hello openharmony";
71 const Crypto_DataBlob in = {.data = reinterpret_cast<uint8_t *>(const_cast<char *>(data)), .len = strlen(data)};
72 const Crypto_DataBlob in2 = {.data = reinterpret_cast<uint8_t *>(const_cast<char *>(data2)), .len = strlen(data2)};
73 ret = OH_CryptoMac_Update(ctx, &in);
74 if (ret != CRYPTO_SUCCESS) {
75 return ret;
76 }
77 ret = OH_CryptoMac_Update(ctx, &in2);
78 if (ret != CRYPTO_SUCCESS) {
79 return ret;
80 }
81
82 Crypto_DataBlob out = {0};
83 ret = OH_CryptoMac_Final(ctx, &out);
84 if (ret != CRYPTO_SUCCESS) {
85 return ret;
86 }
87 OH_Crypto_FreeDataBlob(&out);
88 uint32_t macLen = 0;
89 ret = OH_CryptoMac_GetLength(ctx, &macLen);
90 if (ret != CRYPTO_SUCCESS) {
91 return ret;
92 }
93 return CRYPTO_SUCCESS;
94 }
95
96 HWTEST_F(NativeMacTest, NativeMacTest001, TestSize.Level0)
97 {
98 OH_CryptoSymKey *keyCtx = GenSymKey("HMAC|SM3");
99 ASSERT_NE(keyCtx, nullptr);
100
101 OH_CryptoMac *ctx = nullptr;
102 OH_Crypto_ErrCode ret = OH_CryptoMac_Create("HMAC", &ctx);
103 EXPECT_EQ(ret, CRYPTO_SUCCESS);
104 const char *digestName = "SM3";
105 Crypto_DataBlob digestNameData = {.data = reinterpret_cast<uint8_t *>(const_cast<char *>(digestName)),
106 .len = strlen(digestName)};
107 ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_DIGEST_NAME_STR, &digestNameData);
108 EXPECT_EQ(ret, CRYPTO_SUCCESS);
109 ret = CalculateMacTest(ctx, keyCtx);
110 EXPECT_EQ(ret, CRYPTO_SUCCESS);
111
112 OH_CryptoMac_Destroy(ctx);
113 OH_CryptoSymKey_Destroy(keyCtx);
114 }
115
116 HWTEST_F(NativeMacTest, NativeMacTest002, TestSize.Level0)
117 {
118 OH_CryptoSymKey *keyCtx = GenSymKey("AES128");
119 ASSERT_NE(keyCtx, nullptr);
120
121 OH_CryptoMac *ctx = nullptr;
122 OH_Crypto_ErrCode ret = OH_CryptoMac_Create("CMAC", &ctx);
123 EXPECT_EQ(ret, CRYPTO_SUCCESS);
124 const char *cipherName = "AES128";
125 Crypto_DataBlob cipherNameData = {.data = reinterpret_cast<uint8_t *>(const_cast<char *>(cipherName)),
126 .len = strlen(cipherName)};
127 ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_CIPHER_NAME_STR, &cipherNameData);
128 EXPECT_EQ(ret, CRYPTO_SUCCESS);
129 ret = CalculateMacTest(ctx, keyCtx);
130 EXPECT_EQ(ret, CRYPTO_SUCCESS);
131
132 OH_CryptoMac_Destroy(ctx);
133 OH_CryptoSymKey_Destroy(keyCtx);
134 }
135
136 HWTEST_F(NativeMacTest, NativeMacTest003, TestSize.Level0)
137 {
138 OH_CryptoMac *ctx = nullptr;
139 OH_Crypto_ErrCode ret = OH_CryptoMac_Create(nullptr, &ctx);
140 EXPECT_NE(ret, CRYPTO_SUCCESS);
141 ret = OH_CryptoMac_Create("HMAC", nullptr);
142 EXPECT_NE(ret, CRYPTO_SUCCESS);
143 ret = OH_CryptoMac_Create("XMAC", &ctx);
144 EXPECT_NE(ret, CRYPTO_SUCCESS);
145 ret = OH_CryptoMac_Create("HMAC", &ctx);
146 EXPECT_EQ(ret, CRYPTO_SUCCESS);
147
148 OH_CryptoMac_Destroy(ctx);
149 }
150
151 HWTEST_F(NativeMacTest, NativeMacTest004, TestSize.Level0)
152 {
153 OH_CryptoMac *ctx = nullptr;
154 OH_Crypto_ErrCode ret = OH_CryptoMac_Create("HMAC", &ctx);
155 EXPECT_EQ(ret, CRYPTO_SUCCESS);
156
157 const char *cipherName = "AES128";
158 Crypto_DataBlob cipherNameData = {.data = reinterpret_cast<uint8_t *>(const_cast<char *>(cipherName)),
159 .len = strlen(cipherName)};
160 ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_CIPHER_NAME_STR, &cipherNameData);
161 EXPECT_NE(ret, CRYPTO_SUCCESS);
162 const char *digestName = "SM3";
163 Crypto_DataBlob digestNameData = {.data = reinterpret_cast<uint8_t *>(const_cast<char *>(digestName)),
164 .len = strlen(digestName)};
165 ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_DIGEST_NAME_STR, &digestNameData);
166 EXPECT_EQ(ret, CRYPTO_SUCCESS);
167
168 OH_CryptoMac_Destroy(ctx);
169 }
170
171 HWTEST_F(NativeMacTest, NativeMacTest005, TestSize.Level0)
172 {
173 OH_CryptoMac *ctx = nullptr;
174 OH_Crypto_ErrCode ret = OH_CryptoMac_Create("CMAC", &ctx);
175 EXPECT_EQ(ret, CRYPTO_SUCCESS);
176
177 const char *cipherName = "AES128";
178 Crypto_DataBlob cipherNameData = {.data = reinterpret_cast<uint8_t *>(const_cast<char *>(cipherName)),
179 .len = strlen(cipherName)};
180 ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_CIPHER_NAME_STR, &cipherNameData);
181 EXPECT_EQ(ret, CRYPTO_SUCCESS);
182 const char *digestName = "SM3";
183 Crypto_DataBlob digestNameData = {.data = reinterpret_cast<uint8_t *>(const_cast<char *>(digestName)),
184 .len = strlen(digestName)};
185 ret = OH_CryptoMac_SetParam(ctx, CRYPTO_MAC_DIGEST_NAME_STR, &digestNameData);
186 EXPECT_NE(ret, CRYPTO_SUCCESS);
187
188 OH_CryptoMac_Destroy(ctx);
189 }
190
191 HWTEST_F(NativeMacTest, NativeMacTest006, TestSize.Level0)
192 {
193 OH_CryptoMac *ctx = nullptr;
194 OH_Crypto_ErrCode ret = OH_CryptoMac_Create("CMAC", &ctx);
195 EXPECT_EQ(ret, CRYPTO_SUCCESS);
196
197 ret = OH_CryptoMac_Init(ctx, nullptr);
198 EXPECT_NE(ret, CRYPTO_SUCCESS);
199 ret = OH_CryptoMac_Update(ctx, nullptr);
200 EXPECT_NE(ret, CRYPTO_SUCCESS);
201 ret = OH_CryptoMac_Final(ctx, nullptr);
202 EXPECT_NE(ret, CRYPTO_SUCCESS);
203 ret = OH_CryptoMac_GetLength(ctx, nullptr);
204 EXPECT_NE(ret, CRYPTO_SUCCESS);
205
206 OH_CryptoMac_Destroy(ctx);
207 }
208 }