1 /*
2  * Copyright (c) 2022-2023 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 "hks_access_control_test_common.h"
17 #include "hks_aes_cipher_test_common.h"
18 #include "hks_test_adapt_for_de.h"
19 
20 #include <gtest/gtest.h>
21 #include <iostream>
22 #include <thread>
23 #include <chrono>
24 
25 using namespace testing::ext;
26 using namespace Unittest::HksAccessControlPartTest;
27 namespace Unittest::AesCipher {
28 #ifdef L2_STANDARD
HksAesCipherTestEncrypt(const struct HksBlob * keyAlias,const struct HksParamSet * encryptParamSet,const struct HksBlob * inData,struct HksBlob * cipherText)29 int32_t HksAesCipherTestEncrypt(const struct HksBlob *keyAlias,
30     const struct HksParamSet *encryptParamSet, const struct HksBlob *inData, struct HksBlob *cipherText)
31 {
32     uint8_t handleE[sizeof(uint64_t)] = {0};
33     struct HksBlob handleEncrypt = { sizeof(uint64_t), handleE };
34     int32_t ret = HksInitForDe(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
35     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
36     if (ret != HKS_SUCCESS) {
37         return ret;
38     }
39 
40     ret = TestUpdateLoopFinish(&handleEncrypt, encryptParamSet, inData, cipherText);
41     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
42     if (ret != HKS_SUCCESS) {
43         return ret;
44     }
45     EXPECT_NE(HksMemCmp(inData->data, cipherText->data, inData->size), HKS_SUCCESS) << "cipherText equals inData";
46 
47     uint8_t tmpOut[AES_COMMON_SIZE] = {0};
48     struct HksBlob outData = { AES_COMMON_SIZE, tmpOut };
49     ret = HksEncryptForDe(keyAlias, encryptParamSet, inData, &outData);
50     EXPECT_EQ(ret, HKS_SUCCESS) << "HksEncrypt failed.";
51     if (ret != HKS_SUCCESS) {
52         return ret;
53     }
54     EXPECT_EQ(HksMemCmp(outData.data, cipherText->data, outData.size), HKS_SUCCESS) << "cipherText not equals outData";
55 
56     return HKS_SUCCESS;
57 }
58 
HksAesCipherTestEncryptWithoutNonce(const struct HksBlob * keyAlias,struct HksParamSet * encryptParamSet,const struct HksBlob * inData,struct HksBlob * cipherText,bool needAccessControl)59 static int32_t HksAesCipherTestEncryptWithoutNonce(const struct HksBlob *keyAlias,
60     struct HksParamSet *encryptParamSet, const struct HksBlob *inData, struct HksBlob *cipherText,
61     bool needAccessControl)
62 {
63     uint8_t handleE[sizeof(uint64_t)] = {0};
64     uint8_t challenge[32] = {0};
65     struct HksBlob challengeBlob = { 32, challenge };
66     struct HksBlob handleEncrypt = { sizeof(uint64_t), handleE };
67     int32_t ret = HksInitForDe(keyAlias, encryptParamSet, &handleEncrypt, &challengeBlob);
68     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
69     if (ret != HKS_SUCCESS) {
70         return ret;
71     }
72 
73 #ifdef USE_HKS_MOCK
74     if (needAccessControl) {
75         const IDMParams testIDMParams = {
76             .secureUid = 1,
77             .enrolledId = 1,
78             .time = 0,
79             .authType = 1
80         };
81         ret = HksBuildAuthtoken(&encryptParamSet, &challengeBlob, testIDMParams);
82         if (ret != HKS_SUCCESS) {
83             HKS_LOG_I("HksBuildAuthtoken failed, ret : %" LOG_PUBLIC "d", ret);
84             return ret;
85         }
86     }
87 #endif
88 
89     ret = TestUpdateLoopFinish(&handleEncrypt, encryptParamSet, inData, cipherText);
90     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
91     if (ret != HKS_SUCCESS) {
92         return ret;
93     }
94     EXPECT_NE(HksMemCmp(inData->data, cipherText->data, inData->size), HKS_SUCCESS) << "cipherText equals inData";
95 
96     return HKS_SUCCESS;
97 }
98 
HksAesCipherTestDecrypt(const struct HksBlob * keyAlias,const struct HksParamSet * decryptParamSet,const struct HksBlob * cipherText,struct HksBlob * plainText,const struct HksBlob * inData)99 int32_t HksAesCipherTestDecrypt(const struct HksBlob *keyAlias,
100     const struct HksParamSet *decryptParamSet, const struct HksBlob *cipherText, struct HksBlob *plainText,
101     const struct HksBlob *inData)
102 {
103     uint8_t handleD[sizeof(uint64_t)] = {0};
104     struct HksBlob handleDecrypt = { sizeof(uint64_t), handleD };
105     int32_t ret = HksInitForDe(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
106     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
107     if (ret != HKS_SUCCESS) {
108         return ret;
109     }
110 
111     ret = TestUpdateLoopFinish(&handleDecrypt, decryptParamSet, cipherText, plainText);
112     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
113     if (ret != HKS_SUCCESS) {
114         return ret;
115     }
116     EXPECT_EQ(HksMemCmp(inData->data, plainText->data, inData->size), HKS_SUCCESS) << "plainText not equals inData";
117 
118     uint8_t tmpOut[AES_COMMON_SIZE] = {0};
119     struct HksBlob outData = { AES_COMMON_SIZE, tmpOut };
120     ret = HksDecryptForDe(keyAlias, decryptParamSet, cipherText, &outData);
121     EXPECT_EQ(ret, HKS_SUCCESS) << "HksDecrypt failed.";
122     if (ret != HKS_SUCCESS) {
123         return ret;
124     }
125     EXPECT_EQ(HksMemCmp(outData.data, plainText->data, outData.size), HKS_SUCCESS) << "plainText not equals outData";
126 
127     return HKS_SUCCESS;
128 }
129 
HksAesCipherTestParamAbsentCase(const struct HksBlob * keyAlias,struct HksParamSet * genParamSet,struct HksParamSet * encryptParamSet,struct HksParamSet * decryptParamSet)130 int32_t HksAesCipherTestParamAbsentCase(const struct HksBlob *keyAlias, struct HksParamSet *genParamSet,
131     struct HksParamSet *encryptParamSet, struct HksParamSet *decryptParamSet)
132 {
133     (void)decryptParamSet;
134     /* 1. Generate Key */
135     uint32_t ret = HksGenerateKeyForDe(keyAlias, genParamSet, nullptr);
136     EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
137     if (ret != HKS_SUCCESS) {
138         return ret;
139     }
140     /* 2. Encrypt, init will fail */
141     uint8_t handleE[sizeof(uint64_t)] = {0};
142     struct HksBlob handleEncrypt = { sizeof(uint64_t), handleE };
143     ret = HksInitForDe(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
144     EXPECT_NE(ret, HKS_SUCCESS) << "Init failed.";
145 
146     /* 3. Delete Key */
147     EXPECT_EQ(HksDeleteKeyForDe(keyAlias, genParamSet), HKS_SUCCESS) << "DeleteKey failed.";
148     return ret;
149 }
150 
HksAesCipherTestCaseOther(const struct HksBlob * keyAlias,struct HksParamSet * genParamSet,struct HksParamSet * encryptParamSet,struct HksParamSet * decryptParamSet)151 int32_t HksAesCipherTestCaseOther(const struct HksBlob *keyAlias, struct HksParamSet *genParamSet,
152     struct HksParamSet *encryptParamSet, struct HksParamSet *decryptParamSet)
153 {
154     char tmpInData[] = "AES_ECB_INDATA_1";
155     struct HksBlob inData = {
156         g_inData.length(),
157         const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(g_inData.c_str()))
158     };
159 
160     struct HksParam *modeParam = nullptr;
161     int32_t ret = HksGetParam(genParamSet, HKS_TAG_BLOCK_MODE, &modeParam);
162     if ((ret == HKS_SUCCESS) && (modeParam->uint32Param == HKS_MODE_ECB)) {
163         inData.size = strlen(tmpInData);
164         inData.data = reinterpret_cast<uint8_t *>(tmpInData);
165     }
166 
167     /* 1. Generate Key */
168     ret = HksGenerateKeyForDe(keyAlias, genParamSet, nullptr);
169     EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
170     if (ret != HKS_SUCCESS) {
171         return ret;
172     }
173 
174     /* 2. Encrypt */
175     uint8_t cipher[AES_COMMON_SIZE] = {0};
176     struct HksBlob cipherText = { AES_COMMON_SIZE, cipher };
177     ret = HksAesCipherTestEncrypt(keyAlias, encryptParamSet, &inData, &cipherText);
178     EXPECT_EQ(ret, HKS_SUCCESS) << "HksAesCipherTestEncrypt failed.";
179 
180     /* 3. Decrypt Three Stage */
181     uint8_t plain[AES_COMMON_SIZE] = {0};
182     struct HksBlob plainText = { AES_COMMON_SIZE, plain };
183     ret = HksAesCipherTestDecrypt(keyAlias, decryptParamSet, &cipherText, &plainText, &inData);
184     EXPECT_EQ(ret, HKS_SUCCESS) << "HksAesCipherTestDecrypt failed.";
185 
186     /* 3. Delete Key */
187     EXPECT_EQ(HksDeleteKeyForDe(keyAlias, genParamSet), HKS_SUCCESS) << "DeleteKey failed.";
188     return ret;
189 }
190 
HksAesCipherTestCaseGcm2(const struct HksBlob * keyAlias,struct HksParamSet * genParamSet,struct HksParamSet * encryptParamSet,struct HksParamSet * decryptParamSet,struct HksParamSet * decrypt1ParamSet)191 int32_t HksAesCipherTestCaseGcm2(const struct HksBlob *keyAlias, struct HksParamSet *genParamSet,
192     struct HksParamSet *encryptParamSet, struct HksParamSet *decryptParamSet, struct HksParamSet *decrypt1ParamSet)
193 {
194     struct HksBlob inData = {
195         g_inData.length(),
196         const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(g_inData.c_str()))
197     };
198 
199     /* 1. Generate Key */
200     int32_t ret = HksGenerateKeyForDe(keyAlias, genParamSet, nullptr);
201     EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
202     if (ret != HKS_SUCCESS) {
203         return ret;
204     }
205 
206     /* 2. Encrypt Three Stage */
207     uint8_t cipher[AES_COMMON_SIZE] = {0};
208     struct HksBlob cipherText = { AES_COMMON_SIZE, cipher };
209     ret = HksAesCipherTestEncrypt(keyAlias, encryptParamSet, &inData, &cipherText);
210     EXPECT_EQ(ret, HKS_SUCCESS) << "HksAesCipherTestEncrypt failed.";
211 
212     uint8_t plain1[AES_COMMON_SIZE] = {0};
213     struct HksBlob plainText1 = { AES_COMMON_SIZE, plain1 };
214     ret = HksDecryptForDe(keyAlias, decrypt1ParamSet, &cipherText, &plainText1);
215     EXPECT_EQ(ret, HKS_SUCCESS) << "HksDecrypt failed.";
216 
217     cipherText.size -= AEAD_SIZE;
218 
219     uint32_t i = 0;
220     for (i = 0; i < decryptParamSet->paramsCnt; i++) {
221         if (decryptParamSet->params[i].tag == HKS_TAG_AE_TAG) {
222             uint8_t *tempPtrTest = cipherText.data;
223             (void)memcpy_s(decryptParamSet->params[i].blob.data, AEAD_SIZE,
224                 tempPtrTest + cipherText.size, AEAD_SIZE);
225             break;
226         }
227     }
228 
229     /* 3. Decrypt Three Stage */
230     // Init
231     uint8_t handleD[sizeof(uint64_t)] = {0};
232     struct HksBlob handleDecryptTest = { sizeof(uint64_t), handleD };
233     ret = HksInitForDe(keyAlias, decryptParamSet, &handleDecryptTest, nullptr);
234     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
235 
236     // Update & Finish
237     uint8_t plain[AES_COMMON_SIZE] = {0};
238     struct HksBlob plainText = { AES_COMMON_SIZE, plain };
239     ret = TestUpdateLoopFinish(&handleDecryptTest, decryptParamSet, &cipherText, &plainText);
240     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
241     EXPECT_EQ(HksMemCmp(inData.data, plainText.data, inData.size), HKS_SUCCESS) << "plainText not equals inData";
242     EXPECT_EQ(HksMemCmp(plainText1.data, plainText.data, plainText1.size), HKS_SUCCESS) << "plainText != plainText1";
243 
244     /* 3. Delete Key */
245     EXPECT_EQ(HksDeleteKeyForDe(keyAlias, genParamSet), HKS_SUCCESS) << "DeleteKey failed.";
246     return ret;
247 }
248 
HksAesCipherTestCaseGcm3(const struct HksBlob * keyAlias,struct HksParamSet * genParamSet,struct HksParamSet * encryptParamSet,struct HksParamSet * decryptParamSet,bool needAccessControl)249 int32_t HksAesCipherTestCaseGcm3(const struct HksBlob *keyAlias, struct HksParamSet *genParamSet,
250     struct HksParamSet *encryptParamSet, struct HksParamSet *decryptParamSet, bool needAccessControl)
251 {
252     struct HksBlob inData = {
253         g_inData.length(),
254         const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(g_inData.c_str()))
255     };
256 
257     /* 1. Generate Key */
258     int32_t ret = HksGenerateKeyForDe(keyAlias, genParamSet, nullptr);
259     EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
260     if (ret != HKS_SUCCESS) {
261         return ret;
262     }
263 
264     /* 2. Encrypt Three Stage */
265     uint8_t cipher[AES_COMMON_SIZE] = {0};
266     struct HksBlob cipherText = { AES_COMMON_SIZE, cipher };
267     ret = HksAesCipherTestEncryptWithoutNonce(keyAlias, encryptParamSet, &inData, &cipherText, needAccessControl);
268     EXPECT_EQ(ret, HKS_SUCCESS) << "HksAesCipherTestEncrypt failed.";
269 
270     cipherText.size -= NONCE_SIZE;
271     cipherText.size -= AEAD_SIZE;
272 
273     uint32_t i = 0;
274     for (i = 0; i < decryptParamSet->paramsCnt; i++) {
275         if (decryptParamSet->params[i].tag == HKS_TAG_AE_TAG) {
276             uint8_t *tempPtrTest = cipherText.data;
277             (void)memcpy_s(decryptParamSet->params[i].blob.data, AEAD_SIZE,
278                 tempPtrTest + cipherText.size, AEAD_SIZE);
279             break;
280         }
281     }
282 
283     for (i = 0; i < decryptParamSet->paramsCnt; i++) {
284         if (decryptParamSet->params[i].tag == HKS_TAG_NONCE) {
285             uint8_t *tempPtrTest = cipherText.data;
286             (void)memcpy_s(decryptParamSet->params[i].blob.data, NONCE_SIZE,
287                 tempPtrTest + cipherText.size + AEAD_SIZE, NONCE_SIZE);
288             break;
289         }
290     }
291 
292     /* 3. Decrypt Three Stage */
293     // Init
294     uint8_t handleD[sizeof(uint64_t)] = {0};
295     struct HksBlob handleDecryptTest = { sizeof(uint64_t), handleD };
296     ret = HksInitForDe(keyAlias, decryptParamSet, &handleDecryptTest, nullptr);
297     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
298 
299     // Update & Finish
300     uint8_t plain[AES_COMMON_SIZE] = {0};
301     struct HksBlob plainText = { AES_COMMON_SIZE, plain };
302     ret = TestUpdateLoopFinish(&handleDecryptTest, decryptParamSet, &cipherText, &plainText);
303     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
304     EXPECT_EQ(HksMemCmp(inData.data, plainText.data, inData.size), HKS_SUCCESS) << "plainText not equals inData";
305 
306     /* 3. Delete Key */
307     ret = HksDeleteKeyForDe(keyAlias, genParamSet);
308     EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
309     return ret;
310 }
311 
HksAesGcmAppendAeadAndNonce(struct HksParamSet * paramSet,struct HksBlob * cipherText)312 static void HksAesGcmAppendAeadAndNonce(struct HksParamSet *paramSet, struct HksBlob *cipherText)
313 {
314     uint32_t i = 0;
315     for (i = 0; i < paramSet->paramsCnt; i++) {
316         if (paramSet->params[i].tag == HKS_TAG_AE_TAG) {
317             uint8_t *tempPtrTest = cipherText->data;
318             (void)memcpy_s(paramSet->params[i].blob.data, AEAD_SIZE,
319                 tempPtrTest + cipherText->size, AEAD_SIZE);
320             break;
321         }
322     }
323     for (i = 0; i < paramSet->paramsCnt; i++) {
324         if (paramSet->params[i].tag == HKS_TAG_NONCE) {
325             uint8_t *tempPtrTest = cipherText->data;
326             (void)memcpy_s(paramSet->params[i].blob.data, NONCE_SIZE,
327                 tempPtrTest + cipherText->size + AEAD_SIZE, NONCE_SIZE);
328             break;
329         }
330     }
331     return;
332 }
333 
HksAesCipherTestCaseGcm4(const struct HksBlob * keyAlias,struct HksParamSet * genParamSet,struct HksParamSet * encryptParamSet,struct HksParamSet * decryptParamSet,bool isTimeOut)334 int32_t HksAesCipherTestCaseGcm4(const struct HksBlob *keyAlias, struct HksParamSet *genParamSet,
335     struct HksParamSet *encryptParamSet, struct HksParamSet *decryptParamSet, bool isTimeOut)
336 {
337     static const std::string tmp_inData = "Hks_string";
338 
339     struct HksBlob inData = {
340         tmp_inData.length(),
341         const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(tmp_inData.c_str()))
342     };
343 
344     /* 1. Generate Key */
345     int32_t ret = HksGenerateKeyForDe(keyAlias, genParamSet, nullptr);
346     if (ret != HKS_SUCCESS) {
347         return ret;
348     }
349 
350     /* 2. Encrypt Three Stage */
351     uint8_t cipher[AES_COMMON_SIZE] = {0};
352     struct HksBlob cipherText = { AES_COMMON_SIZE, cipher };
353     ret = HksAesCipherTestEncryptWithoutNonce(keyAlias, encryptParamSet, &inData, &cipherText, false);
354     EXPECT_EQ(ret, HKS_SUCCESS) << "HksAesCipherTestEncrypt failed.";
355     if (ret != HKS_SUCCESS) {
356         return ret;
357     }
358 
359     cipherText.size -= NONCE_SIZE;
360     cipherText.size -= AEAD_SIZE;
361 
362     HksAesGcmAppendAeadAndNonce(decryptParamSet, &cipherText);
363 
364     /* 3. Decrypt Three Stage */
365     // Init
366     uint8_t handleD[sizeof(uint64_t)] = {0};
367     struct HksBlob handleDecryptTest = { sizeof(uint64_t), handleD };
368     ret = HksInitForDe(keyAlias, decryptParamSet, &handleDecryptTest, nullptr);
369     if (ret != HKS_SUCCESS) {
370         return ret;
371     }
372 
373     if (isTimeOut) {
374         std::this_thread::sleep_for(std::chrono::seconds(1));
375     }
376 
377     // Update & Finish
378     uint8_t plain[AES_COMMON_SIZE] = {0};
379     struct HksBlob plainText = { AES_COMMON_SIZE, plain };
380     ret = TestBatchUpdateLoopFinish(&handleDecryptTest, decryptParamSet, &cipherText, &plainText);
381     if (isTimeOut == true) {
382         EXPECT_EQ(ret, HKS_ERROR_INVALID_TIME_OUT) << "TestUpdateLoopFinish failed.";
383     } else {
384         EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
385     }
386     if (ret != HKS_SUCCESS) {
387         return ret;
388     }
389     EXPECT_EQ(HksMemCmp(inData.data, plainText.data, inData.size), HKS_SUCCESS) << "plainText not equals inData";
390 
391     /* 3. Delete Key */
392     ret = HksDeleteKeyForDe(keyAlias, genParamSet);
393     EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
394     return ret;
395 }
396 
HksAesEncryptThreeStage(const struct HksBlob * keyAlias,struct HksParamSet * encryptParamSet,const struct HksBlob * inData,struct HksBlob * cipherText)397 int32_t HksAesEncryptThreeStage(const struct HksBlob *keyAlias, struct HksParamSet *encryptParamSet,
398     const struct HksBlob *inData, struct HksBlob *cipherText)
399 {
400     int32_t ret = HksAesCipherTestEncryptWithoutNonce(keyAlias, encryptParamSet, inData, cipherText, false);
401     EXPECT_EQ(ret, HKS_SUCCESS) << "HksAesCipherTestEncrypt failed.";
402     return ret;
403 }
404 
HksAesDecryptThreeStage(const struct HksBlob * keyAlias,struct HksParamSet * decryptParamSet,const struct HksBlob * inData,struct HksBlob * cipherText,struct HksBlob * plainText)405 int32_t HksAesDecryptThreeStage(const struct HksBlob *keyAlias, struct HksParamSet *decryptParamSet,
406     const struct HksBlob *inData, struct HksBlob *cipherText, struct HksBlob *plainText)
407 {
408     cipherText->size -= NONCE_SIZE;
409     cipherText->size -= AEAD_SIZE;
410 
411     uint32_t i = 0;
412     for (i = 0; i < decryptParamSet->paramsCnt; i++) {
413         if (decryptParamSet->params[i].tag == HKS_TAG_AE_TAG) {
414             uint8_t *tempPtrTest = cipherText->data;
415             (void)memcpy_s(decryptParamSet->params[i].blob.data, AEAD_SIZE,
416                 tempPtrTest + cipherText->size, AEAD_SIZE);
417             break;
418         }
419     }
420 
421     for (i = 0; i < decryptParamSet->paramsCnt; i++) {
422         if (decryptParamSet->params[i].tag == HKS_TAG_NONCE) {
423             uint8_t *tempPtrTest = cipherText->data;
424             (void)memcpy_s(decryptParamSet->params[i].blob.data, NONCE_SIZE,
425                 tempPtrTest + cipherText->size + AEAD_SIZE, NONCE_SIZE);
426             break;
427         }
428     }
429 
430     /* 3. Decrypt Three Stage */
431     // Init
432     uint8_t handleD[sizeof(uint64_t)] = {0};
433     struct HksBlob handleDecryptTest = { sizeof(uint64_t), handleD };
434     uint8_t challenge[32] = {0};
435     struct HksBlob challengeBlob = { 32, challenge };
436     int32_t ret = HksInitForDe(keyAlias, decryptParamSet, &handleDecryptTest, &challengeBlob);
437     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
438     if (ret != HKS_SUCCESS) {
439         return ret;
440     }
441 
442     // Update & Finish
443     ret = TestUpdateLoopFinish(&handleDecryptTest, decryptParamSet, cipherText, plainText);
444     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
445     if (ret != HKS_SUCCESS) {
446         return ret;
447     }
448     EXPECT_EQ(HksMemCmp(inData->data, plainText->data, inData->size), HKS_SUCCESS) << "plainText not equals inData";
449     return ret;
450 }
451 
HksAesDecryptForBatch(const struct HksBlob * keyAlias,struct HksParamSet * decryptParamSet,const struct HksBlob * inData1,struct HksBlob * cipherText1,struct HksBlob * plainText1,const struct HksBlob * inData2,struct HksBlob * cipherText2,struct HksBlob * plainText2)452 int32_t HksAesDecryptForBatch(const struct HksBlob *keyAlias, struct HksParamSet *decryptParamSet,
453     const struct HksBlob *inData1, struct HksBlob *cipherText1, struct HksBlob *plainText1,
454     const struct HksBlob *inData2, struct HksBlob *cipherText2, struct HksBlob *plainText2)
455 {
456     HksAesGcmAppendAeadAndNonce(decryptParamSet, cipherText1);
457 
458     // Init
459     uint8_t handleD[sizeof(uint64_t)] = {0};
460     struct HksBlob handleDecryptTest = { sizeof(uint64_t), handleD };
461     uint8_t challenge[32] = {0};
462     struct HksBlob challengeBlob = { 32, challenge };
463     int32_t ret = HksInitForDe(keyAlias, decryptParamSet, &handleDecryptTest, &challengeBlob);
464     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
465     if (ret != HKS_SUCCESS) {
466         return ret;
467     }
468 
469     // Update & Finish
470     ret = HksUpdateForDe(&handleDecryptTest, decryptParamSet, cipherText1, plainText1);
471     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
472     if (ret != HKS_SUCCESS) {
473         return ret;
474     }
475 
476     HksAesGcmAppendAeadAndNonce(decryptParamSet, cipherText2);
477 
478     ret = HksUpdateForDe(&handleDecryptTest, decryptParamSet, cipherText2, plainText2);
479     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
480     if (ret != HKS_SUCCESS) {
481         return ret;
482     }
483 
484     uint8_t finishIn[AES_COMMON_SIZE] = {0};
485     struct HksBlob finishInText = { AES_COMMON_SIZE, finishIn };
486     uint8_t finishOut[AES_COMMON_SIZE] = {0};
487     struct HksBlob finishOutText = { AES_COMMON_SIZE, finishOut };
488     ret = HksFinishForDe(&handleDecryptTest, decryptParamSet, &finishInText, &finishOutText);
489     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
490     if (ret != HKS_SUCCESS) {
491         return ret;
492     }
493     EXPECT_EQ(HksMemCmp(inData1->data, plainText1->data, inData1->size), HKS_SUCCESS) << "plainText not equals inData";
494     EXPECT_EQ(HksMemCmp(inData2->data, plainText2->data, inData2->size), HKS_SUCCESS) << "plainText not equals inData";
495 
496     return ret;
497 }
498 
499 #else
500     int32_t HksAesCipherTestCaseOther(const struct HksBlob *keyAlias, struct HksParamSet *genParamSet,
501         struct HksParamSet *encryptParamSet, struct HksParamSet *decryptParamSet)
502 {
503     struct HksBlob inData = {
504         (uint32_t)g_inData.length(),
505         const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(g_inData.c_str()))
506     };
507 
508     /* 1. Generate Key */
509     int32_t ret = HksGenerateKeyForDe(keyAlias, genParamSet, nullptr);
510     EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
511     if (ret != HKS_SUCCESS) {
512         return ret;
513     }
514 
515     /* 2. Encrypt Three Stage */
516     // Init
517     uint8_t handleE[sizeof(uint64_t)] = {0};
518     struct HksBlob handleEncrypt = { sizeof(uint64_t), handleE };
519     ret = HksInitForDe(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
520     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
521 
522     // Update & Finish
523     uint8_t cipher[AES_COMMON_SIZE] = {0};
524     struct HksBlob cipherText = { AES_COMMON_SIZE, cipher };
525     ret = TestUpdateLoopFinish(&handleEncrypt, encryptParamSet, &inData, &cipherText);
526     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
527     EXPECT_NE(memcmp(inData.data, cipherText.data, inData.size), HKS_SUCCESS) << "cipherText equals inData";
528     if (ret != HKS_SUCCESS) {
529         HksDeleteKeyForDe(keyAlias, genParamSet);
530         return ret;
531     }
532 
533     HKS_LOG_I("enter Decrypt Three Stage");
534 
535     /* 3. Decrypt Three Stage */
536     // Init
537     uint8_t handleD[sizeof(uint64_t)] = {0};
538     struct HksBlob handleDecrypt = { sizeof(uint64_t), handleD };
539     ret = HksInitForDe(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
540     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
541 
542     HKS_LOG_I("enter Update & Finish");
543 
544     // Update & Finish
545     uint8_t plain[AES_COMMON_SIZE] = {0};
546     struct HksBlob plainText = { AES_COMMON_SIZE, plain };
547     ret = TestUpdateLoopFinish(&handleDecrypt, decryptParamSet, &cipherText, &plainText);
548 
549     HKS_LOG_I("enter memcmp, inData.size %d, plainText.size %d", inData.size, plainText.size);
550 
551     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
552     EXPECT_EQ(inData.size, plainText.size) << "plainText not equals inData";
553     if (inData.size != plainText.size) {
554         HKS_LOG_E("xxxxxxxxxxx");
555         return -1;
556     }
557     EXPECT_EQ(memcmp(inData.data, plainText.data, inData.size), HKS_SUCCESS) << "plainText not equals inData";
558     if (ret != HKS_SUCCESS) {
559         HksDeleteKeyForDe(keyAlias, genParamSet);
560         return ret;
561     }
562 
563     HKS_LOG_I("enter HksDeleteKey");
564 
565     /* 3. Delete Key */
566     ret = HksDeleteKeyForDe(keyAlias, genParamSet);
567     EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
568     return ret;
569 }
570 
571 int32_t HksAesCipherTestCaseGcm1(const struct HksBlob *keyAlias, struct HksParamSet *genParamSet,
572     struct HksParamSet *encryptParamSet, struct HksParamSet *decryptParamSet)
573 {
574     struct HksBlob inData = {
575         (uint32_t)g_inData.length(),
576         const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(g_inData.c_str()))
577     };
578 
579     /* 1. Generate Key */
580     int32_t ret = HksGenerateKeyForDe(keyAlias, genParamSet, nullptr);
581     EXPECT_EQ(ret, HKS_SUCCESS) << "GenerateKey failed.";
582     if (ret != HKS_SUCCESS) {
583         return ret;
584     }
585 
586     /* 2. Encrypt Three Stage */
587     // Init
588     uint8_t cipher[AES_COMMON_SIZE] = {0};
589     uint8_t handleE[sizeof(uint64_t)] = {0};
590     struct HksBlob handleEncrypt = { sizeof(uint64_t), handleE };
591     ret = HksInitForDe(keyAlias, encryptParamSet, &handleEncrypt, nullptr);
592     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
593 
594     // Update & Finish
595     struct HksBlob cipherText = { AES_COMMON_SIZE, cipher };
596     ret = TestUpdateLoopFinish(&handleEncrypt, encryptParamSet, &inData, &cipherText);
597     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
598     EXPECT_NE(memcmp(inData.data, cipherText.data, inData.size), HKS_SUCCESS) << "cipherText equals inData";
599     if (ret != HKS_SUCCESS) {
600         HksDeleteKeyForDe(keyAlias, genParamSet);
601         return ret;
602     }
603 
604     cipherText.size -= AEAD_SIZE;
605 
606     uint32_t i = 0;
607     for (i = 0; i < decryptParamSet->paramsCnt; i++) {
608         if (decryptParamSet->params[i].tag == HKS_TAG_AE_TAG) {
609             uint8_t *tempPtr = cipherText.data;
610             (void)memcpy_s(decryptParamSet->params[i].blob.data, AEAD_SIZE,
611                 tempPtr + cipherText.size, AEAD_SIZE);
612             break;
613         }
614     }
615 
616     /* 3. Decrypt Three Stage */
617     // Init
618     uint8_t handleD[sizeof(uint64_t)] = {0};
619     struct HksBlob handleDecrypt = { sizeof(uint64_t), handleD };
620     ret = HksInitForDe(keyAlias, decryptParamSet, &handleDecrypt, nullptr);
621     EXPECT_EQ(ret, HKS_SUCCESS) << "Init failed.";
622 
623     // Update & Finish
624     uint8_t plain[AES_COMMON_SIZE] = {0};
625     struct HksBlob plainText = { AES_COMMON_SIZE, plain };
626     ret = TestUpdateLoopFinish(&handleDecrypt, decryptParamSet, &cipherText, &plainText);
627     EXPECT_EQ(ret, HKS_SUCCESS) << "TestUpdateLoopFinish failed.";
628     EXPECT_EQ(memcmp(inData.data, plainText.data, inData.size), HKS_SUCCESS) << "plainText not equals inData";
629     if (ret != HKS_SUCCESS) {
630         HksDeleteKeyForDe(keyAlias, genParamSet);
631         return ret;
632     }
633 
634     /* 3. Delete Key */
635     ret = HksDeleteKeyForDe(keyAlias, genParamSet);
636     EXPECT_EQ(ret, HKS_SUCCESS) << "DeleteKey failed.";
637     return ret;
638 }
639 #endif
640 }
641