mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-04-18 05:42:35 +00:00
Merge pull request #4618 from ronald-cron-arm/rsa-padding
Remove mbedtls_rsa_init() padding parameters
This commit is contained in:
commit
364380e70c
5
ChangeLog.d/rsa-padding.txt
Normal file
5
ChangeLog.d/rsa-padding.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
API changes
|
||||||
|
* mbedtls_rsa_init() now always selects the PKCS#1v1.5 encoding for an RSA
|
||||||
|
key. To use an RSA key with PSS or OAEP, call mbedtls_rsa_set_padding()
|
||||||
|
after initializing the context. mbedtls_rsa_set_padding() now returns an
|
||||||
|
error if its parameters are invalid.
|
29
docs/3.0-migration-guide.d/rsa-padding.md
Normal file
29
docs/3.0-migration-guide.d/rsa-padding.md
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
Remove the padding parameters from mbedtls_rsa_init()
|
||||||
|
-----------------------------------------------------
|
||||||
|
|
||||||
|
This affects all users who use the RSA encryption, decryption, sign and
|
||||||
|
verify APIs.
|
||||||
|
|
||||||
|
The function mbedtls_rsa_init() no longer supports selecting the PKCS#1 v2.1
|
||||||
|
encoding and its hash. It just selects the PKCS#1 v1.5 encoding by default. If
|
||||||
|
you were using the PKCS#1 v2.1 encoding you now need, subsequently to the call
|
||||||
|
to mbedtls_rsa_init(), to call mbedtls_rsa_set_padding() to set it.
|
||||||
|
|
||||||
|
To choose the padding type when initializing a context, instead of
|
||||||
|
```C
|
||||||
|
mbedtls_rsa_init(ctx, padding, hash_id);
|
||||||
|
```
|
||||||
|
, use
|
||||||
|
```C
|
||||||
|
mbedtls_rsa_init(ctx);
|
||||||
|
mbedtls_rsa_set_padding(ctx, padding, hash_id);
|
||||||
|
```
|
||||||
|
|
||||||
|
To use PKCS#1 v1.5 padding, instead of
|
||||||
|
```C
|
||||||
|
mbedtls_rsa_init(ctx, MBEDTLS_RSA_PKCS_V15, <ignored>);
|
||||||
|
```
|
||||||
|
, just use
|
||||||
|
```C
|
||||||
|
mbedtls_rsa_init(ctx);
|
||||||
|
```
|
@ -134,33 +134,51 @@ mbedtls_rsa_context;
|
|||||||
/**
|
/**
|
||||||
* \brief This function initializes an RSA context.
|
* \brief This function initializes an RSA context.
|
||||||
*
|
*
|
||||||
|
* \note This function initializes the padding and the hash
|
||||||
|
* identifier to respectively #MBEDTLS_RSA_PKCS_V15 and
|
||||||
|
* #MBEDTLS_MD_NONE. See mbedtls_rsa_set_padding() for more
|
||||||
|
* information about those parameters.
|
||||||
|
*
|
||||||
|
* \param ctx The RSA context to initialize. This must not be \c NULL.
|
||||||
|
*/
|
||||||
|
void mbedtls_rsa_init( mbedtls_rsa_context *ctx );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief This function sets padding for an already initialized RSA
|
||||||
|
* context.
|
||||||
|
*
|
||||||
* \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
|
* \note Set padding to #MBEDTLS_RSA_PKCS_V21 for the RSAES-OAEP
|
||||||
* encryption scheme and the RSASSA-PSS signature scheme.
|
* encryption scheme and the RSASSA-PSS signature scheme.
|
||||||
*
|
*
|
||||||
* \note The \p hash_id parameter is ignored when using
|
* \note The \p hash_id parameter is ignored when using
|
||||||
* #MBEDTLS_RSA_PKCS_V15 padding.
|
* #MBEDTLS_RSA_PKCS_V15 padding.
|
||||||
*
|
*
|
||||||
* \note The choice of padding mode is strictly enforced for private key
|
* \note The choice of padding mode is strictly enforced for private
|
||||||
* operations, since there might be security concerns in
|
* key operations, since there might be security concerns in
|
||||||
* mixing padding modes. For public key operations it is
|
* mixing padding modes. For public key operations it is
|
||||||
* a default value, which can be overridden by calling specific
|
* a default value, which can be overridden by calling specific
|
||||||
* \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
|
* \c mbedtls_rsa_rsaes_xxx or \c mbedtls_rsa_rsassa_xxx
|
||||||
|
* functions.
|
||||||
*
|
*
|
||||||
* \note The hash selected in \p hash_id is always used for OEAP
|
* \note The hash selected in \p hash_id is always used for OEAP
|
||||||
* encryption. For PSS signatures, it is always used for
|
* encryption. For PSS signatures, it is always used for
|
||||||
* making signatures, but can be overridden for verifying them.
|
* making signatures, but can be overridden for verifying them.
|
||||||
* If set to #MBEDTLS_MD_NONE, it is always overridden.
|
* If set to #MBEDTLS_MD_NONE, it is always overridden.
|
||||||
*
|
*
|
||||||
* \param ctx The RSA context to initialize. This must not be \c NULL.
|
* \param ctx The initialized RSA context to be configured.
|
||||||
* \param padding The padding mode to use. This must be either
|
* \param padding The padding mode to use. This must be either
|
||||||
* #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
|
* #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
|
||||||
* \param hash_id The hash identifier of ::mbedtls_md_type_t type, if
|
* \param hash_id The hash identifier for PSS or OAEP, if \p padding is
|
||||||
* \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused
|
* #MBEDTLS_RSA_PKCS_V21. #MBEDTLS_MD_NONE is accepted by this
|
||||||
* otherwise.
|
* function but may be not suitable for some operations.
|
||||||
|
* Ignored if \p padding is #MBEDTLS_RSA_PKCS_V15.
|
||||||
|
*
|
||||||
|
* \return \c 0 on success.
|
||||||
|
* \return #MBEDTLS_ERR_RSA_INVALID_PADDING failure:
|
||||||
|
* \p padding or \p hash_id is invalid.
|
||||||
*/
|
*/
|
||||||
void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
|
int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
|
||||||
int padding,
|
mbedtls_md_type_t hash_id );
|
||||||
int hash_id );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief This function imports a set of core parameters into an
|
* \brief This function imports a set of core parameters into an
|
||||||
@ -391,18 +409,6 @@ int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx,
|
|||||||
int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
|
int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
|
||||||
mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
|
mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP );
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief This function sets padding for an already initialized RSA
|
|
||||||
* context. See mbedtls_rsa_init() for details.
|
|
||||||
*
|
|
||||||
* \param ctx The initialized RSA context to be configured.
|
|
||||||
* \param padding The padding mode to use. This must be either
|
|
||||||
* #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21.
|
|
||||||
* \param hash_id The #MBEDTLS_RSA_PKCS_V21 hash identifier.
|
|
||||||
*/
|
|
||||||
void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
|
|
||||||
int hash_id );
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief This function retrieves the length of RSA modulus in Bytes.
|
* \brief This function retrieves the length of RSA modulus in Bytes.
|
||||||
*
|
*
|
||||||
|
@ -165,7 +165,7 @@ static void *rsa_alloc_wrap( void )
|
|||||||
void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
|
void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_rsa_context ) );
|
||||||
|
|
||||||
if( ctx != NULL )
|
if( ctx != NULL )
|
||||||
mbedtls_rsa_init( (mbedtls_rsa_context *) ctx, 0, 0 );
|
mbedtls_rsa_init( (mbedtls_rsa_context *) ctx );
|
||||||
|
|
||||||
return( ctx );
|
return( ctx );
|
||||||
}
|
}
|
||||||
|
@ -2838,13 +2838,14 @@ psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
||||||
static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
|
static int psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
|
||||||
mbedtls_rsa_context *rsa )
|
mbedtls_rsa_context *rsa )
|
||||||
{
|
{
|
||||||
psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
|
psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
|
||||||
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
|
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
|
||||||
mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
|
mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
|
||||||
mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
|
|
||||||
|
return( mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg ) );
|
||||||
}
|
}
|
||||||
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
|
#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
|
||||||
|
|
||||||
@ -2917,7 +2918,11 @@ psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
|
|||||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
||||||
if( PSA_ALG_IS_RSA_OAEP( alg ) )
|
if( PSA_ALG_IS_RSA_OAEP( alg ) )
|
||||||
{
|
{
|
||||||
psa_rsa_oaep_set_padding_mode( alg, rsa );
|
status = mbedtls_to_psa_error(
|
||||||
|
psa_rsa_oaep_set_padding_mode( alg, rsa ) );
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
goto rsa_exit;
|
||||||
|
|
||||||
status = mbedtls_to_psa_error(
|
status = mbedtls_to_psa_error(
|
||||||
mbedtls_rsa_rsaes_oaep_encrypt( rsa,
|
mbedtls_rsa_rsaes_oaep_encrypt( rsa,
|
||||||
mbedtls_psa_get_random,
|
mbedtls_psa_get_random,
|
||||||
@ -3023,7 +3028,11 @@ psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
|
|||||||
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
|
||||||
if( PSA_ALG_IS_RSA_OAEP( alg ) )
|
if( PSA_ALG_IS_RSA_OAEP( alg ) )
|
||||||
{
|
{
|
||||||
psa_rsa_oaep_set_padding_mode( alg, rsa );
|
status = mbedtls_to_psa_error(
|
||||||
|
psa_rsa_oaep_set_padding_mode( alg, rsa ) );
|
||||||
|
if( status != PSA_SUCCESS )
|
||||||
|
goto rsa_exit;
|
||||||
|
|
||||||
status = mbedtls_to_psa_error(
|
status = mbedtls_to_psa_error(
|
||||||
mbedtls_rsa_rsaes_oaep_decrypt( rsa,
|
mbedtls_rsa_rsaes_oaep_decrypt( rsa,
|
||||||
mbedtls_psa_get_random,
|
mbedtls_psa_get_random,
|
||||||
|
@ -317,7 +317,7 @@ static psa_status_t rsa_generate_key(
|
|||||||
if( status != PSA_SUCCESS )
|
if( status != PSA_SUCCESS )
|
||||||
return( status );
|
return( status );
|
||||||
|
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
|
mbedtls_rsa_init( &rsa );
|
||||||
ret = mbedtls_rsa_gen_key( &rsa,
|
ret = mbedtls_rsa_gen_key( &rsa,
|
||||||
mbedtls_psa_get_random,
|
mbedtls_psa_get_random,
|
||||||
MBEDTLS_PSA_RANDOM_STATE,
|
MBEDTLS_PSA_RANDOM_STATE,
|
||||||
@ -416,29 +416,36 @@ static psa_status_t rsa_sign_hash(
|
|||||||
#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
|
#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
|
||||||
if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
|
if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
|
||||||
{
|
{
|
||||||
mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
|
ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
|
||||||
MBEDTLS_MD_NONE );
|
MBEDTLS_MD_NONE );
|
||||||
ret = mbedtls_rsa_pkcs1_sign( rsa,
|
if( ret == 0 )
|
||||||
mbedtls_psa_get_random,
|
{
|
||||||
MBEDTLS_PSA_RANDOM_STATE,
|
ret = mbedtls_rsa_pkcs1_sign( rsa,
|
||||||
md_alg,
|
mbedtls_psa_get_random,
|
||||||
(unsigned int) hash_length,
|
MBEDTLS_PSA_RANDOM_STATE,
|
||||||
hash,
|
md_alg,
|
||||||
signature );
|
(unsigned int) hash_length,
|
||||||
|
hash,
|
||||||
|
signature );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
#endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
|
#endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
|
||||||
#if defined(BUILTIN_ALG_RSA_PSS)
|
#if defined(BUILTIN_ALG_RSA_PSS)
|
||||||
if( PSA_ALG_IS_RSA_PSS( alg ) )
|
if( PSA_ALG_IS_RSA_PSS( alg ) )
|
||||||
{
|
{
|
||||||
mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
|
ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
|
||||||
ret = mbedtls_rsa_rsassa_pss_sign( rsa,
|
|
||||||
mbedtls_psa_get_random,
|
if( ret == 0 )
|
||||||
MBEDTLS_PSA_RANDOM_STATE,
|
{
|
||||||
MBEDTLS_MD_NONE,
|
ret = mbedtls_rsa_rsassa_pss_sign( rsa,
|
||||||
(unsigned int) hash_length,
|
mbedtls_psa_get_random,
|
||||||
hash,
|
MBEDTLS_PSA_RANDOM_STATE,
|
||||||
signature );
|
MBEDTLS_MD_NONE,
|
||||||
|
(unsigned int) hash_length,
|
||||||
|
hash,
|
||||||
|
signature );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
#endif /* BUILTIN_ALG_RSA_PSS */
|
#endif /* BUILTIN_ALG_RSA_PSS */
|
||||||
@ -489,25 +496,31 @@ static psa_status_t rsa_verify_hash(
|
|||||||
#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
|
#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
|
||||||
if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
|
if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
|
||||||
{
|
{
|
||||||
mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
|
ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
|
||||||
MBEDTLS_MD_NONE );
|
MBEDTLS_MD_NONE );
|
||||||
ret = mbedtls_rsa_pkcs1_verify( rsa,
|
if( ret == 0 )
|
||||||
md_alg,
|
{
|
||||||
(unsigned int) hash_length,
|
ret = mbedtls_rsa_pkcs1_verify( rsa,
|
||||||
hash,
|
md_alg,
|
||||||
signature );
|
(unsigned int) hash_length,
|
||||||
|
hash,
|
||||||
|
signature );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
#endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
|
#endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
|
||||||
#if defined(BUILTIN_ALG_RSA_PSS)
|
#if defined(BUILTIN_ALG_RSA_PSS)
|
||||||
if( PSA_ALG_IS_RSA_PSS( alg ) )
|
if( PSA_ALG_IS_RSA_PSS( alg ) )
|
||||||
{
|
{
|
||||||
mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
|
ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
|
||||||
ret = mbedtls_rsa_rsassa_pss_verify( rsa,
|
if( ret == 0 )
|
||||||
MBEDTLS_MD_NONE,
|
{
|
||||||
(unsigned int) hash_length,
|
ret = mbedtls_rsa_rsassa_pss_verify( rsa,
|
||||||
hash,
|
MBEDTLS_MD_NONE,
|
||||||
signature );
|
(unsigned int) hash_length,
|
||||||
|
hash,
|
||||||
|
signature );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
#endif /* BUILTIN_ALG_RSA_PSS */
|
#endif /* BUILTIN_ALG_RSA_PSS */
|
||||||
|
@ -477,17 +477,14 @@ int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx,
|
|||||||
/*
|
/*
|
||||||
* Initialize an RSA context
|
* Initialize an RSA context
|
||||||
*/
|
*/
|
||||||
void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
|
void mbedtls_rsa_init( mbedtls_rsa_context *ctx )
|
||||||
int padding,
|
|
||||||
int hash_id )
|
|
||||||
{
|
{
|
||||||
RSA_VALIDATE( ctx != NULL );
|
RSA_VALIDATE( ctx != NULL );
|
||||||
RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
|
|
||||||
padding == MBEDTLS_RSA_PKCS_V21 );
|
|
||||||
|
|
||||||
memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
|
memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
|
||||||
|
|
||||||
mbedtls_rsa_set_padding( ctx, padding, hash_id );
|
ctx->padding = MBEDTLS_RSA_PKCS_V15;
|
||||||
|
ctx->hash_id = MBEDTLS_MD_NONE;
|
||||||
|
|
||||||
#if defined(MBEDTLS_THREADING_C)
|
#if defined(MBEDTLS_THREADING_C)
|
||||||
/* Set ctx->ver to nonzero to indicate that the mutex has been
|
/* Set ctx->ver to nonzero to indicate that the mutex has been
|
||||||
@ -500,15 +497,38 @@ void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
|
|||||||
/*
|
/*
|
||||||
* Set padding for an existing RSA context
|
* Set padding for an existing RSA context
|
||||||
*/
|
*/
|
||||||
void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
|
int mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding,
|
||||||
int hash_id )
|
mbedtls_md_type_t hash_id )
|
||||||
{
|
{
|
||||||
RSA_VALIDATE( ctx != NULL );
|
switch( padding )
|
||||||
RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 ||
|
{
|
||||||
padding == MBEDTLS_RSA_PKCS_V21 );
|
#if defined(MBEDTLS_PKCS1_V15)
|
||||||
|
case MBEDTLS_RSA_PKCS_V15:
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_PKCS1_V21)
|
||||||
|
case MBEDTLS_RSA_PKCS_V21:
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
return( MBEDTLS_ERR_RSA_INVALID_PADDING );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( ( padding == MBEDTLS_RSA_PKCS_V21 ) &&
|
||||||
|
( hash_id != MBEDTLS_MD_NONE ) )
|
||||||
|
{
|
||||||
|
const mbedtls_md_info_t *md_info;
|
||||||
|
|
||||||
|
md_info = mbedtls_md_info_from_type( hash_id );
|
||||||
|
if( md_info == NULL )
|
||||||
|
return( MBEDTLS_ERR_RSA_INVALID_PADDING );
|
||||||
|
}
|
||||||
|
|
||||||
ctx->padding = padding;
|
ctx->padding = padding;
|
||||||
ctx->hash_id = hash_id;
|
ctx->hash_id = hash_id;
|
||||||
|
|
||||||
|
return( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -2580,7 +2600,7 @@ int mbedtls_rsa_self_test( int verbose )
|
|||||||
mbedtls_mpi K;
|
mbedtls_mpi K;
|
||||||
|
|
||||||
mbedtls_mpi_init( &K );
|
mbedtls_mpi_init( &K );
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &rsa );
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) );
|
||||||
MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
|
MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) );
|
||||||
|
@ -92,7 +92,6 @@ int main( void )
|
|||||||
mbedtls_aes_context aes;
|
mbedtls_aes_context aes;
|
||||||
|
|
||||||
mbedtls_net_init( &server_fd );
|
mbedtls_net_init( &server_fd );
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256 );
|
|
||||||
mbedtls_dhm_init( &dhm );
|
mbedtls_dhm_init( &dhm );
|
||||||
mbedtls_aes_init( &aes );
|
mbedtls_aes_init( &aes );
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||||
@ -125,7 +124,7 @@ int main( void )
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &rsa );
|
||||||
|
|
||||||
if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
||||||
( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
||||||
|
@ -95,7 +95,6 @@ int main( void )
|
|||||||
|
|
||||||
mbedtls_net_init( &listen_fd );
|
mbedtls_net_init( &listen_fd );
|
||||||
mbedtls_net_init( &client_fd );
|
mbedtls_net_init( &client_fd );
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256 );
|
|
||||||
mbedtls_dhm_init( &dhm );
|
mbedtls_dhm_init( &dhm );
|
||||||
mbedtls_aes_init( &aes );
|
mbedtls_aes_init( &aes );
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||||
@ -131,7 +130,7 @@ int main( void )
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &rsa );
|
||||||
|
|
||||||
if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
|
if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
|
||||||
( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
|
( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
|
||||||
|
@ -90,7 +90,7 @@ int main( int argc, char *argv[] )
|
|||||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||||
fflush( stdout );
|
fflush( stdout );
|
||||||
|
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &rsa );
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||||
mbedtls_entropy_init( &entropy );
|
mbedtls_entropy_init( &entropy );
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||||
|
@ -87,7 +87,7 @@ int main( int argc, char *argv[] )
|
|||||||
fflush( stdout );
|
fflush( stdout );
|
||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &rsa );
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||||
mbedtls_entropy_init( &entropy );
|
mbedtls_entropy_init( &entropy );
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ int main( void )
|
|||||||
const char *pers = "rsa_genkey";
|
const char *pers = "rsa_genkey";
|
||||||
|
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &rsa );
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||||
|
@ -67,7 +67,7 @@ int main( int argc, char *argv[] )
|
|||||||
char filename[512];
|
char filename[512];
|
||||||
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
||||||
|
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &rsa );
|
||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||||
|
@ -115,7 +115,13 @@ int main( int argc, char *argv[] )
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
|
if( ( ret = mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ),
|
||||||
|
MBEDTLS_RSA_PKCS_V21,
|
||||||
|
MBEDTLS_MD_SHA256 ) ) != 0 )
|
||||||
|
{
|
||||||
|
mbedtls_printf( " failed\n ! Padding not supported\n" );
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Compute the SHA-256 hash of the input file,
|
* Compute the SHA-256 hash of the input file,
|
||||||
|
@ -66,7 +66,7 @@ int main( int argc, char *argv[] )
|
|||||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||||
char filename[512];
|
char filename[512];
|
||||||
|
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &rsa );
|
||||||
|
|
||||||
if( argc != 2 )
|
if( argc != 2 )
|
||||||
{
|
{
|
||||||
|
@ -98,7 +98,13 @@ int main( int argc, char *argv[] )
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
|
if( ( ret = mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ),
|
||||||
|
MBEDTLS_RSA_PKCS_V21,
|
||||||
|
MBEDTLS_MD_SHA256 ) ) != 0 )
|
||||||
|
{
|
||||||
|
mbedtls_printf( " failed\n ! Invalid padding\n" );
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Extract the RSA signature from the file
|
* Extract the RSA signature from the file
|
||||||
|
@ -786,7 +786,7 @@ int main( int argc, char *argv[] )
|
|||||||
{
|
{
|
||||||
mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
|
mbedtls_snprintf( title, sizeof( title ), "RSA-%d", keysize );
|
||||||
|
|
||||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &rsa );
|
||||||
mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
|
mbedtls_rsa_gen_key( &rsa, myrand, NULL, keysize, 65537 );
|
||||||
|
|
||||||
TIME_PUBLIC( title, " public",
|
TIME_PUBLIC( title, " public",
|
||||||
|
@ -863,7 +863,7 @@ void pk_rsa_alt( )
|
|||||||
size_t sig_len, ciph_len, test_len;
|
size_t sig_len, ciph_len, test_len;
|
||||||
int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
|
int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
|
||||||
|
|
||||||
mbedtls_rsa_init( &raw, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
|
mbedtls_rsa_init( &raw );
|
||||||
mbedtls_pk_init( &rsa ); mbedtls_pk_init( &alt );
|
mbedtls_pk_init( &rsa ); mbedtls_pk_init( &alt );
|
||||||
|
|
||||||
memset( hash, 0x2a, sizeof hash );
|
memset( hash, 0x2a, sizeof hash );
|
||||||
|
@ -25,7 +25,9 @@ void pkcs1_rsaes_v15_encrypt( int mod, int radix_N, char * input_N,
|
|||||||
info.length = rnd_buf->len;
|
info.length = rnd_buf->len;
|
||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V15, hash ) == 0 );
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
||||||
@ -71,7 +73,9 @@ void pkcs1_rsaes_v15_decrypt( int mod, int radix_P, char * input_P,
|
|||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
||||||
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V15, hash ) == 0 );
|
||||||
|
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
||||||
@ -193,7 +197,7 @@ void pkcs1_v15_decode( data_t *input,
|
|||||||
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
||||||
mbedtls_mpi_init( &Nmpi ); mbedtls_mpi_init( &Empi );
|
mbedtls_mpi_init( &Nmpi ); mbedtls_mpi_init( &Empi );
|
||||||
mbedtls_mpi_init( &Pmpi ); mbedtls_mpi_init( &Qmpi );
|
mbedtls_mpi_init( &Pmpi ); mbedtls_mpi_init( &Qmpi );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_mpi_read_binary( &Nmpi, N, sizeof( N ) ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_binary( &Nmpi, N, sizeof( N ) ) == 0 );
|
||||||
TEST_ASSERT( mbedtls_mpi_read_binary( &Empi, E, sizeof( E ) ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_binary( &Empi, E, sizeof( E ) ) == 0 );
|
||||||
@ -277,7 +281,9 @@ void pkcs1_rsassa_v15_sign( int mod, int radix_P, char * input_P, int radix_Q,
|
|||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
||||||
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V15, hash ) == 0 );
|
||||||
|
|
||||||
memset( hash_result, 0x00, sizeof( hash_result ) );
|
memset( hash_result, 0x00, sizeof( hash_result ) );
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
@ -325,7 +331,9 @@ void pkcs1_rsassa_v15_verify( int mod, int radix_N, char * input_N,
|
|||||||
((void) salt);
|
((void) salt);
|
||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V15, hash ) == 0 );
|
||||||
memset( hash_result, 0x00, sizeof( hash_result ) );
|
memset( hash_result, 0x00, sizeof( hash_result ) );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
||||||
|
@ -24,7 +24,9 @@ void pkcs1_rsaes_oaep_encrypt( int mod, data_t * input_N, data_t * input_E,
|
|||||||
info.length = rnd_buf->len;
|
info.length = rnd_buf->len;
|
||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V21, hash ) == 0 );
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_mpi_read_binary( &N, input_N->x, input_N->len ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_binary( &N, input_N->x, input_N->len ) == 0 );
|
||||||
@ -67,7 +69,9 @@ void pkcs1_rsaes_oaep_decrypt( int mod, data_t * input_P, data_t * input_Q,
|
|||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
||||||
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
||||||
|
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V21, hash ) == 0 );
|
||||||
|
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
||||||
@ -131,7 +135,9 @@ void pkcs1_rsassa_pss_sign( int mod, data_t * input_P, data_t * input_Q,
|
|||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
||||||
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V21, hash ) == 0 );
|
||||||
|
|
||||||
memset( hash_result, 0x00, sizeof( hash_result ) );
|
memset( hash_result, 0x00, sizeof( hash_result ) );
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
@ -189,7 +195,9 @@ void pkcs1_rsassa_pss_verify( int mod, data_t * input_N, data_t * input_E,
|
|||||||
((void) salt);
|
((void) salt);
|
||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V21, hash ) == 0 );
|
||||||
memset( hash_result, 0x00, sizeof( hash_result ) );
|
memset( hash_result, 0x00, sizeof( hash_result ) );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_mpi_read_binary( &N, input_N->x, input_N->len ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_binary( &N, input_N->x, input_N->len ) == 0 );
|
||||||
@ -225,7 +233,9 @@ void pkcs1_rsassa_pss_verify_ext( int mod, data_t * input_N, data_t * input_E,
|
|||||||
mbedtls_mpi N, E;
|
mbedtls_mpi N, E;
|
||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, ctx_hash );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V21, ctx_hash ) == 0 );
|
||||||
memset( hash_result, 0x00, sizeof( hash_result ) );
|
memset( hash_result, 0x00, sizeof( hash_result ) );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_mpi_read_binary( &N, input_N->x, input_N->len ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_binary( &N, input_N->x, input_N->len ) == 0 );
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
RSA parameter validation
|
||||||
|
rsa_invalid_param:
|
||||||
|
|
||||||
RSA init-free-free
|
RSA init-free-free
|
||||||
rsa_init_free:0
|
rsa_init_free:0
|
||||||
|
|
||||||
|
@ -17,6 +17,44 @@
|
|||||||
* END_DEPENDENCIES
|
* END_DEPENDENCIES
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* BEGIN_CASE */
|
||||||
|
void rsa_invalid_param( )
|
||||||
|
{
|
||||||
|
mbedtls_rsa_context ctx;
|
||||||
|
const int invalid_padding = 42;
|
||||||
|
const int invalid_hash_id = 0xff;
|
||||||
|
|
||||||
|
mbedtls_rsa_init( &ctx );
|
||||||
|
|
||||||
|
TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
invalid_padding,
|
||||||
|
MBEDTLS_MD_NONE ),
|
||||||
|
MBEDTLS_ERR_RSA_INVALID_PADDING );
|
||||||
|
|
||||||
|
TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V21,
|
||||||
|
invalid_hash_id ),
|
||||||
|
MBEDTLS_ERR_RSA_INVALID_PADDING );
|
||||||
|
|
||||||
|
#if !defined(MBEDTLS_PKCS1_V15)
|
||||||
|
TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V15,
|
||||||
|
MBEDTLS_MD_NONE ),
|
||||||
|
MBEDTLS_ERR_RSA_INVALID_PADDING );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(MBEDTLS_PKCS1_V21)
|
||||||
|
TEST_EQUAL( mbedtls_rsa_set_padding( &ctx,
|
||||||
|
MBEDTLS_RSA_PKCS_V21,
|
||||||
|
MBEDTLS_MD_NONE ),
|
||||||
|
MBEDTLS_ERR_RSA_INVALID_PADDING );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
exit:
|
||||||
|
mbedtls_rsa_free( &ctx );
|
||||||
|
}
|
||||||
|
/* END_CASE */
|
||||||
|
|
||||||
/* BEGIN_CASE */
|
/* BEGIN_CASE */
|
||||||
void rsa_init_free( int reinit )
|
void rsa_init_free( int reinit )
|
||||||
{
|
{
|
||||||
@ -27,11 +65,11 @@ void rsa_init_free( int reinit )
|
|||||||
* unconditionally on an error path without checking whether it has
|
* unconditionally on an error path without checking whether it has
|
||||||
* already been called in the success path. */
|
* already been called in the success path. */
|
||||||
|
|
||||||
mbedtls_rsa_init( &ctx, 0, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
mbedtls_rsa_free( &ctx );
|
mbedtls_rsa_free( &ctx );
|
||||||
|
|
||||||
if( reinit )
|
if( reinit )
|
||||||
mbedtls_rsa_init( &ctx, 0, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
mbedtls_rsa_free( &ctx );
|
mbedtls_rsa_free( &ctx );
|
||||||
|
|
||||||
/* This test case always succeeds, functionally speaking. A plausible
|
/* This test case always succeeds, functionally speaking. A plausible
|
||||||
@ -55,7 +93,9 @@ void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
|
|||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
||||||
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, padding_mode, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx,padding_mode,
|
||||||
|
MBEDTLS_MD_NONE ) == 0 );
|
||||||
|
|
||||||
memset( hash_result, 0x00, sizeof( hash_result ) );
|
memset( hash_result, 0x00, sizeof( hash_result ) );
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
@ -104,7 +144,9 @@ void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
|
|||||||
mbedtls_mpi N, E;
|
mbedtls_mpi N, E;
|
||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, padding_mode, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
|
||||||
|
MBEDTLS_MD_NONE ) == 0 );
|
||||||
memset( hash_result, 0x00, sizeof( hash_result ) );
|
memset( hash_result, 0x00, sizeof( hash_result ) );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
||||||
@ -138,7 +180,9 @@ void rsa_pkcs1_sign_raw( data_t * hash_result,
|
|||||||
mbedtls_mpi N, P, Q, E;
|
mbedtls_mpi N, P, Q, E;
|
||||||
mbedtls_test_rnd_pseudo_info rnd_info;
|
mbedtls_test_rnd_pseudo_info rnd_info;
|
||||||
|
|
||||||
mbedtls_rsa_init( &ctx, padding_mode, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
|
||||||
|
MBEDTLS_MD_NONE ) == 0 );
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
||||||
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
||||||
|
|
||||||
@ -185,7 +229,9 @@ void rsa_pkcs1_verify_raw( data_t * hash_result,
|
|||||||
mbedtls_mpi N, E;
|
mbedtls_mpi N, E;
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||||
|
|
||||||
mbedtls_rsa_init( &ctx, padding_mode, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
|
||||||
|
MBEDTLS_MD_NONE ) == 0 );
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
||||||
@ -219,7 +265,9 @@ void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
|
|||||||
|
|
||||||
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
||||||
|
|
||||||
mbedtls_rsa_init( &ctx, padding_mode, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
|
||||||
|
MBEDTLS_MD_NONE ) == 0 );
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
||||||
@ -260,7 +308,9 @@ void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
|
|||||||
mbedtls_mpi N, E;
|
mbedtls_mpi N, E;
|
||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, padding_mode, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
|
||||||
|
MBEDTLS_MD_NONE ) == 0 );
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
||||||
@ -305,7 +355,9 @@ void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
|
|||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
||||||
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
||||||
|
|
||||||
mbedtls_rsa_init( &ctx, padding_mode, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
TEST_ASSERT( mbedtls_rsa_set_padding( &ctx, padding_mode,
|
||||||
|
MBEDTLS_MD_NONE ) == 0 );
|
||||||
|
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
||||||
@ -353,8 +405,8 @@ void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
|
|||||||
mbedtls_mpi N, E;
|
mbedtls_mpi N, E;
|
||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &ctx2 );
|
||||||
memset( output, 0x00, sizeof( output ) );
|
memset( output, 0x00, sizeof( output ) );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
|
||||||
@ -411,8 +463,8 @@ void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
|
|||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
|
||||||
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &ctx2 );
|
||||||
|
|
||||||
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
|
||||||
|
|
||||||
@ -488,7 +540,7 @@ void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
|
|||||||
mbedtls_mpi N, E;
|
mbedtls_mpi N, E;
|
||||||
|
|
||||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
|
||||||
if( strlen( input_N ) )
|
if( strlen( input_N ) )
|
||||||
{
|
{
|
||||||
@ -519,7 +571,7 @@ void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
|
|||||||
{
|
{
|
||||||
mbedtls_rsa_context ctx;
|
mbedtls_rsa_context ctx;
|
||||||
|
|
||||||
mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
|
||||||
ctx.len = mod / 8;
|
ctx.len = mod / 8;
|
||||||
if( strlen( input_P ) )
|
if( strlen( input_P ) )
|
||||||
@ -580,8 +632,8 @@ void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
|
|||||||
{
|
{
|
||||||
mbedtls_rsa_context pub, prv;
|
mbedtls_rsa_context pub, prv;
|
||||||
|
|
||||||
mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &pub );
|
||||||
mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
|
mbedtls_rsa_init( &prv );
|
||||||
|
|
||||||
pub.len = mod / 8;
|
pub.len = mod / 8;
|
||||||
prv.len = mod / 8;
|
prv.len = mod / 8;
|
||||||
@ -652,7 +704,7 @@ void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
|
|||||||
|
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||||
mbedtls_entropy_init( &entropy );
|
mbedtls_entropy_init( &entropy );
|
||||||
mbedtls_rsa_init ( &ctx, 0, 0 );
|
mbedtls_rsa_init ( &ctx );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||||
&entropy, (const unsigned char *) pers,
|
&entropy, (const unsigned char *) pers,
|
||||||
@ -804,7 +856,7 @@ void mbedtls_rsa_import( int radix_N, char *input_N,
|
|||||||
|
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||||
mbedtls_entropy_init( &entropy );
|
mbedtls_entropy_init( &entropy );
|
||||||
mbedtls_rsa_init( &ctx, 0, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
|
||||||
mbedtls_mpi_init( &N );
|
mbedtls_mpi_init( &N );
|
||||||
mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||||
@ -945,7 +997,7 @@ void mbedtls_rsa_export( int radix_N, char *input_N,
|
|||||||
|
|
||||||
mbedtls_rsa_context ctx;
|
mbedtls_rsa_context ctx;
|
||||||
|
|
||||||
mbedtls_rsa_init( &ctx, 0, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
|
||||||
mbedtls_mpi_init( &N );
|
mbedtls_mpi_init( &N );
|
||||||
mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||||
@ -1125,7 +1177,7 @@ void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
|
|||||||
|
|
||||||
mbedtls_rsa_context ctx;
|
mbedtls_rsa_context ctx;
|
||||||
|
|
||||||
mbedtls_rsa_init( &ctx, 0, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
|
||||||
/* Setup RSA context */
|
/* Setup RSA context */
|
||||||
TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
|
TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
|
||||||
@ -1227,7 +1279,7 @@ void mbedtls_rsa_import_raw( data_t *input_N,
|
|||||||
|
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||||
mbedtls_entropy_init( &entropy );
|
mbedtls_entropy_init( &entropy );
|
||||||
mbedtls_rsa_init( &ctx, 0, 0 );
|
mbedtls_rsa_init( &ctx );
|
||||||
|
|
||||||
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||||
&entropy, (const unsigned char *) pers,
|
&entropy, (const unsigned char *) pers,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user