2014-07-14 17:24:43 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2015-05-10 16:24:45 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2014, 2015.
|
2014-07-14 17:24:43 -04:00
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
random.cpp
|
|
|
|
Kernel entropy gathering.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2015-05-10 16:24:45 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-07-14 17:24:43 -04:00
|
|
|
|
2015-02-17 15:20:04 -05:00
|
|
|
#include <sortix/clock.h>
|
|
|
|
|
2014-07-14 17:24:43 -04:00
|
|
|
#include <sortix/kernel/copy.h>
|
2015-02-17 15:20:04 -05:00
|
|
|
#include <sortix/kernel/interlock.h>
|
2014-07-14 17:24:43 -04:00
|
|
|
#include <sortix/kernel/syscall.h>
|
2015-02-17 15:20:04 -05:00
|
|
|
#include <sortix/kernel/time.h>
|
2014-07-14 17:24:43 -04:00
|
|
|
|
|
|
|
namespace Sortix {
|
2015-05-10 16:24:45 -04:00
|
|
|
namespace Random {
|
2014-07-14 17:24:43 -04:00
|
|
|
|
2015-02-17 15:20:04 -05:00
|
|
|
static unsigned long sequence = 0;
|
|
|
|
|
2015-05-10 16:24:45 -04:00
|
|
|
bool HasEntropy()
|
|
|
|
{
|
|
|
|
// We only have new entropy once and that's at boot.
|
|
|
|
return sequence == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetEntropy(void* result, size_t size)
|
2014-07-14 17:24:43 -04:00
|
|
|
{
|
2015-02-17 15:20:04 -05:00
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned char buffer[256];
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
struct timespec realtime;
|
|
|
|
struct timespec monotonic;
|
|
|
|
unsigned long sequence;
|
|
|
|
} seed;
|
|
|
|
};
|
2014-07-14 17:24:43 -04:00
|
|
|
if ( sizeof(buffer) < size )
|
2015-05-10 16:24:45 -04:00
|
|
|
size = sizeof(buffer);
|
2014-07-14 17:24:43 -04:00
|
|
|
// TODO: SECURITY: We need to actually gather entropy and deliver it.
|
|
|
|
for ( size_t i = 0; i < size; i++ )
|
|
|
|
buffer[i] = i;
|
2015-02-17 15:20:04 -05:00
|
|
|
// 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;
|
2015-05-10 16:24:45 -04:00
|
|
|
memcpy(result, buffer, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Random
|
|
|
|
} // namespace Sortix
|
|
|
|
|
|
|
|
namespace Sortix {
|
|
|
|
|
|
|
|
int sys_getentropy(void* user_buffer, size_t size)
|
|
|
|
{
|
|
|
|
unsigned char buffer[256];
|
|
|
|
if ( sizeof(buffer) < size )
|
|
|
|
return errno = EIO, -1;
|
|
|
|
arc4random_buf(buffer, sizeof(buffer));
|
2014-07-14 17:24:43 -04:00
|
|
|
if ( !CopyToUser(user_buffer, buffer, size) )
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Sortix
|