diff options
| author | eeeee <eeeee@qwe123.info> | 2018-08-18 17:33:58 -0700 |
|---|---|---|
| committer | Learath <learath2@gmail.com> | 2018-08-20 15:33:34 +0300 |
| commit | 2cd0223a8df54ae1c8715915a331dfc56a5a29d3 (patch) | |
| tree | a5c3789c4aa2c93255d824b5eb343172d3642eac /src/base | |
| parent | 3f3abebebf20e8901376b929e2afcc6bdb404a3b (diff) | |
| download | zcatch-2cd0223a8df54ae1c8715915a331dfc56a5a29d3.tar.gz zcatch-2cd0223a8df54ae1c8715915a331dfc56a5a29d3.zip | |
Port antispoof
Diffstat (limited to 'src/base')
| -rw-r--r-- | src/base/system.c | 63 | ||||
| -rw-r--r-- | src/base/system.h | 21 |
2 files changed, 84 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) } diff --git a/src/base/system.h b/src/base/system.h index ad606ceb..e23adf29 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -1297,6 +1297,27 @@ int str_utf8_encode(char *ptr, int chr); */ int str_utf8_check(const char *str); +/* + Function: secure_random_init + Initializes the secure random module. + You *MUST* check the return value of this function. + + Returns: + 0 - Initialization succeeded. + 1 - Initialization failed. +*/ +int secure_random_init(); + +/* + Function: secure_random_fill + Fills the buffer with the specified amount of random bytes. + + Parameters: + buffer - Pointer to the start of the buffer. + length - Length of the buffer. +*/ +void secure_random_fill(void *bytes, unsigned length); + #ifdef __cplusplus } #endif |