"Fossies" - the Fresh Open Source Software Archive

Member "src/Main/Forms/EncryptionTestDialog.cpp" (10 Oct 2018, 5891 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 "EncryptionTestDialog.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 "Volume/EncryptionModeXTS.h"
   15 #include "Volume/EncryptionTest.h"
   16 #include "Main/GraphicUserInterface.h"
   17 #include "EncryptionTestDialog.h"
   18 
   19 namespace VeraCrypt
   20 {
   21     EncryptionTestDialog::EncryptionTestDialog (wxWindow* parent)
   22         : EncryptionTestDialogBase (parent)
   23     {
   24         EncryptionAlgorithms = EncryptionAlgorithm::GetAvailableAlgorithms();
   25         foreach (shared_ptr <EncryptionAlgorithm> ea, EncryptionAlgorithms)
   26         {
   27             if (!ea->IsDeprecated())
   28                 EncryptionAlgorithmChoice->Append (ea->GetName(true), ea.get());
   29         }
   30 
   31         EncryptionAlgorithmChoice->Select (0);
   32         Reset();
   33 
   34         Fit();
   35         Layout();
   36         Center();
   37     }
   38 
   39     void EncryptionTestDialog::EncryptOrDecrypt (bool encrypt)
   40     {
   41         try
   42         {
   43             bool xts = XtsModeCheckBox->IsChecked();
   44 
   45             shared_ptr <EncryptionAlgorithm> ea = GetSelectedEncryptionAlgorithm();
   46 
   47             Buffer key;
   48             GetTextCtrlData (KeyTextCtrl, key);
   49 
   50             if (key.Size() != ea->GetKeySize())
   51                 throw_err (LangString["TEST_KEY_SIZE"]);
   52 
   53             ea->SetKey (key);
   54 
   55             Buffer data;
   56             GetTextCtrlData (encrypt ? PlainTextTextCtrl : CipherTextTextCtrl, data);
   57 
   58             if (data.Size() != ea->GetMaxBlockSize())
   59                 throw_err (LangString[encrypt ? "TEST_PLAINTEXT_SIZE" : "TEST_CIPHERTEXT_SIZE"]);
   60 
   61             if (xts)
   62             {
   63                 Buffer secondaryKey;
   64                 GetTextCtrlData (SecondaryKeyTextCtrl, secondaryKey);
   65 
   66                 if (secondaryKey.Size() != ea->GetKeySize())
   67                     throw_err (LangString["TEST_INCORRECT_SECONDARY_KEY_SIZE"]);
   68 
   69                 uint64 dataUnitNumber;
   70                 size_t blockNumber;
   71 
   72                 try
   73                 {
   74                     dataUnitNumber = StringConverter::ToUInt64 (wstring (DataUnitNumberTextCtrl->GetValue()));
   75                 }
   76                 catch (...)
   77                 {
   78                     DataUnitNumberTextCtrl->SetFocus();
   79                     throw StringConversionFailed (SRC_POS);
   80                 }
   81 
   82                 try
   83                 {
   84                     blockNumber = StringConverter::ToUInt32 (wstring (BlockNumberTextCtrl->GetValue()));
   85                     if (blockNumber > 31)
   86                     {
   87                         blockNumber = 31;
   88                         BlockNumberTextCtrl->SetValue (L"31");
   89                     }
   90                 }
   91                 catch (...)
   92                 {
   93                     BlockNumberTextCtrl->SetFocus();
   94                     throw StringConversionFailed (SRC_POS);
   95                 }
   96 
   97                 shared_ptr <EncryptionMode> xts (new EncryptionModeXTS);
   98                 xts->SetKey (secondaryKey);
   99                 ea->SetMode (xts);
  100 
  101                 Buffer sector (ENCRYPTION_DATA_UNIT_SIZE);
  102                 BufferPtr block = sector.GetRange (blockNumber * ea->GetMaxBlockSize(), ea->GetMaxBlockSize());
  103 
  104                 block.CopyFrom (data);
  105 
  106                 if (encrypt)
  107                     ea->EncryptSectors (sector, dataUnitNumber, 1, sector.Size());
  108                 else
  109                     ea->DecryptSectors (sector, dataUnitNumber, 1, sector.Size());
  110 
  111                 data.CopyFrom (block);
  112             }
  113             else
  114             {
  115                 if (encrypt)
  116                     ea->GetCiphers().front()->EncryptBlock (data);
  117                 else
  118                     ea->GetCiphers().front()->DecryptBlock (data);
  119             }
  120 
  121             SetTextCtrlData (encrypt ? CipherTextTextCtrl : PlainTextTextCtrl, data);
  122         }
  123         catch (exception &e)
  124         {
  125             Gui->ShowError (e);
  126         }
  127     }
  128 
  129     shared_ptr <EncryptionAlgorithm> EncryptionTestDialog::GetSelectedEncryptionAlgorithm () const
  130     {
  131         return Gui->GetSelectedData <EncryptionAlgorithm> (EncryptionAlgorithmChoice)->GetNew();
  132     }
  133 
  134     void EncryptionTestDialog::GetTextCtrlData (wxTextCtrl *textCtrl, Buffer &buffer) const
  135     {
  136         vector <byte> data;
  137         string dataStr = StringConverter::ToSingle (wstring (textCtrl->GetValue()));
  138 
  139         for (size_t i = 0; i < dataStr.size() / 2; ++i)
  140         {
  141             unsigned int dataByte;
  142             if (sscanf (dataStr.substr (i * 2, 2).c_str(), "%x", &dataByte) != 1)
  143             {
  144                 textCtrl->SetFocus();
  145                 throw StringConversionFailed (SRC_POS);
  146             }
  147 
  148             data.push_back ((byte) dataByte);
  149         }
  150 
  151         if (data.empty())
  152             return;
  153 
  154         buffer.CopyFrom (ConstBufferPtr (&data.front(), data.size()));
  155     }
  156 
  157     void EncryptionTestDialog::OnAutoTestAllButtonClick (wxCommandEvent& event)
  158     {
  159         try
  160         {
  161             {
  162                 wxBusyCursor busy;
  163                 EncryptionTest::TestAll();
  164             }
  165 
  166             Gui->ShowInfo ("TESTS_PASSED");
  167         }
  168         catch (Exception &e)
  169         {
  170             Gui->ShowError (e);
  171             Gui->ShowError ("TESTS_FAILED");
  172         }
  173     }
  174 
  175     void EncryptionTestDialog::OnEncryptionAlgorithmSelected ()
  176     {
  177         shared_ptr <EncryptionAlgorithm> ea = GetSelectedEncryptionAlgorithm();
  178 
  179         KeySizeStaticText->SetLabel (StringFormatter (L"{0} {1}", (uint32) ea->GetKeySize() * 8, LangString["BITS"]));
  180 
  181         Buffer key (ea->GetKeySize());
  182         key.Zero();
  183         SetTextCtrlData (KeyTextCtrl, key);
  184         SetTextCtrlData (SecondaryKeyTextCtrl, key);
  185 
  186         Buffer block (ea->GetMaxBlockSize());
  187         block.Zero();
  188         SetTextCtrlData (PlainTextTextCtrl, block);
  189         SetTextCtrlData (CipherTextTextCtrl, block);
  190 
  191         if (ea->GetCiphers().size() > 1)
  192         {
  193             XtsModeCheckBox->Disable();
  194             XtsModeCheckBox->SetValue (true);
  195             SecondaryKeyTextCtrl->Enable (true);
  196             DataUnitNumberTextCtrl->Enable (true);
  197             BlockNumberTextCtrl->Enable (true);
  198         }
  199         else
  200             XtsModeCheckBox->Enable();
  201     }
  202 
  203     void EncryptionTestDialog::OnXtsModeCheckBoxClick (wxCommandEvent& event)
  204     {
  205         bool enabled = event.IsChecked();
  206         SecondaryKeyTextCtrl->Enable (enabled);
  207         DataUnitNumberTextCtrl->Enable (enabled);
  208         BlockNumberTextCtrl->Enable (enabled);
  209     }
  210 
  211     void EncryptionTestDialog::SetTextCtrlData (wxTextCtrl *textCtrl, const BufferPtr &data)
  212     {
  213         wstring str;
  214         for (size_t i = 0; i < data.Size(); i++)
  215         {
  216             char strBuf[3];
  217             sprintf (strBuf, "%02x", (int) data[i]);
  218             str += StringConverter::ToWide (strBuf);
  219         }
  220 
  221         textCtrl->SetValue (str);
  222     }
  223 
  224     void EncryptionTestDialog::Reset ()
  225     {
  226         OnEncryptionAlgorithmSelected();
  227 
  228         DataUnitNumberTextCtrl->SetValue (L"0");
  229         BlockNumberTextCtrl->SetValue (L"0");
  230     }
  231 }