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