2020-10-13 15:04:59 +02:00
|
|
|
/******************************************************************************/
|
2020-10-19 21:56:12 +02:00
|
|
|
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
|
2020-10-13 15:04:59 +02:00
|
|
|
/* This work has been released under the CC0 1.0 Universal license! */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2020-10-21 17:07:03 +02:00
|
|
|
#define _WIN32_WINNT 0x0600
|
2020-10-13 15:04:59 +02:00
|
|
|
#define _CRT_RAND_S 1
|
2020-10-13 19:33:01 +02:00
|
|
|
#define WIN32_LEAN_AND_MEAN 1
|
2020-10-13 15:04:59 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-19 21:56:12 +02:00
|
|
|
#include <slunkcrypt.h>
|
2020-10-13 15:04:59 +02:00
|
|
|
|
2020-10-13 19:33:01 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
2020-10-13 15:37:40 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2020-10-21 17:07:03 +02:00
|
|
|
#include <string.h>
|
2020-10-13 17:42:22 +02:00
|
|
|
#if (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12))
|
|
|
|
#include <sys/random.h>
|
2020-10-13 19:33:01 +02:00
|
|
|
#endif
|
2020-10-13 15:37:40 +02:00
|
|
|
#endif
|
|
|
|
|
2020-10-19 21:56:12 +02:00
|
|
|
int slunkcrypt_random_bytes(uint8_t* const buffer, const size_t length)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
|
|
|
#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
|
2020-10-21 17:07:03 +02:00
|
|
|
#if (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 12))
|
2020-10-13 17:42:22 +02:00
|
|
|
if (getrandom(buffer, length, 0U) >= length)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
static const char* const PATH[] = { "/dev/urandom", "/dev/arandom", "/dev/random" };
|
2020-10-13 15:04:59 +02:00
|
|
|
int result = -1;
|
2020-10-13 17:42:22 +02:00
|
|
|
for (size_t i = 0; (i < 3U) && (result < 0); ++i)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
|
|
|
const int fd = open(PATH[i], O_RDONLY);
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
2020-10-13 15:37:40 +02:00
|
|
|
if (read(fd, buffer, length) >= length)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2020-10-13 17:42:22 +02:00
|
|
|
#endif
|
2020-10-13 15:04:59 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-10-19 21:56:12 +02:00
|
|
|
void slunkcrypt_bzero(void* const ptr, const size_t length)
|
2020-10-13 15:04:59 +02:00
|
|
|
{
|
2020-10-14 13:14:47 +02:00
|
|
|
if ((ptr) && (length > 0U))
|
|
|
|
{
|
2020-10-21 17:07:03 +02:00
|
|
|
#if defined(_WIN32) && defined(SecureZeroMemory)
|
2020-10-14 13:14:47 +02:00
|
|
|
SecureZeroMemory(ptr, length);
|
2020-10-13 19:33:01 +02:00
|
|
|
#else
|
2020-10-21 17:07:03 +02:00
|
|
|
#if (defined(__GLIBC__) && defined(__GLIBC_MINOR__) && (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 25)) || (defined(__FreeBSD__) && (__FreeBSD__ >= 11))
|
2020-10-14 13:14:47 +02:00
|
|
|
explicit_bzero(ptr, length);
|
2020-10-13 19:33:01 +02:00
|
|
|
#else
|
2020-10-21 17:07:03 +02:00
|
|
|
volatile uint8_t *buffer = (volatile uint8_t*)ptr;
|
2020-10-14 13:14:47 +02:00
|
|
|
for (size_t i = 0U; i < length; ++i)
|
|
|
|
{
|
|
|
|
buffer[i] = 0U;
|
|
|
|
}
|
2020-10-13 19:33:01 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2020-10-14 13:14:47 +02:00
|
|
|
}
|
2020-10-13 15:04:59 +02:00
|
|
|
}
|