Coverage Report

Created: 2023-06-08 06:41

/src/openssl/crypto/bn/bn_rand.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stdio.h>
11
#include <time.h>
12
#include "internal/cryptlib.h"
13
#include "crypto/rand.h"
14
#include "bn_local.h"
15
#include <openssl/rand.h>
16
#include <openssl/sha.h>
17
#include <openssl/evp.h>
18
19
typedef enum bnrand_flag_e {
20
    NORMAL, TESTING, PRIVATE
21
} BNRAND_FLAG;
22
23
static int bnrand(BNRAND_FLAG flag, BIGNUM *rnd, int bits, int top, int bottom,
24
                  unsigned int strength, BN_CTX *ctx)
25
32.7k
{
26
32.7k
    unsigned char *buf = NULL;
27
32.7k
    int b, ret = 0, bit, bytes, mask;
28
32.7k
    OSSL_LIB_CTX *libctx = ossl_bn_get_libctx(ctx);
29
30
32.7k
    if (bits == 0) {
31
0
        if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)
32
0
            goto toosmall;
33
0
        BN_zero(rnd);
34
0
        return 1;
35
0
    }
36
32.7k
    if (bits < 0 || (bits == 1 && top > 0))
37
0
        goto toosmall;
38
39
32.7k
    bytes = (bits + 7) / 8;
40
32.7k
    bit = (bits - 1) % 8;
41
32.7k
    mask = 0xff << (bit + 1);
42
43
32.7k
    buf = OPENSSL_malloc(bytes);
44
32.7k
    if (buf == NULL)
45
0
        goto err;
46
47
    /* make a random number and set the top and bottom bits */
48
32.7k
    b = flag == NORMAL ? RAND_bytes_ex(libctx, buf, bytes, strength)
49
32.7k
                       : RAND_priv_bytes_ex(libctx, buf, bytes, strength);
50
32.7k
    if (b <= 0)
51
0
        goto err;
52
53
32.7k
    if (flag == TESTING) {
54
        /*
55
         * generate patterns that are more likely to trigger BN library bugs
56
         */
57
0
        int i;
58
0
        unsigned char c;
59
60
0
        for (i = 0; i < bytes; i++) {
61
0
            if (RAND_bytes_ex(libctx, &c, 1, strength) <= 0)
62
0
                goto err;
63
0
            if (c >= 128 && i > 0)
64
0
                buf[i] = buf[i - 1];
65
0
            else if (c < 42)
66
0
                buf[i] = 0;
67
0
            else if (c < 84)
68
0
                buf[i] = 255;
69
0
        }
70
0
    }
71
72
32.7k
    if (top >= 0) {
73
26.1k
        if (top) {
74
0
            if (bit == 0) {
75
0
                buf[0] = 1;
76
0
                buf[1] |= 0x80;
77
0
            } else {
78
0
                buf[0] |= (3 << (bit - 1));
79
0
            }
80
26.1k
        } else {
81
26.1k
            buf[0] |= (1 << bit);
82
26.1k
        }
83
26.1k
    }
84
32.7k
    buf[0] &= ~mask;
85
32.7k
    if (bottom)                 /* set bottom bit if requested */
86
0
        buf[bytes - 1] |= 1;
87
32.7k
    if (!BN_bin2bn(buf, bytes, rnd))
88
0
        goto err;
89
32.7k
    ret = 1;
90
32.7k
 err:
91
32.7k
    OPENSSL_clear_free(buf, bytes);
92
32.7k
    bn_check_top(rnd);
93
32.7k
    return ret;
94
95
0
toosmall:
96
0
    ERR_raise(ERR_LIB_BN, BN_R_BITS_TOO_SMALL);
97
0
    return 0;
98
32.7k
}
99
100
int BN_rand_ex(BIGNUM *rnd, int bits, int top, int bottom,
101
               unsigned int strength, BN_CTX *ctx)
102
0
{
103
0
    return bnrand(NORMAL, rnd, bits, top, bottom, strength, ctx);
104
0
}
105
#ifndef FIPS_MODULE
106
int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
107
0
{
108
0
    return bnrand(NORMAL, rnd, bits, top, bottom, 0, NULL);
109
0
}
110
111
int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
112
0
{
113
0
    return bnrand(TESTING, rnd, bits, top, bottom, 0, NULL);
114
0
}
115
#endif
116
117
int BN_priv_rand_ex(BIGNUM *rnd, int bits, int top, int bottom,
118
                    unsigned int strength, BN_CTX *ctx)
119
32.7k
{
120
32.7k
    return bnrand(PRIVATE, rnd, bits, top, bottom, strength, ctx);
121
32.7k
}
122
123
#ifndef FIPS_MODULE
124
int BN_priv_rand(BIGNUM *rnd, int bits, int top, int bottom)
125
0
{
126
0
    return bnrand(PRIVATE, rnd, bits, top, bottom, 0, NULL);
127
0
}
128
#endif
129
130
/* random number r:  0 <= r < range */
131
static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range,
132
                        unsigned int strength, BN_CTX *ctx)
133
0
{
134
0
    int n;
135
0
    int count = 100;
136
137
0
    if (r == NULL) {
138
0
        ERR_raise(ERR_LIB_BN, ERR_R_PASSED_NULL_PARAMETER);
139
0
        return 0;
140
0
    }
141
142
0
    if (range->neg || BN_is_zero(range)) {
143
0
        ERR_raise(ERR_LIB_BN, BN_R_INVALID_RANGE);
144
0
        return 0;
145
0
    }
146
147
0
    n = BN_num_bits(range);     /* n > 0 */
148
149
    /* BN_is_bit_set(range, n - 1) always holds */
150
151
0
    if (n == 1)
152
0
        BN_zero(r);
153
0
    else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
154
        /*
155
         * range = 100..._2, so 3*range (= 11..._2) is exactly one bit longer
156
         * than range
157
         */
158
0
        do {
159
0
            if (!bnrand(flag, r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY,
160
0
                        strength, ctx))
161
0
                return 0;
162
163
            /*
164
             * If r < 3*range, use r := r MOD range (which is either r, r -
165
             * range, or r - 2*range). Otherwise, iterate once more. Since
166
             * 3*range = 11..._2, each iteration succeeds with probability >=
167
             * .75.
168
             */
169
0
            if (BN_cmp(r, range) >= 0) {
170
0
                if (!BN_sub(r, r, range))
171
0
                    return 0;
172
0
                if (BN_cmp(r, range) >= 0)
173
0
                    if (!BN_sub(r, r, range))
174
0
                        return 0;
175
0
            }
176
177
0
            if (!--count) {
178
0
                ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
179
0
                return 0;
180
0
            }
181
182
0
        }
183
0
        while (BN_cmp(r, range) >= 0);
184
0
    } else {
185
0
        do {
186
            /* range = 11..._2  or  range = 101..._2 */
187
0
            if (!bnrand(flag, r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0,
188
0
                        ctx))
189
0
                return 0;
190
191
0
            if (!--count) {
192
0
                ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
193
0
                return 0;
194
0
            }
195
0
        }
196
0
        while (BN_cmp(r, range) >= 0);
197
0
    }
198
199
0
    bn_check_top(r);
200
0
    return 1;
201
0
}
202
203
int BN_rand_range_ex(BIGNUM *r, const BIGNUM *range, unsigned int strength,
204
                     BN_CTX *ctx)
205
0
{
206
0
    return bnrand_range(NORMAL, r, range, strength, ctx);
207
0
}
208
209
#ifndef FIPS_MODULE
210
int BN_rand_range(BIGNUM *r, const BIGNUM *range)
211
0
{
212
0
    return bnrand_range(NORMAL, r, range, 0, NULL);
213
0
}
214
#endif
215
216
int BN_priv_rand_range_ex(BIGNUM *r, const BIGNUM *range, unsigned int strength,
217
                          BN_CTX *ctx)
218
0
{
219
0
    return bnrand_range(PRIVATE, r, range, strength, ctx);
220
0
}
221
222
#ifndef FIPS_MODULE
223
int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range)
224
0
{
225
0
    return bnrand_range(PRIVATE, r, range, 0, NULL);
226
0
}
227
228
# ifndef OPENSSL_NO_DEPRECATED_3_0
229
int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
230
0
{
231
0
    return BN_rand(rnd, bits, top, bottom);
232
0
}
233
234
int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range)
235
0
{
236
0
    return BN_rand_range(r, range);
237
0
}
238
# endif
239
#endif
240
241
/*
242
 * BN_generate_dsa_nonce generates a random number 0 <= out < range. Unlike
243
 * BN_rand_range, it also includes the contents of |priv| and |message| in
244
 * the generation so that an RNG failure isn't fatal as long as |priv|
245
 * remains secret. This is intended for use in DSA and ECDSA where an RNG
246
 * weakness leads directly to private key exposure unless this function is
247
 * used.
248
 */
249
int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
250
                          const BIGNUM *priv, const unsigned char *message,
251
                          size_t message_len, BN_CTX *ctx)
252
0
{
253
0
    EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
254
    /*
255
     * We use 512 bits of random data per iteration to ensure that we have at
256
     * least |range| bits of randomness.
257
     */
258
0
    unsigned char random_bytes[64];
259
0
    unsigned char digest[SHA512_DIGEST_LENGTH];
260
0
    unsigned done, todo;
261
    /* We generate |range|+8 bytes of random output. */
262
0
    const unsigned num_k_bytes = BN_num_bytes(range) + 8;
263
0
    unsigned char private_bytes[96];
264
0
    unsigned char *k_bytes = NULL;
265
0
    int ret = 0;
266
0
    EVP_MD *md = NULL;
267
0
    OSSL_LIB_CTX *libctx = ossl_bn_get_libctx(ctx);
268
269
0
    if (mdctx == NULL)
270
0
        goto err;
271
272
0
    k_bytes = OPENSSL_malloc(num_k_bytes);
273
0
    if (k_bytes == NULL)
274
0
        goto err;
275
276
    /* We copy |priv| into a local buffer to avoid exposing its length. */
277
0
    if (BN_bn2binpad(priv, private_bytes, sizeof(private_bytes)) < 0) {
278
        /*
279
         * No reasonable DSA or ECDSA key should have a private key this
280
         * large and we don't handle this case in order to avoid leaking the
281
         * length of the private key.
282
         */
283
0
        ERR_raise(ERR_LIB_BN, BN_R_PRIVATE_KEY_TOO_LARGE);
284
0
        goto err;
285
0
    }
286
287
0
    md = EVP_MD_fetch(libctx, "SHA512", NULL);
288
0
    if (md == NULL) {
289
0
        ERR_raise(ERR_LIB_BN, BN_R_NO_SUITABLE_DIGEST);
290
0
        goto err;
291
0
    }
292
0
    for (done = 0; done < num_k_bytes;) {
293
0
        if (RAND_priv_bytes_ex(libctx, random_bytes, sizeof(random_bytes), 0) <= 0)
294
0
            goto err;
295
296
0
        if (!EVP_DigestInit_ex(mdctx, md, NULL)
297
0
                || !EVP_DigestUpdate(mdctx, &done, sizeof(done))
298
0
                || !EVP_DigestUpdate(mdctx, private_bytes,
299
0
                                     sizeof(private_bytes))
300
0
                || !EVP_DigestUpdate(mdctx, message, message_len)
301
0
                || !EVP_DigestUpdate(mdctx, random_bytes, sizeof(random_bytes))
302
0
                || !EVP_DigestFinal_ex(mdctx, digest, NULL))
303
0
            goto err;
304
305
0
        todo = num_k_bytes - done;
306
0
        if (todo > SHA512_DIGEST_LENGTH)
307
0
            todo = SHA512_DIGEST_LENGTH;
308
0
        memcpy(k_bytes + done, digest, todo);
309
0
        done += todo;
310
0
    }
311
312
0
    if (!BN_bin2bn(k_bytes, num_k_bytes, out))
313
0
        goto err;
314
0
    if (BN_mod(out, out, range, ctx) != 1)
315
0
        goto err;
316
0
    ret = 1;
317
318
0
 err:
319
0
    EVP_MD_CTX_free(mdctx);
320
0
    EVP_MD_free(md);
321
0
    OPENSSL_clear_free(k_bytes, num_k_bytes);
322
0
    OPENSSL_cleanse(digest, sizeof(digest));
323
0
    OPENSSL_cleanse(random_bytes, sizeof(random_bytes));
324
0
    OPENSSL_cleanse(private_bytes, sizeof(private_bytes));
325
0
    return ret;
326
0
}