Annotation of truecrypt/volume/cipher.cpp, revision 1.1.1.1

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: #include "Platform/Platform.h"
                     10: #include "Cipher.h"
                     11: #include "Crypto/Aes.h"
                     12: #include "Crypto/Blowfish.h"
                     13: #include "Crypto/Des.h"
                     14: #include "Crypto/Cast.h"
                     15: #include "Crypto/Serpent.h"
                     16: #include "Crypto/Twofish.h"
                     17: 
                     18: namespace TrueCrypt
                     19: {
                     20:        Cipher::Cipher () : Initialized (false)
                     21:        {
                     22:        }
                     23: 
                     24:        Cipher::~Cipher ()
                     25:        {
                     26:        }
                     27: 
                     28:        void Cipher::DecryptBlock (byte *data) const
                     29:        {
                     30:                if (!Initialized)
                     31:                        throw NotInitialized (SRC_POS);
                     32: 
                     33:                Decrypt (data);
                     34:        }
                     35: 
                     36:        void Cipher::EncryptBlock (byte *data) const
                     37:        {
                     38:                if (!Initialized)
                     39:                        throw NotInitialized (SRC_POS);
                     40: 
                     41:                Encrypt (data);
                     42:        }
                     43: 
                     44:        CipherList Cipher::GetAvailableCiphers ()
                     45:        {
                     46:                CipherList l;
                     47: 
                     48:                l.push_back (shared_ptr <Cipher> (new CipherAES ()));
                     49:                l.push_back (shared_ptr <Cipher> (new CipherSerpent ()));
                     50:                l.push_back (shared_ptr <Cipher> (new CipherTwofish ()));
                     51:                l.push_back (shared_ptr <Cipher> (new CipherBlowfish ()));
                     52:                l.push_back (shared_ptr <Cipher> (new CipherCast5 ()));
                     53:                l.push_back (shared_ptr <Cipher> (new CipherTripleDES ()));
                     54: 
                     55:                return l;
                     56:        }
                     57: 
                     58:        void Cipher::SetKey (const ConstBufferPtr &key)
                     59:        {
                     60:                if (key.Size() != GetKeySize ())
                     61:                        throw ParameterIncorrect (SRC_POS);
                     62: 
                     63:                if (!Initialized)
                     64:                        ScheduledKey.Allocate (GetScheduledKeySize ());
                     65: 
                     66:                SetCipherKey (key);
                     67:                Initialized = true;
                     68:        }
                     69: 
                     70: #define TC_EXCEPTION(TYPE) TC_SERIALIZER_FACTORY_ADD(TYPE)
                     71: #undef TC_EXCEPTION_NODECL
                     72: #define TC_EXCEPTION_NODECL(TYPE) TC_SERIALIZER_FACTORY_ADD(TYPE)
                     73: 
                     74:        TC_SERIALIZER_FACTORY_ADD_EXCEPTION_SET (CipherException);
                     75: 
                     76: 
                     77:        // AES
                     78:        void CipherAES::Decrypt (byte *data) const
                     79:        {
                     80:                aes_decrypt (data, data, (aes_decrypt_ctx *) (ScheduledKey.Ptr() + sizeof (aes_encrypt_ctx)));
                     81:        }
                     82: 
                     83:        void CipherAES::Encrypt (byte *data) const
                     84:        {
                     85:                aes_encrypt (data, data, (aes_encrypt_ctx *) ScheduledKey.Ptr());
                     86:        }
                     87: 
                     88:        size_t CipherAES::GetScheduledKeySize () const
                     89:        {
                     90:                return sizeof(aes_encrypt_ctx) + sizeof(aes_decrypt_ctx);
                     91:        }
                     92: 
                     93:        void CipherAES::SetCipherKey (const byte *key)
                     94:        {
                     95:                if (aes_encrypt_key (key, (int) GetKeySize(), (aes_encrypt_ctx *) ScheduledKey.Ptr()) != EXIT_SUCCESS)
                     96:                        throw CipherInitError (SRC_POS);
                     97: 
                     98:                if (aes_decrypt_key (key, (int) GetKeySize(), (aes_decrypt_ctx *) (ScheduledKey.Ptr() + sizeof (aes_encrypt_ctx))) != EXIT_SUCCESS)
                     99:                        throw CipherInitError (SRC_POS);
                    100:        }
                    101: 
                    102:        
                    103:        // Blowfish
                    104:        void CipherBlowfish::Decrypt (byte *data) const
                    105:        {
                    106:                BF_ecb_le_encrypt (data, data, (BF_KEY *) ScheduledKey.Ptr(), 0);
                    107:        }
                    108: 
                    109:        void CipherBlowfish::Encrypt (byte *data) const
                    110:        {
                    111:                BF_ecb_le_encrypt (data, data, (BF_KEY *) ScheduledKey.Ptr(), 1);
                    112:        }
                    113: 
                    114:        size_t CipherBlowfish::GetScheduledKeySize () const
                    115:        {
                    116:                return 4168;
                    117:        }
                    118: 
                    119:        void CipherBlowfish::SetCipherKey (const byte *key)
                    120:        {
                    121:                BF_set_key ((BF_KEY *) ScheduledKey.Ptr(), static_cast<int> (GetKeySize ()), (unsigned char *) key);
                    122:        }
                    123: 
                    124: 
                    125:        // CAST5
                    126:        void CipherCast5::Decrypt (byte *data) const
                    127:        {
                    128:                CAST_ecb_encrypt (data, data, (CAST_KEY *) ScheduledKey.Ptr(), 0);
                    129:        }
                    130: 
                    131:        void CipherCast5::Encrypt (byte *data) const
                    132:        {
                    133:                CAST_ecb_encrypt (data, data, (CAST_KEY *) ScheduledKey.Ptr(), 1);
                    134:        }
                    135: 
                    136:        size_t CipherCast5::GetScheduledKeySize () const
                    137:        {
                    138:                return 128;
                    139:        }
                    140: 
                    141:        void CipherCast5::SetCipherKey (const byte *key)
                    142:        {
                    143:                CAST_set_key((CAST_KEY *) ScheduledKey.Ptr(), static_cast<int> (GetKeySize ()), (unsigned char *) key);
                    144:        }
                    145: 
                    146: 
                    147:        // Serpent
                    148:        void CipherSerpent::Decrypt (byte *data) const
                    149:        {
                    150:                serpent_decrypt (data, data, ScheduledKey);
                    151:        }
                    152: 
                    153:        void CipherSerpent::Encrypt (byte *data) const
                    154:        {
                    155:                serpent_encrypt (data, data, ScheduledKey);
                    156:        }
                    157:        
                    158:        size_t CipherSerpent::GetScheduledKeySize () const
                    159:        {
                    160:                return 140*4;
                    161:        }
                    162: 
                    163:        void CipherSerpent::SetCipherKey (const byte *key)
                    164:        {
                    165:                serpent_set_key (key, static_cast<int> (GetKeySize ()), ScheduledKey);
                    166:        }
                    167: 
                    168: 
                    169:        // Triple-DES
                    170:        void CipherTripleDES::Decrypt (byte *data) const
                    171:        {
                    172:                des_ecb3_encrypt ((des_cblock *) data, (des_cblock *) data,
                    173:                        (des_ks_struct *) ScheduledKey.Ptr(),
                    174:                        (des_ks_struct *) (ScheduledKey.Ptr() + 128),
                    175:                        (des_ks_struct *) (ScheduledKey.Ptr() + 128 * 2), 0);
                    176:        }
                    177: 
                    178:        void CipherTripleDES::Encrypt (byte *data) const
                    179:        {
                    180:                des_ecb3_encrypt ((des_cblock *) data, (des_cblock *) data,
                    181:                        (des_ks_struct *) ScheduledKey.Ptr(),
                    182:                        (des_ks_struct *) (ScheduledKey.Ptr() + 128),
                    183:                        (des_ks_struct *) (ScheduledKey.Ptr() + 128 * 2), 1);
                    184:        }
                    185: 
                    186:        size_t CipherTripleDES::GetScheduledKeySize () const
                    187:        {
                    188:                return 128 * 3;
                    189:        }
                    190: 
                    191:        void CipherTripleDES::SetCipherKey (const byte *key)
                    192:        {
                    193:                des_key_sched ((des_cblock *) (key + 8 * 0), (struct des_ks_struct *) (ScheduledKey.Ptr() + 128 * 0));
                    194:                des_key_sched ((des_cblock *) (key + 8 * 1), (struct des_ks_struct *) (ScheduledKey.Ptr() + 128 * 1));
                    195:                des_key_sched ((des_cblock *) (key + 8 * 2), (struct des_ks_struct *) (ScheduledKey.Ptr() + 128 * 2));
                    196:        }
                    197: 
                    198: 
                    199:        // Twofish
                    200:        void CipherTwofish::Decrypt (byte *data) const
                    201:        {
                    202:                twofish_decrypt ((TwofishInstance *) ScheduledKey.Ptr(), (unsigned int *)data, (unsigned int *)data);
                    203:        }
                    204: 
                    205:        void CipherTwofish::Encrypt (byte *data) const
                    206:        {
                    207:                twofish_encrypt ((TwofishInstance *) ScheduledKey.Ptr(), (unsigned int *)data, (unsigned int *)data);
                    208:        }
                    209: 
                    210:        size_t CipherTwofish::GetScheduledKeySize () const
                    211:        {
                    212:                return TWOFISH_KS;
                    213:        }
                    214: 
                    215:        void CipherTwofish::SetCipherKey (const byte *key)
                    216:        {
                    217:                twofish_set_key ((TwofishInstance *) ScheduledKey.Ptr(), (unsigned int *) key, static_cast<int> (GetKeySize ()) * 8);
                    218:        }
                    219: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.