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