/* zip_source_win32a.c -- create data source from Windows file (ANSI) Copyright (C) 1999-2016 Dieter Baron and Thomas Klausner This file is part of libzip, a library to manipulate ZIP archives. The authors can be contacted at Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include "zipint.h" #include "zipwin32.h" static void * _win32_strdup_a(const void *str); static HANDLE _win32_open_a(_zip_source_win32_read_file_t *ctx); static HANDLE _win32_create_temp_a(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32_t value, PSECURITY_ATTRIBUTES sa); static int _win32_rename_temp_a(_zip_source_win32_read_file_t *ctx); static int _win32_remove_a(const void *fname); static _zip_source_win32_file_ops_t win32_ops_a = { _win32_strdup_a, _win32_open_a, _win32_create_temp_a, _win32_rename_temp_a, _win32_remove_a }; ZIP_EXTERN zip_source_t * zip_source_win32a(zip_t *za, const char *fname, zip_uint64_t start, zip_int64_t len) { if (za == NULL) return NULL; return zip_source_win32a_create(fname, start, len, &za->error); } ZIP_EXTERN zip_source_t * zip_source_win32a_create(const char *fname, zip_uint64_t start, zip_int64_t length, zip_error_t *error) { if (fname == NULL || length < -1) { zip_error_set(error, ZIP_ER_INVAL, 0); return NULL; } return _zip_source_win32_handle_or_name(fname, INVALID_HANDLE_VALUE, start, length, 1, NULL, &win32_ops_a, error); } static void * _win32_strdup_a(const void *str) { return strdup((const char *)str); } static HANDLE _win32_open_a(_zip_source_win32_read_file_t *ctx) { return CreateFileA(ctx->fname, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); } static HANDLE _win32_create_temp_a(_zip_source_win32_read_file_t *ctx, void **temp, zip_uint32_t value, PSECURITY_ATTRIBUTES sa) { int len; len = strlen((const char *)ctx->fname) + 10; if (*temp == NULL) { if ((*temp = malloc(sizeof(char) * len)) == NULL) { zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); return INVALID_HANDLE_VALUE; } } if (sprintf((char *)*temp, "%s.%08x", (const char *)ctx->fname, value) != len - 1) { return INVALID_HANDLE_VALUE; } return CreateFileA((const char *)*temp, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, sa, CREATE_NEW, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY, NULL); } static int _win32_rename_temp_a(_zip_source_win32_read_file_t *ctx) { if (!MoveFileExA(ctx->tmpname, ctx->fname, MOVEFILE_REPLACE_EXISTING)) return -1; return 0; } static int _win32_remove_a(const void *fname) { DeleteFileA((const char *)fname); return 0; } '#n9'>9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/*
 Derived from source code of TrueCrypt 7.1a, which is
 Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
 by the TrueCrypt License 3.0.

 Modifications and additions to the original source code (contained in this file)
 and all other portions of this file are Copyright (c) 2013-2017 IDRIX
 and are governed by the Apache License 2.0 the full text of which is
 contained in the file License.txt included in VeraCrypt binary and source
 code distribution packages.
*/

#include "System.h"
#include "Volume/EncryptionModeXTS.h"
#include "Volume/EncryptionTest.h"
#include "Main/GraphicUserInterface.h"
#include "EncryptionTestDialog.h"

namespace VeraCrypt
{
	EncryptionTestDialog::EncryptionTestDialog (wxWindow* parent)
		: EncryptionTestDialogBase (parent)
	{
		EncryptionAlgorithms = EncryptionAlgorithm::GetAvailableAlgorithms();
		foreach (shared_ptr <EncryptionAlgorithm> ea, EncryptionAlgorithms)
		{
			if (!ea->IsDeprecated())
				EncryptionAlgorithmChoice->Append (ea->GetName(true), ea.get());
		}

		EncryptionAlgorithmChoice->Select (0);
		Reset();

		Fit();
		Layout();
		Center();
	}

	void EncryptionTestDialog::EncryptOrDecrypt (bool encrypt)
	{
		try
		{
			bool xts = XtsModeCheckBox->IsChecked();

			shared_ptr <EncryptionAlgorithm> ea = GetSelectedEncryptionAlgorithm();

			Buffer key;
			GetTextCtrlData (KeyTextCtrl, key);

			if (key.Size() != ea->GetKeySize())
				throw_err (LangString["TEST_KEY_SIZE"]);

			ea->SetKey (key);

			Buffer data;
			GetTextCtrlData (encrypt ? PlainTextTextCtrl : CipherTextTextCtrl, data);

			if (data.Size() != ea->GetMaxBlockSize())
				throw_err (LangString[encrypt ? "TEST_PLAINTEXT_SIZE" : "TEST_CIPHERTEXT_SIZE"]);

			if (xts)
			{
				Buffer secondaryKey;
				GetTextCtrlData (SecondaryKeyTextCtrl, secondaryKey);

				if (secondaryKey.Size() != ea->GetKeySize())
					throw_err (LangString["TEST_INCORRECT_SECONDARY_KEY_SIZE"]);

				uint64 dataUnitNumber;
				size_t blockNumber;

				try
				{
					dataUnitNumber = StringConverter::ToUInt64 (wstring (DataUnitNumberTextCtrl->GetValue()));
				}
				catch (...)
				{
					DataUnitNumberTextCtrl->SetFocus();
					throw StringConversionFailed (SRC_POS);
				}

				try
				{
					blockNumber = StringConverter::ToUInt32 (wstring (BlockNumberTextCtrl->GetValue()));
					if (blockNumber > 31)
					{
						blockNumber = 31;
						BlockNumberTextCtrl->SetValue (L"31");
					}
				}
				catch (...)
				{
					BlockNumberTextCtrl->SetFocus();
					throw StringConversionFailed (SRC_POS);
				}

				shared_ptr <EncryptionMode> xts (new EncryptionModeXTS);
				xts->SetKey (secondaryKey);
				ea->SetMode (xts);

				Buffer sector (ENCRYPTION_DATA_UNIT_SIZE);
				BufferPtr block = sector.GetRange (blockNumber * ea->GetMaxBlockSize(), ea->GetMaxBlockSize());

				block.CopyFrom (data);

				if (encrypt)
					ea->EncryptSectors (sector, dataUnitNumber, 1, sector.Size());
				else
					ea->DecryptSectors (sector, dataUnitNumber, 1, sector.Size());

				data.CopyFrom (block);
			}
			else
			{
				if (encrypt)
					ea->GetCiphers().front()->EncryptBlock (data);
				else
					ea->GetCiphers().front()->DecryptBlock (data);
			}

			SetTextCtrlData (encrypt ? CipherTextTextCtrl : PlainTextTextCtrl, data);
		}
		catch (exception &e)
		{
			Gui->ShowError (e);
		}
	}

	shared_ptr <EncryptionAlgorithm> EncryptionTestDialog::GetSelectedEncryptionAlgorithm () const
	{
		return Gui->GetSelectedData <EncryptionAlgorithm> (EncryptionAlgorithmChoice)->GetNew();
	}

	void EncryptionTestDialog::GetTextCtrlData (wxTextCtrl *textCtrl, Buffer &buffer) const
	{
		vector <byte> data;
		string dataStr = StringConverter::ToSingle (wstring (textCtrl->GetValue()));

		for (size_t i = 0; i < dataStr.size() / 2; ++i)
		{
			unsigned int dataByte;
			if (sscanf (dataStr.substr (i * 2, 2).c_str(), "%x", &dataByte) != 1)
			{
				textCtrl->SetFocus();
				throw StringConversionFailed (SRC_POS);
			}

			data.push_back ((byte) dataByte);
		}

		if (data.empty())
			return;

		buffer.CopyFrom (ConstBufferPtr (&data.front(), data.size()));
	}

	void EncryptionTestDialog::OnAutoTestAllButtonClick (wxCommandEvent& event)
	{
		try
		{
			{
				wxBusyCursor busy;
				EncryptionTest::TestAll();
			}

			Gui->ShowInfo ("TESTS_PASSED");
		}
		catch (Exception &e)
		{
			Gui->ShowError (e);
			Gui->ShowError ("TESTS_FAILED");
		}
	}

	void EncryptionTestDialog::OnEncryptionAlgorithmSelected ()
	{
		shared_ptr <EncryptionAlgorithm> ea = GetSelectedEncryptionAlgorithm();

		KeySizeStaticText->SetLabel (StringFormatter (L"{0} {1}", (uint32) ea->GetKeySize() * 8, LangString["BITS"]));

		Buffer key (ea->GetKeySize());
		key.Zero();
		SetTextCtrlData (KeyTextCtrl, key);
		SetTextCtrlData (SecondaryKeyTextCtrl, key);

		Buffer block (ea->GetMaxBlockSize());
		block.Zero();
		SetTextCtrlData (PlainTextTextCtrl, block);
		SetTextCtrlData (CipherTextTextCtrl, block);

		if (ea->GetCiphers().size() > 1)
		{
			XtsModeCheckBox->Disable();
			XtsModeCheckBox->SetValue (true);
			SecondaryKeyTextCtrl->Enable (true);
			DataUnitNumberTextCtrl->Enable (true);
			BlockNumberTextCtrl->Enable (true);
		}
		else
			XtsModeCheckBox->Enable();
	}

	void EncryptionTestDialog::OnXtsModeCheckBoxClick (wxCommandEvent& event)
	{
		bool enabled = event.IsChecked();
		SecondaryKeyTextCtrl->Enable (enabled);
		DataUnitNumberTextCtrl->Enable (enabled);
		BlockNumberTextCtrl->Enable (enabled);
	}

	void EncryptionTestDialog::SetTextCtrlData (wxTextCtrl *textCtrl, const BufferPtr &data)
	{
		wstring str;
		for (size_t i = 0; i < data.Size(); i++)
		{
			char strBuf[3];
			sprintf (strBuf, "%02x", (int) data[i]);
			str += StringConverter::ToWide (strBuf);
		}

		textCtrl->SetValue (str);
	}

	void EncryptionTestDialog::Reset ()
	{
		OnEncryptionAlgorithmSelected();

		DataUnitNumberTextCtrl->SetValue (L"0");
		BlockNumberTextCtrl->SetValue (L"0");
	}
}