bignum_mod: Added mbedtls_mpi_mod_read/write() IO functions

This patch adds input and ouput fucntions in the `bignum_mod` layer.
The data will be automatically converted between Cannonical and
Montgomery representation if required.

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
This commit is contained in:
Minos Galanakis 2022-11-10 14:40:38 +00:00 committed by Janos Follath
parent 590ae5363d
commit 81f4b11010
2 changed files with 91 additions and 0 deletions

View file

@ -209,7 +209,54 @@ exit:
/* END MERGE SLOT 6 */
/* BEGIN MERGE SLOT 7 */
int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
mbedtls_mpi_mod_modulus *m,
unsigned char *buf,
size_t buflen )
{
int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
if ( r == NULL || m == NULL )
goto cleanup;
if ( r->p == NULL || m->p == NULL || r->limbs > m->limbs ||\
r->limbs == 0 || m->limbs == 0 )
goto cleanup;
ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen );
if( ret != 0 )
goto cleanup;
if (m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY)
ret = mbedtls_mpi_mod_raw_to_mont_rep(r->p, m);
cleanup:
return ( ret );
}
int mbedtls_mpi_mod_write( mbedtls_mpi_mod_residue *r,
mbedtls_mpi_mod_modulus *m,
unsigned char *buf,
size_t buflen )
{
int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
if ( r == NULL || m == NULL )
goto cleanup;
if ( r->p == NULL || m->p == NULL || r->limbs > m->limbs ||\
r->limbs == 0 || m->limbs == 0 )
goto cleanup;
if ( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY)
ret = mbedtls_mpi_mod_raw_from_mont_rep( r->p, m );
ret = mbedtls_mpi_mod_raw_write( r->p, m, buf, buflen );
cleanup:
return ( ret );
}
/* END MERGE SLOT 7 */
/* BEGIN MERGE SLOT 8 */

View file

@ -173,7 +173,51 @@ void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m );
/* END MERGE SLOT 6 */
/* BEGIN MERGE SLOT 7 */
/** Read public representation data stored in a buffer into a residue structure.
*
* The `mbedtls_mpi_mod_residue` and `mbedtls_mpi_mod_modulus` structures must
* be compatible. The data will be automatically converted into the appropriate
* representation based on the value of `m->int_rep field`.
*
* \param r The address of the residue related to \p m. It must have as
* many limbs as the modulus \p m.
* \param m The address of the modulus.
* \param buf The input buffer to import from.
* \param buflen The length in bytes of \p buf.
*
* \return \c 0 if successful.
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
* large enough to hold the value in \p buf.
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
* of \p m is invalid or \p X is not less than \p m.
*/
int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
mbedtls_mpi_mod_modulus *m,
unsigned char *buf,
size_t buflen );
/** Write residue data onto a buffer using public representation data.
*
* The `mbedtls_mpi_mod_residue` and `mbedtls_mpi_mod_modulus` structures must
* be compatible. The data will be automatically converted into the appropriate
* representation based on the value of `m->int_rep field`.
*
* \param r The address of the residue related to \p m. It must have as
* many limbs as the modulus \p m.
* \param m The address of the modulus.
* \param buf The output buffer to export to.
* \param buflen The length in bytes of \p buf.
*
* \return \c 0 if successful.
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
* large enough to hold the value of \p X.
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
* of \p m is invalid.
*/
int mbedtls_mpi_mod_write( mbedtls_mpi_mod_residue *r,
mbedtls_mpi_mod_modulus *m,
unsigned char *buf,
size_t buflen );
/* END MERGE SLOT 7 */
/* BEGIN MERGE SLOT 8 */