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_type.h"
17 #define HKS_SLICE_MAX_LEN  (64 * 1024)
18 #define DEFAULT_TEST_CASE_ID 0xFFFFFFFF
19 #define DEFAULT_EXECUTION_TIMES 0xFFFFFFFF
20 #define DEFAULT_PERFORM_TIMES 0xFFFFFFFF
21 #define MAX_ARGS_LEN 20
22 #define DEFAULT_KEY_ALIAS_SIZE 64
23 #define DEFAULT_SLICE_SIZE (2 * 128 + 1)
24 #define DEFAULT_PARAM_SET_OUT_SIZE 4096
25 #define HKS_MAX_RANDOM_LEN 1024
26 #define MAX_OUT_BLOB_SIZE (5 * 1024 * 1024)
27 #define TEST_MAX_FILE_NAME_LEN 512
28 #define DEFAULT_LOCAL_KEY_SIZE 256
29 
30 #define HKS_TEST_GEN_KEY \
31 bool paramSetExist; \
32 bool setAlg; \
33 uint32_t alg; \
34 bool setKeySize; \
35 uint32_t keySize; \
36 bool setPurpose; \
37 uint32_t purpose; \
38 bool setDigest; \
39 uint32_t digest; \
40 bool setPadding; \
41 uint32_t padding; \
42 bool setBlockMode; \
43 uint32_t mode; \
44 bool setKeyStorageFlag; \
45 uint32_t keyStorageFlag
46 
47 struct HksTestBlobParams {
48     bool blobExist;              /* blob null or not */
49     uint32_t blobSize;           /* blob->size value */
50     bool blobDataExist;          /* blob->data null or not */
51     uint32_t blobDataSize;       /* blob->data malloc size */
52 };
53 struct HksTestGenKeyParamsParamSet {
54     HKS_TEST_GEN_KEY;
55 };
56 struct HksTestGenKeyParamsParamSetOut {
57     bool paramSetExist;          /* paramSet null or not */
58     uint32_t paramSetSize;
59 };
60 enum HksTestCipherType {
61     HKS_TEST_CIPHER_TYPE_AES,
62     HKS_TEST_CIPHER_TYPE_RSA,
63 };
64 struct TestRsaCipherParamSet {
65     struct HksParamSet **outParamSet;
66     bool paramSetExist;
67     bool setAlg;
68     uint32_t alg;
69     bool setPurpose;
70     uint32_t purpose;
71     bool setDigest;
72     uint32_t digest;
73     bool setPadding;
74     uint32_t padding;
75 };
76 struct HksTestCipherParamsParamSet {
77     enum HksTestCipherType cipherType;
78     bool paramSetExist;          /* paramSet null or not */
79     bool setAlg;
80     uint32_t alg;
81     bool setPurpose;
82     uint32_t purpose;
83     bool setDigest;
84     uint32_t digest;
85     bool setPadding;
86     uint32_t padding;
87     bool setBlockMode;
88     uint32_t mode;
89     bool setIv;
90     uint32_t ivSize;
91     bool setNonce;
92     uint32_t nonceSize;
93     bool setAad;
94     uint32_t aadSize;
95     bool setIsKeyAlias;
96     bool isKeyAlias;
97 };
98 struct HksTestMacParamSet {
99     bool paramSetExist;          /* paramSet null or not */
100     bool setPurpose;
101     uint32_t purpose;
102     bool setDigest;
103     uint32_t digest;
104 };
105 struct HksTestDeriveParamSet {
106     bool paramSetExist;          /* paramSet null or not */
107     bool setAlg;
108     uint32_t alg;
109     bool setPurpose;
110     uint32_t purpose;
111     bool setDigest;
112     uint32_t digest;
113     bool setIteration;
114     uint32_t iteration;
115     bool setSalt;
116     uint32_t saltSize;
117     bool setInfo;
118     uint32_t infoSize;
119     bool setIsKeyAlias;
120     bool isKeyAlias;
121 };
122 struct HksTestAgreeParamSet {
123     bool paramSetExist;          /* paramSet null or not */
124     bool setAlg;
125     uint32_t alg;
126     bool setKeySize;
127     uint32_t keySize;
128     bool setIsKeyAlias;
129     bool isKeyAlias;
130 };
131 enum HksTestMacType {
132     HKS_TEST_MAC_TYPE_REE,
133     HKS_TEST_MAC_TYPE_TEE,
134 };
135 struct HksTestHashParamsParamSet {
136     bool paramSetExist;          /* paramSet null or not */
137     bool setDigest;
138     uint32_t digest;
139 };
140 struct HksTestParamSetOut {
141     bool paramSetExist;
142     uint32_t paramSetSize;
143     uint32_t realParamSetSize;
144 };
145 struct HksTestGenKeyParams {
146     uint32_t testId;
147     int32_t expectResult;
148     struct HksTestBlobParams keyAliasParams;
149     struct HksTestGenKeyParamsParamSet paramSetParams;
150     struct HksTestGenKeyParamsParamSetOut paramSetParamsOut;
151 };
152 struct HksTestCipherParams {
153     uint32_t testId;
154     int32_t expectResult;
155     struct HksTestBlobParams keyAliasParams;
156     struct HksTestGenKeyParamsParamSet genKeyParamSetParams;
157     struct HksTestGenKeyParamsParamSetOut genKeyParamSetParamsOut;
158     struct HksTestCipherParamsParamSet encryptParamSetParams;
159     struct HksTestCipherParamsParamSet decryptParamSetParams;
160     struct HksTestBlobParams plainTextParams;
161     struct HksTestBlobParams cipherTextParams;
162     struct HksTestBlobParams decryptedTextParams;
163     struct HksTestBlobParams localKeyParams;
164 };
165 struct HksTestEncryptParams {
166     uint32_t testId;
167     int32_t expectResult;
168     struct HksTestBlobParams keyAliasParams;
169     struct HksTestGenKeyParamsParamSet genKeyParamSetParams;
170     struct HksTestBlobParams encryptAliasParams;
171     struct HksTestCipherParamsParamSet encryptParamSetParams;
172     struct HksTestBlobParams inDataParams;
173     struct HksTestBlobParams outDataParams;
174     struct HksTestBlobParams localKeyParams;
175 };
176 struct HksTestDecryptParams {
177     uint32_t testId;
178     int32_t expectResult;
179     struct HksTestBlobParams keyAliasParams;
180     struct HksTestGenKeyParamsParamSet genKeyParamSetParams;
181     struct HksTestBlobParams decryptAliasParams;
182     struct HksTestCipherParamsParamSet decryptParamSetParams;
183     struct HksTestBlobParams inDataParams;
184     struct HksTestBlobParams outDataParams;
185     struct HksTestBlobParams localKeyParams;
186 };
187 struct HksTestMacParams {
188     uint32_t testId;
189     int32_t expectResult;
190     enum HksTestMacType macType;
191     struct HksTestBlobParams keyAliasParams;
192     struct HksTestGenKeyParamsParamSet genKeyParamSetParams;
193     struct HksTestBlobParams keyParams;
194     struct HksTestMacParamSet macParamSetParams;
195     struct HksTestBlobParams srcDataParams;
196     struct HksTestBlobParams macParams;
197 };
198 struct HksTestDeriveParams {
199     uint32_t testId;
200     int32_t expectResult;
201     struct HksTestBlobParams keyAliasParams;
202     struct HksTestGenKeyParamsParamSet genKeyParamSetParams;
203     struct HksTestBlobParams masterKeyParams;
204     struct HksTestDeriveParamSet deriveParamSetParams;
205     struct HksTestBlobParams derivedKeyParams;
206     struct HksTestBlobParams localKeyParams;
207 };
208 struct HksTestKeyExistParams {
209     uint32_t testId;
210     int32_t expectResult;
211     bool isGenKey;
212     struct HksTestBlobParams keyAliasParams;
213 };
214 struct HksTestGetKeyParamSetParams {
215     uint32_t testId;
216     int32_t expectResult;
217     bool isGenKey;
218     struct HksTestBlobParams keyAliasParams;
219     struct HksTestParamSetOut paramSetOutParams;
220 };
221 struct HksTestGenRandomParams {
222     uint32_t testId;
223     int32_t expectResult;
224     struct HksTestBlobParams randomParams;
225 };
226 struct HksTestHashParams {
227     uint32_t testId;
228     int32_t expectResult;
229     struct HksTestHashParamsParamSet paramSetParams;
230     struct HksTestBlobParams srcDataParams;
231     struct HksTestBlobParams hashParams;
232 };
233 struct HksTestBnExpModParams {
234     uint32_t testId;
235     int32_t expectResult;
236     bool isTestValue;
237     struct HksTestBlobParams xParams;
238     struct HksTestBlobParams aParams;
239     struct HksTestBlobParams eParams;
240     struct HksTestBlobParams nParams;
241 };
242 struct HksTestAgreeParams {
243     uint32_t testId;
244     int32_t expectResult;
245     struct HksTestBlobParams keyAlias1Params;
246     struct HksTestBlobParams keyAlias2Params;
247     struct HksTestGenKeyParamsParamSet genKeyParamSetParams;
248     struct HksTestBlobParams privateKeyParams;
249     struct HksTestAgreeParamSet agreeParamSetParams;
250     struct HksTestBlobParams peerPublicParams;
251     struct HksTestBlobParams agreedKeyParams;
252     struct HksTestBlobParams localPrivateKeyParams;
253     struct HksTestBlobParams localPublicKeyParams;
254 };
255 struct TestAgreeParamSetStructure {
256     struct HksParamSet **outParamSet;
257     bool paramSetExist;
258     bool setAlg;
259     uint32_t alg;
260     bool setKeySize;
261     uint32_t keySize;
262     bool setIsKeyAlias;
263     bool isKeyAlias;
264 };
265 struct GenerateKeyParamSetStructure {
266     struct HksParamSet **outParamSet;
267     HKS_TEST_GEN_KEY;
268 };
269 struct AesCipherParamSetStructure {
270     struct HksParamSet **outParamSet;
271     bool paramSetExist;
272     bool setAlg;
273     uint32_t alg;
274     bool setPurpose;
275     uint32_t purpose;
276     bool setPadding;
277     uint32_t padding;
278     bool setBlockMode;
279     uint32_t mode;
280     bool setIv;
281     const struct HksBlob *ivBlob;
282     bool setNonce;
283     const struct HksBlob *nonceBlob;
284     bool setAad;
285     const struct HksBlob *aadBlob;
286     bool setIsKeyAlias;
287     bool isKeyAlias;
288 };
289 struct TestMacParamSetStructure {
290     struct HksParamSet **outParamSet;
291     bool paramSetExist;
292     bool setPurpose;
293     uint32_t purpose;
294     bool setDigest;
295     uint32_t digest;
296     bool setKeyAlias;
297     bool isKeyAlias;
298 };
299 struct TestDeriveParamSetStructure {
300     struct HksParamSet **outParamSet;
301     bool paramSetExist;
302     bool setAlg;
303     uint32_t alg;
304     bool setPurpose;
305     uint32_t purpose;
306     bool setDigest;
307     uint32_t digest;
308     bool setIteration;
309     uint32_t iteration;
310     bool setSalt;
311     const struct HksBlob *saltBlob;
312     bool setInfo;
313     const struct HksBlob *infoBlob;
314     bool setIsKeyAlias;
315     bool isKeyAlias;
316 };
317 
318 #ifdef __cplusplus
319 extern "C" {
320 #endif
321 
322 void TestFreeBlob(struct HksBlob **key);
323 
324 int32_t TestConstuctBlob(struct HksBlob **blob,
325     bool blobExist,
326     uint32_t blobSize,
327     bool blobDataExist,
328     uint32_t realBlobDataSize);
329 
330 int32_t TestConstructGenerateKeyParamSetOut(
331     struct HksParamSet **outParamSet,
332     bool paramSetExist, uint32_t paramSetSize);
333 
334 int32_t TestConstructGenerateKeyParamSet(struct GenerateKeyParamSetStructure *paramStruct);
335 
336 int32_t TestConstructRsaCipherParamSet(struct TestRsaCipherParamSet *paramStruct);
337 
338 int32_t TestConstructAesCipherParamSet(struct AesCipherParamSetStructure *paramStruct);
339 
340 int32_t TestConstructMacParamSet(struct TestMacParamSetStructure *paramStruct);
341 
342 int32_t TestConstructAgreeParamSet(struct TestAgreeParamSetStructure *paramStruct);
343 
344 int32_t GenerateLocalX25519Key(struct HksBlob **privateKey, struct HksBlob **publicKey,
345     const struct HksTestBlobParams *localPrivateKeyParams, const struct HksTestBlobParams *localPublicKeyParams);
346 
347 int32_t TestConstructDeriveParamSet(struct TestDeriveParamSetStructure *paramStruct);
348 
349 int32_t TestConstructHashParamSet(
350     struct HksParamSet **outParamSet,
351     bool paramSetExist,
352     bool setDigest, uint32_t digest);
353 
354 int32_t GenerateKey(struct HksBlob **keyAlias, const struct HksTestBlobParams *keyAliasParams,
355     const struct HksTestGenKeyParamsParamSet *genKeyParamSetParams,
356     const struct HksTestGenKeyParamsParamSetOut *genKeyParamSetParamsOut);
357 
358 int32_t GenerateLocalRandomKey(struct HksBlob **keyAlias, const struct HksTestBlobParams *localKeyParams);
359 
360 int32_t TestConstructBlobOut(struct HksBlob **blob,
361     bool blobExist,
362     uint32_t blobSize,
363     bool blobDataExist,
364     uint32_t realBlobDataSize);
365 
366 int32_t TestGenDefaultKeyAndGetAlias(struct HksBlob **keyAlias);
367 
368 #ifdef __cplusplus
369 }
370 #endif
371 
372