void BufferPtr::CopyFrom (const ConstBufferPtr &bufferPtr) const { if (bufferPtr.Size() > DataSize) throw ParameterTooLarge (SRC_POS); Memory::Copy (DataPtr, bufferPtr.Get(), bufferPtr.Size()); }
void Buffer::CopyFrom (const ConstBufferPtr &bufferPtr) { if (!IsAllocated ()) Allocate (bufferPtr.Size()); else if (bufferPtr.Size() > DataSize) throw ParameterTooLarge (SRC_POS); Memory::Copy (DataPtr, bufferPtr.Get(), bufferPtr.Size()); }
void File::WriteAt (const ConstBufferPtr &buffer, uint64 position) const { if_debug (ValidateState()); #ifdef TC_TRACE_FILE_OPERATIONS TraceFileOperation (FileHandle, Path, true, buffer.Size(), position); #endif throw_sys_sub_if (pwrite (FileHandle, buffer, buffer.Size(), position) != (ssize_t) buffer.Size(), wstring (Path)); }
uint64 MemoryStream::Read (const BufferPtr &buffer) { if (Data.size() == 0) throw ParameterIncorrect (SRC_POS); ConstBufferPtr streamBuf (*this); size_t len = buffer.Size(); if (streamBuf.Size() - ReadPosition < len) len = streamBuf.Size() - ReadPosition; BufferPtr(buffer).CopyFrom (streamBuf.GetRange (ReadPosition, len)); ReadPosition += len; return len; }
T VolumeHeader::DeserializeEntryAt (const ConstBufferPtr &header, const size_t &offset) const { if (offset > header.Size()) throw ParameterIncorrect (SRC_POS); return Endian::Big (*reinterpret_cast<const T *> (header.Get() + offset)); }
void VolumeHeader::EncryptNew (const BufferPtr &newHeaderBuffer, const ConstBufferPtr &newSalt, const ConstBufferPtr &newHeaderKey, shared_ptr <Pkcs5Kdf> newPkcs5Kdf) { if (newHeaderBuffer.Size() != HeaderSize || newSalt.Size() != SaltSize) throw ParameterIncorrect (SRC_POS); shared_ptr <EncryptionMode> mode = EA->GetMode()->GetNew(); shared_ptr <EncryptionAlgorithm> ea = EA->GetNew(); if (typeid (*mode) == typeid (EncryptionModeXTS)) { mode->SetKey (newHeaderKey.GetRange (EA->GetKeySize(), EA->GetKeySize())); ea->SetKey (newHeaderKey.GetRange (0, ea->GetKeySize())); } else { mode->SetKey (newHeaderKey.GetRange (0, mode->GetKeySize())); ea->SetKey (newHeaderKey.GetRange (LegacyEncryptionModeKeyAreaSize, ea->GetKeySize())); } ea->SetMode (mode); newHeaderBuffer.CopyFrom (newSalt); BufferPtr headerData = newHeaderBuffer.GetRange (EncryptedHeaderDataOffset, EncryptedHeaderDataSize); Serialize (headerData); ea->Encrypt (headerData); if (newPkcs5Kdf) Pkcs5 = newPkcs5Kdf; }
void Serializer::Serialize (const string &name, const ConstBufferPtr &data) { SerializeString (name); uint64 size = data.Size(); Serialize (size); DataStream->Write (data); }
void Cipher::SetKey (const ConstBufferPtr &key) { if (key.Size() != GetKeySize ()) throw ParameterIncorrect (SRC_POS); if (!Initialized) ScheduledKey.Allocate (GetScheduledKeySize ()); SetCipherKey (key); Key.CopyFrom (key); Initialized = true; }
void EncryptionAlgorithm::SetKey (const ConstBufferPtr &key) { if (Ciphers.size() < 1) throw NotInitialized (SRC_POS); if (GetKeySize() != key.Size()) throw ParameterIncorrect (SRC_POS); size_t keyOffset = 0; foreach_ref (Cipher &c, Ciphers) { c.SetKey (key.GetRange (keyOffset, c.GetKeySize())); keyOffset += c.GetKeySize(); }
void EncryptionModeLRW::SetKey (const ConstBufferPtr &key) { if (key.Size() != 16) throw ParameterIncorrect (SRC_POS); if (!KeySet) GfContext.Allocate (sizeof (GfCtx)); if (!Gf64TabInit ((unsigned char *) key.Get(), (GfCtx *) (GfContext.Ptr()))) throw bad_alloc(); if (!Gf128Tab64Init ((unsigned char *) key.Get(), (GfCtx *) (GfContext.Ptr()))) throw bad_alloc(); Key.CopyFrom (key); KeySet = true; }
void RandomPoolEnrichmentDialog::ShowBytes (wxStaticText *textCtrl, const ConstBufferPtr &buffer) { wxString str; for (size_t i = 0; i < buffer.Size(); ++i) { str += wxString::Format (L"%02X", buffer[i]); } str += L".."; textCtrl->SetLabel (str.c_str()); for (size_t i = 0; i < str.size(); ++i) { str[i] = L'X'; } }
void VolumeCreationProgressWizardPage::ShowBytes (wxStaticText *textCtrl, const ConstBufferPtr &buffer, bool appendDots) { wxString str; for (size_t i = 0; i < MaxDisplayedKeyBytes && i < buffer.Size(); ++i) { str += wxString::Format (L"%02X", buffer[i]); } if (appendDots) str += L".."; textCtrl->SetLabel (str.c_str()); for (size_t i = 0; i < str.size(); ++i) { str[i] = L'X'; } }
void KeyfileGeneratorDialog::ShowBytes (wxStaticText *textCtrl, const ConstBufferPtr &buffer, bool appendDots) { wxString str; for (size_t i = 0; i < buffer.Size(); ++i) { str += wxString::Format (L"%02X", buffer[i]); } if (appendDots) str += L".."; textCtrl->SetLabel (str.c_str()); for (size_t i = 0; i < str.size(); ++i) { str[i] = L'X'; } }
shared_ptr <Buffer> FuseService::GetVolumeInfo () { shared_ptr <Stream> stream (new MemoryStream); { ScopeLock lock (OpenVolumeInfoMutex); OpenVolumeInfo.Set (*MountedVolume); OpenVolumeInfo.SlotNumber = SlotNumber; OpenVolumeInfo.Serialize (stream); } ConstBufferPtr infoBuf = dynamic_cast <MemoryStream&> (*stream); shared_ptr <Buffer> outBuf (new Buffer (infoBuf.Size())); outBuf->CopyFrom (infoBuf); return outBuf; }
void MemoryStream::Write (const ConstBufferPtr &data) { for (uint64 i = 0; i < data.Size(); i++) Data.push_back (data[i]); }
void VolumePassword::Set (const ConstBufferPtr &password) { Set (password, password.Size()); }
void Sha256::ProcessData (const ConstBufferPtr &data) { if_debug (ValidateDataParameters (data)); sha256_hash (data.Get(), (int) data.Size(), (sha256_ctx *) Context.Ptr()); }
void Ripemd160::ProcessData (const ConstBufferPtr &data) { if_debug (ValidateDataParameters (data)); RMD160Update ((RMD160_CTX *) Context.Ptr(), data.Get(), (int) data.Size()); }
void Hash::ValidateDataParameters (const ConstBufferPtr &data) const { if (data.Size() < 1) throw ParameterIncorrect (SRC_POS); }
MemoryStream::MemoryStream (const ConstBufferPtr &data) : ReadPosition (0) { Data = vector <byte> (data.Size()); BufferPtr (&Data[0], Data.size()).CopyFrom (data); }
bool VolumeHeader::Deserialize (const ConstBufferPtr &header, shared_ptr <EncryptionAlgorithm> &ea, shared_ptr <EncryptionMode> &mode, bool truecryptMode) { if (header.Size() != EncryptedHeaderDataSize) throw ParameterIncorrect (SRC_POS); if (truecryptMode && (header[0] != 'T' || header[1] != 'R' || header[2] != 'U' || header[3] != 'E')) return false; if (!truecryptMode && (header[0] != 'V' || header[1] != 'E' || header[2] != 'R' || header[3] != 'A')) return false; size_t offset = 4; HeaderVersion = DeserializeEntry <uint16> (header, offset); if (HeaderVersion < MinAllowedHeaderVersion) return false; if (HeaderVersion > CurrentHeaderVersion) throw HigherVersionRequired (SRC_POS); if (HeaderVersion >= 4 && Crc32::ProcessBuffer (header.GetRange (0, TC_HEADER_OFFSET_HEADER_CRC - TC_HEADER_OFFSET_MAGIC)) != DeserializeEntryAt <uint32> (header, TC_HEADER_OFFSET_HEADER_CRC - TC_HEADER_OFFSET_MAGIC)) { return false; } RequiredMinProgramVersion = DeserializeEntry <uint16> (header, offset); if (!truecryptMode && (RequiredMinProgramVersion > Version::Number())) throw HigherVersionRequired (SRC_POS); if (truecryptMode) { if (RequiredMinProgramVersion < 0x600 || RequiredMinProgramVersion > 0x71a) throw UnsupportedTrueCryptFormat (SRC_POS); RequiredMinProgramVersion = CurrentRequiredMinProgramVersion; } VolumeKeyAreaCrc32 = DeserializeEntry <uint32> (header, offset); VolumeCreationTime = DeserializeEntry <uint64> (header, offset); HeaderCreationTime = DeserializeEntry <uint64> (header, offset); HiddenVolumeDataSize = DeserializeEntry <uint64> (header, offset); mVolumeType = (HiddenVolumeDataSize != 0 ? VolumeType::Hidden : VolumeType::Normal); VolumeDataSize = DeserializeEntry <uint64> (header, offset); EncryptedAreaStart = DeserializeEntry <uint64> (header, offset); EncryptedAreaLength = DeserializeEntry <uint64> (header, offset); Flags = DeserializeEntry <uint32> (header, offset); SectorSize = DeserializeEntry <uint32> (header, offset); if (HeaderVersion < 5) SectorSize = TC_SECTOR_SIZE_LEGACY; if (SectorSize < TC_MIN_VOLUME_SECTOR_SIZE || SectorSize > TC_MAX_VOLUME_SECTOR_SIZE || SectorSize % ENCRYPTION_DATA_UNIT_SIZE != 0) { throw ParameterIncorrect (SRC_POS); } #if !(defined (TC_WINDOWS) || defined (TC_LINUX) || defined (TC_MACOSX)) if (SectorSize != TC_SECTOR_SIZE_LEGACY) throw UnsupportedSectorSize (SRC_POS); #endif offset = DataAreaKeyOffset; if (VolumeKeyAreaCrc32 != Crc32::ProcessBuffer (header.GetRange (offset, DataKeyAreaMaxSize))) return false; DataAreaKey.CopyFrom (header.GetRange (offset, DataKeyAreaMaxSize)); ea = ea->GetNew(); mode = mode->GetNew(); if (typeid (*mode) == typeid (EncryptionModeXTS)) { ea->SetKey (header.GetRange (offset, ea->GetKeySize())); mode->SetKey (header.GetRange (offset + ea->GetKeySize(), ea->GetKeySize())); } else { mode->SetKey (header.GetRange (offset, mode->GetKeySize())); ea->SetKey (header.GetRange (offset + LegacyEncryptionModeKeyAreaSize, ea->GetKeySize())); } ea->SetMode (mode); return true; }
void Whirlpool::ProcessData (const ConstBufferPtr &data) { if_debug (ValidateDataParameters (data)); WHIRLPOOL_add (data.Get(), (int) data.Size() * 8, (WHIRLPOOL_CTX *) Context.Ptr()); }
string Resources::GetLanguageXml () { #ifdef TC_WINDOWS ConstBufferPtr res = GetWindowsResource (L"XML", L"IDR_LANGUAGE"); Buffer strBuf (res.Size() + 1); strBuf.Zero(); strBuf.CopyFrom (res); return string (reinterpret_cast <char *> (strBuf.Ptr())); #else static const char LanguageXml[] = { // include "../Common/Language.xml.h" ??? it does not exist and the next is known wrong ??? # include "Common/Language.xml.h" , 0 }; return string (LanguageXml); #endif } string Resources::GetLegalNotices () { #ifdef TC_WINDOWS ConstBufferPtr res = GetWindowsResource (L"TEXT", L"IDR_LICENSE"); Buffer strBuf (res.Size() + 1); strBuf.Zero(); strBuf.CopyFrom (res); return string (reinterpret_cast <char *> (strBuf.Ptr())); #else static const char License[] = { # include "License.txt.h" , 0 }; return string (License); #endif } #ifndef TC_NO_GUI wxBitmap Resources::GetDriveIconBitmap () { #ifdef TC_WINDOWS return wxBitmap (L"IDB_DRIVE_ICON", wxBITMAP_TYPE_BMP_RESOURCE).ConvertToImage().Resize (wxSize (16, 12), wxPoint (0, 0)); #else static const byte DriveIcon[] = { # include "Mount/Drive_icon_96dpi.bmp.h" }; wxMemoryInputStream stream (DriveIcon, sizeof (DriveIcon)); return wxBitmap (wxImage (stream).Resize (wxSize (16, 12), wxPoint (0, 0))); #endif } wxBitmap Resources::GetDriveIconMaskBitmap () { #ifdef TC_WINDOWS wxImage image = wxBitmap (L"IDB_DRIVE_ICON_MASK", wxBITMAP_TYPE_BMP_RESOURCE).ConvertToImage().Resize (wxSize (16, 12), wxPoint (0, 0)); return wxBitmap (image.ConvertToMono (0, 0, 0), 1); #else static const byte DriveIconMask[] = { # include "Mount/Drive_icon_mask_96dpi.bmp.h" }; wxMemoryInputStream stream (DriveIconMask, sizeof (DriveIconMask)); wxImage image (stream); image.Resize (wxSize (16, 12), wxPoint (0, 0)); # ifdef __WXGTK__ return wxBitmap (image.ConvertToMono (0, 0, 0), 1); # else return wxBitmap (image); # endif #endif } wxBitmap Resources::GetLogoBitmap () { #ifdef TC_WINDOWS return wxBitmap (L"IDB_LOGO", wxBITMAP_TYPE_BMP_RESOURCE); #else static const byte Logo[] = { # include "Mount/Logo_96dpi.bmp.h" }; wxMemoryInputStream stream (Logo, sizeof (Logo)); return wxBitmap (wxImage (stream)); #endif } wxBitmap Resources::GetTextualLogoBitmap () { #ifdef TC_WINDOWS return wxBitmap (L"IDB_TEXTUAL_LOGO", wxBITMAP_TYPE_BMP_RESOURCE); #else static const byte Logo[] = { # include "Common/Textual_logo_96dpi.bmp.h" }; wxMemoryInputStream stream (Logo, sizeof (Logo)); return wxBitmap (wxImage (stream)); #endif } wxIcon Resources::GetCipherShedIcon () { #ifdef TC_WINDOWS return wxIcon (L"IDI_CIPHERSHED_ICON", wxBITMAP_TYPE_ICO_RESOURCE, 16, 16); #else # include "Resources/Icons/CipherShed-16x16.xpm" return wxIcon (CipherShedIcon16x16); #endif } wxBitmap Resources::GetVolumeCreationWizardBitmap (int height) { #ifdef TC_WINDOWS return wxBitmap (L"IDB_VOLUME_WIZARD_BITMAP", wxBITMAP_TYPE_BMP_RESOURCE); #else static const byte VolumeWizardIcon[] = { # include "Format/CipherShed_Wizard.bmp.h" }; wxMemoryInputStream stream (VolumeWizardIcon, sizeof (VolumeWizardIcon)); wxImage image (stream); if (height != -1) { double scaleFactor = double (height) / double (image.GetHeight()); image.Rescale (int (image.GetWidth() * scaleFactor), int (image.GetHeight() * scaleFactor), wxIMAGE_QUALITY_HIGH); } return wxBitmap (image); #endif } #endif // !TC_NO_GUI }