00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00039 #ifndef BLOCXX_SECURE_RAND_HPP_INCLUDE_GUARD_
00040 #define BLOCXX_SECURE_RAND_HPP_INCLUDE_GUARD_
00041
00042
00043 #include "blocxx/BLOCXX_config.h"
00044
00045 #ifdef BLOCXX_HAVE_OPENSSL
00046
00047
00048
00049
00050 #include "blocxx/Exception.hpp"
00051 #include <cstdlib>
00052 #include "blocxx/Types.hpp"
00053
00054 namespace BLOCXX_NAMESPACE
00055 {
00056 BLOCXX_DECLARE_EXCEPTION(SecureRand);
00057
00058 namespace Secure
00059 {
00060 namespace Impl
00061 {
00062
00063
00064 template <typename T> struct assert_unsigned_integer_type;
00065
00066 template <> struct assert_unsigned_integer_type<unsigned char> { };
00067 template <> struct assert_unsigned_integer_type<unsigned short> { };
00068 template <> struct assert_unsigned_integer_type<unsigned int> { };
00069 template <> struct assert_unsigned_integer_type<unsigned long> { };
00070 template <> struct assert_unsigned_integer_type<unsigned long long> { };
00071
00072
00073
00074 template <typename T> struct assert_integer_type;
00075
00076 template <> struct assert_integer_type<char> { };
00077
00078 template <> struct assert_integer_type<signed char> { };
00079 template <> struct assert_integer_type<short> { };
00080 template <> struct assert_integer_type<int> { };
00081 template <> struct assert_integer_type<long> { };
00082 template <> struct assert_integer_type<long long> { };
00083
00084 template <> struct assert_integer_type<unsigned char> { };
00085 template <> struct assert_integer_type<unsigned short> { };
00086 template <> struct assert_integer_type<unsigned int> { };
00087 template <> struct assert_integer_type<unsigned long> { };
00088 template <> struct assert_integer_type<unsigned long long> { };
00089
00090
00091
00092 template <typename T> struct assert_float_type;
00093
00094 template <> struct assert_float_type<float> { };
00095 template <> struct assert_float_type<double> { };
00096 template <> struct assert_float_type<long double> { };
00097
00098 template <typename UnsignedInt>
00099 UnsignedInt rand_uint_lt(UnsignedInt n);
00100
00101 template <typename Integer>
00102 Integer rand_range(Integer min_value, Integer max_value);
00103
00104 template <typename Real>
00105 Real rand_unit_interval();
00106 }
00107
00113 BLOCXX_COMMON_API void rand_init();
00114
00117 BLOCXX_COMMON_API void rand_save_state();
00118
00127 BLOCXX_COMMON_API unsigned char * rand(unsigned char * buf, std::size_t n);
00128
00137 template <typename UnsignedInt>
00138 inline UnsignedInt rand_uint()
00139 {
00140 Impl::assert_unsigned_integer_type<UnsignedInt> dummy;
00141 UnsignedInt n;
00142 rand(reinterpret_cast<unsigned char *>(&n), sizeof(n));
00143 return n;
00144 }
00145
00155 template <typename UnsignedInt>
00156 inline UnsignedInt rand_uint_lt(UnsignedInt n)
00157 {
00158 Impl::assert_unsigned_integer_type<UnsignedInt> dummy;
00159 return Impl::rand_uint_lt(n);
00160 }
00161
00172 template <typename Integer>
00173 inline Integer rand_range(Integer min_val, Integer max_val)
00174 {
00175 Impl::assert_integer_type<Integer> dummy;
00176 return Impl::rand_range(min_val, max_val);
00177 }
00178
00188 template <typename Real>
00189 inline Real rand_unit_interval()
00190 {
00191 Impl::assert_float_type<Real> dummy;
00192 return Impl::rand_unit_interval<Real>();
00193 }
00194
00205 pid_t fork_reseed();
00206
00207 }
00208 }
00209
00210 #endif
00211
00212 #endif