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