blocxx
SecureRand.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Vintela, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
39 #ifndef BLOCXX_SECURE_RAND_HPP_INCLUDE_GUARD_
40 #define BLOCXX_SECURE_RAND_HPP_INCLUDE_GUARD_
41 
42 
43 #include "blocxx/BLOCXX_config.h"
44 
45 #ifdef BLOCXX_HAVE_OPENSSL
46 // If you don't have SSL, you don't have cryptographically secure random
47 // numbers. Don't try to fall back to a weaker PRNG, as this violates the
48 // security principle of "fail safe".
49 
50 #include "blocxx/Exception.hpp"
51 #include <cstdlib>
52 #include "blocxx/Types.hpp"
53 
54 namespace BLOCXX_NAMESPACE
55 {
56 BLOCXX_DECLARE_EXCEPTION(SecureRand);
57 
58 namespace Secure
59 {
60  namespace Impl
61  {
62  // This template must remain undefined for any type that is not
63  // an unsigned integer type.
64  template <typename T> struct assert_unsigned_integer_type;
65 
66  template <> struct assert_unsigned_integer_type<unsigned char> { };
67  template <> struct assert_unsigned_integer_type<unsigned short> { };
68  template <> struct assert_unsigned_integer_type<unsigned int> { };
69  template <> struct assert_unsigned_integer_type<unsigned long> { };
70  template <> struct assert_unsigned_integer_type<unsigned long long> { };
71 
72  // This template must remain undefined for any type that is not
73  // an integer type.
74  template <typename T> struct assert_integer_type;
75 
76  template <> struct assert_integer_type<char> { };
77 
78  template <> struct assert_integer_type<signed char> { };
79  template <> struct assert_integer_type<short> { };
80  template <> struct assert_integer_type<int> { };
81  template <> struct assert_integer_type<long> { };
82  template <> struct assert_integer_type<long long> { };
83 
84  template <> struct assert_integer_type<unsigned char> { };
85  template <> struct assert_integer_type<unsigned short> { };
86  template <> struct assert_integer_type<unsigned int> { };
87  template <> struct assert_integer_type<unsigned long> { };
88  template <> struct assert_integer_type<unsigned long long> { };
89 
90  // This template must remain undefined for any type that is not
91  // a floating-point type.
92  template <typename T> struct assert_float_type;
93 
94  template <> struct assert_float_type<float> { };
95  template <> struct assert_float_type<double> { };
96  template <> struct assert_float_type<long double> { };
97 
98  template <typename UnsignedInt>
99  UnsignedInt rand_uint_lt(UnsignedInt n);
100 
101  template <typename Integer>
102  Integer rand_range(Integer min_value, Integer max_value);
103 
104  template <typename Real>
105  Real rand_unit_interval();
106  }
107 
113  BLOCXX_COMMON_API void rand_init();
114 
117  BLOCXX_COMMON_API void rand_save_state();
118 
127  BLOCXX_COMMON_API unsigned char * rand(unsigned char * buf, std::size_t n);
128 
137  template <typename UnsignedInt>
138  inline UnsignedInt rand_uint()
139  {
140  Impl::assert_unsigned_integer_type<UnsignedInt> dummy;
141  UnsignedInt n;
142  rand(reinterpret_cast<unsigned char *>(&n), sizeof(n));
143  return n;
144  }
145 
155  template <typename UnsignedInt>
156  inline UnsignedInt rand_uint_lt(UnsignedInt n)
157  {
158  Impl::assert_unsigned_integer_type<UnsignedInt> dummy;
159  return Impl::rand_uint_lt(n);
160  }
161 
172  template <typename Integer>
173  inline Integer rand_range(Integer min_val, Integer max_val)
174  {
175  Impl::assert_integer_type<Integer> dummy;
176  return Impl::rand_range(min_val, max_val);
177  }
178 
188  template <typename Real>
189  inline Real rand_unit_interval()
190  {
191  Impl::assert_float_type<Real> dummy;
192  return Impl::rand_unit_interval<Real>();
193  }
194 
205  pid_t fork_reseed();
206 
207 } // namespace Secure
208 } // namespace BLOCXX_NAMESPACE
209 
210 #endif
211 
212 #endif