"Fossies" - the Fresh Open Source Software Archive

Member "src/Boot/Windows/BootEncryptedIo.cpp" (10 Oct 2018, 3774 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "BootEncryptedIo.cpp" see the Fossies "Dox" file reference documentation.

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