2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/dhm.h"
|
2021-03-30 23:28:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int check_get_value(const mbedtls_dhm_context *ctx,
|
|
|
|
mbedtls_dhm_parameter param,
|
|
|
|
const mbedtls_mpi *expected)
|
2021-05-27 22:50:53 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi actual;
|
|
|
|
int ok = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&actual);
|
2021-05-27 22:50:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_dhm_get_value(ctx, param, &actual) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&actual, expected) == 0);
|
2021-05-27 22:50:53 +02:00
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&actual);
|
|
|
|
return ok;
|
2021-05-27 22:50:53 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 22:16:45 +02:00
|
|
|
/* Sanity checks on a Diffie-Hellman parameter: check the length-value
|
|
|
|
* syntax and check that the value is the expected one (taken from the
|
|
|
|
* DHM context by the caller). */
|
2023-01-11 14:50:10 +01:00
|
|
|
static int check_dhm_param_output(const mbedtls_mpi *expected,
|
|
|
|
const unsigned char *buffer,
|
|
|
|
size_t size,
|
|
|
|
size_t *offset)
|
2021-03-30 23:28:51 +02:00
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
mbedtls_mpi actual;
|
|
|
|
int ok = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&actual);
|
2021-03-30 23:28:51 +02:00
|
|
|
|
2023-10-27 19:41:02 +02:00
|
|
|
mbedtls_test_increment_step();
|
2021-03-30 23:28:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(size >= *offset + 2);
|
|
|
|
n = (buffer[*offset] << 8) | buffer[*offset + 1];
|
2021-03-30 23:28:51 +02:00
|
|
|
*offset += 2;
|
2021-04-13 22:10:24 +02:00
|
|
|
/* The DHM param output from Mbed TLS has leading zeros stripped, as
|
|
|
|
* permitted but not required by RFC 5246 \S4.4. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(n, mbedtls_mpi_size(expected));
|
|
|
|
TEST_ASSERT(size >= *offset + n);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_read_binary(&actual, buffer + *offset, n));
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_cmp_mpi(expected, &actual));
|
2021-03-30 23:28:51 +02:00
|
|
|
*offset += n;
|
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&actual);
|
|
|
|
return ok;
|
2021-03-30 23:28:51 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 22:16:45 +02:00
|
|
|
/* Sanity checks on Diffie-Hellman parameters: syntax, range, and comparison
|
|
|
|
* against the context. */
|
2023-01-11 14:50:10 +01:00
|
|
|
static int check_dhm_params(const mbedtls_dhm_context *ctx,
|
|
|
|
size_t x_size,
|
|
|
|
const unsigned char *ske, size_t ske_len)
|
2021-03-30 23:28:51 +02:00
|
|
|
{
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
/* Check that ctx->X and ctx->GX are within range. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_int(&ctx->X, 1) > 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&ctx->X, &ctx->P) < 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_size(&ctx->X) <= x_size);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_int(&ctx->GX, 1) > 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&ctx->GX, &ctx->P) < 0);
|
2021-03-30 23:28:51 +02:00
|
|
|
|
|
|
|
/* Check ske: it must contain P, G and G^X, each prefixed with a
|
|
|
|
* 2-byte size. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!check_dhm_param_output(&ctx->P, ske, ske_len, &offset)) {
|
2021-03-30 23:28:51 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (!check_dhm_param_output(&ctx->G, ske, ske_len, &offset)) {
|
2021-03-30 23:28:51 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (!check_dhm_param_output(&ctx->GX, ske, ske_len, &offset)) {
|
2021-03-30 23:28:51 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
TEST_EQUAL(offset, ske_len);
|
2021-03-30 23:28:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 1;
|
2021-03-30 23:28:51 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-03-30 23:28:51 +02:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2009-07-08 21:47:36 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_DHM_C:MBEDTLS_BIGNUM_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2011-05-26 15:16:06 +02:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void dhm_do_dhm(char *input_P, int x_size,
|
|
|
|
char *input_G, int result)
|
2009-07-08 21:47:36 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_dhm_context ctx_srv;
|
|
|
|
mbedtls_dhm_context ctx_cli;
|
2009-07-08 21:47:36 +02:00
|
|
|
unsigned char ske[1000];
|
|
|
|
unsigned char *p = ske;
|
|
|
|
unsigned char pub_cli[1000];
|
|
|
|
unsigned char sec_srv[1000];
|
|
|
|
unsigned char sec_cli[1000];
|
2011-04-24 17:53:29 +02:00
|
|
|
size_t ske_len = 0;
|
|
|
|
size_t pub_cli_len = 0;
|
2015-06-02 17:17:08 +02:00
|
|
|
size_t sec_srv_len;
|
|
|
|
size_t sec_cli_len;
|
2021-03-30 23:44:22 +02:00
|
|
|
int i;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2009-07-08 21:47:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_dhm_init(&ctx_srv);
|
|
|
|
mbedtls_dhm_init(&ctx_cli);
|
|
|
|
memset(ske, 0x00, 1000);
|
|
|
|
memset(pub_cli, 0x00, 1000);
|
|
|
|
memset(sec_srv, 0x00, 1000);
|
|
|
|
memset(sec_cli, 0x00, 1000);
|
|
|
|
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
|
2009-07-08 21:47:36 +02:00
|
|
|
|
2013-09-04 16:29:59 +02:00
|
|
|
/*
|
|
|
|
* Set params
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&ctx_srv.P, input_P) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&ctx_srv.G, input_G) == 0);
|
|
|
|
pub_cli_len = mbedtls_mpi_size(&ctx_srv.P);
|
|
|
|
TEST_ASSERT(check_get_value(&ctx_srv, MBEDTLS_DHM_PARAM_P, &ctx_srv.P));
|
|
|
|
TEST_ASSERT(check_get_value(&ctx_srv, MBEDTLS_DHM_PARAM_G, &ctx_srv.G));
|
2009-07-08 21:47:36 +02:00
|
|
|
|
2013-09-04 16:29:59 +02:00
|
|
|
/*
|
|
|
|
* First key exchange
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_set_step(10);
|
|
|
|
TEST_ASSERT(mbedtls_dhm_make_params(&ctx_srv, x_size, ske, &ske_len,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == result);
|
|
|
|
if (result != 0) {
|
2017-09-20 14:46:37 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (!check_dhm_params(&ctx_srv, x_size, ske, ske_len)) {
|
2021-03-30 23:28:51 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-09-20 14:46:37 +02:00
|
|
|
|
2009-07-08 21:47:36 +02:00
|
|
|
ske[ske_len++] = 0;
|
|
|
|
ske[ske_len++] = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_dhm_read_params(&ctx_cli, &p, ske + ske_len) == 0);
|
2021-05-27 22:50:53 +02:00
|
|
|
/* The domain parameters must be the same on both side. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(check_get_value(&ctx_cli, MBEDTLS_DHM_PARAM_P, &ctx_srv.P));
|
|
|
|
TEST_ASSERT(check_get_value(&ctx_cli, MBEDTLS_DHM_PARAM_G, &ctx_srv.G));
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_dhm_make_public(&ctx_cli, x_size, pub_cli, pub_cli_len,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_dhm_read_public(&ctx_srv, pub_cli, pub_cli_len) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_dhm_calc_secret(&ctx_srv, sec_srv, sizeof(sec_srv),
|
|
|
|
&sec_srv_len,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_dhm_calc_secret(&ctx_cli, sec_cli, sizeof(sec_cli),
|
|
|
|
&sec_cli_len,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(sec_srv_len == sec_cli_len);
|
|
|
|
TEST_ASSERT(sec_srv_len != 0);
|
|
|
|
TEST_ASSERT(memcmp(sec_srv, sec_cli, sec_srv_len) == 0);
|
2013-09-04 16:29:59 +02:00
|
|
|
|
2021-05-27 22:50:53 +02:00
|
|
|
/* Internal value checks */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(check_get_value(&ctx_cli, MBEDTLS_DHM_PARAM_X, &ctx_cli.X));
|
|
|
|
TEST_ASSERT(check_get_value(&ctx_srv, MBEDTLS_DHM_PARAM_X, &ctx_srv.X));
|
2021-05-27 22:50:53 +02:00
|
|
|
/* Cross-checks */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(check_get_value(&ctx_cli, MBEDTLS_DHM_PARAM_GX, &ctx_srv.GY));
|
|
|
|
TEST_ASSERT(check_get_value(&ctx_cli, MBEDTLS_DHM_PARAM_GY, &ctx_srv.GX));
|
|
|
|
TEST_ASSERT(check_get_value(&ctx_cli, MBEDTLS_DHM_PARAM_K, &ctx_srv.K));
|
|
|
|
TEST_ASSERT(check_get_value(&ctx_srv, MBEDTLS_DHM_PARAM_GX, &ctx_cli.GY));
|
|
|
|
TEST_ASSERT(check_get_value(&ctx_srv, MBEDTLS_DHM_PARAM_GY, &ctx_cli.GX));
|
|
|
|
TEST_ASSERT(check_get_value(&ctx_srv, MBEDTLS_DHM_PARAM_K, &ctx_cli.K));
|
2021-05-27 22:50:53 +02:00
|
|
|
|
2013-09-17 11:34:11 +02:00
|
|
|
/* Re-do calc_secret on server a few times to test update of blinding values */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
mbedtls_test_set_step(20 + i);
|
2013-09-17 11:34:11 +02:00
|
|
|
sec_srv_len = 1000;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_dhm_calc_secret(&ctx_srv, sec_srv,
|
|
|
|
sizeof(sec_srv), &sec_srv_len,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(sec_srv_len == sec_cli_len);
|
|
|
|
TEST_ASSERT(sec_srv_len != 0);
|
|
|
|
TEST_ASSERT(memcmp(sec_srv, sec_cli, sec_srv_len) == 0);
|
2013-09-17 11:34:11 +02:00
|
|
|
}
|
2013-09-04 16:29:59 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Second key exchange to test change of blinding values on server
|
|
|
|
*/
|
|
|
|
p = ske;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_set_step(30);
|
|
|
|
TEST_ASSERT(mbedtls_dhm_make_params(&ctx_srv, x_size, ske, &ske_len,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
if (!check_dhm_params(&ctx_srv, x_size, ske, ske_len)) {
|
2021-03-30 23:28:51 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-04 16:29:59 +02:00
|
|
|
ske[ske_len++] = 0;
|
|
|
|
ske[ske_len++] = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_dhm_read_params(&ctx_cli, &p, ske + ske_len) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_dhm_make_public(&ctx_cli, x_size, pub_cli, pub_cli_len,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_dhm_read_public(&ctx_srv, pub_cli, pub_cli_len) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_dhm_calc_secret(&ctx_srv, sec_srv, sizeof(sec_srv),
|
|
|
|
&sec_srv_len,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_dhm_calc_secret(&ctx_cli, sec_cli, sizeof(sec_cli),
|
|
|
|
&sec_cli_len,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(sec_srv_len == sec_cli_len);
|
|
|
|
TEST_ASSERT(sec_srv_len != 0);
|
|
|
|
TEST_ASSERT(memcmp(sec_srv, sec_cli, sec_srv_len) == 0);
|
2011-02-20 17:34:26 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_dhm_free(&ctx_srv);
|
|
|
|
mbedtls_dhm_free(&ctx_cli);
|
2009-07-08 21:47:36 +02:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-09-15 17:43:54 +02:00
|
|
|
|
2020-12-02 11:41:50 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void dhm_make_public(int P_bytes, char *input_G, int result)
|
2020-12-02 11:41:50 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi P, G;
|
|
|
|
mbedtls_dhm_context ctx;
|
|
|
|
unsigned char output[MBEDTLS_MPI_MAX_SIZE];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&P);
|
|
|
|
mbedtls_mpi_init(&G);
|
|
|
|
mbedtls_dhm_init(&ctx);
|
2020-12-02 11:41:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_lset(&P, 1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_shift_l(&P, (P_bytes * 8) - 1) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_set_bit(&P, 0, 1) == 0);
|
2020-12-02 11:41:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&G, input_G) == 0);
|
2020-12-02 11:41:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_dhm_set_group(&ctx, &P, &G) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_dhm_make_public(&ctx, (int) mbedtls_mpi_size(&P),
|
|
|
|
output, sizeof(output),
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
NULL) == result);
|
2020-12-02 11:41:50 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&P);
|
|
|
|
mbedtls_mpi_free(&G);
|
|
|
|
mbedtls_dhm_free(&ctx);
|
2020-12-02 11:41:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void dhm_file(char *filename, char *p, char *g, int len)
|
2014-03-29 16:42:38 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_dhm_context ctx;
|
|
|
|
mbedtls_mpi P, G;
|
2014-03-29 16:42:38 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_dhm_init(&ctx);
|
|
|
|
mbedtls_mpi_init(&P); mbedtls_mpi_init(&G);
|
2014-03-29 16:42:38 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&P, p) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&G, g) == 0);
|
2014-03-29 16:42:38 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_dhm_parse_dhmfile(&ctx, filename) == 0);
|
2014-03-29 16:42:38 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_dhm_get_len(&ctx), (size_t) len);
|
|
|
|
TEST_EQUAL(mbedtls_dhm_get_bitlen(&ctx), mbedtls_mpi_bitlen(&P));
|
|
|
|
TEST_ASSERT(check_get_value(&ctx, MBEDTLS_DHM_PARAM_P, &P));
|
|
|
|
TEST_ASSERT(check_get_value(&ctx, MBEDTLS_DHM_PARAM_G, &G));
|
2014-03-29 16:42:38 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&P); mbedtls_mpi_free(&G);
|
|
|
|
mbedtls_dhm_free(&ctx);
|
2014-03-29 16:42:38 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2023-01-11 14:50:10 +01:00
|
|
|
void dhm_selftest()
|
2013-09-15 17:43:54 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_dhm_self_test(1) == 0);
|
2013-09-15 17:43:54 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|