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 #include "bsl_sal.h"
18 #include "crypt_errno.h"
19 #include "crypt_eal_pkey.h"
20 #include "crypt_util_rand.h"
21 #include "eal_pkey_local.h"
22 #include "securec.h"
23 /* END_HEADER */
24
25 /* @
26 * @test SDV_CRYPTO_HYBRID_API_TC001
27 * @spec -
28 * @title Check the value returned by the ctrl interface meets the expectation.
29 * @precon nan
30 * @brief
31 * 1.Create the context of the algorithm.
32 * 2.Call CRYPT_EAL_PkeyCtrl to set and get parameters in the context.
33 * @expect 1.success 2.success
34 * @prior nan
35 * @auto FALSE
36 @ */
37 /* BEGIN_CASE */
SDV_CRYPTO_HYBRID_API_TC001(int algid,int type,int ekLen,int ctLen,int skLen)38 void SDV_CRYPTO_HYBRID_API_TC001(int algid, int type, int ekLen, int ctLen, int skLen)
39 {
40 TestMemInit();
41 CRYPT_EAL_PkeyCtx *ctxA = CRYPT_EAL_PkeyNewCtx((int32_t)algid);
42 ASSERT_TRUE(ctxA != NULL);
43
44 int32_t val = CRYPT_PKEY_PARAID_MAX;
45 ASSERT_EQ(CRYPT_EAL_PkeySetParaById(ctxA, val), CRYPT_ERR_ALGID);
46 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, 0, &val, sizeof(val)), CRYPT_NOT_SUPPORT);
47
48 val = (int32_t)type;
49 ASSERT_EQ(CRYPT_EAL_PkeySetParaById(ctxA, val), CRYPT_SUCCESS);
50
51 uint32_t encapsKeyLen = 0;
52 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_PUBKEY_LEN, &encapsKeyLen, sizeof(encapsKeyLen)),
53 CRYPT_SUCCESS);
54 ASSERT_EQ(encapsKeyLen, ekLen);
55
56 uint32_t cipherLen = 0;
57 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_CIPHERTEXT_LEN, &cipherLen, sizeof(cipherLen)),
58 CRYPT_SUCCESS);
59 ASSERT_EQ(cipherLen, ctLen);
60
61 uint32_t sharedLen = 0;
62 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_SHARED_KEY_LEN, &sharedLen, sizeof(sharedLen)), CRYPT_SUCCESS);
63 ASSERT_EQ(sharedLen, skLen);
64
65 if (type != CRYPT_HYBRID_X25519_MLKEM512 && type != CRYPT_HYBRID_X25519_MLKEM768 && type !=
66 CRYPT_HYBRID_X25519_MLKEM1024) {
67 val = CRYPT_POINT_COMPRESSED;
68 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_SET_ECC_POINT_FORMAT, &val, sizeof(val)), CRYPT_SUCCESS);
69 }
70 EXIT:
71 CRYPT_EAL_PkeyFreeCtx(ctxA);
72 return;
73 }
74 /* END_CASE */
75
76 /* @
77 * @test SDV_CRYPTO_HYBRID_ENCAPS_DECAPS_FUNC_TC001
78 * @spec -
79 * @title Generating key pairs and key exchange tests
80 * @precon nan
81 * @brief
82 * 1.Registers the callback function of the memory and random.
83 * 2.Create a context for key exchange and set parameters.
84 * 3.Generating a key pair.
85 * 4.Perform key exchange.
86 * 5.Check whether the shared keys are the same.
87 * @expect 1.success 2.success 3.success 4.success 5.The shared key is the same.
88 * @prior nan
89 * @auto FALSE
90 @ */
91 /* BEGIN_CASE */
SDV_CRYPTO_HYBRID_ENCAPS_DECAPS_FUNC_TC001(int algid,int type,int isProvider)92 void SDV_CRYPTO_HYBRID_ENCAPS_DECAPS_FUNC_TC001(int algid, int type, int isProvider)
93 {
94 TestMemInit();
95 CRYPT_RandRegist(TestSimpleRand);
96 CRYPT_RandRegistEx(TestSimpleRandEx);
97 CRYPT_EAL_PkeyCtx *ctxA = NULL;
98 CRYPT_EAL_PkeyCtx *ctxB = NULL;
99 #ifdef HITLS_CRYPTO_PROVIDER
100 if (isProvider == 1) {
101 ctxA = CRYPT_EAL_ProviderPkeyNewCtx(NULL, algid, CRYPT_EAL_PKEY_KEM_OPERATE, "provider=default");
102 ASSERT_TRUE(ctxA != NULL);
103 ctxB = CRYPT_EAL_ProviderPkeyNewCtx(NULL, algid, CRYPT_EAL_PKEY_KEM_OPERATE, "provider=default");
104 ASSERT_TRUE(ctxB != NULL);
105 } else
106 #endif
107 {
108 (void) isProvider;
109 ctxA = CRYPT_EAL_PkeyNewCtx(algid);
110 ASSERT_TRUE(ctxA != NULL);
111 ctxB = CRYPT_EAL_PkeyNewCtx(algid);
112 ASSERT_TRUE(ctxB != NULL);
113 }
114
115 uint32_t val = (uint32_t)type;
116 ASSERT_EQ(CRYPT_EAL_PkeySetParaById(ctxA, val), CRYPT_SUCCESS);
117 ASSERT_EQ(CRYPT_EAL_PkeySetParaById(ctxB, val), CRYPT_SUCCESS);
118
119 uint32_t encapsKeyLen = 0;
120 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_PUBKEY_LEN, &encapsKeyLen, sizeof(encapsKeyLen)),
121 CRYPT_SUCCESS);
122 uint32_t cipherLen = 0;
123 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_CIPHERTEXT_LEN, &cipherLen, sizeof(cipherLen)),
124 CRYPT_SUCCESS);
125 uint8_t *ciphertext = BSL_SAL_Malloc(cipherLen);
126
127 CRYPT_EAL_PkeyPub ek = { 0 };
128 ek.id = algid;
129 ek.key.kemEk.len = encapsKeyLen;
130 ek.key.kemEk.data = BSL_SAL_Malloc(encapsKeyLen);
131 ASSERT_TRUE(ek.key.kemEk.data != NULL);
132
133 uint32_t sharedLenA = 0;
134 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_SHARED_KEY_LEN, &sharedLenA, sizeof(sharedLenA)), CRYPT_SUCCESS);
135 uint8_t *sharedKeyA = BSL_SAL_Malloc(sharedLenA);
136 ASSERT_TRUE(sharedKeyA != NULL);
137 uint32_t sharedLenB = sharedLenA;
138 uint8_t *sharedKeyB = BSL_SAL_Malloc(sharedLenB);
139 ASSERT_TRUE(sharedKeyB != NULL);
140
141 ASSERT_EQ(CRYPT_EAL_PkeyGen(ctxA), CRYPT_SUCCESS);
142 ASSERT_EQ(CRYPT_EAL_PkeyGetPub(ctxA, &ek), CRYPT_SUCCESS);
143
144 ASSERT_EQ(CRYPT_EAL_PkeySetPub(ctxB, &ek), CRYPT_SUCCESS);
145 ASSERT_EQ(CRYPT_EAL_PkeyEncaps(ctxB, ciphertext, &cipherLen, sharedKeyA, &sharedLenA), CRYPT_SUCCESS);
146
147 ASSERT_EQ(CRYPT_EAL_PkeyDecaps(ctxA, ciphertext, cipherLen, sharedKeyB, &sharedLenB), CRYPT_SUCCESS);
148 ASSERT_COMPARE("compare sharedKey", sharedKeyB, sharedLenB, sharedKeyA, sharedLenA);
149 EXIT:
150 BSL_SAL_Free(ek.key.kemEk.data);
151 BSL_SAL_Free(ciphertext);
152 BSL_SAL_Free(sharedKeyA);
153 BSL_SAL_Free(sharedKeyB);
154 CRYPT_EAL_PkeyFreeCtx(ctxA);
155 CRYPT_EAL_PkeyFreeCtx(ctxB);
156 CRYPT_RandRegist(NULL);
157 CRYPT_RandRegistEx(NULL);
158 return;
159 }
160 /* END_CASE */
161
162 /* Use default random numbers for end-to-end testing */
163 /* BEGIN_CASE */
SDV_CRYPTO_HYBRID_ENCAPS_DECAPS_API_TC002(int algid,int type,int isProvider)164 void SDV_CRYPTO_HYBRID_ENCAPS_DECAPS_API_TC002(int algid, int type, int isProvider)
165 {
166 TestMemInit();
167 TestRandInit();
168 CRYPT_EAL_PkeyCtx *ctxA = NULL;
169 CRYPT_EAL_PkeyCtx *ctxB = NULL;
170 #ifdef HITLS_CRYPTO_PROVIDER
171 if (isProvider == 1) {
172 ctxA = CRYPT_EAL_ProviderPkeyNewCtx(NULL, algid, CRYPT_EAL_PKEY_KEM_OPERATE, "provider=default");
173 ASSERT_TRUE(ctxA != NULL);
174 ctxB = CRYPT_EAL_ProviderPkeyNewCtx(NULL, algid, CRYPT_EAL_PKEY_KEM_OPERATE, "provider=default");
175 ASSERT_TRUE(ctxB != NULL);
176 } else
177 #endif
178 {
179 (void) isProvider;
180 ctxA = CRYPT_EAL_PkeyNewCtx(algid);
181 ASSERT_TRUE(ctxA != NULL);
182 ctxB = CRYPT_EAL_PkeyNewCtx(algid);
183 ASSERT_TRUE(ctxB != NULL);
184 }
185
186 uint32_t val = (uint32_t)type;
187 ASSERT_EQ(CRYPT_EAL_PkeySetParaById(ctxA, val), CRYPT_SUCCESS);
188 ASSERT_EQ(CRYPT_EAL_PkeySetParaById(ctxB, val), CRYPT_SUCCESS);
189
190 uint32_t encapsKeyLen = 0;
191 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_PUBKEY_LEN, &encapsKeyLen, sizeof(encapsKeyLen)),
192 CRYPT_SUCCESS);
193 uint32_t cipherLen = 0;
194 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_CIPHERTEXT_LEN, &cipherLen, sizeof(cipherLen)),
195 CRYPT_SUCCESS);
196 uint8_t *ciphertext = BSL_SAL_Malloc(cipherLen);
197
198 CRYPT_EAL_PkeyPub ek = { 0 };
199 ek.id = algid;
200 ek.key.kemEk.len = encapsKeyLen;
201 ek.key.kemEk.data = BSL_SAL_Malloc(encapsKeyLen);
202 ASSERT_TRUE(ek.key.kemEk.data != NULL);
203
204 uint32_t sharedLenA = 0;
205 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_SHARED_KEY_LEN, &sharedLenA, sizeof(sharedLenA)), CRYPT_SUCCESS);
206 uint8_t *sharedKeyA = BSL_SAL_Malloc(sharedLenA);
207 ASSERT_TRUE(sharedKeyA != NULL);
208 uint32_t sharedLenB = sharedLenA;
209 uint8_t *sharedKeyB = BSL_SAL_Malloc(sharedLenB);
210 ASSERT_TRUE(sharedKeyB != NULL);
211
212 ASSERT_EQ(CRYPT_EAL_PkeyGen(ctxA), CRYPT_SUCCESS);
213 ASSERT_EQ(CRYPT_EAL_PkeyGetPub(ctxA, &ek), CRYPT_SUCCESS);
214
215 ASSERT_EQ(CRYPT_EAL_PkeySetPub(ctxB, &ek), CRYPT_SUCCESS);
216 ASSERT_EQ(CRYPT_EAL_PkeyEncaps(ctxB, ciphertext, &cipherLen, sharedKeyA, &sharedLenA), CRYPT_SUCCESS);
217
218 ASSERT_EQ(CRYPT_EAL_PkeyDecaps(ctxA, ciphertext, cipherLen, sharedKeyB, &sharedLenB), CRYPT_SUCCESS);
219 EXIT:
220 BSL_SAL_Free(ek.key.kemEk.data);
221 BSL_SAL_Free(ciphertext);
222 BSL_SAL_Free(sharedKeyA);
223 BSL_SAL_Free(sharedKeyB);
224 CRYPT_EAL_PkeyFreeCtx(ctxA);
225 CRYPT_EAL_PkeyFreeCtx(ctxB);
226 TestRandDeInit();
227 return;
228 }
229 /* END_CASE */
230
231 /* @
232 * @test SDV_CRYPTO_HYBRID_ENCAPS_DECAPS_FUNC_TC002
233 * @spec -
234 * @title Setting the key pair and key exchange test
235 * @precon nan
236 * @brief
237 * 1.Registers the callback function of the memory and random.
238 * 2.Create a context for key exchange and set parameters.
239 * 3.Setting a key pair.
240 * 4.Perform key exchange.
241 * 5.Check whether the shared keys are the same.
242 * @expect 1.success 2.success 3.success 4.success 5.The shared key is the same.
243 * @prior nan
244 * @auto FALSE
245 @ */
246 /* BEGIN_CASE */
SDV_CRYPTO_HYBRID_ENCAPS_DECAPS_FUNC_TC002(int algid,int type,int isProvider,Hex * encapsKeyA,Hex * decapsKeyA)247 void SDV_CRYPTO_HYBRID_ENCAPS_DECAPS_FUNC_TC002(int algid, int type, int isProvider, Hex *encapsKeyA, Hex *decapsKeyA)
248 {
249 TestMemInit();
250 CRYPT_RandRegist(TestSimpleRand);
251 CRYPT_RandRegistEx(TestSimpleRandEx);
252 CRYPT_EAL_PkeyCtx *ctxA = NULL;
253 CRYPT_EAL_PkeyCtx *ctxB = NULL;
254 #ifdef HITLS_CRYPTO_PROVIDER
255 if (isProvider == 1) {
256 ctxA = CRYPT_EAL_ProviderPkeyNewCtx(NULL, algid, CRYPT_EAL_PKEY_KEM_OPERATE, "provider=default");
257 ASSERT_TRUE(ctxA != NULL);
258 ctxB = CRYPT_EAL_ProviderPkeyNewCtx(NULL, algid, CRYPT_EAL_PKEY_KEM_OPERATE, "provider=default");
259 ASSERT_TRUE(ctxB != NULL);
260 } else
261 #endif
262 {
263 (void)isProvider;
264 ctxA = CRYPT_EAL_PkeyNewCtx(algid);
265 ASSERT_TRUE(ctxA != NULL);
266 ctxB = CRYPT_EAL_PkeyNewCtx(algid);
267 ASSERT_TRUE(ctxB != NULL);
268 }
269 uint32_t val = (uint32_t)type;
270 ASSERT_EQ(CRYPT_EAL_PkeySetParaById(ctxA, val), CRYPT_SUCCESS);
271 ASSERT_EQ(CRYPT_EAL_PkeySetParaById(ctxB, val), CRYPT_SUCCESS);
272 uint32_t cipherLen = 0;
273 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_CIPHERTEXT_LEN, &cipherLen, sizeof(cipherLen)),
274 CRYPT_SUCCESS);
275 uint8_t *ciphertext = BSL_SAL_Malloc(cipherLen);
276
277 uint32_t sharedLenA = 0;
278 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_SHARED_KEY_LEN, &sharedLenA, sizeof(sharedLenA)), CRYPT_SUCCESS);
279 uint8_t *sharedKeyA = BSL_SAL_Malloc(sharedLenA);
280 ASSERT_TRUE(sharedKeyA != NULL);
281 uint32_t sharedLenB = sharedLenA;
282 uint8_t *sharedKeyB = BSL_SAL_Malloc(sharedLenB);
283 ASSERT_TRUE(sharedKeyB != NULL);
284
285 CRYPT_EAL_PkeyPub ek = { 0 };
286 ek.id = algid;
287 ek.key.kemEk.len = encapsKeyA->len;
288 ek.key.kemEk.data = encapsKeyA->x;
289 ASSERT_EQ(CRYPT_EAL_PkeySetPub(ctxA, &ek), CRYPT_SUCCESS);
290 CRYPT_EAL_PkeyPrv dk = { 0 };
291 dk.id = algid;
292 dk.key.kemDk.len = decapsKeyA->len;
293 dk.key.kemDk.data = decapsKeyA->x;
294 ASSERT_EQ(CRYPT_EAL_PkeySetPrv(ctxA, &dk), CRYPT_SUCCESS);
295
296 ek.key.kemEk.len = encapsKeyA->len;
297 ek.key.kemEk.data = encapsKeyA->x;
298 ASSERT_EQ(CRYPT_EAL_PkeySetPub(ctxB, &ek), CRYPT_SUCCESS);
299 uint32_t decapsLen = 0;
300 ASSERT_EQ(CRYPT_EAL_PkeyCtrl(ctxA, CRYPT_CTRL_GET_PRVKEY_LEN, &decapsLen, sizeof(decapsLen)),
301 CRYPT_SUCCESS);
302 ASSERT_EQ(decapsLen, dk.key.kemDk.len);
303
304 ASSERT_EQ(CRYPT_EAL_PkeyGetPrv(ctxA, &dk), CRYPT_SUCCESS);
305 ASSERT_COMPARE("compare private key", dk.key.kemDk.data, dk.key.kemDk.len, decapsKeyA->x, decapsKeyA->len);
306
307 ASSERT_EQ(CRYPT_EAL_PkeyEncaps(ctxB, ciphertext, &cipherLen, sharedKeyB, &sharedLenB), CRYPT_SUCCESS);
308 ASSERT_EQ(CRYPT_EAL_PkeyDecaps(ctxA, ciphertext, cipherLen, sharedKeyA, &sharedLenA), CRYPT_SUCCESS);
309 ASSERT_COMPARE("compare sharedKey", sharedKeyB, sharedLenB, sharedKeyA, sharedLenA);
310 EXIT:
311 BSL_SAL_Free(ciphertext);
312 BSL_SAL_Free(sharedKeyA);
313 BSL_SAL_Free(sharedKeyB);
314 CRYPT_EAL_PkeyFreeCtx(ctxA);
315 CRYPT_EAL_PkeyFreeCtx(ctxB);
316 CRYPT_RandRegist(NULL);
317 CRYPT_RandRegistEx(NULL);
318 return;
319 }
320 /* END_CASE */
321