diff --git a/include/polarssl/ecp.h b/include/polarssl/ecp.h index 0919cbe1c..373de8d5a 100644 --- a/include/polarssl/ecp.h +++ b/include/polarssl/ecp.h @@ -255,7 +255,7 @@ int ecp_group_read_string( ecp_group *grp, int radix, * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL */ int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P, - int format, uint8_t *olen, + int format, size_t *olen, unsigned char *buf, size_t buflen ); /** @@ -348,7 +348,8 @@ int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt, * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL */ int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt, - int format, unsigned char *buf, size_t buf_len ); + int format, size_t *olen, + unsigned char *buf, size_t blen ); /** * \brief Addition: R = P + Q diff --git a/library/ecp.c b/library/ecp.c index 41fc9dbac..233c2eba4 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -186,7 +186,7 @@ cleanup: * Export a point into unsigned binary data (SEC1 2.3.3) */ int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P, - int format, uint8_t *olen, + int format, size_t *olen, unsigned char *buf, size_t buflen ) { int ret; @@ -293,15 +293,28 @@ int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt, * } ECPoint; */ int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt, - int format, unsigned char *buf, size_t buf_len ) + int format, size_t *olen, + unsigned char *buf, size_t blen ) { + int ret; + /* - * buf_len must be at least one, for our length byte + * buffer length must be at least one, for our length byte */ - if( buf_len < 1 ) + if( blen < 1 ) return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); - return ecp_point_write_binary( grp, pt, format, buf, buf + 1, buf_len - 1); + if( ( ret = ecp_point_write_binary( grp, pt, format, + olen, buf + 1, blen - 1) ) != 0 ) + return( ret ); + + /* + * write length to the first byte and update total length + */ + buf[0] = *olen; + ++*olen; + + return 0; } /* diff --git a/tests/suites/test_suite_ecp.function b/tests/suites/test_suite_ecp.function index d07531c97..bf6104e57 100644 --- a/tests/suites/test_suite_ecp.function +++ b/tests/suites/test_suite_ecp.function @@ -230,7 +230,7 @@ ecp_write_binary:id:x:y:z:format:out:blen:ret ecp_group grp; ecp_point P; unsigned char buf[256], str[512]; - uint8_t olen; + size_t olen; memset( buf, 0, sizeof( buf ) ); memset( str, 0, sizeof( str ) ); @@ -299,6 +299,7 @@ ecp_tls_write_read_point:id ecp_group grp; ecp_point pt; unsigned char buf[256]; + size_t olen; ecp_group_init( &grp ); ecp_point_init( &pt ); @@ -307,27 +308,27 @@ ecp_tls_write_read_point:id TEST_ASSERT( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_{id} ) == 0 ); TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G, - POLARSSL_ECP_PF_COMPRESSED, buf, 256 ) == 0 ); - TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, 256 ) + POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 ); + TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, olen ) == POLARSSL_ERR_ECP_BAD_INPUT_DATA ); TEST_ASSERT( ecp_tls_write_point( &grp, &grp.G, - POLARSSL_ECP_PF_UNCOMPRESSED, buf, 256 ) == 0 ); - TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, 256 ) == 0 ); + POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 ); + TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, olen ) == 0 ); TEST_ASSERT( mpi_cmp_mpi( &grp.G.X, &pt.X ) == 0 ); TEST_ASSERT( mpi_cmp_mpi( &grp.G.Y, &pt.Y ) == 0 ); TEST_ASSERT( mpi_cmp_mpi( &grp.G.Z, &pt.Z ) == 0 ); TEST_ASSERT( ecp_set_zero( &pt ) == 0 ); TEST_ASSERT( ecp_tls_write_point( &grp, &pt, - POLARSSL_ECP_PF_COMPRESSED, buf, 256 ) == 0 ); - TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, 256 ) == 0 ); + POLARSSL_ECP_PF_COMPRESSED, &olen, buf, 256 ) == 0 ); + TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, olen ) == 0 ); TEST_ASSERT( ecp_is_zero( &pt ) ); TEST_ASSERT( ecp_set_zero( &pt ) == 0 ); TEST_ASSERT( ecp_tls_write_point( &grp, &pt, - POLARSSL_ECP_PF_UNCOMPRESSED, buf, 256 ) == 0 ); - TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, 256 ) == 0 ); + POLARSSL_ECP_PF_UNCOMPRESSED, &olen, buf, 256 ) == 0 ); + TEST_ASSERT( ecp_tls_read_point( &grp, &pt, buf, olen ) == 0 ); TEST_ASSERT( ecp_is_zero( &pt ) ); ecp_group_free( &grp );