"Fossies" - the Fresh Open Source Software Archive

Member "src/Main/Forms/BenchmarkDialog.cpp" (10 Oct 2018, 11317 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 "BenchmarkDialog.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.22_Source_vs_1.23_Source.

    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 "Main/GraphicUserInterface.h"
   16 #include "BenchmarkDialog.h"
   17 
   18 namespace VeraCrypt
   19 {
   20     BenchmarkDialog::BenchmarkDialog (wxWindow *parent)
   21         : BenchmarkDialogBase (parent)
   22     {
   23         BenchmarkNoteStaticText->SetLabel (LangString["IDT_BOX_BENCHMARK_INFO"]);
   24         BenchmarkNoteStaticText->Wrap (RightSizer->GetSize().GetWidth());
   25 
   26         list <size_t> bufferSizes;
   27         bufferSizes.push_back (1 * BYTES_PER_MB);
   28         bufferSizes.push_back (5 * BYTES_PER_MB);
   29         bufferSizes.push_back (10 * BYTES_PER_MB);
   30         bufferSizes.push_back (50 * BYTES_PER_MB);
   31         bufferSizes.push_back (100 * BYTES_PER_MB);
   32         bufferSizes.push_back (200 * BYTES_PER_MB);
   33         bufferSizes.push_back (500 * BYTES_PER_MB);
   34         bufferSizes.push_back (1 * BYTES_PER_GB);
   35 
   36         foreach (size_t size, bufferSizes)
   37         {
   38             BufferSizeChoice->Append (Gui->SizeToString (size), (void *) size);
   39         }
   40 
   41         BenchmarkChoice->Select (0);
   42         BufferSizeChoice->Select (1);
   43         
   44         UpdateBenchmarkList ();
   45         
   46         wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);  // wxFILTER_NUMERIC does not exclude - . , etc.
   47         const wxChar *valArr[] = { L"0", L"1", L"2", L"3", L"4", L"5", L"6", L"7", L"8", L"9" };
   48         validator.SetIncludes (wxArrayString (array_capacity (valArr), (const wxChar **) &valArr));
   49         VolumePimText->SetValidator (validator);
   50 
   51         Layout();
   52         Fit();
   53         Center();
   54     }
   55     
   56     void BenchmarkDialog::UpdateBenchmarkList ()
   57     {
   58         int index = BenchmarkChoice->GetSelection ();
   59         if (index == 1)
   60         {
   61             // PRF case
   62             m_volumePimLabel->Show ();
   63             VolumePimText->Show ();
   64             
   65             BufferSizeChoice->Hide ();
   66             m_bufferSizeLabel->Hide ();
   67         }
   68         else
   69         {
   70             m_volumePimLabel->Hide ();
   71             VolumePimText->Hide ();
   72             
   73             BufferSizeChoice->Show ();
   74             m_bufferSizeLabel->Show ();
   75         }
   76         
   77         BenchmarkListCtrl->DeleteAllItems();
   78         BenchmarkListCtrl->DeleteAllColumns();
   79         
   80         if (index == 0)
   81         {
   82             // encryption case
   83             list <int> colPermilles;
   84             BenchmarkListCtrl->InsertColumn (ColumnAlgorithm, LangString["ALGORITHM"], wxLIST_FORMAT_LEFT, 1);
   85             colPermilles.push_back (322);
   86 
   87             BenchmarkListCtrl->InsertColumn (ColumnEncryption, LangString["ENCRYPTION"], wxLIST_FORMAT_RIGHT, 1);
   88             colPermilles.push_back (226);
   89 
   90             BenchmarkListCtrl->InsertColumn (ColumnDecryption, LangString["DECRYPTION"], wxLIST_FORMAT_RIGHT, 1);
   91             colPermilles.push_back (226);
   92 
   93             BenchmarkListCtrl->InsertColumn (ColumnMean, LangString["MEAN"], wxLIST_FORMAT_RIGHT, 1);
   94             colPermilles.push_back (226);
   95             
   96             Gui->SetListCtrlWidth (BenchmarkListCtrl, 62, false);
   97             Gui->SetListCtrlHeight (BenchmarkListCtrl, 14);
   98             Gui->SetListCtrlColumnWidths (BenchmarkListCtrl, colPermilles);
   99         }
  100         else if (index == 1)
  101         {
  102             // PRF case
  103             list <int> colPermilles;
  104             BenchmarkListCtrl->InsertColumn (ColumnAlgorithm, LangString["ALGORITHM"], wxLIST_FORMAT_LEFT, 1);
  105             colPermilles.push_back (322);
  106 
  107             BenchmarkListCtrl->InsertColumn (ColumnTime, LangString["TIME"], wxLIST_FORMAT_RIGHT, 1);
  108             colPermilles.push_back (226);
  109 
  110             BenchmarkListCtrl->InsertColumn (ColumnIterations, LangString["ITERATIONS"], wxLIST_FORMAT_RIGHT, 1);
  111             colPermilles.push_back (226);
  112             
  113             Gui->SetListCtrlWidth (BenchmarkListCtrl, 62, false);
  114             Gui->SetListCtrlHeight (BenchmarkListCtrl, 14);
  115             Gui->SetListCtrlColumnWidths (BenchmarkListCtrl, colPermilles);
  116         }
  117         else
  118         {
  119             // Hash case
  120             list <int> colPermilles;
  121             BenchmarkListCtrl->InsertColumn (ColumnAlgorithm, LangString["ALGORITHM"], wxLIST_FORMAT_LEFT, 1);
  122             colPermilles.push_back (322);
  123 
  124             BenchmarkListCtrl->InsertColumn (ColumnEncryption, LangString["MEAN"], wxLIST_FORMAT_RIGHT, 1);
  125             colPermilles.push_back (226);
  126             
  127             Gui->SetListCtrlWidth (BenchmarkListCtrl, 62, false);
  128             Gui->SetListCtrlHeight (BenchmarkListCtrl, 14);
  129             Gui->SetListCtrlColumnWidths (BenchmarkListCtrl, colPermilles);
  130         }
  131     }
  132     
  133     void BenchmarkDialog::OnBenchmarkChoiceSelected (wxCommandEvent& event)
  134     {
  135         UpdateBenchmarkList ();
  136         
  137         Layout();
  138         Fit();
  139     }
  140 
  141     void BenchmarkDialog::OnBenchmarkButtonClick (wxCommandEvent& event)
  142     {
  143         list <BenchmarkResult> results;
  144 
  145         wxBusyCursor busy;
  146         int opIndex = BenchmarkChoice->GetSelection ();
  147         Buffer buffer ((opIndex == 1)? sizeof (unsigned long) : (size_t) Gui->GetSelectedData <size_t> (BufferSizeChoice));
  148         
  149         if (opIndex == 1)
  150         {
  151             unsigned long pim = 0;
  152             if (!VolumePimText->GetValue().ToULong (&pim))
  153                 pim = 0;
  154                 
  155             memcpy (buffer.Ptr (), &pim, sizeof (unsigned long));
  156         }
  157         
  158 
  159         BenchmarkThreadRoutine routine(this, results, buffer, opIndex);
  160         Gui->ExecuteWaitThreadRoutine (this, &routine);
  161 
  162         BenchmarkListCtrl->DeleteAllItems();
  163 
  164         foreach (const BenchmarkResult &result, results)
  165         {
  166             vector <wstring> fields (BenchmarkListCtrl->GetColumnCount());
  167 
  168             fields[ColumnAlgorithm] = result.AlgorithmName;
  169             if (opIndex == 0)
  170             {
  171                 fields[ColumnEncryption] = Gui->SpeedToString (result.EncryptionSpeed);
  172                 fields[ColumnDecryption] = Gui->SpeedToString (result.DecryptionSpeed);
  173                 fields[ColumnMean] = Gui->SpeedToString (result.MeanSpeed);
  174             }
  175             else if (opIndex == 1)
  176             {
  177                 fields[ColumnTime] = wxString::Format (wxT("%llu ms"), (unsigned long long) result.Time);
  178                 fields[ColumnIterations] = wxString::Format (wxT("%llu"), (unsigned long long) result.Iterations);
  179             }
  180             else
  181             {
  182                 fields[ColumnHashMean] = Gui->SpeedToString (result.MeanSpeed);
  183             }
  184 
  185             Gui->AppendToListCtrl (BenchmarkListCtrl, fields);
  186         }
  187 
  188         BenchmarkListCtrl->SetColumnWidth(0, wxLIST_AUTOSIZE);
  189         wxSize minSize = BenchmarkListCtrl->GetBestSize ();
  190         minSize.IncBy (10, 20);
  191         BenchmarkListCtrl->SetMinSize(minSize);
  192         Layout ();
  193         Fit();
  194     }
  195 
  196     void BenchmarkDialog::DoBenchmark (list<BenchmarkResult>& results, Buffer& buffer, int opIndex)
  197     {
  198         try
  199         {
  200             if (opIndex == 0)
  201             {
  202                 EncryptionAlgorithmList encryptionAlgorithms = EncryptionAlgorithm::GetAvailableAlgorithms();
  203                 foreach (shared_ptr <EncryptionAlgorithm> ea, encryptionAlgorithms)
  204                 {
  205                     if (!ea->IsDeprecated())
  206                     {
  207                         BenchmarkResult result;
  208                         result.AlgorithmName = ea->GetName(true);
  209 
  210                         Buffer key (ea->GetKeySize());
  211                         ea->SetKey (key);
  212 
  213                         shared_ptr <EncryptionMode> xts (new EncryptionModeXTS);
  214                         xts->SetKey (key);
  215                         ea->SetMode (xts);
  216 
  217                         wxLongLong startTime = wxGetLocalTimeMillis();
  218 
  219                         // CPU "warm up" (an attempt to prevent skewed results on systems where CPU frequency gradually changes depending on CPU load).
  220                         do
  221                         {
  222                             ea->EncryptSectors (buffer, 0, buffer.Size() / ENCRYPTION_DATA_UNIT_SIZE, ENCRYPTION_DATA_UNIT_SIZE);
  223                         }
  224                         while (wxGetLocalTimeMillis().GetValue() - startTime.GetValue() < 20);
  225 
  226                         uint64 size = 0;
  227                         uint64 time;
  228                         startTime = wxGetLocalTimeMillis();
  229 
  230                         do
  231                         {
  232                             ea->EncryptSectors (buffer, 0, buffer.Size() / ENCRYPTION_DATA_UNIT_SIZE, ENCRYPTION_DATA_UNIT_SIZE);
  233                             size += buffer.Size();
  234                             time = (uint64) (wxGetLocalTimeMillis().GetValue() - startTime.GetValue());
  235                         }
  236                         while (time < 100);
  237 
  238                         result.EncryptionSpeed = size * 1000 / time;
  239 
  240                         startTime = wxGetLocalTimeMillis();
  241                         size = 0;
  242 
  243                         do
  244                         {
  245                             ea->DecryptSectors (buffer, 0, buffer.Size() / ENCRYPTION_DATA_UNIT_SIZE, ENCRYPTION_DATA_UNIT_SIZE);
  246                             size += buffer.Size();
  247                             time = (uint64) (wxGetLocalTimeMillis().GetValue() - startTime.GetValue());
  248                         }
  249                         while (time < 100);
  250 
  251                         result.DecryptionSpeed = size * 1000 / time;
  252                         result.MeanSpeed = (result.EncryptionSpeed + result.DecryptionSpeed) / 2;
  253 
  254                         bool inserted = false;
  255                         for (list <BenchmarkResult>::iterator i = results.begin(); i != results.end(); ++i)
  256                         {
  257                             if (i->MeanSpeed < result.MeanSpeed)
  258                             {
  259                                 results.insert (i, result);
  260                                 inserted = true;
  261                                 break;
  262                             }
  263                         }
  264 
  265                         if (!inserted)
  266                             results.push_back (result);
  267                     }
  268                 }
  269             }
  270             else if (opIndex == 1)
  271             {
  272                 Buffer dk(MASTER_KEYDATA_SIZE);
  273                 Buffer salt(64);
  274                 const char *tmp_salt = {"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF\x01\x23\x45\x67\x89\xAB\xCD\xEF\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF\x01\x23\x45\x67\x89\xAB\xCD\xEF\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF"};
  275                 unsigned long pim;
  276                 Pkcs5KdfList prfList = Pkcs5Kdf::GetAvailableAlgorithms (false);
  277                 VolumePassword password ((const byte*) "passphrase-1234567890", 21);
  278 
  279                 memcpy (&pim, buffer.Ptr (), sizeof (unsigned long));
  280                 memcpy (salt.Ptr(), tmp_salt, 64);
  281                 
  282                 foreach (shared_ptr <Pkcs5Kdf> prf, prfList)
  283                 {
  284                     if (!prf->IsDeprecated())
  285                     {
  286                         BenchmarkResult result;
  287                         result.AlgorithmName = prf->GetName ();
  288                         result.Iterations = (uint64) prf->GetIterationCount (pim);
  289 
  290                         uint64 time;
  291                         wxLongLong startTime = wxGetLocalTimeMillis();
  292                         
  293                         for (int i = 1; i <= 2; i++) 
  294                         {
  295                             prf->DeriveKey (dk, password, pim, salt);
  296                         }
  297                         
  298                         time = (uint64) (wxGetLocalTimeMillis().GetValue() - startTime.GetValue());
  299 
  300                         result.Time = time / 2;
  301                 
  302                         bool inserted = false;
  303                         for (list <BenchmarkResult>::iterator i = results.begin(); i != results.end(); ++i)
  304                         {
  305                             if (i->Time > result.Time)
  306                             {
  307                                 results.insert (i, result);
  308                                 inserted = true;
  309                                 break;
  310                             }
  311                         }
  312 
  313                         if (!inserted)
  314                             results.push_back (result);                         
  315                     }
  316                 }
  317                 
  318             }
  319             else
  320             {
  321                 Buffer digest (1024);
  322                 HashList hashAlgorithms = Hash::GetAvailableAlgorithms ();
  323                 foreach (shared_ptr <Hash> hash, hashAlgorithms)
  324                 {
  325                     if (!hash->IsDeprecated())
  326                     {
  327                         BenchmarkResult result;
  328                         result.AlgorithmName = hash->GetName ();
  329                         
  330                         uint64 size = 0;
  331                         uint64 time;
  332                         wxLongLong startTime = wxGetLocalTimeMillis();
  333                         
  334                         // CPU "warm up" (an attempt to prevent skewed results on systems where CPU frequency gradually changes depending on CPU load).
  335                         do
  336                         {
  337                             hash->Init ();
  338                             hash->ProcessData (digest);
  339                             hash->GetDigest (digest);
  340                         }
  341                         while (wxGetLocalTimeMillis().GetValue() - startTime.GetValue() < 100);
  342 
  343 
  344                         startTime = wxGetLocalTimeMillis();
  345                         do
  346                         {
  347                             hash->Init ();
  348                             hash->ProcessData (buffer);
  349                             hash->GetDigest (digest);
  350                             time = (uint64) (wxGetLocalTimeMillis().GetValue() - startTime.GetValue());
  351                             size += buffer.Size ();
  352                         }
  353                         while (time < 2000);
  354 
  355                         result.MeanSpeed = size * 1000 / time;
  356 
  357                         bool inserted = false;
  358                         for (list <BenchmarkResult>::iterator i = results.begin(); i != results.end(); ++i)
  359                         {
  360                             if (i->MeanSpeed < result.MeanSpeed)
  361                             {
  362                                 results.insert (i, result);
  363                                 inserted = true;
  364                                 break;
  365                             }
  366                         }
  367 
  368                         if (!inserted)
  369                             results.push_back (result);
  370                         
  371                     }
  372                 }
  373             }
  374         }
  375         catch (exception &e)
  376         {
  377             Gui->ShowError (e);
  378         }
  379     }
  380 }