"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/Cache.c" (10 Oct 2018, 3211 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 "Cache.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  Legal Notice: Some portions of the source code contained in this file were
    3  derived from the source code of TrueCrypt 7.1a, which is
    4  Copyright (c) 2003-2012 TrueCrypt Developers Association and which is
    5  governed by the TrueCrypt License 3.0, also from the source code of
    6  Encryption for the Masses 2.02a, which is Copyright (c) 1998-2000 Paul Le Roux
    7  and which is governed by the 'License Agreement for Encryption for the Masses'
    8  Modifications and additions to the original source code (contained in this file)
    9  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
   10  and are governed by the Apache License 2.0 the full text of which is
   11  contained in the file License.txt included in VeraCrypt binary and source
   12  code distribution packages. */
   13 
   14 #include "Tcdefs.h"
   15 #include "Crypto.h"
   16 #include "Fat.h"
   17 #include "Volumes.h"
   18 #include "Apidrvr.h"
   19 #include "Common.h"
   20 #include "Cache.h"
   21 
   22 Password CachedPasswords[CACHE_SIZE];
   23 int  CachedPim[CACHE_SIZE];
   24 int cacheEmpty = 1;
   25 static int nPasswordIdx = 0;
   26 
   27 int ReadVolumeHeaderWCache (BOOL bBoot, BOOL bCache, BOOL bCachePim, char *header, Password *password, int pkcs5_prf, int pim, BOOL truecryptMode, PCRYPTO_INFO *retInfo)
   28 {
   29     int nReturnCode = ERR_PASSWORD_WRONG;
   30     int i, effectivePim;
   31 
   32     /* Attempt to recognize volume using mount password */
   33     if (password->Length > 0)
   34     {
   35         nReturnCode = ReadVolumeHeader (bBoot, header, password, pkcs5_prf, pim, truecryptMode, retInfo, NULL);
   36 
   37         /* Save mount passwords back into cache if asked to do so */
   38         if (bCache && (nReturnCode == 0 || nReturnCode == ERR_CIPHER_INIT_WEAK_KEY))
   39         {
   40             for (i = 0; i < CACHE_SIZE; i++)
   41             {
   42                 if (memcmp (&CachedPasswords[i], password, sizeof (Password)) == 0)
   43                     break;
   44             }
   45 
   46             if (i == CACHE_SIZE)
   47             {
   48                 /* Store the password */
   49                 CachedPasswords[nPasswordIdx] = *password;
   50 
   51                 /* Store also PIM if requested, otherwise set to default */
   52                 if (bCachePim && (pim > 0))
   53                     CachedPim[nPasswordIdx] = pim;
   54                 else
   55                     CachedPim[nPasswordIdx] = 0;
   56 
   57                 /* Try another slot */
   58                 nPasswordIdx = (nPasswordIdx + 1) % CACHE_SIZE;
   59 
   60                 cacheEmpty = 0;
   61             }
   62             else if (bCachePim)
   63             {
   64                 CachedPim[i] = pim > 0? pim : 0;
   65             }
   66         }
   67     }
   68     else if (!cacheEmpty)
   69     {
   70         /* Attempt to recognize volume using cached passwords */
   71         for (i = 0; i < CACHE_SIZE; i++)
   72         {
   73             if (CachedPasswords[i].Length > 0)
   74             {
   75                 if (truecryptMode)
   76                     effectivePim = 0;
   77                 else if (pim == -1)
   78                     effectivePim = CachedPim[i];
   79                 else
   80                     effectivePim = pim;
   81                 nReturnCode = ReadVolumeHeader (bBoot, header, &CachedPasswords[i], pkcs5_prf, effectivePim, truecryptMode, retInfo, NULL);
   82 
   83                 if (nReturnCode != ERR_PASSWORD_WRONG)
   84                     break;
   85             }
   86         }
   87     }
   88 
   89     return nReturnCode;
   90 }
   91 
   92 
   93 void AddPasswordToCache (Password *password, int pim)
   94 {
   95     int i;
   96     for (i = 0; i < CACHE_SIZE; i++)
   97     {
   98         if (memcmp (&CachedPasswords[i], password, sizeof (Password)) == 0)
   99             return;
  100     }
  101 
  102     CachedPasswords[nPasswordIdx] = *password;
  103     CachedPim[nPasswordIdx] = pim > 0? pim : 0;
  104     nPasswordIdx = (nPasswordIdx + 1) % CACHE_SIZE;
  105     cacheEmpty = 0;
  106 }
  107 
  108 
  109 void WipeCache ()
  110 {
  111     burn (CachedPasswords, sizeof (CachedPasswords));
  112     burn (CachedPim, sizeof (CachedPim));
  113     nPasswordIdx = 0;
  114     cacheEmpty = 1;
  115 }