1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include "../testutil.h"
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ci/*
13e1051a39Sopenharmony_ci * This is an implementation of the algorithm used by the GNU C library's
14e1051a39Sopenharmony_ci * random(3) pseudorandom number generator as described:
15e1051a39Sopenharmony_ci *      https://www.mscs.dal.ca/~selinger/random/
16e1051a39Sopenharmony_ci */
17e1051a39Sopenharmony_cistatic uint32_t test_random_state[31];
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ciuint32_t test_random(void) {
20e1051a39Sopenharmony_ci    static unsigned int pos = 3;
21e1051a39Sopenharmony_ci
22e1051a39Sopenharmony_ci    if (pos == 31)
23e1051a39Sopenharmony_ci        pos = 0;
24e1051a39Sopenharmony_ci    test_random_state[pos] += test_random_state[(pos + 28) % 31];
25e1051a39Sopenharmony_ci    return test_random_state[pos++] / 2;
26e1051a39Sopenharmony_ci}
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_civoid test_random_seed(uint32_t sd) {
29e1051a39Sopenharmony_ci    int i;
30e1051a39Sopenharmony_ci    int32_t s;
31e1051a39Sopenharmony_ci    const unsigned int mod = (1u << 31) - 1;
32e1051a39Sopenharmony_ci
33e1051a39Sopenharmony_ci    test_random_state[0] = sd;
34e1051a39Sopenharmony_ci    for (i = 1; i < 31; i++) {
35e1051a39Sopenharmony_ci        s = (int32_t)test_random_state[i - 1];
36e1051a39Sopenharmony_ci        test_random_state[i] = (uint32_t)((16807 * (int64_t)s) % mod);
37e1051a39Sopenharmony_ci    }
38e1051a39Sopenharmony_ci    for (i = 34; i < 344; i++)
39e1051a39Sopenharmony_ci        test_random();
40e1051a39Sopenharmony_ci}
41