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 "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 
26 using namespace testing::ext;
27 namespace OHOS {
28 namespace Security {
29 namespace Huks {
30 namespace UnitTest {
31 namespace {
32 struct TestCaseParams {
33     HksKeySpec spec = {0};
34     HksKeyDigest digest = HksKeyDigest::HKS_DIGEST_NONE;
35     HksStageType runStage = HksStageType::HKS_STAGE_THREE;
36 
37     HksErrorCode generateKeyResult = HksErrorCode::HKS_SUCCESS;
38     HksErrorCode hmacResult = HksErrorCode::HKS_SUCCESS;
39 };
40 
41 const uint32_t HMAC_KEY_SIZE = 256;
42 const uint32_t SIGNATURE_SIZE = 1024;
43 
44 #ifdef HKS_UNTRUSTED_RUNNING_ENV
45 const TestCaseParams HKS_CRYPTO_HAL_HMAC_HMAC_001_PARAMS = {
46     .spec = {
47         .algType = HKS_ALG_HMAC,
48         .keyLen = HMAC_KEY_SIZE,
49         .algParam = nullptr,
50     },
51     .digest = HKS_DIGEST_SHA1,
52 
53     .generateKeyResult = HKS_SUCCESS,
54     .hmacResult = HKS_SUCCESS,
55 };
56 
57 const TestCaseParams HKS_CRYPTO_HAL_HMAC_HMAC_002_PARAMS = {
58     .spec = {
59         .algType = HKS_ALG_HMAC,
60         .keyLen = HMAC_KEY_SIZE,
61         .algParam = nullptr,
62     },
63     .digest = HKS_DIGEST_SHA224,
64 
65     .generateKeyResult = HKS_SUCCESS,
66     .hmacResult = HKS_SUCCESS,
67 };
68 #endif
69 
70 const TestCaseParams HKS_CRYPTO_HAL_HMAC_HMAC_003_PARAMS = {
71     .spec = {
72         .algType = HKS_ALG_HMAC,
73         .keyLen = HMAC_KEY_SIZE,
74         .algParam = nullptr,
75     },
76     .digest = HKS_DIGEST_SHA256,
77 
78     .generateKeyResult = HKS_SUCCESS,
79     .hmacResult = HKS_SUCCESS,
80 };
81 
82 const TestCaseParams HKS_CRYPTO_HAL_HMAC_HMAC_004_PARAMS = {
83     .spec = {
84         .algType = HKS_ALG_HMAC,
85         .keyLen = HMAC_KEY_SIZE,
86         .algParam = nullptr,
87     },
88     .digest = HKS_DIGEST_SHA384,
89 
90     .generateKeyResult = HKS_SUCCESS,
91     .hmacResult = HKS_SUCCESS,
92 };
93 
94 const TestCaseParams HKS_CRYPTO_HAL_HMAC_HMAC_005_PARAMS = {
95     .spec = {
96         .algType = HKS_ALG_HMAC,
97         .keyLen = HMAC_KEY_SIZE,
98         .algParam = nullptr,
99     },
100     .digest = HKS_DIGEST_SHA512,
101 
102     .generateKeyResult = HKS_SUCCESS,
103     .hmacResult = HKS_SUCCESS,
104 };
105 }  // namespace
106 
107 class HksCryptoHalHmacHmac : public HksCryptoHalCommon, public testing::Test {
108 public:
109     static void SetUpTestCase(void);
110     static void TearDownTestCase(void);
111     void SetUp();
112     void TearDown();
113 protected:
RunTestCase(const TestCaseParams & testCaseParams) const114     void RunTestCase(const TestCaseParams &testCaseParams) const
115     {
116         HksBlob key = { .size = 0, .data = nullptr };
117 
118         EXPECT_EQ(HksCryptoHalGenerateKey(&testCaseParams.spec, &key), testCaseParams.generateKeyResult);
119 
120         const char *hexData = "00112233445566778899aabbccddeeff";
121         uint32_t dataLen = strlen(hexData) / HKS_COUNT_OF_HALF;
122 
123         HksBlob message = { .size = dataLen, .data = (uint8_t *)HksMalloc(dataLen) };
124         ASSERT_NE(message.data, nullptr);
125 
126         for (uint32_t ii = 0; ii < dataLen; ii++) {
127             message.data[ii] = ReadHex((const uint8_t *)&hexData[HKS_COUNT_OF_HALF * ii]);
128         }
129         struct HksBlob signature = { .size = SIGNATURE_SIZE, .data = (uint8_t *)HksMalloc(SIGNATURE_SIZE) };
130         ASSERT_NE(signature.data, nullptr);
131 
132         if (testCaseParams.runStage == HksStageType::HKS_STAGE_THREE) {
133             uint8_t buff[1] = {0};
134             HksBlob endMessage = { .size = 0, .data = buff };
135             void *context = (void *)HksMalloc(1024 * 1024);
136             ASSERT_NE(context, nullptr);
137 
138             EXPECT_EQ(HksCryptoHalHmacInit(&key, testCaseParams.digest, &context), testCaseParams.hmacResult);
139             EXPECT_EQ(HksCryptoHalHmacUpdate(&message, context), testCaseParams.hmacResult);
140             EXPECT_EQ(HksCryptoHalHmacFinal(&endMessage, &context, &signature), testCaseParams.hmacResult);
141         } else {
142             EXPECT_EQ(HksCryptoHalHmac(&key, testCaseParams.digest, &message, &signature), testCaseParams.hmacResult);
143         }
144 
145         HKS_FREE(message.data);
146         HKS_FREE(signature.data);
147         HKS_FREE(key.data);
148     }
149 };
150 
SetUpTestCase(void)151 void HksCryptoHalHmacHmac::SetUpTestCase(void)
152 {
153 }
154 
TearDownTestCase(void)155 void HksCryptoHalHmacHmac::TearDownTestCase(void)
156 {
157 }
158 
SetUp()159 void HksCryptoHalHmacHmac::SetUp()
160 {
161     EXPECT_EQ(HksCryptoAbilityInit(), 0);
162 }
163 
TearDown()164 void HksCryptoHalHmacHmac::TearDown()
165 {
166 }
167 
168 #ifdef HKS_UNTRUSTED_RUNNING_ENV
169 /**
170  * @tc.number    : HksCryptoHalHmacHmac_001
171  * @tc.name      : HksCryptoHalHmacHmac_001
172  * @tc.desc      : Using HksCryptoHalHmac HMAC HMAC-256-SHA1 key.
173  */
174 HWTEST_F(HksCryptoHalHmacHmac, HksCryptoHalHmacHmac_001, Function | SmallTest | Level0)
175 {
176     RunTestCase(HKS_CRYPTO_HAL_HMAC_HMAC_001_PARAMS);
177 }
178 
179 /**
180  * @tc.number    : HksCryptoHalHmacHmac_002
181  * @tc.name      : HksCryptoHalHmacHmac_002
182  * @tc.desc      : Using HksCryptoHalHmac HMAC HMAC-256-SHA224 key.
183  */
184 HWTEST_F(HksCryptoHalHmacHmac, HksCryptoHalHmacHmac_002, Function | SmallTest | Level0)
185 {
186     RunTestCase(HKS_CRYPTO_HAL_HMAC_HMAC_002_PARAMS);
187 }
188 #endif
189 
190 /**
191  * @tc.number    : HksCryptoHalHmacHmac_003
192  * @tc.name      : HksCryptoHalHmacHmac_003
193  * @tc.desc      : Using HksCryptoHalHmac HMAC HMAC-256-SHA256 key.
194  */
195 HWTEST_F(HksCryptoHalHmacHmac, HksCryptoHalHmacHmac_003, Function | SmallTest | Level0)
196 {
197     RunTestCase(HKS_CRYPTO_HAL_HMAC_HMAC_003_PARAMS);
198 }
199 
200 /**
201  * @tc.number    : HksCryptoHalHmacHmac_004
202  * @tc.name      : HksCryptoHalHmacHmac_004
203  * @tc.desc      : Using HksCryptoHalHmac HMAC HMAC-256-SHA384 key.
204  */
205 HWTEST_F(HksCryptoHalHmacHmac, HksCryptoHalHmacHmac_004, Function | SmallTest | Level0)
206 {
207     RunTestCase(HKS_CRYPTO_HAL_HMAC_HMAC_004_PARAMS);
208 }
209 
210 /**
211  * @tc.number    : HksCryptoHalHmacHmac_005
212  * @tc.name      : HksCryptoHalHmacHmac_005
213  * @tc.desc      : Using HksCryptoHalHmac HMAC HMAC-256-SHA512 key.
214  */
215 HWTEST_F(HksCryptoHalHmacHmac, HksCryptoHalHmacHmac_005, Function | SmallTest | Level0)
216 {
217     RunTestCase(HKS_CRYPTO_HAL_HMAC_HMAC_005_PARAMS);
218 }
219 }  // namespace UnitTest
220 }  // namespace Huks
221 }  // namespace Security
222 }  // namespace OHOS