|
|
1.1 root 1: /*
2: Copyright (c) 2008 TrueCrypt Foundation. All rights reserved.
3:
1.1.1.3 ! root 4: Governed by the TrueCrypt License 2.5 the full text of which is contained
1.1 root 5: in the file License.txt included in TrueCrypt binary and source code
6: distribution packages.
7: */
8:
9: #include "Crypto.h"
10: #include "Platform.h"
11: #include "BootConfig.h"
12: #include "BootDebug.h"
1.1.1.2 root 13: #include "BootDefs.h"
1.1 root 14: #include "BootDiskIo.h"
15: #include "BootEncryptedIo.h"
16:
17:
18: BiosResult ReadEncryptedSectors (uint16 destSegment, uint16 destOffset, byte drive, uint64 sector, uint16 sectorCount)
19: {
20: BiosResult result;
1.1.1.3 ! root 21: bool decrypt = true;
! 22:
! 23: if (BootCryptoInfo->hiddenVolume)
! 24: {
! 25: if (ReadWritePartiallyCoversEncryptedArea (sector, sectorCount))
! 26: return BiosResultInvalidFunction;
! 27:
! 28: if (sector >= EncryptedVirtualPartition.StartSector && sector <= EncryptedVirtualPartition.EndSector)
! 29: {
! 30: // Remap the request to the hidden volume
! 31: sector -= EncryptedVirtualPartition.StartSector;
! 32: sector += HiddenVolumeStartSector;
! 33: }
! 34: else
! 35: decrypt = false;
! 36: }
1.1 root 37:
1.1.1.2 root 38: result = ReadSectors (destSegment, destOffset, drive, sector, sectorCount);
1.1 root 39:
1.1.1.3 ! root 40: if (result != BiosResultSuccess || !decrypt)
1.1.1.2 root 41: return result;
1.1 root 42:
1.1.1.3 ! root 43: if (BootCryptoInfo->hiddenVolume)
! 44: {
! 45: // Convert sector number to data unit number of the hidden volume
! 46: sector -= HiddenVolumeStartSector;
! 47: sector += HiddenVolumeStartUnitNo;
! 48: }
! 49:
1.1.1.2 root 50: if (drive == EncryptedVirtualPartition.Drive)
51: {
52: while (sectorCount-- > 0)
1.1 root 53: {
1.1.1.3 ! root 54: if (BootCryptoInfo->hiddenVolume
! 55: || (sector >= EncryptedVirtualPartition.StartSector && sector <= EncryptedVirtualPartition.EndSector))
1.1.1.2 root 56: {
57: AcquireSectorBuffer();
58: CopyMemory (destSegment, destOffset, SectorBuffer, TC_LB_SIZE);
59:
60: DecryptDataUnits (SectorBuffer, §or, 1, BootCryptoInfo);
61:
62: CopyMemory (SectorBuffer, destSegment, destOffset, TC_LB_SIZE);
63: ReleaseSectorBuffer();
64: }
1.1 root 65:
1.1.1.2 root 66: ++sector;
67: destOffset += TC_LB_SIZE;
68: }
1.1 root 69: }
70:
71: return result;
72: }
73:
74:
75: BiosResult WriteEncryptedSectors (uint16 sourceSegment, uint16 sourceOffset, byte drive, uint64 sector, uint16 sectorCount)
76: {
77: BiosResult result;
1.1.1.2 root 78: AcquireSectorBuffer();
1.1.1.3 ! root 79: uint64 dataUnitNo;
! 80: uint64 writeOffset;
! 81:
! 82: dataUnitNo = sector;
! 83: writeOffset.HighPart = 0;
! 84: writeOffset.LowPart = 0;
! 85:
! 86: if (BootCryptoInfo->hiddenVolume)
! 87: {
! 88: if (ReadWritePartiallyCoversEncryptedArea (sector, sectorCount))
! 89: return BiosResultInvalidFunction;
! 90:
! 91: // Remap the request to the hidden volume
! 92: writeOffset = HiddenVolumeStartSector;
! 93: writeOffset -= EncryptedVirtualPartition.StartSector;
! 94: dataUnitNo -= EncryptedVirtualPartition.StartSector;
! 95: dataUnitNo += HiddenVolumeStartUnitNo;
! 96: }
1.1 root 97:
98: while (sectorCount-- > 0)
99: {
1.1.1.2 root 100: CopyMemory (sourceSegment, sourceOffset, SectorBuffer, TC_LB_SIZE);
1.1 root 101:
102: if (drive == EncryptedVirtualPartition.Drive && sector >= EncryptedVirtualPartition.StartSector && sector <= EncryptedVirtualPartition.EndSector)
103: {
1.1.1.3 ! root 104: EncryptDataUnits (SectorBuffer, &dataUnitNo, 1, BootCryptoInfo);
1.1 root 105: }
106:
1.1.1.3 ! root 107: result = WriteSectors (SectorBuffer, drive, sector + writeOffset, 1);
1.1 root 108:
109: if (result != BiosResultSuccess)
1.1.1.2 root 110: break;
1.1 root 111:
112: ++sector;
1.1.1.3 ! root 113: ++dataUnitNo;
1.1.1.2 root 114: sourceOffset += TC_LB_SIZE;
1.1 root 115: }
116:
1.1.1.2 root 117: ReleaseSectorBuffer();
1.1 root 118: return result;
119: }
1.1.1.3 ! root 120:
! 121:
! 122: static bool ReadWritePartiallyCoversEncryptedArea (const uint64 §or, uint16 sectorCount)
! 123: {
! 124: uint64 readWriteEnd = sector + --sectorCount;
! 125:
! 126: return ((sector < EncryptedVirtualPartition.StartSector && readWriteEnd >= EncryptedVirtualPartition.StartSector)
! 127: || (sector >= EncryptedVirtualPartition.StartSector && readWriteEnd > EncryptedVirtualPartition.EndSector));
! 128: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.