diff --git a/SW01-XTEA-Cipher/SW01-XTEA-Cipher.sln b/SW01-XTEA-Cipher/SW01-XTEA-Cipher.sln index 945b8a1..523d05d 100644 --- a/SW01-XTEA-Cipher/SW01-XTEA-Cipher.sln +++ b/SW01-XTEA-Cipher/SW01-XTEA-Cipher.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.4.33103.184 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SW01-XTEA-Cipher", "SW01-XTEA-Cipher\SW01-XTEA-Cipher.vcxproj", "{0A55D2C2-29E0-459E-9D86-975DAA49A8FC}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SW02-XTEA-CBC", "SW02-XTEA-CBC\SW02-XTEA-CBC.vcxproj", "{101384C8-6D89-428A-A336-D82A488483B2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -21,6 +23,14 @@ Global {0A55D2C2-29E0-459E-9D86-975DAA49A8FC}.Release|x64.Build.0 = Release|x64 {0A55D2C2-29E0-459E-9D86-975DAA49A8FC}.Release|x86.ActiveCfg = Release|Win32 {0A55D2C2-29E0-459E-9D86-975DAA49A8FC}.Release|x86.Build.0 = Release|Win32 + {101384C8-6D89-428A-A336-D82A488483B2}.Debug|x64.ActiveCfg = Debug|x64 + {101384C8-6D89-428A-A336-D82A488483B2}.Debug|x64.Build.0 = Debug|x64 + {101384C8-6D89-428A-A336-D82A488483B2}.Debug|x86.ActiveCfg = Debug|Win32 + {101384C8-6D89-428A-A336-D82A488483B2}.Debug|x86.Build.0 = Debug|Win32 + {101384C8-6D89-428A-A336-D82A488483B2}.Release|x64.ActiveCfg = Release|x64 + {101384C8-6D89-428A-A336-D82A488483B2}.Release|x64.Build.0 = Release|x64 + {101384C8-6D89-428A-A336-D82A488483B2}.Release|x86.ActiveCfg = Release|Win32 + {101384C8-6D89-428A-A336-D82A488483B2}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SW01-XTEA-Cipher/SW02-XTEA-CBC/SW02-XTEA-CBC.vcxproj b/SW01-XTEA-Cipher/SW02-XTEA-CBC/SW02-XTEA-CBC.vcxproj new file mode 100644 index 0000000..6848610 --- /dev/null +++ b/SW01-XTEA-Cipher/SW02-XTEA-CBC/SW02-XTEA-CBC.vcxproj @@ -0,0 +1,141 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {101384c8-6d89-428a-a336-d82a488483b2} + SW02XTEACBC + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SW01-XTEA-Cipher/SW02-XTEA-CBC/SW02-XTEA-CBC.vcxproj.filters b/SW01-XTEA-Cipher/SW02-XTEA-CBC/SW02-XTEA-CBC.vcxproj.filters new file mode 100644 index 0000000..01e9879 --- /dev/null +++ b/SW01-XTEA-Cipher/SW02-XTEA-CBC/SW02-XTEA-CBC.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + \ No newline at end of file diff --git a/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA-CBC.c b/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA-CBC.c new file mode 100644 index 0000000..4dba116 --- /dev/null +++ b/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA-CBC.c @@ -0,0 +1,37 @@ +/** + *--------------------------------------------------------------------\n + * HSLU T&A Hochschule Luzern Technik+Architektur \n + *--------------------------------------------------------------------\n + * + * \brief model solution for ASYD assignment crypto 01 + * \file + * \author SIMON FREI, stefano.nicora@hslu.ch + * \date 04.10.22 + * + *-------------------------------------------------------------------- + */ + +#include +#include "XTEA-CBC.h" +#include "XTEA.h" + +#define INITIALIZATION_VECTOR { 'COFE', 'SIMI' } + +void encipher_CBC(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]) { + static uint32_t v_prev[2] = INITIALIZATION_VECTOR; + v[0] = v[0] ^ v_prev[0]; + v[1] = v[1] ^ v_prev[1]; + encipher(num_cycles, v, k); + v_prev[0] = v[0]; + v_prev[1] = v[1]; +} + +void decipher_CBC(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]) { + static uint32_t v_prev[2] = INITIALIZATION_VECTOR; + uint32_t v_input[2] = { v[0], v[1] }; + decipher(num_cycles, v, k); + v[0] = v[0] ^ v_prev[0]; + v[1] = v[1] ^ v_prev[1]; + v_prev[0] = v_input[0]; + v_prev[1] = v_input[1]; +} diff --git a/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA-CBC.h b/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA-CBC.h new file mode 100644 index 0000000..8ce9a35 --- /dev/null +++ b/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA-CBC.h @@ -0,0 +1,25 @@ +/** + *--------------------------------------------------------------------\n + * HSLU T&A Hochschule Luzern Technik+Architektur \n + *--------------------------------------------------------------------\n + * + * \brief model solution for ASYD assignment crypto 01 + * \file + * \author Stefano Nicora, stefano.nicora@hslu.ch + * \date 04.10.22 + * + *-------------------------------------------------------------------- + */ + +#ifndef XTEA_CBC_H_ +#define XTEA_CBC_H_ +#define _CRT_SECURE_NO_WARININGS +#pragma warning(disable:4996) + +#include + +void encipher_CBC(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]); + +void decipher_CBC(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]); + +#endif // !XTEA_CBC_H_ \ No newline at end of file diff --git a/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA.c b/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA.c new file mode 100644 index 0000000..fd2a74c --- /dev/null +++ b/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA.c @@ -0,0 +1,39 @@ +/** + *--------------------------------------------------------------------\n + * HSLU T&A Hochschule Luzern Technik+Architektur \n + *--------------------------------------------------------------------\n + * + * \brief model solution for ASYD assignment crypto 01 + * \file + * \author Stefano Nicora, stefano.nicora@hslu.ch + * \date 04.10.22 + * + *-------------------------------------------------------------------- + */ + +#include +#include "XTEA.h" + +void encipher(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]) { + unsigned int i; + const uint32_t delta = 0x9E3779B9; + uint32_t v0 = v[0], v1 = v[1], sum = 0; + for (i = 0; i < num_cycles; i++) { + v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); + sum += delta; + v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]); + } + v[0] = v0; v[1] = v1; +} + +void decipher(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]) { + unsigned int i; + const uint32_t delta = 0x9E3779B9; + uint32_t v0 = v[0], v1 = v[1], sum = delta * num_cycles; + for (i = 0; i < num_cycles; i++) { + v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]); + sum -= delta; + v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); + } + v[0] = v0; v[1] = v1; +} diff --git a/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA.h b/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA.h new file mode 100644 index 0000000..048d7dd --- /dev/null +++ b/SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA.h @@ -0,0 +1,26 @@ +/** + *--------------------------------------------------------------------\n + * HSLU T&A Hochschule Luzern Technik+Architektur \n + *--------------------------------------------------------------------\n + * + * \brief model solution for ASYD assignment crypto 01 + * \file + * \author Stefano Nicora, stefano.nicora@hslu.ch + * \date 04.10.22 + * + *-------------------------------------------------------------------- + */ + +#ifndef XTEA_H_ +#define XTEA_H_ +#define _CRT_SECURE_NO_WARININGS +#pragma warning(disable:4996) + +#include + +void encipher(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]); + +void decipher(unsigned int num_cycles, uint32_t v[2], uint32_t const k[4]); + +#endif // !XTEA_H_ + diff --git a/SW01-XTEA-Cipher/SW02-XTEA-CBC/main.c b/SW01-XTEA-Cipher/SW02-XTEA-CBC/main.c new file mode 100644 index 0000000..fd46996 --- /dev/null +++ b/SW01-XTEA-Cipher/SW02-XTEA-CBC/main.c @@ -0,0 +1,163 @@ +/** + *--------------------------------------------------------------------\n + * HSLU T&A Hochschule Luzern Technik+Architektur \n + *--------------------------------------------------------------------\n + * + * \brief model solution for ASYD assignment crypto 01 + * \file + * \author Stefano Nicora, stefano.nicora@hslu.ch + * \date 12.10.22 + * + *-------------------------------------------------------------------- + */ + +#include +#include +#include +#include +#include "XTEA-CBC.h" + +void encrypt(uint32_t key[4], uint8_t numberOfCycles); +void decrypt(uint32_t key[4], uint8_t numberOfCycles); + +/* source: https://en.wikipedia.org/wiki/XTEA */ + +/* filenames as of this code: + * plaintext.txt => holds the message you want to encrypt + * encrypted.cip => holds the encrypted message + * decrypted.txt => holds the decrypted output from "encrypted.cip" + */ + +void main(void) { + uint32_t key[4] = { 1, 3, 1, 2}; + encrypt(key, 32); + decrypt(key, 32); +} + +void encrypt(uint32_t key[4], uint8_t numberOfCycles) { + + /* duplicate code that hasn't been moved to a separate function for readability reasons */ + /* -------------------------------------------------------------------- */ + + /* variables */ + uint32_t amountOfCharsInFile = 0; + /* https://www.programiz.com/c-programming/c-file-input-output */ + FILE* fileHandle = fopen("plaintext.txt", "r"); + if (fileHandle == NULL) { + return; + } + + /* get total number of characters in file to encrypt */ + while (getc(fileHandle) != EOF) { + amountOfCharsInFile++; + } + + /* allocate enough memory for the whole file to be read */ + char* inputText = (char*)calloc(amountOfCharsInFile + 8, sizeof(char)); /* add 8 for extra padding in case the file contains some sort of rest in regards to amountOfCharsInFile % 8 */ + if (inputText == NULL) { + return; + } + + /* close and reopen file to start at the beginning of the file again */ + fclose(fileHandle); + fileHandle = fopen("plaintext.txt", "r"); + if (fileHandle == NULL) { + return; + } + + /* read the whole file into memory */ + for (uint32_t i = 0; i < amountOfCharsInFile; i++) { + inputText[i] = getc(fileHandle); + } + fclose(fileHandle); + /* -------------------------------------------------------------------- */ + + /* loop through the whole array to encrypt the whole message + This happens two blocks of 32bit (= 8 characters) at a time + Hence the increase of "i" by 8 + */ + for (uint32_t i = 0; i < amountOfCharsInFile; i += 8) { /* + 8 to compensate for the requested element size of the encipher function (32 bit) */ + /* dereferencing and forwarding the address of the key ensures, + * that we don't ready from a random address in memory. Rookie mistakes happen :) */ + encipher_CBC(numberOfCycles, &(inputText[i]), &(*key)); + } + + /* generate new file where encryption is stored */ + fileHandle = fopen("encrypted.cip", "w"); + if (fileHandle == NULL) { + return; + } + /* fill new file with content */ + /* to get the extra character padding we might unintentionally drop + * we need to read up to a multiple of 8 (32 bit) + */ + for (uint32_t i = 0; i < (amountOfCharsInFile + (8-(amountOfCharsInFile%8))); i++) { + putc(inputText[i], fileHandle); + } + fclose(fileHandle); + free(inputText); +} + +void decrypt(uint32_t key[4], uint8_t numberOfCycles) { + + /* duplicate code that hasn't been moved to a separate function for readability reasons */ + /* -------------------------------------------------------------------- */ + + /* variables */ + uint32_t amountOfCharsInFile = 0; + + /* https://www.programiz.com/c-programming/c-file-input-output */ + FILE* fileHandle = fopen("encrypted.cip", "r"); + if (fileHandle == NULL) { + return; + } + + /* get total number of characters in file to decrypt */ + while (getc(fileHandle) != EOF) { + amountOfCharsInFile++; + } + + /* allocate memory for the whole file to be read */ + char* inputText = (char*)calloc(amountOfCharsInFile + 10, sizeof(char)); /* add 10 for extra padding */ + if (inputText == NULL) { + return; + } + + /* close and reopen file to start at the beginning of the file again */ + fclose(fileHandle); + fileHandle = fopen("encrypted.cip", "r"); + if (fileHandle == NULL) { + return; + } + + /* read the whole file into memory */ + for (uint32_t i = 0; i < amountOfCharsInFile; i++) { + inputText[i] = getc(fileHandle); + } + fclose(fileHandle); + + /* -------------------------------------------------------------------- */ + + /* loop through the whole array to encrypt the whole message + This happens two blocks of 32bit (= 4 characters) at a time + Hence the increase of "i" by 8 + */ + + for (uint32_t i = 0; i < amountOfCharsInFile; i += 8) { /* i += 8*/ + /* dereferencing and forwarding the address of the key ensures, + * that we don't ready from a random address in memory. Rookie mistakes happen :) */ + decipher_CBC(numberOfCycles, &(inputText[i]), &(*key)); + } + + /* generate new file where the decryption is stored */ + fileHandle = fopen("decrypted.txt", "w"); + if (fileHandle == NULL) { + return; + } + /* fill new file with content */ + for (uint32_t i = 0; i < amountOfCharsInFile; i++) { + putc(inputText[i], fileHandle); + } + fclose(fileHandle); + free(inputText); +} \ No newline at end of file diff --git a/SW01-XTEA-Cipher/SW02-XTEA-CBC/musterloesung_XTEA b/SW01-XTEA-Cipher/SW02-XTEA-CBC/musterloesung_XTEA new file mode 100644 index 0000000..233f4bb Binary files /dev/null and b/SW01-XTEA-Cipher/SW02-XTEA-CBC/musterloesung_XTEA differ