1a8e1175bSopenharmony_ci/* BEGIN_HEADER */
2a8e1175bSopenharmony_ci#include "mbedtls/ecjpake.h"
3a8e1175bSopenharmony_ci
4a8e1175bSopenharmony_ci#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) && defined(MBEDTLS_MD_CAN_SHA256)
5a8e1175bSopenharmony_cistatic const unsigned char ecjpake_test_x1[] = {
6a8e1175bSopenharmony_ci    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
7a8e1175bSopenharmony_ci    0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
8a8e1175bSopenharmony_ci    0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x21
9a8e1175bSopenharmony_ci};
10a8e1175bSopenharmony_ci
11a8e1175bSopenharmony_cistatic const unsigned char ecjpake_test_x2[] = {
12a8e1175bSopenharmony_ci    0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
13a8e1175bSopenharmony_ci    0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
14a8e1175bSopenharmony_ci    0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x81
15a8e1175bSopenharmony_ci};
16a8e1175bSopenharmony_ci
17a8e1175bSopenharmony_cistatic const unsigned char ecjpake_test_x3[] = {
18a8e1175bSopenharmony_ci    0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
19a8e1175bSopenharmony_ci    0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
20a8e1175bSopenharmony_ci    0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x81
21a8e1175bSopenharmony_ci};
22a8e1175bSopenharmony_ci
23a8e1175bSopenharmony_cistatic const unsigned char ecjpake_test_x4[] = {
24a8e1175bSopenharmony_ci    0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc,
25a8e1175bSopenharmony_ci    0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
26a8e1175bSopenharmony_ci    0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe1
27a8e1175bSopenharmony_ci};
28a8e1175bSopenharmony_ci
29a8e1175bSopenharmony_cistatic const unsigned char ecjpake_test_X1[] = {
30a8e1175bSopenharmony_ci    0x04, 0xac, 0xcf, 0x01, 0x06, 0xef, 0x85, 0x8f, 0xa2, 0xd9, 0x19, 0x33,
31a8e1175bSopenharmony_ci    0x13, 0x46, 0x80, 0x5a, 0x78, 0xb5, 0x8b, 0xba, 0xd0, 0xb8, 0x44, 0xe5,
32a8e1175bSopenharmony_ci    0xc7, 0x89, 0x28, 0x79, 0x14, 0x61, 0x87, 0xdd, 0x26, 0x66, 0xad, 0xa7,
33a8e1175bSopenharmony_ci    0x81, 0xbb, 0x7f, 0x11, 0x13, 0x72, 0x25, 0x1a, 0x89, 0x10, 0x62, 0x1f,
34a8e1175bSopenharmony_ci    0x63, 0x4d, 0xf1, 0x28, 0xac, 0x48, 0xe3, 0x81, 0xfd, 0x6e, 0xf9, 0x06,
35a8e1175bSopenharmony_ci    0x07, 0x31, 0xf6, 0x94, 0xa4
36a8e1175bSopenharmony_ci};
37a8e1175bSopenharmony_ci
38a8e1175bSopenharmony_cistatic const unsigned char ecjpake_test_X2[] = {
39a8e1175bSopenharmony_ci    0x04, 0x7e, 0xa6, 0xe3, 0xa4, 0x48, 0x70, 0x37, 0xa9, 0xe0, 0xdb, 0xd7,
40a8e1175bSopenharmony_ci    0x92, 0x62, 0xb2, 0xcc, 0x27, 0x3e, 0x77, 0x99, 0x30, 0xfc, 0x18, 0x40,
41a8e1175bSopenharmony_ci    0x9a, 0xc5, 0x36, 0x1c, 0x5f, 0xe6, 0x69, 0xd7, 0x02, 0xe1, 0x47, 0x79,
42a8e1175bSopenharmony_ci    0x0a, 0xeb, 0x4c, 0xe7, 0xfd, 0x65, 0x75, 0xab, 0x0f, 0x6c, 0x7f, 0xd1,
43a8e1175bSopenharmony_ci    0xc3, 0x35, 0x93, 0x9a, 0xa8, 0x63, 0xba, 0x37, 0xec, 0x91, 0xb7, 0xe3,
44a8e1175bSopenharmony_ci    0x2b, 0xb0, 0x13, 0xbb, 0x2b
45a8e1175bSopenharmony_ci};
46a8e1175bSopenharmony_ci
47a8e1175bSopenharmony_cistatic const unsigned char ecjpake_test_X3[] = {
48a8e1175bSopenharmony_ci    0x04, 0x7e, 0xa6, 0xe3, 0xa4, 0x48, 0x70, 0x37, 0xa9, 0xe0, 0xdb, 0xd7,
49a8e1175bSopenharmony_ci    0x92, 0x62, 0xb2, 0xcc, 0x27, 0x3e, 0x77, 0x99, 0x30, 0xfc, 0x18, 0x40,
50a8e1175bSopenharmony_ci    0x9a, 0xc5, 0x36, 0x1c, 0x5f, 0xe6, 0x69, 0xd7, 0x02, 0xe1, 0x47, 0x79,
51a8e1175bSopenharmony_ci    0x0a, 0xeb, 0x4c, 0xe7, 0xfd, 0x65, 0x75, 0xab, 0x0f, 0x6c, 0x7f, 0xd1,
52a8e1175bSopenharmony_ci    0xc3, 0x35, 0x93, 0x9a, 0xa8, 0x63, 0xba, 0x37, 0xec, 0x91, 0xb7, 0xe3,
53a8e1175bSopenharmony_ci    0x2b, 0xb0, 0x13, 0xbb, 0x2b
54a8e1175bSopenharmony_ci};
55a8e1175bSopenharmony_ci
56a8e1175bSopenharmony_cistatic const unsigned char ecjpake_test_X4[] = {
57a8e1175bSopenharmony_ci    0x04, 0x19, 0x0a, 0x07, 0x70, 0x0f, 0xfa, 0x4b, 0xe6, 0xae, 0x1d, 0x79,
58a8e1175bSopenharmony_ci    0xee, 0x0f, 0x06, 0xae, 0xb5, 0x44, 0xcd, 0x5a, 0xdd, 0xaa, 0xbe, 0xdf,
59a8e1175bSopenharmony_ci    0x70, 0xf8, 0x62, 0x33, 0x21, 0x33, 0x2c, 0x54, 0xf3, 0x55, 0xf0, 0xfb,
60a8e1175bSopenharmony_ci    0xfe, 0xc7, 0x83, 0xed, 0x35, 0x9e, 0x5d, 0x0b, 0xf7, 0x37, 0x7a, 0x0f,
61a8e1175bSopenharmony_ci    0xc4, 0xea, 0x7a, 0xce, 0x47, 0x3c, 0x9c, 0x11, 0x2b, 0x41, 0xcc, 0xd4,
62a8e1175bSopenharmony_ci    0x1a, 0xc5, 0x6a, 0x56, 0x12
63a8e1175bSopenharmony_ci};
64a8e1175bSopenharmony_ci
65a8e1175bSopenharmony_ci/* Load my private and public keys, and peer's public keys */
66a8e1175bSopenharmony_cistatic int ecjpake_test_load(mbedtls_ecjpake_context *ctx,
67a8e1175bSopenharmony_ci                             const unsigned char *xm1, size_t len_xm1,
68a8e1175bSopenharmony_ci                             const unsigned char *xm2, size_t len_xm2,
69a8e1175bSopenharmony_ci                             const unsigned char *Xm1, size_t len_Xm1,
70a8e1175bSopenharmony_ci                             const unsigned char *Xm2, size_t len_Xm2,
71a8e1175bSopenharmony_ci                             const unsigned char *Xp1, size_t len_Xp1,
72a8e1175bSopenharmony_ci                             const unsigned char *Xp2, size_t len_Xp2)
73a8e1175bSopenharmony_ci{
74a8e1175bSopenharmony_ci    int ret;
75a8e1175bSopenharmony_ci
76a8e1175bSopenharmony_ci    MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->xm1, xm1, len_xm1));
77a8e1175bSopenharmony_ci    MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->xm2, xm2, len_xm2));
78a8e1175bSopenharmony_ci
79a8e1175bSopenharmony_ci    MBEDTLS_MPI_CHK(mbedtls_ecp_point_read_binary(&ctx->grp,
80a8e1175bSopenharmony_ci                                                  &ctx->Xm1, Xm1, len_Xm1));
81a8e1175bSopenharmony_ci    MBEDTLS_MPI_CHK(mbedtls_ecp_point_read_binary(&ctx->grp,
82a8e1175bSopenharmony_ci                                                  &ctx->Xm2, Xm2, len_Xm2));
83a8e1175bSopenharmony_ci    MBEDTLS_MPI_CHK(mbedtls_ecp_point_read_binary(&ctx->grp,
84a8e1175bSopenharmony_ci                                                  &ctx->Xp1, Xp1, len_Xp1));
85a8e1175bSopenharmony_ci    MBEDTLS_MPI_CHK(mbedtls_ecp_point_read_binary(&ctx->grp,
86a8e1175bSopenharmony_ci                                                  &ctx->Xp2, Xp2, len_Xp2));
87a8e1175bSopenharmony_ci
88a8e1175bSopenharmony_cicleanup:
89a8e1175bSopenharmony_ci    return ret;
90a8e1175bSopenharmony_ci}
91a8e1175bSopenharmony_ci
92a8e1175bSopenharmony_ci#define ADD_SIZE(x)   x, sizeof(x)
93a8e1175bSopenharmony_ci#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED && MBEDTLS_MD_CAN_SHA256 */
94a8e1175bSopenharmony_ci/* END_HEADER */
95a8e1175bSopenharmony_ci
96a8e1175bSopenharmony_ci/* BEGIN_DEPENDENCIES
97a8e1175bSopenharmony_ci * depends_on:MBEDTLS_ECJPAKE_C
98a8e1175bSopenharmony_ci * END_DEPENDENCIES
99a8e1175bSopenharmony_ci */
100a8e1175bSopenharmony_ci
101a8e1175bSopenharmony_ci/* BEGIN_CASE */
102a8e1175bSopenharmony_civoid ecjpake_invalid_param()
103a8e1175bSopenharmony_ci{
104a8e1175bSopenharmony_ci    mbedtls_ecjpake_context ctx;
105a8e1175bSopenharmony_ci    unsigned char buf[42] = { 0 };
106a8e1175bSopenharmony_ci    size_t const len = sizeof(buf);
107a8e1175bSopenharmony_ci    mbedtls_ecjpake_role invalid_role = (mbedtls_ecjpake_role) 42;
108a8e1175bSopenharmony_ci    mbedtls_md_type_t valid_md = MBEDTLS_MD_SHA256;
109a8e1175bSopenharmony_ci    mbedtls_ecp_group_id valid_group = MBEDTLS_ECP_DP_SECP256R1;
110a8e1175bSopenharmony_ci
111a8e1175bSopenharmony_ci    MD_PSA_INIT();
112a8e1175bSopenharmony_ci
113a8e1175bSopenharmony_ci    mbedtls_ecjpake_init(&ctx);
114a8e1175bSopenharmony_ci
115a8e1175bSopenharmony_ci    TEST_EQUAL(MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
116a8e1175bSopenharmony_ci               mbedtls_ecjpake_setup(&ctx,
117a8e1175bSopenharmony_ci                                     invalid_role,
118a8e1175bSopenharmony_ci                                     valid_md,
119a8e1175bSopenharmony_ci                                     valid_group,
120a8e1175bSopenharmony_ci                                     buf, len));
121a8e1175bSopenharmony_ciexit:
122a8e1175bSopenharmony_ci    MD_PSA_DONE();
123a8e1175bSopenharmony_ci}
124a8e1175bSopenharmony_ci/* END_CASE */
125a8e1175bSopenharmony_ci
126a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
127a8e1175bSopenharmony_civoid ecjpake_selftest()
128a8e1175bSopenharmony_ci{
129a8e1175bSopenharmony_ci    MD_PSA_INIT();
130a8e1175bSopenharmony_ci
131a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_ecjpake_self_test(1) == 0);
132a8e1175bSopenharmony_ci
133a8e1175bSopenharmony_ciexit:
134a8e1175bSopenharmony_ci    MD_PSA_DONE();
135a8e1175bSopenharmony_ci}
136a8e1175bSopenharmony_ci/* END_CASE */
137a8e1175bSopenharmony_ci
138a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_MD_CAN_SHA256 */
139a8e1175bSopenharmony_civoid read_bad_md(data_t *msg)
140a8e1175bSopenharmony_ci{
141a8e1175bSopenharmony_ci    mbedtls_ecjpake_context corrupt_ctx;
142a8e1175bSopenharmony_ci    const unsigned char *pw = NULL;
143a8e1175bSopenharmony_ci    const size_t pw_len = 0;
144a8e1175bSopenharmony_ci    int any_role = MBEDTLS_ECJPAKE_CLIENT;
145a8e1175bSopenharmony_ci
146a8e1175bSopenharmony_ci    MD_PSA_INIT();
147a8e1175bSopenharmony_ci
148a8e1175bSopenharmony_ci    mbedtls_ecjpake_init(&corrupt_ctx);
149a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_ecjpake_setup(&corrupt_ctx, any_role,
150a8e1175bSopenharmony_ci                                      MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
151a8e1175bSopenharmony_ci                                      pw_len) == 0);
152a8e1175bSopenharmony_ci    corrupt_ctx.md_type = MBEDTLS_MD_NONE;
153a8e1175bSopenharmony_ci
154a8e1175bSopenharmony_ci    TEST_EQUAL(mbedtls_ecjpake_read_round_one(&corrupt_ctx, msg->x,
155a8e1175bSopenharmony_ci                                              msg->len), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
156a8e1175bSopenharmony_ci
157a8e1175bSopenharmony_ciexit:
158a8e1175bSopenharmony_ci    mbedtls_ecjpake_free(&corrupt_ctx);
159a8e1175bSopenharmony_ci    MD_PSA_DONE();
160a8e1175bSopenharmony_ci}
161a8e1175bSopenharmony_ci/* END_CASE */
162a8e1175bSopenharmony_ci
163a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_MD_CAN_SHA256 */
164a8e1175bSopenharmony_civoid read_round_one(int role, data_t *msg, int ref_ret)
165a8e1175bSopenharmony_ci{
166a8e1175bSopenharmony_ci    mbedtls_ecjpake_context ctx;
167a8e1175bSopenharmony_ci    const unsigned char *pw = NULL;
168a8e1175bSopenharmony_ci    const size_t pw_len = 0;
169a8e1175bSopenharmony_ci
170a8e1175bSopenharmony_ci    MD_PSA_INIT();
171a8e1175bSopenharmony_ci
172a8e1175bSopenharmony_ci    mbedtls_ecjpake_init(&ctx);
173a8e1175bSopenharmony_ci
174a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_ecjpake_setup(&ctx, role,
175a8e1175bSopenharmony_ci                                      MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
176a8e1175bSopenharmony_ci                                      pw_len) == 0);
177a8e1175bSopenharmony_ci
178a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_ecjpake_read_round_one(&ctx, msg->x, msg->len) == ref_ret);
179a8e1175bSopenharmony_ci
180a8e1175bSopenharmony_ciexit:
181a8e1175bSopenharmony_ci    mbedtls_ecjpake_free(&ctx);
182a8e1175bSopenharmony_ci    MD_PSA_DONE();
183a8e1175bSopenharmony_ci}
184a8e1175bSopenharmony_ci/* END_CASE */
185a8e1175bSopenharmony_ci
186a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_MD_CAN_SHA256 */
187a8e1175bSopenharmony_civoid read_round_two_cli(data_t *msg, int ref_ret)
188a8e1175bSopenharmony_ci{
189a8e1175bSopenharmony_ci    mbedtls_ecjpake_context ctx;
190a8e1175bSopenharmony_ci    const unsigned char *pw = NULL;
191a8e1175bSopenharmony_ci    const size_t pw_len = 0;
192a8e1175bSopenharmony_ci
193a8e1175bSopenharmony_ci    MD_PSA_INIT();
194a8e1175bSopenharmony_ci
195a8e1175bSopenharmony_ci    mbedtls_ecjpake_init(&ctx);
196a8e1175bSopenharmony_ci
197a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_ecjpake_setup(&ctx, MBEDTLS_ECJPAKE_CLIENT,
198a8e1175bSopenharmony_ci                                      MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
199a8e1175bSopenharmony_ci                                      pw_len) == 0);
200a8e1175bSopenharmony_ci
201a8e1175bSopenharmony_ci    TEST_ASSERT(ecjpake_test_load(&ctx,
202a8e1175bSopenharmony_ci                                  ADD_SIZE(ecjpake_test_x1), ADD_SIZE(ecjpake_test_x2),
203a8e1175bSopenharmony_ci                                  ADD_SIZE(ecjpake_test_X1), ADD_SIZE(ecjpake_test_X2),
204a8e1175bSopenharmony_ci                                  ADD_SIZE(ecjpake_test_X3), ADD_SIZE(ecjpake_test_X4))
205a8e1175bSopenharmony_ci                == 0);
206a8e1175bSopenharmony_ci
207a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_ecjpake_read_round_two(&ctx, msg->x, msg->len) == ref_ret);
208a8e1175bSopenharmony_ci
209a8e1175bSopenharmony_ciexit:
210a8e1175bSopenharmony_ci    mbedtls_ecjpake_free(&ctx);
211a8e1175bSopenharmony_ci    MD_PSA_DONE();
212a8e1175bSopenharmony_ci}
213a8e1175bSopenharmony_ci/* END_CASE */
214a8e1175bSopenharmony_ci
215a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_MD_CAN_SHA256 */
216a8e1175bSopenharmony_civoid read_round_two_srv(data_t *msg, int ref_ret)
217a8e1175bSopenharmony_ci{
218a8e1175bSopenharmony_ci    mbedtls_ecjpake_context ctx;
219a8e1175bSopenharmony_ci    const unsigned char *pw = NULL;
220a8e1175bSopenharmony_ci    const size_t pw_len = 0;
221a8e1175bSopenharmony_ci
222a8e1175bSopenharmony_ci    MD_PSA_INIT();
223a8e1175bSopenharmony_ci
224a8e1175bSopenharmony_ci    mbedtls_ecjpake_init(&ctx);
225a8e1175bSopenharmony_ci
226a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_ecjpake_setup(&ctx, MBEDTLS_ECJPAKE_SERVER,
227a8e1175bSopenharmony_ci                                      MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
228a8e1175bSopenharmony_ci                                      pw_len) == 0);
229a8e1175bSopenharmony_ci
230a8e1175bSopenharmony_ci    TEST_ASSERT(ecjpake_test_load(&ctx,
231a8e1175bSopenharmony_ci                                  ADD_SIZE(ecjpake_test_x3), ADD_SIZE(ecjpake_test_x4),
232a8e1175bSopenharmony_ci                                  ADD_SIZE(ecjpake_test_X3), ADD_SIZE(ecjpake_test_X4),
233a8e1175bSopenharmony_ci                                  ADD_SIZE(ecjpake_test_X1), ADD_SIZE(ecjpake_test_X2))
234a8e1175bSopenharmony_ci                == 0);
235a8e1175bSopenharmony_ci
236a8e1175bSopenharmony_ci    TEST_ASSERT(mbedtls_ecjpake_read_round_two(&ctx, msg->x, msg->len) == ref_ret);
237a8e1175bSopenharmony_ci
238a8e1175bSopenharmony_ciexit:
239a8e1175bSopenharmony_ci    mbedtls_ecjpake_free(&ctx);
240a8e1175bSopenharmony_ci    MD_PSA_DONE();
241a8e1175bSopenharmony_ci}
242a8e1175bSopenharmony_ci/* END_CASE */
243