X-Git-Url: http://git.euphorik.ch/?p=crypto_lab3.git;a=blobdiff_plain;f=src%2FTests.cpp;fp=src%2FTests.cpp;h=2adcd32f2a1d4117990b7924a03bb359b50ac547;hp=92bccd0ace882171840cd5ea95690835d986c301;hb=2745bc6570ac32789650336b8c84a52d1883c62a;hpb=d061bc06b7e5681e9da4c2c0b7642f50d126ff76 diff --git a/src/Tests.cpp b/src/Tests.cpp index 92bccd0..2adcd32 100644 --- a/src/Tests.cpp +++ b/src/Tests.cpp @@ -47,6 +47,59 @@ void Tests::runTimeMeasures() cout << "Speedup: " << (double(timeRsaStd) / double(timeRsaCRT)) << endl; } +void Tests::doAttack() +{ + const auto& keys = RsaCrt::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS); + const auto& kPub = keys.first; + const auto& kPriv = keys.second; + mpz_class message = Rand::randSize(128); + mpz_class faultySignature = RsaCrt::signWithFaultySp(message, kPriv); + mpz_class correctSignature = RsaCrt::sign(message, kPriv); + + bool attackOK = true; + + cout << "Original:" << endl; + cout << " p = " << kPriv.p << endl; + cout << " q = " << kPriv.q << endl; + + // At this point the attacker doesn't know the private key but he has intercepted the message and the faulty signature. + { + mpz_class faultySignaturePowerE; + mpz_pow_ui(faultySignaturePowerE.get_mpz_t(), faultySignature.get_mpz_t(), RSA_PUBLIC_EXPONENT); + mpz_class messageMinuxFaultySignaturePowerE = message - faultySignaturePowerE; + mpz_class q; + mpz_gcd(q.get_mpz_t(), messageMinuxFaultySignaturePowerE.get_mpz_t(), kPub.n.get_mpz_t()); + mpz_class p = kPub.n / q; + + cout << "Found with a faulty signature:" << endl; + cout << " p = " << p << endl; + cout << " q = " << q << endl; + + attackOK = attackOK && kPriv.p == p && kPriv.q == q; // With p and q we can recreate the original private key. + } + + // Try the attack with a correct signature. + { + mpz_class correctSignaturePowerE; + mpz_pow_ui(correctSignaturePowerE.get_mpz_t(), correctSignature.get_mpz_t(), RSA_PUBLIC_EXPONENT); + mpz_class messageMinuxCorrectSignaturePowerE = message - correctSignaturePowerE; + mpz_class q; + mpz_gcd(q.get_mpz_t(), messageMinuxCorrectSignaturePowerE.get_mpz_t(), kPub.n.get_mpz_t()); + mpz_class p = kPub.n / q; + + cout << "Found with a correct signature:" << endl; + cout << " p = " << p << endl; // Equal to 1. + cout << " q = " << q << endl; // Equal to n. + + attackOK = attackOK && kPriv.p != p && kPriv.q != q; + } + + if (attackOK) + cout << "Attack successful" << endl; + else + cout << "Attack failed" << endl; +} + bool Tests::rsaStandard() { const auto& keys = RsaStd::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS);