mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Fix fake kernel entropy being entirely static.
This commit is contained in:
parent
3bef590a0f
commit
d383ada0d8
1 changed files with 24 additions and 1 deletions
|
@ -24,19 +24,42 @@
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <sortix/clock.h>
|
||||||
|
|
||||||
#include <sortix/kernel/copy.h>
|
#include <sortix/kernel/copy.h>
|
||||||
|
#include <sortix/kernel/interlock.h>
|
||||||
#include <sortix/kernel/syscall.h>
|
#include <sortix/kernel/syscall.h>
|
||||||
|
#include <sortix/kernel/time.h>
|
||||||
|
|
||||||
namespace Sortix {
|
namespace Sortix {
|
||||||
|
|
||||||
|
static unsigned long sequence = 0;
|
||||||
|
|
||||||
int sys_getentropy(void* user_buffer, size_t size)
|
int sys_getentropy(void* user_buffer, size_t size)
|
||||||
|
{
|
||||||
|
union
|
||||||
{
|
{
|
||||||
unsigned char buffer[256];
|
unsigned char buffer[256];
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
struct timespec realtime;
|
||||||
|
struct timespec monotonic;
|
||||||
|
unsigned long sequence;
|
||||||
|
} seed;
|
||||||
|
};
|
||||||
if ( sizeof(buffer) < size )
|
if ( sizeof(buffer) < size )
|
||||||
return errno = EIO, -1;
|
return errno = EIO, -1;
|
||||||
// TODO: SECURITY: We need to actually gather entropy and deliver it.
|
// TODO: SECURITY: We need to actually gather entropy and deliver it.
|
||||||
for ( size_t i = 0; i < size; i++ )
|
for ( size_t i = 0; i < size; i++ )
|
||||||
buffer[i] = i;
|
buffer[i] = i;
|
||||||
|
// NOTE: This is not random and is not meant to be random, this is just
|
||||||
|
// meant to make the returned entropy a little different each time
|
||||||
|
// until we have real randomness, even across reboots. The userland
|
||||||
|
// arc4random mixer will mix it around and the produced streams will
|
||||||
|
// look random and should not repeat in practice.
|
||||||
|
seed.realtime = Time::Get(CLOCK_REALTIME);
|
||||||
|
seed.monotonic = Time::Get(CLOCK_MONOTONIC);
|
||||||
|
seed.sequence = InterlockedIncrement(&sequence).o;
|
||||||
if ( !CopyToUser(user_buffer, buffer, size) )
|
if ( !CopyToUser(user_buffer, buffer, size) )
|
||||||
return -1;
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue