1 /*
2  * Copyright (c) 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_test_adapt_for_de.h"
17 #include "hks_test_cipher_c.h"
18 
ConstructDataToBlobTest(struct HksBlob ** inData,struct HksBlob ** outData,const struct HksTestBlobParams * inTextParams,const struct HksTestBlobParams * outTextParams)19 int32_t ConstructDataToBlobTest(struct HksBlob **inData, struct HksBlob **outData,
20     const struct HksTestBlobParams *inTextParams, const struct HksTestBlobParams *outTextParams)
21 {
22     int32_t ret = TestConstuctBlob(inData,
23         inTextParams->blobExist,
24         inTextParams->blobSize,
25         inTextParams->blobDataExist,
26         inTextParams->blobDataSize);
27     HKS_TEST_ASSERT(ret == 0);
28 
29     ret = TestConstuctBlob(outData,
30         outTextParams->blobExist,
31         outTextParams->blobSize,
32         outTextParams->blobDataExist,
33         outTextParams->blobDataSize);
34     HKS_TEST_ASSERT(ret == 0);
35     return ret;
36 }
37 
EncryptTest(struct CipherEncryptStructure * encryptStructTest)38 int32_t EncryptTest(struct CipherEncryptStructure *encryptStructTest)
39 {
40     int32_t ret;
41     struct HksParamSet *encryptParamSet = NULL;
42 
43     if (encryptStructTest->cipherParms->cipherType == HKS_TEST_CIPHER_TYPE_RSA) {
44         struct TestRsaCipherParamSet rsaCipherParamStructure = {
45             &encryptParamSet,
46             encryptStructTest->cipherParms->paramSetExist,
47             encryptStructTest->cipherParms->setAlg, encryptStructTest->cipherParms->alg,
48             encryptStructTest->cipherParms->setPurpose, encryptStructTest->cipherParms->purpose,
49             encryptStructTest->cipherParms->setDigest, encryptStructTest->cipherParms->digest,
50             encryptStructTest->cipherParms->setPadding, encryptStructTest->cipherParms->padding
51         };
52         ret = TestConstructRsaCipherParamSet(&rsaCipherParamStructure);
53     } else {
54         uint32_t ivSize = encryptStructTest->cipherParms->ivSize;
55         uint32_t nonceSize = encryptStructTest->cipherParms->nonceSize;
56         uint32_t aadSize = encryptStructTest->cipherParms->aadSize;
57         if (ivSize != 0) {
58             ret = TestConstuctBlob(encryptStructTest->ivData, true, ivSize, true, ivSize);
59             HKS_TEST_ASSERT(ret == 0);
60         }
61         if (nonceSize != 0) {
62             ret = TestConstuctBlob(encryptStructTest->nonceData, true, nonceSize, true, nonceSize);
63             HKS_TEST_ASSERT(ret == 0);
64         }
65         if (aadSize != 0) {
66             ret = TestConstuctBlob(encryptStructTest->aadData, true, aadSize, true, aadSize);
67             HKS_TEST_ASSERT(ret == 0);
68         }
69         struct AesCipherParamSetStructure enParamStruct = {
70             &encryptParamSet,
71             encryptStructTest->cipherParms->paramSetExist,
72             encryptStructTest->cipherParms->setAlg, encryptStructTest->cipherParms->alg,
73             encryptStructTest->cipherParms->setPurpose, encryptStructTest->cipherParms->purpose,
74             encryptStructTest->cipherParms->setPadding, encryptStructTest->cipherParms->padding,
75             encryptStructTest->cipherParms->setBlockMode, encryptStructTest->cipherParms->mode,
76             encryptStructTest->cipherParms->setIv, *(encryptStructTest->ivData),
77             encryptStructTest->cipherParms->setNonce, *(encryptStructTest->nonceData),
78             encryptStructTest->cipherParms->setAad, *(encryptStructTest->aadData),
79             encryptStructTest->cipherParms->setIsKeyAlias, encryptStructTest->cipherParms->isKeyAlias
80         };
81         ret = TestConstructAesCipherParamSet(&enParamStruct);
82         HKS_TEST_ASSERT(ret == 0);
83     }
84 
85     ret = HksEncryptRun(encryptStructTest->keyAlias, encryptParamSet, encryptStructTest->plainData,
86         encryptStructTest->cipherData, encryptStructTest->performTimes);
87     HksFreeParamSet(&encryptParamSet);
88     return ret;
89 }
90 
DecryptCipherTest(struct CipherDecryptStructure * decryptStruct)91 int32_t DecryptCipherTest(struct CipherDecryptStructure *decryptStruct)
92 {
93     int32_t ret = TestConstuctBlob(decryptStruct->decryptedData,
94         decryptStruct->cipherParms->decryptedTextParams.blobExist,
95         decryptStruct->cipherParms->decryptedTextParams.blobSize,
96         decryptStruct->cipherParms->decryptedTextParams.blobDataExist,
97         decryptStruct->cipherParms->decryptedTextParams.blobDataSize);
98     HKS_TEST_ASSERT(ret == 0);
99 
100     struct HksParamSet *decryptParamSet = NULL;
101     if (decryptStruct->cipherParms->decryptParamSetParams.cipherType == HKS_TEST_CIPHER_TYPE_RSA) {
102         struct TestRsaCipherParamSet rsaDeCipherParamStructure = {
103             &decryptParamSet,
104             decryptStruct->cipherParms->decryptParamSetParams.paramSetExist,
105             decryptStruct->cipherParms->decryptParamSetParams.setAlg,
106             decryptStruct->cipherParms->decryptParamSetParams.alg,
107             decryptStruct->cipherParms->decryptParamSetParams.setPurpose,
108             decryptStruct->cipherParms->decryptParamSetParams.purpose,
109             decryptStruct->cipherParms->decryptParamSetParams.setDigest,
110             decryptStruct->cipherParms->decryptParamSetParams.digest,
111             decryptStruct->cipherParms->decryptParamSetParams.setPadding,
112             decryptStruct->cipherParms->decryptParamSetParams.padding
113         };
114         ret = TestConstructRsaCipherParamSet(&rsaDeCipherParamStructure);
115     } else {
116         struct AesCipherParamSetStructure deParamStruct = {
117             &decryptParamSet,
118             decryptStruct->cipherParms->decryptParamSetParams.paramSetExist,
119             decryptStruct->cipherParms->decryptParamSetParams.setAlg,
120             decryptStruct->cipherParms->decryptParamSetParams.alg,
121             decryptStruct->cipherParms->decryptParamSetParams.setPurpose,
122             decryptStruct->cipherParms->decryptParamSetParams.purpose,
123             decryptStruct->cipherParms->decryptParamSetParams.setPadding,
124             decryptStruct->cipherParms->decryptParamSetParams.padding,
125             decryptStruct->cipherParms->decryptParamSetParams.setBlockMode,
126             decryptStruct->cipherParms->decryptParamSetParams.mode,
127             decryptStruct->cipherParms->decryptParamSetParams.setIv, decryptStruct->ivData,
128             decryptStruct->cipherParms->decryptParamSetParams.setNonce, decryptStruct->nonceData,
129             decryptStruct->cipherParms->decryptParamSetParams.setAad, decryptStruct->aadData,
130             decryptStruct->cipherParms->decryptParamSetParams.setIsKeyAlias,
131             decryptStruct->cipherParms->decryptParamSetParams.isKeyAlias
132         };
133         ret = TestConstructAesCipherParamSet(&deParamStruct);
134         HKS_TEST_ASSERT(ret == 0);
135     }
136 
137     ret = HksDecryptRun(decryptStruct->keyAlias, decryptParamSet, decryptStruct->cipherData,
138         *(decryptStruct->decryptedData), decryptStruct->performTimes);
139     HksFreeParamSet(&decryptParamSet);
140     return ret;
141 }
142 
Decrypt(struct OnlyDecryptStructure * onlyDecryptStruct)143 int32_t Decrypt(struct OnlyDecryptStructure *onlyDecryptStruct)
144 {
145     struct HksParamSet *decryptParamSet = NULL;
146     int32_t ret;
147     if (onlyDecryptStruct->cipherParms->cipherType == HKS_TEST_CIPHER_TYPE_RSA) {
148         struct TestRsaCipherParamSet rsaCipherParamStructure = {
149             &decryptParamSet,
150             onlyDecryptStruct->cipherParms->paramSetExist,
151             onlyDecryptStruct->cipherParms->setAlg, onlyDecryptStruct->cipherParms->alg,
152             onlyDecryptStruct->cipherParms->setPurpose, onlyDecryptStruct->cipherParms->purpose,
153             onlyDecryptStruct->cipherParms->setDigest, onlyDecryptStruct->cipherParms->digest,
154             onlyDecryptStruct->cipherParms->setPadding, onlyDecryptStruct->cipherParms->padding
155         };
156         ret = TestConstructRsaCipherParamSet(&rsaCipherParamStructure);
157     } else {
158         uint32_t ivSize = onlyDecryptStruct->cipherParms->ivSize;
159         uint32_t nonceSize = onlyDecryptStruct->cipherParms->nonceSize;
160         uint32_t aadSize = onlyDecryptStruct->cipherParms->aadSize;
161         if (ivSize != 0) {
162             ret = TestConstuctBlob(onlyDecryptStruct->ivData, true, ivSize, true, ivSize);
163             HKS_TEST_ASSERT(ret == 0);
164         }
165         if (nonceSize != 0) {
166             ret = TestConstuctBlob(onlyDecryptStruct->nonceData, true, nonceSize, true, nonceSize);
167             HKS_TEST_ASSERT(ret == 0);
168         }
169         if (aadSize != 0) {
170             ret = TestConstuctBlob(onlyDecryptStruct->aadData, true, aadSize, true, aadSize);
171             HKS_TEST_ASSERT(ret == 0);
172         }
173         struct AesCipherParamSetStructure onlyDeParamStruct = {
174             &decryptParamSet,
175             onlyDecryptStruct->cipherParms->paramSetExist,
176             onlyDecryptStruct->cipherParms->setAlg, onlyDecryptStruct->cipherParms->alg,
177             onlyDecryptStruct->cipherParms->setPurpose, onlyDecryptStruct->cipherParms->purpose,
178             onlyDecryptStruct->cipherParms->setPadding, onlyDecryptStruct->cipherParms->padding,
179             onlyDecryptStruct->cipherParms->setBlockMode, onlyDecryptStruct->cipherParms->mode,
180             onlyDecryptStruct->cipherParms->setIv, *(onlyDecryptStruct->ivData),
181             onlyDecryptStruct->cipherParms->setNonce, *(onlyDecryptStruct->nonceData),
182             onlyDecryptStruct->cipherParms->setAad, *(onlyDecryptStruct->aadData),
183             onlyDecryptStruct->cipherParms->setIsKeyAlias, onlyDecryptStruct->cipherParms->isKeyAlias
184         };
185         ret = TestConstructAesCipherParamSet(&onlyDeParamStruct);
186     }
187     HKS_TEST_ASSERT(ret == 0);
188 
189     ret = HksDecryptRun(onlyDecryptStruct->keyAlias, decryptParamSet, onlyDecryptStruct->cipherData,
190         onlyDecryptStruct->decryptedData, onlyDecryptStruct->performTimes);
191     HksFreeParamSet(&decryptParamSet);
192     return ret;
193 }
194 
BaseTestCipher(uint32_t times,uint32_t index,uint32_t performTimes)195 int32_t BaseTestCipher(uint32_t times, uint32_t index, uint32_t performTimes)
196 {
197     /* 1. generate key */
198     struct HksBlob *keyAlias = NULL;
199     int32_t ret;
200     if ((g_testCipherParams[index].genKeyParamSetParams.setKeyStorageFlag) &&
201         g_testCipherParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP) {
202         ret = GenerateLocalRandomKey(&keyAlias, &g_testCipherParams[index].localKeyParams);
203     } else {
204         ret = GenerateKey(&keyAlias, &g_testCipherParams[index].keyAliasParams,
205             &g_testCipherParams[index].genKeyParamSetParams, &g_testCipherParams[index].genKeyParamSetParamsOut);
206     }
207     HKS_TEST_ASSERT(ret == 0);
208     struct HksBlob *plainData = NULL;
209     struct HksBlob *cipherData = NULL;
210     ret = ConstructDataToBlobTest(&plainData, &cipherData,
211         &g_testCipherParams[index].plainTextParams, &g_testCipherParams[index].cipherTextParams);
212     HKS_TEST_ASSERT(ret == 0);
213     struct HksBlob *ivData = NULL;
214     struct HksBlob *nonceData = NULL;
215     struct HksBlob *aadData = NULL;
216     /* 2. encrypt */
217     struct CipherEncryptStructure testEncryptStruct = {
218         keyAlias, &g_testCipherParams[index].encryptParamSetParams,
219         plainData, cipherData, &ivData, &nonceData, &aadData, performTimes
220     };
221     ret = EncryptTest(&testEncryptStruct);
222     HKS_TEST_ASSERT(ret == g_testCipherParams[index].expectResult);
223 
224     /* 3. decrypt */
225     struct HksBlob *decryptedData = NULL;
226     struct CipherDecryptStructure testDecryptStruct = {
227         keyAlias, &g_testCipherParams[index], cipherData,
228         &decryptedData, ivData, nonceData, aadData, performTimes
229     };
230     ret = DecryptCipherTest(&testDecryptStruct);
231     HKS_TEST_ASSERT(ret == g_testCipherParams[index].expectResult);
232 
233     if (decryptedData == NULL || plainData == NULL) {
234         return HKS_FAILURE;
235     }
236 
237     HKS_TEST_ASSERT(plainData->size == decryptedData->size);
238     HKS_TEST_ASSERT(memcmp(plainData->data, decryptedData->data, plainData->size) == 0);
239     if (!((g_testCipherParams[index].genKeyParamSetParams.setKeyStorageFlag) &&
240         g_testCipherParams[index].genKeyParamSetParams.keyStorageFlag == HKS_STORAGE_TEMP)) {
241         HKS_TEST_ASSERT(HksDeleteKeyForDe(keyAlias, NULL) == 0);
242     }
243     TestFreeBlob(&keyAlias);
244     TestFreeBlob(&plainData);
245     TestFreeBlob(&cipherData);
246     TestFreeBlob(&decryptedData);
247     TestFreeBlob(&ivData);
248     TestFreeBlob(&nonceData);
249     TestFreeBlob(&aadData);
250     return (ret != g_testCipherParams[index].expectResult);
251 }
252 
BaseTestEncrypt(uint32_t times,uint32_t index,uint32_t performTimes)253 int32_t BaseTestEncrypt(uint32_t times, uint32_t index, uint32_t performTimes)
254 {
255     /* 1. generate key */
256     struct HksBlob *keyAlias = NULL;
257     int32_t ret;
258     if (g_testEncryptParams[index].encryptParamSetParams.setIsKeyAlias &&
259         !g_testEncryptParams[index].encryptParamSetParams.isKeyAlias) {
260         ret = GenerateLocalRandomKey(&keyAlias, &g_testEncryptParams[index].localKeyParams);
261     } else {
262         if (g_testEncryptParams[index].keyAliasParams.blobExist) {
263             ret = GenerateKey(&keyAlias, &g_testEncryptParams[index].keyAliasParams,
264                 &g_testEncryptParams[index].genKeyParamSetParams, NULL);
265         } else {
266             ret = TestConstuctBlob(&keyAlias,
267                 g_testEncryptParams[index].encryptAliasParams.blobExist,
268                 g_testEncryptParams[index].encryptAliasParams.blobSize,
269                 g_testEncryptParams[index].encryptAliasParams.blobDataExist,
270                 g_testEncryptParams[index].encryptAliasParams.blobDataSize);
271         }
272     }
273     HKS_TEST_ASSERT(ret == 0);
274 
275     struct HksBlob *plainData = NULL;
276     struct HksBlob *cipherData = NULL;
277     ret = ConstructDataToBlobTest(&plainData, &cipherData,
278         &g_testEncryptParams[index].inDataParams, &g_testEncryptParams[index].outDataParams);
279     HKS_TEST_ASSERT(ret == 0);
280 
281     struct HksBlob *ivData = NULL;
282     struct HksBlob *nonceData = NULL;
283     struct HksBlob *aadData = NULL;
284     /* 2. encrypt */
285     struct CipherEncryptStructure encryptStruct = {
286         keyAlias, &g_testEncryptParams[index].encryptParamSetParams, plainData, cipherData, &ivData,
287         &nonceData, &aadData, performTimes
288     };
289     ret = EncryptTest(&encryptStruct);
290     HKS_TEST_ASSERT(ret == g_testEncryptParams[index].expectResult);
291 
292     /* 4. delete key */
293     if (g_testEncryptParams[index].keyAliasParams.blobExist) {
294         HKS_TEST_ASSERT(HksDeleteKeyForDe(keyAlias, NULL) == 0);
295     }
296     TestFreeBlob(&keyAlias);
297     TestFreeBlob(&plainData);
298     TestFreeBlob(&cipherData);
299     TestFreeBlob(&ivData);
300     TestFreeBlob(&nonceData);
301     TestFreeBlob(&aadData);
302     return (ret != g_testEncryptParams[index].expectResult);
303 }
304 
BaseTestDecrypt(uint32_t times,uint32_t index,uint32_t performTimes)305 int32_t BaseTestDecrypt(uint32_t times, uint32_t index, uint32_t performTimes)
306 {
307     /* 1. generate key */
308     struct HksBlob *keyAlias = NULL;
309     int32_t ret;
310     if (g_testDecryptParams[index].decryptParamSetParams.setIsKeyAlias &&
311         !g_testDecryptParams[index].decryptParamSetParams.isKeyAlias) {
312         ret = GenerateLocalRandomKey(&keyAlias, &g_testDecryptParams[index].localKeyParams);
313     } else {
314         if (g_testDecryptParams[index].keyAliasParams.blobExist) {
315             ret = GenerateKey(&keyAlias, &g_testDecryptParams[index].keyAliasParams,
316                 &g_testDecryptParams[index].genKeyParamSetParams, NULL);
317         } else {
318             ret = TestConstuctBlob(&keyAlias,
319                 g_testDecryptParams[index].decryptAliasParams.blobExist,
320                 g_testDecryptParams[index].decryptAliasParams.blobSize,
321                 g_testDecryptParams[index].decryptAliasParams.blobDataExist,
322                 g_testDecryptParams[index].decryptAliasParams.blobDataSize);
323         }
324     }
325     HKS_TEST_ASSERT(ret == 0);
326 
327     struct HksBlob *cipherData = NULL;
328     struct HksBlob *decryptedData = NULL;
329     ret = ConstructDataToBlobTest(&cipherData, &decryptedData,
330         &g_testDecryptParams[index].inDataParams, &g_testDecryptParams[index].outDataParams);
331     HKS_TEST_ASSERT(ret == 0);
332 
333     struct HksBlob *ivData = NULL;
334     struct HksBlob *nonceData = NULL;
335     struct HksBlob *aadData = NULL;
336     /* 3. encrypt */
337     struct OnlyDecryptStructure onlyDecryptStruct = {
338         keyAlias, &g_testDecryptParams[index].decryptParamSetParams, cipherData, decryptedData, &ivData,
339         &nonceData, &aadData, performTimes
340     };
341     ret = Decrypt(&onlyDecryptStruct);
342     HKS_TEST_ASSERT(ret == g_testDecryptParams[index].expectResult);
343 
344     /* 4. delete key */
345     if (g_testDecryptParams[index].keyAliasParams.blobExist) {
346         HKS_TEST_ASSERT(HksDeleteKeyForDe(keyAlias, NULL) == 0);
347     }
348     TestFreeBlob(&keyAlias);
349     TestFreeBlob(&decryptedData);
350     TestFreeBlob(&cipherData);
351     TestFreeBlob(&ivData);
352     TestFreeBlob(&nonceData);
353     TestFreeBlob(&aadData);
354     return (ret != g_testDecryptParams[index].expectResult);
355 }