Add getentropy(2).

This commit is contained in:
Jonas 'Sortie' Termansen 2014-07-14 23:24:43 +02:00
parent 690b5ada52
commit e460be7a72
8 changed files with 127 additions and 1 deletions

View File

@ -123,6 +123,7 @@ pipe.o \
poll.o \
process.o \
ptable.o \
random.o \
refcount.o \
registers.o \
resource.o \

View File

@ -0,0 +1,31 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2014.
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/>.
sortix/kernel/random.h
Kernel entropy gathering.
*******************************************************************************/
namespace Sortix {
namespace Random {
void Init();
} // namespace Random
} // namespace Sortix

View File

@ -172,6 +172,7 @@
#define SYSCALL_GETPEERNAME 144
#define SYSCALL_GETSOCKNAME 145
#define SYSCALL_SHUTDOWN 146
#define SYSCALL_MAX_NUM 147 /* index of highest constant + 1 */
#define SYSCALL_GETENTROPY 147
#define SYSCALL_MAX_NUM 148 /* index of highest constant + 1 */
#endif

View File

@ -54,6 +54,7 @@
#include <sortix/kernel/pci.h>
#include <sortix/kernel/process.h>
#include <sortix/kernel/ptable.h>
#include <sortix/kernel/random.h>
#include <sortix/kernel/refcount.h>
#include <sortix/kernel/scheduler.h>
#include <sortix/kernel/signal.h>
@ -297,6 +298,9 @@ extern "C" void KernelInit(unsigned long magic, multiboot_info_t* bootinfo)
// Initialize the interrupt handler table and enable interrupts.
Interrupt::Init();
// Initialize entropy gathering.
Random::Init();
// Load the kernel symbols if provided by the bootloader.
do if ( bootinfo->flags & MULTIBOOT_INFO_ELF_SHDR )
{

53
kernel/random.cpp Normal file
View File

@ -0,0 +1,53 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2014.
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>
#include <sortix/kernel/copy.h>
#include <sortix/kernel/random.h>
#include <sortix/kernel/syscall.h>
namespace Sortix {
namespace Random {
int sys_getentropy(void* user_buffer, size_t size)
{
unsigned char buffer[256];
if ( sizeof(buffer) < size )
return errno = EIO, -1;
// TODO: SECURITY: We need to actually gather entropy and deliver it.
for ( size_t i = 0; i < size; i++ )
buffer[i] = i;
if ( !CopyToUser(user_buffer, buffer, size) )
return -1;
return 0;
}
void Init()
{
Syscall::Register(SYSCALL_GETENTROPY, (void*) sys_getentropy);
}
} // namespace Random
} // namespace Sortix

View File

@ -590,6 +590,7 @@ unistd/ftruncate.o \
unistd/getcwd.o \
unistd/getdomainname.o \
unistd/getegid.o \
unistd/getentropy.o \
unistd/geteuid.o \
unistd/getgid.o \
unistd/gethostname.o \

View File

@ -551,6 +551,7 @@ int dup3(int, int, int);
int execvpe(const char*, char* const [], char* const []);
char* get_current_dir_name(void);
int getdomainname(char*, size_t);
int getentropy(void*, size_t);
int pipe2(int [2], int);
void* sbrk(__intptr_t increment);
typedef unsigned int useconds_t;

View File

@ -0,0 +1,34 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
unistd/getentropy.cpp
Read entropy into an user buffer.
*******************************************************************************/
#include <sys/syscall.h>
#include <unistd.h>
DEFN_SYSCALL2(int, sys_getentropy, SYSCALL_GETENTROPY, void*, size_t);
extern "C" int getentropy(void* buffer, size_t size)
{
return sys_getentropy(buffer, size);
}