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