Switch from ossl_rand to DRBG rand
[openssl.git] / crypto / bn / bn_rand.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 "bn_lcl.h"
14 #include <openssl/rand.h>
15 #include <openssl/sha.h>
16
17 static int bnrand(int testing, BIGNUM *rnd, int bits, int top, int bottom)
18 {
19     unsigned char *buf = NULL;
20     int ret = 0, bit, bytes, mask;
21
22     if (bits == 0) {
23         if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)
24             goto toosmall;
25         BN_zero(rnd);
26         return 1;
27     }
28     if (bits < 0 || (bits == 1 && top > 0))
29         goto toosmall;
30
31     bytes = (bits + 7) / 8;
32     bit = (bits - 1) % 8;
33     mask = 0xff << (bit + 1);
34
35     buf = OPENSSL_malloc(bytes);
36     if (buf == NULL) {
37         BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
38         goto err;
39     }
40
41     /* make a random number and set the top and bottom bits */
42     if (RAND_bytes(buf, bytes) <= 0)
43         goto err;
44
45     if (testing) {
46         /*
47          * generate patterns that are more likely to trigger BN library bugs
48          */
49         int i;
50         unsigned char c;
51
52         for (i = 0; i < bytes; i++) {
53             if (RAND_bytes(&c, 1) <= 0)
54                 goto err;
55             if (c >= 128 && i > 0)
56                 buf[i] = buf[i - 1];
57             else if (c < 42)
58                 buf[i] = 0;
59             else if (c < 84)
60                 buf[i] = 255;
61         }
62     }
63
64     if (top >= 0) {
65         if (top) {
66             if (bit == 0) {
67                 buf[0] = 1;
68                 buf[1] |= 0x80;
69             } else {
70                 buf[0] |= (3 << (bit - 1));
71             }
72         } else {
73             buf[0] |= (1 << bit);
74         }
75     }
76     buf[0] &= ~mask;
77     if (bottom)                 /* set bottom bit if requested */
78         buf[bytes - 1] |= 1;
79     if (!BN_bin2bn(buf, bytes, rnd))
80         goto err;
81     ret = 1;
82  err:
83     OPENSSL_clear_free(buf, bytes);
84     bn_check_top(rnd);
85     return (ret);
86
87 toosmall:
88     BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);
89     return 0;
90 }
91
92 int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
93 {
94     return bnrand(0, rnd, bits, top, bottom);
95 }
96
97 int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
98 {
99     return bnrand(1, rnd, bits, top, bottom);
100 }
101
102 /* random number r:  0 <= r < range */
103 int BN_rand_range(BIGNUM *r, const BIGNUM *range)
104 {
105     int n;
106     int count = 100;
107
108     if (range->neg || BN_is_zero(range)) {
109         BNerr(BN_F_BN_RAND_RANGE, BN_R_INVALID_RANGE);
110         return 0;
111     }
112
113     n = BN_num_bits(range);     /* n > 0 */
114
115     /* BN_is_bit_set(range, n - 1) always holds */
116
117     if (n == 1)
118         BN_zero(r);
119     else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
120         /*
121          * range = 100..._2, so 3*range (= 11..._2) is exactly one bit longer
122          * than range
123          */
124         do {
125             if (!BN_rand(r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
126                 return 0;
127             /*
128              * If r < 3*range, use r := r MOD range (which is either r, r -
129              * range, or r - 2*range). Otherwise, iterate once more. Since
130              * 3*range = 11..._2, each iteration succeeds with probability >=
131              * .75.
132              */
133             if (BN_cmp(r, range) >= 0) {
134                 if (!BN_sub(r, r, range))
135                     return 0;
136                 if (BN_cmp(r, range) >= 0)
137                     if (!BN_sub(r, r, range))
138                         return 0;
139             }
140
141             if (!--count) {
142                 BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
143                 return 0;
144             }
145
146         }
147         while (BN_cmp(r, range) >= 0);
148     } else {
149         do {
150             /* range = 11..._2  or  range = 101..._2 */
151             if (!BN_rand(r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
152                 return 0;
153
154             if (!--count) {
155                 BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
156                 return 0;
157             }
158         }
159         while (BN_cmp(r, range) >= 0);
160     }
161
162     bn_check_top(r);
163     return 1;
164 }
165
166 int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
167 {
168     return BN_rand(rnd, bits, top, bottom);
169 }
170
171 int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range)
172 {
173     return BN_rand_range(r, range);
174 }
175
176 /*
177  * BN_generate_dsa_nonce generates a random number 0 <= out < range. Unlike
178  * BN_rand_range, it also includes the contents of |priv| and |message| in
179  * the generation so that an RNG failure isn't fatal as long as |priv|
180  * remains secret. This is intended for use in DSA and ECDSA where an RNG
181  * weakness leads directly to private key exposure unless this function is
182  * used.
183  */
184 int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
185                           const BIGNUM *priv, const unsigned char *message,
186                           size_t message_len, BN_CTX *ctx)
187 {
188     SHA512_CTX sha;
189     /*
190      * We use 512 bits of random data per iteration to ensure that we have at
191      * least |range| bits of randomness.
192      */
193     unsigned char random_bytes[64];
194     unsigned char digest[SHA512_DIGEST_LENGTH];
195     unsigned done, todo;
196     /* We generate |range|+8 bytes of random output. */
197     const unsigned num_k_bytes = BN_num_bytes(range) + 8;
198     unsigned char private_bytes[96];
199     unsigned char *k_bytes;
200     int ret = 0;
201
202     k_bytes = OPENSSL_malloc(num_k_bytes);
203     if (k_bytes == NULL)
204         goto err;
205
206     /* We copy |priv| into a local buffer to avoid exposing its length. */
207     todo = sizeof(priv->d[0]) * priv->top;
208     if (todo > sizeof(private_bytes)) {
209         /*
210          * No reasonable DSA or ECDSA key should have a private key this
211          * large and we don't handle this case in order to avoid leaking the
212          * length of the private key.
213          */
214         BNerr(BN_F_BN_GENERATE_DSA_NONCE, BN_R_PRIVATE_KEY_TOO_LARGE);
215         goto err;
216     }
217     memcpy(private_bytes, priv->d, todo);
218     memset(private_bytes + todo, 0, sizeof(private_bytes) - todo);
219
220     for (done = 0; done < num_k_bytes;) {
221         if (RAND_bytes(random_bytes, sizeof(random_bytes)) != 1)
222             goto err;
223         SHA512_Init(&sha);
224         SHA512_Update(&sha, &done, sizeof(done));
225         SHA512_Update(&sha, private_bytes, sizeof(private_bytes));
226         SHA512_Update(&sha, message, message_len);
227         SHA512_Update(&sha, random_bytes, sizeof(random_bytes));
228         SHA512_Final(digest, &sha);
229
230         todo = num_k_bytes - done;
231         if (todo > SHA512_DIGEST_LENGTH)
232             todo = SHA512_DIGEST_LENGTH;
233         memcpy(k_bytes + done, digest, todo);
234         done += todo;
235     }
236
237     if (!BN_bin2bn(k_bytes, num_k_bytes, out))
238         goto err;
239     if (BN_mod(out, out, range, ctx) != 1)
240         goto err;
241     ret = 1;
242
243  err:
244     OPENSSL_free(k_bytes);
245     OPENSSL_cleanse(private_bytes, sizeof(private_bytes));
246     return ret;
247 }