Merge branch 'feature/rsa-crt_hardening' into develop

This fixes #77
This commit is contained in:
Steffen Jaeckel 2015-09-08 21:04:54 +02:00
commit 0b140206cf
3 changed files with 22 additions and 3 deletions

View File

@ -402,6 +402,11 @@
#define LTC_RSA_BLINDING
#endif /* LTC_NO_RSA_BLINDING */
#if defined(LTC_MRSA) && !defined(LTC_NO_RSA_CRT_HARDENING)
/* Enable RSA CRT hardening when doing private key operations by default */
#define LTC_RSA_CRT_HARDENING
#endif /* LTC_NO_RSA_CRT_HARDENING */
#if defined(LTC_MECC) && !defined(LTC_NO_ECC_TIMING_RESISTANT)
/* Enable ECC timing resistant version by default */
#define LTC_ECC_TIMING_RESISTANT

View File

@ -269,8 +269,12 @@ const char *crypt_build_settings =
"\nPK Algs:\n"
#if defined(LTC_MRSA)
" RSA"
#if defined(LTC_RSA_BLINDING)
#if defined(LTC_RSA_BLINDING) && defined(LTC_RSA_CRT_HARDENING)
" (with blinding and CRT hardening)"
#elif defined(LTC_RSA_BLINDING)
" (with blinding)"
#elif defined(LTC_RSA_CRT_HARDENING)
" (with CRT hardening)"
#endif
"\n"
#endif

View File

@ -38,7 +38,7 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen,
void *rnd, *rndi /* inverse of rnd */;
#endif
unsigned long x;
int err;
int err, no_crt;
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(out != NULL);
@ -100,7 +100,9 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen,
}
#endif /* LTC_RSA_BLINDING */
if ((key->dP == NULL) || (mp_get_digit_count(key->dP) == 0)) {
no_crt = (key->dP == NULL) || (mp_get_digit_count(key->dP) == 0);
if (no_crt) {
/*
* In case CRT optimization parameters are not provided,
* the private key is directly used to exptmod it
@ -129,6 +131,14 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen,
goto error;
}
#endif
#ifdef LTC_RSA_CRT_HARDENING
if (!no_crt) {
if ((err = mp_exptmod(tmp, key->e, key->N, tmpa)) != CRYPT_OK) { goto error; }
if ((err = mp_read_unsigned_bin(tmpb, (unsigned char *)in, (int)inlen)) != CRYPT_OK) { goto error; }
if (mp_cmp(tmpa, tmpb) != LTC_MP_EQ) { err = CRYPT_ERROR; goto error; }
}
#endif
} else {
/* exptmod it */
if ((err = mp_exptmod(tmp, key->e, key->N, tmp)) != CRYPT_OK) { goto error; }