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 {
23 class HksHmacTest : public testing::Test
24 {
25 public:
26 static void SetUpTestCase(void);
27
28 static void TearDownTestCase(void);
29
30 void SetUp();
31
32 void TearDown();
33 };
34
SetUpTestCase(void)35 void HksHmacTest::SetUpTestCase(void) {}
36
TearDownTestCase(void)37 void HksHmacTest::TearDownTestCase(void) {}
38
SetUp()39 void HksHmacTest::SetUp() {}
40
TearDown()41 void HksHmacTest::TearDown() {}
42
43 static struct OH_Huks_Param g_genParams001[] = {
44 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_HMAC},
45 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_MAC},
46 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA384},
47 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = Unittest::Hmac::COMMON_SIZE},
48 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
49 static struct OH_Huks_Param g_hmacParams001[] = {
50 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_HMAC},
51 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_MAC},
52 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SHA384},
53 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
54
55 static struct OH_Huks_Param g_genParams006[] = {
56 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_HMAC},
57 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_MAC},
58 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SM3},
59 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = Unittest::Hmac::COMMON_SIZE},
60 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
61 static struct OH_Huks_Param g_hmacParams006[] = {
62 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_HMAC},
63 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_MAC},
64 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_SM3},
65 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
66
HksHmacTestCase(const struct OH_Huks_Blob * keyAlias,struct OH_Huks_ParamSet * genParamSet,struct OH_Huks_ParamSet * hmacParamSet)67 static OH_Huks_Result HksHmacTestCase(const struct OH_Huks_Blob *keyAlias, struct OH_Huks_ParamSet *genParamSet,
68 struct OH_Huks_ParamSet *hmacParamSet)
69 {
70 struct OH_Huks_Blob inData = {g_inData.length(), (uint8_t *)g_inData.c_str()};
71
72 /* 1. Generate Key */
73 OH_Huks_Result ret = OH_Huks_GenerateKeyItem(keyAlias, genParamSet, nullptr);
74 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
75 return ret;
76 }
77
78 /* 2. HMAC Three Stage */
79 // Init
80 uint8_t handle[sizeof(uint64_t)] = {0};
81 struct OH_Huks_Blob handleHMAC = {sizeof(uint64_t), handle};
82 ret = OH_Huks_InitSession(keyAlias, hmacParamSet, &handleHMAC, nullptr);
83 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
84 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
85 OH_Huks_DeleteKeyItem(keyAlias, genParamSet);
86 return ret;
87 }
88 // Update & Finish
89 uint8_t out[Unittest::Hmac::COMMON_SIZE] = {0};
90 struct OH_Huks_Blob outData = {Unittest::Hmac::COMMON_SIZE, out};
91 ret = TestUpdateFinish(&handleHMAC, hmacParamSet, OH_HUKS_KEY_PURPOSE_MAC, &inData, &outData);
92 if (ret.errorCode != (int32_t)OH_HUKS_SUCCESS) {
93 OH_Huks_DeleteKeyItem(keyAlias, genParamSet);
94 return ret;
95 }
96
97 /* 3. Delete Key */
98 ret = OH_Huks_DeleteKeyItem(keyAlias, genParamSet);
99 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
100 return ret;
101 }
102
103 /**
104 * @tc.name: HksHmacTest.Security_HUKS_NAPI_HMAC_hmac_0100
105 * @tc.desc: alg-HMAC pur-MAC dig-SHA384.
106 * @tc.type: FUNC
107 */
108 HWTEST_F(HksHmacTest, Security_HUKS_NAPI_HMAC_hmac_0100, TestSize.Level1)
109 {
110 char tmpKeyAlias[] = "HksHMACKeyAliasTest001";
111 struct OH_Huks_Blob keyAlias = {strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias};
112
113 struct OH_Huks_ParamSet *genParamSet = nullptr;
114 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(OH_Huks_Param));
115
116 struct OH_Huks_ParamSet *hmacParamSet = nullptr;
117 ret = InitParamSet(&hmacParamSet, g_hmacParams001, sizeof(g_hmacParams001) / sizeof(OH_Huks_Param));
118
119 ret = HksHmacTestCase(&keyAlias, genParamSet, hmacParamSet);
120 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "this case failed.";
121
122 OH_Huks_FreeParamSet(&genParamSet);
123 OH_Huks_FreeParamSet(&hmacParamSet);
124 }
125
126 /**
127 * @tc.name: HksHmacTest.Security_HUKS_NAPI_HMAC_SM3_0100
128 * @tc.desc: alg-HMAC pur-MAC dig-sm3.
129 * @tc.type: FUNC
130 */
131 HWTEST_F(HksHmacTest, Security_HUKS_NAPI_HMAC_SM3_0100, TestSize.Level1)
132 {
133 char tmpKeyAlias[] = "HksHMACKeyAliasTest008";
134 struct OH_Huks_Blob keyAlias = {strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias};
135
136 struct OH_Huks_ParamSet *genParamSet = nullptr;
137 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams006, sizeof(g_genParams006) / sizeof(OH_Huks_Param));
138
139 struct OH_Huks_ParamSet *hmacParamSet = nullptr;
140 ret = InitParamSet(&hmacParamSet, g_hmacParams006, sizeof(g_hmacParams006) / sizeof(OH_Huks_Param));
141
142 ret = HksHmacTestCase(&keyAlias, genParamSet, hmacParamSet);
143 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "this case failed.";
144
145 OH_Huks_FreeParamSet(&genParamSet);
146 OH_Huks_FreeParamSet(&hmacParamSet);
147 }
148 } // namespace Unittest::Hmac