You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
901 B
37 lines
901 B
/* platform_compat.c - Platform compatibility layer implementation */ |
|
|
|
#include "platform_compat.h" |
|
#include <stddef.h> |
|
#include <stdint.h> |
|
|
|
#ifdef _WIN32 |
|
#include <windows.h> |
|
#include <bcrypt.h> |
|
#ifndef STATUS_SUCCESS |
|
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L) |
|
#endif |
|
#else |
|
#include <fcntl.h> |
|
#include <unistd.h> |
|
#endif |
|
|
|
/* |
|
* Generate cryptographically secure random bytes |
|
* Returns 0 on success, -1 on error |
|
*/ |
|
int random_bytes(uint8_t *buffer, size_t len) { |
|
if (!buffer || len == 0) return -1; |
|
|
|
#ifdef _WIN32 |
|
NTSTATUS status = BCryptGenRandom(NULL, buffer, (ULONG)len, BCRYPT_USE_SYSTEM_PREFERRED_RNG); |
|
return (status == STATUS_SUCCESS) ? 0 : -1; |
|
#else |
|
int fd = open("/dev/urandom", O_RDONLY); |
|
if (fd < 0) return -1; |
|
|
|
ssize_t ret = read(fd, buffer, len); |
|
close(fd); |
|
|
|
return (ret == (ssize_t)len) ? 0 : -1; |
|
#endif |
|
}
|
|
|