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_cipher_sm4_test.h"
17
18 #include <gtest/gtest.h>
19
20 #include "huks_cipher_sm4_test_common.h"
21
22 using namespace testing::ext;
23 namespace Unittest::Sm4Cipher
24 {
25 class HuksCipherSM4Test : public testing::Test
26 {
27 public:
28 static void SetUpTestCase(void);
29
30 static void TearDownTestCase(void);
31
32 void SetUp();
33
34 void TearDown();
35 };
36
SetUpTestCase(void)37 void HuksCipherSM4Test::SetUpTestCase(void) {}
38
TearDownTestCase(void)39 void HuksCipherSM4Test::TearDownTestCase(void) {}
40
SetUp()41 void HuksCipherSM4Test::SetUp() {}
42
TearDown()43 void HuksCipherSM4Test::TearDown() {}
44
45 static struct OH_Huks_Param g_genParams001[] = {
46 {
47 .tag = OH_HUKS_TAG_ALGORITHM,
48 .uint32Param = OH_HUKS_ALG_SM4,
49 },
50 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT | OH_HUKS_KEY_PURPOSE_DECRYPT},
51 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_SM4_KEY_SIZE_128},
52 {.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_NONE},
53 {.tag = OH_HUKS_TAG_BLOCK_MODE, .uint32Param = OH_HUKS_MODE_CBC},
54 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
55
56 static uint8_t g_hksSm4TestIv[HKS_SM4_IV_SIZE] = {0};
57
58 static struct OH_Huks_Param g_encryptParams001[] = {
59 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_SM4},
60 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT},
61 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_SM4_KEY_SIZE_128},
62 {.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_NONE},
63 {.tag = OH_HUKS_TAG_BLOCK_MODE, .uint32Param = OH_HUKS_MODE_CBC},
64 {.tag = OH_HUKS_TAG_IV, .blob = {.size = HKS_SM4_IV_SIZE, .data = (uint8_t *)g_hksSm4TestIv}},
65 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
66
67 static struct OH_Huks_Param g_decryptParams001[] = {
68 {.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_SM4},
69 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_DECRYPT},
70 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_SM4_KEY_SIZE_128},
71 {.tag = OH_HUKS_TAG_PADDING, .uint32Param = OH_HUKS_PADDING_NONE},
72 {.tag = OH_HUKS_TAG_BLOCK_MODE, .uint32Param = OH_HUKS_MODE_CBC},
73 {.tag = OH_HUKS_TAG_IV, .blob = {.size = HKS_SM4_IV_SIZE, .data = (uint8_t *)g_hksSm4TestIv}},
74 {.tag = OH_HUKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = OH_HUKS_AUTH_STORAGE_LEVEL_DE}};
75
76 /**
77 * @tc.name: HuksCipherSM4Test.Security_HUKS_NAPI_Cipher_SM4_0100
78 * @tc.desc: alg-SM4
79 * @tc.type: FUNC
80 */
81 HWTEST_F(HuksCipherSM4Test, Security_HUKS_NAPI_Cipher_SM4_0100, TestSize.Level0)
82 {
83 char tmpKeyAlias[] = "HksSm4CipherKeyAliasTest001";
84 struct OH_Huks_Blob keyAlias = {strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias};
85
86 struct OH_Huks_ParamSet *genParamSet = nullptr;
87 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(OH_Huks_Param));
88 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(gen) failed.";
89
90 struct OH_Huks_ParamSet *encryptParamSet = nullptr;
91 ret = InitParamSet(&encryptParamSet, g_encryptParams001, sizeof(g_encryptParams001) / sizeof(OH_Huks_Param));
92 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(encrypt) failed.";
93
94 struct OH_Huks_ParamSet *decryptParamSet = nullptr;
95 ret = InitParamSet(&decryptParamSet, g_decryptParams001, sizeof(g_decryptParams001) / sizeof(OH_Huks_Param));
96 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(decrypt) failed.";
97
98 ret = HksSm4CipherTestCaseOther(&keyAlias, genParamSet, encryptParamSet, decryptParamSet);
99 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "this case failed.";
100
101 OH_Huks_FreeParamSet(&genParamSet);
102 OH_Huks_FreeParamSet(&encryptParamSet);
103 OH_Huks_FreeParamSet(&decryptParamSet);
104 }
105 } // namespace Unittest::Sm4Cipher
106