RSA blinding threading support

This commit is contained in:
Paul Bakker 2013-09-29 14:58:17 +02:00
parent 1337affc91
commit c9965dca27
2 changed files with 26 additions and 1 deletions

View File

@ -32,6 +32,10 @@
#include "bignum.h" #include "bignum.h"
#include "md.h" #include "md.h"
#if defined(POLARSSL_THREADING_C)
#include "threading.h"
#endif
/* /*
* RSA Error codes * RSA Error codes
*/ */
@ -100,6 +104,9 @@ typedef struct
specified in the md.h header file specified in the md.h header file
for the EME-OAEP and EMSA-PSS for the EME-OAEP and EMSA-PSS
encoding */ encoding */
#if defined(POLARSSL_THREADING_C)
threading_mutex_t mutex; /*!< Thread-safety mutex */
#endif
} }
rsa_context; rsa_context;

View File

@ -54,6 +54,10 @@ void rsa_init( rsa_context *ctx,
ctx->padding = padding; ctx->padding = padding;
ctx->hash_id = hash_id; ctx->hash_id = hash_id;
#if defined(POLARSSL_THREADING_C)
polarssl_mutex_init( &ctx->mutex );
#endif
} }
#if defined(POLARSSL_GENPRIME) #if defined(POLARSSL_GENPRIME)
@ -298,6 +302,9 @@ int rsa_private( rsa_context *ctx,
unsigned char *output ) unsigned char *output )
{ {
int ret; int ret;
#if defined(POLARSSL_THREADING_C)
int locked = 0;
#endif
size_t olen; size_t olen;
mpi T, T1, T2; mpi T, T1, T2;
@ -315,6 +322,10 @@ int rsa_private( rsa_context *ctx,
#else #else
if( f_rng != NULL ) if( f_rng != NULL )
{ {
#if defined(POLARSSL_THREADING_C)
polarssl_mutex_lock( &ctx->mutex );
locked = 1;
#endif
/* /*
* Blinding * Blinding
* T = T * Vi mod N * T = T * Vi mod N
@ -361,7 +372,10 @@ int rsa_private( rsa_context *ctx,
MPI_CHK( mpi_write_binary( &T, output, olen ) ); MPI_CHK( mpi_write_binary( &T, output, olen ) );
cleanup: cleanup:
#if defined(POLARSSL_THREADING_C)
if( locked )
polarssl_mutex_unlock( &ctx->mutex );
#endif
mpi_free( &T ); mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T ); mpi_free( &T1 ); mpi_free( &T2 );
if( ret != 0 ) if( ret != 0 )
@ -1330,6 +1344,10 @@ void rsa_free( rsa_context *ctx )
mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP ); mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP );
mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D ); mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D );
mpi_free( &ctx->E ); mpi_free( &ctx->N ); mpi_free( &ctx->E ); mpi_free( &ctx->N );
#if defined(POLARSSL_THREADING_C)
polarssl_mutex_free( &ctx->mutex );
#endif
} }
#if defined(POLARSSL_SELF_TEST) #if defined(POLARSSL_SELF_TEST)