v0.2.2 release
[yandexfotkisp] / src / CP_RSA.h
diff --git a/src/CP_RSA.h b/src/CP_RSA.h
new file mode 100644 (file)
index 0000000..86f0567
--- /dev/null
@@ -0,0 +1,96 @@
+#ifndef CP_RSA_H\r
+#define CP_RSA_H\r
+\r
+#include <stddef.h>\r
+\r
+// VLONG.HPP ---------------------------------\r
+\r
+class vlong // very long integer - can be used like long\r
+{\r
+public:\r
+       // Standard arithmetic operators\r
+       friend vlong operator +( const vlong& x, const vlong& y );\r
+       friend vlong operator -( const vlong& x, const vlong& y );\r
+       friend vlong operator *( const vlong& x, const vlong& y );\r
+       friend vlong operator /( const vlong& x, const vlong& y );\r
+       friend vlong operator %( const vlong& x, const vlong& y );\r
+       vlong& operator +=( const vlong& x );\r
+       vlong& operator -=( const vlong& x );\r
+\r
+       // Standard comparison operators\r
+       friend inline int operator !=( const vlong& x, const vlong& y ){ return x.cf( y ) != 0; }\r
+       friend inline int operator ==( const vlong& x, const vlong& y ){ return x.cf( y ) == 0; }\r
+       friend inline int operator >=( const vlong& x, const vlong& y ){ return x.cf( y ) >= 0; }\r
+       friend inline int operator <=( const vlong& x, const vlong& y ){ return x.cf( y ) <= 0; }\r
+       friend inline int operator > ( const vlong& x, const vlong& y ){ return x.cf( y ) > 0; }\r
+       friend inline int operator < ( const vlong& x, const vlong& y ){ return x.cf( y ) < 0; }\r
+\r
+       // Construction and conversion operations\r
+       vlong ( unsigned x=0 );\r
+       vlong ( const vlong& x ); // copy constructor\r
+       ~vlong();\r
+       operator unsigned ();\r
+       vlong& operator =(const vlong& x);\r
+\r
+       void load( unsigned * a, unsigned n ); // load value, a[0] is lsw\r
+       void store( unsigned * a, unsigned n ) const; // low level save, a[0] is lsw\r
+       unsigned get_nunits() const;\r
+       unsigned bits() const;\r
+\r
+private:\r
+       class vlong_value * value;\r
+       int negative;\r
+       int cf( const vlong x ) const;\r
+       void docopy();\r
+       friend class monty;\r
+};\r
+\r
+// RSA.HPP -------------------------------------------\r
+\r
+class public_key\r
+{\r
+       public:\r
+       vlong m,e;\r
+       vlong encrypt( const vlong& plain ); // Requires 0 <= plain < m\r
+       void MakeMe(const char *);\r
+};\r
+\r
+class private_key : public public_key\r
+{\r
+       public:\r
+       vlong p,q;\r
+       vlong decrypt( const vlong& cipher );\r
+\r
+       void create( const char * r1, const char * r2 );\r
+       // r1 and r2 should be null terminated random strings\r
+       // each of length around 35 characters (for a 500 bit modulus)\r
+\r
+       void MakeMeStr(char *);\r
+       void MakePq(const char *);\r
+       void MakePqStr(char *);\r
+};\r
+\r
+#define MAX_CRYPT_BITS 1024\r
+\r
+class CCryptoProviderRSA\r
+{\r
+       class private_key prkface;\r
+\r
+       void EncryptPortion(const char *pt, size_t,char *ct,size_t &);\r
+       void DecryptPortion(const char *ct, size_t,char *pt,size_t &);\r
+\r
+public:\r
+\r
+       CCryptoProviderRSA();\r
+       virtual ~CCryptoProviderRSA();\r
+\r
+       virtual void Encrypt(const char *, size_t,char *, size_t &);\r
+       virtual void Decrypt(const char *, size_t,char *, size_t &);\r
+       virtual void ExportPublicKey(char *);\r
+       virtual void ImportPublicKey(const char *);\r
+       virtual void ExportPrivateKey(char *);\r
+       virtual void ImportPrivateKey(const char *);\r
+       virtual void GetBlockSize(int&, int&);\r
+};\r
+\r
+#endif\r