Skip to content

Commit ccd65f3

Browse files
author
Di Ferrari
authored
Merge pull request #15 from CodeAnarchist/random
implement global random(xoroshiro128+)
2 parents 3e0a9bd + c51a386 commit ccd65f3

4 files changed

Lines changed: 137 additions & 4 deletions

File tree

kernel/kernel.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "networking/processes/net_proc.h"
1818
#include "memory/page_allocator.h"
1919
#include "networking/network.h"
20+
#include "math/random.h"
2021

2122
void kernel_main() {
2223

@@ -27,7 +28,11 @@ void kernel_main() {
2728
enable_uart();
2829
kprintf_l("UART output enabled");
2930
// enable_talloc_verbose();
30-
31+
uint64_t seed;
32+
asm volatile("mrs %0, cntvct_el0" : "=r"(seed)); //virtual timer counter as entropy source for rng seed
33+
rng_init_global(seed);
34+
kprintf("Random init. seed: %i\n", seed);
35+
//kprintf("Next32: %i\n", rng_next32(&global_rng));
3136
set_exception_vectors();
3237
kprintf_l("Exception vectors set");
3338

@@ -86,4 +91,4 @@ void kernel_main() {
8691

8792
panic("Kernel did not activate any process");
8893

89-
}
94+
}

shared/math/random.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "random.h"
2+
#include "std/memfunctions.h"
3+
4+
rng_t global_rng;
5+
6+
void rng_seed(rng_t* rng, uint64_t seed){ //i guess it is "private", no definition in header
7+
uint64_t z = seed + 0x9E3779B97F4A7C15;
8+
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9;
9+
z = (z ^ (z >> 27)) *0x94D049BB133111EB;
10+
rng->s0 = z ^ (z >> 31);
11+
12+
z = seed + 0x9E3779B97F4A7C15 + 1;
13+
z = (z ^ (z >> 30)) *0xBF58476D1CE4E5B9;
14+
z = (z ^ (z >> 27))*0x94D049BB133111EB;
15+
rng->s1 = z ^ (z >> 31);
16+
}
17+
18+
uint64_t rng_next64(rng_t* rng){
19+
uint64_t s0 = rng->s0;
20+
uint64_t s1 = rng->s1;
21+
uint64_t result = s0 + s1;
22+
23+
s1 ^= s0;
24+
rng->s0 = rotl(s0, 55)^s1^(s1 << 14);
25+
rng->s1 = rotl(s1, 36);
26+
27+
return result;
28+
}
29+
30+
uint32_t rng_next32(rng_t* rng){
31+
return (uint32_t)(rng_next64(rng) >> 32);
32+
}
33+
34+
uint16_t rng_next16(rng_t* rng){
35+
return (uint16_t)(rng_next64(rng) >> 48);
36+
}
37+
38+
uint8_t rng_next8(rng_t* rng){
39+
return (uint8_t)(rng_next64(rng) >> 56);
40+
}
41+
42+
uint64_t rng_between64(rng_t* rng, uint64_t min, uint64_t max){
43+
if (max <= min) return min;
44+
return rng_next64(rng) % (max - min) + min;
45+
}
46+
47+
uint32_t rng_between32(rng_t* rng, uint32_t min, uint32_t max){
48+
if (max <= min) return min;
49+
return rng_next64(rng) % (max - min) + min;
50+
}
51+
52+
uint16_t rng_between16(rng_t* rng, uint16_t min, uint16_t max){
53+
if (max <= min) return min;
54+
return (uint16_t)(rng_next64(rng) % (max - min)) + min;
55+
}
56+
57+
uint8_t rng_between8(rng_t* rng, uint8_t min, uint8_t max){
58+
if (max <= min) return min;
59+
return (uint8_t)(rng_next64(rng) % (max - min)) + min;
60+
}
61+
62+
void rng_fill64(rng_t* rng, uint64_t* dst, uint32_t count){
63+
for (uint32_t i = 0; i < count; i++)
64+
dst[i] = rng_next64(rng);
65+
}
66+
67+
void rng_fill32(rng_t* rng, uint32_t* dst, uint32_t count){
68+
for (uint32_t i = 0; i < count; i++)
69+
dst[i] = rng_next32(rng);
70+
}
71+
72+
void rng_fill16(rng_t* rng, uint16_t* dst, uint32_t count){
73+
for (uint32_t i = 0; i < count; i++)
74+
dst[i] = rng_next16(rng);
75+
}
76+
77+
void rng_fill8(rng_t* rng, uint8_t* dst, uint32_t count){
78+
for (uint32_t i = 0; i < count; i++)
79+
dst[i] = rng_next8(rng);
80+
}
81+
82+
void rng_init_global(uint64_t seed) {
83+
rng_seed(&global_rng, seed);
84+
}

shared/math/random.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include "types.h"
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
//xoroshiro128+, pseudorandom
9+
typedef struct{
10+
uint64_t s0;
11+
uint64_t s1;
12+
}rng_t;
13+
14+
static inline uint64_t rotl(uint64_t x, int k){
15+
return (x << k)|(x >> (64 - k));
16+
}
17+
extern rng_t global_rng; //use &global_rng as rng_t* rng argument
18+
//init
19+
void rng_init_global(uint64_t seed);
20+
//single random
21+
uint8_t rng_next8(rng_t* rng);
22+
uint16_t rng_next16(rng_t* rng);
23+
uint32_t rng_next32(rng_t* rng);
24+
uint64_t rng_next64(rng_t* rng);
25+
26+
//random in range
27+
uint8_t rng_between8(rng_t* rng, uint8_t min, uint8_t max);
28+
uint16_t rng_between16(rng_t* rng, uint16_t min, uint16_t max);
29+
uint32_t rng_between32(rng_t* rng, uint32_t min, uint32_t max);
30+
uint64_t rng_between64(rng_t* rng, uint64_t min, uint64_t max);
31+
32+
//array fill
33+
void rng_fill8(rng_t* rng, uint8_t* dst, uint32_t count);
34+
void rng_fill16(rng_t* rng, uint16_t* dst, uint32_t count);
35+
void rng_fill32(rng_t* rng, uint32_t* dst, uint32_t count);
36+
void rng_fill64(rng_t* rng, uint64_t* dst, uint32_t count);
37+
38+
#ifdef __cplusplus
39+
}
40+
#endif
41+

shared/net/dhcp.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "dhcp.h"
22
#include "std/memfunctions.h"
3+
#include "math/random.h"
34

45
void create_dhcp_packet(uintptr_t p, dhcp_request *payload){
56
network_connection_ctx source = (network_connection_ctx){
@@ -15,7 +16,7 @@ void create_dhcp_packet(uintptr_t p, dhcp_request *payload){
1516
.htype = 1,//Ethernet
1617
.hlen = 6,//Mac length
1718
.hops = 0,
18-
.xid = 372,//Transaction ID: Static, could be random
19+
.xid = rng_next32(&global_rng),//Transaction ID: RANDOM
1920
.secs = 0,
2021
.flags = __builtin_bswap16(0x8000),//Broadcast
2122
.ciaddr = 0,
@@ -63,4 +64,6 @@ uint16_t dhcp_parse_option(dhcp_packet *pack, uint16_t option){
6364
if (pack->options[i] == option) return i;
6465

6566
return 0;
66-
}
67+
}
68+
69+

0 commit comments

Comments
 (0)