BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_randomdevice.h
Go to the documentation of this file.
1/// @file bdlb_randomdevice.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_randomdevice.h -*-C++-*-
8#ifndef INCLUDED_BDLB_RANDOMDEVICE
9#define INCLUDED_BDLB_RANDOMDEVICE
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_randomdevice bdlb_randomdevice
15/// @brief Provide a common interface to a system's random number generator.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_randomdevice
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_randomdevice-purpose"> Purpose</a>
25/// * <a href="#bdlb_randomdevice-classes"> Classes </a>
26/// * <a href="#bdlb_randomdevice-description"> Description </a>
27/// * <a href="#bdlb_randomdevice-usage"> Usage </a>
28/// * <a href="#bdlb_randomdevice-example-1-seeding-the-random-number-generator"> Example 1: Seeding the Random-Number Generator </a>
29///
30/// # Purpose {#bdlb_randomdevice-purpose}
31/// Provide a common interface to a system's random number generator.
32///
33/// # Classes {#bdlb_randomdevice-classes}
34///
35/// - bdlb::RandomDevice: namespace for system specific random-number generators.
36///
37/// @see
38///
39/// # Description {#bdlb_randomdevice-description}
40/// This component provides a namespace, `bdlb::RandomDevice`, for
41/// a suite of functions used to generate random seeds from platform-dependent
42/// random number generators. Two variants are provided: one which may block,
43/// but which potentially samples from a stronger distribution, and another
44/// which does not block, but which potentially should not be used for
45/// cryptography. The strength of these random numbers and the performance of
46/// these calls is strongly dependent on the underlying system. On UNIX-like
47/// platforms `getRandomBytes()` reads from `/dev/random` and
48/// `getRandomBytesNonBlocking()` reads from `/dev/urandom`. On Windows both
49/// methods use `CryptGenRandom`.
50///
51/// Note that (at least on UNIX-like systems) it is not appropriate to call
52/// these functions repeatedly to generate many random numbers. A call to
53/// `getRandomBytes()` can block if available entropy is exhausted, and both
54/// `getRandomBytes()` and `getRandomBytesNonBlocking()` open and close their
55/// respective devices on each call. Instead, these functions should be used
56/// for seeding pseudo-random random number generators. (E.g., promiscuous use
57/// of `getRandomBytes()` appears to have caused the WP in `{DRQS 92851043}`.)
58///
59/// There is discussion about which of `/dev/random` or `/dev/urandom` is best,
60/// especially on modern Linux systems, while `man` pages on other UNIX systems
61/// continue to make claims about `/dev/random` providing "more secure" numbers
62/// than `/dev/urandom`. See `{http://www.2uo.de/myths-about-urandom/}`, for
63/// example. This component deliberately takes no stand on the issue, making
64/// both available and leaving it for users to decide which to use.
65///
66/// ## Usage {#bdlb_randomdevice-usage}
67///
68///
69/// This section illustrates intended use of this component.
70///
71/// ### Example 1: Seeding the Random-Number Generator {#bdlb_randomdevice-example-1-seeding-the-random-number-generator}
72///
73///
74/// System-provided random-number generators generally must be initialized with
75/// a seed value from which they go on to produce their stream of pseudo-random
76/// numbers. We can use `RandomDevice` to provide such a seed.
77///
78/// First, we obtain the results of invoking the random-number generator without
79/// having seeded it:
80/// @code
81/// int unseededR1 = rand();
82/// int unseededR2 = rand();
83/// @endcode
84/// Then, we obtain a random number:
85/// @code
86/// int seed = 0;
87/// int status = bdlb::RandomDevice::getRandomBytes(
88/// reinterpret_cast<unsigned char *>(&seed), sizeof(seed));
89/// assert(0 == status);
90/// assert(0 != seed); // This will fail every few billion attempts...
91/// @endcode
92/// Next, we seed the random-number generator with our seed:
93/// @code
94/// srand(seed);
95/// @endcode
96/// Finally, we observe that we obtain different numbers:
97/// @code
98/// assert(unseededR1 != rand());
99/// assert(unseededR2 != rand());
100/// @endcode
101/// @}
102/** @} */
103/** @} */
104
105/** @addtogroup bdl
106 * @{
107 */
108/** @addtogroup bdlb
109 * @{
110 */
111/** @addtogroup bdlb_randomdevice
112 * @{
113 */
114
115#include <bdlscm_version.h>
116
117#include <bsls_types.h>
118
119
120namespace bdlb {
121
122 // ===================
123 // struct RandomDevice
124 // ===================
125
126/// This `struct` provides a namespace for a suite of functions used for
127/// acquiring random numbers from the system.
129
130 // TYPES
131 typedef bsls::Types::size_type size_t; // for brevity of name
132
133 // CLASS METHODS
134
135 /// Read the specified `numBytes` from the system random number
136 /// generator into the specified `buffer`. Returns 0 on success,
137 /// non-zero otherwise. Note that this method may block if called
138 /// repeatedly or if `numBytes` is high.
139 static int getRandomBytes(unsigned char *buffer, size_t numBytes);
140
141 /// Read the specified `numBytes` from the system non-blocking random
142 /// number generator into the specified `buffer`. Returns 0 on success,
143 /// non-zero otherwise. Note that the cryptographic strength of samples
144 /// drawn from this pool may or may not be lower than that of those
145 /// drawn from the blocking pool, and may vary by platform.
146 static int getRandomBytesNonBlocking(unsigned char *buffer,
147 size_t numBytes);
148};
149
150} // close package namespace
151
152
153#endif
154
155// ----------------------------------------------------------------------------
156// Copyright 2014 Bloomberg Finance L.P.
157//
158// Licensed under the Apache License, Version 2.0 (the "License");
159// you may not use this file except in compliance with the License.
160// You may obtain a copy of the License at
161//
162// http://www.apache.org/licenses/LICENSE-2.0
163//
164// Unless required by applicable law or agreed to in writing, software
165// distributed under the License is distributed on an "AS IS" BASIS,
166// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
167// See the License for the specific language governing permissions and
168// limitations under the License.
169// ----------------------------- END-OF-FILE ----------------------------------
170
171/** @} */
172/** @} */
173/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_randomdevice.h:128
static int getRandomBytes(unsigned char *buffer, size_t numBytes)
bsls::Types::size_type size_t
Definition bdlb_randomdevice.h:131
static int getRandomBytesNonBlocking(unsigned char *buffer, size_t numBytes)
std::size_t size_type
Definition bsls_types.h:124