From 81f4b11010097d578e81df49927a3743c4cfa210 Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Thu, 10 Nov 2022 14:40:38 +0000 Subject: [PATCH] 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 --- library/bignum_mod.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ library/bignum_mod.h | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/library/bignum_mod.c b/library/bignum_mod.c index 13108c51f1..a4ed32b6a3 100644 --- a/library/bignum_mod.c +++ b/library/bignum_mod.c @@ -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 */ diff --git a/library/bignum_mod.h b/library/bignum_mod.h index 29c26f2ef9..9378aabacd 100644 --- a/library/bignum_mod.h +++ b/library/bignum_mod.h @@ -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 */