Skip to content

Commit

Permalink
Merge pull request #2 from nooperation/split_into_lib
Browse files Browse the repository at this point in the history
Splitting project into a lib that can be exported by other projects
  • Loading branch information
nooperation authored Mar 19, 2018
2 parents 1b89127 + 075a113 commit d642688
Show file tree
Hide file tree
Showing 22 changed files with 980 additions and 331 deletions.
98 changes: 98 additions & 0 deletions LibUserPreferences/LibUserPreferences.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "LibUserPreferences.h"

#include "../UserPreferences.Shared/Common.h"
#include "../UserPreferences.Shared/Encryption.h"

DllExport APIResult DecryptData(
_In_ const uint8_t *encrypted_buffer,
_In_ size_t encrypted_buffer_size,
_In_ const char *machine_guid,
_In_ const char *salt_string,
_Out_ uint8_t *out_decrypted_buffer,
_Inout_ size_t *out_decrypted_buffer_size)
{
if (encrypted_buffer == nullptr || out_decrypted_buffer_size == nullptr)
{
return kResult_GeneralFailure;
}

std::string guid;
if (machine_guid == nullptr)
{
guid = UserPreferences::Common::GetMachineGuid();
}
else
{
guid = machine_guid;
}

std::string salt;
if (salt_string == nullptr)
{
salt = UserPreferences::Common::GetDefaultSalt();
}
else
{
salt = salt_string;
}

const auto encrypted = std::vector<uint8_t>(&encrypted_buffer[0], &encrypted_buffer[encrypted_buffer_size]);
const auto decrypted = UserPreferences::Encryption::DecryptData(encrypted, guid, salt);

if (out_decrypted_buffer == nullptr || *out_decrypted_buffer_size < decrypted.size())
{
*out_decrypted_buffer_size = decrypted.size();
return kResult_BufferTooSmall;
}

*out_decrypted_buffer_size = decrypted.size();
memcpy(out_decrypted_buffer, &decrypted[0], *out_decrypted_buffer_size);
return kResult_GeneralSuccess;
}

DllExport APIResult EncryptData(
_In_ const uint8_t *plaintext_data,
_In_ size_t plaintext_data_size,
_In_ const char *machine_guid,
_In_ const char *salt_string,
_Out_ uint8_t *out_encrypted_buffer,
_Inout_ size_t *out_encrypted_buffer_size)
{
if (plaintext_data == nullptr || out_encrypted_buffer_size == nullptr)
{
return kResult_GeneralFailure;
}

std::string guid;
if (machine_guid == nullptr)
{
guid = UserPreferences::Common::GetMachineGuid();
}
else
{
guid = machine_guid;
}

std::string salt;
if (salt_string == nullptr)
{
salt = UserPreferences::Common::GetDefaultSalt();
}
else
{
salt = salt_string;
}

const auto plaintext = std::vector<uint8_t>(&plaintext_data[0], &plaintext_data[plaintext_data_size]);
const auto encrypted = UserPreferences::Encryption::EncryptData(plaintext, guid, salt);

if (out_encrypted_buffer == nullptr || *out_encrypted_buffer_size < encrypted.size())
{
*out_encrypted_buffer_size = encrypted.size();
return kResult_BufferTooSmall;
}

*out_encrypted_buffer_size = encrypted.size();
memcpy(out_encrypted_buffer, &encrypted[0], *out_encrypted_buffer_size);
return kResult_GeneralSuccess;
}
60 changes: 60 additions & 0 deletions LibUserPreferences/LibUserPreferences.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once
#define DllExport __declspec( dllexport )

#include <sal.h>
#include <cstdint>

extern "C"
{
enum APIResult {
kResult_GeneralSuccess = 0,
kResult_GeneralFailure = -1,
kResult_BufferTooSmall = -2
};

/// <summary>
/// Decrypts encrypted data
/// </summary>
/// <param name="encrypted_buffer">Encrypted bytes buffer to decrypt</param>
/// <param name="encrypted_buffer_size">Encrypted bytes buffer size in bytes</param>
/// <param name="machine_guid">Optional machine GUID. Defaults to current machine GUID if not supplied.</param>
/// <param name="salt_string">Optional salt. Defaults to last known salt if not supplied.</param>
/// <param name="out_decrypted_buffer">Optional decrypted buffer</param>
/// <param name="out_encrypted_buffer_size">Decrypted buffer size in bytes. Will be set to the required size if decrypted buffer is NULL. </param>
/// <returns>
/// kResult_GeneralSuccess on success.
/// kResult_BufferTooSmall if decrypted buffer size is too small or decrypted buffer was not provided.
/// kResult_GeneralFailure on failure.
///</returns>
DllExport APIResult DecryptData(
_In_ const uint8_t *encrypted_buffer,
_In_ size_t encrypted_buffer_size,
_In_opt_ const char *machine_guid,
_In_opt_ const char *salt_string,
_Out_opt_ uint8_t *out_decrypted_buffer,
_Inout_ size_t *out_decrypted_buffer_size
);

/// <summary>
/// Encrypts data
/// </summary>
/// <param name="plaintext_data">Plaintext to enctypt</param>
/// <param name="plaintext_data_size">Plaintext size in bytes</param>
/// <param name="machine_guid">Optional machine GUID. Defaults to current machine GUID if not supplied.</param>
/// <param name="salt_string">Optional salt. Defaults to last known salt if not supplied.</param>
/// <param name="out_encrypted_buffer">Optional enctypted buffer</param>
/// <param name="out_encrypted_buffer_size">Encrypted buffer size in bytes. Will be set to the required size if enctypted buffer is NULL. </param>
/// <returns>
/// kResult_GeneralSuccess on success.
/// kResult_BufferTooSmall if encrypted buffer size is too small or encrypted buffer was not provided.
/// kResult_GeneralFailure on failure.
///</returns>
DllExport APIResult EncryptData(
_In_ const uint8_t *plaintext_data,
_In_ size_t plaintext_data_size,
_In_opt_ const char *machine_guid,
_In_opt_ const char *salt_string,
_Out_opt_ uint8_t *out_encrypted_buffer,
_Inout_ size_t *out_encrypted_buffer_size
);
}
158 changes: 158 additions & 0 deletions LibUserPreferences/LibUserPreferences.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" 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>15.0</VCProjectVersion>
<ProjectGuid>{E8A8F157-EBAE-43E4-90AE-32A8DAC51096}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>LibUserPreferences</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</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" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;LIBUSERPREFERENCES_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(OutputPath);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;LIBUSERPREFERENCES_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(OutputPath);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;LIBUSERPREFERENCES_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(OutputPath);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;LIBUSERPREFERENCES_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(OutputPath);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="LibUserPreferences.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="LibUserPreferences.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
27 changes: 27 additions & 0 deletions LibUserPreferences/LibUserPreferences.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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;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;hm;inl;inc;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>
<ClInclude Include="LibUserPreferences.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="LibUserPreferences.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

#include <unordered_map>

#include "../UserPreferencesExplorer/Encryption.cpp"
#include "../UserPreferencesExplorer/Utils.cpp"
#include "../UserPreferences.Shared/Encryption.h"
#include "../UserPreferences.Shared/Common.h"

#include "../UserPreferences.Shared/Encryption.cpp"
#include "../UserPreferences.Shared/Common.cpp"

TEST_CASE("Encryption")
{
Expand All @@ -26,14 +29,14 @@ TEST_CASE("Encryption")
auto data_to_encrypt = std::vector<uint8_t>(plaintext.begin(), plaintext.end());
data_to_encrypt.push_back('\0');

auto result = Encryption::EncryptData(data_to_encrypt, guid, salt);
auto result = UserPreferences::Encryption::EncryptData(data_to_encrypt, guid, salt);

REQUIRE(result == ciphertext);
}

SECTION("Decrypt")
{
auto result = Encryption::DecryptData(ciphertext, guid, salt);
auto result = UserPreferences::Encryption::DecryptData(ciphertext, guid, salt);
auto result_string = std::string(result.begin(), result.end() - 1);

REQUIRE(result_string == plaintext);
Expand Down
Loading

0 comments on commit d642688

Please sign in to comment.