• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 /* BEGIN_HEADER */
17 
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <pthread.h>
22 #include "securec.h"
23 #include "bsl_err.h"
24 #include "bsl_sal.h"
25 #include "crypt_errno.h"
26 #include "crypt_dsa.h"
27 #include "crypt_eal_pkey.h"
28 #include "crypt_eal_rand.h"
29 #include "crypt_bn.h"
30 #include "crypt_util_rand.h"
31 #include "crypt_eal_md.h"
32 #include "crypt_eal_cipher.h"
33 /* END_HEADER */
34 
35 #define SUCCESS 0
36 #define ERROR (-1)
37 
38 typedef struct {
39     uint8_t *key;
40     uint8_t *iv;
41     uint8_t *aad;
42     uint8_t *pt;
43     uint8_t *ct;
44     uint8_t *tag;
45     uint32_t keyLen;
46     uint32_t ivLen;
47     uint32_t aadLen;
48     uint32_t ptLen;
49     uint32_t ctLen;
50     uint32_t tagLen;
51     int algId;
52 } ThreadParameter;
53 
MultiThreadTest(void * arg)54 void MultiThreadTest(void *arg)
55 {
56     ThreadParameter *threadParameter = (ThreadParameter *)arg;
57     uint32_t outLen = threadParameter->ctLen;
58     uint32_t tagLen = threadParameter->tagLen;
59     uint8_t out[threadParameter->ctLen];
60     uint8_t tag[threadParameter->tagLen];
61     CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(threadParameter->algId);
62     ASSERT_TRUE(ctx != NULL);
63     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, threadParameter->key, threadParameter->keyLen, threadParameter->iv,
64         threadParameter->ivLen, true) == CRYPT_SUCCESS);
65     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_TAGLEN, &tagLen, sizeof(tagLen)) == CRYPT_SUCCESS);
66     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, threadParameter->aad, threadParameter->aadLen) ==
67         CRYPT_SUCCESS);
68     ASSERT_TRUE(CRYPT_EAL_CipherUpdate(ctx, threadParameter->pt, threadParameter->ptLen, (uint8_t *)out, &outLen) ==
69         CRYPT_SUCCESS);
70     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_TAG, (uint8_t *)tag, tagLen) == CRYPT_SUCCESS);
71 
72     ASSERT_COMPARE("Compare Ct", out, threadParameter->ctLen, threadParameter->ct, threadParameter->ctLen);
73     ASSERT_COMPARE("Compare Enc Tag", tag, tagLen, threadParameter->tag, threadParameter->tagLen);
74 
75     CRYPT_EAL_CipherDeinit(ctx);
76 
77     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, threadParameter->key, threadParameter->keyLen, threadParameter->iv,
78         threadParameter->ivLen, false) == CRYPT_SUCCESS);
79     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_TAGLEN, &tagLen, sizeof(tagLen)) == CRYPT_SUCCESS);
80     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, threadParameter->aad, threadParameter->aadLen) ==
81         CRYPT_SUCCESS);
82     ASSERT_TRUE(CRYPT_EAL_CipherUpdate(ctx, threadParameter->ct, threadParameter->ctLen, (uint8_t *)out, &outLen) ==
83         CRYPT_SUCCESS);
84     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_TAG, (uint8_t *)tag, tagLen) == CRYPT_SUCCESS);
85 
86     ASSERT_COMPARE("Compare Pt", out, threadParameter->ptLen, threadParameter->pt, threadParameter->ptLen);
87     ASSERT_COMPARE("Compare Dec Tag", tag, tagLen, threadParameter->tag, threadParameter->tagLen);
88 
89 EXIT:
90     CRYPT_EAL_CipherFreeCtx(ctx);
91 }
92 
93 /* BEGIN_CASE */
SDV_CRYPTO_GCM_API_TC001(int id,int keyLen,int ivLen)94 void SDV_CRYPTO_GCM_API_TC001(int id, int keyLen, int ivLen)
95 {
96     uint8_t key[32] = { 0 }; // The maximum length of the key is 32 bytes.
97     uint8_t iv[256] = { 0 }; // The maximum length of the iv is 256 bytes.
98     TestMemInit();
99     CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
100     ASSERT_TRUE(ctx != NULL);
101     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, (uint8_t *)key, keyLen, (uint8_t *)iv, ivLen, true) == CRYPT_SUCCESS);
102     ASSERT_TRUE(CRYPT_EAL_CipherReinit(ctx, (uint8_t *)iv, 0) != CRYPT_SUCCESS);
103     // Repeat the settings.
104     ASSERT_TRUE(CRYPT_EAL_CipherReinit(ctx, (uint8_t *)iv, ivLen) == CRYPT_SUCCESS);
105 EXIT:
106     CRYPT_EAL_CipherFreeCtx(ctx);
107 }
108 /* END_CASE */
109 
110 /* BEGIN_CASE */
SDV_CRYPTO_GCM_API_TC002(int id,int keyLen)111 void SDV_CRYPTO_GCM_API_TC002(int id, int keyLen)
112 {
113     CRYPT_EAL_CipherCtx *ctx = NULL;
114     uint8_t key[32] = { 0 }; // The maximum length of the key is 32 bytes.
115     uint8_t iv[256] = { 0 }; // The maximum length of the iv is 256 bytes.
116     TestMemInit();
117     ASSERT_TRUE(CRYPT_EAL_CipherNewCtx(99) == NULL); // 99 Indicates an invalid algorithm ID.
118     ctx = CRYPT_EAL_CipherNewCtx(id);
119     ASSERT_TRUE(ctx != NULL);
120 
121     // 256 indicates the IV length.
122     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, NULL, keyLen, (uint8_t *)iv, 256, true) != CRYPT_SUCCESS);
123     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, (uint8_t *)key, 0, (uint8_t *)iv, 256, true) !=
124         CRYPT_SUCCESS);
125     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, (uint8_t *)key, keyLen, NULL, 256, true) !=
126         CRYPT_SUCCESS);
127     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, (uint8_t *)key, keyLen, (uint8_t *)iv, 0, true) != CRYPT_SUCCESS);
128     ASSERT_TRUE(CRYPT_EAL_CipherReinit(NULL, (uint8_t *)iv, 256) != CRYPT_SUCCESS);
129     ASSERT_TRUE(CRYPT_EAL_CipherReinit(ctx, NULL, 256) != CRYPT_SUCCESS);
130     ASSERT_TRUE(CRYPT_EAL_CipherReinit(NULL, (uint8_t *)iv, 0) != CRYPT_SUCCESS);
131 EXIT:
132     CRYPT_EAL_CipherFreeCtx(ctx);
133 }
134 /* END_CASE */
135 
136 /* BEGIN_CASE */
SDV_CRYPTO_GCM_API_TC003(int id,int keyLen)137 void SDV_CRYPTO_GCM_API_TC003(int id, int keyLen)
138 {
139     TestMemInit();
140     uint8_t key[32] = { 0 }; // The maximum length of the key is 32 bytes.
141     uint8_t iv[256] = { 0 }; // The maximum length of the iv is 256 bytes.
142     uint8_t data[256];
143     CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
144     ASSERT_TRUE(ctx != NULL);
145     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, (uint8_t *)key, keyLen, iv, sizeof(iv), true) == CRYPT_SUCCESS);
146     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_MSGLEN, data, sizeof(data)) != CRYPT_SUCCESS);
147 EXIT:
148     CRYPT_EAL_CipherFreeCtx(ctx);
149 }
150 /* END_CASE */
151 
152 /* BEGIN_CASE */
SDV_CRYPTO_GCM_API_TC004(int id,int keyLen)153 void SDV_CRYPTO_GCM_API_TC004(int id, int keyLen)
154 {
155     TestMemInit();
156     uint8_t key[32] = { 0 };   // The maximum length of the key is 32 bytes.
157     uint8_t iv[13] = { 0 };
158     uint8_t data[256] = { 0 };
159     CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
160     ASSERT_TRUE(ctx != NULL);
161     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, (uint8_t *)key, keyLen, iv, sizeof(iv), true) == CRYPT_SUCCESS);
162     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, NULL, 0) == CRYPT_SUCCESS);
163     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_TAG, data, sizeof(data)) != CRYPT_SUCCESS);
164 EXIT:
165     CRYPT_EAL_CipherFreeCtx(ctx);
166 }
167 /* END_CASE */
168 
169 /* BEGIN_CASE */
SDV_CRYPTO_GCM_API_TC005(int id,int keyLen)170 void SDV_CRYPTO_GCM_API_TC005(int id, int keyLen)
171 {
172     TestMemInit();
173     uint8_t key[32] = { 0 }; // The maximum length of the key is 32 bytes.
174     uint8_t iv[13] = { 0 };
175     CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
176     ASSERT_TRUE(ctx != NULL);
177     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, (uint8_t *)key, keyLen, iv, sizeof(iv), true) == CRYPT_SUCCESS);
178     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, NULL, 0) == CRYPT_SUCCESS);
179     ASSERT_EQ(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, NULL, 0), CRYPT_EAL_ERR_STATE);
180 EXIT:
181     CRYPT_EAL_CipherFreeCtx(ctx);
182 }
183 /* END_CASE */
184 
185 /* BEGIN_CASE */
SDV_CRYPTO_GCM_API_TC006(int id,int keyLen)186 void SDV_CRYPTO_GCM_API_TC006(int id, int keyLen)
187 {
188     TestMemInit();
189     uint8_t key[32] = { 0 };   // The maximum length of the key is 32 bytes.
190     uint8_t iv[13] = { 0 };
191     uint8_t data[256] = { 0 };
192     uint8_t out[256] = { 0 };
193     uint32_t outLen = sizeof(out);
194     CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_CipherNewCtx(id);
195     ASSERT_TRUE(ctx != NULL);
196     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, (uint8_t *)key, keyLen, iv, sizeof(iv), true) == CRYPT_SUCCESS);
197     ASSERT_TRUE(CRYPT_EAL_CipherUpdate(ctx, data, sizeof(data), out, &outLen) == CRYPT_SUCCESS);
198 EXIT:
199     CRYPT_EAL_CipherFreeCtx(ctx);
200 }
201 /* END_CASE */
202 
203 /**
204  * @test  SDV_CRYPTO_GCM_FUNC_TC001
205  * @title  Test on the same address in plaintext and ciphertext
206  * @precon Registering memory-related functions.
207  * @brief
208  *    1.Create the context ctx. Expected result 1 is obtained.
209  *    2.Call the Init interface. Expected result 2 is obtained.
210  *    3.Call the Ctrl interface, ctx is not NULL, and set tag len. Expected result 3 is obtained.
211  *    4.Call the Ctrl interface, ctx is not NULL, and set aad. Expected result 4 is obtained.
212  *    5.Call the Update interface, ctx is not NULL, and encrypt data. Expected result 5 is obtained.
213  *    6.Compare the ciphertext. Expected result 6 is obtained.
214  *    7.Compare the tag. Expected result 7 is obtained.
215  *    8.Call the Deinit interface and Call the Init interface. Expected result 8 is obtained.
216  *    9.Call the Ctrl interface set tag len. Expected result 9 is obtained.
217  *    10.Call the Ctrl interface set aad. Expected result 10 is obtained.
218  *    11.Call the Update interface to decrypt data. Expected result 11 is obtained.
219  *    12.Compare the plaintext. Expected result 12 is obtained.
220  *    13.Compare the tag. Expected result 13 is obtained.
221  * @expect
222  *    1.The creation is successful and the ctx is not empty.
223  *    2.Success. Return CRYPT_SUCCESS.
224  *    3.Success. Return CRYPT_SUCCESS.
225  *    4.Success. Return CRYPT_SUCCESS.
226  *    5.Success. Return CRYPT_SUCCESS.
227  *    6.Consistent with expected vector.
228  *    7.Consistent with expected vector.
229  *    8.Success, Return CRYPT_SUCCESS
230  *    9.Success, Return CRYPT_SUCCESS
231  *    10.Success, Return CRYPT_SUCCESS
232  *    11.Success, Return CRYPT_SUCCESS
233  *    12.Consistent with expected vector.
234  *    13.Consistent with expected vector.
235  */
236 /* BEGIN_CASE */
SDV_CRYPTO_GCM_FUNC_TC001(int algId,Hex * key,Hex * iv,Hex * aad,Hex * pt,Hex * ct,Hex * tag)237 void SDV_CRYPTO_GCM_FUNC_TC001(int algId, Hex *key, Hex *iv, Hex *aad, Hex *pt, Hex *ct, Hex *tag)
238 {
239     TestMemInit();
240     CRYPT_EAL_CipherCtx *ctx = NULL;
241     uint8_t *outTag = NULL;
242     uint8_t *out = NULL;
243     uint32_t tagLen = tag->len;
244     uint32_t outLen = ct->len;
245 
246     out = (uint8_t *)malloc(outLen * sizeof(uint8_t));
247     ASSERT_TRUE(out != NULL);
248     outTag = (uint8_t *)malloc(sizeof(uint8_t) * tagLen);
249     ASSERT_TRUE(outTag != NULL);
250     ASSERT_TRUE(memcpy_s(out, outLen, pt->x, pt->len) == EOK);
251     ctx = CRYPT_EAL_CipherNewCtx(algId);
252     ASSERT_TRUE(ctx != NULL);
253     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, true) == CRYPT_SUCCESS);
254     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_TAGLEN, &tagLen, sizeof(tagLen)) == CRYPT_SUCCESS);
255     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, aad->x, aad->len) == CRYPT_SUCCESS);
256     ASSERT_TRUE(CRYPT_EAL_CipherUpdate(ctx, out, pt->len, (uint8_t *)out, &outLen) == CRYPT_SUCCESS);
257     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_TAG, (uint8_t *)outTag, tagLen) == CRYPT_SUCCESS);
258 
259     ASSERT_COMPARE("Compare Ct", out, ct->len, ct->x, ct->len);
260     ASSERT_COMPARE("Compare Enc Tag", outTag, tagLen, tag->x, tag->len);
261 
262     CRYPT_EAL_CipherDeinit(ctx);
263     ASSERT_TRUE(memcpy_s(out, outLen, ct->x, ct->len) == EOK);
264     ASSERT_TRUE(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, false) == CRYPT_SUCCESS);
265     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_TAGLEN, &tagLen, sizeof(tagLen)) == CRYPT_SUCCESS);
266     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, aad->x, aad->len) == CRYPT_SUCCESS);
267     ASSERT_TRUE(CRYPT_EAL_CipherUpdate(ctx, out, ct->len, (uint8_t *)out, &outLen) == CRYPT_SUCCESS);
268     ASSERT_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_TAG, (uint8_t *)outTag, tagLen) == CRYPT_SUCCESS);
269 
270     ASSERT_COMPARE("Compare Pt", out, pt->len, pt->x, pt->len);
271     ASSERT_COMPARE("Compare Dec Tag", outTag, tagLen, tag->x, tag->len);
272 
273 EXIT:
274     CRYPT_EAL_CipherFreeCtx(ctx);
275     free(out);
276     free(outTag);
277 }
278 /* END_CASE */
279 
280 /**
281  * @test  SDV_CRYPTO_GCM_FUNC_TC002
282  * @title  Multi-thread Test
283  * @precon Registering memory-related functions.
284  * @brief
285  *    1.Start three threads. Expected result 1 is obtained.
286  *    2.Call the eal interface in the thread for encryption. Expected result 2 is obtained.
287  *    3.Call the eal interface in the thread for decryption. Expected result 2 is obtained.
288  * @expect
289  *    1.Success.
290  *    2.The encryption is successful. The ciphertext and tag are the same as the vector.
291  *    3.The decryption is successful. The plaintext and tag are consistent with the vector.
292  */
293 /* BEGIN_CASE */
SDV_CRYPTO_GCM_FUNC_TC002(int algId,Hex * key,Hex * iv,Hex * aad,Hex * pt,Hex * ct,Hex * tag)294 void SDV_CRYPTO_GCM_FUNC_TC002(int algId, Hex *key, Hex *iv, Hex *aad, Hex *pt, Hex *ct, Hex *tag)
295 {
296     int ret;
297     TestMemInit();
298     const uint32_t threadNum = 3; // Number of threads.
299     pthread_t thrd[threadNum];
300     ThreadParameter arg[3] = {
301         // 3 threads.
302         {.key = key->x, .iv = iv->x, .aad = aad->x, .pt = pt->x, .ct = ct->x, .tag = tag->x,
303          .keyLen = key->len, .ivLen = iv->len, .aadLen = aad->len,
304          .ptLen = pt->len, .ctLen = ct->len, .tagLen = tag->len,
305          .algId = algId},
306         {.key = key->x, .iv = iv->x, .aad = aad->x, .pt = pt->x, .ct = ct->x, .tag = tag->x,
307          .keyLen = key->len, .ivLen = iv->len, .aadLen = aad->len,
308          .ptLen = pt->len, .ctLen = ct->len, .tagLen = tag->len,
309          .algId = algId},
310         {.key = key->x, .iv = iv->x, .aad = aad->x, .pt = pt->x, .ct = ct->x, .tag = tag->x,
311          .keyLen = key->len, .ivLen = iv->len, .aadLen = aad->len,
312          .ptLen = pt->len, .ctLen = ct->len, .tagLen = tag->len,
313          .algId = algId},
314     };
315     for (uint32_t i = 0; i < threadNum; i++) {
316         ret = pthread_create(&thrd[i], NULL, (void *)MultiThreadTest, &arg[i]);
317         ASSERT_TRUE(ret == 0);
318     }
319     for (uint32_t i = 0; i < threadNum; i++) {
320         pthread_join(thrd[i], NULL);
321     }
322 
323 EXIT:
324     return;
325 }
326 /* END_CASE */