commit 7aa7d93e48271ec1728eb13f5b40954481f56e01 Author: LoRd_MuldeR Date: Mon Oct 12 19:10:19 2020 +0200 Initial commit. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b89ed54 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.user +/.vs +/bin +/obj diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..0e259d4 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/MCrypt.sln b/MCrypt.sln new file mode 100644 index 0000000..726e043 --- /dev/null +++ b/MCrypt.sln @@ -0,0 +1,41 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30523.141 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MCrypt", "MCrypt.vcxproj", "{86D28793-713E-4CEC-9686-335514AC5EF0}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libMCrypt", "libMCrypt\libMCrypt.vcxproj", "{A4A3879C-BD2C-4304-AF66-7349CEF7E4C0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {86D28793-713E-4CEC-9686-335514AC5EF0}.Debug|x64.ActiveCfg = Debug|x64 + {86D28793-713E-4CEC-9686-335514AC5EF0}.Debug|x64.Build.0 = Debug|x64 + {86D28793-713E-4CEC-9686-335514AC5EF0}.Debug|x86.ActiveCfg = Debug|Win32 + {86D28793-713E-4CEC-9686-335514AC5EF0}.Debug|x86.Build.0 = Debug|Win32 + {86D28793-713E-4CEC-9686-335514AC5EF0}.Release|x64.ActiveCfg = Release|x64 + {86D28793-713E-4CEC-9686-335514AC5EF0}.Release|x64.Build.0 = Release|x64 + {86D28793-713E-4CEC-9686-335514AC5EF0}.Release|x86.ActiveCfg = Release|Win32 + {86D28793-713E-4CEC-9686-335514AC5EF0}.Release|x86.Build.0 = Release|Win32 + {A4A3879C-BD2C-4304-AF66-7349CEF7E4C0}.Debug|x64.ActiveCfg = Debug|x64 + {A4A3879C-BD2C-4304-AF66-7349CEF7E4C0}.Debug|x64.Build.0 = Debug|x64 + {A4A3879C-BD2C-4304-AF66-7349CEF7E4C0}.Debug|x86.ActiveCfg = Debug|Win32 + {A4A3879C-BD2C-4304-AF66-7349CEF7E4C0}.Debug|x86.Build.0 = Debug|Win32 + {A4A3879C-BD2C-4304-AF66-7349CEF7E4C0}.Release|x64.ActiveCfg = Release|x64 + {A4A3879C-BD2C-4304-AF66-7349CEF7E4C0}.Release|x64.Build.0 = Release|x64 + {A4A3879C-BD2C-4304-AF66-7349CEF7E4C0}.Release|x86.ActiveCfg = Release|Win32 + {A4A3879C-BD2C-4304-AF66-7349CEF7E4C0}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2182650E-DFB0-4AC3-86BD-758E8A39BAA2} + EndGlobalSection +EndGlobal diff --git a/MCrypt.vcxproj b/MCrypt.vcxproj new file mode 100644 index 0000000..3323d60 --- /dev/null +++ b/MCrypt.vcxproj @@ -0,0 +1,164 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + {a4a3879c-bd2c-4304-af66-7349cef7e4c0} + + + + 16.0 + Win32Proj + {86d28793-713e-4cec-9686-335514ac5ef0} + MCrypt + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)bin\$(Platform)\$(Configuration)\ + $(SolutionDir)obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)bin\$(Platform)\$(Configuration)\ + $(SolutionDir)obj\$(Platform)\$(Configuration)\ + + + true + $(SolutionDir)bin\$(Platform)\$(Configuration)\ + $(SolutionDir)obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)bin\$(Platform)\$(Configuration)\ + $(SolutionDir)obj\$(Platform)\$(Configuration)\ + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)libMCrypt\include + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)libMCrypt\include + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)libMCrypt\include + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $(SolutionDir)libMCrypt\include + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/MCrypt.vcxproj.filters b/MCrypt.vcxproj.filters new file mode 100644 index 0000000..0b7f47e --- /dev/null +++ b/MCrypt.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {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 + + + \ No newline at end of file diff --git a/libMCrypt/include/mcrypt.h b/libMCrypt/include/mcrypt.h new file mode 100644 index 0000000..a0a74f6 --- /dev/null +++ b/libMCrypt/include/mcrypt.h @@ -0,0 +1,29 @@ +/******************************************************************************/ +/* MCrypt, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#ifndef INC_MCYRPT_H +#define INC_MCYRPT_H + +#include +#include + +extern const char *const LIBMCRYPT_VERSION; +extern const char* const LIBMCRYPT_BUILD; + +typedef uintptr_t mcrypt_t; + +int mcrypt_generate_seed(uint64_t* const seed); + +mcrypt_t mcrypt_init(const uint64_t salt, const char* const passphrase); +void mcrypt_free(const mcrypt_t context); + +int mcrypt_enc_process(const mcrypt_t context, const uint8_t* const input, uint8_t* const output, size_t length); +int mcrypt_enc_process_inplace(const mcrypt_t context, uint8_t* const buffer, size_t length); + +int mcrypt_dec_process(const mcrypt_t context, const uint8_t* const input, uint8_t* const output, size_t length); +int mcrypt_dec_process_inplace(const mcrypt_t context, uint8_t* const buffer, size_t length); + + +#endif diff --git a/libMCrypt/libMCrypt.vcxproj b/libMCrypt/libMCrypt.vcxproj new file mode 100644 index 0000000..4885fd2 --- /dev/null +++ b/libMCrypt/libMCrypt.vcxproj @@ -0,0 +1,170 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + 16.0 + Win32Proj + {a4a3879c-bd2c-4304-af66-7349cef7e4c0} + libMCrypt + 10.0 + + + + StaticLibrary + true + v142 + Unicode + + + StaticLibrary + false + v142 + true + Unicode + + + StaticLibrary + true + v142 + Unicode + + + StaticLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)bin\$(Platform)\$(Configuration)\ + $(SolutionDir)obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)bin\$(Platform)\$(Configuration)\ + $(SolutionDir)obj\$(Platform)\$(Configuration)\ + + + true + $(SolutionDir)bin\$(Platform)\$(Configuration)\ + $(SolutionDir)obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)bin\$(Platform)\$(Configuration)\ + $(SolutionDir)obj\$(Platform)\$(Configuration)\ + + + + Level3 + true + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + NotUsing + $(ProjectDir)include + + + + + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + NotUsing + $(ProjectDir)include + + + + + true + true + true + + + + + Level3 + true + _DEBUG;_LIB;%(PreprocessorDefinitions) + true + NotUsing + $(ProjectDir)include + + + + + true + + + + + Level3 + true + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + true + NotUsing + $(ProjectDir)include + + + + + true + true + true + + + + + + \ No newline at end of file diff --git a/libMCrypt/libMCrypt.vcxproj.filters b/libMCrypt/libMCrypt.vcxproj.filters new file mode 100644 index 0000000..7251418 --- /dev/null +++ b/libMCrypt/libMCrypt.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {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 + + + + + Header Files + + + + + Source Files + + + \ No newline at end of file diff --git a/libMCrypt/src/mcrypt.c b/libMCrypt/src/mcrypt.c new file mode 100644 index 0000000..54ef665 --- /dev/null +++ b/libMCrypt/src/mcrypt.c @@ -0,0 +1,295 @@ +/******************************************************************************/ +/* MCrypt, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#ifdef _WIN32 +#define _CRT_RAND_S 1 +#endif + +#include +#include + +const char* const LIBMCRYPT_VERSION = "1.0.0"; +const char* const LIBMCRYPT_BUILD = __DATE__" "__TIME__; + +typedef struct +{ + uint8_t box[256U][256U]; + uint8_t inv[256U][256U]; + uint8_t off[256U]; + uint8_t pos; +} +crypt_state_t; + +typedef struct +{ + uint32_t a, b, c, d; + uint32_t counter; +} +rand_state_t; + +// ========================================================================== +// Utilities +// ========================================================================== + +static int random_bytes(uint8_t* const buffer, const size_t length) +{ +#ifdef _WIN32 + size_t pos = 0U; + while (pos < length) + { + const size_t bytes_left = length - pos; + const size_t bytes_copy = (bytes_left < sizeof(uint32_t)) ? bytes_left : sizeof(uint32_t); + uint32_t temp; + if (rand_s(&temp) != 0) + { + return -1; + } + for (size_t i = 0; i < bytes_copy; ++i) + { + buffer[pos++] = (uint8_t)(temp & 0xFF); + temp >>= 8; + } + } + return 0; +#else +#ifdef __unix__ + static const char* const PATH[] = { "/dev/urandom", "/dev/random" }; + int result = -1; + for (size_t i = 0; (i < 2U) && (result < 0); ++i) + { + const int fd = open(PATH[i], O_RDONLY); + if (fd >= 0) + { + if (read(fd, buffer, length) < length) + { + result = 0; + } + close(fd); + } + } + return result; +#else +#error Unsupported target platform! +#endif +#endif +} + +static void erase(void* ptr, const size_t length) +{ + volatile uint8_t* buffer = ptr; + for (size_t i = 0U; i < length; ++i) + { + buffer[i] = 0U; + } +} + +// ========================================================================== +// Hash function +// ========================================================================== + +static void hash_update(uint64_t* const h, const uint8_t* const data, const size_t data_len) +{ + for (size_t i = 0U; i < data_len; ++i) + { + *h ^= data[i]; + *h *= 0x00000100000001B3ull; + } +} + +static uint64_t hash_code(const uint64_t salt, const uint8_t pepper, const uint8_t* const data, const size_t data_len) +{ + uint64_t h = 0xCBF29CE484222325ull; + hash_update(&h, (uint8_t*)&salt, sizeof(uint64_t)); + hash_update(&h, &pepper, 1U); + hash_update(&h, data, data_len); + return h; +} + +// ========================================================================== +// PRNG +// ========================================================================== + +static void random_init(rand_state_t* const state, const uint64_t seed_0, const uint64_t seed_1) +{ + state->counter = 0U; + state->a = (uint32_t)(seed_0 & 0xFFFFFFFF); + state->b = (uint32_t)((seed_0 >> 32) & 0xFFFFFFFF); + state->c = (uint32_t)(seed_1 & 0xFFFFFFFF); + state->d = (uint32_t)((seed_1 >> 32) & 0xFFFFFFFF); +} + +static uint32_t random_next(rand_state_t* const state) +{ + uint32_t t = state->d; + const uint32_t s = state->a; + state->d = state->c; + state->c = state->b; + state->b = s; + t ^= t >> 2; + t ^= t << 1; + t ^= s ^ (s << 4); + state->a = t; + return t + (state->counter += 362437U); +} + +static void random_seed(rand_state_t* const state, const uint64_t salt, const uint8_t* const key, const size_t key_len) +{ + random_init(state, hash_code(salt, 0x6B, key, key_len), hash_code(salt, 0x94, key, key_len)); + for (size_t i = 0U; i < 13U; ++i) + { + random_next(state); + } +} + +// ========================================================================== +// Initialization +// ========================================================================== + +static void initialize_state(crypt_state_t *const crypt_state, const uint64_t salt, const uint8_t *const key, const size_t key_len) +{ + rand_state_t rand_state; + for (size_t r = 0U; r < 256U; ++r) + { + random_seed(&rand_state, salt + r, key, key_len); + crypt_state->off[r] = (uint8_t)random_next(&rand_state); + for (size_t i = 0U; i < 256U; ++i) + { + const size_t j = random_next(&rand_state) % (i + 1U); + if (j != i) + { + crypt_state->box[r][i] = crypt_state->box[r][j]; + } + crypt_state->box[r][j] = (uint8_t)i; + } + for (size_t i = 0U; i < 256U; ++i) + { + const size_t j = crypt_state->box[r][i]; + crypt_state->inv[r][j] = (uint8_t)i; + } + } + random_seed(&rand_state, salt + 9973U, key, key_len); + crypt_state->pos = (uint8_t)random_next(&rand_state); + erase(&rand_state, sizeof(rand_state_t)); +} + +// ========================================================================== +// Encrypt / Decrypt +// ========================================================================== + +static uint8_t process_enc(crypt_state_t* const crypt_state, uint8_t value) +{ + for (size_t i = 0U; i < 256U; ++i) + { + value = crypt_state->box[i][(value + crypt_state->off[i]) & 0xFF]; + } + ++crypt_state->off[crypt_state->pos++]; + return value; +} + +static uint8_t process_dec(crypt_state_t* const crypt_state, uint8_t value) +{ + size_t i = 256U; + while(i--) + { + value = (crypt_state->inv[i][value] - crypt_state->off[i]) & 0xFF; + } + ++crypt_state->off[crypt_state->pos++]; + return value; +} + +// ========================================================================== +// Public API +// ========================================================================== + +int mcrypt_generate_seed(uint64_t *const seed) +{ + if (seed) + { + return random_bytes((uint8_t*)seed, sizeof(uint64_t)); + } + return -1; +} + +mcrypt_t mcrypt_init(const uint64_t salt, const char* const passphrase) +{ + if (!passphrase) + { + return ((mcrypt_t)NULL); + } + crypt_state_t *const state = (crypt_state_t*) malloc(sizeof(crypt_state_t)); + if (!state) + { + return ((mcrypt_t)NULL); + } + initialize_state(state, salt, (uint8_t*)passphrase, strlen(passphrase)); + return ((mcrypt_t)state); +} + +int mcrypt_enc_process(const mcrypt_t context, const uint8_t *const input, uint8_t *const output, size_t length) +{ + crypt_state_t* const state = (crypt_state_t*)context; + if (!context) + { + return -1; + } + for (size_t i = 0; i < length; ++i) + { + output[i] = process_enc(state, input[i]); + } + return 0; +} + +int mcrypt_enc_process_inplace(const mcrypt_t context, uint8_t* const buffer, size_t length) +{ + crypt_state_t* const state = (crypt_state_t*)context; + if (!context) + { + return -1; + } + for (size_t i = 0; i < length; ++i) + { + buffer[i] = process_enc(state, buffer[i]); + } + return 0; +} + + +int mcrypt_dec_process(const mcrypt_t context, const uint8_t* const input, uint8_t* const output, size_t length) +{ + crypt_state_t* const state = (crypt_state_t*)context; + if (!context) + { + return -1; + } + for (size_t i = 0; i < length; ++i) + { + output[i] = process_dec(state, input[i]); + } + return 0; +} + +int mcrypt_dec_process_inplace(const mcrypt_t context, uint8_t* const buffer, size_t length) +{ + crypt_state_t* const state = (crypt_state_t*)context; + if (!context) + { + return -1; + } + for (size_t i = 0; i < length; ++i) + { + buffer[i] = process_dec(state, buffer[i]); + } + return 0; +} + +void mcrypt_free(const mcrypt_t context) +{ + crypt_state_t* const state = (crypt_state_t*)context; + if (context) + { + erase((void*)context, sizeof(crypt_state_t)); + free((void*)context); + } +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..0c2a35a --- /dev/null +++ b/src/main.c @@ -0,0 +1,69 @@ +/******************************************************************************/ +/* MCrypt, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#include +#include +#include + +int main() +{ + printf("MCrypt Demo [%s]\n", __DATE__" "__TIME__); + printf("Using libMCrypt v%s [%s]\n\n", LIBMCRYPT_VERSION, LIBMCRYPT_BUILD); + + const char *const plaintext = "AAAALorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."; + const size_t length = strlen(plaintext) + 1U; + + fprintf(stderr, "\"%s\"\n\n", plaintext); + + /* ---- Allocate buffers ---- */ + + char* ciphertext = malloc(length); + if (!ciphertext) + { + abort(); + } + + char* decoded = malloc(length); + if (!decoded) + { + abort(); + } + + /* ---- Generate seed ---- */ + + uint64_t seed; + if (mcrypt_generate_seed(&seed) != 0) + { + abort(); + } + + /* ---- Encryt message ---- */ + + mcrypt_t enc = mcrypt_init(seed, "Yp#QaQfH)t`DT23q"); + if (!enc) + { + abort(); + } + + mcrypt_enc_process(enc, plaintext, ciphertext, length); + mcrypt_free(enc); + + fprintf(stderr, "\"%s\"\n\n", ciphertext); + + + /* ---- Decryt message ---- */ + + mcrypt_t dec = mcrypt_init(seed, "Yp#QaQfH)t`DT23q"); + if (!dec) + { + abort(); + } + + mcrypt_dec_process(dec, ciphertext, decoded, length); + mcrypt_free(dec); + + fprintf(stderr, "\"%s\"\n\n", decoded); + +} \ No newline at end of file