"Fossies" - the Fresh Open Source Software Archive

Member "src/Main/Forms/KeyfileGeneratorDialog.cpp" (10 Oct 2018, 6393 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 "KeyfileGeneratorDialog.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 "System.h"
   14 #include "Main/GraphicUserInterface.h"
   15 #include "Volume/Hash.h"
   16 #include "KeyfileGeneratorDialog.h"
   17 
   18 namespace VeraCrypt
   19 {
   20     KeyfileGeneratorDialog::KeyfileGeneratorDialog (wxWindow* parent) : KeyfileGeneratorDialogBase (parent)
   21     {
   22         RandomNumberGenerator::Start();
   23 
   24         Hashes = Hash::GetAvailableAlgorithms();
   25         foreach (shared_ptr <Hash> hash, Hashes)
   26         {
   27             if (!hash->IsDeprecated())
   28                 HashChoice->Append (hash->GetName(), hash.get());
   29         }
   30 
   31         HashChoice->Select (0);
   32         RandomNumberGenerator::SetHash (Gui->GetSelectedData <Hash> (HashChoice)->GetNew());
   33 
   34         HideBytes (RandomPoolStaticText, 24);
   35         MouseStaticText->Wrap (Gui->GetCharWidth (MouseStaticText) * 70);
   36 
   37         CollectedEntropy->SetRange (RNG_POOL_SIZE * 8);
   38 
   39         MainSizer->SetMinSize (wxSize (-1, Gui->GetCharHeight (this) * 24));
   40 
   41         Layout();
   42         Fit();
   43         Center();
   44 
   45         MouseEventsCounter = 0;
   46 
   47         foreach (wxWindow *c, this->GetChildren())
   48             c->Connect (wxEVT_MOTION, wxMouseEventHandler (KeyfileGeneratorDialog::OnMouseMotion), nullptr, this);
   49     }
   50 
   51     KeyfileGeneratorDialog::~KeyfileGeneratorDialog ()
   52     {
   53     }
   54 
   55     void KeyfileGeneratorDialog::OnGenerateButtonClick (wxCommandEvent& event)
   56     {
   57         try
   58         {
   59             int keyfilesCount = NumberOfKeyfiles->GetValue();
   60             int keyfilesSize = KeyfilesSize->GetValue();
   61             bool useRandomSize = RandomSizeCheckBox->IsChecked();
   62             wxString keyfileBaseName = KeyfilesBaseName->GetValue();
   63             keyfileBaseName.Trim(true);
   64             keyfileBaseName.Trim(false);
   65 
   66             if (keyfileBaseName.IsEmpty())
   67             {
   68                 Gui->ShowWarning("KEYFILE_EMPTY_BASE_NAME");
   69                 return;
   70             }
   71 
   72             wxFileName baseFileName = wxFileName::FileName (keyfileBaseName);
   73             if (!baseFileName.IsOk())
   74             {
   75                 Gui->ShowWarning("KEYFILE_INVALID_BASE_NAME");
   76                 return;
   77             }
   78 
   79             DirectoryPath keyfilesDir = Gui->SelectDirectory (Gui->GetActiveWindow(), LangString["SELECT_KEYFILE_GENERATION_DIRECTORY"], false);
   80             if (keyfilesDir.IsEmpty())
   81                 return;
   82 
   83             wxFileName dirFileName = wxFileName::DirName( wstring(keyfilesDir).c_str() );
   84             if (!dirFileName.IsDirWritable ())
   85             {
   86                 Gui->ShowWarning(L"You don't have write permission on the selected directory");
   87                 return;
   88             }
   89 
   90             wxBusyCursor busy;
   91             for (int i = 0; i < keyfilesCount; i++)
   92             {
   93                 int bufferLen;
   94                 if (useRandomSize)
   95                 {
   96                     SecureBuffer sizeBuffer (sizeof(int));
   97                     RandomNumberGenerator::GetData (sizeBuffer, true);
   98 
   99                     memcpy(&bufferLen, sizeBuffer.Ptr(), sizeof(int));
  100 
  101                     /* since keyfilesSize < 1024 * 1024, we mask with 0x000FFFFF */
  102                     bufferLen = (long) (((unsigned long) bufferLen) & 0x000FFFFF);
  103 
  104                     bufferLen %= ((1024*1024 - 64) + 1);
  105                     bufferLen += 64;
  106                 }
  107                 else
  108                     bufferLen = keyfilesSize;
  109 
  110                 SecureBuffer keyfileBuffer (bufferLen);
  111                 RandomNumberGenerator::GetData (keyfileBuffer, true);
  112 
  113                 wstringstream convertStream;
  114                 convertStream << i;
  115                 wxString suffix = L"_";
  116                 suffix += convertStream.str().c_str();
  117 
  118                 wxFileName keyfileName;
  119                 if (i == 0)
  120                 {
  121                     keyfileName.Assign(dirFileName.GetPath(), keyfileBaseName);
  122                 }
  123                 else
  124                 {
  125                     if (baseFileName.HasExt())
  126                     {
  127                         keyfileName.Assign(dirFileName.GetPath(), baseFileName.GetName() + suffix + L"." + baseFileName.GetExt());
  128                     }
  129                     else
  130                     {
  131                         keyfileName.Assign(dirFileName.GetPath(), keyfileBaseName + suffix);
  132                     }
  133                 }
  134 
  135                 if (keyfileName.Exists())
  136                 {
  137                     wxString msg = wxString::Format(LangString["KEYFILE_ALREADY_EXISTS"], keyfileName.GetFullPath());
  138                     if (!Gui->AskYesNo (msg, false, true))
  139                         return;
  140                 }
  141 
  142                 {
  143                     FilePath keyfilePath((const wchar_t*) keyfileName.GetFullPath().c_str());
  144                     File keyfile;
  145                     keyfile.Open (keyfilePath, File::CreateWrite);
  146                     keyfile.Write (keyfileBuffer);
  147                 }
  148 
  149             }
  150             Gui->ShowInfo ("KEYFILE_CREATED");
  151         }
  152         catch (exception &e)
  153         {
  154             Gui->ShowError (e);
  155         }
  156     }
  157 
  158     void KeyfileGeneratorDialog::OnHashSelected (wxCommandEvent& event)
  159     {
  160         RandomNumberGenerator::SetHash (Gui->GetSelectedData <Hash> (HashChoice)->GetNew());
  161     }
  162 
  163     void KeyfileGeneratorDialog::OnMouseMotion (wxMouseEvent& event)
  164     {
  165         event.Skip();
  166 
  167         RandomNumberGenerator::AddToPool (ConstBufferPtr (reinterpret_cast <byte *> (&event), sizeof (event)));
  168 
  169         long coord = event.GetX();
  170         RandomNumberGenerator::AddToPool (ConstBufferPtr (reinterpret_cast <byte *> (&coord), sizeof (coord)));
  171         coord = event.GetY();
  172         RandomNumberGenerator::AddToPool (ConstBufferPtr (reinterpret_cast <byte *> (&coord), sizeof (coord)));
  173 
  174         if (ShowRandomPoolCheckBox->IsChecked())
  175             ShowBytes (RandomPoolStaticText, RandomNumberGenerator::PeekPool().GetRange (0, 24));
  176         else
  177             HideBytes (RandomPoolStaticText, 24);
  178 
  179         /* conservative estimate: 1 mouse move event brings 1 bit of entropy
  180          * https://security.stackexchange.com/questions/32844/for-how-much-time-should-i-randomly-move-the-mouse-for-generating-encryption-key/32848#32848
  181          */
  182         ScopeLock lock (AccessMutex);
  183         if (MouseEventsCounter < (RNG_POOL_SIZE * 8))
  184             CollectedEntropy->SetValue (++MouseEventsCounter);
  185     }
  186 
  187     void KeyfileGeneratorDialog::OnShowRandomPoolCheckBoxClicked (wxCommandEvent& event)
  188     {
  189         if (!event.IsChecked())
  190             HideBytes (RandomPoolStaticText, 24);
  191     }
  192 
  193     void KeyfileGeneratorDialog::OnRandomSizeCheckBoxClicked (wxCommandEvent& event)
  194     {
  195         if (!event.IsChecked())
  196             KeyfilesSize->Enable();
  197         else
  198             KeyfilesSize->Disable();
  199     }
  200 
  201     void KeyfileGeneratorDialog::ShowBytes (wxStaticText *textCtrl, const ConstBufferPtr &buffer, bool appendDots)
  202     {
  203         wxString str;
  204 
  205         for (size_t i = 0; i < buffer.Size(); ++i)
  206         {
  207             str += wxString::Format (L"%02X", buffer[i]);
  208         }
  209 
  210         if (appendDots)
  211             str += L"..";
  212 
  213         textCtrl->SetLabel (str.c_str());
  214 
  215         for (size_t i = 0; i < str.size(); ++i)
  216         {
  217             str[i] = L'X';
  218         }
  219     }
  220 
  221     void KeyfileGeneratorDialog::HideBytes (wxStaticText *textCtrl, size_t len)
  222     {
  223         wxString str;
  224 
  225         for (size_t i = 0; i < len + 1; ++i)
  226         {
  227             str += L"**";
  228         }
  229 
  230         textCtrl->SetLabel (str.c_str());
  231     }
  232 }