aboutsummaryrefslogtreecommitdiff
path: root/src/kernel
diff options
context:
space:
mode:
authorAlex Auvolat <alex.auvolat@ens.fr>2015-03-08 11:10:45 +0100
committerAlex Auvolat <alex.auvolat@ens.fr>2015-03-08 11:10:45 +0100
commit6c50033dfb7a4dc7094d96f9339459b08b4efac9 (patch)
treeb3c8032d95620c2eb009273adb675c699b8a5a94 /src/kernel
parent88636d28b812e425c51be2d4d24d16a504dae147 (diff)
downloadkogata-6c50033dfb7a4dc7094d96f9339459b08b4efac9.tar.gz
kogata-6c50033dfb7a4dc7094d96f9339459b08b4efac9.zip
Add PRNG
Diffstat (limited to 'src/kernel')
-rw-r--r--src/kernel/Makefile4
-rw-r--r--src/kernel/core/kmain.c10
-rw-r--r--src/kernel/core/prng.c38
-rw-r--r--src/kernel/include/prng.h9
4 files changed, 59 insertions, 2 deletions
diff --git a/src/kernel/Makefile b/src/kernel/Makefile
index 88a83e2..1a7c12e 100644
--- a/src/kernel/Makefile
+++ b/src/kernel/Makefile
@@ -2,14 +2,14 @@
OBJ = core/loader.o core/dbglog.o \
core/gdt.o core/idt.o core/interrupt.o core/context_switch.o core/thread.o \
core/frame.o core/paging.o core/freemem.o core/region.o core/kmalloc.o \
- core/worker.o \
+ core/worker.o core/prng.o \
user/vfs.o user/nullfs.o user/process.o user/elf.o user/syscall.o \
dev/pci.o dev/pciide.o \
fs/iso9660.o
LIB = ../common/libc/libc.lib ../common/libkogata/libkogata.lib ../common/libalgo/libalgo.lib
-CFLAGS = -I ./include -I ../common/include
+CFLAGS = -I ./include -I ../common/include -DPRNG_INIT_ENTROPY=$(shell date +%N)
LDFLAGS = -T linker.ld -Xlinker -Map=kernel.map
diff --git a/src/kernel/core/kmain.c b/src/kernel/core/kmain.c
index 5cd71c5..7cdd109 100644
--- a/src/kernel/core/kmain.c
+++ b/src/kernel/core/kmain.c
@@ -23,10 +23,16 @@
#include <slab_alloc.h>
#include <string.h>
+#include <prng.h>
+
#include <dev/pci.h>
#include <dev/pciide.h>
#include <fs/iso9660.h>
+#ifndef PRNG_INIT_ENTROPY
+#define PRNG_INIT_ENTROPY 1299733235
+#endif
+
// ===== FOR TESTS =====
#define TEST_PLACEHOLDER_AFTER_IDT
#define TEST_PLACEHOLDER_AFTER_REGION
@@ -149,6 +155,10 @@ void kernel_init_stage2(void* data) {
// Register FS drivers
register_iso9660_driver();
+ // Add entropy to prng
+ uint32_t x = PRNG_INIT_ENTROPY;
+ prng_add_entropy((uint8_t*)&x, sizeof(x));
+
// Parse command line
btree_t *cmdline = parse_cmdline((const char*)mbd->cmdline);
diff --git a/src/kernel/core/prng.c b/src/kernel/core/prng.c
new file mode 100644
index 0000000..d4307b6
--- /dev/null
+++ b/src/kernel/core/prng.c
@@ -0,0 +1,38 @@
+#include <prng.h>
+
+#define EPOOLSIZE 2048
+
+static uint32_t x = 211;
+static int n = 0;
+static char entropy[EPOOLSIZE];
+static int entropy_count = 0;
+static const uint32_t a = 16807;
+static const uint32_t m = 0x7FFFFFFF;
+
+uint16_t prng_word() {
+ if (++n == 100) {
+ n = 0;
+ if (entropy_count) {
+ entropy_count--;
+ x += entropy[entropy_count];
+ }
+ }
+ x = (x * a) % m;
+ return x & 0xFFFF;
+}
+
+void prng_bytes(uint8_t* out, size_t nbytes) {
+ uint16_t *d = (uint16_t*)out;
+ for (size_t i = 0; i < nbytes / 2; i++) {
+ d[i] = prng_word();
+ }
+ if (nbytes & 1) out[nbytes-1] = prng_word() & 0xFF;
+}
+
+void prng_add_entropy(const uint8_t* ptr, size_t nbytes) {
+ while (nbytes-- && entropy_count < EPOOLSIZE - 1) {
+ entropy[entropy_count++] = *(ptr++);
+ }
+}
+
+/* vim: set ts=4 sw=4 tw=0 noet :*/
diff --git a/src/kernel/include/prng.h b/src/kernel/include/prng.h
new file mode 100644
index 0000000..0a9e036
--- /dev/null
+++ b/src/kernel/include/prng.h
@@ -0,0 +1,9 @@
+#include <stdint.h>
+#include <stddef.h>
+
+void prng_add_entropy(const uint8_t* ptr, size_t nbytes); // add entropy to generator
+
+uint16_t prng_word();
+void prng_bytes(uint8_t* out, size_t nbytes); // generate some bytes
+
+/* vim: set ts=4 sw=4 tw=0 noet :*/