mbedtls_mpi_mod_write: prevent data corruption

The function wasn't converting back data to internal representation when
writing it out.

Signed-off-by: Janos Follath <janos.follath@arm.com>
diff --git a/library/bignum_mod.c b/library/bignum_mod.c
index 7f7c715..4fe6e48 100644
--- a/library/bignum_mod.c
+++ b/library/bignum_mod.c
@@ -231,6 +231,7 @@
                            mbedtls_mpi_mod_ext_rep ext_rep )
 {
     int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
+    int conv_ret = 0;
 
     /* Do our best to check if r and m have been set up */
     if ( r->limbs == 0 || m->limbs == 0 )
@@ -238,12 +239,23 @@
     if ( r->limbs != m->limbs )
         goto cleanup;
 
-    if ( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY)
-        ret = mbedtls_mpi_mod_raw_from_mont_rep( r->p, m );
+    if ( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
+    {
+        conv_ret = mbedtls_mpi_mod_raw_from_mont_rep( r->p, m );
+        if( conv_ret != 0 )
+            goto cleanup;
+    }
 
     ret = mbedtls_mpi_mod_raw_write( r->p, m, buf, buflen, ext_rep );
 
+    if ( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
+        conv_ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
+
 cleanup:
+
+    if ( ret == 0 )
+        ret = conv_ret;
+
     return ( ret );
 }
 /* END MERGE SLOT 7 */