1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2015 Cedric Hnyda <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * Calls getrandom(2), checks that the buffer is filled with random bytes
6*49cdfc7eSAndroid Build Coastguard Worker * and expects success.
7*49cdfc7eSAndroid Build Coastguard Worker */
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
10*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/getrandom.h"
11*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker #define PROC_ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker static int modes[] = { 0, GRND_RANDOM, GRND_NONBLOCK,
16*49cdfc7eSAndroid Build Coastguard Worker GRND_RANDOM | GRND_NONBLOCK };
17*49cdfc7eSAndroid Build Coastguard Worker
check_content(unsigned char * buf,int nb)18*49cdfc7eSAndroid Build Coastguard Worker static int check_content(unsigned char *buf, int nb)
19*49cdfc7eSAndroid Build Coastguard Worker {
20*49cdfc7eSAndroid Build Coastguard Worker int table[256];
21*49cdfc7eSAndroid Build Coastguard Worker int i, index, max;
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker memset(table, 0, sizeof(table));
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker max = 6 + nb * 0.2;
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < nb; i++) {
28*49cdfc7eSAndroid Build Coastguard Worker index = buf[i];
29*49cdfc7eSAndroid Build Coastguard Worker table[index]++;
30*49cdfc7eSAndroid Build Coastguard Worker }
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < nb; i++) {
33*49cdfc7eSAndroid Build Coastguard Worker if (max > 0 && table[i] > max)
34*49cdfc7eSAndroid Build Coastguard Worker return 0;
35*49cdfc7eSAndroid Build Coastguard Worker }
36*49cdfc7eSAndroid Build Coastguard Worker return 1;
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker
verify_getrandom(unsigned int n)39*49cdfc7eSAndroid Build Coastguard Worker static void verify_getrandom(unsigned int n)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker unsigned char buf[256];
42*49cdfc7eSAndroid Build Coastguard Worker int bufsize = 64, entropy_avail;
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker if (access(PROC_ENTROPY_AVAIL, F_OK) == 0) {
45*49cdfc7eSAndroid Build Coastguard Worker SAFE_FILE_SCANF(PROC_ENTROPY_AVAIL, "%d", &entropy_avail);
46*49cdfc7eSAndroid Build Coastguard Worker if (entropy_avail > 256)
47*49cdfc7eSAndroid Build Coastguard Worker bufsize = sizeof(buf);
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker memset(buf, 0, sizeof(buf));
51*49cdfc7eSAndroid Build Coastguard Worker do {
52*49cdfc7eSAndroid Build Coastguard Worker TEST(tst_syscall(__NR_getrandom, buf, bufsize, modes[n]));
53*49cdfc7eSAndroid Build Coastguard Worker } while ((modes[n] & GRND_NONBLOCK) && TST_RET == -1
54*49cdfc7eSAndroid Build Coastguard Worker && TST_ERR == EAGAIN);
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker if (!check_content(buf, TST_RET))
57*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "getrandom failed");
58*49cdfc7eSAndroid Build Coastguard Worker else
59*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "getrandom returned %ld", TST_RET);
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
63*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(modes),
64*49cdfc7eSAndroid Build Coastguard Worker .test = verify_getrandom,
65*49cdfc7eSAndroid Build Coastguard Worker };
66