From c9965dca278becce5bac9a1d9996c8ecc7154d1d Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Sun, 29 Sep 2013 14:58:17 +0200 Subject: [PATCH] RSA blinding threading support --- include/polarssl/rsa.h | 7 +++++++ library/rsa.c | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/include/polarssl/rsa.h b/include/polarssl/rsa.h index 71fbff2d8..e7b619159 100644 --- a/include/polarssl/rsa.h +++ b/include/polarssl/rsa.h @@ -32,6 +32,10 @@ #include "bignum.h" #include "md.h" +#if defined(POLARSSL_THREADING_C) +#include "threading.h" +#endif + /* * RSA Error codes */ @@ -100,6 +104,9 @@ typedef struct specified in the md.h header file for the EME-OAEP and EMSA-PSS encoding */ +#if defined(POLARSSL_THREADING_C) + threading_mutex_t mutex; /*!< Thread-safety mutex */ +#endif } rsa_context; diff --git a/library/rsa.c b/library/rsa.c index 42fea4182..1784379f3 100644 --- a/library/rsa.c +++ b/library/rsa.c @@ -54,6 +54,10 @@ void rsa_init( rsa_context *ctx, ctx->padding = padding; ctx->hash_id = hash_id; + +#if defined(POLARSSL_THREADING_C) + polarssl_mutex_init( &ctx->mutex ); +#endif } #if defined(POLARSSL_GENPRIME) @@ -298,6 +302,9 @@ int rsa_private( rsa_context *ctx, unsigned char *output ) { int ret; +#if defined(POLARSSL_THREADING_C) + int locked = 0; +#endif size_t olen; mpi T, T1, T2; @@ -315,6 +322,10 @@ int rsa_private( rsa_context *ctx, #else if( f_rng != NULL ) { +#if defined(POLARSSL_THREADING_C) + polarssl_mutex_lock( &ctx->mutex ); + locked = 1; +#endif /* * Blinding * T = T * Vi mod N @@ -361,7 +372,10 @@ int rsa_private( rsa_context *ctx, MPI_CHK( mpi_write_binary( &T, output, olen ) ); cleanup: - +#if defined(POLARSSL_THREADING_C) + if( locked ) + polarssl_mutex_unlock( &ctx->mutex ); +#endif mpi_free( &T ); mpi_free( &T1 ); mpi_free( &T2 ); 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->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D ); mpi_free( &ctx->E ); mpi_free( &ctx->N ); + +#if defined(POLARSSL_THREADING_C) + polarssl_mutex_free( &ctx->mutex ); +#endif } #if defined(POLARSSL_SELF_TEST)