|
|
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/Serializer.h" ! 10: #include "Crc32.h" ! 11: #include "Keyfile.h" ! 12: ! 13: namespace TrueCrypt ! 14: { ! 15: void Keyfile::Apply (const BufferPtr &pool) const ! 16: { ! 17: if (Path.IsDirectory()) ! 18: throw ParameterIncorrect (SRC_POS); ! 19: ! 20: File file; ! 21: file.Open (Path, File::OpenRead, File::ShareRead, File::PreserveTimestamps); ! 22: ! 23: Crc32 crc32; ! 24: size_t poolPos = 0; ! 25: uint64 totalLength = 0; ! 26: uint64 readLength; ! 27: ! 28: SecureBuffer keyfileBuf (File::GetOptimalReadSize()); ! 29: ! 30: while ((readLength = file.Read (keyfileBuf)) > 0) ! 31: { ! 32: for (size_t i = 0; i < readLength; i++) ! 33: { ! 34: uint32 crc = crc32.Process (keyfileBuf[i]); ! 35: ! 36: pool[poolPos++] += (byte) (crc >> 24); ! 37: pool[poolPos++] += (byte) (crc >> 16); ! 38: pool[poolPos++] += (byte) (crc >> 8); ! 39: pool[poolPos++] += (byte) crc; ! 40: ! 41: if (poolPos >= pool.Size()) ! 42: poolPos = 0; ! 43: ! 44: if (++totalLength >= MaxProcessedLength) ! 45: goto done; ! 46: } ! 47: } ! 48: done: ! 49: if (totalLength < MinProcessedLength) ! 50: throw InsufficientData (SRC_POS, Path); ! 51: } ! 52: ! 53: shared_ptr <VolumePassword> Keyfile::ApplyListToPassword (shared_ptr <KeyfileList> keyfiles, shared_ptr <VolumePassword> password) ! 54: { ! 55: if (!password) ! 56: password.reset (new VolumePassword); ! 57: ! 58: if (!keyfiles || keyfiles->size() < 1) ! 59: return password; ! 60: ! 61: KeyfileList keyfilesExp; ! 62: ! 63: // Enumerate directories ! 64: foreach (shared_ptr <Keyfile> keyfile, *keyfiles) ! 65: { ! 66: if (FilesystemPath (*keyfile).GetType() == FilesystemPathType::Directory) ! 67: { ! 68: foreach_ref (const FilePath &path, Directory::GetFilePaths (*keyfile)) ! 69: { ! 70: keyfilesExp.push_back (make_shared <Keyfile> (path)); ! 71: } ! 72: } ! 73: else ! 74: { ! 75: keyfilesExp.push_back (keyfile); ! 76: } ! 77: } ! 78: ! 79: make_shared_auto (VolumePassword, newPassword); ! 80: ! 81: if (keyfilesExp.size() < 1) ! 82: { ! 83: newPassword->Set (*password); ! 84: } ! 85: else ! 86: { ! 87: SecureBuffer keyfilePool (VolumePassword::MaxSize); ! 88: ! 89: // Pad password with zeros if shorter than max length ! 90: keyfilePool.Zero(); ! 91: keyfilePool.CopyFrom (ConstBufferPtr (password->DataPtr(), password->Size())); ! 92: ! 93: // Apply all keyfiles ! 94: foreach_ref (const Keyfile &k, keyfilesExp) ! 95: { ! 96: k.Apply (keyfilePool); ! 97: } ! 98: ! 99: newPassword->Set (keyfilePool); ! 100: } ! 101: ! 102: return newPassword; ! 103: } ! 104: ! 105: shared_ptr <KeyfileList> Keyfile::DeserializeList (shared_ptr <Stream> stream, const string &name) ! 106: { ! 107: shared_ptr <KeyfileList> keyfiles; ! 108: Serializer sr (stream); ! 109: ! 110: if (!sr.DeserializeBool (name + "Null")) ! 111: { ! 112: keyfiles.reset (new KeyfileList); ! 113: foreach (const wstring &k, sr.DeserializeWStringList (name)) ! 114: keyfiles->push_back (make_shared <Keyfile> (k)); ! 115: } ! 116: return keyfiles; ! 117: } ! 118: ! 119: void Keyfile::SerializeList (shared_ptr <Stream> stream, const string &name, shared_ptr <KeyfileList> keyfiles) ! 120: { ! 121: Serializer sr (stream); ! 122: sr.Serialize (name + "Null", keyfiles == nullptr); ! 123: if (keyfiles) ! 124: { ! 125: list <wstring> sl; ! 126: ! 127: foreach_ref (const Keyfile &k, *keyfiles) ! 128: sl.push_back (FilesystemPath (k)); ! 129: ! 130: sr.Serialize (name, sl); ! 131: } ! 132: } ! 133: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.