diff --git a/kernel/Makefile b/kernel/Makefile
index 3308b54b..a4b44668 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -123,6 +123,7 @@ pipe.o \
poll.o \
process.o \
ptable.o \
+random.o \
refcount.o \
registers.o \
resource.o \
diff --git a/kernel/include/sortix/kernel/random.h b/kernel/include/sortix/kernel/random.h
new file mode 100644
index 00000000..0f85e741
--- /dev/null
+++ b/kernel/include/sortix/kernel/random.h
@@ -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 .
+
+ sortix/kernel/random.h
+ Kernel entropy gathering.
+
+*******************************************************************************/
+
+namespace Sortix {
+namespace Random {
+
+void Init();
+
+} // namespace Random
+} // namespace Sortix
diff --git a/kernel/include/sortix/syscall.h b/kernel/include/sortix/syscall.h
index 401c33ec..229faa9c 100644
--- a/kernel/include/sortix/syscall.h
+++ b/kernel/include/sortix/syscall.h
@@ -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
diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp
index 19c95865..40719440 100644
--- a/kernel/kernel.cpp
+++ b/kernel/kernel.cpp
@@ -54,6 +54,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -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 )
{
diff --git a/kernel/random.cpp b/kernel/random.cpp
new file mode 100644
index 00000000..a3a52cb2
--- /dev/null
+++ b/kernel/random.cpp
@@ -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 .
+
+ random.cpp
+ Kernel entropy gathering.
+
+*******************************************************************************/
+
+#include
+
+#include
+#include
+#include
+
+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
diff --git a/libc/Makefile b/libc/Makefile
index 202b188b..51cd7696 100644
--- a/libc/Makefile
+++ b/libc/Makefile
@@ -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 \
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 1f654ca3..73291194 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -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;
diff --git a/libc/unistd/getentropy.cpp b/libc/unistd/getentropy.cpp
new file mode 100644
index 00000000..e8c02352
--- /dev/null
+++ b/libc/unistd/getentropy.cpp
@@ -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 .
+
+ unistd/getentropy.cpp
+ Read entropy into an user buffer.
+
+*******************************************************************************/
+
+#include
+
+#include
+
+DEFN_SYSCALL2(int, sys_getentropy, SYSCALL_GETENTROPY, void*, size_t);
+
+extern "C" int getentropy(void* buffer, size_t size)
+{
+ return sys_getentropy(buffer, size);
+}