2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ecdsa.h"
|
2022-09-06 13:08:28 +02:00
|
|
|
#include "hash_info.h"
|
2022-09-15 11:29:35 +02:00
|
|
|
#include "mbedtls/legacy_or_psa.h"
|
2023-01-11 14:50:10 +01:00
|
|
|
#if (defined(MBEDTLS_ECDSA_DETERMINISTIC) && defined(MBEDTLS_SHA256_C)) || \
|
|
|
|
(!defined(MBEDTLS_ECDSA_DETERMINISTIC) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_LOWLEVEL_OR_PSA))
|
2022-08-08 09:49:10 +02:00
|
|
|
#define MBEDTLS_HAS_ALG_SHA_256_VIA_MD_IF_DETERMINISTIC
|
|
|
|
#endif
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2013-01-26 19:09:07 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_ECDSA_C
|
2013-08-20 11:48:36 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2013-01-26 19:09:07 +01:00
|
|
|
|
2021-04-07 19:16:18 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdsa_prim_zero(int id)
|
2021-04-07 19:16:18 +02:00
|
|
|
{
|
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point Q;
|
|
|
|
mbedtls_mpi d, r, s;
|
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2022-09-06 13:08:28 +02:00
|
|
|
unsigned char buf[MBEDTLS_HASH_MAX_SIZE];
|
2021-04-07 19:16:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_init(&grp);
|
|
|
|
mbedtls_ecp_point_init(&Q);
|
|
|
|
mbedtls_mpi_init(&d); mbedtls_mpi_init(&r); mbedtls_mpi_init(&s);
|
|
|
|
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2021-04-07 19:16:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecp_gen_keypair(&grp, &d, &Q,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
2021-04-07 19:16:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_sign(&grp, &r, &s, &d, buf, sizeof(buf),
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdsa_verify(&grp, buf, sizeof(buf), &Q, &r, &s) == 0);
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_free(&grp);
|
|
|
|
mbedtls_ecp_point_free(&Q);
|
|
|
|
mbedtls_mpi_free(&d); mbedtls_mpi_free(&r); mbedtls_mpi_free(&s);
|
2021-04-07 19:16:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdsa_prim_random(int id)
|
2013-01-26 19:09:07 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point Q;
|
|
|
|
mbedtls_mpi d, r, s;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2022-09-06 13:08:28 +02:00
|
|
|
unsigned char buf[MBEDTLS_HASH_MAX_SIZE];
|
2013-01-26 19:09:07 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_init(&grp);
|
|
|
|
mbedtls_ecp_point_init(&Q);
|
|
|
|
mbedtls_mpi_init(&d); mbedtls_mpi_init(&r); mbedtls_mpi_init(&s);
|
|
|
|
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2013-01-26 19:09:07 +01:00
|
|
|
|
2021-04-07 19:16:18 +02:00
|
|
|
/* prepare material for signature */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_rnd_pseudo_rand(&rnd_info,
|
|
|
|
buf, sizeof(buf)) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecp_gen_keypair(&grp, &d, &Q,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_ecdsa_sign(&grp, &r, &s, &d, buf, sizeof(buf),
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdsa_verify(&grp, buf, sizeof(buf), &Q, &r, &s) == 0);
|
2013-01-26 19:09:07 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_free(&grp);
|
|
|
|
mbedtls_ecp_point_free(&Q);
|
|
|
|
mbedtls_mpi_free(&d); mbedtls_mpi_free(&r); mbedtls_mpi_free(&s);
|
2013-01-26 19:09:07 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-01-27 08:10:28 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdsa_prim_test_vectors(int id, char *d_str, char *xQ_str,
|
|
|
|
char *yQ_str, data_t *rnd_buf,
|
|
|
|
data_t *hash, char *r_str, char *s_str,
|
|
|
|
int result)
|
2013-01-27 08:10:28 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point Q;
|
2022-04-21 09:25:23 +02:00
|
|
|
mbedtls_mpi d, r, s, r_check, s_check, zero;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_buf_info rnd_info;
|
2013-01-27 08:10:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_init(&grp);
|
|
|
|
mbedtls_ecp_point_init(&Q);
|
|
|
|
mbedtls_mpi_init(&d); mbedtls_mpi_init(&r); mbedtls_mpi_init(&s);
|
|
|
|
mbedtls_mpi_init(&r_check); mbedtls_mpi_init(&s_check);
|
|
|
|
mbedtls_mpi_init(&zero);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecp_point_read_string(&Q, 16, xQ_str, yQ_str) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&d, d_str) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&r_check, r_str) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&s_check, s_str) == 0);
|
2021-03-24 00:48:57 +01:00
|
|
|
rnd_info.fallback_f_rng = mbedtls_test_rnd_std_rand;
|
|
|
|
rnd_info.fallback_p_rng = NULL;
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_info.buf = rnd_buf->x;
|
|
|
|
rnd_info.length = rnd_buf->len;
|
2014-01-06 11:00:07 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
/* Fix rnd_buf->x by shifting it left if necessary */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (grp.nbits % 8 != 0) {
|
|
|
|
unsigned char shift = 8 - (grp.nbits % 8);
|
2014-01-06 11:00:07 +01:00
|
|
|
size_t i;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < rnd_info.length - 1; i++) {
|
|
|
|
rnd_buf->x[i] = rnd_buf->x[i] << shift | rnd_buf->x[i+1] >> (8 - shift);
|
|
|
|
}
|
2014-01-06 11:00:07 +01:00
|
|
|
|
2017-06-09 05:32:58 +02:00
|
|
|
rnd_buf->x[rnd_info.length-1] <<= shift;
|
2014-01-06 11:00:07 +01:00
|
|
|
}
|
2013-01-27 08:10:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_sign(&grp, &r, &s, &d, hash->x, hash->len,
|
|
|
|
mbedtls_test_rnd_buffer_rand, &rnd_info) == result);
|
2013-01-27 08:10:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (result == 0) {
|
2022-04-21 09:25:23 +02:00
|
|
|
/* Check we generated the expected values */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_mpi(&r, &r_check), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_cmp_mpi(&s, &s_check), 0);
|
2022-04-20 10:34:22 +02:00
|
|
|
|
|
|
|
/* Valid signature */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len,
|
|
|
|
&Q, &r_check, &s_check), 0);
|
2022-04-20 10:34:22 +02:00
|
|
|
|
|
|
|
/* Invalid signature: wrong public key (G instead of Q) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len,
|
|
|
|
&grp.G, &r_check, &s_check), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2022-04-20 10:34:22 +02:00
|
|
|
|
|
|
|
/* Invalid signatures: r or s or both one off */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_sub_int(&r, &r_check, 1), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_add_int(&s, &s_check, 1), 0);
|
2022-04-20 10:34:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&r, &s_check), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&r_check, &s), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&r, &s), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2022-04-20 10:34:22 +02:00
|
|
|
|
|
|
|
/* Invalid signatures: r, s or both (CVE-2022-21449) are zero */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&zero, 0), 0);
|
2022-04-20 10:34:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&zero, &s_check), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&r_check, &zero), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&zero, &zero), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2022-04-20 10:34:22 +02:00
|
|
|
|
2022-04-21 09:25:23 +02:00
|
|
|
/* Invalid signatures: r, s or both are == N */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&grp.N, &s_check), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&r_check, &grp.N), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&grp.N, &grp.N), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2022-04-20 10:34:22 +02:00
|
|
|
|
2022-04-21 09:25:23 +02:00
|
|
|
/* Invalid signatures: r, s or both are negative */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_sub_mpi(&r, &r_check, &grp.N), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_sub_mpi(&s, &s_check, &grp.N), 0);
|
2022-04-20 10:34:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&r, &s_check), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&r_check, &s), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&r, &s), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2022-04-20 10:34:22 +02:00
|
|
|
|
|
|
|
/* Invalid signatures: r or s or both are > N */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_add_mpi(&r, &r_check, &grp.N), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_add_mpi(&s, &s_check, &grp.N), 0);
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&r, &s_check), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&r_check, &s), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
|
|
|
TEST_EQUAL(mbedtls_ecdsa_verify(&grp, hash->x, hash->len, &Q,
|
|
|
|
&r, &s), MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2018-06-13 09:53:21 +02:00
|
|
|
}
|
2017-04-21 10:04:02 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_free(&grp);
|
|
|
|
mbedtls_ecp_point_free(&Q);
|
|
|
|
mbedtls_mpi_free(&d); mbedtls_mpi_free(&r); mbedtls_mpi_free(&s);
|
|
|
|
mbedtls_mpi_free(&r_check); mbedtls_mpi_free(&s_check);
|
|
|
|
mbedtls_mpi_free(&zero);
|
2013-01-27 08:10:28 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2013-08-08 13:30:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECDSA_DETERMINISTIC */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdsa_det_test_vectors(int id, char *d_str, int md_alg, data_t *hash,
|
|
|
|
char *r_str, char *s_str)
|
2014-01-06 14:25:56 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_mpi d, r, s, r_check, s_check;
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_init(&grp);
|
|
|
|
mbedtls_mpi_init(&d); mbedtls_mpi_init(&r); mbedtls_mpi_init(&s);
|
|
|
|
mbedtls_mpi_init(&r_check); mbedtls_mpi_init(&s_check);
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&grp, id) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&d, d_str) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&r_check, r_str) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&s_check, s_str) == 0);
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2019-01-04 16:51:24 +01:00
|
|
|
TEST_ASSERT(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_sign_det_ext(&grp, &r, &s, &d,
|
|
|
|
hash->x, hash->len, md_alg,
|
|
|
|
mbedtls_test_rnd_std_rand,
|
|
|
|
NULL)
|
|
|
|
== 0);
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&r, &r_check) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&s, &s_check) == 0);
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_free(&grp);
|
|
|
|
mbedtls_mpi_free(&d); mbedtls_mpi_free(&r); mbedtls_mpi_free(&s);
|
|
|
|
mbedtls_mpi_free(&r_check); mbedtls_mpi_free(&s_check);
|
2014-01-06 14:25:56 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-08-08 09:49:10 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_IF_DETERMINISTIC */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdsa_write_read_zero(int id)
|
2013-08-08 13:30:57 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecdsa_context ctx;
|
2020-06-10 12:12:18 +02:00
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
2015-03-31 11:41:42 +02:00
|
|
|
unsigned char hash[32];
|
2013-08-08 13:30:57 +02:00
|
|
|
unsigned char sig[200];
|
|
|
|
size_t sig_len, i;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_init(&ctx);
|
|
|
|
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
|
|
|
|
memset(hash, 0, sizeof(hash));
|
|
|
|
memset(sig, 0x2a, sizeof(sig));
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
/* generate signing key */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_genkey(&ctx, id,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
/* generate and write signature, then read and verify it */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_write_signature(&ctx, MBEDTLS_MD_SHA256,
|
|
|
|
hash, sizeof(hash),
|
|
|
|
sig, sizeof(sig), &sig_len,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len) == 0);
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
/* check we didn't write past the announced length */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = sig_len; i < sizeof(sig); i++) {
|
|
|
|
TEST_ASSERT(sig[i] == 0x2a);
|
|
|
|
}
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
/* try verification with invalid length */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len - 1) != 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len + 1) != 0);
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
/* try invalid sequence tag */
|
|
|
|
sig[0]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len) != 0);
|
2021-04-07 19:16:18 +02:00
|
|
|
sig[0]--;
|
|
|
|
|
|
|
|
/* try modifying r */
|
|
|
|
sig[10]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len) == MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2021-04-07 19:16:18 +02:00
|
|
|
sig[10]--;
|
|
|
|
|
|
|
|
/* try modifying s */
|
|
|
|
sig[sig_len - 1]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len) == MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2021-04-07 19:16:18 +02:00
|
|
|
sig[sig_len - 1]--;
|
2021-04-06 19:55:17 +02:00
|
|
|
|
2021-04-07 19:16:18 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_free(&ctx);
|
2021-04-07 19:16:18 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-08-08 09:49:10 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_IF_DETERMINISTIC */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdsa_write_read_random(int id)
|
2021-04-07 19:16:18 +02:00
|
|
|
{
|
|
|
|
mbedtls_ecdsa_context ctx;
|
|
|
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
|
|
|
unsigned char hash[32];
|
|
|
|
unsigned char sig[200];
|
|
|
|
size_t sig_len, i;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_init(&ctx);
|
|
|
|
memset(&rnd_info, 0x00, sizeof(mbedtls_test_rnd_pseudo_info));
|
|
|
|
memset(hash, 0, sizeof(hash));
|
|
|
|
memset(sig, 0x2a, sizeof(sig));
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
/* prepare material for signature */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_rnd_pseudo_rand(&rnd_info,
|
|
|
|
hash, sizeof(hash)) == 0);
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
/* generate signing key */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_genkey(&ctx, id,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
/* generate and write signature, then read and verify it */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_write_signature(&ctx, MBEDTLS_MD_SHA256,
|
|
|
|
hash, sizeof(hash),
|
|
|
|
sig, sizeof(sig), &sig_len,
|
|
|
|
&mbedtls_test_rnd_pseudo_rand,
|
|
|
|
&rnd_info) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len) == 0);
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
/* check we didn't write past the announced length */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = sig_len; i < sizeof(sig); i++) {
|
|
|
|
TEST_ASSERT(sig[i] == 0x2a);
|
|
|
|
}
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
/* try verification with invalid length */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len - 1) != 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len + 1) != 0);
|
2021-04-07 19:16:18 +02:00
|
|
|
|
|
|
|
/* try invalid sequence tag */
|
|
|
|
sig[0]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len) != 0);
|
2021-04-07 19:16:18 +02:00
|
|
|
sig[0]--;
|
|
|
|
|
|
|
|
/* try modifying r */
|
|
|
|
sig[10]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len) == MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2021-04-07 19:16:18 +02:00
|
|
|
sig[10]--;
|
|
|
|
|
|
|
|
/* try modifying s */
|
|
|
|
sig[sig_len - 1]++;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecdsa_read_signature(&ctx, hash, sizeof(hash),
|
|
|
|
sig, sig_len) == MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2021-04-07 19:16:18 +02:00
|
|
|
sig[sig_len - 1]--;
|
2013-08-08 13:30:57 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_free(&ctx);
|
2013-08-08 13:30:57 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2014-01-06 10:27:16 +01:00
|
|
|
|
2017-04-21 11:04:47 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdsa_read_restart(int id, data_t *pk, data_t *hash, data_t *sig,
|
|
|
|
int max_ops, int min_restart, int max_restart)
|
2017-04-21 11:04:47 +02:00
|
|
|
{
|
|
|
|
mbedtls_ecdsa_context ctx;
|
|
|
|
mbedtls_ecdsa_restart_ctx rs_ctx;
|
|
|
|
int ret, cnt_restart;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_init(&ctx);
|
|
|
|
mbedtls_ecdsa_restart_init(&rs_ctx);
|
2017-04-21 11:04:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&ctx.grp, id) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ecp_point_read_binary(&ctx.grp, &ctx.Q,
|
|
|
|
pk->x, pk->len) == 0);
|
2017-04-21 11:04:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_set_max_ops(max_ops);
|
2017-04-21 11:04:47 +02:00
|
|
|
|
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ecdsa_read_signature_restartable(&ctx,
|
|
|
|
hash->x, hash->len, sig->x, sig->len,
|
|
|
|
&rs_ctx);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
|
2017-04-21 11:04:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(ret == 0);
|
|
|
|
TEST_ASSERT(cnt_restart >= min_restart);
|
|
|
|
TEST_ASSERT(cnt_restart <= max_restart);
|
2017-04-21 11:04:47 +02:00
|
|
|
|
|
|
|
/* try modifying r */
|
2020-06-25 09:03:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(sig->len > 10);
|
2020-06-25 09:03:34 +02:00
|
|
|
sig->x[10]++;
|
2017-04-21 11:04:47 +02:00
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ecdsa_read_signature_restartable(&ctx,
|
|
|
|
hash->x, hash->len, sig->x, sig->len,
|
|
|
|
&rs_ctx);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
|
|
|
|
TEST_ASSERT(ret == MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2020-06-25 09:03:34 +02:00
|
|
|
sig->x[10]--;
|
2017-04-21 11:04:47 +02:00
|
|
|
|
|
|
|
/* try modifying s */
|
2020-06-25 09:03:34 +02:00
|
|
|
sig->x[sig->len - 1]++;
|
2017-04-21 11:04:47 +02:00
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ecdsa_read_signature_restartable(&ctx,
|
|
|
|
hash->x, hash->len, sig->x, sig->len,
|
|
|
|
&rs_ctx);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
|
|
|
|
TEST_ASSERT(ret == MBEDTLS_ERR_ECP_VERIFY_FAILED);
|
2020-06-25 09:03:34 +02:00
|
|
|
sig->x[sig->len - 1]--;
|
2017-04-21 11:04:47 +02:00
|
|
|
|
2017-08-28 12:20:39 +02:00
|
|
|
/* Do we leak memory when aborting an operation?
|
|
|
|
* This test only makes sense when we actually restart */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (min_restart > 0) {
|
|
|
|
ret = mbedtls_ecdsa_read_signature_restartable(&ctx,
|
|
|
|
hash->x, hash->len, sig->x, sig->len,
|
|
|
|
&rs_ctx);
|
|
|
|
TEST_ASSERT(ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
|
2017-08-28 12:20:39 +02:00
|
|
|
}
|
2017-04-21 11:04:47 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_free(&ctx);
|
|
|
|
mbedtls_ecdsa_restart_free(&rs_ctx);
|
2017-04-21 11:04:47 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2017-04-25 10:57:30 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_DETERMINISTIC */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdsa_write_restart(int id, char *d_str, int md_alg,
|
|
|
|
data_t *hash, data_t *sig_check,
|
|
|
|
int max_ops, int min_restart, int max_restart)
|
2017-04-25 10:57:30 +02:00
|
|
|
{
|
|
|
|
int ret, cnt_restart;
|
|
|
|
mbedtls_ecdsa_restart_ctx rs_ctx;
|
|
|
|
mbedtls_ecdsa_context ctx;
|
|
|
|
unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
|
2022-07-19 16:54:28 +02:00
|
|
|
size_t slen;
|
2017-04-25 10:57:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_restart_init(&rs_ctx);
|
|
|
|
mbedtls_ecdsa_init(&ctx);
|
|
|
|
memset(sig, 0, sizeof(sig));
|
2017-04-25 10:57:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ecp_group_load(&ctx.grp, id) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_test_read_mpi(&ctx.d, d_str) == 0);
|
2017-04-25 10:57:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_set_max_ops(max_ops);
|
2017-04-25 10:57:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
slen = sizeof(sig);
|
2017-04-25 10:57:30 +02:00
|
|
|
cnt_restart = 0;
|
|
|
|
do {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ecdsa_write_signature_restartable(&ctx,
|
|
|
|
md_alg,
|
|
|
|
hash->x,
|
|
|
|
hash->len,
|
|
|
|
sig,
|
|
|
|
sizeof(sig),
|
|
|
|
&slen,
|
|
|
|
mbedtls_test_rnd_std_rand,
|
|
|
|
NULL,
|
|
|
|
&rs_ctx);
|
|
|
|
} while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
|
|
|
|
|
|
|
|
TEST_ASSERT(ret == 0);
|
|
|
|
TEST_ASSERT(slen == sig_check->len);
|
|
|
|
TEST_ASSERT(memcmp(sig, sig_check->x, slen) == 0);
|
|
|
|
|
|
|
|
TEST_ASSERT(cnt_restart >= min_restart);
|
|
|
|
TEST_ASSERT(cnt_restart <= max_restart);
|
2017-04-25 10:57:30 +02:00
|
|
|
|
2017-08-28 12:20:39 +02:00
|
|
|
/* Do we leak memory when aborting an operation?
|
|
|
|
* This test only makes sense when we actually restart */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (min_restart > 0) {
|
|
|
|
ret = mbedtls_ecdsa_write_signature_restartable(&ctx,
|
|
|
|
md_alg,
|
|
|
|
hash->x,
|
|
|
|
hash->len,
|
|
|
|
sig,
|
|
|
|
sizeof(sig),
|
|
|
|
&slen,
|
|
|
|
mbedtls_test_rnd_std_rand,
|
|
|
|
NULL,
|
|
|
|
&rs_ctx);
|
|
|
|
TEST_ASSERT(ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
|
2017-08-28 12:20:39 +02:00
|
|
|
}
|
2017-04-25 10:57:30 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_restart_free(&rs_ctx);
|
|
|
|
mbedtls_ecdsa_free(&ctx);
|
2017-04-25 10:57:30 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-08-10 12:32:07 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ecdsa_verify(int grp_id, char *x, char *y, char *r, char *s, data_t *content, int expected)
|
2022-08-10 12:32:07 +02:00
|
|
|
{
|
|
|
|
mbedtls_ecdsa_context ctx;
|
|
|
|
mbedtls_mpi sig_r, sig_s;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_init(&ctx);
|
|
|
|
mbedtls_mpi_init(&sig_r);
|
|
|
|
mbedtls_mpi_init(&sig_s);
|
2022-08-10 12:32:07 +02:00
|
|
|
|
|
|
|
/* Prepare ECP group context */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_ecp_group_load(&ctx.grp, grp_id), 0);
|
2022-08-10 12:32:07 +02:00
|
|
|
|
|
|
|
/* Prepare public key */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi(&ctx.Q.X, x), 0);
|
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi(&ctx.Q.Y, y), 0);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_lset(&ctx.Q.Z, 1), 0);
|
2022-08-10 12:32:07 +02:00
|
|
|
|
|
|
|
/* Prepare signature R & S */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi(&sig_r, r), 0);
|
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi(&sig_s, s), 0);
|
2022-08-10 12:32:07 +02:00
|
|
|
|
|
|
|
/* Test whether public key has expected validity */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_ecp_check_pubkey(&ctx.grp, &ctx.Q),
|
|
|
|
expected == MBEDTLS_ERR_ECP_INVALID_KEY ? MBEDTLS_ERR_ECP_INVALID_KEY : 0);
|
2022-08-10 12:32:07 +02:00
|
|
|
|
|
|
|
/* Verification */
|
2023-01-11 14:50:10 +01:00
|
|
|
int result = mbedtls_ecdsa_verify(&ctx.grp, content->x, content->len, &ctx.Q, &sig_r, &sig_s);
|
2022-08-10 12:32:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(result, expected);
|
2022-08-10 12:32:07 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdsa_free(&ctx);
|
|
|
|
mbedtls_mpi_free(&sig_r);
|
|
|
|
mbedtls_mpi_free(&sig_s);
|
2022-08-10 12:32:07 +02:00
|
|
|
}
|
2022-08-10 13:21:23 +02:00
|
|
|
/* END_CASE */
|