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 #include <gtest/gtest.h>
18
19 #include "huks_cipher_sm4_test_common.h"
20
21 using namespace testing::ext;
22 namespace Unittest::Sm4Cipher {
23 class HuksCipherSM4Test : public testing::Test {
24 public:
25 static void SetUpTestCase(void);
26
27 static void TearDownTestCase(void);
28
29 void SetUp();
30
31 void TearDown();
32 };
33
SetUpTestCase(void)34 void HuksCipherSM4Test::SetUpTestCase(void)
35 {
36 }
37
TearDownTestCase(void)38 void HuksCipherSM4Test::TearDownTestCase(void)
39 {
40 }
41
SetUp()42 void HuksCipherSM4Test::SetUp()
43 {
44
45 }
46
TearDown()47 void HuksCipherSM4Test::TearDown()
48 {
49 }
50
51 static struct OH_Huks_Param g_genParams001[] = {
52 {
53 .tag = OH_HUKS_TAG_ALGORITHM,
54 .uint32Param = OH_HUKS_ALG_SM4,
55 }, {
56 .tag = OH_HUKS_TAG_PURPOSE,
57 .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT | OH_HUKS_KEY_PURPOSE_DECRYPT
58 }, {
59 .tag = OH_HUKS_TAG_KEY_SIZE,
60 .uint32Param = OH_HUKS_SM4_KEY_SIZE_128
61 }, {
62 .tag = OH_HUKS_TAG_PADDING,
63 .uint32Param = OH_HUKS_PADDING_NONE
64 }, {
65 .tag = OH_HUKS_TAG_BLOCK_MODE,
66 .uint32Param = OH_HUKS_MODE_CBC
67 }
68 };
69
70 static uint8_t g_hksSm4TestIv[HKS_SM4_IV_SIZE] = {0};
71
72 static struct OH_Huks_Param g_encryptParams001[] = {
73 {
74 .tag = OH_HUKS_TAG_ALGORITHM,
75 .uint32Param = OH_HUKS_ALG_SM4
76 }, {
77 .tag = OH_HUKS_TAG_PURPOSE,
78 .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT
79 }, {
80 .tag = OH_HUKS_TAG_KEY_SIZE,
81 .uint32Param = OH_HUKS_SM4_KEY_SIZE_128
82 }, {
83 .tag = OH_HUKS_TAG_PADDING,
84 .uint32Param = OH_HUKS_PADDING_NONE
85 }, {
86 .tag = OH_HUKS_TAG_BLOCK_MODE,
87 .uint32Param = OH_HUKS_MODE_CBC
88 }, {
89 .tag = OH_HUKS_TAG_IV,
90 .blob = {
91 .size = HKS_SM4_IV_SIZE,
92 .data = (uint8_t *)g_hksSm4TestIv
93 }
94 }
95 };
96
97 static struct OH_Huks_Param g_decryptParams001[] = {
98 {
99 .tag = OH_HUKS_TAG_ALGORITHM,
100 .uint32Param = OH_HUKS_ALG_SM4
101 }, {
102 .tag = OH_HUKS_TAG_PURPOSE,
103 .uint32Param = OH_HUKS_KEY_PURPOSE_DECRYPT
104 }, {
105 .tag = OH_HUKS_TAG_KEY_SIZE,
106 .uint32Param = OH_HUKS_SM4_KEY_SIZE_128
107 }, {
108 .tag = OH_HUKS_TAG_PADDING,
109 .uint32Param = OH_HUKS_PADDING_NONE
110 }, {
111 .tag = OH_HUKS_TAG_BLOCK_MODE,
112 .uint32Param = OH_HUKS_MODE_CBC
113 }, {
114 .tag = OH_HUKS_TAG_IV,
115 .blob = {
116 .size = HKS_SM4_IV_SIZE,
117 .data = (uint8_t *)g_hksSm4TestIv
118 }
119 }
120 };
121
122 /**
123 * @tc.name: HuksCipherSM4Test.Security_HUKS_NAPI_Cipher_SM4_0100
124 * @tc.desc: alg-SM4
125 * @tc.type: FUNC
126 */
127 HWTEST_F(HuksCipherSM4Test, Security_HUKS_NAPI_Cipher_SM4_0100, TestSize.Level0)
128 {
129 char tmpKeyAlias[] = "HksSm4CipherKeyAliasTest001";
130 struct OH_Huks_Blob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
131
132 struct OH_Huks_ParamSet *genParamSet = nullptr;
133 OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams001, sizeof(g_genParams001) / sizeof(OH_Huks_Param));
134 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(gen) failed.";
135
136 struct OH_Huks_ParamSet *encryptParamSet = nullptr;
137 ret = InitParamSet(&encryptParamSet, g_encryptParams001, sizeof(g_encryptParams001) / sizeof(OH_Huks_Param));
138 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(encrypt) failed.";
139
140 struct OH_Huks_ParamSet *decryptParamSet = nullptr;
141 ret = InitParamSet(&decryptParamSet, g_decryptParams001, sizeof(g_decryptParams001) / sizeof(OH_Huks_Param));
142 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(decrypt) failed.";
143
144 ret = HksSm4CipherTestCaseOther(&keyAlias, genParamSet, encryptParamSet, decryptParamSet);
145 EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "this case failed.";
146
147 OH_Huks_FreeParamSet(&genParamSet);
148 OH_Huks_FreeParamSet(&encryptParamSet);
149 OH_Huks_FreeParamSet(&decryptParamSet);
150 }
151 }
152