2013-04-11 06:22:22 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Haiku, Inc. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Paweł Dziepak, pdziepak@quarnos.org
|
|
|
|
*/
|
|
|
|
#ifndef KERNEL_UTIL_RANDOM_H
|
|
|
|
#define KERNEL_UTIL_RANDOM_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <smp.h>
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_FAST_RANDOM_VALUE 0x7fff
|
|
|
|
#define MAX_RANDOM_VALUE 0x7fffffffu
|
2013-04-11 14:31:58 +04:00
|
|
|
#define MAX_SECURE_RANDOM_VALUE 0xffffffffu
|
2013-04-11 06:22:22 +04:00
|
|
|
|
2013-04-11 14:15:47 +04:00
|
|
|
static const int kFastRandomShift = 15;
|
|
|
|
static const int kRandomShift = 31;
|
2013-04-11 14:31:58 +04:00
|
|
|
static const int kSecureRandomShift = 32;
|
2013-04-11 06:22:22 +04:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
unsigned int fast_random_value(void);
|
|
|
|
unsigned int random_value(void);
|
2013-04-11 14:31:58 +04:00
|
|
|
unsigned int secure_random_value(void);
|
2013-04-11 06:22:22 +04:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T
|
|
|
|
fast_get_random()
|
|
|
|
{
|
|
|
|
size_t shift = 0;
|
|
|
|
T random = 0;
|
|
|
|
while (shift < sizeof(T) * 8) {
|
|
|
|
random |= (T)fast_random_value() << shift;
|
|
|
|
shift += kFastRandomShift;
|
|
|
|
}
|
|
|
|
|
|
|
|
return random;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T
|
|
|
|
get_random()
|
|
|
|
{
|
|
|
|
size_t shift = 0;
|
|
|
|
T random = 0;
|
|
|
|
while (shift < sizeof(T) * 8) {
|
|
|
|
random |= (T)random_value() << shift;
|
|
|
|
shift += kRandomShift;
|
|
|
|
}
|
|
|
|
|
|
|
|
return random;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-11 14:31:58 +04:00
|
|
|
template<typename T>
|
|
|
|
T
|
|
|
|
secure_get_random()
|
|
|
|
{
|
|
|
|
size_t shift = 0;
|
|
|
|
T random = 0;
|
|
|
|
while (shift < sizeof(T) * 8) {
|
|
|
|
random |= (T)secure_random_value() << shift;
|
|
|
|
shift += kSecureRandomShift;
|
|
|
|
}
|
|
|
|
|
|
|
|
return random;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-11 06:22:22 +04:00
|
|
|
#endif // __cplusplus
|
|
|
|
|
|
|
|
#endif // KERNEL_UTIL_RANDOM_H
|
|
|
|
|