about summary refs log tree commit diff
path: root/src/base/system.c
diff options
context:
space:
mode:
authorSavander <savander.pl@gmail.com>2018-09-28 17:44:19 +0200
committerGitHub <noreply@github.com>2018-09-28 17:44:19 +0200
commit012cda5afd013e27e529026b67722609ecd61d41 (patch)
treea5c3789c4aa2c93255d824b5eb343172d3642eac /src/base/system.c
parentfc6d8b4d154049e6e3d6df9039669580f8ff747c (diff)
parent2cd0223a8df54ae1c8715915a331dfc56a5a29d3 (diff)
downloadzcatch-012cda5afd013e27e529026b67722609ecd61d41.tar.gz
zcatch-012cda5afd013e27e529026b67722609ecd61d41.zip
Merge pull request #4 from Learath2/zcatchspoof
Antispoof
Diffstat (limited to 'src/base/system.c')
-rw-r--r--src/base/system.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/base/system.c b/src/base/system.c
index bc0c261d..970d1c6b 100644
--- a/src/base/system.c
+++ b/src/base/system.c
@@ -2060,6 +2060,69 @@ unsigned str_quickhash(const char *str)
 	return hash;
 }
 
+struct SECURE_RANDOM_DATA
+{
+	int initialized;
+#if defined(CONF_FAMILY_WINDOWS)
+	HCRYPTPROV provider;
+#else
+	IOHANDLE urandom;
+#endif
+};
+
+static struct SECURE_RANDOM_DATA secure_random_data = { 0 };
+
+int secure_random_init()
+{
+	if(secure_random_data.initialized)
+	{
+		return 0;
+	}
+#if defined(CONF_FAMILY_WINDOWS)
+	if(CryptAcquireContext(&secure_random_data.provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
+	{
+		secure_random_data.initialized = 1;
+		return 0;
+	}
+	else
+	{
+		return 1;
+	}
+#else
+	secure_random_data.urandom = io_open("/dev/urandom", IOFLAG_READ);
+	if(secure_random_data.urandom)
+	{
+		secure_random_data.initialized = 1;
+		return 0;
+	}
+	else
+	{
+		return 1;
+	}
+#endif
+}
+
+void secure_random_fill(void *bytes, unsigned length)
+{
+	if(!secure_random_data.initialized)
+	{
+		dbg_msg("secure", "called secure_random_fill before secure_random_init");
+		dbg_break();
+	}
+#if defined(CONF_FAMILY_WINDOWS)
+	if(!CryptGenRandom(secure_random_data.provider, length, bytes))
+	{
+		dbg_msg("secure", "CryptGenRandom failed, last_error=%ld", GetLastError());
+		dbg_break();
+	}
+#else
+	if(length != io_read(secure_random_data.urandom, bytes, length))
+	{
+		dbg_msg("secure", "io_read returned with a short read");
+		dbg_break();
+	}
+#endif
+}
 
 #if defined(__cplusplus)
 }