From c614219a87b8f587774be781816be8785dfddfe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesco=20Pomp=C3=B2?= Date: Mon, 27 Jun 2016 21:04:21 +0200 Subject: [PATCH 1/2] added mbedtls_ecp_decompress_pubkey function --- library/ecp.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/library/ecp.c b/library/ecp.c index f51f2251edf8..5e96722bad48 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -1958,6 +1958,64 @@ int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ec return( ret ); } +/* + * Decompresses an EC Public Key + */ +int mbedtls_ecp_decompress_pubkey( const mbedtls_ecp_group *grp, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize ){ + + int ret; + mbedtls_mpi x, x3, ax, z, zexp, y; + size_t plen; + + plen = mbedtls_mpi_size( &grp->P ); + *olen = 2 * plen + 1; + + if( ilen != plen + 1 || input[0] == 0x04 ) + return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); + + if( osize < 1 ) + return( 0 ); + + if( osize < *olen ) + return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); + + mbedtls_mpi_init( &x ); + mbedtls_mpi_init( &x3 ); + mbedtls_mpi_init( &ax ); + mbedtls_mpi_init( &z ); + mbedtls_mpi_init( &zexp ); + mbedtls_mpi_init( &y ); + + + MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &x, input+1, ilen-1 ) ); // X point of the pubkey + MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &x3, &x, &x ) ); // X^2 + MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &x3, &x3, &x ) ); // X^3 + MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ax, &grp->A, &x ) ); // AX + MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &z, &x3, &ax ) ); // X^3 + AX + MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &z, &z, &grp->B ) ); // X^3 + AX + B + MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &zexp, &grp->P) ); // Z exponent + MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &zexp, &zexp, 1 ) ); // Z exponent + 1 + MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( &zexp, 0, &zexp, 4 ) ); // Z exponent / 4 + MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &y, &z, &zexp, &grp->P, 0 ) ); // Z^Zexp % P + MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( &y, 0, input[0] == 0x03 ? 1 : 0 ) ); // Setting the correct sign + + output[0] = 0x04; // Uncompressed format + memcpy( output+1, input+1, ilen-1 ); //0x04 + X + MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &y, (output+1+ilen-1), plen ) ); // 0x04 + X + Y + + cleanup: + mbedtls_mpi_free( &x ); + mbedtls_mpi_free( &x3 ); + mbedtls_mpi_free( &ax ); + mbedtls_mpi_free( &z ); + mbedtls_mpi_free( &zexp ); + mbedtls_mpi_free( &y ); + + + return( ret ); +} + + #if defined(MBEDTLS_SELF_TEST) /* From 77cad20a30c54ec3b69e964027629ff48bbafd4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francesco=20Pomp=C3=B2?= Date: Mon, 27 Jun 2016 21:12:58 +0200 Subject: [PATCH 2/2] Added mbedtls_ecp_decompress_pubkey declaration --- include/mbedtls/ecp.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/mbedtls/ecp.h b/include/mbedtls/ecp.h index 5246c789d31e..ccd9f39d98ab 100644 --- a/include/mbedtls/ecp.h +++ b/include/mbedtls/ecp.h @@ -653,6 +653,21 @@ int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, */ int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv ); +/** + * \brief Decompresses an EC Public Keypair + * + * \param grp ECP group + * \param input Compressed EC Public Key (Created using mbedtls_ecp_point_write_binary with format = MBEDTLS_ECP_PF_COMPRESSED) + * \param ilen Input length + * \param output Uncompressed EC Public Key + * \param olen Uncompressed EC Public Key output length + * \param osize Size of the output buffer + * + * \return 0 on success, or a specific error code. + */ +int mbedtls_ecp_decompress_pubkey( const mbedtls_ecp_group *grp, const unsigned char *input, size_t ilen, + unsigned char *output, size_t *olen, size_t osize ); + #if defined(MBEDTLS_SELF_TEST) /** * \brief Checkup routine