first version of XTEA CBC

devel_freefall
Jonas Arnold 3 years ago
parent a1e00b5716
commit 5c24e160bc
  1. 10
      SW01-XTEA-Cipher/SW01-XTEA-Cipher.sln
  2. 141
      SW01-XTEA-Cipher/SW02-XTEA-CBC/SW02-XTEA-CBC.vcxproj
  3. 36
      SW01-XTEA-Cipher/SW02-XTEA-CBC/SW02-XTEA-CBC.vcxproj.filters
  4. 37
      SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA-CBC.c
  5. 25
      SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA-CBC.h
  6. 39
      SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA.c
  7. 26
      SW01-XTEA-Cipher/SW02-XTEA-CBC/XTEA.h
  8. 163
      SW01-XTEA-Cipher/SW02-XTEA-CBC/main.c
  9. BIN
      SW01-XTEA-Cipher/SW02-XTEA-CBC/musterloesung_XTEA

@ -5,6 +5,8 @@ VisualStudioVersion = 17.4.33103.184
MinimumVisualStudioVersion = 10.0.40219.1 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}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SW01-XTEA-Cipher", "SW01-XTEA-Cipher\SW01-XTEA-Cipher.vcxproj", "{0A55D2C2-29E0-459E-9D86-975DAA49A8FC}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SW02-XTEA-CBC", "SW02-XTEA-CBC\SW02-XTEA-CBC.vcxproj", "{101384C8-6D89-428A-A336-D82A488483B2}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64 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|x64.Build.0 = Release|x64
{0A55D2C2-29E0-459E-9D86-975DAA49A8FC}.Release|x86.ActiveCfg = Release|Win32 {0A55D2C2-29E0-459E-9D86-975DAA49A8FC}.Release|x86.ActiveCfg = Release|Win32
{0A55D2C2-29E0-459E-9D86-975DAA49A8FC}.Release|x86.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

@ -0,0 +1,141 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{101384c8-6d89-428a-a336-d82a488483b2}</ProjectGuid>
<RootNamespace>SW02XTEACBC</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.c" />
<ClCompile Include="XTEA-CBC.c" />
<ClCompile Include="XTEA.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="XTEA-CBC.h" />
<ClInclude Include="XTEA.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="XTEA.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="XTEA-CBC.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="XTEA.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="XTEA-CBC.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

@ -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 <stdint.h>
#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];
}

@ -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 <stdint.h>
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_

@ -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 <stdint.h>
#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;
}

@ -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 <stdint.h>
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_

@ -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 <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#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);
}
Loading…
Cancel
Save