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 VolumeHeader::Serialize (const BufferPtr &header) const { if (header.Size() != EncryptedHeaderDataSize) throw ParameterIncorrect (SRC_POS); header.Zero(); header[0] = 'V'; header[1] = 'E'; header[2] = 'R'; header[3] = 'A'; size_t offset = 4; header.GetRange (DataAreaKeyOffset, DataAreaKey.Size()).CopyFrom (DataAreaKey); uint16 headerVersion = CurrentHeaderVersion; SerializeEntry (headerVersion, header, offset); SerializeEntry (RequiredMinProgramVersion, header, offset); SerializeEntry (Crc32::ProcessBuffer (header.GetRange (DataAreaKeyOffset, DataKeyAreaMaxSize)), header, offset); uint64 reserved64 = 0; SerializeEntry (reserved64, header, offset); SerializeEntry (reserved64, header, offset); SerializeEntry (HiddenVolumeDataSize, header, offset); SerializeEntry (VolumeDataSize, header, offset); SerializeEntry (EncryptedAreaStart, header, offset); SerializeEntry (EncryptedAreaLength, header, offset); SerializeEntry (Flags, header, offset); if (SectorSize < TC_MIN_VOLUME_SECTOR_SIZE || SectorSize > TC_MAX_VOLUME_SECTOR_SIZE || SectorSize % ENCRYPTION_DATA_UNIT_SIZE != 0) { throw ParameterIncorrect (SRC_POS); } SerializeEntry (SectorSize, header, offset); offset = TC_HEADER_OFFSET_HEADER_CRC - TC_HEADER_OFFSET_MAGIC; SerializeEntry (Crc32::ProcessBuffer (header.GetRange (0, TC_HEADER_OFFSET_HEADER_CRC - TC_HEADER_OFFSET_MAGIC)), header, offset); }
void File::ReadCompleteBuffer (const BufferPtr &buffer) const { size_t dataLeft = buffer.Size(); size_t offset = 0; while (dataLeft > 0) { size_t dataRead = static_cast <size_t> (Read (buffer.GetRange (offset, dataLeft))); if (dataRead == 0) throw InsufficientData (SRC_POS); dataLeft -= dataRead; offset += dataRead; } }