1dba28784SAugustin Cavalier /*
2*8548a4adSAugustin Cavalier * Copyright 2018-2022, Haiku, Inc. All rights reserved.
3dba28784SAugustin Cavalier * Distributed under the terms of the MIT License.
4dba28784SAugustin Cavalier */
5dba28784SAugustin Cavalier
6dba28784SAugustin Cavalier extern "C" {
7dba28784SAugustin Cavalier #include <compat/sys/libkern.h>
8dba28784SAugustin Cavalier }
9dba28784SAugustin Cavalier
10dba28784SAugustin Cavalier #include <util/Random.h>
11dba28784SAugustin Cavalier
12dba28784SAugustin Cavalier
13dba28784SAugustin Cavalier u_int
read_random(void * buf,u_int len)14dba28784SAugustin Cavalier read_random(void* buf, u_int len)
15dba28784SAugustin Cavalier {
16dba28784SAugustin Cavalier uint8* bufs = (uint8*)buf;
17dba28784SAugustin Cavalier for (int i = 0; i < len; i++)
18dba28784SAugustin Cavalier bufs[i] = secure_get_random<uint8>();
19dba28784SAugustin Cavalier return len;
20dba28784SAugustin Cavalier }
21dba28784SAugustin Cavalier
22dba28784SAugustin Cavalier
23dba28784SAugustin Cavalier void
arc4rand(void * ptr,u_int len,int reseed)24dba28784SAugustin Cavalier arc4rand(void *ptr, u_int len, int reseed)
25dba28784SAugustin Cavalier {
26dba28784SAugustin Cavalier read_random(ptr, len);
27dba28784SAugustin Cavalier }
28dba28784SAugustin Cavalier
29dba28784SAugustin Cavalier
30dba28784SAugustin Cavalier uint32_t
arc4random(void)31dba28784SAugustin Cavalier arc4random(void)
32dba28784SAugustin Cavalier {
33dba28784SAugustin Cavalier uint32_t ret;
34dba28784SAugustin Cavalier
35dba28784SAugustin Cavalier arc4rand(&ret, sizeof ret, 0);
36dba28784SAugustin Cavalier return ret;
37dba28784SAugustin Cavalier }
38*8548a4adSAugustin Cavalier
39*8548a4adSAugustin Cavalier
40*8548a4adSAugustin Cavalier void
arc4random_buf(void * ptr,size_t len)41*8548a4adSAugustin Cavalier arc4random_buf(void *ptr, size_t len)
42*8548a4adSAugustin Cavalier {
43*8548a4adSAugustin Cavalier arc4rand(ptr, len, 0);
44*8548a4adSAugustin Cavalier }
45