mirror of
https://github.com/ARMmbed/mbedtls.git
synced 2025-05-11 01:11:42 +08:00
Bignum: clean up use of enums
- Made use of enums in struct and function declaration - All enums are handled by switch case now - If the switch does nothing on default, omit the default case to make compiler warnings more powerful - The two enums are now disjoint and the value 1 is skipped to make mistakes easier to detect Signed-off-by: Janos Follath <janos.follath@arm.com>
This commit is contained in:
parent
9dfb5621ff
commit
296ea66442
@ -88,10 +88,12 @@ void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
|
|||||||
switch( m->int_rep )
|
switch( m->int_rep )
|
||||||
{
|
{
|
||||||
case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
|
case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
|
||||||
mbedtls_free( m->rep.mont ); break;
|
mbedtls_free( m->rep.mont );
|
||||||
|
break;
|
||||||
case MBEDTLS_MPI_MOD_REP_OPT_RED:
|
case MBEDTLS_MPI_MOD_REP_OPT_RED:
|
||||||
mbedtls_free( m->rep.ored ); break;
|
mbedtls_free( m->rep.ored );
|
||||||
default:
|
break;
|
||||||
|
case MBEDTLS_MPI_MOD_REP_INVALID:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,8 +107,8 @@ void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
|
|||||||
int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
|
int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
|
||||||
mbedtls_mpi_uint *p,
|
mbedtls_mpi_uint *p,
|
||||||
size_t pn,
|
size_t pn,
|
||||||
int ext_rep,
|
mbedtls_mpi_mod_ext_rep ext_rep,
|
||||||
int int_rep )
|
mbedtls_mpi_mod_rep_selector int_rep )
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
@ -121,7 +123,8 @@ int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
|
|||||||
{
|
{
|
||||||
case MBEDTLS_MPI_MOD_EXT_REP_LE:
|
case MBEDTLS_MPI_MOD_EXT_REP_LE:
|
||||||
case MBEDTLS_MPI_MOD_EXT_REP_BE:
|
case MBEDTLS_MPI_MOD_EXT_REP_BE:
|
||||||
m->ext_rep = ext_rep; break;
|
m->ext_rep = ext_rep;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
||||||
goto exit;
|
goto exit;
|
||||||
@ -131,10 +134,12 @@ int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
|
|||||||
{
|
{
|
||||||
case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
|
case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
|
||||||
m->int_rep = int_rep;
|
m->int_rep = int_rep;
|
||||||
m->rep.mont = NULL; break;
|
m->rep.mont = NULL;
|
||||||
|
break;
|
||||||
case MBEDTLS_MPI_MOD_REP_OPT_RED:
|
case MBEDTLS_MPI_MOD_REP_OPT_RED:
|
||||||
m->int_rep = int_rep;
|
m->int_rep = int_rep;
|
||||||
m->rep.ored = NULL; break;
|
m->rep.ored = NULL;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
||||||
goto exit;
|
goto exit;
|
||||||
|
@ -26,6 +26,22 @@
|
|||||||
#include "mbedtls/bignum.h"
|
#include "mbedtls/bignum.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Skip 1 as it is slightly easier to accidentally pass to functions. */
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
MBEDTLS_MPI_MOD_REP_INVALID = 0,
|
||||||
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY = 2,
|
||||||
|
MBEDTLS_MPI_MOD_REP_OPT_RED
|
||||||
|
} mbedtls_mpi_mod_rep_selector;
|
||||||
|
|
||||||
|
/* Make mbedtls_mpi_mod_rep_selector and mbedtls_mpi_mod_ext_rep disjoint to
|
||||||
|
* make it easier to catch when they are accidentally swapped. */
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0,
|
||||||
|
MBEDTLS_MPI_MOD_EXT_REP_LE = 8,
|
||||||
|
MBEDTLS_MPI_MOD_EXT_REP_BE
|
||||||
|
} mbedtls_mpi_mod_ext_rep;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
@ -40,8 +56,8 @@ typedef struct {
|
|||||||
mbedtls_mpi_uint *p;
|
mbedtls_mpi_uint *p;
|
||||||
size_t n; // number of limbs
|
size_t n; // number of limbs
|
||||||
size_t plen; // bitlen of p
|
size_t plen; // bitlen of p
|
||||||
int ext_rep; // signals external representation (eg. byte order)
|
mbedtls_mpi_mod_ext_rep ext_rep; // signals external representation (eg. byte order)
|
||||||
int int_rep; // selector to signal the active member of the union
|
mbedtls_mpi_mod_rep_selector int_rep; // selector to signal the active member of the union
|
||||||
union rep
|
union rep
|
||||||
{
|
{
|
||||||
mbedtls_mpi_mont_struct mont;
|
mbedtls_mpi_mont_struct mont;
|
||||||
@ -49,21 +65,6 @@ typedef struct {
|
|||||||
} rep;
|
} rep;
|
||||||
} mbedtls_mpi_mod_modulus;
|
} mbedtls_mpi_mod_modulus;
|
||||||
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
MBEDTLS_MPI_MOD_REP_INVALID = 0,
|
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY,
|
|
||||||
MBEDTLS_MPI_MOD_REP_OPT_RED
|
|
||||||
} mbedtls_mpi_mod_rep_selector;
|
|
||||||
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
MBEDTLS_MPI_MOD_EXT_REP_INVALID = 0,
|
|
||||||
MBEDTLS_MPI_MOD_EXT_REP_LE,
|
|
||||||
MBEDTLS_MPI_MOD_EXT_REP_BE
|
|
||||||
} mbedtls_mpi_mod_ext_rep;
|
|
||||||
|
|
||||||
|
|
||||||
/** Setup a residue structure.
|
/** Setup a residue structure.
|
||||||
*
|
*
|
||||||
* \param r The address of residue to setup. The size is determined by \p m.
|
* \param r The address of residue to setup. The size is determined by \p m.
|
||||||
@ -124,8 +125,8 @@ void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
|
|||||||
int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
|
int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
|
||||||
mbedtls_mpi_uint *p,
|
mbedtls_mpi_uint *p,
|
||||||
size_t pn,
|
size_t pn,
|
||||||
int ext_rep,
|
mbedtls_mpi_mod_ext_rep ext_rep,
|
||||||
int int_rep );
|
mbedtls_mpi_mod_rep_selector int_rep );
|
||||||
|
|
||||||
/** Free elements of a modulus structure.
|
/** Free elements of a modulus structure.
|
||||||
*
|
*
|
||||||
|
@ -48,13 +48,17 @@ int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
|
|||||||
{
|
{
|
||||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||||
|
|
||||||
if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_LE )
|
switch( m->ext_rep )
|
||||||
|
{
|
||||||
|
case MBEDTLS_MPI_MOD_EXT_REP_LE:
|
||||||
ret = mbedtls_mpi_core_read_le( X, m->n, buf, buflen );
|
ret = mbedtls_mpi_core_read_le( X, m->n, buf, buflen );
|
||||||
|
break;
|
||||||
else if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_BE )
|
case MBEDTLS_MPI_MOD_EXT_REP_BE:
|
||||||
ret = mbedtls_mpi_core_read_be( X, m->n, buf, buflen );
|
ret = mbedtls_mpi_core_read_be( X, m->n, buf, buflen );
|
||||||
else
|
break;
|
||||||
|
default:
|
||||||
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
|
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
|
||||||
|
}
|
||||||
|
|
||||||
if( ret != 0 )
|
if( ret != 0 )
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -75,16 +79,15 @@ int mbedtls_mpi_mod_raw_write( mbedtls_mpi_uint *X,
|
|||||||
unsigned char *buf,
|
unsigned char *buf,
|
||||||
size_t buflen )
|
size_t buflen )
|
||||||
{
|
{
|
||||||
if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_LE )
|
switch( m->ext_rep )
|
||||||
|
{
|
||||||
|
case MBEDTLS_MPI_MOD_EXT_REP_LE:
|
||||||
return mbedtls_mpi_core_write_le( X, m->n, buf, buflen );
|
return mbedtls_mpi_core_write_le( X, m->n, buf, buflen );
|
||||||
|
case MBEDTLS_MPI_MOD_EXT_REP_BE:
|
||||||
else if( m->ext_rep == MBEDTLS_MPI_MOD_EXT_REP_BE )
|
|
||||||
return mbedtls_mpi_core_write_be( X, m->n, buf, buflen );
|
return mbedtls_mpi_core_write_be( X, m->n, buf, buflen );
|
||||||
|
default:
|
||||||
else
|
|
||||||
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
|
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
|
||||||
|
}
|
||||||
return( 0 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* MBEDTLS_BIGNUM_C */
|
#endif /* MBEDTLS_BIGNUM_C */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user