blocxx

SecureRand.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
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 // If you don't have SSL, you don't have cryptographically secure random
00047 // numbers.  Don't try to fall back to a weaker PRNG, as this violates the
00048 // security principle of "fail safe".
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       // This template must remain undefined for any type that is not
00063       // an unsigned integer type.
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       // This template must remain undefined for any type that is not
00073       // an integer type.
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       // This template must remain undefined for any type that is not
00091       // a floating-point type.
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 } // namespace Secure
00208 } // namespace BLOCXX_NAMESPACE
00209 
00210 #endif
00211 
00212 #endif