Shamir's trick implementation.
[crypto_lab3.git] / src / RsaCrtShamirsTrick.h
1 #ifndef RSACRT_SHAMIRS_TRICK_H
2 #define RSACRT_SHAMIRS_TRICK_H
3
4 #include <utility>
5 #include <exception>
6
7 #include <gmpxx.h>
8
9 #include "Rsa.h"
10
11 class RsaCrtShamirsTrick
12 {
13 public:
14 class UnableToSignWithShamirsTrick : public std::exception {};
15
16 struct KeyPriv {
17 mpz_class p;
18 mpz_class q;
19 mpz_class d;
20 mpz_class qInv;
21 };
22
23 /**
24 * Generate a pair of keys (public, private).
25 */
26 static std::pair<Rsa::KeyPub, KeyPriv> generateRSAKeys(uint exponent, uint keySizeBits);
27
28 /**
29 * m must not be greater or equal than kPriv.n.
30 * Use the Shamir's trick to test if a fault has been created during the computation of Sp and Sq.
31 * If so it throws 'UnableToSignWithShamirsTrick'.
32 * @param m the message to sign. No padding is used.
33 */
34 static mpz_class sign(const mpz_class& m, const KeyPriv& kPriv);
35
36 /**
37 * Sp is altered by flipping its 42nd bit.
38 * @param m the message to sign. No padding is used.
39 */
40 static mpz_class signWithFaultySp(const mpz_class& m, const KeyPriv& kPriv);
41
42 private:
43 static mpz_class sign(const mpz_class& m, const KeyPriv& kPriv, bool withError);
44 };
45
46 #endif