Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/bn/random.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected])
2 // Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/bn.h>
6
7 #include <limits.h>
8 #include <string.h>
9
10 #include <openssl/err.h>
11 #include <openssl/rand.h>
12 #include <openssl/type_check.h>
13
14 #include "internal.h"
15 #include "../../internal.h"
16
17
18 int BN_rand(BIGNUM *rnd, int bits, int top, int bottom) {
19 if (rnd == NULL) {
20 return 0;
21 }
22
23 if (top != BN_RAND_TOP_ANY && top != BN_RAND_TOP_ONE &&
24 top != BN_RAND_TOP_TWO) {
25 OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
26 return 0;
27 }
28
29 if (bottom != BN_RAND_BOTTOM_ANY && bottom != BN_RAND_BOTTOM_ODD) {
30 OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
31 return 0;
32 }
33
34 if (bits == 0) {
35 BN_zero(rnd);
36 return 1;
37 }
38
39 if (bits > INT_MAX - (BN_BITS2 - 1)) {
40 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
41 return 0;
42 }
43
44 int words = (bits + BN_BITS2 - 1) / BN_BITS2;
45 int bit = (bits - 1) % BN_BITS2;
46 const BN_ULONG kOne = 1;
47 const BN_ULONG kThree = 3;
48 BN_ULONG mask = bit < BN_BITS2 - 1 ? (kOne << (bit + 1)) - 1 : BN_MASK2;
49 if (!bn_wexpand(rnd, words)) {
50 return 0;
51 }
52
53 // |RAND_bytes| calls within the fipsmodule should be wrapped with state lock
54 // functions to avoid updating the service indicator with the DRBG functions.
55 FIPS_service_indicator_lock_state();
56 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes((uint8_t *)rnd->d, words * sizeof(BN_ULONG)));
57 FIPS_service_indicator_unlock_state();
58
59 rnd->d[words - 1] &= mask;
60 if (top != BN_RAND_TOP_ANY) {
61 if (top == BN_RAND_TOP_TWO && bits > 1) {
62 if (bit == 0) {
63 rnd->d[words - 1] |= 1;
64 rnd->d[words - 2] |= kOne << (BN_BITS2 - 1);
65 } else {
66 rnd->d[words - 1] |= kThree << (bit - 1);
67 }
68 } else {
69 rnd->d[words - 1] |= kOne << bit;
70 }
71 }
72 if (bottom == BN_RAND_BOTTOM_ODD) {
73 rnd->d[0] |= 1;
74 }
75
76 rnd->neg = 0;
77 rnd->width = words;
78 return 1;
79 }
80
81 int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom) {
82 return BN_rand(rnd, bits, top, bottom);
83 }
84
85 // bn_less_than_word_mask returns a mask of all ones if the number represented
86 // by |len| words at |a| is less than |b| and zero otherwise. It performs this
87 // computation in time independent of the value of |a|. |b| is assumed public.
88 static crypto_word_t bn_less_than_word_mask(const BN_ULONG *a, size_t len,
89 BN_ULONG b) {
90 if (b == 0) {
91 return CONSTTIME_FALSE_W;
92 }
93 if (len == 0) {
94 return CONSTTIME_TRUE_W;
95 }
96
97 // |a| < |b| iff a[1..len-1] are all zero and a[0] < b.
98 OPENSSL_STATIC_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
99 crypto_word_t_is_too_small)
100 crypto_word_t mask = 0;
101 for (size_t i = 1; i < len; i++) {
102 mask |= a[i];
103 }
104 // |mask| is now zero iff a[1..len-1] are all zero.
105 mask = constant_time_is_zero_w(mask);
106 mask &= constant_time_lt_w(a[0], b);
107 return mask;
108 }
109
110 int bn_in_range_words(const BN_ULONG *a, BN_ULONG min_inclusive,
111 const BN_ULONG *max_exclusive, size_t len) {
112 crypto_word_t mask = ~bn_less_than_word_mask(a, len, min_inclusive);
113 return mask & bn_less_than_words(a, max_exclusive, len);
114 }
115
116 static int bn_range_to_mask(size_t *out_words, BN_ULONG *out_mask,
117 size_t min_inclusive, const BN_ULONG *max_exclusive,
118 size_t len) {
119 // The magnitude of |max_exclusive| is assumed public.
120 size_t words = len;
121 while (words > 0 && max_exclusive[words - 1] == 0) {
122 words--;
123 }
124 if (words == 0 ||
125 (words == 1 && max_exclusive[0] <= min_inclusive)) {
126 OPENSSL_PUT_ERROR(BN, BN_R_INVALID_RANGE);
127 return 0;
128 }
129 BN_ULONG mask = max_exclusive[words - 1];
130 // This sets all bits in |mask| below the most significant bit.
131 mask |= mask >> 1;
132 mask |= mask >> 2;
133 mask |= mask >> 4;
134 mask |= mask >> 8;
135 mask |= mask >> 16;
136 #if defined(OPENSSL_64_BIT)
137 mask |= mask >> 32;
138 #endif
139
140 *out_words = words;
141 *out_mask = mask;
142 return 1;
143 }
144
145 int bn_rand_range_words(BN_ULONG *out, BN_ULONG min_inclusive,
146 const BN_ULONG *max_exclusive, size_t len,
147 const uint8_t additional_data[RAND_PRED_RESISTANCE_LEN]) {
148 // This function implements the equivalent of steps 4 through 7 of FIPS 186-4
149 // appendices B.4.2 and B.5.2. When called in those contexts, |max_exclusive|
150 // is n and |min_inclusive| is one.
151
152 // |RAND_bytes| calls within the fipsmodule should be wrapped with state lock
153 // functions to avoid updating the service indicator with the DRBG functions.
154 FIPS_service_indicator_lock_state();
155 int ret = 0;
156
157 // Compute the bit length of |max_exclusive| (step 1), in terms of a number of
158 // |words| worth of entropy to fill and a mask of bits to clear in the top
159 // word.
160 size_t words;
161 BN_ULONG mask;
162 if (!bn_range_to_mask(&words, &mask, min_inclusive, max_exclusive, len)) {
163 goto end;
164 }
165
166 // Fill any unused words with zero.
167 OPENSSL_memset(out + words, 0, (len - words) * sizeof(BN_ULONG));
168
169 unsigned count = 100;
170 do {
171 if (!--count) {
172 OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_ITERATIONS);
173 goto end;
174 }
175
176 // Steps 4 and 5. Use |words| and |mask| together to obtain a string of N
177 // bits, where N is the bit length of |max_exclusive|.
178 RAND_bytes_with_user_prediction_resistance((uint8_t *)out, words * sizeof(BN_ULONG),
179 additional_data);
180 out[words - 1] &= mask;
181
182 // If out >= max_exclusive or out < min_inclusive, retry. This implements
183 // the equivalent of steps 6 and 7 without leaking the value of |out|. The
184 // result of this comparison may be treated as public. It only reveals how
185 // many attempts were needed before we found a value in range. This is
186 // independent of the final secret output, and has a distribution that
187 // depends only on |min_inclusive| and |max_exclusive|, both of which are
188 // public.
189 } while (!constant_time_declassify_int(
190 bn_in_range_words(out, min_inclusive, max_exclusive, words)));
191
192 ret = 1;
193
194 end:
195 FIPS_service_indicator_unlock_state();
196 return ret;
197 }
198
199 int BN_rand_range_ex(BIGNUM *r, BN_ULONG min_inclusive,
200 const BIGNUM *max_exclusive) {
201 static const uint8_t kDefaultAdditionalData[RAND_PRED_RESISTANCE_LEN] = {0};
202 if (!bn_wexpand(r, max_exclusive->width) ||
203 !bn_rand_range_words(r->d, min_inclusive, max_exclusive->d,
204 max_exclusive->width, kDefaultAdditionalData)) {
205 return 0;
206 }
207
208 r->neg = 0;
209 r->width = max_exclusive->width;
210 return 1;
211 }
212
213 int bn_rand_secret_range(BIGNUM *r, int *out_is_uniform, BN_ULONG min_inclusive,
214 const BIGNUM *max_exclusive) {
215 // |RAND_bytes| calls within the fipsmodule should be wrapped with state lock
216 // functions to avoid updating the service indicator with the DRBG functions.
217 FIPS_service_indicator_lock_state();
218 int ret = 0;
219
220 size_t words;
221 BN_ULONG mask;
222 if (!bn_range_to_mask(&words, &mask, min_inclusive, max_exclusive->d,
223 max_exclusive->width) ||
224 !bn_wexpand(r, words)) {
225 goto end;
226 }
227
228 assert(words > 0);
229 assert(mask != 0);
230 // The range must be large enough for bit tricks to fix invalid values.
231 if (words == 1 && min_inclusive > mask >> 1) {
232 OPENSSL_PUT_ERROR(BN, BN_R_INVALID_RANGE);
233 goto end;
234 }
235
236 // Select a uniform random number with num_bits(max_exclusive) bits.
237 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes((uint8_t *)r->d, words * sizeof(BN_ULONG)));
238 r->d[words - 1] &= mask;
239
240 // Check, in constant-time, if the value is in range.
241 *out_is_uniform =
242 bn_in_range_words(r->d, min_inclusive, max_exclusive->d, words);
243 crypto_word_t in_range = *out_is_uniform;
244 in_range = 0 - in_range;
245
246 // If the value is not in range, force it to be in range.
247 r->d[0] |= constant_time_select_w(in_range, 0, min_inclusive);
248 r->d[words - 1] &= constant_time_select_w(in_range, BN_MASK2, mask >> 1);
249 declassify_assert(
250 bn_in_range_words(r->d, min_inclusive, max_exclusive->d, words));
251
252 r->neg = 0;
253 r->width = (int)words;
254 ret = 1;
255
256 end:
257 FIPS_service_indicator_unlock_state();
258 return ret;
259 }
260
261 int BN_rand_range(BIGNUM *r, const BIGNUM *range) {
262 return BN_rand_range_ex(r, 0, range);
263 }
264
265 int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range) {
266 return BN_rand_range(r, range);
267 }