|
|
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 "EncryptionAlgorithm.h" ! 10: #include "EncryptionModeCBC.h" ! 11: #include "EncryptionModeLRW.h" ! 12: #include "EncryptionModeXTS.h" ! 13: ! 14: namespace TrueCrypt ! 15: { ! 16: EncryptionAlgorithm::EncryptionAlgorithm () : Deprecated (false) ! 17: { ! 18: } ! 19: ! 20: EncryptionAlgorithm::~EncryptionAlgorithm () ! 21: { ! 22: } ! 23: ! 24: void EncryptionAlgorithm::Decrypt (byte *data, uint64 length) const ! 25: { ! 26: if_debug (ValidateState ()); ! 27: Mode->Decrypt (data, length); ! 28: } ! 29: ! 30: void EncryptionAlgorithm::Decrypt (const BufferPtr &data) const ! 31: { ! 32: Decrypt (data, data.Size()); ! 33: } ! 34: ! 35: void EncryptionAlgorithm::DecryptSectors (byte *data, uint64 sectorIndex, uint64 sectorCount, size_t sectorSize) const ! 36: { ! 37: if_debug (ValidateState()); ! 38: Mode->DecryptSectors (data, sectorIndex, sectorCount, sectorSize); ! 39: } ! 40: ! 41: void EncryptionAlgorithm::Encrypt (byte *data, uint64 length) const ! 42: { ! 43: if_debug (ValidateState()); ! 44: Mode->Encrypt (data, length); ! 45: } ! 46: ! 47: void EncryptionAlgorithm::Encrypt (const BufferPtr &data) const ! 48: { ! 49: Encrypt (data, data.Size()); ! 50: } ! 51: ! 52: void EncryptionAlgorithm::EncryptSectors (byte *data, uint64 sectorIndex, uint64 sectorCount, size_t sectorSize) const ! 53: { ! 54: if_debug (ValidateState ()); ! 55: Mode->EncryptSectors (data, sectorIndex, sectorCount, sectorSize); ! 56: } ! 57: ! 58: EncryptionAlgorithmList EncryptionAlgorithm::GetAvailableAlgorithms () ! 59: { ! 60: EncryptionAlgorithmList l; ! 61: ! 62: l.push_back (shared_ptr <EncryptionAlgorithm> (new AES ())); ! 63: l.push_back (shared_ptr <EncryptionAlgorithm> (new Serpent ())); ! 64: l.push_back (shared_ptr <EncryptionAlgorithm> (new Twofish ())); ! 65: l.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofish ())); ! 66: l.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofishSerpent ())); ! 67: l.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentAES ())); ! 68: l.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentTwofishAES ())); ! 69: l.push_back (shared_ptr <EncryptionAlgorithm> (new TwofishSerpent ())); ! 70: ! 71: l.push_back (shared_ptr <EncryptionAlgorithm> (new AESBlowfish ())); ! 72: l.push_back (shared_ptr <EncryptionAlgorithm> (new AESBlowfishSerpent ())); ! 73: l.push_back (shared_ptr <EncryptionAlgorithm> (new Blowfish ())); ! 74: l.push_back (shared_ptr <EncryptionAlgorithm> (new Cast5 ())); ! 75: l.push_back (shared_ptr <EncryptionAlgorithm> (new TripleDES ())); ! 76: return l; ! 77: } ! 78: ! 79: size_t EncryptionAlgorithm::GetLargestKeySize (const EncryptionAlgorithmList &algorithms) ! 80: { ! 81: size_t largestKeySize = 0; ! 82: ! 83: foreach_ref (const EncryptionAlgorithm &ea, algorithms) ! 84: { ! 85: if (ea.GetKeySize() > largestKeySize) ! 86: largestKeySize = ea.GetKeySize(); ! 87: } ! 88: ! 89: return largestKeySize; ! 90: } ! 91: ! 92: size_t EncryptionAlgorithm::GetKeySize () const ! 93: { ! 94: if (Ciphers.size() < 1) ! 95: throw NotInitialized (SRC_POS); ! 96: ! 97: size_t keySize = 0; ! 98: ! 99: foreach_ref (const Cipher &c, Ciphers) ! 100: keySize += c.GetKeySize(); ! 101: ! 102: return keySize; ! 103: } ! 104: ! 105: size_t EncryptionAlgorithm::GetMaxBlockSize () const ! 106: { ! 107: size_t blockSize = 0; ! 108: ! 109: foreach_ref (const Cipher &c, Ciphers) ! 110: if (c.GetBlockSize() > blockSize) ! 111: blockSize = c.GetBlockSize(); ! 112: ! 113: return blockSize; ! 114: } ! 115: ! 116: size_t EncryptionAlgorithm::GetMinBlockSize () const ! 117: { ! 118: size_t blockSize = 0; ! 119: ! 120: foreach_ref (const Cipher &c, Ciphers) ! 121: if (blockSize == 0 || c.GetBlockSize() < blockSize) ! 122: blockSize = c.GetBlockSize(); ! 123: ! 124: return blockSize; ! 125: } ! 126: ! 127: shared_ptr <EncryptionMode> EncryptionAlgorithm::GetMode () const ! 128: { ! 129: if (Mode.get() == nullptr) ! 130: throw NotInitialized (SRC_POS); ! 131: ! 132: return Mode; ! 133: } ! 134: ! 135: wstring EncryptionAlgorithm::GetName () const ! 136: { ! 137: if (Ciphers.size() < 1) ! 138: throw NotInitialized (SRC_POS); ! 139: ! 140: wstring name; ! 141: ! 142: foreach_reverse_ref (const Cipher &c, Ciphers) ! 143: { ! 144: if (name.empty()) ! 145: name = c.GetName(); ! 146: else ! 147: name += wstring (L"-") + c.GetName(); ! 148: } ! 149: ! 150: return name; ! 151: } ! 152: ! 153: bool EncryptionAlgorithm::IsModeSupported (const EncryptionMode &mode) const ! 154: { ! 155: bool supported = false; ! 156: ! 157: foreach_ref (const EncryptionMode &em, SupportedModes) ! 158: { ! 159: if (typeid (mode) == typeid (em)) ! 160: { ! 161: supported = true; ! 162: break; ! 163: } ! 164: } ! 165: ! 166: return supported; ! 167: } ! 168: ! 169: ! 170: bool EncryptionAlgorithm::IsModeSupported (const shared_ptr <EncryptionMode> mode) const ! 171: { ! 172: return IsModeSupported (*mode); ! 173: } ! 174: ! 175: void EncryptionAlgorithm::SetMode (shared_ptr <EncryptionMode> mode) ! 176: { ! 177: if (!IsModeSupported (*mode)) ! 178: throw ParameterIncorrect (SRC_POS); ! 179: ! 180: mode->SetCiphers (Ciphers); ! 181: Mode = mode; ! 182: } ! 183: ! 184: void EncryptionAlgorithm::SetKey (const ConstBufferPtr &key) ! 185: { ! 186: if (Ciphers.size() < 1) ! 187: throw NotInitialized (SRC_POS); ! 188: ! 189: if (GetKeySize() != key.Size()) ! 190: throw ParameterIncorrect (SRC_POS); ! 191: ! 192: size_t keyOffset = 0; ! 193: foreach_ref (Cipher &c, Ciphers) ! 194: { ! 195: c.SetKey (key.GetRange (keyOffset, c.GetKeySize())); ! 196: keyOffset += c.GetKeySize(); ! 197: } ! 198: } ! 199: ! 200: void EncryptionAlgorithm::ValidateState () const ! 201: { ! 202: if (Ciphers.size() < 1 || Mode.get() == nullptr) ! 203: throw NotInitialized (SRC_POS); ! 204: } ! 205: ! 206: // AES ! 207: AES::AES () ! 208: { ! 209: Ciphers.push_back (shared_ptr <Cipher> (new CipherAES())); ! 210: ! 211: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ())); ! 212: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeLRW ())); ! 213: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 214: } ! 215: ! 216: // AES-Blowfish ! 217: AESBlowfish::AESBlowfish () ! 218: { ! 219: Deprecated = true; ! 220: ! 221: Ciphers.push_back (shared_ptr <Cipher> (new CipherBlowfish ())); ! 222: Ciphers.push_back (shared_ptr <Cipher> (new CipherAES ())); ! 223: ! 224: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 225: } ! 226: ! 227: // AES-Blowfish-Serpent ! 228: AESBlowfishSerpent::AESBlowfishSerpent () ! 229: { ! 230: Deprecated = true; ! 231: ! 232: Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent ())); ! 233: Ciphers.push_back (shared_ptr <Cipher> (new CipherBlowfish ())); ! 234: Ciphers.push_back (shared_ptr <Cipher> (new CipherAES ())); ! 235: ! 236: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 237: } ! 238: ! 239: // AES-Twofish ! 240: AESTwofish::AESTwofish () ! 241: { ! 242: Ciphers.push_back (shared_ptr <Cipher> (new CipherTwofish ())); ! 243: Ciphers.push_back (shared_ptr <Cipher> (new CipherAES ())); ! 244: ! 245: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ())); ! 246: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeLRW ())); ! 247: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 248: } ! 249: ! 250: // AES-Twofish-Serpent ! 251: AESTwofishSerpent::AESTwofishSerpent () ! 252: { ! 253: Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent ())); ! 254: Ciphers.push_back (shared_ptr <Cipher> (new CipherTwofish ())); ! 255: Ciphers.push_back (shared_ptr <Cipher> (new CipherAES ())); ! 256: ! 257: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ())); ! 258: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeLRW ())); ! 259: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 260: } ! 261: ! 262: // Blowfish ! 263: Blowfish::Blowfish () ! 264: { ! 265: Deprecated = true; ! 266: Ciphers.push_back (shared_ptr <Cipher> (new CipherBlowfish())); ! 267: ! 268: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeLRW ())); ! 269: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 270: } ! 271: ! 272: // CAST5 ! 273: Cast5::Cast5 () ! 274: { ! 275: Deprecated = true; ! 276: Ciphers.push_back (shared_ptr <Cipher> (new CipherCast5())); ! 277: ! 278: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeLRW ())); ! 279: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 280: } ! 281: ! 282: // Serpent ! 283: Serpent::Serpent () ! 284: { ! 285: Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent())); ! 286: ! 287: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ())); ! 288: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeLRW ())); ! 289: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 290: } ! 291: ! 292: // Serpent-AES ! 293: SerpentAES::SerpentAES () ! 294: { ! 295: Ciphers.push_back (shared_ptr <Cipher> (new CipherAES ())); ! 296: Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent ())); ! 297: ! 298: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ())); ! 299: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeLRW ())); ! 300: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 301: } ! 302: ! 303: // Triple-DES ! 304: TripleDES::TripleDES () ! 305: { ! 306: Deprecated = true; ! 307: Ciphers.push_back (shared_ptr <Cipher> (new CipherTripleDES())); ! 308: ! 309: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeLRW ())); ! 310: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 311: } ! 312: ! 313: // Twofish ! 314: Twofish::Twofish () ! 315: { ! 316: Ciphers.push_back (shared_ptr <Cipher> (new CipherTwofish())); ! 317: ! 318: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ())); ! 319: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeLRW ())); ! 320: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 321: } ! 322: ! 323: // Twofish-Serpent ! 324: TwofishSerpent::TwofishSerpent () ! 325: { ! 326: Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent ())); ! 327: Ciphers.push_back (shared_ptr <Cipher> (new CipherTwofish ())); ! 328: ! 329: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ())); ! 330: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeLRW ())); ! 331: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 332: } ! 333: ! 334: // Serpent-Twofish-AES ! 335: SerpentTwofishAES::SerpentTwofishAES () ! 336: { ! 337: Ciphers.push_back (shared_ptr <Cipher> (new CipherAES ())); ! 338: Ciphers.push_back (shared_ptr <Cipher> (new CipherTwofish ())); ! 339: Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent ())); ! 340: ! 341: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ())); ! 342: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeLRW ())); ! 343: SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeCBC ())); ! 344: } ! 345: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.