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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  
     | 
    
      /*
 * QTest testcase for the Nuvoton NPCM7xx Random Number Generator
 *
 * Copyright 2020 Google LLC
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 */
#include "qemu/osdep.h"
#include <math.h>
#include "libqtest-single.h"
#include "qemu/bitops.h"
#define RNG_BASE_ADDR   0xf000b000
/* Control and Status Register */
#define RNGCS   0x00
# define DVALID     BIT(1)  /* Data Valid */
# define RNGE       BIT(0)  /* RNG Enable */
/* Data Register */
#define RNGD    0x04
/* Mode Register */
#define RNGMODE 0x08
# define ROSEL_NORMAL   (2) /* RNG only works in this mode */
/* Number of bits to collect for randomness tests. */
#define TEST_INPUT_BITS  (128)
static void rng_writeb(unsigned int offset, uint8_t value)
{
    writeb(RNG_BASE_ADDR + offset, value);
}
static uint8_t rng_readb(unsigned int offset)
{
    return readb(RNG_BASE_ADDR + offset);
}
/* Disable RNG and set normal ring oscillator mode. */
static void rng_reset(void)
{
    rng_writeb(RNGCS, 0);
    rng_writeb(RNGMODE, ROSEL_NORMAL);
}
/* Reset RNG and then enable it. */
static void rng_reset_enable(void)
{
    rng_reset();
    rng_writeb(RNGCS, RNGE);
}
/* Wait until Data Valid bit is set. */
static bool rng_wait_ready(void)
{
    /* qemu_guest_getrandom may fail. Assume it won't fail 10 times in a row. */
    int retries = 10;
    while (retries-- > 0) {
        if (rng_readb(RNGCS) & DVALID) {
            return true;
        }
    }
    return false;
}
/*
 * Perform a frequency (monobit) test, as defined by NIST SP 800-22, on the
 * sequence in buf and return the P-value. This represents the probability of a
 * truly random sequence having the same proportion of zeros and ones as the
 * sequence in buf.
 *
 * An RNG which always returns 0x00 or 0xff, or has some bits stuck at 0 or 1,
 * will fail this test. However, an RNG which always returns 0x55, 0xf0 or some
 * other value with an equal number of zeroes and ones will pass.
 */
static double calc_monobit_p(const uint8_t *buf, unsigned int len)
{
    unsigned int i;
    double s_obs;
    int sn = 0;
    for (i = 0; i < len; i++) {
        /*
         * Each 1 counts as 1, each 0 counts as -1.
         * s = cp - (8 - cp) = 2 * cp - 8
         */
        sn += 2 * ctpop8(buf[i]) - 8;
    }
    s_obs = abs(sn) / sqrt(len * BITS_PER_BYTE);
    return erfc(s_obs / sqrt(2));
}
/*
 * Perform a runs test, as defined by NIST SP 800-22, and return the P-value.
 * This represents the probability of a truly random sequence having the same
 * number of runs (i.e. uninterrupted sequences of identical bits) as the
 * sequence in buf.
 */
static double calc_runs_p(const unsigned long *buf, unsigned int nr_bits)
{
    unsigned int j;
    unsigned int k;
    int nr_ones = 0;
    int vn_obs = 0;
    double pi;
    g_assert(nr_bits % BITS_PER_LONG == 0);
    for (j = 0; j < nr_bits / BITS_PER_LONG; j++) {
        nr_ones += __builtin_popcountl(buf[j]);
    }
    pi = (double)nr_ones / nr_bits;
    for (k = 0; k < nr_bits - 1; k++) {
        vn_obs += (test_bit(k, buf) ^ test_bit(k + 1, buf));
    }
    vn_obs += 1;
    return erfc(fabs(vn_obs - 2 * nr_bits * pi * (1.0 - pi))
                / (2 * sqrt(2 * nr_bits) * pi * (1.0 - pi)));
}
/*
 * Verifies that DVALID is clear, and RNGD reads zero, when RNGE is cleared,
 * and DVALID eventually becomes set when RNGE is set.
 */
static void test_enable_disable(void)
{
    /* Disable: DVALID should not be set, and RNGD should read zero */
    rng_reset();
    g_assert_cmphex(rng_readb(RNGCS), ==, 0);
    g_assert_cmphex(rng_readb(RNGD), ==, 0);
    /* Enable: DVALID should be set, but we can't make assumptions about RNGD */
    rng_writeb(RNGCS, RNGE);
    g_assert_true(rng_wait_ready());
    g_assert_cmphex(rng_readb(RNGCS), ==, DVALID | RNGE);
    /* Disable: DVALID should not be set, and RNGD should read zero */
    rng_writeb(RNGCS, 0);
    g_assert_cmphex(rng_readb(RNGCS), ==, 0);
    g_assert_cmphex(rng_readb(RNGD), ==, 0);
}
/*
 * Verifies that the RNG only produces data when RNGMODE is set to 'normal'
 * ring oscillator mode.
 */
static void test_rosel(void)
{
    rng_reset_enable();
    g_assert_true(rng_wait_ready());
    rng_writeb(RNGMODE, 0);
    g_assert_false(rng_wait_ready());
    rng_writeb(RNGMODE, ROSEL_NORMAL);
    g_assert_true(rng_wait_ready());
    rng_writeb(RNGMODE, 0);
    g_assert_false(rng_wait_ready());
}
/*
 * Verifies that a continuous sequence of bits collected after enabling the RNG
 * satisfies a monobit test.
 */
static void test_continuous_monobit(void)
{
    uint8_t buf[TEST_INPUT_BITS / BITS_PER_BYTE];
    unsigned int i;
    rng_reset_enable();
    for (i = 0; i < sizeof(buf); i++) {
        g_assert_true(rng_wait_ready());
        buf[i] = rng_readb(RNGD);
    }
    g_assert_cmpfloat(calc_monobit_p(buf, sizeof(buf)), >, 0.01);
}
/*
 * Verifies that a continuous sequence of bits collected after enabling the RNG
 * satisfies a runs test.
 */
static void test_continuous_runs(void)
{
    union {
        unsigned long l[TEST_INPUT_BITS / BITS_PER_LONG];
        uint8_t c[TEST_INPUT_BITS / BITS_PER_BYTE];
    } buf;
    unsigned int i;
    rng_reset_enable();
    for (i = 0; i < sizeof(buf); i++) {
        g_assert_true(rng_wait_ready());
        buf.c[i] = rng_readb(RNGD);
    }
    g_assert_cmpfloat(calc_runs_p(buf.l, sizeof(buf) * BITS_PER_BYTE), >, 0.01);
}
/*
 * Verifies that the first data byte collected after enabling the RNG satisfies
 * a monobit test.
 */
static void test_first_byte_monobit(void)
{
    /* Enable, collect one byte, disable. Repeat until we have 100 bits. */
    uint8_t buf[TEST_INPUT_BITS / BITS_PER_BYTE];
    unsigned int i;
    rng_reset();
    for (i = 0; i < sizeof(buf); i++) {
        rng_writeb(RNGCS, RNGE);
        g_assert_true(rng_wait_ready());
        buf[i] = rng_readb(RNGD);
        rng_writeb(RNGCS, 0);
    }
    g_assert_cmpfloat(calc_monobit_p(buf, sizeof(buf)), >, 0.01);
}
/*
 * Verifies that the first data byte collected after enabling the RNG satisfies
 * a runs test.
 */
static void test_first_byte_runs(void)
{
    /* Enable, collect one byte, disable. Repeat until we have 100 bits. */
    union {
        unsigned long l[TEST_INPUT_BITS / BITS_PER_LONG];
        uint8_t c[TEST_INPUT_BITS / BITS_PER_BYTE];
    } buf;
    unsigned int i;
    rng_reset();
    for (i = 0; i < sizeof(buf); i++) {
        rng_writeb(RNGCS, RNGE);
        g_assert_true(rng_wait_ready());
        buf.c[i] = rng_readb(RNGD);
        rng_writeb(RNGCS, 0);
    }
    g_assert_cmpfloat(calc_runs_p(buf.l, sizeof(buf) * BITS_PER_BYTE), >, 0.01);
}
int main(int argc, char **argv)
{
    int ret;
    g_test_init(&argc, &argv, NULL);
    g_test_set_nonfatal_assertions();
    qtest_add_func("npcm7xx_rng/enable_disable", test_enable_disable);
    qtest_add_func("npcm7xx_rng/rosel", test_rosel);
    /*
     * These tests fail intermittently; only run them on explicit
     * request until we figure out why.
     */
    if (getenv("QEMU_TEST_FLAKY_RNG_TESTS")) {
        qtest_add_func("npcm7xx_rng/continuous/monobit", test_continuous_monobit);
        qtest_add_func("npcm7xx_rng/continuous/runs", test_continuous_runs);
        qtest_add_func("npcm7xx_rng/first_byte/monobit", test_first_byte_monobit);
        qtest_add_func("npcm7xx_rng/first_byte/runs", test_first_byte_runs);
    }
    qtest_start("-machine npcm750-evb");
    ret = g_test_run();
    qtest_end();
    return ret;
}
 
     |