• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "huks_cipher_aes_test.h"
16 #include "huks_cipher_aes_test_common.h"
17 
18 #include <gtest/gtest.h>
19 
20 using namespace testing::ext;
21 namespace Unittest::AesCipher {
22 class HuksCipherAESTest : public testing::Test {
23 public:
24     static void SetUpTestCase(void);
25 
26     static void TearDownTestCase(void);
27 
28     void SetUp();
29 
30     void TearDown();
31 };
32 
SetUpTestCase(void)33 void HuksCipherAESTest::SetUpTestCase(void)
34 {
35 }
36 
TearDownTestCase(void)37 void HuksCipherAESTest::TearDownTestCase(void)
38 {
39 }
40 
SetUp()41 void HuksCipherAESTest::SetUp()
42 {
43 
44 }
45 
TearDown()46 void HuksCipherAESTest::TearDown()
47 {
48 }
49 
50 static struct OH_Huks_Param g_genParams015[] = {
51     {
52         .tag = OH_HUKS_TAG_ALGORITHM,
53         .uint32Param = OH_HUKS_ALG_AES
54     }, {
55         .tag = OH_HUKS_TAG_PURPOSE,
56         .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT | OH_HUKS_KEY_PURPOSE_DECRYPT
57     }, {
58         .tag = OH_HUKS_TAG_KEY_SIZE,
59         .uint32Param = OH_HUKS_AES_KEY_SIZE_256
60     }, {
61         .tag = OH_HUKS_TAG_PADDING,
62         .uint32Param = OH_HUKS_PADDING_NONE
63     }, {
64         .tag = OH_HUKS_TAG_BLOCK_MODE,
65         .uint32Param = OH_HUKS_MODE_CBC
66     }
67 };
68 static struct OH_Huks_Param g_encryptParams015[] = {
69     {
70         .tag = OH_HUKS_TAG_ALGORITHM,
71         .uint32Param = OH_HUKS_ALG_AES
72     }, {
73         .tag = OH_HUKS_TAG_PURPOSE,
74         .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT
75     }, {
76         .tag = OH_HUKS_TAG_KEY_SIZE,
77         .uint32Param = OH_HUKS_AES_KEY_SIZE_256
78     }, {
79         .tag = OH_HUKS_TAG_PADDING,
80         .uint32Param = OH_HUKS_PADDING_NONE
81     }, {
82         .tag = OH_HUKS_TAG_BLOCK_MODE,
83         .uint32Param = OH_HUKS_MODE_CBC
84     }, {
85         .tag = OH_HUKS_TAG_DIGEST,
86         .uint32Param = OH_HUKS_DIGEST_NONE
87     }, {
88         .tag = OH_HUKS_TAG_IV,
89         .blob = {
90             .size = IV_SIZE,
91             .data = (uint8_t *)IV
92         }
93     }
94 };
95 static struct OH_Huks_Param g_decryptParams015[] = {
96     {
97         .tag = OH_HUKS_TAG_ALGORITHM,
98         .uint32Param = OH_HUKS_ALG_AES
99     }, {
100         .tag = OH_HUKS_TAG_PURPOSE,
101         .uint32Param = OH_HUKS_KEY_PURPOSE_DECRYPT
102     }, {
103         .tag = OH_HUKS_TAG_KEY_SIZE,
104         .uint32Param = OH_HUKS_AES_KEY_SIZE_256
105     }, {
106         .tag = OH_HUKS_TAG_PADDING,
107         .uint32Param = OH_HUKS_PADDING_NONE
108     }, {
109         .tag = OH_HUKS_TAG_BLOCK_MODE,
110         .uint32Param = OH_HUKS_MODE_CBC
111     }, {
112         .tag = OH_HUKS_TAG_DIGEST,
113         .uint32Param = OH_HUKS_DIGEST_NONE
114     }, {
115         .tag = OH_HUKS_TAG_IV,
116         .blob = {
117             .size = IV_SIZE,
118             .data = (uint8_t *)IV
119         }
120     }
121 };
122 
123 static struct OH_Huks_Param g_genParams019[] = {
124     {
125         .tag = OH_HUKS_TAG_ALGORITHM,
126         .uint32Param = OH_HUKS_ALG_AES
127     }, {
128         .tag = OH_HUKS_TAG_PURPOSE,
129         .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT | OH_HUKS_KEY_PURPOSE_DECRYPT
130     }, {
131         .tag = OH_HUKS_TAG_KEY_SIZE,
132         .uint32Param = OH_HUKS_AES_KEY_SIZE_256
133     }, {
134         .tag = OH_HUKS_TAG_PADDING,
135         .uint32Param = OH_HUKS_PADDING_NONE
136     }, {
137         .tag = OH_HUKS_TAG_BLOCK_MODE,
138         .uint32Param = OH_HUKS_MODE_CTR
139     }
140 };
141 static struct OH_Huks_Param g_encryptParams019[] = {
142     {
143         .tag = OH_HUKS_TAG_ALGORITHM,
144         .uint32Param = OH_HUKS_ALG_AES
145     }, {
146         .tag = OH_HUKS_TAG_PURPOSE,
147         .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT
148     }, {
149         .tag = OH_HUKS_TAG_KEY_SIZE,
150         .uint32Param = OH_HUKS_AES_KEY_SIZE_256
151     }, {
152         .tag = OH_HUKS_TAG_PADDING,
153         .uint32Param = OH_HUKS_PADDING_NONE
154     }, {
155         .tag = OH_HUKS_TAG_BLOCK_MODE,
156         .uint32Param = OH_HUKS_MODE_CTR
157     }, {
158         .tag = OH_HUKS_TAG_DIGEST,
159         .uint32Param = OH_HUKS_DIGEST_NONE
160     }, {
161         .tag = OH_HUKS_TAG_IV,
162         .blob = {
163             .size = IV_SIZE,
164             .data = (uint8_t *)IV
165         }
166     }
167 };
168 static struct OH_Huks_Param g_decryptParams019[] = {
169     {
170         .tag = OH_HUKS_TAG_ALGORITHM,
171         .uint32Param = OH_HUKS_ALG_AES
172     }, {
173         .tag = OH_HUKS_TAG_PURPOSE,
174         .uint32Param = OH_HUKS_KEY_PURPOSE_DECRYPT
175     }, {
176         .tag = OH_HUKS_TAG_KEY_SIZE,
177         .uint32Param = OH_HUKS_AES_KEY_SIZE_256
178     }, {
179         .tag = OH_HUKS_TAG_PADDING,
180         .uint32Param = OH_HUKS_PADDING_NONE
181     }, {
182         .tag = OH_HUKS_TAG_BLOCK_MODE,
183         .uint32Param = OH_HUKS_MODE_CTR
184     }, {
185         .tag = OH_HUKS_TAG_DIGEST,
186         .uint32Param = OH_HUKS_DIGEST_NONE
187     }, {
188         .tag = OH_HUKS_TAG_IV,
189         .blob = {
190             .size = IV_SIZE,
191             .data = (uint8_t *)IV
192         }
193     }
194 };
195 
196 /**
197  * @tc.name: HuksCipherAESTest.Security_HUKS_NAPI_Cipher_AES_0100
198  * @tc.desc: alg-AES pur-ENCRYPT&DECRYPT mod-CBC pad-NONE size-256.
199  * @tc.type: FUNC
200  */
201 HWTEST_F(HuksCipherAESTest, Security_HUKS_NAPI_Cipher_AES_0100, TestSize.Level0)
202 {
203     char tmpKeyAlias[] = "HksAESCipherKeyAliasTest015";
204     struct OH_Huks_Blob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
205 
206     struct OH_Huks_ParamSet *genParamSet = nullptr;
207     OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams015, sizeof(g_genParams015) / sizeof(OH_Huks_Param));
208     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(gen) failed.";
209 
210     struct OH_Huks_ParamSet *encryptParamSet = nullptr;
211     ret = InitParamSet(&encryptParamSet, g_encryptParams015, sizeof(g_encryptParams015) / sizeof(OH_Huks_Param));
212     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(encrypt) failed.";
213 
214     struct OH_Huks_ParamSet *decryptParamSet = nullptr;
215     ret = InitParamSet(&decryptParamSet, g_decryptParams015, sizeof(g_decryptParams015) / sizeof(OH_Huks_Param));
216     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet(decrypt) failed.";
217 
218     ret = HksAesCipherTestCaseOther(&keyAlias, genParamSet, encryptParamSet, decryptParamSet);
219     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "this case failed.";
220 
221     OH_Huks_FreeParamSet(&genParamSet);
222     OH_Huks_FreeParamSet(&encryptParamSet);
223     OH_Huks_FreeParamSet(&decryptParamSet);
224 }
225 
226 /**
227  * @tc.name: HuksCipherAESTest.Security_HUKS_NAPI_Cipher_AES_0200
228  * @tc.desc: alg-AES pur-ENCRYPT&DECRYPT mod-CBC pad-PKCS7 size-256. Decrypt-Abort
229  * @tc.type: FUNC
230  */
231 HWTEST_F(HuksCipherAESTest, Security_HUKS_NAPI_Cipher_AES_0200, TestSize.Level0)
232 {
233     char tmpKeyAlias[] = "HksAESDecryptKeyAliasTest022";
234     struct OH_Huks_Blob keyAlias = { strlen(tmpKeyAlias), (uint8_t *)tmpKeyAlias };
235     struct OH_Huks_Blob inData = { g_inData.length(),
236                               (uint8_t *)g_inData.c_str() };
237 
238     /* 1. Generate Key */
239     struct OH_Huks_ParamSet *genParamSet = nullptr;
240     OH_Huks_Result ret = InitParamSet(&genParamSet, g_genParams019, sizeof(g_genParams019) / sizeof(OH_Huks_Param));
241     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
242     // Generate Key
243     ret = OH_Huks_GenerateKeyItem(&keyAlias, genParamSet, nullptr);
244     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "GenerateKey failed.";
245 
246     /* 2. Encrypt Three Stage */
247     struct OH_Huks_ParamSet *encryptParamSet = nullptr;
248     ret = InitParamSet(&encryptParamSet, g_encryptParams019, sizeof(g_encryptParams019) / sizeof(OH_Huks_Param));
249     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
250     // Init
251     uint8_t handleE[sizeof(uint64_t)] = {0};
252     struct OH_Huks_Blob handleEncrypt = { sizeof(uint64_t), handleE };
253     ret = OH_Huks_InitSession(&keyAlias, encryptParamSet, &handleEncrypt, nullptr);
254     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
255     // Update & Finish
256     uint8_t cipher[AES_COMMON_SIZE] = {0};
257     struct OH_Huks_Blob cipherText = { AES_COMMON_SIZE, cipher };
258     ret = TestUpdateLoopFinish(&handleEncrypt, encryptParamSet, &inData, &cipherText);
259     EXPECT_NE(HksMemCmp(inData.data, cipherText.data, inData.size), OH_HUKS_SUCCESS) << "cipherText equals inData";
260 
261     /* 3. Decrypt Three Stage */
262     struct OH_Huks_ParamSet *decryptParamSet = nullptr;
263     ret = InitParamSet(&decryptParamSet, g_decryptParams019, sizeof(g_decryptParams019) / sizeof(OH_Huks_Param));
264     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "InitParamSet failed.";
265     // Init
266     uint8_t handleD[sizeof(uint64_t)] = {0};
267     struct OH_Huks_Blob handleDecrypt = { sizeof(uint64_t), handleD };
268     ret = OH_Huks_InitSession(&keyAlias, decryptParamSet, &handleDecrypt, nullptr);
269     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Init failed.";
270     // Update loop
271     ret = HksTestUpdate(&handleDecrypt, decryptParamSet, &cipherText);
272     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Update failed.";
273     // Abort
274     ret = OH_Huks_AbortSession(&handleDecrypt, decryptParamSet);
275     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "Abort failed.";
276 
277     /* 4. Delete Key */
278     ret = OH_Huks_DeleteKeyItem(&keyAlias, genParamSet);
279     EXPECT_EQ(ret.errorCode, (int32_t)OH_HUKS_SUCCESS) << "DeleteKey failed.";
280 
281     OH_Huks_FreeParamSet(&genParamSet);
282     OH_Huks_FreeParamSet(&encryptParamSet);
283     OH_Huks_FreeParamSet(&decryptParamSet);
284 }
285 
286 } // namespace Unittest::AesCipher