2 * QTest testcase for the Nuvoton NPCM7xx Random Number Generator
4 * Copyright 2020 Google LLC
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "qemu/osdep.h"
21 #include "libqtest-single.h"
22 #include "qemu/bitops.h"
23 #include "qemu/cutils.h"
25 #define RNG_BASE_ADDR 0xf000b000
27 /* Control and Status Register */
29 # define DVALID BIT(1) /* Data Valid */
30 # define RNGE BIT(0) /* RNG Enable */
35 # define ROSEL_NORMAL (2) /* RNG only works in this mode */
37 /* Number of bits to collect for randomness tests. */
38 #define TEST_INPUT_BITS (128)
40 static void dump_buf_if_failed(const uint8_t *buf
, size_t size
)
42 if (g_test_failed()) {
43 qemu_hexdump(stderr
, "", buf
, size
);
47 static void rng_writeb(unsigned int offset
, uint8_t value
)
49 writeb(RNG_BASE_ADDR
+ offset
, value
);
52 static uint8_t rng_readb(unsigned int offset
)
54 return readb(RNG_BASE_ADDR
+ offset
);
57 /* Disable RNG and set normal ring oscillator mode. */
58 static void rng_reset(void)
61 rng_writeb(RNGMODE
, ROSEL_NORMAL
);
64 /* Reset RNG and then enable it. */
65 static void rng_reset_enable(void)
68 rng_writeb(RNGCS
, RNGE
);
71 /* Wait until Data Valid bit is set. */
72 static bool rng_wait_ready(void)
74 /* qemu_guest_getrandom may fail. Assume it won't fail 10 times in a row. */
77 while (retries
-- > 0) {
78 if (rng_readb(RNGCS
) & DVALID
) {
87 * Perform a frequency (monobit) test, as defined by NIST SP 800-22, on the
88 * sequence in buf and return the P-value. This represents the probability of a
89 * truly random sequence having the same proportion of zeros and ones as the
92 * An RNG which always returns 0x00 or 0xff, or has some bits stuck at 0 or 1,
93 * will fail this test. However, an RNG which always returns 0x55, 0xf0 or some
94 * other value with an equal number of zeroes and ones will pass.
96 static double calc_monobit_p(const uint8_t *buf
, unsigned int len
)
102 for (i
= 0; i
< len
; i
++) {
104 * Each 1 counts as 1, each 0 counts as -1.
105 * s = cp - (8 - cp) = 2 * cp - 8
107 sn
+= 2 * ctpop8(buf
[i
]) - 8;
110 s_obs
= abs(sn
) / sqrt(len
* BITS_PER_BYTE
);
112 return erfc(s_obs
/ sqrt(2));
116 * Perform a runs test, as defined by NIST SP 800-22, and return the P-value.
117 * This represents the probability of a truly random sequence having the same
118 * number of runs (i.e. uninterrupted sequences of identical bits) as the
121 static double calc_runs_p(const unsigned long *buf
, unsigned int nr_bits
)
129 g_assert(nr_bits
% BITS_PER_LONG
== 0);
131 for (j
= 0; j
< nr_bits
/ BITS_PER_LONG
; j
++) {
132 nr_ones
+= __builtin_popcountl(buf
[j
]);
134 pi
= (double)nr_ones
/ nr_bits
;
136 for (k
= 0; k
< nr_bits
- 1; k
++) {
137 vn_obs
+= (test_bit(k
, buf
) ^ test_bit(k
+ 1, buf
));
141 return erfc(fabs(vn_obs
- 2 * nr_bits
* pi
* (1.0 - pi
))
142 / (2 * sqrt(2 * nr_bits
) * pi
* (1.0 - pi
)));
146 * Verifies that DVALID is clear, and RNGD reads zero, when RNGE is cleared,
147 * and DVALID eventually becomes set when RNGE is set.
149 static void test_enable_disable(void)
151 /* Disable: DVALID should not be set, and RNGD should read zero */
153 g_assert_cmphex(rng_readb(RNGCS
), ==, 0);
154 g_assert_cmphex(rng_readb(RNGD
), ==, 0);
156 /* Enable: DVALID should be set, but we can't make assumptions about RNGD */
157 rng_writeb(RNGCS
, RNGE
);
158 g_assert_true(rng_wait_ready());
159 g_assert_cmphex(rng_readb(RNGCS
), ==, DVALID
| RNGE
);
161 /* Disable: DVALID should not be set, and RNGD should read zero */
162 rng_writeb(RNGCS
, 0);
163 g_assert_cmphex(rng_readb(RNGCS
), ==, 0);
164 g_assert_cmphex(rng_readb(RNGD
), ==, 0);
168 * Verifies that the RNG only produces data when RNGMODE is set to 'normal'
169 * ring oscillator mode.
171 static void test_rosel(void)
174 g_assert_true(rng_wait_ready());
175 rng_writeb(RNGMODE
, 0);
176 g_assert_false(rng_wait_ready());
177 rng_writeb(RNGMODE
, ROSEL_NORMAL
);
178 g_assert_true(rng_wait_ready());
179 rng_writeb(RNGMODE
, 0);
180 g_assert_false(rng_wait_ready());
184 * Verifies that a continuous sequence of bits collected after enabling the RNG
185 * satisfies a monobit test.
187 static void test_continuous_monobit(void)
189 uint8_t buf
[TEST_INPUT_BITS
/ BITS_PER_BYTE
];
193 for (i
= 0; i
< sizeof(buf
); i
++) {
194 g_assert_true(rng_wait_ready());
195 buf
[i
] = rng_readb(RNGD
);
198 g_assert_cmpfloat(calc_monobit_p(buf
, sizeof(buf
)), >, 0.01);
199 dump_buf_if_failed(buf
, sizeof(buf
));
203 * Verifies that a continuous sequence of bits collected after enabling the RNG
204 * satisfies a runs test.
206 static void test_continuous_runs(void)
209 unsigned long l
[TEST_INPUT_BITS
/ BITS_PER_LONG
];
210 uint8_t c
[TEST_INPUT_BITS
/ BITS_PER_BYTE
];
215 for (i
= 0; i
< sizeof(buf
); i
++) {
216 g_assert_true(rng_wait_ready());
217 buf
.c
[i
] = rng_readb(RNGD
);
220 g_assert_cmpfloat(calc_runs_p(buf
.l
, sizeof(buf
) * BITS_PER_BYTE
), >, 0.01);
221 dump_buf_if_failed(buf
.c
, sizeof(buf
));
225 * Verifies that the first data byte collected after enabling the RNG satisfies
228 static void test_first_byte_monobit(void)
230 /* Enable, collect one byte, disable. Repeat until we have 100 bits. */
231 uint8_t buf
[TEST_INPUT_BITS
/ BITS_PER_BYTE
];
235 for (i
= 0; i
< sizeof(buf
); i
++) {
236 rng_writeb(RNGCS
, RNGE
);
237 g_assert_true(rng_wait_ready());
238 buf
[i
] = rng_readb(RNGD
);
239 rng_writeb(RNGCS
, 0);
242 g_assert_cmpfloat(calc_monobit_p(buf
, sizeof(buf
)), >, 0.01);
243 dump_buf_if_failed(buf
, sizeof(buf
));
247 * Verifies that the first data byte collected after enabling the RNG satisfies
250 static void test_first_byte_runs(void)
252 /* Enable, collect one byte, disable. Repeat until we have 100 bits. */
254 unsigned long l
[TEST_INPUT_BITS
/ BITS_PER_LONG
];
255 uint8_t c
[TEST_INPUT_BITS
/ BITS_PER_BYTE
];
260 for (i
= 0; i
< sizeof(buf
); i
++) {
261 rng_writeb(RNGCS
, RNGE
);
262 g_assert_true(rng_wait_ready());
263 buf
.c
[i
] = rng_readb(RNGD
);
264 rng_writeb(RNGCS
, 0);
267 g_assert_cmpfloat(calc_runs_p(buf
.l
, sizeof(buf
) * BITS_PER_BYTE
), >, 0.01);
268 dump_buf_if_failed(buf
.c
, sizeof(buf
));
271 int main(int argc
, char **argv
)
275 g_test_init(&argc
, &argv
, NULL
);
276 g_test_set_nonfatal_assertions();
278 qtest_add_func("npcm7xx_rng/enable_disable", test_enable_disable
);
279 qtest_add_func("npcm7xx_rng/rosel", test_rosel
);
281 * These tests fail intermittently; only run them on explicit
282 * request until we figure out why.
284 if (getenv("QEMU_TEST_FLAKY_RNG_TESTS")) {
285 qtest_add_func("npcm7xx_rng/continuous/monobit", test_continuous_monobit
);
286 qtest_add_func("npcm7xx_rng/continuous/runs", test_continuous_runs
);
287 qtest_add_func("npcm7xx_rng/first_byte/monobit", test_first_byte_monobit
);
288 qtest_add_func("npcm7xx_rng/first_byte/runs", test_first_byte_runs
);
291 qtest_start("-machine npcm750-evb");