Shamir's trick implementation.
[crypto_lab3.git] / src / Tests.cpp
index c9a2839..73ee552 100644 (file)
@@ -18,27 +18,23 @@ Tests::Tests(uint keySizeBits, uint rsaPublicExponent) :
 
 void Tests::runTests()
 {
-   if (this->rsaStandard())
-      cout << "RSA standard OK" << endl;
-   else
-      cout << "RSA standard failed!" << endl;
+   cout << "Tests::runTests() ..." << endl;
 
-   if (this->rsaCrt())
-      cout << "RSA CRT OK" << endl;
-   else
-      cout << "RSA CRT failed!" << endl;
+   cout << "RSA standard: " << (this->rsaStandard() ? "OK" : "failed!") << endl;
+   cout << "RSA CRT: " <<  (this->rsaCrt() ? "OK" : "failed!") << endl;
 }
 
 void Tests::runTestsWithShamirsTrick()
 {
-   if (this->rsaCrtWithShamirsTrick())
-      cout << "RSA CRT with shamir's trick OK" << endl;
-   else
-      cout << "RSA CRT with shamir's trick failed!" << endl;
+   cout << "Tests::runTestsWithShamirsTrick() ..." << endl;
+
+   cout << "RSA CRT with Shamir's trick: " << (this->rsaCrtWithShamirsTrick() ? "OK" : "failed!") << endl;
 }
 
 void Tests::runTimeMeasures()
 {
+   cout << "Tests::runTimeMeasures() ..." << endl;
+
    const int N = 1000;
    const int nbKeys = 20; // Number of different generated key.
 
@@ -57,11 +53,13 @@ void Tests::runTimeMeasures()
    cout << N * nbKeys << " x RSA CRT: " << timeRsaCRT << " ms" << endl;
    cout << N * nbKeys << " x RSA CRT Shamir's trick: " << timeRsaCRTShamirsTrick << " ms" << endl;
    cout << "Speedup for CRT: " << (double(timeRsaStd) / double(timeRsaCRT)) << endl;
-   cout << "Speedup for CRT with Shamir's trick: " << (double(timeRsaStd) / double(timeRsaCRT)) << endl;
+   cout << "Speedup for CRT with Shamir's trick: " << (double(timeRsaStd) / double(timeRsaCRTShamirsTrick)) << endl;
 }
 
 void Tests::doAttack()
 {
+   cout << "Tests::doAttack() ..." << endl;
+
    const auto& keys = RsaCrt::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS);
    const auto& kPub = keys.first;
    const auto& kPriv = keys.second;
@@ -92,7 +90,7 @@ void Tests::doAttack()
       attackSuccessful = attackSuccessful && kPriv.p == p && kPriv.q == q; // With p and q we can recreate the original private key.
    }
 
-   // Try the attack with a correct signature.
+   // Try the attack with a correct signature (p and q shouldn't be found).
    {
       mpz_class correctSignaturePowerE;
       mpz_pow_ui(correctSignaturePowerE.get_mpz_t(), correctSignature.get_mpz_t(), RSA_PUBLIC_EXPONENT);
@@ -116,9 +114,22 @@ void Tests::doAttack()
 
 void Tests::doAttackFixed()
 {
-   const auto& keys = RsaCrt::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS);
-   const auto& kPub = keys.first;
+   cout << "Tests::doAttackFixed() ..." << endl;
+
+   const auto& keys = RsaCrtShamirsTrick::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS);
    const auto& kPriv = keys.second;
+
+   mpz_class message = Rand::randSize(128);
+
+   try
+   {
+      RsaCrtShamirsTrick::signWithFaultySp(message, kPriv);
+      cout << "Attack successful -> incorrect" << endl;
+   }
+   catch (const RsaCrtShamirsTrick::UnableToSignWithShamirsTrick& e)
+   {
+      cout << "Attack failed -> correct" << endl;
+   }
 }
 
 bool Tests::rsaStandard()