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 const uint32_t HMAC_KEY_SIZE = 256;
32 class HksCryptoHalHmacKey : public HksCryptoHalCommon, public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35     static void TearDownTestCase(void);
36     void SetUp();
37     void TearDown();
38 };
39 
SetUpTestCase(void)40 void HksCryptoHalHmacKey::SetUpTestCase(void)
41 {
42 }
43 
TearDownTestCase(void)44 void HksCryptoHalHmacKey::TearDownTestCase(void)
45 {
46 }
47 
SetUp()48 void HksCryptoHalHmacKey::SetUp()
49 {
50     EXPECT_EQ(HksCryptoAbilityInit(), 0);
51 }
52 
TearDown()53 void HksCryptoHalHmacKey::TearDown()
54 {
55 }
56 
57 /**
58  * @tc.number    : HksCryptoHalHmacKey_001
59  * @tc.name      : HksCryptoHalHmacKey_001
60  * @tc.desc      : Using HksCryptoHalGenerateKey Generate HMAC-256bit key.
61  */
62 HWTEST_F(HksCryptoHalHmacKey, HksCryptoHalHmacKey_001, Function | SmallTest | Level0)
63 {
64     int32_t ret;
65 
66     HksKeySpec spec = {
67         .algType = HKS_ALG_HMAC,
68         .keyLen = HMAC_KEY_SIZE,
69         .algParam = nullptr,
70     };
71 
72     HksBlob key = { .size = 0, .data = nullptr };
73 
74     ret = HksCryptoHalGenerateKey(&spec, &key);
75 #if defined(HKS_SUPPORT_HMAC_C) && defined(HKS_SUPPORT_HMAC_GENERATE_KEY)
76     ASSERT_EQ(HKS_SUCCESS, ret);
77     ASSERT_NE((uint32_t)0, key.size);
78     ASSERT_NE(nullptr, key.data);
79     HKS_FREE(key.data);
80 #else
81     ASSERT_EQ(HKS_ERROR_NOT_SUPPORTED, ret);
82 #endif
83 }
84 }  // namespace UnitTest
85 }  // namespace Huks
86 }  // namespace Security
87 }  // namespace OHOS