Blame test/testutil/random.c

Packit c4476c
/*
Packit c4476c
 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
Packit c4476c
 *
Packit c4476c
 * Licensed under the Apache License 2.0 (the "License").  You may not use
Packit c4476c
 * this file except in compliance with the License.  You can obtain a copy
Packit c4476c
 * in the file LICENSE in the source distribution or at
Packit c4476c
 * https://www.openssl.org/source/license.html
Packit c4476c
 */
Packit c4476c
Packit c4476c
#include "../testutil.h"
Packit c4476c
Packit c4476c
/*
Packit c4476c
 * This is an implementation of the algorithm used by the GNU C library's
Packit c4476c
 * random(3) pseudorandom number generator as described:
Packit c4476c
 *      https://www.mscs.dal.ca/~selinger/random/
Packit c4476c
 */
Packit c4476c
static uint32_t test_random_state[31];
Packit c4476c
Packit c4476c
uint32_t test_random(void) {
Packit c4476c
    static unsigned int pos = 3;
Packit c4476c
Packit c4476c
    if (pos == 31)
Packit c4476c
        pos = 0;
Packit c4476c
    test_random_state[pos] += test_random_state[(pos + 28) % 31];
Packit c4476c
    return test_random_state[pos++] / 2;
Packit c4476c
}
Packit c4476c
Packit c4476c
void test_random_seed(uint32_t sd) {
Packit c4476c
    int i;
Packit c4476c
    int32_t s;
Packit c4476c
    const unsigned int mod = (1u << 31) - 1;
Packit c4476c
Packit c4476c
    test_random_state[0] = sd;
Packit c4476c
    for (i = 1; i < 31; i++) {
Packit c4476c
        s = (int32_t)test_random_state[i - 1];
Packit c4476c
        test_random_state[i] = (uint32_t)((16807 * (int64_t)s) % mod);
Packit c4476c
    }
Packit c4476c
    for (i = 34; i < 344; i++)
Packit c4476c
        test_random();
Packit c4476c
}