v0.2.2 release
[yandexfotkisp] / src / CP_RSA.h
1 #ifndef CP_RSA_H\r
2 #define CP_RSA_H\r
3 \r
4 #include <stddef.h>\r
5 \r
6 // VLONG.HPP ---------------------------------\r
7 \r
8 class vlong // very long integer - can be used like long\r
9 {\r
10 public:\r
11         // Standard arithmetic operators\r
12         friend vlong operator +( const vlong& x, const vlong& y );\r
13         friend vlong operator -( const vlong& x, const vlong& y );\r
14         friend vlong operator *( const vlong& x, const vlong& y );\r
15         friend vlong operator /( const vlong& x, const vlong& y );\r
16         friend vlong operator %( const vlong& x, const vlong& y );\r
17         vlong& operator +=( const vlong& x );\r
18         vlong& operator -=( const vlong& x );\r
19 \r
20         // Standard comparison operators\r
21         friend inline int operator !=( const vlong& x, const vlong& y ){ return x.cf( y ) != 0; }\r
22         friend inline int operator ==( const vlong& x, const vlong& y ){ return x.cf( y ) == 0; }\r
23         friend inline int operator >=( const vlong& x, const vlong& y ){ return x.cf( y ) >= 0; }\r
24         friend inline int operator <=( const vlong& x, const vlong& y ){ return x.cf( y ) <= 0; }\r
25         friend inline int operator > ( const vlong& x, const vlong& y ){ return x.cf( y ) > 0; }\r
26         friend inline int operator < ( const vlong& x, const vlong& y ){ return x.cf( y ) < 0; }\r
27 \r
28         // Construction and conversion operations\r
29         vlong ( unsigned x=0 );\r
30         vlong ( const vlong& x ); // copy constructor\r
31         ~vlong();\r
32         operator unsigned ();\r
33         vlong& operator =(const vlong& x);\r
34 \r
35         void load( unsigned * a, unsigned n ); // load value, a[0] is lsw\r
36         void store( unsigned * a, unsigned n ) const; // low level save, a[0] is lsw\r
37         unsigned get_nunits() const;\r
38         unsigned bits() const;\r
39 \r
40 private:\r
41         class vlong_value * value;\r
42         int negative;\r
43         int cf( const vlong x ) const;\r
44         void docopy();\r
45         friend class monty;\r
46 };\r
47 \r
48 // RSA.HPP -------------------------------------------\r
49 \r
50 class public_key\r
51 {\r
52         public:\r
53         vlong m,e;\r
54         vlong encrypt( const vlong& plain ); // Requires 0 <= plain < m\r
55         void MakeMe(const char *);\r
56 };\r
57 \r
58 class private_key : public public_key\r
59 {\r
60         public:\r
61         vlong p,q;\r
62         vlong decrypt( const vlong& cipher );\r
63 \r
64         void create( const char * r1, const char * r2 );\r
65         // r1 and r2 should be null terminated random strings\r
66         // each of length around 35 characters (for a 500 bit modulus)\r
67 \r
68         void MakeMeStr(char *);\r
69         void MakePq(const char *);\r
70         void MakePqStr(char *);\r
71 };\r
72 \r
73 #define MAX_CRYPT_BITS 1024\r
74 \r
75 class CCryptoProviderRSA\r
76 {\r
77         class private_key prkface;\r
78 \r
79         void EncryptPortion(const char *pt, size_t,char *ct,size_t &);\r
80         void DecryptPortion(const char *ct, size_t,char *pt,size_t &);\r
81 \r
82 public:\r
83 \r
84         CCryptoProviderRSA();\r
85         virtual ~CCryptoProviderRSA();\r
86 \r
87         virtual void Encrypt(const char *, size_t,char *, size_t &);\r
88         virtual void Decrypt(const char *, size_t,char *, size_t &);\r
89         virtual void ExportPublicKey(char *);\r
90         virtual void ImportPublicKey(const char *);\r
91         virtual void ExportPrivateKey(char *);\r
92         virtual void ImportPrivateKey(const char *);\r
93         virtual void GetBlockSize(int&, int&);\r
94 };\r
95 \r
96 #endif\r