diff --git a/frontend/MCrypt.vcxproj b/frontend/MCrypt.vcxproj index 9ca8347..55fc8e6 100644 --- a/frontend/MCrypt.vcxproj +++ b/frontend/MCrypt.vcxproj @@ -19,13 +19,20 @@ + + {a4a3879c-bd2c-4304-af66-7349cef7e4c0} + + + + + 16.0 Win32Proj diff --git a/frontend/MCrypt.vcxproj.filters b/frontend/MCrypt.vcxproj.filters index 0b7f47e..efc4c65 100644 --- a/frontend/MCrypt.vcxproj.filters +++ b/frontend/MCrypt.vcxproj.filters @@ -18,5 +18,22 @@ Source Files + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/frontend/src/crc.c b/frontend/src/crc.c new file mode 100644 index 0000000..0b93072 --- /dev/null +++ b/frontend/src/crc.c @@ -0,0 +1,152 @@ +/******************************************************************************/ +/* MCrypt, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#include "crc.h" + +static const uint64_t CRC64_TAB[256] = +{ + 0x0000000000000000ULL, 0x42f0e1eba9ea3693ULL, + 0x85e1c3d753d46d26ULL, 0xc711223cfa3e5bb5ULL, + 0x493366450e42ecdfULL, 0x0bc387aea7a8da4cULL, + 0xccd2a5925d9681f9ULL, 0x8e224479f47cb76aULL, + 0x9266cc8a1c85d9beULL, 0xd0962d61b56fef2dULL, + 0x17870f5d4f51b498ULL, 0x5577eeb6e6bb820bULL, + 0xdb55aacf12c73561ULL, 0x99a54b24bb2d03f2ULL, + 0x5eb4691841135847ULL, 0x1c4488f3e8f96ed4ULL, + 0x663d78ff90e185efULL, 0x24cd9914390bb37cULL, + 0xe3dcbb28c335e8c9ULL, 0xa12c5ac36adfde5aULL, + 0x2f0e1eba9ea36930ULL, 0x6dfeff5137495fa3ULL, + 0xaaefdd6dcd770416ULL, 0xe81f3c86649d3285ULL, + 0xf45bb4758c645c51ULL, 0xb6ab559e258e6ac2ULL, + 0x71ba77a2dfb03177ULL, 0x334a9649765a07e4ULL, + 0xbd68d2308226b08eULL, 0xff9833db2bcc861dULL, + 0x388911e7d1f2dda8ULL, 0x7a79f00c7818eb3bULL, + 0xcc7af1ff21c30bdeULL, 0x8e8a101488293d4dULL, + 0x499b3228721766f8ULL, 0x0b6bd3c3dbfd506bULL, + 0x854997ba2f81e701ULL, 0xc7b97651866bd192ULL, + 0x00a8546d7c558a27ULL, 0x4258b586d5bfbcb4ULL, + 0x5e1c3d753d46d260ULL, 0x1cecdc9e94ace4f3ULL, + 0xdbfdfea26e92bf46ULL, 0x990d1f49c77889d5ULL, + 0x172f5b3033043ebfULL, 0x55dfbadb9aee082cULL, + 0x92ce98e760d05399ULL, 0xd03e790cc93a650aULL, + 0xaa478900b1228e31ULL, 0xe8b768eb18c8b8a2ULL, + 0x2fa64ad7e2f6e317ULL, 0x6d56ab3c4b1cd584ULL, + 0xe374ef45bf6062eeULL, 0xa1840eae168a547dULL, + 0x66952c92ecb40fc8ULL, 0x2465cd79455e395bULL, + 0x3821458aada7578fULL, 0x7ad1a461044d611cULL, + 0xbdc0865dfe733aa9ULL, 0xff3067b657990c3aULL, + 0x711223cfa3e5bb50ULL, 0x33e2c2240a0f8dc3ULL, + 0xf4f3e018f031d676ULL, 0xb60301f359dbe0e5ULL, + 0xda050215ea6c212fULL, 0x98f5e3fe438617bcULL, + 0x5fe4c1c2b9b84c09ULL, 0x1d14202910527a9aULL, + 0x93366450e42ecdf0ULL, 0xd1c685bb4dc4fb63ULL, + 0x16d7a787b7faa0d6ULL, 0x5427466c1e109645ULL, + 0x4863ce9ff6e9f891ULL, 0x0a932f745f03ce02ULL, + 0xcd820d48a53d95b7ULL, 0x8f72eca30cd7a324ULL, + 0x0150a8daf8ab144eULL, 0x43a04931514122ddULL, + 0x84b16b0dab7f7968ULL, 0xc6418ae602954ffbULL, + 0xbc387aea7a8da4c0ULL, 0xfec89b01d3679253ULL, + 0x39d9b93d2959c9e6ULL, 0x7b2958d680b3ff75ULL, + 0xf50b1caf74cf481fULL, 0xb7fbfd44dd257e8cULL, + 0x70eadf78271b2539ULL, 0x321a3e938ef113aaULL, + 0x2e5eb66066087d7eULL, 0x6cae578bcfe24bedULL, + 0xabbf75b735dc1058ULL, 0xe94f945c9c3626cbULL, + 0x676dd025684a91a1ULL, 0x259d31cec1a0a732ULL, + 0xe28c13f23b9efc87ULL, 0xa07cf2199274ca14ULL, + 0x167ff3eacbaf2af1ULL, 0x548f120162451c62ULL, + 0x939e303d987b47d7ULL, 0xd16ed1d631917144ULL, + 0x5f4c95afc5edc62eULL, 0x1dbc74446c07f0bdULL, + 0xdaad56789639ab08ULL, 0x985db7933fd39d9bULL, + 0x84193f60d72af34fULL, 0xc6e9de8b7ec0c5dcULL, + 0x01f8fcb784fe9e69ULL, 0x43081d5c2d14a8faULL, + 0xcd2a5925d9681f90ULL, 0x8fdab8ce70822903ULL, + 0x48cb9af28abc72b6ULL, 0x0a3b7b1923564425ULL, + 0x70428b155b4eaf1eULL, 0x32b26afef2a4998dULL, + 0xf5a348c2089ac238ULL, 0xb753a929a170f4abULL, + 0x3971ed50550c43c1ULL, 0x7b810cbbfce67552ULL, + 0xbc902e8706d82ee7ULL, 0xfe60cf6caf321874ULL, + 0xe224479f47cb76a0ULL, 0xa0d4a674ee214033ULL, + 0x67c58448141f1b86ULL, 0x253565a3bdf52d15ULL, + 0xab1721da49899a7fULL, 0xe9e7c031e063acecULL, + 0x2ef6e20d1a5df759ULL, 0x6c0603e6b3b7c1caULL, + 0xf6fae5c07d3274cdULL, 0xb40a042bd4d8425eULL, + 0x731b26172ee619ebULL, 0x31ebc7fc870c2f78ULL, + 0xbfc9838573709812ULL, 0xfd39626eda9aae81ULL, + 0x3a28405220a4f534ULL, 0x78d8a1b9894ec3a7ULL, + 0x649c294a61b7ad73ULL, 0x266cc8a1c85d9be0ULL, + 0xe17dea9d3263c055ULL, 0xa38d0b769b89f6c6ULL, + 0x2daf4f0f6ff541acULL, 0x6f5faee4c61f773fULL, + 0xa84e8cd83c212c8aULL, 0xeabe6d3395cb1a19ULL, + 0x90c79d3fedd3f122ULL, 0xd2377cd44439c7b1ULL, + 0x15265ee8be079c04ULL, 0x57d6bf0317edaa97ULL, + 0xd9f4fb7ae3911dfdULL, 0x9b041a914a7b2b6eULL, + 0x5c1538adb04570dbULL, 0x1ee5d94619af4648ULL, + 0x02a151b5f156289cULL, 0x4051b05e58bc1e0fULL, + 0x87409262a28245baULL, 0xc5b073890b687329ULL, + 0x4b9237f0ff14c443ULL, 0x0962d61b56fef2d0ULL, + 0xce73f427acc0a965ULL, 0x8c8315cc052a9ff6ULL, + 0x3a80143f5cf17f13ULL, 0x7870f5d4f51b4980ULL, + 0xbf61d7e80f251235ULL, 0xfd913603a6cf24a6ULL, + 0x73b3727a52b393ccULL, 0x31439391fb59a55fULL, + 0xf652b1ad0167feeaULL, 0xb4a25046a88dc879ULL, + 0xa8e6d8b54074a6adULL, 0xea16395ee99e903eULL, + 0x2d071b6213a0cb8bULL, 0x6ff7fa89ba4afd18ULL, + 0xe1d5bef04e364a72ULL, 0xa3255f1be7dc7ce1ULL, + 0x64347d271de22754ULL, 0x26c49cccb40811c7ULL, + 0x5cbd6cc0cc10fafcULL, 0x1e4d8d2b65facc6fULL, + 0xd95caf179fc497daULL, 0x9bac4efc362ea149ULL, + 0x158e0a85c2521623ULL, 0x577eeb6e6bb820b0ULL, + 0x906fc95291867b05ULL, 0xd29f28b9386c4d96ULL, + 0xcedba04ad0952342ULL, 0x8c2b41a1797f15d1ULL, + 0x4b3a639d83414e64ULL, 0x09ca82762aab78f7ULL, + 0x87e8c60fded7cf9dULL, 0xc51827e4773df90eULL, + 0x020905d88d03a2bbULL, 0x40f9e43324e99428ULL, + 0x2cffe7d5975e55e2ULL, 0x6e0f063e3eb46371ULL, + 0xa91e2402c48a38c4ULL, 0xebeec5e96d600e57ULL, + 0x65cc8190991cb93dULL, 0x273c607b30f68faeULL, + 0xe02d4247cac8d41bULL, 0xa2dda3ac6322e288ULL, + 0xbe992b5f8bdb8c5cULL, 0xfc69cab42231bacfULL, + 0x3b78e888d80fe17aULL, 0x7988096371e5d7e9ULL, + 0xf7aa4d1a85996083ULL, 0xb55aacf12c735610ULL, + 0x724b8ecdd64d0da5ULL, 0x30bb6f267fa73b36ULL, + 0x4ac29f2a07bfd00dULL, 0x08327ec1ae55e69eULL, + 0xcf235cfd546bbd2bULL, 0x8dd3bd16fd818bb8ULL, + 0x03f1f96f09fd3cd2ULL, 0x41011884a0170a41ULL, + 0x86103ab85a2951f4ULL, 0xc4e0db53f3c36767ULL, + 0xd8a453a01b3a09b3ULL, 0x9a54b24bb2d03f20ULL, + 0x5d45907748ee6495ULL, 0x1fb5719ce1045206ULL, + 0x919735e51578e56cULL, 0xd367d40ebc92d3ffULL, + 0x1476f63246ac884aULL, 0x568617d9ef46bed9ULL, + 0xe085162ab69d5e3cULL, 0xa275f7c11f7768afULL, + 0x6564d5fde549331aULL, 0x279434164ca30589ULL, + 0xa9b6706fb8dfb2e3ULL, 0xeb46918411358470ULL, + 0x2c57b3b8eb0bdfc5ULL, 0x6ea7525342e1e956ULL, + 0x72e3daa0aa188782ULL, 0x30133b4b03f2b111ULL, + 0xf7021977f9cceaa4ULL, 0xb5f2f89c5026dc37ULL, + 0x3bd0bce5a45a6b5dULL, 0x79205d0e0db05dceULL, + 0xbe317f32f78e067bULL, 0xfcc19ed95e6430e8ULL, + 0x86b86ed5267cdbd3ULL, 0xc4488f3e8f96ed40ULL, + 0x0359ad0275a8b6f5ULL, 0x41a94ce9dc428066ULL, + 0xcf8b0890283e370cULL, 0x8d7be97b81d4019fULL, + 0x4a6acb477bea5a2aULL, 0x089a2aacd2006cb9ULL, + 0x14dea25f3af9026dULL, 0x562e43b4931334feULL, + 0x913f6188692d6f4bULL, 0xd3cf8063c0c759d8ULL, + 0x5dedc41a34bbeeb2ULL, 0x1f1d25f19d51d821ULL, + 0xd80c07cd676f8394ULL, 0x9afce626ce85b507ULL, +}; + +uint64_t crc64_update(uint64_t crc, const uint8_t *const data, const size_t data_len) +{ + for (size_t i = 0U; i < data_len; ++i) + { + crc = CRC64_TAB[((crc >> 56) ^ data[i]) & 0xFF] ^ (crc << 8); + } + return crc; +} + +uint64_t crc64_finish(const uint64_t crc) +{ + return crc ^ ~0U; +} diff --git a/frontend/src/crc.h b/frontend/src/crc.h new file mode 100644 index 0000000..a984126 --- /dev/null +++ b/frontend/src/crc.h @@ -0,0 +1,17 @@ +/******************************************************************************/ +/* MCrypt, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#ifndef INC_CRC_H +#define INC_CRC_H + +#include +#include +#include + +#define CRC_INITIALIZER ((uint64_t)~0U) +uint64_t crc64_update(uint64_t crc, const uint8_t *const data, const size_t data_len); +uint64_t crc64_finish(const uint64_t crc); + +#endif diff --git a/frontend/src/main.c b/frontend/src/main.c index 2c116a9..59cedb8 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -3,184 +3,13 @@ /* This work has been released under the CC0 1.0 Universal license! */ /******************************************************************************/ -#define WIN32_LEAN_AND_MEAN 1 #define _CRT_SECURE_NO_WARNINGS 1 #include -#include +#include "utils.h" +#include "crc.h" #include #include -#include -#include -#include - -static const uint64_t CRC64_TAB[256] = -{ - 0x0000000000000000ULL, 0x42f0e1eba9ea3693ULL, - 0x85e1c3d753d46d26ULL, 0xc711223cfa3e5bb5ULL, - 0x493366450e42ecdfULL, 0x0bc387aea7a8da4cULL, - 0xccd2a5925d9681f9ULL, 0x8e224479f47cb76aULL, - 0x9266cc8a1c85d9beULL, 0xd0962d61b56fef2dULL, - 0x17870f5d4f51b498ULL, 0x5577eeb6e6bb820bULL, - 0xdb55aacf12c73561ULL, 0x99a54b24bb2d03f2ULL, - 0x5eb4691841135847ULL, 0x1c4488f3e8f96ed4ULL, - 0x663d78ff90e185efULL, 0x24cd9914390bb37cULL, - 0xe3dcbb28c335e8c9ULL, 0xa12c5ac36adfde5aULL, - 0x2f0e1eba9ea36930ULL, 0x6dfeff5137495fa3ULL, - 0xaaefdd6dcd770416ULL, 0xe81f3c86649d3285ULL, - 0xf45bb4758c645c51ULL, 0xb6ab559e258e6ac2ULL, - 0x71ba77a2dfb03177ULL, 0x334a9649765a07e4ULL, - 0xbd68d2308226b08eULL, 0xff9833db2bcc861dULL, - 0x388911e7d1f2dda8ULL, 0x7a79f00c7818eb3bULL, - 0xcc7af1ff21c30bdeULL, 0x8e8a101488293d4dULL, - 0x499b3228721766f8ULL, 0x0b6bd3c3dbfd506bULL, - 0x854997ba2f81e701ULL, 0xc7b97651866bd192ULL, - 0x00a8546d7c558a27ULL, 0x4258b586d5bfbcb4ULL, - 0x5e1c3d753d46d260ULL, 0x1cecdc9e94ace4f3ULL, - 0xdbfdfea26e92bf46ULL, 0x990d1f49c77889d5ULL, - 0x172f5b3033043ebfULL, 0x55dfbadb9aee082cULL, - 0x92ce98e760d05399ULL, 0xd03e790cc93a650aULL, - 0xaa478900b1228e31ULL, 0xe8b768eb18c8b8a2ULL, - 0x2fa64ad7e2f6e317ULL, 0x6d56ab3c4b1cd584ULL, - 0xe374ef45bf6062eeULL, 0xa1840eae168a547dULL, - 0x66952c92ecb40fc8ULL, 0x2465cd79455e395bULL, - 0x3821458aada7578fULL, 0x7ad1a461044d611cULL, - 0xbdc0865dfe733aa9ULL, 0xff3067b657990c3aULL, - 0x711223cfa3e5bb50ULL, 0x33e2c2240a0f8dc3ULL, - 0xf4f3e018f031d676ULL, 0xb60301f359dbe0e5ULL, - 0xda050215ea6c212fULL, 0x98f5e3fe438617bcULL, - 0x5fe4c1c2b9b84c09ULL, 0x1d14202910527a9aULL, - 0x93366450e42ecdf0ULL, 0xd1c685bb4dc4fb63ULL, - 0x16d7a787b7faa0d6ULL, 0x5427466c1e109645ULL, - 0x4863ce9ff6e9f891ULL, 0x0a932f745f03ce02ULL, - 0xcd820d48a53d95b7ULL, 0x8f72eca30cd7a324ULL, - 0x0150a8daf8ab144eULL, 0x43a04931514122ddULL, - 0x84b16b0dab7f7968ULL, 0xc6418ae602954ffbULL, - 0xbc387aea7a8da4c0ULL, 0xfec89b01d3679253ULL, - 0x39d9b93d2959c9e6ULL, 0x7b2958d680b3ff75ULL, - 0xf50b1caf74cf481fULL, 0xb7fbfd44dd257e8cULL, - 0x70eadf78271b2539ULL, 0x321a3e938ef113aaULL, - 0x2e5eb66066087d7eULL, 0x6cae578bcfe24bedULL, - 0xabbf75b735dc1058ULL, 0xe94f945c9c3626cbULL, - 0x676dd025684a91a1ULL, 0x259d31cec1a0a732ULL, - 0xe28c13f23b9efc87ULL, 0xa07cf2199274ca14ULL, - 0x167ff3eacbaf2af1ULL, 0x548f120162451c62ULL, - 0x939e303d987b47d7ULL, 0xd16ed1d631917144ULL, - 0x5f4c95afc5edc62eULL, 0x1dbc74446c07f0bdULL, - 0xdaad56789639ab08ULL, 0x985db7933fd39d9bULL, - 0x84193f60d72af34fULL, 0xc6e9de8b7ec0c5dcULL, - 0x01f8fcb784fe9e69ULL, 0x43081d5c2d14a8faULL, - 0xcd2a5925d9681f90ULL, 0x8fdab8ce70822903ULL, - 0x48cb9af28abc72b6ULL, 0x0a3b7b1923564425ULL, - 0x70428b155b4eaf1eULL, 0x32b26afef2a4998dULL, - 0xf5a348c2089ac238ULL, 0xb753a929a170f4abULL, - 0x3971ed50550c43c1ULL, 0x7b810cbbfce67552ULL, - 0xbc902e8706d82ee7ULL, 0xfe60cf6caf321874ULL, - 0xe224479f47cb76a0ULL, 0xa0d4a674ee214033ULL, - 0x67c58448141f1b86ULL, 0x253565a3bdf52d15ULL, - 0xab1721da49899a7fULL, 0xe9e7c031e063acecULL, - 0x2ef6e20d1a5df759ULL, 0x6c0603e6b3b7c1caULL, - 0xf6fae5c07d3274cdULL, 0xb40a042bd4d8425eULL, - 0x731b26172ee619ebULL, 0x31ebc7fc870c2f78ULL, - 0xbfc9838573709812ULL, 0xfd39626eda9aae81ULL, - 0x3a28405220a4f534ULL, 0x78d8a1b9894ec3a7ULL, - 0x649c294a61b7ad73ULL, 0x266cc8a1c85d9be0ULL, - 0xe17dea9d3263c055ULL, 0xa38d0b769b89f6c6ULL, - 0x2daf4f0f6ff541acULL, 0x6f5faee4c61f773fULL, - 0xa84e8cd83c212c8aULL, 0xeabe6d3395cb1a19ULL, - 0x90c79d3fedd3f122ULL, 0xd2377cd44439c7b1ULL, - 0x15265ee8be079c04ULL, 0x57d6bf0317edaa97ULL, - 0xd9f4fb7ae3911dfdULL, 0x9b041a914a7b2b6eULL, - 0x5c1538adb04570dbULL, 0x1ee5d94619af4648ULL, - 0x02a151b5f156289cULL, 0x4051b05e58bc1e0fULL, - 0x87409262a28245baULL, 0xc5b073890b687329ULL, - 0x4b9237f0ff14c443ULL, 0x0962d61b56fef2d0ULL, - 0xce73f427acc0a965ULL, 0x8c8315cc052a9ff6ULL, - 0x3a80143f5cf17f13ULL, 0x7870f5d4f51b4980ULL, - 0xbf61d7e80f251235ULL, 0xfd913603a6cf24a6ULL, - 0x73b3727a52b393ccULL, 0x31439391fb59a55fULL, - 0xf652b1ad0167feeaULL, 0xb4a25046a88dc879ULL, - 0xa8e6d8b54074a6adULL, 0xea16395ee99e903eULL, - 0x2d071b6213a0cb8bULL, 0x6ff7fa89ba4afd18ULL, - 0xe1d5bef04e364a72ULL, 0xa3255f1be7dc7ce1ULL, - 0x64347d271de22754ULL, 0x26c49cccb40811c7ULL, - 0x5cbd6cc0cc10fafcULL, 0x1e4d8d2b65facc6fULL, - 0xd95caf179fc497daULL, 0x9bac4efc362ea149ULL, - 0x158e0a85c2521623ULL, 0x577eeb6e6bb820b0ULL, - 0x906fc95291867b05ULL, 0xd29f28b9386c4d96ULL, - 0xcedba04ad0952342ULL, 0x8c2b41a1797f15d1ULL, - 0x4b3a639d83414e64ULL, 0x09ca82762aab78f7ULL, - 0x87e8c60fded7cf9dULL, 0xc51827e4773df90eULL, - 0x020905d88d03a2bbULL, 0x40f9e43324e99428ULL, - 0x2cffe7d5975e55e2ULL, 0x6e0f063e3eb46371ULL, - 0xa91e2402c48a38c4ULL, 0xebeec5e96d600e57ULL, - 0x65cc8190991cb93dULL, 0x273c607b30f68faeULL, - 0xe02d4247cac8d41bULL, 0xa2dda3ac6322e288ULL, - 0xbe992b5f8bdb8c5cULL, 0xfc69cab42231bacfULL, - 0x3b78e888d80fe17aULL, 0x7988096371e5d7e9ULL, - 0xf7aa4d1a85996083ULL, 0xb55aacf12c735610ULL, - 0x724b8ecdd64d0da5ULL, 0x30bb6f267fa73b36ULL, - 0x4ac29f2a07bfd00dULL, 0x08327ec1ae55e69eULL, - 0xcf235cfd546bbd2bULL, 0x8dd3bd16fd818bb8ULL, - 0x03f1f96f09fd3cd2ULL, 0x41011884a0170a41ULL, - 0x86103ab85a2951f4ULL, 0xc4e0db53f3c36767ULL, - 0xd8a453a01b3a09b3ULL, 0x9a54b24bb2d03f20ULL, - 0x5d45907748ee6495ULL, 0x1fb5719ce1045206ULL, - 0x919735e51578e56cULL, 0xd367d40ebc92d3ffULL, - 0x1476f63246ac884aULL, 0x568617d9ef46bed9ULL, - 0xe085162ab69d5e3cULL, 0xa275f7c11f7768afULL, - 0x6564d5fde549331aULL, 0x279434164ca30589ULL, - 0xa9b6706fb8dfb2e3ULL, 0xeb46918411358470ULL, - 0x2c57b3b8eb0bdfc5ULL, 0x6ea7525342e1e956ULL, - 0x72e3daa0aa188782ULL, 0x30133b4b03f2b111ULL, - 0xf7021977f9cceaa4ULL, 0xb5f2f89c5026dc37ULL, - 0x3bd0bce5a45a6b5dULL, 0x79205d0e0db05dceULL, - 0xbe317f32f78e067bULL, 0xfcc19ed95e6430e8ULL, - 0x86b86ed5267cdbd3ULL, 0xc4488f3e8f96ed40ULL, - 0x0359ad0275a8b6f5ULL, 0x41a94ce9dc428066ULL, - 0xcf8b0890283e370cULL, 0x8d7be97b81d4019fULL, - 0x4a6acb477bea5a2aULL, 0x089a2aacd2006cb9ULL, - 0x14dea25f3af9026dULL, 0x562e43b4931334feULL, - 0x913f6188692d6f4bULL, 0xd3cf8063c0c759d8ULL, - 0x5dedc41a34bbeeb2ULL, 0x1f1d25f19d51d821ULL, - 0xd80c07cd676f8394ULL, 0x9afce626ce85b507ULL, -}; - -static void erase(void *const ptr, const size_t length) -{ - volatile uint8_t* buffer = ptr; - for (size_t i = 0U; i < length; ++i) - { - buffer[i] = 0U; - } -} - -static char* utf16_to_bytes(const wchar_t* const input, const UINT code_page) -{ - char* buffer; - DWORD buffer_size = 0U, result = 0U; - - buffer_size = WideCharToMultiByte(code_page, 0, input, -1, NULL, 0, NULL, NULL); - if (buffer_size < 1U) - { - return NULL; - } - - buffer = (char*)malloc(sizeof(char) * buffer_size); - if (!buffer) - { - return NULL; - } - - result = WideCharToMultiByte(code_page, 0, input, -1, (LPSTR)buffer, buffer_size, NULL, NULL); - if ((result > 0U) && (result <= buffer_size)) - { - return buffer; - } - - free(buffer); - return NULL; -} static void print_string(const char *const text, const size_t length) { @@ -192,19 +21,19 @@ static void print_string(const char *const text, const size_t length) fputs("\"\n\n", stderr); } -static int open_files(FILE** const fin, FILE** const fout, const wchar_t* const input, const wchar_t* const output) +static int open_files(FILE** const fin, FILE** const fout, const CHR* const input, const CHR* const output) { - *fin = _wfopen(input, L"rb"); + *fin = FOPEN(input, T("rb")); if (!(*fin)) { - fputws(L"Error: Failed to open input file for reading!\n\n", stderr); + FPUTS(T("Error: Failed to open input file for reading!\n\n"), stderr); return 1; } - *fout = _wfopen(output, L"wb"); + *fout = FOPEN(output, T("wb")); if (!(*fout)) { - fputws(L"Error: Failed to open output file for writing!\n\n", stderr); + FPUTS(T("Error: Failed to open output file for writing!\n\n"), stderr); fclose(*fin); return 1; } @@ -212,27 +41,22 @@ static int open_files(FILE** const fin, FILE** const fout, const wchar_t* const return 0; } -static uint64_t get_file_size(FILE *const file) -{ - struct _stati64 stat; - if (_fstati64(_fileno(file), &stat) != 0) - { - fputws(L"I/O error: Failed to determine size of input file!\n\n", stderr); - return UINT64_MAX; - } - return (stat.st_size >= 0) ? ((uint64_t)stat.st_size) : 0U; -} - -static int encrypt(const wchar_t* const passphrase, const wchar_t* const input, const wchar_t* const output) +static int encrypt(const CHR* const passphrase, const CHR* const input, const CHR* const output) { mcrypt_t ctx = NULL; FILE *fin = NULL, *fout = NULL; int result = -1; + if (!passphrase[0U]) + { + FPUTS(T("Error: The given passphrase is empty!\n\n"), stderr); + goto clean_up; + } + char *const passphrase_utf8 = utf16_to_bytes(passphrase, CP_UTF8); if (!passphrase_utf8) { - fputws(L"Error: Failed to convert passphrase to UTF-8 format!\n\n", stderr); + FPUTS(T("Error: Failed to convert passphrase to UTF-8 format!\n\n"), stderr); goto clean_up; } @@ -241,69 +65,94 @@ static int encrypt(const wchar_t* const passphrase, const wchar_t* const input, goto clean_up;; } + const uint64_t file_size = get_file_size(fin); + if (file_size == UINT64_MAX) + { + FPUTS(T("I/O error: Failed to determine size of input file!\n\n"), stderr); + goto clean_up; + } + else if (file_size < 1LL) + { + FPUTS(T("Error: Input file is empty!\n\n"), stderr); + goto clean_up; + } + uint64_t seed; if (mcrypt_generate_seed(&seed) != 0) { - fputws(L"MCrypt error: Failed to generate seed!\n\n", stderr); + FPUTS(T("MCrypt error: Failed to generate seed!\n\n"), stderr); goto clean_up; } if (fwrite(&seed, sizeof(uint64_t), 1U, fout) < 1U) { - fputws(L"I/O error: Failed to write seed value!\n\n", stderr); + FPUTS(T("I/O error: Failed to write seed value!\n\n"), stderr); goto clean_up; } ctx = mcrypt_alloc(seed, passphrase_utf8); if (!ctx) { - fputws(L"MCrypt error: Failed to initialize encryption!\n\n", stderr); + FPUTS(T("MCrypt error: Failed to initialize encryption!\n\n"), stderr); goto clean_up; } - fputws(L"Encrypting, please be patient... ", stderr); + FPRINTF(stderr, T("Encrypting, please be patient... %5.1f%%"), 0.0); + clock_t clk_now, clk_update = clock(); + uint64_t crc_actual = CRC_INITIALIZER, bytes_read = sizeof(uint64_t); uint8_t buffer[1024U]; - uint64_t crc_actual = ~0U; - while (!feof(fin)) + while ((!feof(fin)) && (bytes_read < file_size)) { const size_t count = fread(buffer, sizeof(uint8_t), 1024U, fin); if (ferror(fin)) { - fputws(L"failed!\n\nI/O error: Failed to read input data!\n\n", stderr); + FPUTS(T("\n\nI/O error: Failed to read input data!\n\n"), stderr); goto clean_up; } if (count > 0U) { - for (size_t i = 0U; i < count; ++i) - { - crc_actual = CRC64_TAB[((crc_actual >> 56) ^ buffer[i]) & 0xFF] ^ (crc_actual << 8); - } + crc_actual = crc64_update(crc_actual, buffer, count); + bytes_read += count; if (mcrypt_enc_process_inplace(ctx, buffer, count) != 0) { - fputws(L"failed!\n\nMCrypt error: Failed to encrypt data!\n\n", stderr); + FPUTS(T("\n\nMCrypt error: Failed to encrypt data!\n\n"), stderr); goto clean_up; } if (fwrite(buffer, sizeof(uint8_t), count, fout) < count) { - fputws(L"failed!\n\nI/O error: Failed to write encrypted data!\n\n", stderr); + FPUTS(T("\n\nI/O error: Failed to write encrypted data!\n\n"), stderr); goto clean_up; } } + if ((clk_now = clock()) - clk_update > CLOCKS_PER_SEC) + { + FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%"), (bytes_read / ((double)file_size)) * 100.0); + fflush(stderr); + clk_update = clk_now; + } } - crc_actual ^= ~0U; + crc_actual = crc64_finish(crc_actual); + if (bytes_read < file_size) + { + FPUTS(T("\n\nI/O error: Input file could not be fully read!\n\n"), stderr); + goto clean_up; + } + + FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%\n\n"), 100.0); + if (fwrite(&crc_actual, sizeof(uint64_t), 1U, fout) < 1U) { - fputws(L"failed!\n\nI/O error: Failed to write CRC checksum!\n\n", stderr); + FPUTS(T("I/O error: Failed to write CRC checksum!\n\n"), stderr); goto clean_up; } result = 0; - fputws(L"done.\n\n", stderr); + FPUTS(T("All is done.\n\n"), stderr); fflush(stderr); clean_up: @@ -332,16 +181,22 @@ clean_up: return result; } -static int decrypt(const wchar_t* const passphrase, const wchar_t* const input, const wchar_t* const output) +static int decrypt(const CHR* const passphrase, const CHR* const input, const CHR* const output) { mcrypt_t ctx = NULL; FILE *fin = NULL, *fout = NULL; int result = -1; + if (!passphrase[0U]) + { + FPUTS(T("Error: The given passphrase is empty!\n\n"), stderr); + goto clean_up; + } + char *const passphrase_utf8 = utf16_to_bytes(passphrase, CP_UTF8); if (!passphrase_utf8) { - fputws(L"Error: Failed to convert passphrase to UTF-8 format!\n\n", stderr); + FPUTS(T("Error: Failed to convert passphrase to UTF-8 format!\n\n"), stderr); goto clean_up; } @@ -353,44 +208,44 @@ static int decrypt(const wchar_t* const passphrase, const wchar_t* const input, const uint64_t file_size = get_file_size(fin); if (file_size == UINT64_MAX) { + FPUTS(T("I/O error: Failed to determine size of input file!\n\n"), stderr); goto clean_up; } - - if (file_size < 16LL) + else if (file_size < 16LL) { - fputws(L"Error: Input file is too small!\n\n", stderr); + FPUTS(T("Error: Input file is too small! Truncated?\n\n"), stderr); goto clean_up; } uint64_t seed; if (fread(&seed, sizeof(uint64_t), 1U, fin) < 1U) { - fputws(L"I/O error: Failed to read seed value!\n\n", stderr); + FPUTS(T("I/O error: Failed to read seed value!\n\n"), stderr); goto clean_up; } ctx = mcrypt_alloc(seed, passphrase_utf8); if (!ctx) { - fputws(L"MCrypt error: Failed to initialize decryption!\n\n", stderr); + FPUTS(T("MCrypt error: Failed to initialize decryption!\n\n"), stderr); goto clean_up; } - fputws(L"Decrypting, please be patient... ", stderr); + FPRINTF(stderr, T("Decrypting, please be patient... %5.1f%%"), 0.0); - uint64_t crc_actual = ~0U; - int64_t bytes_read = sizeof(uint64_t); + clock_t clk_now, clk_update = clock(); + uint64_t crc_actual = CRC_INITIALIZER, bytes_read = sizeof(uint64_t); uint8_t buffer[1024U]; + const uint64_t read_limit = file_size - sizeof(uint64_t); - const int64_t read_limit = file_size - sizeof(uint64_t); while ((!feof(fin)) && (bytes_read < read_limit)) { - const int64_t bytes_remaining = read_limit - bytes_read; + const uint64_t bytes_remaining = read_limit - bytes_read; const size_t read_len = (bytes_remaining < 1024U) ? ((size_t)bytes_remaining) : 1024U; const size_t count = fread(buffer, sizeof(uint8_t), read_len, fin); if (ferror(fin)) { - fputws(L"failed!\n\nI/O error: Failed to read encrypted data!\n\n", stderr); + FPUTS(T("\n\nI/O error: Failed to read encrypted data!\n\n"), stderr); goto clean_up; } if (count > 0U) @@ -398,45 +253,50 @@ static int decrypt(const wchar_t* const passphrase, const wchar_t* const input, bytes_read += count; if (mcrypt_dec_process_inplace(ctx, buffer, count) != 0) { - fputws(L"failed!\n\nMCrypt error: Failed to decrypt data!\n\n", stderr); + FPUTS(T("\n\nMCrypt error: Failed to decrypt data!\n\n"), stderr); goto clean_up; } - for (size_t i = 0U; i < count; ++i) - { - crc_actual = CRC64_TAB[((crc_actual >> 56) ^ buffer[i]) & 0xFF] ^ (crc_actual << 8); - } + crc_actual = crc64_update(crc_actual, buffer, count); if (fwrite(buffer, sizeof(uint8_t), count, fout) < count) { - fputws(L"failed!\n\nI/O error: Failed to write decrypted data!\n\n", stderr); + FPUTS(T("failed!\n\nI/O error: Failed to write decrypted data!\n\n"), stderr); goto clean_up; } } + if ((clk_now = clock()) - clk_update > CLOCKS_PER_SEC) + { + FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%"), (bytes_read / ((double)read_limit)) * 100.0); + fflush(stderr); + clk_update = clk_now; + } } - crc_actual ^= ~0U; + crc_actual = crc64_finish(crc_actual); if (bytes_read < read_limit) { - fputws(L"failed!\n\nI/O error: Input file could not be fully read!\n\n", stderr); + FPUTS(T("\n\nI/O error: Input file could not be fully read!\n\n"), stderr); goto clean_up; } + FPRINTF(stderr, T("\b\b\b\b\b\b%5.1f%%\n\n"), 100.0); + uint64_t crc_expected; if (fread(&crc_expected, sizeof(uint64_t), 1U, fin) < 1U) { - fputws(L"failed!\n\nI/O error: Failed to read CRC checksum!\n\n", stderr); + FPUTS(T("I/O error: Failed to read CRC checksum!\n\n"), stderr); goto clean_up; } if (crc_actual != crc_expected) { - fwprintf(stderr, L"failed!\n\nCRC error: Checksum mismatch detected! [0x%016llX vs. 0x%016llX]\n\nWrong passphrase?\n\n", crc_actual, crc_expected); + FPRINTF(stderr, T("CRC error: Checksum mismatch detected! [0x%016") FMT_I64X T(" vs. 0x%016") FMT_I64X T("\n\nWrong passphrase?\n\n"), crc_actual, crc_expected); goto clean_up; } result = 0; - fputws(L"done.\n\nCRC checksum is correct.\n\n", stderr); + FPUTS(T("CRC checksum is correct :-)\n\n"), stderr); fflush(stderr); clean_up: @@ -465,35 +325,35 @@ clean_up: return result; } -int wmain(int argc, wchar_t* argv[]) +int MAIN(int argc, CHR* argv[]) { - fwprintf(stderr, L"MCrypt Utility [%S]\n", __DATE__" "__TIME__); - fwprintf(stderr, L"Powered by libMCrypt v%S [%S]\n\n", LIBMCRYPT_VERSION, LIBMCRYPT_BUILDNO); + FPRINTF(stderr, T("MCrypt Utility [%") FMT_char T("]\n"), __DATE__" "__TIME__); + FPRINTF(stderr, T("Powered by libMCrypt v%") FMT_char T(" [%") FMT_char T("]\n\n"), LIBMCRYPT_VERSION, LIBMCRYPT_BUILDNO); if (argc < 4) { - fputws(L"Usage:\n", stderr); - fwprintf(stderr, L" %s [--decrypt] \n\n", argv[0U]); + FPUTS(T("Usage:\n"), stderr); + FPRINTF(stderr, T(" %s [--decrypt] \n\n"), argv[0U]); return 1; } int result = -1; const clock_t clk_start = clock(); - if ((argc > 4) && (!_wcsicmp(argv[1U], L"--decrypt"))) + if ((argc > 4) && ((!STRICMP(argv[1U], T("-d"))) || (!STRICMP(argv[1U], T("--decrypt"))))) { result = decrypt(argv[2U], argv[3U], argv[4U]); - erase(argv[2U], wcslen(argv[2U]) * sizeof(wchar_t)); + erase(argv[2U], STRLEN(argv[2U]) * sizeof(CHR)); } else { result = encrypt(argv[1U], argv[2U], argv[3U]); - erase(argv[1U], wcslen(argv[1U]) * sizeof(wchar_t)); + erase(argv[1U], STRLEN(argv[1U]) * sizeof(CHR)); } - fputws(L"--------\n\n", stderr); + FPUTS(T("--------\n\n"), stderr); fflush(stderr); const clock_t clk_end = clock(); - fwprintf(stderr, L"Operation completed after %.1f seconds.\n\n", (clk_end - clk_start) / ((double)CLOCKS_PER_SEC)); + FPRINTF(stderr, T("Operation completed after %.1f seconds.\n\n"), (clk_end - clk_start) / ((double)CLOCKS_PER_SEC)); } diff --git a/frontend/src/platform.h b/frontend/src/platform.h new file mode 100644 index 0000000..a4f3d21 --- /dev/null +++ b/frontend/src/platform.h @@ -0,0 +1,44 @@ +/******************************************************************************/ +/* MCrypt, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#ifndef INC_PLATFORM_H +#define INC_PLATFORM_H + +#ifdef _WIN32 +#define MAIN wmain +#define CHR wchar_t +#define _T(X) L##X +#define T(X) _T(X) +#define STRLEN(X) wcslen((X)) +#define STRICMP(X,Y) _wcsicmp((X),(Y)) +#define FPUTS(X,Y) fputws((X),(Y)) +#define FPRINTF(X,Y,...) fwprintf((X),(Y),__VA_ARGS__) +#define FOPEN(X,Y) _wfopen((X),(Y)) +#define FILENO(X) _fileno((X)) +#define FSTAT64(X,Y) _fstati64((X),(Y)) +#define STAT64_T struct _stati64 +#ifdef __MINGW32__ +#define FMT_char L"hs" +#define FMT_I64X L"llX" +#else +#define FMT_char L"S" +#define FMT_I64X L"I64X" +#endif +#else +#define MAIN main +#define CHR char +#define STRLEN(X) strlen((X)) +#define STRICMP(X,Y) stricmp((X),(Y)) +#define FPUTS(X,Y) fputs((X),(Y)) +#define FPRINTF(X,Y,...) fprintf((X),(Y),__VA_ARGS__) +#define FOPEN(X,Y) fopen((X),(Y)) +#define FILENO(X) fileno((X)) +#define FSTAT64(X,Y) fstat64((X),(Y)) +#define STAT64_T struct stat64 +#define FMT_char "s" +#define FMT_I64X L"llX" +#endif + +#endif \ No newline at end of file diff --git a/frontend/src/utils.c b/frontend/src/utils.c new file mode 100644 index 0000000..30e4789 --- /dev/null +++ b/frontend/src/utils.c @@ -0,0 +1,71 @@ +/******************************************************************************/ +/* MCrypt, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN 1 +#define _CRT_SECURE_NO_WARNINGS 1 +#endif + +#include "utils.h" +#include +#include +#ifdef _WIN32 +#include +#endif + +void erase(void *const ptr, const size_t length) +{ + volatile uint8_t* buffer = ptr; + for (size_t i = 0U; i < length; ++i) + { + buffer[i] = 0U; + } +} + +char* utf16_to_bytes(const CHR*const input, const uint32_t code_page) +{ +#ifdef _WIN32 + char* buffer; + DWORD buffer_size = 0U, result = 0U; + + buffer_size = WideCharToMultiByte(code_page, 0, input, -1, NULL, 0, NULL, NULL); + if (buffer_size < 1U) + { + return NULL; + } + + buffer = (char*)malloc(sizeof(char) * buffer_size); + if (!buffer) + { + return NULL; + } + + result = WideCharToMultiByte(code_page, 0, input, -1, (LPSTR)buffer, buffer_size, NULL, NULL); + if ((result > 0U) && (result <= buffer_size)) + { + return buffer; + } + + free(buffer); + return NULL; +#else + return strdup(input); +#endif +} + +uint64_t get_file_size(FILE *const file) +{ + STAT64_T stat; + if (FSTAT64(FILENO(file), &stat) != 0) + { + return UINT64_MAX; + } + uint16_t file_type = stat.st_mode & _S_IFMT; + if ((file_type != _S_IFDIR) && (file_type != _S_IFIFO)) + { + return (stat.st_size >= 0LL) ? ((uint64_t)stat.st_size) : 0U; + } + return 0U; +} diff --git a/frontend/src/utils.h b/frontend/src/utils.h new file mode 100644 index 0000000..dddae8c --- /dev/null +++ b/frontend/src/utils.h @@ -0,0 +1,22 @@ +/******************************************************************************/ +/* MCrypt, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#ifndef INC_UTILS_H +#define INC_UTILS_H + +#include +#include +#include +#include "platform.h" + +#ifndef CP_UTF8 +#define CP_UTF8 65001 +#endif + +void erase(void *const ptr, const size_t length); +uint64_t get_file_size(FILE* const file); +char* utf16_to_bytes(const CHR *const input, const uint32_t code_page); + +#endif diff --git a/libMCrypt/libMCrypt.vcxproj b/libMCrypt/libMCrypt.vcxproj index 4d3cff9..f09c55a 100644 --- a/libMCrypt/libMCrypt.vcxproj +++ b/libMCrypt/libMCrypt.vcxproj @@ -19,10 +19,12 @@ - + + - + + 16.0 diff --git a/libMCrypt/libMCrypt.vcxproj.filters b/libMCrypt/libMCrypt.vcxproj.filters index 7251418..d050e06 100644 --- a/libMCrypt/libMCrypt.vcxproj.filters +++ b/libMCrypt/libMCrypt.vcxproj.filters @@ -14,14 +14,20 @@ rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - Header Files - - Source Files + + Source Files + + + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/libMCrypt/src/mcrypt.c b/libMCrypt/src/mcrypt.c index 6d9abe0..d56497a 100644 --- a/libMCrypt/src/mcrypt.c +++ b/libMCrypt/src/mcrypt.c @@ -8,6 +8,7 @@ #endif #include +#include "utils.h" #include const char* const LIBMCRYPT_VERSION = "1.0.0"; @@ -29,62 +30,6 @@ typedef struct } 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 *const ptr, const size_t length) -{ - volatile uint8_t* buffer = ptr; - for (size_t i = 0U; i < length; ++i) - { - buffer[i] = 0U; - } -} - // ========================================================================== // Hash function // ========================================================================== @@ -136,8 +81,8 @@ static uint32_t random_next(rand_state_t* const state) static void random_seed(rand_state_t* const state, const uint64_t salt, const uint16_t pepper, const uint8_t* const key, const size_t key_len) { - const uint64_t hash_code_0 = hash_code(salt, pepper + 0xB8A1, key, key_len); - const uint64_t hash_code_1 = hash_code(salt, pepper + 0x475E, key, key_len); + const uint64_t hash_code_0 = hash_code(salt, pepper & 0x7FFF, key, key_len); + const uint64_t hash_code_1 = hash_code(salt, pepper | 0x8000, key, key_len); random_init(state, hash_code_0, hash_code_1); for (size_t i = 0U; i < 13U; ++i) { @@ -173,7 +118,7 @@ static void initialize_state(crypt_state_t* const crypt_state, const uint64_t sa } random_seed(&rand_state, salt, 0x0100, key, key_len); crypt_state->pos = (uint8_t)random_next(&rand_state); - erase(&rand_state, sizeof(rand_state_t)); + mcrypt_erase(&rand_state, sizeof(rand_state_t)); } // ========================================================================== @@ -209,7 +154,7 @@ int mcrypt_generate_seed(uint64_t* const seed) { if (seed) { - return random_bytes((uint8_t*)seed, sizeof(uint64_t)); + return mcrypt_random_bytes((uint8_t*)seed, sizeof(uint64_t)); } return -1; } @@ -291,7 +236,7 @@ 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)); + mcrypt_erase((void*)context, sizeof(crypt_state_t)); free(context); } } diff --git a/libMCrypt/src/utils.c b/libMCrypt/src/utils.c new file mode 100644 index 0000000..96bfd8a --- /dev/null +++ b/libMCrypt/src/utils.c @@ -0,0 +1,62 @@ +/******************************************************************************/ +/* 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 "utils.h" + +int mcrypt_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 +} + +void mcrypt_erase(void* const ptr, const size_t length) +{ + volatile uint8_t* buffer = ptr; + for (size_t i = 0U; i < length; ++i) + { + buffer[i] = 0U; + } +} diff --git a/libMCrypt/src/utils.h b/libMCrypt/src/utils.h new file mode 100644 index 0000000..589d1e8 --- /dev/null +++ b/libMCrypt/src/utils.h @@ -0,0 +1,15 @@ +/******************************************************************************/ +/* MCrypt, by LoRd_MuldeR */ +/* This work has been released under the CC0 1.0 Universal license! */ +/******************************************************************************/ + +#ifndef INC_MCRYPT_UTILS_H +#define INC_MCRYPT_UTILS_H + +#include +#include + +int mcrypt_random_bytes(uint8_t* const buffer, const size_t length); +void mcrypt_erase(void* const ptr, const size_t length); + +#endif