blob: 759271f0c82b100685087f327f0e0cdfe7087fa6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include <common.h>
#include <stdlib.h>
#include <linux/hw_random.h>
static unsigned int random_seed;
#if RAND_MAX > 32767
#error this rand implementation is for RAND_MAX < 32678 only.
#endif
unsigned int rand(void)
{
random_seed = random_seed * 1103515245 + 12345;
return (random_seed / 65536) % (RAND_MAX + 1);
}
void srand(unsigned int seed)
{
random_seed = seed;
}
/**
* get_random_bytes - get pseudo random numbers.
* This interface can be good enough to generate MAC address
* or use for NAND test.
*/
void get_random_bytes(void *_buf, int len)
{
char *buf = _buf;
while (len--)
*buf++ = rand() % 256;
}
/**
* get_crypto_bytes - get random numbers suitable for cryptographic needs.
*/
static int _get_crypto_bytes(void *buf, int len)
{
struct hwrng *rng;
rng = hwrng_get_first();
if (IS_ERR(rng))
return PTR_ERR(rng);
while (len) {
int bytes = hwrng_get_data(rng, buf, len, true);
if (!bytes)
return -ENOMEDIUM;
if (bytes < 0)
return bytes;
len -= bytes;
buf = buf + bytes;
}
return 0;
}
int get_crypto_bytes(void *buf, int len)
{
int err;
err = _get_crypto_bytes(buf, len);
if (!err)
return 0;
if (!IS_ENABLED(CONFIG_ALLOW_PRNG_FALLBACK)) {
pr_err("error: no HWRNG available!\n");
return err;
}
pr_warn("warning: falling back to Pseudo RNG source!\n");
get_random_bytes(buf, len);
return 0;
}
|