|
|
1.1 root 1: /*
2: Copyright (c) 2008 TrueCrypt Foundation. All rights reserved.
3:
4: Governed by the TrueCrypt License 2.4 the full text of which is contained
5: in the file License.txt included in TrueCrypt binary and source code
6: distribution packages.
7: */
8:
9: #ifndef TC_HEADER_Encryption_Ciphers
10: #define TC_HEADER_Encryption_Ciphers
11:
12: #include "Platform/Platform.h"
13:
14:
15: namespace TrueCrypt
16: {
17: class Cipher;
18: typedef vector < shared_ptr <Cipher> > CipherList;
19:
20: class Cipher
21: {
22: public:
23: virtual ~Cipher ();
24:
25: virtual void DecryptBlock (byte *data) const;
26: virtual void EncryptBlock (byte *data) const;
27: static CipherList GetAvailableCiphers ();
28: virtual size_t GetBlockSize () const = 0;
29: virtual size_t GetKeySize () const = 0;
30: virtual wstring GetName () const = 0;
31: virtual shared_ptr <Cipher> GetNew () const = 0;
32: virtual void SetKey (const ConstBufferPtr &key);
33:
34: static const int MaxBlockSize = 16;
35:
36: protected:
37: Cipher ();
38:
39: virtual void Decrypt (byte *data) const = 0;
40: virtual void Encrypt (byte *data) const = 0;
41: virtual size_t GetScheduledKeySize () const = 0;
42: virtual void SetCipherKey (const byte *key) = 0;
43:
44: bool Initialized;
45: SecureBuffer ScheduledKey;
46:
47: private:
48: Cipher (const Cipher &);
49: Cipher &operator= (const Cipher &);
50: };
51:
52: struct CipherException : public Exception
53: {
54: protected:
55: CipherException () { }
56: CipherException (const string &message) : Exception (message) { }
57: CipherException (const string &message, const wstring &subject) : Exception (message, subject) { }
58: };
59:
60:
61: #define TC_CIPHER(NAME, BLOCK_SIZE, KEY_SIZE) \
62: class TC_JOIN (Cipher,NAME) : public Cipher \
63: { \
64: public: \
65: TC_JOIN (Cipher,NAME) () { } \
66: virtual ~TC_JOIN (Cipher,NAME) () { } \
67: \
68: virtual size_t GetBlockSize () const { return BLOCK_SIZE; }; \
69: virtual size_t GetKeySize () const { return KEY_SIZE; }; \
70: virtual wstring GetName () const { return L###NAME; }; \
71: virtual shared_ptr <Cipher> GetNew () const { return shared_ptr <Cipher> (new TC_JOIN (Cipher,NAME)()); } \
72: \
73: protected: \
74: virtual void Decrypt (byte *data) const; \
75: virtual void Encrypt (byte *data) const; \
76: virtual size_t GetScheduledKeySize () const; \
77: virtual void SetCipherKey (const byte *key); \
78: \
79: private: \
80: TC_JOIN (Cipher,NAME) (const TC_JOIN (Cipher,NAME) &); \
81: TC_JOIN (Cipher,NAME) &operator= (const TC_JOIN (Cipher,NAME) &); \
82: }
83:
84: TC_CIPHER (AES, 16, 32);
85: TC_CIPHER (Blowfish, 8, 56);
86: TC_CIPHER (Cast5, 8, 16);
87: TC_CIPHER (Serpent, 16, 32);
88: TC_CIPHER (TripleDES, 8, 24);
89: TC_CIPHER (Twofish, 16, 32);
90:
91: #undef TC_CIPHER
92:
93:
94: #define TC_EXCEPTION(NAME) TC_EXCEPTION_DECL(NAME,CipherException)
95:
96: #undef TC_EXCEPTION_SET
97: #define TC_EXCEPTION_SET \
98: TC_EXCEPTION (CipherInitError); \
99: TC_EXCEPTION (WeakKeyDetected);
100:
101: TC_EXCEPTION_SET;
102:
103: #undef TC_EXCEPTION
104: }
105:
106: #endif // TC_HEADER_Encryption_Ciphers
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.