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