From 30740ca4131d1f574718262451b4410207dc8d4e Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Sun, 14 Jul 2024 21:12:37 +0200 Subject: Reworking the build system --- kit/secure_random.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 kit/secure_random.c (limited to 'kit/secure_random.c') diff --git a/kit/secure_random.c b/kit/secure_random.c new file mode 100644 index 0000000..2565f6f --- /dev/null +++ b/kit/secure_random.c @@ -0,0 +1,45 @@ +#include "secure_random.h" + +#include +#include +#include + +#if defined(_WIN32) && !defined(__CYGWIN__) +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN 1 +# endif +# include +# include +#else +# include +#endif + +s32 kit_secure_random(i64 size, void *data) { + assert(size >= 0); + assert(data != NULL); + + if (size <= 0 || data == NULL) + return KIT_ERROR_INVALID_ARGUMENT; + +#if defined(_WIN32) && !defined(__CYGWIN__) + HCRYPTPROV prov = 0; + if (!CryptAcquireContextW(&prov, NULL, NULL, PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT | CRYPT_SILENT) || + !CryptGenRandom(prov, (DWORD) size, (BYTE *) data) || + !CryptReleaseContext(prov, 0)) + return KIT_ERROR_RESOURCE_UNAVAILABLE; +#else + FILE *f = fopen("/dev/urandom", "rb"); + + if (f == NULL) + return KIT_ERROR_RESOURCE_UNAVAILABLE; + + i64 n = (i64) fread(data, 1, size, f); + fclose(f); + + if (n != size) + return KIT_ERROR_RESOURCE_UNAVAILABLE; +#endif + + return KIT_OK; +} -- cgit v1.2.3