• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "huks_hmac_test.h"
17 
18 #include <gtest/gtest.h>
19 
20 using namespace testing::ext;
21 namespace Unittest::Hmac {
22 class HksHmacTest : public testing::Test {
23 public:
24     static void SetUpTestCase(void);
25 
26     static void TearDownTestCase(void);
27 
28     void SetUp();
29 
30     void TearDown();
31 };
32 
SetUpTestCase(void)33 void HksHmacTest::SetUpTestCase(void)
34 {
35 }
36 
TearDownTestCase(void)37 void HksHmacTest::TearDownTestCase(void)
38 {
39 }
40 
SetUp()41 void HksHmacTest::SetUp()
42 {
43 
44 }
45 
TearDown()46 void HksHmacTest::TearDown()
47 {
48 }
49 
50 static struct OH_Huks_Param g_genParams001[] = {
51     {
52         .tag = OH_HUKS_TAG_ALGORITHM,
53         .uint32Param = OH_HUKS_ALG_HMAC
54     }, {
55         .tag = OH_HUKS_TAG_PURPOSE,
56         .uint32Param = OH_HUKS_KEY_PURPOSE_MAC
57     }, {
58         .tag = OH_HUKS_TAG_DIGEST,
59         .uint32Param = OH_HUKS_DIGEST_SHA384
60     }, {
61         .tag = OH_HUKS_TAG_KEY_SIZE,
62         .uint32Param = Unittest::Hmac::COMMON_SIZE
63     }
64 };
65 static struct OH_Huks_Param g_hmacParams001[] = {
66     {
67         .tag = OH_HUKS_TAG_ALGORITHM,
68         .uint32Param = OH_HUKS_ALG_HMAC
69     }, {
70         .tag = OH_HUKS_TAG_PURPOSE,
71         .uint32Param = OH_HUKS_KEY_PURPOSE_MAC
72     }, {
73         .tag = OH_HUKS_TAG_DIGEST,
74         .uint32Param = OH_HUKS_DIGEST_SHA384
75     }
76 };
77 
78 static struct OH_Huks_Param g_genParams006[] = {
79     {
80         .tag = OH_HUKS_TAG_ALGORITHM,
81         .uint32Param = OH_HUKS_ALG_HMAC
82     }, {
83         .tag = OH_HUKS_TAG_PURPOSE,
84         .uint32Param = OH_HUKS_KEY_PURPOSE_MAC
85     }, {
86         .tag = OH_HUKS_TAG_DIGEST,
87         .uint32Param = OH_HUKS_DIGEST_SM3
88     }, {
89         .tag = OH_HUKS_TAG_KEY_SIZE,
90         .uint32Param = Unittest::Hmac::COMMON_SIZE
91     }
92 };
93 static struct OH_Huks_Param g_hmacParams006[] = {
94     {
95         .tag = OH_HUKS_TAG_ALGORITHM,
96         .uint32Param = OH_HUKS_ALG_HMAC
97     }, {
98         .tag = OH_HUKS_TAG_PURPOSE,
99         .uint32Param = OH_HUKS_KEY_PURPOSE_MAC
100     }, {
101         .tag = OH_HUKS_TAG_DIGEST,
102         .uint32Param = OH_HUKS_DIGEST_SM3
103     }
104 };
105 
HksHmacTestCase(const struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * hmacParamSet)106 static OH_Huks_Result HksHmacTestCase(const struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *genParamSet,
107     struct OH_Huks_ParamSet *hmacParamSet)
108 {
109     struct OH_Huks_Blob inData = { g_inData.length(), (uint8_t *)g_inData.c_str() };
110 
111     /* 1. Generate Key */
112     OH_Huks_Result ret = OH_Huks_GenerateKeyItem(keyAlias, genParamSet, nullptr);
113     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
114         return ret;
115     }
116 
117     /* 2. HMAC Three Stage */
118     // Init
119     uint8_t handle[sizeof(uint64_t)] = {0};
120     struct OH_Huks_Blob handleHMAC = { sizeof(uint64_t), handle };
121     ret = OH_Huks_InitSession(keyAlias, hmacParamSet, &handleHMAC, nullptr);
122     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
123     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
124         OH_Huks_DeleteKeyItem(keyAlias, genParamSet);
125         return ret;
126     }
127     // Update & Finish
128     uint8_t out[Unittest::Hmac::COMMON_SIZE] = {0};
129     struct OH_Huks_Blob outData = { Unittest::Hmac::COMMON_SIZE, out };
130     ret = TestUpdateFinish(&handleHMAC, hmacParamSet, OH_HUKS_KEY_PURPOSE_MAC, &inData, &outData);
131     if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
132         OH_Huks_DeleteKeyItem(keyAlias, genParamSet);
133         return ret;
134     }
135 
136     /* 3. Delete Key */
137     ret = OH_Huks_DeleteKeyItem(keyAlias, genParamSet);
138     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
139     return ret;
140 }
141 
142 /**
143  * @tc.name: HksHmacTest.Security_HUKS_NAPI_HMAC_hmac_0100
144  * @tc.desc: alg-HMAC pur-MAC dig-SHA384.
145  * @tc.type: FUNC
146  */
147 HWTEST_F(HksHmacTest, Security_HUKS_NAPI_HMAC_hmac_0100, TestSize.Level0)
148 {
149     char tmpKeyAlias[] = "HksHMACKeyAliasTest001";
150     struct OH_Huks_Blob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
151 
152     struct OH_Huks_ParamSet *genParamSet = nullptr;
153     OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(OH_Huks_Param));
154 
155     struct OH_Huks_ParamSet *hmacParamSet = nullptr;
156     ret = InitParamSet(&hmacParamSet, g_hmacParams001, sizeof(g_hmacParams001) / sizeof(OH_Huks_Param));
157 
158     ret = HksHmacTestCase(&keyAlias, genParamSet, hmacParamSet);
159     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "this case failed.";
160 
161     OH_Huks_FreeParamSet(&genParamSet);
162     OH_Huks_FreeParamSet(&hmacParamSet);
163 }
164 
165 /**
166  * @tc.name: HksHmacTest.Security_HUKS_NAPI_HMAC_SM3_0100
167  * @tc.desc: alg-HMAC pur-MAC dig-sm3.
168  * @tc.type: FUNC
169  */
170 HWTEST_F(HksHmacTest, Security_HUKS_NAPI_HMAC_SM3_0100, TestSize.Level0)
171 {
172     char tmpKeyAlias[] = "HksHMACKeyAliasTest008";
173     struct OH_Huks_Blob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
174 
175     struct OH_Huks_ParamSet *genParamSet = nullptr;
176     OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams006, sizeof(g_genParams006) / sizeof(OH_Huks_Param));
177 
178     struct OH_Huks_ParamSet *hmacParamSet = nullptr;
179     ret = InitParamSet(&hmacParamSet, g_hmacParams006, sizeof(g_hmacParams006) / sizeof(OH_Huks_Param));
180 
181     ret = HksHmacTestCase(&keyAlias, genParamSet, hmacParamSet);
182     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "this case failed.";
183 
184     OH_Huks_FreeParamSet(&genParamSet);
185     OH_Huks_FreeParamSet(&hmacParamSet);
186 }
187 } // namespace Unittest::Hmac