9324bfc02d593ecb31faa926ba213035f809aa44
[crypto_lab3.git] / src / main.cpp
1 /**
2 * ICR - Labo 3.
3 * It uses GMPxx lib and C++11.
4 * It needs the RNG file "/dev/urandom".
5 *
6 * Author: Grégory Burri
7 */
8
9 #include <iostream>
10 using namespace std;
11
12 #include <gmpxx.h>
13
14 #include "Utils.h"
15 #include "Rand.h"
16 #include "Rsa.h"
17 #include "RsaStd.h"
18 #include "RsaCrt.h"
19
20 const uint KEY_SIZE_BITS = 1024;
21 const uint RSA_PUBLIC_EXPONENT = 65537;
22
23 bool testRsaStandard()
24 {
25 const auto& keys = RsaStd::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS);
26 const auto& kPub = keys.first;
27 const auto& kPriv = keys.second;
28
29 mpz_class message = Rand::randSize(KEY_SIZE_BITS / 2);
30 mpz_class signature = RsaStd::sign(message, kPriv);
31
32 return Rsa::verifySignature(message, signature, kPub) && !Rsa::verifySignature(message + 1, signature, kPub);
33 }
34
35 bool testRsaCrt()
36 {
37 const auto& keys = RsaCrt::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS);
38 const auto& kPub = keys.first;
39 const auto& kPriv = keys.second;
40
41 mpz_class message = Rand::randSize(KEY_SIZE_BITS / 2);
42 mpz_class signature = RsaCrt::sign(message, kPriv);
43
44 return Rsa::verifySignature(message, signature, kPub) && !Rsa::verifySignature(message + 1, signature, kPub);
45 }
46
47 int timeSignRsaStd(int N)
48 {
49 Timer timer;
50 const auto& keys = RsaStd::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS);
51
52 for (int i = 0; i < N; i++)
53 {
54 mpz_class message = Rand::randSize(KEY_SIZE_BITS / 2);
55 RsaStd::sign(message, keys.second);
56 }
57
58 return timer.ms();
59 }
60
61 int timeSignRsaCRT(int N)
62 {
63 Timer timer;
64 const auto& keys = RsaCrt::generateRSAKeys(RSA_PUBLIC_EXPONENT, KEY_SIZE_BITS);
65
66 for (int i = 0; i < N; i++)
67 {
68 mpz_class message = Rand::randSize(KEY_SIZE_BITS / 2);
69 RsaCrt::sign(message, keys.second);
70 }
71
72 return timer.ms();
73 }
74
75 void measuresRsaDurations()
76 {
77 const int N = 10000;
78 int timeRsaStd = timeSignRsaStd(N);
79 int timeRsaCRT = timeSignRsaCRT(N);
80
81 cout << N << " x RSA standard: " << timeRsaStd << " ms" << endl;
82 cout << N << " x RSA CRT: " << timeRsaCRT << " ms" << endl;
83 cout << "Speedup: " << (double(timeRsaStd) / double(timeRsaCRT)) << endl;
84 }
85
86 int main(int argc, char** argv)
87 {
88 vector<string> args;
89 for (int i = 0; i < argc; i++)
90 args.push_back(string(argv[i]));
91
92 if (!testRsaStandard())
93 cout << "RSA standard failed!" << endl;
94
95 if (!testRsaCrt())
96 cout << "RSA CRT failed!" << endl;
97
98 measuresRsaDurations();
99
100 return 0;
101 }