blob: 177e09a9a6389b4e3d58fe653aa3b85de758be17 [file] [log] [blame]
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +02001/* BEGIN_HEADER */
2#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +02003
Manuel Pégourié-Gonnardb2eb1f72023-03-15 00:12:24 +01004#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) && defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +02005static const unsigned char ecjpake_test_x1[] = {
6 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
7 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
8 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x21
9};
10
11static const unsigned char ecjpake_test_x2[] = {
12 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
13 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
14 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x81
15};
16
17static const unsigned char ecjpake_test_x3[] = {
18 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
19 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
20 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x81
21};
22
23static const unsigned char ecjpake_test_x4[] = {
24 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc,
25 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
26 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe1
27};
28
29static const unsigned char ecjpake_test_X1[] = {
30 0x04, 0xac, 0xcf, 0x01, 0x06, 0xef, 0x85, 0x8f, 0xa2, 0xd9, 0x19, 0x33,
31 0x13, 0x46, 0x80, 0x5a, 0x78, 0xb5, 0x8b, 0xba, 0xd0, 0xb8, 0x44, 0xe5,
32 0xc7, 0x89, 0x28, 0x79, 0x14, 0x61, 0x87, 0xdd, 0x26, 0x66, 0xad, 0xa7,
33 0x81, 0xbb, 0x7f, 0x11, 0x13, 0x72, 0x25, 0x1a, 0x89, 0x10, 0x62, 0x1f,
34 0x63, 0x4d, 0xf1, 0x28, 0xac, 0x48, 0xe3, 0x81, 0xfd, 0x6e, 0xf9, 0x06,
35 0x07, 0x31, 0xf6, 0x94, 0xa4
36};
37
38static const unsigned char ecjpake_test_X2[] = {
39 0x04, 0x7e, 0xa6, 0xe3, 0xa4, 0x48, 0x70, 0x37, 0xa9, 0xe0, 0xdb, 0xd7,
40 0x92, 0x62, 0xb2, 0xcc, 0x27, 0x3e, 0x77, 0x99, 0x30, 0xfc, 0x18, 0x40,
41 0x9a, 0xc5, 0x36, 0x1c, 0x5f, 0xe6, 0x69, 0xd7, 0x02, 0xe1, 0x47, 0x79,
42 0x0a, 0xeb, 0x4c, 0xe7, 0xfd, 0x65, 0x75, 0xab, 0x0f, 0x6c, 0x7f, 0xd1,
43 0xc3, 0x35, 0x93, 0x9a, 0xa8, 0x63, 0xba, 0x37, 0xec, 0x91, 0xb7, 0xe3,
44 0x2b, 0xb0, 0x13, 0xbb, 0x2b
45};
46
47static const unsigned char ecjpake_test_X3[] = {
48 0x04, 0x7e, 0xa6, 0xe3, 0xa4, 0x48, 0x70, 0x37, 0xa9, 0xe0, 0xdb, 0xd7,
49 0x92, 0x62, 0xb2, 0xcc, 0x27, 0x3e, 0x77, 0x99, 0x30, 0xfc, 0x18, 0x40,
50 0x9a, 0xc5, 0x36, 0x1c, 0x5f, 0xe6, 0x69, 0xd7, 0x02, 0xe1, 0x47, 0x79,
51 0x0a, 0xeb, 0x4c, 0xe7, 0xfd, 0x65, 0x75, 0xab, 0x0f, 0x6c, 0x7f, 0xd1,
52 0xc3, 0x35, 0x93, 0x9a, 0xa8, 0x63, 0xba, 0x37, 0xec, 0x91, 0xb7, 0xe3,
53 0x2b, 0xb0, 0x13, 0xbb, 0x2b
54};
55
56static const unsigned char ecjpake_test_X4[] = {
57 0x04, 0x19, 0x0a, 0x07, 0x70, 0x0f, 0xfa, 0x4b, 0xe6, 0xae, 0x1d, 0x79,
58 0xee, 0x0f, 0x06, 0xae, 0xb5, 0x44, 0xcd, 0x5a, 0xdd, 0xaa, 0xbe, 0xdf,
59 0x70, 0xf8, 0x62, 0x33, 0x21, 0x33, 0x2c, 0x54, 0xf3, 0x55, 0xf0, 0xfb,
60 0xfe, 0xc7, 0x83, 0xed, 0x35, 0x9e, 0x5d, 0x0b, 0xf7, 0x37, 0x7a, 0x0f,
61 0xc4, 0xea, 0x7a, 0xce, 0x47, 0x3c, 0x9c, 0x11, 0x2b, 0x41, 0xcc, 0xd4,
62 0x1a, 0xc5, 0x6a, 0x56, 0x12
63};
64
65/* Load my private and public keys, and peer's public keys */
Gilles Peskine449bd832023-01-11 14:50:10 +010066static int ecjpake_test_load(mbedtls_ecjpake_context *ctx,
67 const unsigned char *xm1, size_t len_xm1,
68 const unsigned char *xm2, size_t len_xm2,
69 const unsigned char *Xm1, size_t len_Xm1,
70 const unsigned char *Xm2, size_t len_Xm2,
71 const unsigned char *Xp1, size_t len_Xp1,
72 const unsigned char *Xp2, size_t len_Xp2)
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +020073{
74 int ret;
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->xm1, xm1, len_xm1));
77 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&ctx->xm2, xm2, len_xm2));
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +020078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 MBEDTLS_MPI_CHK(mbedtls_ecp_point_read_binary(&ctx->grp,
80 &ctx->Xm1, Xm1, len_Xm1));
81 MBEDTLS_MPI_CHK(mbedtls_ecp_point_read_binary(&ctx->grp,
82 &ctx->Xm2, Xm2, len_Xm2));
83 MBEDTLS_MPI_CHK(mbedtls_ecp_point_read_binary(&ctx->grp,
84 &ctx->Xp1, Xp1, len_Xp1));
85 MBEDTLS_MPI_CHK(mbedtls_ecp_point_read_binary(&ctx->grp,
86 &ctx->Xp2, Xp2, len_Xp2));
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +020087
88cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +010089 return ret;
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +020090}
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092#define ADD_SIZE(x) x, sizeof(x)
Manuel Pégourié-Gonnardb2eb1f72023-03-15 00:12:24 +010093#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED && MBEDTLS_MD_CAN_SHA256 */
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020094/* END_HEADER */
95
96/* BEGIN_DEPENDENCIES
97 * depends_on:MBEDTLS_ECJPAKE_C
98 * END_DEPENDENCIES
99 */
100
Tuvshinzaya Erdenekhuu8df96032022-07-27 15:13:38 +0100101/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102void ecjpake_invalid_param()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500103{
104 mbedtls_ecjpake_context ctx;
105 unsigned char buf[42] = { 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 size_t const len = sizeof(buf);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500107 mbedtls_ecjpake_role invalid_role = (mbedtls_ecjpake_role) 42;
108 mbedtls_md_type_t valid_md = MBEDTLS_MD_SHA256;
109 mbedtls_ecp_group_id valid_group = MBEDTLS_ECP_DP_SECP256R1;
110
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100111 MD_PSA_INIT();
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 mbedtls_ecjpake_init(&ctx);
Gabor Mezei0bb13822022-09-27 18:48:44 +0200114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 TEST_EQUAL(MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
116 mbedtls_ecjpake_setup(&ctx,
117 invalid_role,
118 valid_md,
119 valid_group,
120 buf, len));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121exit:
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100122 MD_PSA_DONE();
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500123}
124/* END_CASE */
125
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200126/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127void ecjpake_selftest()
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200128{
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100129 MD_PSA_INIT();
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 TEST_ASSERT(mbedtls_ecjpake_self_test(1) == 0);
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100132
133exit:
134 MD_PSA_DONE();
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200135}
136/* END_CASE */
Manuel Pégourié-Gonnardbbe4e522015-08-14 17:12:50 +0200137
Manuel Pégourié-Gonnardb2eb1f72023-03-15 00:12:24 +0100138/* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_MD_CAN_SHA256 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139void read_bad_md(data_t *msg)
k-stachowiak01b3be42019-06-28 14:17:04 +0200140{
141 mbedtls_ecjpake_context corrupt_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 const unsigned char *pw = NULL;
k-stachowiak01b3be42019-06-28 14:17:04 +0200143 const size_t pw_len = 0;
144 int any_role = MBEDTLS_ECJPAKE_CLIENT;
145
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100146 MD_PSA_INIT();
147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_ecjpake_init(&corrupt_ctx);
149 TEST_ASSERT(mbedtls_ecjpake_setup(&corrupt_ctx, any_role,
150 MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
151 pw_len) == 0);
Neil Armstrong0d763412022-08-11 10:32:22 +0200152 corrupt_ctx.md_type = MBEDTLS_MD_NONE;
k-stachowiak01b3be42019-06-28 14:17:04 +0200153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 TEST_EQUAL(mbedtls_ecjpake_read_round_one(&corrupt_ctx, msg->x,
155 msg->len), MBEDTLS_ERR_MD_BAD_INPUT_DATA);
k-stachowiak01b3be42019-06-28 14:17:04 +0200156
157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 mbedtls_ecjpake_free(&corrupt_ctx);
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100159 MD_PSA_DONE();
k-stachowiak01b3be42019-06-28 14:17:04 +0200160}
161/* END_CASE */
162
Manuel Pégourié-Gonnardb2eb1f72023-03-15 00:12:24 +0100163/* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_MD_CAN_SHA256 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164void read_round_one(int role, data_t *msg, int ref_ret)
Manuel Pégourié-Gonnardbbe4e522015-08-14 17:12:50 +0200165{
166 mbedtls_ecjpake_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 const unsigned char *pw = NULL;
Hanno Becker8435c382017-06-05 15:02:46 +0100168 const size_t pw_len = 0;
169
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100170 MD_PSA_INIT();
171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 mbedtls_ecjpake_init(&ctx);
Manuel Pégourié-Gonnardbbe4e522015-08-14 17:12:50 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 TEST_ASSERT(mbedtls_ecjpake_setup(&ctx, role,
175 MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
176 pw_len) == 0);
Manuel Pégourié-Gonnardbbe4e522015-08-14 17:12:50 +0200177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 TEST_ASSERT(mbedtls_ecjpake_read_round_one(&ctx, msg->x, msg->len) == ref_ret);
Manuel Pégourié-Gonnardbbe4e522015-08-14 17:12:50 +0200179
180exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_ecjpake_free(&ctx);
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100182 MD_PSA_DONE();
Manuel Pégourié-Gonnardbbe4e522015-08-14 17:12:50 +0200183}
184/* END_CASE */
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200185
Manuel Pégourié-Gonnardb2eb1f72023-03-15 00:12:24 +0100186/* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_MD_CAN_SHA256 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187void read_round_two_cli(data_t *msg, int ref_ret)
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200188{
189 mbedtls_ecjpake_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 const unsigned char *pw = NULL;
Hanno Becker8435c382017-06-05 15:02:46 +0100191 const size_t pw_len = 0;
192
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100193 MD_PSA_INIT();
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 mbedtls_ecjpake_init(&ctx);
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 TEST_ASSERT(mbedtls_ecjpake_setup(&ctx, MBEDTLS_ECJPAKE_CLIENT,
198 MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
199 pw_len) == 0);
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 TEST_ASSERT(ecjpake_test_load(&ctx,
202 ADD_SIZE(ecjpake_test_x1), ADD_SIZE(ecjpake_test_x2),
203 ADD_SIZE(ecjpake_test_X1), ADD_SIZE(ecjpake_test_X2),
204 ADD_SIZE(ecjpake_test_X3), ADD_SIZE(ecjpake_test_X4))
205 == 0);
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 TEST_ASSERT(mbedtls_ecjpake_read_round_two(&ctx, msg->x, msg->len) == ref_ret);
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200208
209exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 mbedtls_ecjpake_free(&ctx);
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100211 MD_PSA_DONE();
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200212}
213/* END_CASE */
214
Manuel Pégourié-Gonnardb2eb1f72023-03-15 00:12:24 +0100215/* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_MD_CAN_SHA256 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216void read_round_two_srv(data_t *msg, int ref_ret)
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200217{
218 mbedtls_ecjpake_context ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 const unsigned char *pw = NULL;
Hanno Becker8435c382017-06-05 15:02:46 +0100220 const size_t pw_len = 0;
221
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100222 MD_PSA_INIT();
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 mbedtls_ecjpake_init(&ctx);
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 TEST_ASSERT(mbedtls_ecjpake_setup(&ctx, MBEDTLS_ECJPAKE_SERVER,
227 MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw,
228 pw_len) == 0);
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 TEST_ASSERT(ecjpake_test_load(&ctx,
231 ADD_SIZE(ecjpake_test_x3), ADD_SIZE(ecjpake_test_x4),
232 ADD_SIZE(ecjpake_test_X3), ADD_SIZE(ecjpake_test_X4),
233 ADD_SIZE(ecjpake_test_X1), ADD_SIZE(ecjpake_test_X2))
234 == 0);
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 TEST_ASSERT(mbedtls_ecjpake_read_round_two(&ctx, msg->x, msg->len) == ref_ret);
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200237
238exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_ecjpake_free(&ctx);
Manuel Pégourié-Gonnard41bc8b62023-03-14 23:59:24 +0100240 MD_PSA_DONE();
Manuel Pégourié-Gonnardd9802af2015-08-17 12:47:38 +0200241}
242/* END_CASE */