2015-04-11 01:44:37 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2015 Wind River Systems, Inc.
|
|
|
|
*
|
2017-01-19 02:01:01 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-11 01:44:37 +02:00
|
|
|
*/
|
|
|
|
|
2015-12-04 16:09:39 +01:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Non-random number generator based on system timer
|
|
|
|
*
|
2015-10-20 18:42:33 +02:00
|
|
|
* This module provides a non-random implementation of sys_rand32_get(), which
|
|
|
|
* is not meant to be used in a final product as a truly random number
|
|
|
|
* generator. It was provided to allow testing on a platform that does not (yet)
|
|
|
|
* provide a random number generator.
|
2015-07-01 23:22:39 +02:00
|
|
|
*/
|
2015-04-11 01:44:37 +02:00
|
|
|
|
2023-10-07 00:38:53 +02:00
|
|
|
#include <zephyr/random/random.h>
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/drivers/timer/system_timer.h>
|
|
|
|
#include <zephyr/kernel.h>
|
2023-01-21 06:42:30 +01:00
|
|
|
#include <zephyr/spinlock.h>
|
2019-07-23 21:16:24 +02:00
|
|
|
#include <string.h>
|
2015-04-11 01:44:37 +02:00
|
|
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
|
2023-01-21 06:42:30 +01:00
|
|
|
static struct k_spinlock rand32_lock;
|
2015-05-25 17:07:09 +02:00
|
|
|
|
2015-07-01 23:22:39 +02:00
|
|
|
/**
|
2015-07-01 23:51:40 +02:00
|
|
|
* @brief Get a 32 bit random number
|
2015-04-11 01:44:37 +02:00
|
|
|
*
|
2023-01-21 06:42:30 +01:00
|
|
|
* This pseudo-random number generator returns values that are based off the
|
2015-05-25 17:07:09 +02:00
|
|
|
* target's clock counter, which means that successive calls will return
|
|
|
|
* different values.
|
2015-04-11 01:44:37 +02:00
|
|
|
*
|
2015-07-01 23:29:04 +02:00
|
|
|
* @return a 32-bit number
|
2015-04-11 01:44:37 +02:00
|
|
|
*/
|
2024-03-25 19:49:05 +01:00
|
|
|
static inline uint32_t rand32_get(void)
|
2015-04-11 01:44:37 +02:00
|
|
|
{
|
2023-10-10 19:34:23 +02:00
|
|
|
/* initial seed value */
|
|
|
|
static uint64_t state = (uint64_t)CONFIG_TIMER_RANDOM_INITIAL_STATE;
|
2023-01-21 06:42:30 +01:00
|
|
|
k_spinlock_key_t key = k_spin_lock(&rand32_lock);
|
|
|
|
|
|
|
|
state = state + k_cycle_get_32();
|
|
|
|
state = state * 2862933555777941757ULL + 3037000493ULL;
|
|
|
|
uint32_t val = (uint32_t)(state >> 32);
|
|
|
|
|
|
|
|
k_spin_unlock(&rand32_lock, key);
|
|
|
|
return val;
|
2015-04-11 01:44:37 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 21:16:24 +02:00
|
|
|
/**
|
|
|
|
* @brief Fill destination buffer with random numbers
|
|
|
|
*
|
2023-01-21 06:42:30 +01:00
|
|
|
* The pseudo-random number generator returns values that are based off the
|
2019-07-23 21:16:24 +02:00
|
|
|
* target's clock counter, which means that successive calls will return
|
|
|
|
* different values.
|
|
|
|
*
|
|
|
|
* @param dst destination buffer to fill
|
|
|
|
* @param outlen size of destination buffer to fill
|
|
|
|
*/
|
2020-06-04 20:00:45 +02:00
|
|
|
void z_impl_sys_rand_get(void *dst, size_t outlen)
|
2019-07-23 21:16:24 +02:00
|
|
|
{
|
2021-04-09 17:52:14 +02:00
|
|
|
uint8_t *udst = dst;
|
|
|
|
uint32_t blocksize;
|
2020-05-27 18:26:57 +02:00
|
|
|
uint32_t ret;
|
2019-07-23 21:16:24 +02:00
|
|
|
|
2021-04-09 17:52:14 +02:00
|
|
|
while (outlen) {
|
2024-03-25 19:49:05 +01:00
|
|
|
ret = rand32_get();
|
2021-04-09 17:52:14 +02:00
|
|
|
blocksize = MIN(outlen, sizeof(ret));
|
|
|
|
(void)memcpy((void *)udst, &ret, blocksize);
|
|
|
|
udst += blocksize;
|
|
|
|
outlen -= blocksize;
|
2019-07-23 21:16:24 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 01:44:37 +02:00
|
|
|
#endif /* __GNUC__ */
|