2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl Service Runtime. Secure RNG implementation.
36 #include "native_client/service_runtime/nacl_secure_random.h"
38 #include "native_client/service_runtime/nacl_log.h"
40 uint32_t NaClSecureRngGenUint32(struct NaClSecureRng
*self
)
44 rv
= (0xff & NaClSecureRngGenByte(self
));
46 rv
|= (0xff & NaClSecureRngGenByte(self
));
48 rv
|= (0xff & NaClSecureRngGenByte(self
));
50 rv
|= (0xff & NaClSecureRngGenByte(self
));
54 void NaClSecureRngGenBytes(struct NaClSecureRng
*self
,
60 for (i
= 0; i
< nbytes
; ++i
) {
61 buf
[i
] = NaClSecureRngGenByte(self
);
65 uint32_t NaClSecureRngUniform(struct NaClSecureRng
*self
,
72 * First, is range_max a power of 2? If so, we can just keep the
75 if (0 == (range_max
& (range_max
- 1))) {
76 return NaClSecureRngGenUint32(self
) & (range_max
- 1);
78 /* post condition: range_max is not a power of 2 */
81 * Generator output range is uniform in [0, ~(uint32_t) 0] or [0, 2^{32}).
83 * NB: Number of integer values in the range [0, n) is n,
84 * and number of integer values in the range [m, n) is n-m,
85 * (n and m are integers).
87 bias
= ((~(uint32_t) 0) % range_max
) + 1;
89 * bias = (2^{32}-1 \bmod range_max) + 1
91 * 1 <= bias <= range_max.
93 * Aside: bias = range_max is impossible. Here's why:
95 * Suppose bias = range_max. Then,
97 * 2^{32}-1 \bmod range_max + 1 = range_max
98 * 2^{32}-1 \bmod range_max = range_max - 1
99 * 2^{32}-1 = k range_max + range_max - 1 for some k \in Z.
100 * 2^{32}-1 = (k+1) range_max - 1
101 * 2^{32} = (k+1) range_max
103 * Since range_max evenly divides the right hand side, it must
104 * divide the left. However, the only divisors of 2^{32} are powers
105 * of 2. Thus, range_max must also be a power of 2. =><=
107 * Therefore, bias \ne range_max. QED.
109 * post condition: 1 <= bias < range_max.
114 v
= NaClSecureRngGenUint32(self
);
115 /* v is uniform in [0, 2^{32}) */
118 * v is uniform in [bias, 2^{32}).
120 * the number of integers in the half-open interval is
122 * 2^{32} - bias = 2^{32} - ((2^{32}-1 \bmod range_max + 1))
123 * = 2^{32}-1 - (2^{32}-1 \bmod range_max)
124 * = range_max * \lfloor (2^{32}-1)/range_max \rfloor
126 * and range_max clearly divides the size of the range.
128 * The mapping v \rightarrow v % range_max takes values from [bias,
129 * 2^{32}) to [0, range_max). Each integer in the range interval
130 * [0, range_max) will have exactly \lfloor (2^{32}-1)/range_max
131 * \rfloor preimages from the domain interval.
133 * Therefore, v % range_max is uniform over [0, range_max). QED.
136 return v
% range_max
;