"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/Wipe.c" (10 Oct 2018, 3633 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 "Wipe.c" 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 "Tcdefs.h"
   14 #include "Wipe.h"
   15 
   16 
   17 static BOOL Wipe1PseudoRandom (int pass, byte *buffer, size_t size)
   18 {
   19     return FALSE;
   20 }
   21 
   22 
   23 // Fill buffer with wipe patterns defined in "National Industrial Security Program Operating Manual", US DoD 5220.22-M.
   24 // Return: FALSE = buffer must be filled with random data
   25 
   26 static BOOL Wipe3Dod5220 (int pass, byte *buffer, size_t size)
   27 {
   28     byte wipeChar;
   29 
   30     switch (pass)
   31     {
   32     case 1:
   33         wipeChar = 0;
   34         break;
   35 
   36     case 2:
   37         wipeChar = 0xff;
   38         break;
   39 
   40     default:
   41         return FALSE;
   42     }
   43 
   44     memset (buffer, wipeChar, size);
   45     return TRUE;
   46 }
   47 
   48 
   49 static BOOL Wipe7Dod5220 (int pass, byte randChars[TC_WIPE_RAND_CHAR_COUNT], byte *buffer, size_t size)
   50 {
   51     byte wipeChar;
   52 
   53     switch (pass)
   54     {
   55     case 1:
   56         wipeChar = randChars[0];
   57         break;
   58 
   59     case 2:
   60         wipeChar = ~randChars[0];
   61         break;
   62 
   63     case 4:
   64         wipeChar = randChars[1];
   65         break;
   66 
   67     case 5:
   68         wipeChar = randChars[2];
   69         break;
   70 
   71     case 6:
   72         wipeChar = ~randChars[2];
   73         break;
   74 
   75     default:
   76         return FALSE;
   77     }
   78 
   79     memset (buffer, wipeChar, size);
   80     return TRUE;
   81 }
   82 
   83 
   84 // Fill the buffer with wipe patterns defined in the paper "Secure Deletion of Data from Magnetic and Solid-State Memory" by Peter Gutmann.
   85 // Return: FALSE = buffer must be filled with random data
   86 
   87 static BOOL Wipe35Gutmann (int pass, byte *buffer, size_t size)
   88 {
   89     byte wipePat3[] = { 0x92, 0x49, 0x24 };
   90     int wipePat3Pos;
   91     size_t i;
   92 
   93     switch (pass)
   94     {
   95     case 5:
   96         memset (buffer, 0x55, size);
   97         break;
   98 
   99     case 6:
  100         memset (buffer, 0xaa, size);
  101         break;
  102 
  103     case 7:
  104     case 26:
  105     case 29:
  106         wipePat3Pos = 0;
  107         goto wipe3;
  108 
  109     case 8:
  110     case 27:
  111     case 30:
  112         wipePat3Pos = 1;
  113         goto wipe3;
  114 
  115     case 9:
  116     case 28:
  117     case 31:
  118         wipePat3Pos = 2;
  119         goto wipe3;
  120 
  121 wipe3:
  122         if (pass >= 29)
  123         {
  124             wipePat3[0] = ~wipePat3[0];
  125             wipePat3[1] = ~wipePat3[1];
  126             wipePat3[2] = ~wipePat3[2];
  127         }
  128 
  129         for (i = 0; i < size; ++i)
  130         {
  131             buffer[i] = wipePat3[wipePat3Pos++ % 3];
  132         }
  133         break;
  134 
  135     default:
  136         if (pass >= 10 && pass <= 25)
  137             memset (buffer, (pass - 10) * 0x11, size);
  138         else
  139             return FALSE;
  140     }
  141 
  142     return TRUE;
  143 }
  144 
  145 
  146 int GetWipePassCount (WipeAlgorithmId algorithm)
  147 {
  148     switch (algorithm)
  149     {
  150     case TC_WIPE_1_RAND:
  151         return 1;
  152 
  153     case TC_WIPE_3_DOD_5220:
  154         return 3;
  155 
  156     case TC_WIPE_7_DOD_5220:
  157         return 7;
  158 
  159     case TC_WIPE_35_GUTMANN:
  160         return 35;
  161 
  162     case TC_WIPE_256:
  163         return 256;
  164     }
  165 
  166     return -1;  // Prevent compiler warnings
  167 }
  168 
  169 
  170 BOOL WipeBuffer (WipeAlgorithmId algorithm, byte randChars[TC_WIPE_RAND_CHAR_COUNT], int pass, byte *buffer, size_t size)
  171 {
  172     switch (algorithm)
  173     {
  174     case TC_WIPE_1_RAND:
  175     case TC_WIPE_256:
  176         return Wipe1PseudoRandom (pass, buffer, size);
  177 
  178     case TC_WIPE_3_DOD_5220:
  179         return Wipe3Dod5220 (pass, buffer, size);
  180 
  181     case TC_WIPE_7_DOD_5220:
  182         return Wipe7Dod5220 (pass, randChars, buffer, size);
  183 
  184     case TC_WIPE_35_GUTMANN:
  185         return Wipe35Gutmann (pass, buffer, size);
  186 
  187     /* we will never reach here because all calls to WipeBuffer are preceeded
  188      * by a call to GetWipePassCount that already checks the same algorithm
  189      * parameters and in case of unsupported value an error is returned before
  190      * calling WipeBuffer
  191      */
  192    /*
  193     default:
  194         TC_THROW_FATAL_EXCEPTION;*/
  195     }
  196 
  197     return FALSE;   // Prevent compiler warnings
  198 }