From eb9cb16aeb33f9904b29b84e270f2d8c8f8431eb Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Tue, 9 Dec 2014 00:11:15 +0100 Subject: Windows: Implement specifying the size of the generated keyfiles and also letting VeraCrypt choose random size values. --- src/Common/Common.rc | 14 ++++++---- src/Common/Dlgcode.c | 73 +++++++++++++++++++++++++++++++++++++++++-------- src/Common/Language.xml | 3 ++ src/Common/Resource.h | 5 +++- 4 files changed, 77 insertions(+), 18 deletions(-) diff --git a/src/Common/Common.rc b/src/Common/Common.rc index 29d35d23..39f6f7a7 100644 --- a/src/Common/Common.rc +++ b/src/Common/Common.rc @@ -194,14 +194,14 @@ BEGIN CONTROL "",IDC_INFO_BOX_TEXT,"RichEdit20A",ES_MULTILINE | ES_READONLY | ES_NUMBER | WS_BORDER | WS_VSCROLL | WS_TABSTOP,5,6,361,188 END -IDD_KEYFILE_GENERATOR DIALOGEX 0, 0, 308, 303 +IDD_KEYFILE_GENERATOR DIALOGEX 0, 0, 308, 325 STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "VeraCrypt - Keyfile Generator" FONT 8, "MS Shell Dlg", 400, 0, 0x1 BEGIN DEFPUSHBUTTON "Close",IDCLOSE,237,10,59,14 COMBOBOX IDC_PRF_ID,79,49,91,90,CBS_DROPDOWNLIST | WS_TABSTOP - PUSHBUTTON "Generate and Save Keyfile...",IDC_GENERATE_AND_SAVE_KEYFILE,92,281,131,14 + PUSHBUTTON "Generate and Save Keyfile...",IDC_GENERATE_AND_SAVE_KEYFILE,92,302,141,14 LTEXT "IMPORTANT: Move your mouse as randomly as possible within this window. The longer you move it, the better. This significantly increases the cryptographic strength of the keyfile.",IDT_KEYFILE_GENERATOR_NOTE,11,5,213,33 CONTROL "",IDC_STATIC,"Static",SS_ETCHEDHORZ,1,40,307,1,WS_EX_STATICEDGE RTEXT "Mixing PRF:",IDT_PRF,6,51,67,10,SS_CENTERIMAGE @@ -210,8 +210,12 @@ BEGIN CONTROL "Display pool content",IDC_DISPLAY_POOL_CONTENTS,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,191,51,111,10 LTEXT "Number of keyfiles:",IDT_NUMBER_KEYFILES,9,247,75,8 EDITTEXT IDC_NUMBER_KEYFILES,92,244,51,14,ES_RIGHT | ES_AUTOHSCROLL | ES_NUMBER - LTEXT "Keyfiles base name:",IDT_KEYFILES_BASE_NAME,9,266,75,8 - EDITTEXT IDC_KEYFILES_BASE_NAME,92,263,131,14,ES_AUTOHSCROLL + LTEXT "Keyfiles base name:",IDT_KEYFILES_BASE_NAME,9,287,75,8 + EDITTEXT IDC_KEYFILES_BASE_NAME,92,284,141,14,ES_AUTOHSCROLL + LTEXT "Keyfiles size (in Bytes):",IDT_KEYFILES_SIZE,9,266,75,8 + EDITTEXT IDC_KEYFILES_SIZE,92,264,51,14,ES_RIGHT | ES_AUTOHSCROLL | ES_NUMBER + CONTROL "Random size ( 64 <-> 1048576 )",IDC_KEYFILES_RANDOM_SIZE, + "Button",BS_AUTOCHECKBOX | WS_TABSTOP,149,266,140,10 END IDD_MULTI_CHOICE_DLG DIALOGEX 0, 0, 167, 322 @@ -377,7 +381,7 @@ BEGIN LEFTMARGIN, 7 RIGHTMARGIN, 299 TOPMARGIN, 7 - BOTTOMMARGIN, 299 + BOTTOMMARGIN, 321 END IDD_MULTI_CHOICE_DLG, DIALOG diff --git a/src/Common/Dlgcode.c b/src/Common/Dlgcode.c index 96938962..1a793517 100644 --- a/src/Common/Dlgcode.c +++ b/src/Common/Dlgcode.c @@ -4996,8 +4996,12 @@ BOOL CALLBACK KeyfileGeneratorDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP #endif SetTimer (hwndDlg, 0xfd, RANDPOOL_DISPLAY_REFRESH_INTERVAL, NULL); SendMessage (GetDlgItem (hwndDlg, IDC_POOL_CONTENTS), WM_SETFONT, (WPARAM) hFixedDigitFont, (LPARAM) TRUE); - SendMessage (GetDlgItem (hwndDlg, IDC_NUMBER_KEYFILES), EM_SETLIMITTEXT, (WPARAM) (TC_MAX_PATH - 1), 0); + // 9-digit limit for the number of keyfiles (more than enough!) + SendMessage (GetDlgItem (hwndDlg, IDC_NUMBER_KEYFILES), EM_SETLIMITTEXT, (WPARAM) 9, 0); SetWindowText(GetDlgItem (hwndDlg, IDC_NUMBER_KEYFILES), "1"); + // maximum keyfile size is 1048576, so limit the edit control to 7 characters + SendMessage (GetDlgItem (hwndDlg, IDC_KEYFILES_SIZE), EM_SETLIMITTEXT, (WPARAM) 7, 0); + SetWindowText(GetDlgItem (hwndDlg, IDC_KEYFILES_SIZE), "64"); // set the maximum length of the keyfile base name to (TC_MAX_PATH - 1) SendMessage (GetDlgItem (hwndDlg, IDC_KEYFILES_BASE_NAME), EM_SETLIMITTEXT, (WPARAM) (TC_MAX_PATH - 1), 0); return 1; @@ -5064,23 +5068,28 @@ BOOL CALLBACK KeyfileGeneratorDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP return 1; } + if (lw == IDC_KEYFILES_RANDOM_SIZE) + { + EnableWindow(GetDlgItem (hwndDlg, IDC_KEYFILES_SIZE), !GetCheckBox (hwndDlg, IDC_KEYFILES_RANDOM_SIZE)); + } + if (lw == IDC_GENERATE_AND_SAVE_KEYFILE) { - char szNumberKeyFiles[TC_MAX_PATH] = {0}; + char szNumber[16] = {0}; char szFileBaseName[TC_MAX_PATH]; char szDirName[TC_MAX_PATH]; - char szFileName [3*TC_MAX_PATH]; - unsigned char keyfile [MAX_PASSWORD]; + char szFileName [2*TC_MAX_PATH + 16]; + unsigned char *keyfile = NULL; int fhKeyfile = -1, status; - long keyfilesCount = 0, i; + long keyfilesCount = 0, keyfilesSize = 0, i; char* fileExtensionPtr = 0; char szSuffix[32]; - BOOL bBaseNameValid = FALSE; + BOOL bRandomSize = GetCheckBox (hwndDlg, IDC_KEYFILES_RANDOM_SIZE); - if (!GetWindowText(GetDlgItem (hwndDlg, IDC_NUMBER_KEYFILES), szNumberKeyFiles, TC_MAX_PATH)) - szNumberKeyFiles[0] = 0; + if (!GetWindowText(GetDlgItem (hwndDlg, IDC_NUMBER_KEYFILES), szNumber, sizeof(szNumber))) + szNumber[0] = 0; - keyfilesCount = strtoul(szNumberKeyFiles, NULL, 0); + keyfilesCount = strtoul(szNumber, NULL, 0); if (keyfilesCount <= 0 || keyfilesCount == LONG_MAX) { Warning("KEYFILE_INCORRECT_NUMBER"); @@ -5088,6 +5097,20 @@ BOOL CALLBACK KeyfileGeneratorDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP return 1; } + if (!bRandomSize) + { + if (!GetWindowText(GetDlgItem (hwndDlg, IDC_KEYFILES_SIZE), szNumber, sizeof(szNumber))) + szNumber[0] = 0; + + keyfilesSize = strtoul(szNumber, NULL, 0); + if (keyfilesSize < 64 || keyfilesSize > 1024*1024) + { + Warning("KEYFILE_INCORRECT_SIZE"); + SendMessage(hwndDlg, WM_NEXTDLGCTL, (WPARAM) GetDlgItem (hwndDlg, IDC_KEYFILES_SIZE), TRUE); + return 1; + } + } + if (!GetWindowText(GetDlgItem (hwndDlg, IDC_KEYFILES_BASE_NAME), szFileBaseName, TC_MAX_PATH)) szFileBaseName[0] = 0; @@ -5117,6 +5140,8 @@ BOOL CALLBACK KeyfileGeneratorDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP WaitCursor(); + keyfile = (unsigned char*) TCalloc( bRandomSize? KEYFILE_MAX_READ_LEN : keyfilesSize ); + for (i= 0; i < keyfilesCount; i++) { StringCbCopy(szFileName, sizeof(szFileName), szDirName); @@ -5154,6 +5179,7 @@ BOOL CALLBACK KeyfileGeneratorDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP status = AskWarnNoYesString (s); if (status == IDNO) { + TCfree(keyfile); NormalCursor(); return 1; } @@ -5162,33 +5188,56 @@ BOOL CALLBACK KeyfileGeneratorDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LP /* Conceive the file */ if ((fhKeyfile = _open(szFileName, _O_CREAT|_O_TRUNC|_O_WRONLY|_O_BINARY, _S_IREAD|_S_IWRITE)) == -1) { + TCfree(keyfile); NormalCursor(); handleWin32Error (hwndDlg); return 1; } + if (bRandomSize) + { + /* Generate a random size */ + if (!RandgetBytes ((unsigned char*) &keyfilesSize, sizeof(keyfilesSize), FALSE)) + { + _close (fhKeyfile); + DeleteFile (szFileName); + TCfree(keyfile); + NormalCursor(); + return 1; + } + + /* since keyfilesSize < 1024 * 1024, we mask with 0x000FFFFF */ + keyfilesSize = (long) (((unsigned long) keyfilesSize) & 0x000FFFFF); + + keyfilesSize %= ((KEYFILE_MAX_READ_LEN - 64) + 1); + keyfilesSize += 64; + } + /* Generate the keyfile */ - if (!RandgetBytes (keyfile, sizeof(keyfile), TRUE)) + if (!RandgetBytesFull (keyfile, keyfilesSize, TRUE, TRUE)) { _close (fhKeyfile); DeleteFile (szFileName); + TCfree(keyfile); NormalCursor(); return 1; } /* Write the keyfile */ - status = _write (fhKeyfile, keyfile, sizeof(keyfile)); - burn (keyfile, sizeof(keyfile)); + status = _write (fhKeyfile, keyfile, keyfilesSize); + burn (keyfile, keyfilesSize); _close (fhKeyfile); if (status == -1) { + TCfree(keyfile); NormalCursor(); handleWin32Error (hwndDlg); return 1; } } + TCfree(keyfile); NormalCursor(); Info("KEYFILE_CREATED"); diff --git a/src/Common/Language.xml b/src/Common/Language.xml index 1a98b79c..6c87273d 100644 --- a/src/Common/Language.xml +++ b/src/Common/Language.xml @@ -44,6 +44,7 @@ Normal mode &KB U&se keyfiles + Random size ( 64 <-> 1048576 ) &Keyfiles... Information on hash algorithms More information @@ -332,6 +333,7 @@ WARNING: If you lose a keyfile or if any bit of its first 1024 kilobytes changes, it will be impossible to mount volumes that use the keyfile! bits Number of keyfiles: + Keyfiles size (in Bytes): Keyfiles base name: Translated by: Plaintext size: @@ -868,6 +870,7 @@ Enter password for the header stored in backup file Keyfiles have been successfully created. The number of keyfiles you supplied is invalid. + The keyfile size must be comprized between 64 and 1048576 bytes. Please enter a name for the keyfile(s) to be generated The base name of the keyfile(s) is invalid The keyfile '%s' already exists.\nDo you want to overwrite it? The generation process will be stopped if you answer No. diff --git a/src/Common/Resource.h b/src/Common/Resource.h index 2b75ea88..3c6a09e8 100644 --- a/src/Common/Resource.h +++ b/src/Common/Resource.h @@ -180,6 +180,9 @@ #define IDC_NUMBER_KEYFILES 5118 #define IDT_KEYFILES_BASE_NAME 5119 #define IDC_KEYFILES_BASE_NAME 5120 +#define IDC_KEYFILES_SIZE 5121 +#define IDC_KEYFILES_RANDOM_SIZE 5122 +#define IDT_KEYFILES_SIZE 5123 // Next default values for new objects // @@ -188,7 +191,7 @@ #define _APS_NO_MFC 1 #define _APS_NEXT_RESOURCE_VALUE 542 #define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 5121 +#define _APS_NEXT_CONTROL_VALUE 5124 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif -- cgit v1.2.3